Subversion Repositories Kolibri OS

Rev

Rev 4431 | Rev 5543 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2014-2015. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  downloader.asm - HTTP client for KolibriOS                     ;;
  7. ;;                                                                 ;;
  8. ;;      hidnplayr@kolibrios.org                                    ;;
  9. ;;                                                                 ;;
  10. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  11. ;;             Version 2, June 1991                                ;;
  12. ;;                                                                 ;;
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.  
  15. URLMAXLEN       = 65535
  16.  
  17. __DEBUG__       = 1
  18. __DEBUG_LEVEL__ = 1
  19.  
  20.  
  21. format binary as ""
  22. use32
  23.         org     0x0
  24.  
  25.         db      'MENUET01'      ; header
  26.         dd      0x01            ; header version
  27.         dd      START           ; entry point
  28.         dd      IM_END          ; image size
  29.         dd      I_END+0x1000    ; required memory
  30.         dd      I_END+0x1000    ; esp
  31.         dd      url
  32.         dd      0x0             ; I_Path
  33.  
  34.  
  35. include '../../macros.inc'
  36. include '../../proc32.inc'
  37. include '../../dll.inc'
  38. include '../../debug-fdo.inc'
  39. include '../../develop/libraries/box_lib/trunk/box_lib.mac'
  40. include '../../develop/libraries/http/http.inc'
  41.  
  42. virtual at 0
  43.         http_msg http_msg
  44. end virtual
  45.  
  46.  
  47. START:
  48.         mcall   68, 11                  ; init heap so we can allocate memory dynamically
  49.  
  50. ; load libraries
  51.         stdcall dll.Load, @IMPORT
  52.         test    eax, eax
  53.         jnz     exit
  54.  
  55. ; wanted events
  56.         mcall   40, EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER
  57.  
  58. ; prepare filename buffers
  59.         mov     edi, fname_buf
  60.         mov     esi, download_file_path
  61.   @@:
  62.         lodsb
  63.         stosb
  64.         test    al, al
  65.         jnz     @r
  66.  
  67. ; Initialise OpenDialog
  68.         invoke  OpenDialog_Init, OpenDialog_data
  69.  
  70. ; If user provided parameters, start download right away!
  71.         cmp     byte[url], 0
  72.         jne     download
  73.  
  74.         mov     [OpenDialog_data.draw_window], draw_window
  75.  
  76. redraw:
  77.         call    draw_window
  78.  
  79. mainloop:
  80.         mcall   10      ; wait here for event
  81.         cmp     eax, EV_REDRAW
  82.         je      redraw
  83.         cmp     eax, EV_KEY
  84.         je      .key
  85.         cmp     eax, EV_BUTTON
  86.         je      .button
  87.         cmp     eax, EV_MOUSE
  88.         je      .mouse
  89.         jmp     mainloop
  90.  
  91.   .key:
  92.         mcall   2       ; read key
  93.         invoke  edit_box_key, edit1
  94.         cmp     ax, 13 shl 8
  95.         je      download
  96.         jmp     mainloop
  97.  
  98.   .button:
  99.         mcall   17      ; get id
  100.         cmp     ah, 1   ; button id=1 ?
  101.         je      exit
  102.  
  103.         cmp     [btn_text], sz_download
  104.         je      download
  105.  
  106.         cmp     [btn_text], sz_open
  107.         je      open_file
  108.  
  109.   .mouse:
  110.         invoke  edit_box_mouse, edit1
  111.         jmp     mainloop
  112.  
  113. open_file:
  114. ;        mcall   70, ...
  115.         jmp     mainloop
  116.  
  117. exit:
  118. error:
  119.         mcall   -1      ; exit
  120.  
  121. download:
  122. ;        int 3
  123. ; Extract the filename from URL
  124.         mov     edi, url
  125.         xor     al, al
  126.         mov     ecx, URLMAXLEN
  127.         repne scasb
  128.         mov     esi, edi
  129.         dec     esi
  130.         dec     esi
  131.         std
  132.   .loop:
  133.         lodsb
  134.         cmp     al, '/'
  135.         je      .done
  136.         test    al, al
  137.         jnz     .loop
  138.   .done:
  139.         cld
  140.         mov     ecx, edi
  141.         sub     ecx, esi
  142.         inc     esi
  143.         inc     esi
  144.         mov     edi, filename_area
  145.         rep movsb
  146.  
  147. ; invoke OpenDialog
  148.         invoke  OpenDialog_Start, OpenDialog_data
  149.         mcall   40, EVM_REDRAW + EVM_BUTTON + EVM_STACK
  150.         call    draw_window
  151.  
  152. ; Create the file
  153.         mov     [fileinfo], 2           ; create/write to file
  154.         xor     eax, eax
  155.         mov     [fileinfo.offset], eax
  156.         mov     [fileinfo.offset+4], eax
  157.         mov     [fileinfo.size], eax
  158.         mcall   70, fileinfo
  159.         test    eax, eax
  160.         jnz     error                   ; TODO: print error message
  161.  
  162. ; And start the download
  163.         invoke  HTTP_get, url, 0, FLAG_STREAM or FLAG_REUSE_BUFFER, 0
  164.         test    eax, eax
  165.         jz      error                   ; TODO: print error message
  166.         mov     [identifier], eax
  167.         mov     [offset], 0
  168.         mov     [btn_text], sz_cancel
  169.  
  170.         or      [edit1.flags], ed_figure_only
  171.         call    draw_window
  172.  
  173. download_loop:
  174.         mcall   10
  175.         cmp     eax, EV_REDRAW
  176.         je      .redraw
  177.         cmp     eax, EV_BUTTON
  178.         je      .button
  179.  
  180.         invoke  HTTP_receive, [identifier]
  181.         test    eax, eax
  182.         jz      save_chunk
  183.  
  184.         mov     eax, [identifier]
  185.         push    [eax + http_msg.content_length]
  186.         pop     [pb.max]
  187.         push    [eax + http_msg.content_received]
  188.         pop     [pb.value]
  189.  
  190.         invoke  progressbar_draw, pb
  191.         jmp     download_loop
  192.  
  193.   .redraw:
  194.         call    draw_window
  195.         jmp     download_loop
  196.  
  197.   .button:
  198.         jmp     http_free
  199.  
  200. save_chunk:
  201.         mov     ebp, [identifier]
  202.         test    [ebp + http_msg.flags], 0xffff0000      ; error?
  203.         jnz     http_free
  204.  
  205.         cmp     [fileinfo], 3
  206.         je      @f
  207.         DEBUGF  1, "new file size=%u\n", [ebp + http_msg.content_length]
  208.         mov     [fileinfo], 4                           ; set end of file
  209.         mov     eax, [ebp + http_msg.content_length]
  210.         mov     [fileinfo.offset], eax                  ; new file size
  211.         mcall   70, fileinfo
  212.  
  213.         mov     [fileinfo], 3                           ; write to existing file
  214.   @@:
  215.         mov     ecx, [ebp + http_msg.content_received]
  216.         sub     ecx, [offset]
  217.         mov     [fileinfo.size], ecx
  218.         mov     eax, [ebp + http_msg.content_ptr]
  219.         mov     [fileinfo.buffer], eax
  220.         mov     ebx, [offset]
  221.         mov     [fileinfo.offset], ebx
  222.         DEBUGF  1, "Writing to disk: size=%u offset=%u\n", ecx, ebx
  223.         mcall   70, fileinfo
  224.  
  225.         mov     eax, [ebp + http_msg.content_received]
  226.         mov     [offset], eax
  227.  
  228.         test    [ebp + http_msg.flags], FLAG_GOT_ALL_DATA
  229.         jz      download_loop
  230.  
  231.         mov     [pb.progress_color], 0x0000c800         ; green
  232.  
  233. http_free:
  234.         mcall   40, EVM_REDRAW + EVM_BUTTON
  235.         push    [ebp + http_msg.content_received]
  236.         pop     [pb.value]
  237.  
  238.         mov     ecx, [ebp + http_msg.content_ptr]
  239.         test    ecx, ecx
  240.         jz      @f
  241.         mcall   68, 13                                  ; free the buffer
  242.   @@:
  243.  
  244.         invoke  HTTP_free, [identifier]                 ; free headers and connection
  245.  
  246.         mov     [btn_text], sz_open
  247.         call    draw_window
  248.         jmp     mainloop
  249.  
  250.  
  251.  
  252. draw_window:
  253.         mcall   12, 1   ; start window draw
  254.  
  255. ; get system colors
  256.         mcall   48, 3, sc, 40
  257.  
  258. ; draw window
  259.         mov     edx, [sc.work]
  260.         or      edx, 0x34000000
  261.         mcall   0, <50, 320>, <350, 110>, , 0, title
  262.  
  263. ; draw button
  264.         mcall   8, <229,75>, <60,16>, 22, [sc.work_button]      ; download
  265.  
  266. ; draw button text
  267.         mov     ecx, [sc.work_button_text]
  268.         or      ecx, 80000000h
  269.         mcall   4, <240,65>, , [btn_text]
  270.  
  271. ; draw editbox
  272.         edit_boxes_set_sys_color edit1, editboxes_end, sc
  273.         invoke  edit_box_draw, edit1
  274.  
  275.         cmp     [identifier], 0
  276.         je     @f
  277. ; draw progressbar
  278.         invoke  progressbar_draw, pb
  279.   @@:
  280.         mcall   12, 2   ; end window draw
  281.         ret
  282.  
  283.  
  284. dont_draw:
  285.  
  286.         ret
  287.  
  288. ;---------------------------------------------------------------------
  289. ; Data area
  290. ;-----------------------------------------------------------------------------
  291. align   4
  292. @IMPORT:
  293.  
  294. library lib_http,       'http.obj', \
  295.         box_lib,        'box_lib.obj', \
  296.         proc_lib,       'proc_lib.obj'
  297.  
  298. import  lib_http, \
  299.         HTTP_get,       'get', \
  300.         HTTP_receive,   'receive', \
  301.         HTTP_free,      'free'
  302.  
  303. import  box_lib, \
  304.         edit_box_draw,    'edit_box', \
  305.         edit_box_key,     'edit_box_key', \
  306.         edit_box_mouse,   'edit_box_mouse', \
  307.         progressbar_draw, 'progressbar_draw', \
  308.         progressbar_prog, 'progressbar_progress'
  309.  
  310. import  proc_lib, \
  311.         OpenDialog_Init,  'OpenDialog_init', \
  312.         OpenDialog_Start, 'OpenDialog_start'
  313.  
  314.  
  315. fileinfo        dd 2
  316.   .offset       dd 0, 0
  317.   .size         dd 0
  318.   .buffer       dd 0
  319.                 db 0
  320.                 dd fname_buf
  321.  
  322. edit1           edit_box 299, 5, 10, 0xffffff, 0x0000ff, 0x0080ff, 0x000000, 0x8000, URLMAXLEN, url, mouse_dd, ed_focus+ed_always_focus, 0, 0
  323. editboxes_end:
  324.  
  325. identifier      dd 0
  326. btn_text        dd sz_download
  327. sz_download     db 'Download', 0
  328. sz_cancel       db ' Cancel ', 0
  329. sz_open         db '  Open  ', 0
  330. title           db 'HTTP Downloader', 0
  331.  
  332. OpenDialog_data:
  333. .type                   dd 1    ; Save
  334. .procinfo               dd procinfo
  335. .com_area_name          dd communication_area_name
  336. .com_area               dd 0
  337. .opendir_path           dd temp_dir_path
  338. .dir_default_path       dd communication_area_default_path
  339. .start_path             dd open_dialog_path
  340. .draw_window            dd dont_draw
  341. .status                 dd 0
  342. .openfile_patch         dd fname_buf
  343. .filename_area          dd filename_area
  344. .filter_area            dd filter
  345. .x:
  346. .x_size                 dw 420  ; Window X size
  347. .x_start                dw 200  ; Window X position
  348. .y:
  349. .y_size                 dw 320  ; Window y size
  350. .y_start                dw 120  ; Window Y position
  351.  
  352. communication_area_name         db 'FFFFFFFF_open_dialog',0
  353. open_dialog_path                db '/sys/File Managers/opendial',0
  354. communication_area_default_path db '/sys',0
  355.  
  356. filter:
  357. dd      0
  358. db      0
  359.  
  360. pb:
  361. .value          dd 0
  362. .left           dd 5
  363. .top            dd 35
  364. .width          dd 300
  365. .height         dd 16
  366. .style          dd 1
  367. .min            dd 0
  368. .max            dd 0
  369. .back_color     dd 0xefefef
  370. .progress_color dd 0xc8c8c8
  371. .frame_color    dd 0x94aece
  372. .frame_color2   dd 0xffffff
  373.  
  374. include_debug_strings
  375.  
  376. download_file_path db '/tmp0/1/', 0
  377.  
  378. IM_END:
  379.  
  380. url             rb URLMAXLEN
  381. sc              system_colors
  382. offset          dd ?
  383. mouse_dd        dd ?
  384.  
  385. filename_area   rb 256
  386. temp_dir_path   rb 4096
  387. procinfo        rb 1024
  388. fname_buf       rb 4096
  389. text_work_area  rb 1024
  390.  
  391. I_END: