Subversion Repositories Kolibri OS

Rev

Rev 2126 | 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,4
  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.  
  609. .write_final_size:
  610.         mov     [final_size],eax        ; if this works, i will b very happy...
  611.        
  612.         mov     ebx,[pos]       ; we well check if it is right
  613.         sub     ebx,[body_pos]
  614.  
  615. ;dps    "check cl eax==ebx "
  616. ;pregs
  617.  
  618. ; everything is ok, so we return
  619.         mov     eax,[body_pos]
  620.         mov     ebx,[buf_ptr]
  621.         add     ebx,eax
  622.         mov     [final_buffer],ebx
  623. ;       mov     ebx,[pos]
  624. ;       sub     ebx,eax
  625. ;       mov     [final_size],ebx
  626.         ret
  627.        
  628. parse_chunks:
  629. ;dps    "parse chunks"
  630. ;newline
  631.         ; we have to look through the data and remove sizes of chunks we see
  632.         ; 1. read size of next chunk
  633.         ; 2. if 0, it's end. if not, continue.
  634.         ; 3. make a good buffer and copy a chunk there
  635.         xor     eax,eax
  636.         mov     [final_buffer],eax      ; 0
  637.         mov     [final_size],eax        ; 0
  638.        
  639. .read_size:
  640.         mov     eax,[prev_chunk_end]
  641.         mov     ebx,eax
  642.         sub     ebx,[buf_ptr]
  643.         mov     edx,eax
  644. ;dps    "rs "
  645. ;pregs
  646.         cmp     ebx,[pos]
  647.         jae     chunks_end      ; not good
  648.        
  649.         call    read_hex        ; in: eax=pointer to text. out:eax=hex number,ebx=end of text.
  650.         cmp     eax,0
  651.         jz      chunks_end
  652.  
  653.         add     ebx,1
  654.         mov     edx,ebx ; edx = size of size of chunk
  655.        
  656.         add     ebx,eax
  657.         mov     [prev_chunk_end],ebx
  658.        
  659. ;dps    "sz "
  660. ;pregs
  661. ; do copying: from buf_ptr+edx to final_buffer+prev_final_size count eax
  662. ; realloc final buffer
  663.         push    eax
  664.         push    edx
  665.         push    dword [final_size]
  666.         add     [final_size],eax
  667.         mcall   68,20,[final_size],[final_buffer]
  668.         mov     [final_buffer],eax
  669. ;dps    "re "
  670. ;pregs
  671.         pop     edi
  672.         pop     esi
  673.         pop     ecx
  674. ;       add     [pos],ecx
  675.         add     edi,[final_buffer]
  676. ;dps    "cp "
  677. ;pregs
  678.         rep     movsb
  679.         jmp     .read_size
  680.        
  681. chunks_end:
  682. ; free old buffer
  683. dps     "chunks end"
  684. newline
  685.         mcall   68,13,[buf_ptr]
  686. ; done!
  687.         ret
  688.  
  689. ; reads content-length from [edi+ecx], result in eax
  690. read_number:
  691.         push    ebx
  692.         xor     eax,eax
  693.         xor     ebx,ebx
  694.  
  695. .next:
  696.         mov     bl,[esi]
  697. ;dph    ebx
  698.         cmp     bl,'0'
  699.         jb      .not_number
  700.         cmp     bl,'9'
  701.         ja      .not_number
  702.         sub     bl,'0'
  703.         shl     eax,1
  704.         lea     eax,[eax + eax * 4]     ; eax *= 10
  705.         add     eax,ebx
  706.  
  707. .not_number:
  708.         cmp     bl,13
  709.         jz      .done
  710.         inc     esi
  711.         jmp     .next
  712.  
  713. .done:
  714.         pop     ebx
  715. ;newline
  716. ;dps    "strtoint eax "
  717. ;pregs
  718.         ret
  719.        
  720. ; reads hex from eax,result in eax,end of text in ebx
  721. read_hex:
  722.         add     eax,2
  723.         mov     ebx,eax
  724.         mov     eax,[ebx]
  725.         mov     [deba],eax
  726. ;       pushf
  727. ;       pushad
  728. ;       mov     edx,deba
  729. ;       call    debug_outstr
  730. ;       popad
  731. ;       popf
  732.         xor     eax,eax
  733.         xor     ecx,ecx
  734. .next:
  735.         mov     cl,[ebx]
  736.         inc     ebx
  737.        
  738.         cmp     cl,0x0d
  739.         jz      .done
  740. ;dph    ebx
  741.         or      cl,0x20
  742.         sub     cl,'0'
  743.         jb      .bad
  744.  
  745.         cmp     cl,0x9
  746.         jbe     .adding
  747.  
  748.         sub     cl,'a'-'0'-10
  749.         cmp     cl,0x0a
  750.         jb      .bad
  751.  
  752.         cmp     cl,0x0f
  753.         ja      .bad
  754.  
  755. .adding:
  756.         shl     eax,4
  757.         or      eax,ecx
  758. ;       jmp     .not_number
  759. ;.bad:
  760. .bad:
  761.         jmp     .next
  762. .done:
  763. ;newline
  764. ;dps    "hextoint eax "
  765. ;pregs
  766.         ret
  767.  
  768. ;****************************************************************************
  769. ;    Function
  770. ;       draw_page
  771. ;
  772. ;   Description
  773. ;       parses the web page data, storing displayable data at 0x20000
  774. ;       and attributes at 0x30000. It then calls display_page to render
  775. ;       the data
  776. ;
  777. ;****************************************************************************
  778. draw_page:
  779.         ret
  780.  
  781. ;****************************************************************************
  782. ;    Function
  783. ;       linefeed
  784. ;
  785. ;   Description
  786. ;
  787. ;
  788. ;****************************************************************************
  789. linefeed:
  790.         ret
  791.  
  792. ;****************************************************************************
  793. ;    Function
  794. ;       display_page
  795. ;
  796. ;   Description
  797. ;       Renders the text decoded by draw_page
  798. ;
  799. ;****************************************************************************
  800. display_page:
  801.         ret
  802.  
  803. ;****************************************************************************
  804. ;    Function
  805. ;       socket_commands
  806. ;
  807. ;   Description
  808. ;       opens or closes the socket
  809. ;
  810. ;****************************************************************************
  811. socket_commands:
  812.         cmp     ah,22   ; open socket
  813.         jnz     tst3
  814.  
  815. dps     "opening socket"
  816. newline
  817. ; Clear all page memory
  818.         xor     eax,eax
  819.         mov     [prev_chunk_end],eax    ; 0
  820.         cmp     [buf_ptr],eax   ; 0
  821.         jz      no_free
  822.  
  823.         mcall   68,13,[buf_ptr] ; free  buffer
  824.  
  825. no_free:
  826.         xor     eax,eax
  827.         mov     [buf_size],eax  ; 0
  828. ; Parse the entered url
  829.         call    parse_url
  830. ; Get a free port number
  831.         mov     ecx,1000        ; local port starting at 1000
  832.  
  833. getlp1:
  834.         inc     ecx
  835.         push    ecx
  836.         mcall   53,9
  837.         pop     ecx
  838.         cmp     eax,0   ; is this local port in use?
  839.         jz      getlp1  ; yes - so try next
  840.  
  841.         mov     edx,80
  842.         cmp     [proxyAddr],byte 0
  843.         jz      sc000
  844.         mov     edx,[proxyPort]
  845. sc000:
  846.         mcall   53,5,,,[server_ip],1
  847.         mov     [socket],eax
  848.         mov     [pagexs],80
  849.        
  850.         push    eax
  851.         xor     eax,eax ; 0
  852.         mov     [pos],eax
  853.         mov     [pagex],eax
  854.         mov     [pagey],eax
  855.         mov     [command_on_off],eax
  856.         mov     [is_body],eax
  857.         pop     eax
  858.         ret
  859.  
  860. tst3:
  861.         cmp     ah,24   ; close socket
  862.         jnz     no_24
  863.  
  864.         mcall   53,8,[socket]
  865.         call    draw_page
  866. no_24:
  867.         ret
  868.  
  869. ;****************************************************************************
  870. ;    Function
  871. ;       parse_url
  872. ;
  873. ;   Description
  874. ;       parses the full url typed in by the user into a web address ( that
  875. ;       can be turned into an IP address by DNS ) and the page to display
  876. ;       DNS will be used to translate the web address into an IP address, if
  877. ;       needed.
  878. ;       url is at document_user and will be space terminated.
  879. ;       web address goes to webAddr and is space terminated.
  880. ;       ip address goes to server_ip
  881. ;       page goes to document and is space terminated.
  882. ;
  883. ;       Supported formats:
  884. ;       <protocol://>address<page>
  885. ;       <protocol> is optional, removed and ignored - only http supported
  886. ;       <address> is required. It can be an ip address or web address
  887. ;       <page> is optional and must start with a leading / character
  888. ;
  889. ;****************************************************************************
  890. parse_url:
  891. ; First, reset destination variables
  892.         cld
  893.         mov     al,' '
  894.         mov     edi,document
  895.         mov     ecx,URLMAXLEN
  896.         rep     stosb
  897.         mov     edi,webAddr
  898.         mov     ecx,URLMAXLEN
  899.         rep     stosb
  900.  
  901.         mov     al,'/'
  902.         mov     [document],al
  903.  
  904.         mov     esi,document_user
  905. ; remove any leading protocol text
  906.         mov     ecx,URLMAXLEN
  907.         mov     ax,'//'
  908.  
  909. pu_000:
  910.         cmp     [esi],byte ' '  ; end of text?
  911.         je      pu_002          ; yep, so not found
  912.         cmp     [esi],ax
  913.         je      pu_001  ; Found it, so esi+2 is start
  914.         inc     esi
  915.         loop    pu_000
  916.  
  917. pu_002:
  918. ; not found, so reset esi to start
  919.         mov     esi,document_user-2
  920.  
  921. pu_001:
  922.         add     esi,2
  923.         mov     ebx,esi ; save address of start of web address
  924.         mov     edi,document_user + URLMAXLEN   ; end of string
  925. ; look for page delimiter - it's a '/' character
  926. pu_003:
  927.         cmp     [esi],byte ' '  ; end of text?
  928.         je      pu_004          ; yep, so none found
  929.         cmp     esi,edi         ; end of string?
  930.         je      pu_004          ; yep, so none found
  931.         cmp     [esi],byte '/'  ; delimiter?
  932.         je      pu_005          ; yep - process it
  933.         inc     esi
  934.         jmp     pu_003
  935.  
  936. pu_005:
  937. ; copy page to document address
  938. ; esi = delimiter
  939.         push    esi
  940.         mov     ecx,edi         ; end of document_user
  941.         mov     edi,document
  942.         cld
  943.  
  944. pu_006:
  945.         movsb
  946.         cmp     esi,ecx
  947.         je      pu_007          ; end of string?
  948.         cmp     [esi],byte ' '  ; end of text
  949. ;       je      pu_007          ; äçåí-àññåìáëåð
  950. ;       jmp     pu_006          ; íå íàäî ïëîäèòü ñóùíîñòè ïî íàïðàñíó
  951.         jne     pu_006
  952.  
  953. pu_007:
  954.         pop     esi     ; point esi to '/' delimiter
  955.  
  956. pu_004:
  957. ; copy web address to webAddr
  958. ; start in ebx,end in esi-1
  959.         mov     ecx,esi
  960.         mov     esi,ebx
  961.         mov     edi,webAddr
  962.         cld
  963.  
  964. pu_008:
  965.         movsb
  966.         cmp     esi,ecx
  967. ;       je      pu_009          ; äçåí-àññåìáëåð
  968. ;       jmp     pu_008          ; íå íàäî ïëîäèòü ñóùíîñòè ïî íàïðàñíó
  969.         jne     pu_008
  970.  
  971. pu_009:
  972. ; For debugging, display resulting strings
  973. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  974.         mov     esi,document_user
  975.         call    debug_print_string
  976.         mov     esi,webAddr
  977.         call    debug_print_string
  978.         mov     esi,document
  979.         call    debug_print_string
  980. end     if
  981. ; Look up the ip address, or was it specified?
  982.         mov     al,[proxyAddr]
  983.         cmp     al,0
  984.         jnz     pu_015
  985.         mov     al,[webAddr]
  986. pu_015:
  987.         cmp     al,'0'
  988.         jb      pu_010  ; Resolve address
  989.         cmp     al,'9'
  990.         ja      pu_010  ; Resolve address
  991.  
  992. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  993.         mov     esi,str2        ; print gotip
  994.         call    debug_print_string
  995. end     if
  996. ; Convert address
  997. ; If proxy is given, get proxy address instead of server
  998.         mov     esi,proxyAddr-1
  999.         cmp     byte [esi+1],0
  1000.         jnz     pu_020
  1001.         mov     esi,webAddr-1
  1002.  
  1003. pu_020:
  1004.         mov     edi,server_ip
  1005.         xor     eax,eax
  1006.  
  1007. ip1:
  1008.         inc     esi
  1009.         cmp     [esi],byte '0'
  1010.         jb      ip2
  1011.         cmp     [esi],byte '9'
  1012.         jg      ip2
  1013.         imul    eax,10
  1014.         movzx   ebx,byte [esi]
  1015.         sub     ebx,48
  1016.         add     eax,ebx
  1017.         jmp     ip1
  1018.  
  1019. ip2:
  1020.         mov     [edi],al
  1021.         xor     eax,eax
  1022.         inc     edi
  1023.         cmp     edi,server_ip+3
  1024.         jbe     ip1
  1025.         jmp     pu_011
  1026.  
  1027. pu_010:
  1028. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1029.         mov     esi,str1        ; print resolving
  1030.         call    debug_print_string
  1031. end     if
  1032. ; Resolve Address
  1033.         call    translateData   ; Convert domain & DNS IP address
  1034.         call    resolveDomain   ; get ip address
  1035.  
  1036. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1037.         mov     esi,str3
  1038.         call    debug_print_string
  1039. end     if
  1040.  
  1041. pu_011:
  1042. ; Done
  1043.         ret
  1044.  
  1045. ;***************************************************************************
  1046. ;   Function
  1047. ;      translateData
  1048. ;
  1049. ;   Description
  1050. ;      Coverts the domain name and DNS IP address typed in by the user into
  1051. ;      a format suitable for the IP layer.
  1052. ;
  1053. ;    The ename, in query, is converted and stored in dnsMsg
  1054. ;
  1055. ;***************************************************************************
  1056. translateData:
  1057.     ; first, get the IP address of the DNS server
  1058.     ; Then, build up the request string.
  1059.  
  1060.     ; Build the request string
  1061.         mov     eax,0x00010100
  1062.         mov     [dnsMsg],eax
  1063.         mov     eax,0x00000100
  1064.         mov     [dnsMsg+4],eax
  1065.         mov     eax,0x00000000
  1066.         mov     [dnsMsg+8],eax
  1067. ; domain name goes in at dnsMsg+12
  1068.         mov     esi,dnsMsg +12  ;location of label length
  1069.         mov     edi,dnsMsg + 13 ;label start
  1070.         mov     edx,proxyAddr
  1071.         cmp     byte [edx],0
  1072.         jnz     td000
  1073.         mov     edx,webAddr
  1074.  
  1075. td000:
  1076.         mov     ecx,12  ; total string length so far
  1077.  
  1078. td002:
  1079.         mov     [esi],byte 0
  1080.         inc     ecx
  1081.  
  1082. td0021:
  1083.         mov     al,[edx]
  1084.         cmp     al,' '
  1085.         je      td001   ; we have finished the string translation
  1086.  
  1087.         cmp     al,0
  1088.         je      td001
  1089.  
  1090.         cmp     al,'.'  ; we have finished the label
  1091.         je      td004
  1092.  
  1093.         inc     byte [esi]
  1094.         inc     ecx
  1095.         mov     [edi],al
  1096.         inc     edi
  1097.         inc     edx
  1098.         jmp     td0021
  1099.  
  1100. td004:
  1101.         mov     esi,edi
  1102.         inc     edi
  1103.         inc     edx
  1104.         jmp     td002
  1105.  
  1106. ; write label len + label text
  1107. td001:
  1108.         mov     [edi],byte 0
  1109.         inc     ecx
  1110.         inc     edi
  1111.         mov     [edi],dword 0x01000100
  1112.         add     ecx,4
  1113.         mov     [dnsMsgLen],ecx
  1114.         ret
  1115.  
  1116. ;***************************************************************************
  1117. ;   Function
  1118. ;      resolveDomain
  1119. ;
  1120. ;   Description
  1121. ;       Sends a question to the dns server
  1122. ;       works out the IP address from the response from the DNS server
  1123. ;
  1124. ;***************************************************************************
  1125. resolveDomain:
  1126. ; Get a free port number
  1127.         mov     ecx,1000        ; local port starting at 1000
  1128. getlp:
  1129.         inc     ecx
  1130.         push    ecx
  1131.         mcall   53,9
  1132.         pop     ecx
  1133.         cmp     eax,0   ; is this local port in use?
  1134.         jz      getlp   ; yes - so try next
  1135.  
  1136. ; Get DNS IP
  1137.         mcall   52,13
  1138.         mov     esi,eax
  1139. ; First, open socket
  1140.         mov     edx,53  ; remote port - dns
  1141. ;       mov     esi,dword [dns_ip]
  1142.         xor     ebx,ebx ; 0
  1143.         mcall   53
  1144.         mov     [socketNum],eax
  1145. ; write to socket ( request DNS lookup )
  1146.         mcall   53,4,[socketNum],[dnsMsgLen],dnsMsg
  1147. ; Setup the DNS response buffer
  1148.         mov     eax,dnsMsg
  1149.         mov     [dnsMsgLen],eax
  1150. ; now, we wait for
  1151. ; UI redraw
  1152. ; UI close
  1153. ; or data from remote
  1154.  
  1155. ctr001:
  1156.         mcall   10      ; wait here for event
  1157.         cmp     eax,1   ; redraw request ?
  1158.         je      ctr003
  1159.  
  1160.         cmp     eax,2   ; key in buffer ?
  1161.         je      ctr004
  1162.  
  1163.         cmp     eax,3   ; button in buffer ?
  1164.         je      ctr005
  1165. ; Any data in the UDP receive buffer?
  1166.         mcall   53,2,[socketNum]
  1167.         cmp     eax,0
  1168.         je      ctr001
  1169.  
  1170. ; we have data - this will be the response
  1171. ctr002:
  1172.         mcall   53,3,[socketNum]        ; read byte - block (high byte)
  1173. ; Store the data in the response buffer
  1174.         mov     eax,[dnsMsgLen]
  1175.         mov     [eax],bl
  1176.         inc     dword [dnsMsgLen]
  1177.  
  1178.         mcall   53,2,[socketNum]        ; any more data?
  1179.         cmp     eax,0
  1180.         jne     ctr002  ; yes, so get it
  1181.  
  1182. ; close socket
  1183.         mcall   53,1,[socketNum]
  1184.         mov     [socketNum],dword  0xFFFF
  1185. ; Now parse the message to get the host IP
  1186. ; Man, this is complicated. It's described in
  1187. ; RFC 1035
  1188. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1189.         mov     esi,str4
  1190.         call    debug_print_string
  1191. end     if
  1192.  
  1193.     ; 1) Validate that we have an answer with > 0 responses
  1194.     ; 2) Find the answer record with TYPE 0001 ( host IP )
  1195.     ; 3) Finally, copy the IP address to the display
  1196.     ; Note: The response is in dnsMsg
  1197.     ;       The end of the buffer is pointed to by [dnsMsgLen]
  1198.  
  1199. ; Clear the IP address text
  1200.         mov     [server_ip],dword  0
  1201.         mov     esi,dnsMsg
  1202. ; Is this a response to my question?
  1203.         mov     al,[esi+2]
  1204.         and     al,0x80
  1205.         cmp     al,0x80
  1206.         jne     ctr002a
  1207. ; Were there any errors?
  1208.         mov     al,[esi+3]
  1209.         and     al,0x0F
  1210.         cmp     al,0x00
  1211.         jne     ctr002a
  1212. ; Is there ( at least 1 ) answer?
  1213.         mov     ax,[esi+6]
  1214.         cmp     ax,0x00
  1215.         je      ctr002a
  1216. ; Header valdated. Scan through and get my answer
  1217. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1218.         pusha
  1219.         mov     esi,str4
  1220.         call    debug_print_string
  1221.         popa
  1222. end     if
  1223.         add     esi,12  ; Skip to the question field
  1224. ; Skip through the question field
  1225.         call    skipName
  1226.         add     esi,4   ; skip past the questions qtype, qclass
  1227.  
  1228. ctr002z:
  1229. ; Now at the answer. There may be several answers,
  1230. ; find the right one ( TYPE = 0x0001 )
  1231.         call    skipName
  1232.         mov     ax,[esi]
  1233.         cmp     ax,0x0100       ; Is this the IP address answer?
  1234.         jne     ctr002c
  1235. ; Yes! Point esi to the first byte of the IP address
  1236.         add     esi,10
  1237.         mov     eax,[esi]
  1238.         mov     [server_ip],eax
  1239.         ret
  1240.  
  1241. ctr002c:        ; Skip through the answer, move to the next
  1242.         add     esi,8
  1243.         movzx   eax,byte [esi+1]
  1244.         mov     ah,[esi]
  1245.         add     esi,eax
  1246.         add     esi,2
  1247. ; Have we reached the end of the msg?
  1248. ; This is an error condition, should not happen
  1249.         cmp     esi,[dnsMsgLen]
  1250.         jl      ctr002z ; Check next answer
  1251.         jmp     ctr002a ; abort
  1252.  
  1253. ctr002a:
  1254.         jmp     ctr001
  1255.  
  1256. ctr003: ; redraw
  1257.         call    draw_window
  1258.         jmp     ctr001
  1259.  
  1260. ctr004: ; key
  1261.         mcall   2       ; just read it and ignore
  1262.         jmp     ctr001
  1263.  
  1264. ctr005: ; button
  1265.         mcall   17      ; get id
  1266.         mov     dl,ah
  1267.  
  1268.         ; close socket
  1269.         mcall   53,1,[socketNum]
  1270.         cmp     dl,1
  1271.         je      exit
  1272.  
  1273.         mov     [socketNum],dword  0xFFFF
  1274.         mov     [server_ip],dword  0
  1275.         ret
  1276.  
  1277. ;***************************************************************************
  1278. ;   Function
  1279. ;      skipName
  1280. ;
  1281. ;   Description
  1282. ;       Increment esi to the first byte past the name field
  1283. ;       Names may use compressed labels. Normally do.
  1284. ;       RFC 1035 page 30 gives details
  1285. ;
  1286. ;***************************************************************************
  1287. skipName:
  1288.         mov     al,[esi]
  1289.         cmp     al,0
  1290.         je      sn_exit
  1291.         and     al,0xc0
  1292.         cmp     al,0xc0
  1293.         je      sn001
  1294.  
  1295.         movzx   eax,byte [esi]
  1296.         inc     eax
  1297.         add     esi,eax
  1298.         jmp     skipName
  1299.  
  1300. sn001:
  1301.         add     esi,2   ; A pointer is always at the end
  1302.         ret
  1303.  
  1304. sn_exit:
  1305.         inc     esi
  1306.         ret
  1307.  
  1308. ;***************************************************************************
  1309. ;   Function
  1310. ;       load_settings
  1311. ;
  1312. ;   Description
  1313. ;       Load settings from configuration file network.ini
  1314. ;
  1315. ;***************************************************************************
  1316. load_settings:
  1317.         stdcall dll.Load,@IMPORT
  1318.         test    eax,eax
  1319.         jnz     ls001
  1320.         invoke  ini.get_str,inifile,sec_proxy,key_proxy,proxyAddr,256,proxyAddr
  1321.         invoke  ini.get_int,inifile,sec_proxy,key_proxyport,80
  1322.         mov     [proxyPort],eax
  1323.         invoke  ini.get_str,inifile,sec_proxy,key_user, proxyUser,256,proxyUser
  1324.         invoke  ini.get_str,inifile,sec_proxy,key_password,proxyPassword,256,proxyPassword
  1325. ls001:
  1326.         ret
  1327.  
  1328. ;***************************************************************************
  1329. ;   Function
  1330. ;       append_proxy_auth_header
  1331. ;
  1332. ;   Description
  1333. ;       Append header to HTTP request for proxy authentification
  1334. ;
  1335. ;***************************************************************************
  1336. append_proxy_auth_header:
  1337.         mov     esi,proxy_auth_basic
  1338.         mov     ecx,proxy_auth_basic_end - proxy_auth_basic
  1339.         rep     movsb
  1340. ; base64-encode string <user>:<password>
  1341.         mov     esi,proxyUser
  1342.  
  1343. apah000:
  1344.         lodsb
  1345.         test    al,al
  1346.         jz      apah001
  1347.         call    encode_base64_byte
  1348.         jmp     apah000
  1349.  
  1350. apah001:
  1351.         mov     al,':'
  1352.         call    encode_base64_byte
  1353.         mov     esi,proxyPassword
  1354.  
  1355. apah002:
  1356.         lodsb
  1357.         test    al,al
  1358.         jz      apah003
  1359.         call    encode_base64_byte
  1360.         jmp     apah002
  1361.  
  1362. apah003:
  1363.         call    encode_base64_final
  1364.         ret
  1365.  
  1366. encode_base64_byte:
  1367.         inc     ecx
  1368.         shl     edx,8
  1369.         mov     dl,al
  1370.         cmp     ecx,3
  1371.         je      ebb001
  1372.         ret
  1373.  
  1374. ebb001:
  1375.         shl     edx,8
  1376.         inc     ecx
  1377.  
  1378. ebb002:
  1379.         rol     edx,6
  1380.         xor     eax,eax
  1381.         xchg    al,dl
  1382.         mov     al,[base64_table+eax]
  1383.         stosb
  1384.         loop    ebb002
  1385.         ret
  1386.  
  1387. encode_base64_final:
  1388.         mov     al,0
  1389.         test    ecx,ecx
  1390.         jz      ebf000
  1391.         call    encode_base64_byte
  1392.         test    ecx,ecx
  1393.         jz      ebf001
  1394.         call    encode_base64_byte
  1395.         mov     byte [edi-2],'='
  1396.  
  1397. ebf001:
  1398.         mov     byte [edi-1],'='
  1399.  
  1400. ebf000:
  1401.         ret
  1402.  
  1403. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1404.  
  1405. ;****************************************************************************
  1406. ;    Function
  1407. ;       debug_print_string
  1408. ;
  1409. ;   Description
  1410. ;       prints a string to the debug board, in quotes
  1411. ;
  1412. ;       esi holds ptr to msg to display, which is space or 0 terminated
  1413. ;
  1414. ;       Nothing preserved; I'm assuming a pusha/popa is done before calling
  1415. ;
  1416. ;****************************************************************************
  1417. debug_print_string:
  1418.         push    esi
  1419.         mov     cl,'"'
  1420.         mcall   63,1
  1421.         pop     esi
  1422.  
  1423. dps_000:
  1424.         mov     cl,[esi]
  1425.         cmp     cl,0
  1426.         je      dps_exit
  1427.  
  1428.         cmp     cl,' '
  1429.         je      dps_exit
  1430.         jmp     dps_001
  1431.  
  1432. dps_exit:
  1433.         mov     cl,'"'
  1434.         mcall   63,1
  1435.         mov     cl,13
  1436.         mcall  
  1437.         mov     cl,10
  1438.         mcall
  1439.         ret
  1440.  
  1441. dps_001:
  1442.         push    esi
  1443.         mcall   63,1
  1444.         pop     esi
  1445.         inc     esi
  1446.         jmp     dps_000
  1447. end     if
  1448.  
  1449. ;****************************************************************************
  1450. ;    Function
  1451. ;       print_text
  1452. ;
  1453. ;   Description
  1454. ;       display the url (full path) text
  1455. ;
  1456. ;****************************************************************************
  1457. print_text:
  1458. ; Draw a bar to blank out previous text
  1459.         mov     ebx,30*65536+URLMAXLEN*6        ; 50 should really be [len] and 103 [xa]
  1460.         mov     ecx,[ya]
  1461.         shl     ecx,16
  1462.         mov     cx,9
  1463.         mcall   13,,,0xFFFFFF
  1464. ; write text
  1465.         mov     ebx,30*65536
  1466.         add     ebx,[ya]
  1467.         xor     ecx,ecx
  1468.         mcall   4,,,[addr],URLMAXLEN
  1469.         ret
  1470.  
  1471. ;   *********************************************
  1472. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  1473. ;   *********************************************
  1474.  
  1475. draw_window:
  1476. ; function 12: tell os about windowdraw
  1477. ; 1 start of draw
  1478.         mcall   12,1
  1479.  
  1480. ;       cmp     [params],byte 0
  1481. ;       jz      .noret
  1482.  
  1483. ; ýòî íåñêîëüêî çàãàäî÷íî, íî åñëè íå ðèñîâàòü îêîøêî ñîâñåì, ïðîãà íå ïàøåò.
  1484. ; DRAW  WINDOW
  1485. ; eax   function 0 : define and draw window
  1486. ;       xor     eax,eax
  1487. ; ebx   [x start] *65536 + [x size]
  1488. ;       xor     ebx,ebx
  1489. ; ecx   [y start] *65536 + [y size]
  1490. ;       xor     ecx,ecx
  1491. ; edx   color of work area RRGGBB,8->color gl
  1492. ; esi   color of bar and flags
  1493. ;       xor     esi,esi
  1494. ;       mcall   ,,,0x14ffffff,,title
  1495.  
  1496. ; function 12: tell os about windowdraw
  1497. ; 2, end of draw
  1498. ;       mcall   12,2
  1499. ;       ret
  1500.        
  1501. ;.noret:
  1502. ; DRAW  WINDOW
  1503. ; eax   function 0 : define and draw window
  1504.         xor     eax,eax
  1505. ; ebx   [x start] *65536 + [x size]
  1506. ; ecx   [y start] *65536 + [y size]
  1507. ; edx   color of work area RRGGBB,8->color gl
  1508. ; esi   color of bar and flags
  1509.         xor     esi,esi
  1510. ; edi   WINDOW  LABEL
  1511.         mcall   ,<50,600>,<350,200>,0x14ffffff,,title
  1512. ; eax   function 4: write text to window
  1513. ; ebx   [x start] *65536 + [y start]
  1514. ; ecx   color of text RRGGBB
  1515. ; edx   pointer to text beginning
  1516. ; esi   max lenght
  1517.         xor     ecx,ecx
  1518.         mcall   4,<30,38>,,document_user,URLMAXLEN
  1519.  
  1520. ;       xor     edx,edx
  1521. ;       mcall   38,<5,545>,<60,60>
  1522.  
  1523. ;       mov     ecx,[winys]
  1524. ;       shl     ecx,16
  1525. ;       add     ecx,[winys]
  1526. ;       sub     ecx,26*65536+26
  1527. ;       mcall   38,<5,545>
  1528.  
  1529. ; RELOAD
  1530. ; eax   function 8 : define and draw button
  1531. ; ebx   [x start] *65536 + [x size]
  1532. ; ecx   [y start] *65536 + [y size]
  1533. ; edx   button id
  1534. ; esi   button color RRGGBB
  1535.         mcall   8,<388,50>,<34,14>,22,0x5588dd
  1536. ; URL
  1537.         mcall   ,<10,12>,<34,12>,10
  1538. ; STOP
  1539.         mcall   ,<443,50>,<34,14>,24
  1540. ; SAVE
  1541.         mcall   ,<498,50>,,26
  1542. ; BUTTON TEXT
  1543. ; eax   function 4 : write text to window
  1544. ; ebx   [x start] *65536 + [y start]
  1545. ; ecx   color   of      text    RRGGBB
  1546. ; edx   pointer to      text    beginning
  1547. ; esi   text    length
  1548.         mcall   4,<390,38>,0xffffff,button_text,30
  1549.         call    display_page
  1550.  
  1551. ; function 12: tell os about windowdraw
  1552. ; 2, end of draw
  1553.         mcall   12,2
  1554.         ret
  1555. ;-----------------------------------------------------------------------------
  1556. ; Data area
  1557. ;-----------------------------------------------------------------------------
  1558. align   4
  1559. @IMPORT:
  1560.  
  1561. library libini,'libini.obj'
  1562.  
  1563. import  libini, \
  1564.         ini.get_str,'ini_get_str', \
  1565.         ini.get_int,'ini_get_int'
  1566.  
  1567. ;---------------------------------------------------------------------
  1568. fileinfo        dd 2,0,0
  1569. final_size      dd 0
  1570. final_buffer    dd 0
  1571.                 db '/rd/1/.download',0
  1572.        
  1573. body_pos        dd 0
  1574.  
  1575. ;fileinfo_tmp   dd 2,0,0
  1576. buf_size        dd 0
  1577. buf_ptr         dd 0
  1578. ;               db      '/rd/1/1',0
  1579.  
  1580. deba            dd 0
  1581.                 db 0
  1582. ;---------------------------------------------------------------------
  1583. if      DEBUGGING_STATE = DEBUGGING_ENABLED
  1584. str1:           db "Resolving...",0
  1585. str3:           db "Resolved",0
  1586. str2:           db "GotIP",0
  1587. str4:           db "GotResponse",0
  1588. end     if
  1589. ;---------------------------------------------------------------------
  1590. button_text     db ' RELOAD    STOP     SAVE      '
  1591. dpx             dd 25   ; x - start of html page in pixels in window
  1592. dpy             dd 65   ; for   y
  1593. lastletter      db 0
  1594. pageyinc        dd 0
  1595. display_from    dd 20
  1596. pos             dd 0x0
  1597. pagex           dd 0x0
  1598. pagey           dd 0x0
  1599. pagexs          dd 80
  1600. command_on_off  dd 0x0
  1601. text_type       db 1
  1602. com2            dd 0x0
  1603. script          dd 0x0
  1604. socket          dd 0x0
  1605.  
  1606. addr            dd 0x0
  1607. ya              dd 0x0
  1608. len             dd 0x00
  1609.  
  1610. title           db 'Downloader',0
  1611.  
  1612. server_ip:      db 207,44,212,20
  1613. ;dns_ip:        db 194,145,128,1
  1614. ;---------------------------------------------------------------------
  1615. ;webAddr:
  1616. ;times URLMAXLEN db ' '
  1617. ;db     0
  1618.  
  1619. ;document_user: db 'Click on the button to the left to enter a URL',0
  1620. ;times URLMAXLEN+document_user-$ db 0
  1621.  
  1622. ;document:      db '/'
  1623. ;times URLMAXLEN-1 db ' '
  1624. ;---------------------------------------------------------------------
  1625. s_contentlength db 'Content-Length:'
  1626. len_contentlength = 15
  1627.  
  1628. s_chunked       db 'Transfer-Encoding: chunked'
  1629. len_chunked     = $ - s_chunked
  1630.  
  1631. is_body         dd 0    ; 0 if headers, 1 if content
  1632. is_chunked      dd 0
  1633. prev_chunk_end  dd 0
  1634. cur_chunk_size  dd 0
  1635.  
  1636. string0:        db 'GET '
  1637.  
  1638. stringh:        db ' HTTP/1.1',13,10,'Host: '
  1639. stringh_end:
  1640. proxy_auth_basic:       db 13,10,'Proxy-Authorization: Basic '
  1641. proxy_auth_basic_end:
  1642. connclose:      db 13,10,'User-Agent: Kolibrios Downloader',13,10,'Connection: Close',13,10,13,10
  1643. connclose_end:
  1644.  
  1645. base64_table    db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  1646.                 db '0123456789+/'
  1647.  
  1648. inifile         db '/sys/network/zeroconf.ini',0
  1649.  
  1650. sec_proxy:
  1651. key_proxy       db 'proxy',0
  1652. key_proxyport   db 'port',0
  1653. key_user        db 'user',0
  1654. key_password    db 'password',0
  1655.  
  1656.  
  1657. proxyPort       dd 80
  1658.  
  1659. shared_name     dd 0
  1660.  
  1661. ;yandex:        db 'menuetos.net'
  1662. ;yandex_end:
  1663.  
  1664. status          dd 0x0
  1665. prev_status     dd 0x0
  1666.  
  1667. onoff           dd 0x0
  1668.  
  1669. nextupdate:     dd 0
  1670. winys:          dd 400
  1671.  
  1672. dnsMsgLen:      dd 0
  1673. socketNum:      dd 0xFFFF
  1674. ;---------------------------------------------------------------------
  1675. document_user:  db 'Click on the button to the left to enter a URL',0
  1676. ;---------------------------------------------------------------------
  1677. IM_END:
  1678.         rb URLMAXLEN-(IM_END - document_user)
  1679. ;---------------------------------------------------------------------
  1680. align 4
  1681. document:
  1682.         rb URLMAXLEN
  1683. ;---------------------------------------------------------------------
  1684. align 4
  1685. webAddr:
  1686.         rb URLMAXLEN+1
  1687. ;---------------------------------------------------------------------
  1688. align 4
  1689. primary_buf:
  1690.         rb primary_buffer_size
  1691. ;---------------------------------------------------------------------
  1692. align 4
  1693. params:         ; db 1024 dup(0)
  1694.         rb 1024
  1695. ;---------------------------------------------------------------------
  1696. align 4
  1697. request:        ; db 256 dup(0)
  1698.         rb 256
  1699. ;---------------------------------------------------------------------
  1700. align 4
  1701. proxyAddr:      ; db 256 dup(0)
  1702.         rb 256
  1703. ;---------------------------------------------------------------------
  1704. align 4
  1705. proxyUser:      ; db 256 dup(0)
  1706.         rb 256
  1707. ;---------------------------------------------------------------------
  1708. align 4
  1709. proxyPassword:  ; db 256 dup(0)
  1710.         rb 256
  1711. ;---------------------------------------------------------------------
  1712. align 4
  1713. dnsMsg:
  1714.         rb 4096
  1715. ;       rb 0x100000
  1716. ;---------------------------------------------------------------------
  1717. align 4
  1718.         rb 4096
  1719. stacktop:
  1720. ;---------------------------------------------------------------------
  1721. I_END:
  1722. ;---------------------------------------------------------------------