Subversion Repositories Kolibri OS

Rev

Rev 1645 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ; version: 0.5
  2. ; last update:  07/10/2010
  3. ; written by:   Marat Zakiyanov aka Mario79, aka Mario
  4. ; changes:      reducing the size of the binary code,
  5. ;               program uses far less memory while running
  6. ;               (>0x7000, the old version used >0x100000),
  7. ;               process only net event at start with parameter
  8. ;-----------------------------------------------------------
  9. ; version 0.3 -0.4
  10. ; written by:   CleverMouse
  11. ;
  12. ;-----------------------------------------------------------
  13. ; wget 0.2 by barsuk
  14. ; based on Menuet Httpc
  15.  
  16.  
  17. ; Enabling debugging puts stuff to the debug board
  18. DEBUGGING_ENABLED       equ 1
  19. DEBUGGING_DISABLED      equ 0
  20. DEBUGGING_STATE         equ DEBUGGING_ENABLED
  21.  
  22. use32
  23.         org     0x0
  24.         db 'MENUET01'   ; header
  25.         dd 0x01         ; header version
  26.         dd START        ; entry point
  27.         dd IM_END       ; image size
  28.         dd I_END        ;0x100000 ; required memory
  29.         dd stacktop     ; esp
  30.         dd params       ; I_PARAM
  31.         dd 0x0          ; I_Path
  32.  
  33. include 'lang.inc'
  34. include '../../../macros.inc'
  35. include "../../../proc32.inc"
  36. include "dll.inc"
  37. include "debug.inc"
  38.  
  39. URLMAXLEN       equ 256 ; maximum length of url string
  40.  
  41. primary_buffer_size     equ 4096
  42.  
  43. ; Memory usage
  44. ; webpage headers at buf_headers
  45.  
  46. START:  ; start of execution
  47. ;dps    <"Program started",13,10>
  48. ; prepare webAddr area 
  49.         mov     al,' '
  50.         mov     edi,webAddr
  51.         mov     ecx,URLMAXLEN
  52.         cld
  53.         rep     stosb
  54.         xor     eax,eax
  55.         stosb
  56. ; prepare document area
  57.         mov     al,'/'
  58.         mov     edi,document
  59.         cld
  60.         stosb
  61.         mov     al,' '
  62.         mov     ecx,URLMAXLEN-1
  63.         rep     stosb
  64.  
  65. ; create local heap
  66.         mcall   68,11
  67.  
  68.         call    load_settings
  69.         cmp     [params],byte 0
  70.         jz      prepare_event   ;red
  71.  
  72.         mcall   40,10000000b ; only net event!!!
  73.  
  74. ; we have an url
  75.         mov     edi,document_user
  76.         mov     al,' '
  77.         mov     ecx,URLMAXLEN
  78.         rep     stosb
  79.        
  80.         mov     esi,params
  81.         mov     edi,document_user
  82.  
  83. .copy_param:
  84.         mov     al,[esi]
  85.         cmp     al,0
  86.         jz      .done
  87.  
  88.         cmp     al,' '
  89.         jz      .done_inc
  90.  
  91.         mov     [edi],al
  92.         inc     esi
  93.         inc     edi
  94.         jmp     .copy_param
  95.  
  96. .done_inc:
  97. ; url is followed by shared memory name.
  98.         inc     esi
  99. .done:
  100.         mov     [shared_name],esi
  101.  
  102.         mov     ah,22   ; strange way to tell that socket should be opened...
  103.         call    socket_commands
  104.  
  105.         jmp     still
  106.  
  107. prepare_event:
  108. ; Report events
  109. ; Stack 8 + defaults
  110.         mcall   40,10000111b
  111.  
  112. red:    ; redraw
  113.         call    draw_window
  114.  
  115. still:
  116.         mcall   23,1    ; wait here for event
  117.         cmp     eax,1   ; redraw request ?
  118.         je      red
  119.  
  120.         cmp     eax,2   ; key in buffer ?
  121.         je      key
  122.  
  123.         cmp     eax,3   ; button in buffer ?
  124.         je      button
  125.  
  126. ; Get the web page data from the remote server
  127.         call    read_incoming_data
  128.         mov     eax,[status]
  129.         mov     [prev_status],eax
  130.         mcall   53,6,[socket]
  131.         mov     [status],eax
  132.  
  133.         cmp     [prev_status],4
  134.         jge     no_send
  135.  
  136.         cmp     [status],4
  137.         jne     no_send
  138.  
  139.         mov     [onoff],1
  140.         call    send_request
  141.  
  142. no_send:
  143.         call    print_status
  144.  
  145.         cmp     [prev_status],4
  146.         jne     no_close
  147.         cmp     [status],4      ; connection closed by server
  148.         jbe     no_close        ; respond to connection close command
  149. ; draw page
  150.         call    read_incoming_data
  151.         mcall   53,8,[socket]
  152.         call    draw_page
  153.         mov     [onoff],0
  154.  
  155. no_close:
  156.         jmp     still
  157.  
  158. key:    ; key
  159.         mcall   2       ; just read it and ignore
  160.         shr     eax,8
  161.         cmp     eax,184
  162.         jne     no_down
  163.         cmp     [display_from],25
  164.         jb      no_down
  165.         sub     [display_from],25
  166.         call    display_page
  167.  
  168. no_down:
  169.         cmp     eax,183
  170.         jne     no_up
  171.         add     [display_from],25
  172.         call    display_page
  173.  
  174. no_up:
  175.         jmp     still
  176.  
  177. button: ; button
  178. ;dps    <"Button pressed",13,10>
  179.         mcall   17      ; get id
  180.         cmp     ah,26
  181.         je      save
  182.         cmp     ah,1    ; button id=1 ?
  183.         jne     noclose
  184. ;       dps     "Closing socket before exit... "
  185.  
  186. close_end_exit:
  187.  
  188. ;dpd    eax
  189. ;dps    <13,10>
  190.  
  191. exit:
  192.         or      eax,-1  ; close this program
  193.         mcall
  194.  
  195. save:
  196. dps     "saving"
  197. newline
  198.         mcall   70,fileinfo
  199. ;pregs
  200.         jmp     still
  201.  
  202. noclose:
  203.         cmp     ah,31
  204.         jne     noup
  205.         sub     [display_from],20
  206.         call    display_page
  207.         jmp     still
  208.  
  209. noup:
  210.         cmp     ah,32
  211.         jne     nodown
  212.         add     [display_from],20
  213.         call    display_page
  214.         jmp     still
  215.  
  216. nodown:
  217.         cmp     ah,10   ; Enter url
  218.         jne     nourl
  219.  
  220.         mov     [addr],dword document_user
  221.         mov     [ya],dword 38
  222.         mov     [len],dword URLMAXLEN
  223.  
  224.         mov     ecx,URLMAXLEN
  225.         mov     edi,[addr]
  226.         mov     al,' '
  227.         rep     stosb
  228.  
  229.         call    print_text
  230.  
  231.         mov     edi,[addr]
  232.  
  233. f11:
  234.         mcall   10
  235.         cmp     eax,2   ; key?
  236.         jz      fbu
  237.         jmp     still
  238.  
  239. fbu:
  240.         mcall   2       ; get key
  241.         shr     eax,8
  242.         cmp     eax,8
  243.         jnz     nobs
  244.         cmp     edi,[addr]
  245.         jz      f11
  246.         sub     edi,1
  247.         mov     [edi],byte ' '
  248.         call    print_text
  249.         jmp     f11
  250.  
  251. nobs:
  252.         cmp     eax,10
  253.         je      retkey
  254.         cmp     eax,13
  255.         je      retkey
  256.  
  257.         cmp     eax,31
  258.         jbe     f11
  259.  
  260. ; Removed in v0.4
  261. ;       cmp     eax,95
  262. ;       jb      keyok
  263. ;       sub     eax,32
  264.  
  265. keyok:
  266.         mov     [edi],al
  267.         call    print_text
  268.         add     edi,1
  269.         mov     esi,[addr]
  270.         add     esi,URLMAXLEN
  271.         cmp     esi,edi
  272.         jnz     f11
  273.         jmp     still
  274.  
  275. retkey:
  276.         mov     ah,22   ; start load
  277.  
  278. nourl:
  279.         call    socket_commands ; opens or closes the connection
  280.         jmp     still
  281.  
  282. ;****************************************************************************
  283. ;    Function
  284. ;       send_request
  285. ;
  286. ;   Description
  287. ;       Transmits the GET request to the server.
  288. ;       This is done as GET then URL then HTTP/1.1',13,10,13,10 in 3 packets
  289. ;
  290. ;****************************************************************************
  291. send_request:
  292.         pusha
  293.         mov     esi,string0
  294.         mov     edi,request
  295.         movsd
  296. ; If proxy is used, make absolute URI - prepend http://<host>
  297.         cmp     [proxyAddr],byte 0
  298.         jz      .noproxy
  299.         mov     dword [edi],'http'
  300.         mov     [edi+4],byte ':'
  301.         mov     [edi+5],word '//'
  302.         add     edi,7
  303.         mov     esi,webAddr
  304.  
  305. .copy_host_loop:
  306.         lodsb
  307.         cmp     al,' '
  308.         jz      .noproxy
  309.         stosb
  310.         jmp     .copy_host_loop
  311.  
  312. .noproxy:
  313.         xor     edx,edx ; 0
  314.  
  315. .next_edx:
  316. ; Determine the length of the url to send in the GET request
  317.         mov     al,[edx+document]
  318.         cmp     al,' '
  319.         je      .document_done
  320.         mov     [edi],al
  321.         inc     edi
  322.         inc     edx
  323.         jmp     .next_edx
  324.  
  325. .document_done:
  326.         mov     esi,stringh
  327.         mov     ecx,stringh_end-stringh
  328.         rep     movsb
  329.         xor     edx,edx ; 0
  330.  
  331. .webaddr_next:
  332.         mov     al,[webAddr + edx]
  333.         cmp     al,' '
  334.         je      .webaddr_done
  335.         mov     [edi],al
  336.         inc     edi
  337.         inc     edx
  338.         jmp     .webaddr_next
  339.  
  340. .webaddr_done:
  341.         cmp     [proxyUser],byte 0
  342.         jz      @f
  343.         call    append_proxy_auth_header
  344. @@:
  345.         mov     esi,connclose
  346.         mov     ecx,connclose_end-connclose
  347.         rep     movsb
  348.  
  349.         pusha  
  350.         mov     eax,63
  351.         mov     ebx,1
  352.         mov     edx,request
  353. @@:
  354.         mov     cl,[edx]
  355.         cmp     edx,edi
  356.         jz      @f
  357.         mcall
  358.         inc     edx
  359.         jmp     @b
  360. @@:
  361.         popa
  362.  
  363.         mov     edx,edi
  364.         sub     edx,request
  365. ;;;;now write \r\nConnection: Close \r\n\r\n
  366.         mcall   53,7,[socket],,request  ;' HTTP/1.1 .. '
  367.         popa
  368.         ret
  369.  
  370. ;****************************************************************************
  371. ;    Function
  372. ;       print_status
  373. ;
  374. ;   Description
  375. ;       displays the socket/data received status information
  376. ;
  377. ;****************************************************************************
  378. print_status:
  379.         pusha
  380.         mcall   26,9
  381.         cmp     eax,[nextupdate]
  382.         jb      status_return
  383.  
  384.         add     eax,25
  385.         mov     [nextupdate],eax
  386.  
  387.         mov     ecx,[winys]
  388.         shl     ecx,16
  389.         add     ecx,-18*65536+10
  390.         mcall   13,<5,100>,,0xffffff
  391.  
  392.         mov     edx,12*65536-18
  393.         add     edx,[winys]
  394.         xor     esi,esi
  395.         mcall   47,<3,0>,[status],,
  396.  
  397.         mov     edx,40*65536-18
  398.         add     edx,[winys]
  399.         mcall   ,<6,0>,[pos]
  400.  
  401. status_return:
  402.         popa
  403.         ret
  404.  
  405. ;****************************************************************************
  406. ;    Function
  407. ;       read_incoming_data
  408. ;
  409. ;   Description
  410. ;       receive the web page from the server, storing it without processing
  411. ;
  412. ;****************************************************************************
  413. read_incoming_data:
  414.         cmp     [onoff],1
  415.         je      rid
  416.         ret
  417.  
  418. rid:
  419.         push    esi
  420.         push    edi
  421. dps     "rid"
  422. newline
  423.  
  424. newbyteread:
  425.         ;call   print_status
  426.         mcall   53,2,[socket]
  427.         cmp     eax,0
  428.         je      no_more_data
  429.  
  430.         mcall   53,11,[socket],primary_buf,primary_buffer_size
  431.        
  432. ;dps    "part "
  433. ;dph    eax
  434. ;newline
  435.         mov     edi,[pos]
  436.         add     [pos],eax
  437.         push    eax
  438.         mcall   68,20,[pos],[buf_ptr]
  439.         mov     [buf_ptr],eax
  440.         add     edi,eax
  441.         mov     esi,primary_buf
  442.         pop     ecx     ; number of recently read bytes
  443.         lea     edx,[ecx - 3]
  444.         rep     movsb
  445.        
  446. no_more_data:
  447.         mcall   53,6,[socket]
  448.         cmp     eax,4
  449.         jne     no_more_data.finish
  450.         jmp     newbyteread
  451.  
  452. .finish:
  453. ;dps    "finish "
  454. ;pregs
  455.         call    parse_result
  456.         mov     ecx,[shared_name]
  457.         cmp     [ecx],byte 0
  458.         jnz     save_in_shared
  459.  
  460.         mcall   70,fileinfo
  461. ;dps    "saving "
  462. ;pregs
  463. ;       jmp     close_end_exit
  464.         pop     edi
  465.         pop     esi
  466. ; if called from command line, then exit
  467.         cmp     [params],byte 0
  468.         jnz     exit
  469.         ret
  470.        
  471. save_in_shared:
  472.         mov     esi,1   ; SHM_OPEN+SHM_WRITE
  473.         mcall   68,22
  474.         test    eax,eax
  475.         jz      save_in_shared_done
  476.  
  477.         sub     edx,4
  478.         jbe     save_in_shared_done
  479.  
  480.         mov     ecx,[final_size]
  481.         cmp     ecx,edx
  482.         jb      @f
  483.  
  484.         mov     ecx,edx
  485. @@:
  486.         mov     [eax],ecx
  487.         lea     edi,[eax+4]
  488.         mov     esi,[final_buffer]
  489.         mov     edx,ecx
  490.         shr     ecx,2
  491.         rep     movsd
  492.         mov     ecx,edx
  493.         and     ecx,3
  494.         rep     movsb
  495.  
  496. save_in_shared_done:
  497.         pop     edi
  498.         pop     esi
  499.         jmp     exit
  500.        
  501. ; this function cuts header, and removes chunk sizes if doc is chunked
  502. ; in: buf_ptr, pos; out: buf_ptr, pos.
  503.        
  504. parse_result:
  505. ; close socket
  506.         mcall   53,8,[socket]
  507.        
  508. dps     "close  socket: "
  509. dph     eax
  510. newline
  511.         mov     edi,[buf_ptr]
  512.         mov     edx,[pos]
  513.         mov     [buf_size],edx
  514. ;       mcall   70,fileinfo_tmp
  515. dps     "pos = "
  516. dph     edx
  517. newline
  518.  
  519. ; first, find end of headers
  520. .next_byte:
  521.         cmp     [edi],dword 0x0d0a0d0a  ; ìíå ëåíü ÷èòàòü ñòàíäàðò, ïóñòü áóäóò îáà âàðèàíòà
  522.         je      .end_of_headers
  523.         cmp     [edi],dword 0x0a0d0a0d
  524.         je      .end_of_headers
  525.         inc     edi
  526.         dec     edx
  527.         jne     .next_byte
  528. ; no end of headers. it's an error. let client see all those headers.
  529.         ret
  530.  
  531. .end_of_headers:
  532. ; here we look at headers and search content-length or transfer-encoding headers
  533. ;dps    "eoh "
  534. ;newline
  535.         sub     edi,[buf_ptr]
  536.         add     edi,3
  537.         mov     [body_pos],edi  ; store position where document body starts
  538.         mov     [is_chunked],0
  539. ; find content-length in headers
  540. ; not good method, but should work for 'Content-Length:'
  541.         mov     esi,[buf_ptr]
  542.         mov     edi,s_contentlength
  543.         mov     ebx,[body_pos]
  544.         xor     edx,edx ; 0
  545. .cl_next:
  546.         mov     al,[esi]
  547.         cmp     al,[edi + edx]
  548.         jne     .cl_fail
  549.         inc     edx
  550.         cmp     edx,len_contentlength
  551.         je      .cl_found
  552.         jmp     .cl_incr
  553. .cl_fail:
  554.         xor     edx,edx ; 0
  555. .cl_incr:
  556.         inc     esi
  557.         dec     ebx
  558.         je      .cl_error
  559.         jmp     .cl_next
  560. .cl_error:
  561. ;pregs
  562. ;newline
  563. ;dph    esi
  564. ;dps    " content-length not found "
  565. ; find 'chunked'
  566. ; äà, ÿ êîïèðóþ êîä, ýòî óæàñíî, íî ìíå õî÷åòñÿ, ÷òîáû ïîñêîðåå çàðàáîòàëî
  567. ; à òàì óæ îòðåôàêòîðþ
  568.         mov     esi,[buf_ptr]
  569.         mov     edi,s_chunked
  570.         mov     ebx,[body_pos]
  571.         xor     edx,edx ; 0
  572.  
  573. .ch_next:
  574.         mov     al,[esi]
  575.         cmp     al,[edi + edx]
  576.         jne     .ch_fail
  577.         inc     edx
  578.         cmp     edx,len_chunked
  579.         je      .ch_found
  580.         jmp     .ch_incr
  581.  
  582. .ch_fail:
  583.         xor     edx,edx ; 0
  584.  
  585. .ch_incr:
  586.         inc     esi
  587.         dec     ebx
  588.         je      .ch_error
  589.         jmp     .ch_next
  590.  
  591. .ch_error:
  592. ; if neither of the 2 headers is found, it's an error
  593. ;dps    "transfer-encoding: chunked not found "
  594.         mov     eax,[pos]
  595.         sub     eax,[body_pos]
  596.         jmp     .write_final_size
  597.  
  598. .ch_found:
  599.         mov     [is_chunked],1
  600.         mov     eax,[body_pos]
  601.         add     eax,[buf_ptr]
  602.         sub     eax,2
  603.         mov     [prev_chunk_end],eax
  604.         jmp     parse_chunks
  605.        
  606. .cl_found:     
  607.         call    read_number     ; eax = number from *esi
  608.         inc     eax     ; bugfix :)
  609.  
  610. .write_final_size:
  611.         mov     [final_size],eax        ; if this works, i will b very happy...
  612.        
  613.         mov     ebx,[pos]       ; we well check if it is right
  614.         sub     ebx,[body_pos]
  615.  
  616. ;dps    "check cl eax==ebx "
  617. ;pregs
  618.  
  619. ; everything is ok, so we return
  620.         mov     eax,[body_pos]
  621.         mov     ebx,[buf_ptr]
  622.         add     ebx,eax
  623.         mov     [final_buffer],ebx
  624. ;       mov     ebx,[pos]
  625. ;       sub     ebx,eax
  626. ;       mov     [final_size],ebx
  627.         ret
  628.        
  629. parse_chunks:
  630. ;dps    "parse chunks"
  631. ;newline
  632.         ; we have to look through the data and remove sizes of chunks we see
  633.         ; 1. read size of next chunk
  634.         ; 2. if 0, it's end. if not, continue.
  635.         ; 3. make a good buffer and copy a chunk there
  636.         xor     eax,eax
  637.         mov     [final_buffer],eax      ; 0
  638.         mov     [final_size],eax        ; 0
  639.        
  640. .read_size:
  641.         mov     eax,[prev_chunk_end]
  642.         mov     ebx,eax
  643.         sub     ebx,[buf_ptr]
  644.         mov     edx,eax
  645. ;dps    "rs "
  646. ;pregs
  647.         cmp     ebx,[pos]
  648.         jae     chunks_end      ; not good
  649.        
  650.         call    read_hex        ; in: eax=pointer to text. out:eax=hex number,ebx=end of text.
  651.         cmp     eax,0
  652.         jz      chunks_end
  653.  
  654.         add     ebx,1
  655.         mov     edx,ebx ; edx = size of size of chunk
  656.        
  657.         add     ebx,eax
  658.         mov     [prev_chunk_end],ebx
  659.        
  660. ;dps    "sz "
  661. ;pregs
  662. ; do copying: from buf_ptr+edx to final_buffer+prev_final_size count eax
  663. ; realloc final buffer
  664.         push    eax
  665.         push    edx
  666.         push    dword [final_size]
  667.         add     [final_size],eax
  668.         mcall   68,20,[final_size],[final_buffer]
  669.         mov     [final_buffer],eax
  670. ;dps    "re "
  671. ;pregs
  672.         pop     edi
  673.         pop     esi
  674.         pop     ecx
  675. ;       add     [pos],ecx
  676.         add     edi,[final_buffer]
  677. ;dps    "cp "
  678. ;pregs
  679.         rep     movsb
  680.         jmp     .read_size
  681.        
  682. chunks_end:
  683. ; free old buffer
  684. dps     "chunks end"
  685. newline
  686.         mcall   68,13,[buf_ptr]
  687. ; done!
  688.         ret
  689.  
  690. ; reads content-length from [edi+ecx], result in eax
  691. read_number:
  692.         push    ebx
  693.         xor     eax,eax
  694.         xor     ebx,ebx
  695.  
  696. .next:
  697.         mov     bl,[esi]
  698. ;dph    ebx
  699.         cmp     bl,'0'
  700.         jb      .not_number
  701.         cmp     bl,'9'
  702.         ja      .not_number
  703.         sub     bl,'0'
  704.         shl     eax,1
  705.         lea     eax,[eax + eax * 4]     ; eax *= 10
  706.         add     eax,ebx
  707.  
  708. .not_number:
  709.         cmp     bl,13
  710.         jz      .done
  711.         inc     esi
  712.         jmp     .next
  713.  
  714. .done:
  715.         pop     ebx
  716. ;newline
  717. ;dps    "strtoint eax "
  718. ;pregs
  719.         ret
  720.        
  721. ; reads hex from eax,result in eax,end of text in ebx
  722. read_hex:
  723.         add     eax,2
  724.         mov     ebx,eax
  725.         mov     eax,[ebx]
  726.         mov     [deba],eax
  727. ;       pushf
  728. ;       pushad
  729. ;       mov     edx,deba
  730. ;       call    debug_outstr
  731. ;       popad
  732. ;       popf
  733.         xor     eax,eax
  734.         xor     ecx,ecx
  735. .next:
  736.         mov     cl,[ebx]
  737.         inc     ebx
  738.        
  739.         cmp     cl,0x0d
  740.         jz      .done
  741. ;dph    ebx
  742.         or      cl,0x20
  743.         sub     cl,'0'
  744.         jb      .bad
  745.  
  746.         cmp     cl,0x9
  747.         jbe     .adding
  748.  
  749.         sub     cl,'a'-'0'-10
  750.         cmp     cl,0x0a
  751.         jb      .bad
  752.  
  753.         cmp     cl,0x0f
  754.         ja      .bad
  755.  
  756. .adding:
  757.         shl     eax,4
  758.         or      eax,ecx
  759. ;       jmp     .not_number
  760. ;.bad:
  761. .bad:
  762.         jmp     .next
  763. .done:
  764. ;newline
  765. ;dps    "hextoint eax "
  766. ;pregs
  767.         ret
  768.  
  769. ;****************************************************************************
  770. ;    Function
  771. ;       draw_page
  772. ;
  773. ;   Description
  774. ;       parses the web page data, storing displayable data at 0x20000
  775. ;       and attributes at 0x30000. It then calls display_page to render
  776. ;       the data
  777. ;
  778. ;****************************************************************************
  779. draw_page:
  780.         ret
  781.  
  782. ;****************************************************************************
  783. ;    Function
  784. ;       linefeed
  785. ;
  786. ;   Description
  787. ;
  788. ;
  789. ;****************************************************************************
  790. linefeed:
  791.         ret
  792.  
  793. ;****************************************************************************
  794. ;    Function
  795. ;       display_page
  796. ;
  797. ;   Description
  798. ;       Renders the text decoded by draw_page
  799. ;
  800. ;****************************************************************************
  801. display_page:
  802.         ret
  803.  
  804. ;****************************************************************************
  805. ;    Function
  806. ;       socket_commands
  807. ;
  808. ;   Description
  809. ;       opens or closes the socket
  810. ;
  811. ;****************************************************************************
  812. socket_commands:
  813.         cmp     ah,22   ; open socket
  814.         jnz     tst3
  815.  
  816. dps     "opening socket"
  817. newline
  818. ; Clear all page memory
  819.         xor     eax,eax
  820.         mov     [prev_chunk_end],eax    ; 0
  821.         cmp     [buf_ptr],eax   ; 0
  822.         jz      no_free
  823.  
  824.         mcall   68,13,[buf_ptr] ; free  buffer
  825.  
  826. no_free:
  827.         xor     eax,eax
  828.         mov     [buf_size],eax  ; 0
  829. ; Parse the entered url
  830.         call    parse_url
  831. ; Get a free port number
  832.         mov     ecx,1000        ; local port starting at 1000
  833.  
  834. getlp1:
  835.         inc     ecx
  836.         push    ecx
  837.         mcall   53,9
  838.         pop     ecx
  839.         cmp     eax,0   ; is this local port in use?
  840.         jz      getlp1  ; yes - so try next
  841.  
  842.         mov     edx,80
  843.         cmp     [proxyAddr],byte 0
  844.         jz      sc000
  845.         mov     edx,[proxyPort]
  846. sc000:
  847.         mcall   53,5,,,[server_ip],1
  848.         mov     [socket],eax
  849.         mov     [pagexs],80
  850.        
  851.         push    eax
  852.         xor     eax,eax ; 0
  853.         mov     [pos],eax
  854.         mov     [pagex],eax
  855.         mov     [pagey],eax
  856.         mov     [command_on_off],eax
  857.         mov     [is_body],eax
  858.         pop     eax
  859.         ret
  860.  
  861. tst3:
  862.         cmp     ah,24   ; close socket
  863.         jnz     no_24
  864.  
  865.         mcall   53,8,[socket]
  866.         call    draw_page
  867. no_24:
  868.         ret
  869.  
  870. ;****************************************************************************
  871. ;    Function
  872. ;       parse_url
  873. ;
  874. ;   Description
  875. ;       parses the full url typed in by the user into a web address ( that
  876. ;       can be turned into an IP address by DNS ) and the page to display
  877. ;       DNS will be used to translate the web address into an IP address, if
  878. ;       needed.
  879. ;       url is at document_user and will be space terminated.
  880. ;       web address goes to webAddr and is space terminated.
  881. ;       ip address goes to server_ip
  882. ;       page goes to document and is space terminated.
  883. ;
  884. ;       Supported formats:
  885. ;       <protocol://>address<page>
  886. ;       <protocol> is optional, removed and ignored - only http supported
  887. ;       <address> is required. It can be an ip address or web address
  888. ;       <page> is optional and must start with a leading / character
  889. ;
  890. ;****************************************************************************
  891. parse_url:
  892. ; First, reset destination variables
  893.         cld
  894.         mov     al,' '
  895.         mov     edi,document
  896.         mov     ecx,URLMAXLEN
  897.         rep     stosb
  898.         mov     edi,webAddr
  899.         mov     ecx,URLMAXLEN
  900.         rep     stosb
  901.  
  902.         mov     al,'/'
  903.         mov     [document],al
  904.  
  905.         mov     esi,document_user
  906. ; remove any leading protocol text
  907.         mov     ecx,URLMAXLEN
  908.         mov     ax,'//'
  909.  
  910. pu_000:
  911.         cmp     [esi],byte ' '  ; end of text?
  912.         je      pu_002          ; yep, so not found
  913.         cmp     [esi],ax
  914.         je      pu_001  ; Found it, so esi+2 is start
  915.         inc     esi
  916.         loop    pu_000
  917.  
  918. pu_002:
  919. ; not found, so reset esi to start
  920.         mov     esi,document_user-2
  921.  
  922. pu_001:
  923.         add     esi,2
  924.         mov     ebx,esi ; save address of start of web address
  925.         mov     edi,document_user + URLMAXLEN   ; end of string
  926. ; look for page delimiter - it's a '/' character
  927. pu_003:
  928.         cmp     [esi],byte ' '  ; end of text?
  929.         je      pu_004          ; yep, so none found
  930.         cmp     esi,edi         ; end of string?
  931.         je      pu_004          ; yep, so none found
  932.         cmp     [esi],byte '/'  ; delimiter?
  933.         je      pu_005          ; yep - process it
  934.         inc     esi
  935.         jmp     pu_003
  936.  
  937. pu_005:
  938. ; copy page to document address
  939. ; esi = delimiter
  940.         push    esi
  941.         mov     ecx,edi         ; end of document_user
  942.         mov     edi,document
  943.         cld
  944.  
  945. pu_006:
  946.         movsb
  947.         cmp     esi,ecx
  948.         je      pu_007          ; end of string?
  949.         cmp     [esi],byte ' '  ; end of text
  950. ;       je      pu_007          ; äçåí-àññåìáëåð
  951. ;       jmp     pu_006          ; íå íàäî ïëîäèòü ñóùíîñòè ïî íàïðàñíó
  952.         jne     pu_006
  953.  
  954. pu_007:
  955.         pop     esi     ; point esi to '/' delimiter
  956.  
  957. pu_004:
  958. ; copy web address to webAddr
  959. ; start in ebx,end in esi-1
  960.         mov     ecx,esi
  961.         mov     esi,ebx
  962.         mov     edi,webAddr
  963.         cld
  964.  
  965. pu_008:
  966.         movsb
  967.         cmp     esi,ecx
  968. ;       je      pu_009          ; äçåí-àññåìáëåð
  969. ;       jmp     pu_008          ; íå íàäî ïëîäèòü ñóùíîñòè ïî íàïðàñíó
  970.         jne     pu_008
  971.  
  972. pu_009:
  973. ; For debugging, display resulting strings
  974. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  975.         mov     esi,document_user
  976.         call    debug_print_string
  977.         mov     esi,webAddr
  978.         call    debug_print_string
  979.         mov     esi,document
  980.         call    debug_print_string
  981. end     if
  982. ; Look up the ip address, or was it specified?
  983.         mov     al,[proxyAddr]
  984.         cmp     al,0
  985.         jnz     pu_015
  986.         mov     al,[webAddr]
  987. pu_015:
  988.         cmp     al,'0'
  989.         jb      pu_010  ; Resolve address
  990.         cmp     al,'9'
  991.         ja      pu_010  ; Resolve address
  992.  
  993. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  994.         mov     esi,str2        ; print gotip
  995.         call    debug_print_string
  996. end     if
  997. ; Convert address
  998. ; If proxy is given, get proxy address instead of server
  999.         mov     esi,proxyAddr-1
  1000.         cmp     byte [esi+1],0
  1001.         jnz     pu_020
  1002.         mov     esi,webAddr-1
  1003.  
  1004. pu_020:
  1005.         mov     edi,server_ip
  1006.         xor     eax,eax
  1007.  
  1008. ip1:
  1009.         inc     esi
  1010.         cmp     [esi],byte '0'
  1011.         jb      ip2
  1012.         cmp     [esi],byte '9'
  1013.         jg      ip2
  1014.         imul    eax,10
  1015.         movzx   ebx,byte [esi]
  1016.         sub     ebx,48
  1017.         add     eax,ebx
  1018.         jmp     ip1
  1019.  
  1020. ip2:
  1021.         mov     [edi],al
  1022.         xor     eax,eax
  1023.         inc     edi
  1024.         cmp     edi,server_ip+3
  1025.         jbe     ip1
  1026.         jmp     pu_011
  1027.  
  1028. pu_010:
  1029. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1030.         mov     esi,str1        ; print resolving
  1031.         call    debug_print_string
  1032. end     if
  1033. ; Resolve Address
  1034.         call    translateData   ; Convert domain & DNS IP address
  1035.         call    resolveDomain   ; get ip address
  1036.  
  1037. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1038.         mov     esi,str3
  1039.         call    debug_print_string
  1040. end     if
  1041.  
  1042. pu_011:
  1043. ; Done
  1044.         ret
  1045.  
  1046. ;***************************************************************************
  1047. ;   Function
  1048. ;      translateData
  1049. ;
  1050. ;   Description
  1051. ;      Coverts the domain name and DNS IP address typed in by the user into
  1052. ;      a format suitable for the IP layer.
  1053. ;
  1054. ;    The ename, in query, is converted and stored in dnsMsg
  1055. ;
  1056. ;***************************************************************************
  1057. translateData:
  1058.     ; first, get the IP address of the DNS server
  1059.     ; Then, build up the request string.
  1060.  
  1061.     ; Build the request string
  1062.         mov     eax,0x00010100
  1063.         mov     [dnsMsg],eax
  1064.         mov     eax,0x00000100
  1065.         mov     [dnsMsg+4],eax
  1066.         mov     eax,0x00000000
  1067.         mov     [dnsMsg+8],eax
  1068. ; domain name goes in at dnsMsg+12
  1069.         mov     esi,dnsMsg +12  ;location of label length
  1070.         mov     edi,dnsMsg + 13 ;label start
  1071.         mov     edx,proxyAddr
  1072.         cmp     byte [edx],0
  1073.         jnz     td000
  1074.         mov     edx,webAddr
  1075.  
  1076. td000:
  1077.         mov     ecx,12  ; total string length so far
  1078.  
  1079. td002:
  1080.         mov     [esi],byte 0
  1081.         inc     ecx
  1082.  
  1083. td0021:
  1084.         mov     al,[edx]
  1085.         cmp     al,' '
  1086.         je      td001   ; we have finished the string translation
  1087.  
  1088.         cmp     al,0
  1089.         je      td001
  1090.  
  1091.         cmp     al,'.'  ; we have finished the label
  1092.         je      td004
  1093.  
  1094.         inc     byte [esi]
  1095.         inc     ecx
  1096.         mov     [edi],al
  1097.         inc     edi
  1098.         inc     edx
  1099.         jmp     td0021
  1100.  
  1101. td004:
  1102.         mov     esi,edi
  1103.         inc     edi
  1104.         inc     edx
  1105.         jmp     td002
  1106.  
  1107. ; write label len + label text
  1108. td001:
  1109.         mov     [edi],byte 0
  1110.         inc     ecx
  1111.         inc     edi
  1112.         mov     [edi],dword 0x01000100
  1113.         add     ecx,4
  1114.         mov     [dnsMsgLen],ecx
  1115.         ret
  1116.  
  1117. ;***************************************************************************
  1118. ;   Function
  1119. ;      resolveDomain
  1120. ;
  1121. ;   Description
  1122. ;       Sends a question to the dns server
  1123. ;       works out the IP address from the response from the DNS server
  1124. ;
  1125. ;***************************************************************************
  1126. resolveDomain:
  1127. ; Get a free port number
  1128.         mov     ecx,1000        ; local port starting at 1000
  1129. getlp:
  1130.         inc     ecx
  1131.         push    ecx
  1132.         mcall   53,9
  1133.         pop     ecx
  1134.         cmp     eax,0   ; is this local port in use?
  1135.         jz      getlp   ; yes - so try next
  1136.  
  1137. ; Get DNS IP
  1138.         mcall   52,13
  1139.         mov     esi,eax
  1140. ; First, open socket
  1141.         mov     edx,53  ; remote port - dns
  1142. ;       mov     esi,dword [dns_ip]
  1143.         xor     ebx,ebx ; 0
  1144.         mcall   53
  1145.         mov     [socketNum],eax
  1146. ; write to socket ( request DNS lookup )
  1147.         mcall   53,4,[socketNum],[dnsMsgLen],dnsMsg
  1148. ; Setup the DNS response buffer
  1149.         mov     eax,dnsMsg
  1150.         mov     [dnsMsgLen],eax
  1151. ; now, we wait for
  1152. ; UI redraw
  1153. ; UI close
  1154. ; or data from remote
  1155.  
  1156. ctr001:
  1157.         mcall   10      ; wait here for event
  1158.         cmp     eax,1   ; redraw request ?
  1159.         je      ctr003
  1160.  
  1161.         cmp     eax,2   ; key in buffer ?
  1162.         je      ctr004
  1163.  
  1164.         cmp     eax,3   ; button in buffer ?
  1165.         je      ctr005
  1166. ; Any data in the UDP receive buffer?
  1167.         mcall   53,2,[socketNum]
  1168.         cmp     eax,0
  1169.         je      ctr001
  1170.  
  1171. ; we have data - this will be the response
  1172. ctr002:
  1173.         mcall   53,3,[socketNum]        ; read byte - block (high byte)
  1174. ; Store the data in the response buffer
  1175.         mov     eax,[dnsMsgLen]
  1176.         mov     [eax],bl
  1177.         inc     dword [dnsMsgLen]
  1178.  
  1179.         mcall   53,2,[socketNum]        ; any more data?
  1180.         cmp     eax,0
  1181.         jne     ctr002  ; yes, so get it
  1182.  
  1183. ; close socket
  1184.         mcall   53,1,[socketNum]
  1185.         mov     [socketNum],dword  0xFFFF
  1186. ; Now parse the message to get the host IP
  1187. ; Man, this is complicated. It's described in
  1188. ; RFC 1035
  1189. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1190.         mov     esi,str4
  1191.         call    debug_print_string
  1192. end     if
  1193.  
  1194.     ; 1) Validate that we have an answer with > 0 responses
  1195.     ; 2) Find the answer record with TYPE 0001 ( host IP )
  1196.     ; 3) Finally, copy the IP address to the display
  1197.     ; Note: The response is in dnsMsg
  1198.     ;       The end of the buffer is pointed to by [dnsMsgLen]
  1199.  
  1200. ; Clear the IP address text
  1201.         mov     [server_ip],dword  0
  1202.         mov     esi,dnsMsg
  1203. ; Is this a response to my question?
  1204.         mov     al,[esi+2]
  1205.         and     al,0x80
  1206.         cmp     al,0x80
  1207.         jne     ctr002a
  1208. ; Were there any errors?
  1209.         mov     al,[esi+3]
  1210.         and     al,0x0F
  1211.         cmp     al,0x00
  1212.         jne     ctr002a
  1213. ; Is there ( at least 1 ) answer?
  1214.         mov     ax,[esi+6]
  1215.         cmp     ax,0x00
  1216.         je      ctr002a
  1217. ; Header valdated. Scan through and get my answer
  1218. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1219.         pusha
  1220.         mov     esi,str4
  1221.         call    debug_print_string
  1222.         popa
  1223. end     if
  1224.         add     esi,12  ; Skip to the question field
  1225. ; Skip through the question field
  1226.         call    skipName
  1227.         add     esi,4   ; skip past the questions qtype, qclass
  1228.  
  1229. ctr002z:
  1230. ; Now at the answer. There may be several answers,
  1231. ; find the right one ( TYPE = 0x0001 )
  1232.         call    skipName
  1233.         mov     ax,[esi]
  1234.         cmp     ax,0x0100       ; Is this the IP address answer?
  1235.         jne     ctr002c
  1236. ; Yes! Point esi to the first byte of the IP address
  1237.         add     esi,10
  1238.         mov     eax,[esi]
  1239.         mov     [server_ip],eax
  1240.         ret
  1241.  
  1242. ctr002c:        ; Skip through the answer, move to the next
  1243.         add     esi,8
  1244.         movzx   eax,byte [esi+1]
  1245.         mov     ah,[esi]
  1246.         add     esi,eax
  1247.         add     esi,2
  1248. ; Have we reached the end of the msg?
  1249. ; This is an error condition, should not happen
  1250.         cmp     esi,[dnsMsgLen]
  1251.         jl      ctr002z ; Check next answer
  1252.         jmp     ctr002a ; abort
  1253.  
  1254. ctr002a:
  1255.         jmp     ctr001
  1256.  
  1257. ctr003: ; redraw
  1258.         call    draw_window
  1259.         jmp     ctr001
  1260.  
  1261. ctr004: ; key
  1262.         mcall   2       ; just read it and ignore
  1263.         jmp     ctr001
  1264.  
  1265. ctr005: ; button
  1266.         mcall   17      ; get id
  1267.         mov     dl,ah
  1268.  
  1269.         ; close socket
  1270.         mcall   53,1,[socketNum]
  1271.         cmp     dl,1
  1272.         je      exit
  1273.  
  1274.         mov     [socketNum],dword  0xFFFF
  1275.         mov     [server_ip],dword  0
  1276.         ret
  1277.  
  1278. ;***************************************************************************
  1279. ;   Function
  1280. ;      skipName
  1281. ;
  1282. ;   Description
  1283. ;       Increment esi to the first byte past the name field
  1284. ;       Names may use compressed labels. Normally do.
  1285. ;       RFC 1035 page 30 gives details
  1286. ;
  1287. ;***************************************************************************
  1288. skipName:
  1289.         mov     al,[esi]
  1290.         cmp     al,0
  1291.         je      sn_exit
  1292.         and     al,0xc0
  1293.         cmp     al,0xc0
  1294.         je      sn001
  1295.  
  1296.         movzx   eax,byte [esi]
  1297.         inc     eax
  1298.         add     esi,eax
  1299.         jmp     skipName
  1300.  
  1301. sn001:
  1302.         add     esi,2   ; A pointer is always at the end
  1303.         ret
  1304.  
  1305. sn_exit:
  1306.         inc     esi
  1307.         ret
  1308.  
  1309. ;***************************************************************************
  1310. ;   Function
  1311. ;       load_settings
  1312. ;
  1313. ;   Description
  1314. ;       Load settings from configuration file network.ini
  1315. ;
  1316. ;***************************************************************************
  1317. load_settings:
  1318.         stdcall dll.Load,@IMPORT
  1319.         test    eax,eax
  1320.         jnz     ls001
  1321.         invoke  ini.get_str,inifile,sec_proxy,key_proxy,proxyAddr,256,proxyAddr
  1322.         invoke  ini.get_int,inifile,sec_proxy,key_proxyport,80
  1323.         mov     [proxyPort],eax
  1324.         invoke  ini.get_str,inifile,sec_proxy,key_user, proxyUser,256,proxyUser
  1325.         invoke  ini.get_str,inifile,sec_proxy,key_password,proxyPassword,256,proxyPassword
  1326. ls001:
  1327.         ret
  1328.  
  1329. ;***************************************************************************
  1330. ;   Function
  1331. ;       append_proxy_auth_header
  1332. ;
  1333. ;   Description
  1334. ;       Append header to HTTP request for proxy authentification
  1335. ;
  1336. ;***************************************************************************
  1337. append_proxy_auth_header:
  1338.         mov     esi,proxy_auth_basic
  1339.         mov     ecx,proxy_auth_basic_end - proxy_auth_basic
  1340.         rep     movsb
  1341. ; base64-encode string <user>:<password>
  1342.         mov     esi,proxyUser
  1343.  
  1344. apah000:
  1345.         lodsb
  1346.         test    al,al
  1347.         jz      apah001
  1348.         call    encode_base64_byte
  1349.         jmp     apah000
  1350.  
  1351. apah001:
  1352.         mov     al,':'
  1353.         call    encode_base64_byte
  1354.         mov     esi,proxyPassword
  1355.  
  1356. apah002:
  1357.         lodsb
  1358.         test    al,al
  1359.         jz      apah003
  1360.         call    encode_base64_byte
  1361.         jmp     apah002
  1362.  
  1363. apah003:
  1364.         call    encode_base64_final
  1365.         ret
  1366.  
  1367. encode_base64_byte:
  1368.         inc     ecx
  1369.         shl     edx,8
  1370.         mov     dl,al
  1371.         cmp     ecx,3
  1372.         je      ebb001
  1373.         ret
  1374.  
  1375. ebb001:
  1376.         shl     edx,8
  1377.         inc     ecx
  1378.  
  1379. ebb002:
  1380.         rol     edx,6
  1381.         xor     eax,eax
  1382.         xchg    al,dl
  1383.         mov     al,[base64_table+eax]
  1384.         stosb
  1385.         loop    ebb002
  1386.         ret
  1387.  
  1388. encode_base64_final:
  1389.         mov     al,0
  1390.         test    ecx,ecx
  1391.         jz      ebf000
  1392.         call    encode_base64_byte
  1393.         test    ecx,ecx
  1394.         jz      ebf001
  1395.         call    encode_base64_byte
  1396.         mov     byte [edi-2],'='
  1397.  
  1398. ebf001:
  1399.         mov     byte [edi-1],'='
  1400.  
  1401. ebf000:
  1402.         ret
  1403.  
  1404. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1405.  
  1406. ;****************************************************************************
  1407. ;    Function
  1408. ;       debug_print_string
  1409. ;
  1410. ;   Description
  1411. ;       prints a string to the debug board, in quotes
  1412. ;
  1413. ;       esi holds ptr to msg to display, which is space or 0 terminated
  1414. ;
  1415. ;       Nothing preserved; I'm assuming a pusha/popa is done before calling
  1416. ;
  1417. ;****************************************************************************
  1418. debug_print_string:
  1419.         push    esi
  1420.         mov     cl,'"'
  1421.         mcall   63,1
  1422.         pop     esi
  1423.  
  1424. dps_000:
  1425.         mov     cl,[esi]
  1426.         cmp     cl,0
  1427.         je      dps_exit
  1428.  
  1429.         cmp     cl,' '
  1430.         je      dps_exit
  1431.         jmp     dps_001
  1432.  
  1433. dps_exit:
  1434.         mov     cl,'"'
  1435.         mcall   63,1
  1436.         mov     cl,13
  1437.         mcall  
  1438.         mov     cl,10
  1439.         mcall
  1440.         ret
  1441.  
  1442. dps_001:
  1443.         push    esi
  1444.         mcall   63,1
  1445.         pop     esi
  1446.         inc     esi
  1447.         jmp     dps_000
  1448. end     if
  1449.  
  1450. ;****************************************************************************
  1451. ;    Function
  1452. ;       print_text
  1453. ;
  1454. ;   Description
  1455. ;       display the url (full path) text
  1456. ;
  1457. ;****************************************************************************
  1458. print_text:
  1459. ; Draw a bar to blank out previous text
  1460.         mov     ebx,30*65536+URLMAXLEN*6        ; 50 should really be [len] and 103 [xa]
  1461.         mov     ecx,[ya]
  1462.         shl     ecx,16
  1463.         mov     cx,9
  1464.         mcall   13,,,0xFFFFFF
  1465. ; write text
  1466.         mov     ebx,30*65536
  1467.         add     ebx,[ya]
  1468.         xor     ecx,ecx
  1469.         mcall   4,,,[addr],URLMAXLEN
  1470.         ret
  1471.  
  1472. ;   *********************************************
  1473. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  1474. ;   *********************************************
  1475.  
  1476. draw_window:
  1477. ; function 12: tell os about windowdraw
  1478. ; 1 start of draw
  1479.         mcall   12,1
  1480.  
  1481. ;       cmp     [params],byte 0
  1482. ;       jz      .noret
  1483.  
  1484. ; ýòî íåñêîëüêî çàãàäî÷íî, íî åñëè íå ðèñîâàòü îêîøêî ñîâñåì, ïðîãà íå ïàøåò.
  1485. ; DRAW  WINDOW
  1486. ; eax   function 0 : define and draw window
  1487. ;       xor     eax,eax
  1488. ; ebx   [x start] *65536 + [x size]
  1489. ;       xor     ebx,ebx
  1490. ; ecx   [y start] *65536 + [y size]
  1491. ;       xor     ecx,ecx
  1492. ; edx   color of work area RRGGBB,8->color gl
  1493. ; esi   color of bar and flags
  1494. ;       xor     esi,esi
  1495. ;       mcall   ,,,0x14ffffff,,title
  1496.  
  1497. ; function 12: tell os about windowdraw
  1498. ; 2, end of draw
  1499. ;       mcall   12,2
  1500. ;       ret
  1501.        
  1502. ;.noret:
  1503. ; DRAW  WINDOW
  1504. ; eax   function 0 : define and draw window
  1505.         xor     eax,eax
  1506. ; ebx   [x start] *65536 + [x size]
  1507. ; ecx   [y start] *65536 + [y size]
  1508. ; edx   color of work area RRGGBB,8->color gl
  1509. ; esi   color of bar and flags
  1510.         xor     esi,esi
  1511. ; edi   WINDOW  LABEL
  1512.         mcall   ,<50,600>,<350,200>,0x14ffffff,,title
  1513. ; eax   function 4: write text to window
  1514. ; ebx   [x start] *65536 + [y start]
  1515. ; ecx   color of text RRGGBB
  1516. ; edx   pointer to text beginning
  1517. ; esi   max lenght
  1518.         xor     ecx,ecx
  1519.         mcall   4,<30,38>,,document_user,URLMAXLEN
  1520.  
  1521. ;       xor     edx,edx
  1522. ;       mcall   38,<5,545>,<60,60>
  1523.  
  1524. ;       mov     ecx,[winys]
  1525. ;       shl     ecx,16
  1526. ;       add     ecx,[winys]
  1527. ;       sub     ecx,26*65536+26
  1528. ;       mcall   38,<5,545>
  1529.  
  1530. ; RELOAD
  1531. ; eax   function 8 : define and draw button
  1532. ; ebx   [x start] *65536 + [x size]
  1533. ; ecx   [y start] *65536 + [y size]
  1534. ; edx   button id
  1535. ; esi   button color RRGGBB
  1536.         mcall   8,<388,50>,<34,14>,22,0x5588dd
  1537. ; URL
  1538.         mcall   ,<10,12>,<34,12>,10
  1539. ; STOP
  1540.         mcall   ,<443,50>,<34,14>,24
  1541. ; SAVE
  1542.         mcall   ,<498,50>,,26
  1543. ; BUTTON TEXT
  1544. ; eax   function 4 : write text to window
  1545. ; ebx   [x start] *65536 + [y start]
  1546. ; ecx   color   of      text    RRGGBB
  1547. ; edx   pointer to      text    beginning
  1548. ; esi   text    length
  1549.         mcall   4,<390,38>,0xffffff,button_text,30
  1550.         call    display_page
  1551.  
  1552. ; function 12: tell os about windowdraw
  1553. ; 2, end of draw
  1554.         mcall   12,2
  1555.         ret
  1556. ;-----------------------------------------------------------------------------
  1557. ; Data area
  1558. ;-----------------------------------------------------------------------------
  1559. align   4
  1560. @IMPORT:
  1561.  
  1562. library libini,'libini.obj'
  1563.  
  1564. import  libini, \
  1565.         ini.get_str,'ini_get_str', \
  1566.         ini.get_int,'ini_get_int'
  1567.  
  1568. ;---------------------------------------------------------------------
  1569. fileinfo        dd 2,0,0
  1570. final_size      dd 0
  1571. final_buffer    dd 0
  1572.                 db '/rd/1/.download',0
  1573.        
  1574. body_pos        dd 0
  1575.  
  1576. ;fileinfo_tmp   dd 2,0,0
  1577. buf_size        dd 0
  1578. buf_ptr         dd 0
  1579. ;               db      '/rd/1/1',0
  1580.  
  1581. deba            dd 0
  1582.                 db 0
  1583. ;---------------------------------------------------------------------
  1584. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1585. str1:           db "Resolving...",0
  1586. str3:           db "Resolved",0
  1587. str2:           db "GotIP",0
  1588. str4:           db "GotResponse",0
  1589. end     if
  1590. ;---------------------------------------------------------------------
  1591. button_text     db ' RELOAD    STOP       SAVE    '
  1592. dpx             dd 25   ; x - start of html page in pixels in window
  1593. dpy             dd 65   ; for   y
  1594. lastletter      db 0
  1595. pageyinc        dd 0
  1596. display_from    dd 20
  1597. pos             dd 0x0
  1598. pagex           dd 0x0
  1599. pagey           dd 0x0
  1600. pagexs          dd 80
  1601. command_on_off  dd 0x0
  1602. text_type       db 1
  1603. com2            dd 0x0
  1604. script          dd 0x0
  1605. socket          dd 0x0
  1606.  
  1607. addr            dd 0x0
  1608. ya              dd 0x0
  1609. len             dd 0x00
  1610.  
  1611. title           db 'Downloader',0
  1612.  
  1613. server_ip:      db 207,44,212,20
  1614. ;dns_ip:        db 194,145,128,1
  1615. ;---------------------------------------------------------------------
  1616. ;webAddr:
  1617. ;times URLMAXLEN db ' '
  1618. ;db     0
  1619.  
  1620. ;document_user: db 'Click on the button to the left to enter a URL',0
  1621. ;times URLMAXLEN+document_user-$ db 0
  1622.  
  1623. ;document:      db '/'
  1624. ;times URLMAXLEN-1 db ' '
  1625. ;---------------------------------------------------------------------
  1626. s_contentlength db 'Content-Length:'
  1627. len_contentlength = 15
  1628.  
  1629. s_chunked       db 'Transfer-Encoding: chunked'
  1630. len_chunked     = $ - s_chunked
  1631.  
  1632. is_body         dd 0    ; 0 if headers, 1 if content
  1633. is_chunked      dd 0
  1634. prev_chunk_end  dd 0
  1635. cur_chunk_size  dd 0
  1636.  
  1637. string0:        db 'GET '
  1638.  
  1639. stringh:        db ' HTTP/1.1',13,10,'Host: '
  1640. stringh_end:
  1641. proxy_auth_basic:       db 13,10,'Proxy-Authorization: Basic '
  1642. proxy_auth_basic_end:
  1643. connclose:      db 13,10,'User-Agent: Kolibrios Downloader',13,10,'Connection: Close',13,10,13,10
  1644. connclose_end:
  1645.  
  1646. base64_table    db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  1647.                 db '0123456789+/'
  1648.  
  1649. inifile         db '/sys/network/zeroconf.ini',0
  1650.  
  1651. sec_proxy:
  1652. key_proxy       db 'proxy',0
  1653. key_proxyport   db 'port',0
  1654. key_user        db 'user',0
  1655. key_password    db 'password',0
  1656.  
  1657.  
  1658. proxyPort       dd 80
  1659.  
  1660. shared_name     dd 0
  1661.  
  1662. ;yandex:        db 'menuetos.net'
  1663. ;yandex_end:
  1664.  
  1665. status          dd 0x0
  1666. prev_status     dd 0x0
  1667.  
  1668. onoff           dd 0x0
  1669.  
  1670. nextupdate:     dd 0
  1671. winys:          dd 400
  1672.  
  1673. dnsMsgLen:      dd 0
  1674. socketNum:      dd 0xFFFF
  1675. ;---------------------------------------------------------------------
  1676. document_user:  db 'Click on the button to the left to enter a URL',0
  1677. ;---------------------------------------------------------------------
  1678. IM_END:
  1679.         rb URLMAXLEN-(IM_END - document_user)
  1680. ;---------------------------------------------------------------------
  1681. align 4
  1682. document:
  1683.         rb URLMAXLEN
  1684. ;---------------------------------------------------------------------
  1685. align 4
  1686. webAddr:
  1687.         rb URLMAXLEN+1
  1688. ;---------------------------------------------------------------------
  1689. align 4
  1690. primary_buf:
  1691.         rb primary_buffer_size
  1692. ;---------------------------------------------------------------------
  1693. align 4
  1694. params:         ; db 1024 dup(0)
  1695.         rb 1024
  1696. ;---------------------------------------------------------------------
  1697. align 4
  1698. request:        ; db 256 dup(0)
  1699.         rb 256
  1700. ;---------------------------------------------------------------------
  1701. align 4
  1702. proxyAddr:      ; db 256 dup(0)
  1703.         rb 256
  1704. ;---------------------------------------------------------------------
  1705. align 4
  1706. proxyUser:      ; db 256 dup(0)
  1707.         rb 256
  1708. ;---------------------------------------------------------------------
  1709. align 4
  1710. proxyPassword:  ; db 256 dup(0)
  1711.         rb 256
  1712. ;---------------------------------------------------------------------
  1713. align 4
  1714. dnsMsg:
  1715.         rb 4096
  1716. ;       rb 0x100000
  1717. ;---------------------------------------------------------------------
  1718. align 4
  1719.         rb 4096
  1720. stacktop:
  1721. ;---------------------------------------------------------------------
  1722. I_END:
  1723. ;---------------------------------------------------------------------