Subversion Repositories Kolibri OS

Rev

Rev 5539 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2009-2015. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  downloader.asm - HTTP client for KolibriOS                     ;;
  7. ;;                                                                 ;;
  8. ;;                                                                 ;;
  9. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  10. ;;             Version 2, June 1991                                ;;
  11. ;;                                                                 ;;
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13.  
  14. URLMAXLEN       = 1024
  15. BUFFERSIZE      = 4096
  16.  
  17. __DEBUG__       = 1
  18. __DEBUG_LEVEL__ = 1
  19.  
  20. format binary as ""
  21. ;--------------------------------------
  22. frame_1:
  23.   .x      = 5
  24.   .y      = 10
  25.   .width  = 350
  26.   .height = 55
  27. ;--------------------------------------  
  28. frame_2:
  29.   .x      = 5
  30.   .y      = 75
  31.   .width  = 350
  32.   .height = 55
  33. ;---------------------------------------------------------------------
  34. use32
  35.         org     0x0
  36.  
  37.         db      'MENUET01'      ; header
  38.         dd      0x01            ; header version
  39.         dd      START           ; entry point
  40.         dd      IM_END          ; image size
  41.         dd      I_END           ; required memory
  42.         dd      stacktop        ; esp
  43.         dd      params          ; I_PARAM
  44.         dd      0x0             ; I_Path
  45. ;---------------------------------------------------------------------
  46. include '../../../../macros.inc'
  47. include '../../../../proc32.inc'
  48. include '../../../../dll.inc'
  49. include '../../../../debug-fdo.inc'
  50. include '../../box_lib/trunk/box_lib.mac'
  51. include '../../http/http.inc'
  52.  
  53. virtual at 0
  54.         http_msg http_msg
  55. end virtual
  56. ;---------------------------------------------------------------------
  57. START:
  58.  
  59.         mcall   68, 11                  ; init heap so we can allocate memory dynamically
  60.  
  61. ; load libraries
  62.         stdcall dll.Load, @IMPORT
  63.         test    eax, eax
  64.         jnz     exit
  65. ;---------------------------------------------------------------------
  66.         mov     edi,filename_area
  67.         mov     esi,start_temp_file_name
  68.         call    copy_file_name_path
  69.  
  70.         mov     edi,fname_buf
  71.         mov     esi,start_file_path
  72.         call    copy_file_name_path
  73.  
  74. ;OpenDialog     initialisation
  75.         push    dword OpenDialog_data
  76.         call    [OpenDialog_Init]
  77.  
  78. ; prepare for PathShow
  79.         push    dword PathShow_data_1
  80.         call    [PathShow_prepare]
  81. ;---------------------------------------------------------------------
  82. ; check parameters
  83.         cmp     byte[params], 0         ; no parameters ?
  84.         je      reset_events            ; load the GUI
  85.  
  86.         inc     [silently]
  87.  
  88. download:
  89.         call    download_1
  90.  
  91.         test    [silently], 0xff
  92.         jnz     save
  93.  
  94. reset_events:
  95.         DEBUGF  1, "resetting events\n"
  96.  
  97. ; Report events
  98. ; defaults + mouse
  99.         mcall   40,EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER
  100. ;---------------------------------------------------------------------
  101. redraw:
  102.         call    draw_window
  103.  
  104. still:
  105. ;;        DEBUGF  1, "waiting for events\n"
  106.  
  107.         mcall   10      ; wait here for event
  108.  
  109.         cmp     eax, EV_REDRAW
  110.         je      redraw
  111.  
  112.         cmp     eax, EV_KEY
  113.         je      key
  114.  
  115.         cmp     eax, EV_BUTTON
  116.         je      button
  117.        
  118.         cmp     eax, EV_MOUSE
  119.         je      mouse
  120.  
  121.         jmp     still
  122. ;---------------------------------------------------------------------
  123. key:
  124.         mcall   2       ; read key
  125.  
  126.         stdcall [edit_box_key], dword edit1
  127.  
  128.         cmp     ax, 13 shl 8
  129.         je      download
  130.        
  131.         jmp     still
  132. ;---------------------------------------------------------------------        
  133. button:
  134.  
  135.         mcall   17      ; get id
  136.  
  137.         cmp     ah, 26
  138.         jne     @f
  139. ; invoke OpenDialog
  140.         push    dword OpenDialog_data
  141.         call    [OpenDialog_Start]
  142.         cmp     [OpenDialog_data.status],1
  143.         jne     still
  144.  
  145. ; prepare for PathShow
  146.         push    dword PathShow_data_1
  147.         call    [PathShow_prepare]
  148.         call    draw_window
  149.         jmp     still
  150. @@:
  151.         cmp     ah, 1   ; button id=1 ?
  152.         je      exit
  153.  
  154.         call    download_1
  155.         jmp     save
  156. ;---------------------------------------------------------------------
  157. mouse:
  158.         stdcall [edit_box_mouse], edit1
  159.         jmp     still
  160. ;---------------------------------------------------------------------
  161. exit:
  162.         DEBUGF  1, "Exiting\n"
  163.         invoke  HTTP_free, [identifier] ; free buffer
  164. fail:
  165.         or      eax, -1 ; close this program
  166.         mcall
  167. ;---------------------------------------------------------------------
  168. download_1:
  169.         DEBUGF  1, "Starting download\n"
  170.  
  171.         invoke  HTTP_get, 0, 0, params, sz_add_header
  172.         test    eax, eax
  173.         jz      fail
  174.         mov     [identifier], eax
  175.  
  176.   .loop:
  177.         invoke  HTTP_receive, [identifier]
  178.         test    eax, eax
  179.         jnz     .loop
  180.  
  181.         mov     eax, [identifier]
  182.         mov     ebx, [eax + http_msg.content_length]
  183.         mov     [final_size], ebx
  184.         mov     ebx, [eax + http_msg.content_ptr]
  185.         mov     [final_buffer], ebx
  186.  
  187.         invoke  HTTP_find_header, [identifier], sz_content_enc
  188.         test    eax, eax
  189.         jz      .no_deflate
  190.  
  191. ; TODO: check if parameter equals "deflate"
  192.  
  193.         DEBUGF  1, "deflating\n"
  194.  
  195.         lea     eax, [final_size]
  196.         invoke  deflate_unpack, [final_buffer], eax
  197.         push    eax
  198.         mcall   68, 13, [final_buffer]             ; Free the compressed data
  199.         pop     [final_buffer]
  200.  
  201.         DEBUGF  1, "deflated size=%u bytes\n", [final_size]
  202.   .no_deflate:
  203.  
  204.         invoke  HTTP_free, [identifier]
  205.         mov     [identifier], 0
  206.  
  207.         ret
  208. ;---------------------------------------------------------------------
  209. save:
  210.         cmp     [final_buffer], 0
  211.         je      still
  212.  
  213.         mcall   70, fileinfo
  214.  
  215.         DEBUGF  1, "File saved\n"
  216.  
  217.         test    [silently], 0xff
  218.         jnz     exit
  219.  
  220.         mov     ecx, [sc.work_text]
  221.         or      ecx, 0x80000000
  222.         mcall   4, <10, frame_2.y+frame_2.height+7>, , download_complete
  223.  
  224.         jmp     still
  225. ;---------------------------------------------------------------------
  226. copy_file_name_path:
  227.         xor     eax,eax
  228.         cld
  229. @@:
  230.         lodsb
  231.         stosb
  232.         test    eax,eax
  233.         jnz     @r
  234.         ret
  235. ;---------------------------------------------------------------------
  236. ;   *********************************************
  237. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  238. ;   *********************************************
  239.  
  240. draw_window:
  241.  
  242.         mcall   12, 1   ; start window draw
  243. ;-----------------------------------
  244. ; get system colors
  245.         mcall   48, 3, sc, 40
  246. ;-----------------------------------
  247. ; draw window
  248.         mov     edx, [sc.work]
  249.         or      edx, 0x34000000
  250.         mcall   0, <50, 370>, <350, 170>, , 0, title
  251. ;-----------------------------------
  252. ; draw frames
  253.         mov     [frame_data.x],dword frame_1.x shl 16+frame_1.width
  254.         mov     [frame_data.y],dword frame_1.y shl 16+frame_1.height
  255.         mov     [frame_data.text_pointer],dword select_addr_text
  256.         mov     eax,[sc.work]
  257.         mov     [frame_data.font_backgr_color],eax
  258.         mov     eax,[sc.work_text]
  259.         mov     [frame_data.font_color],eax
  260.        
  261.         push    dword frame_data
  262.         call    [Frame_draw]
  263. ;-----------------------------------
  264.         mov     [frame_data.x],dword frame_2.x shl 16+frame_2.width
  265.         mov     [frame_data.y],dword frame_2.y shl 16+frame_2.height
  266.         mov     [frame_data.text_pointer],dword select_path_text
  267.  
  268.         push    dword frame_data
  269.         call    [Frame_draw]
  270. ;-----------------------------------
  271. ; draw "url:" text
  272.         mov     ecx, [sc.work_text]
  273.         or      ecx, 80000000h
  274.         mcall   4, <frame_1.x+10, frame_1.y+15>, , type_pls
  275. ;-----------------------------------
  276. ; draw editbox
  277.         edit_boxes_set_sys_color edit1, editboxes_end, sc
  278.         stdcall [edit_box_draw], edit1
  279. ;-----------------------------------
  280. ; draw buttons
  281.         mcall   8,<frame_1.x+frame_1.width-(68+15+50+15),68>,<frame_1.y+30,16>,22,[sc.work_button] ; reload
  282.         mcall   ,<frame_1.x+frame_1.width-(50+15),50>,<frame_1.y+30,16>, 24 ; stop
  283.        
  284.         mcall   , <frame_2.x+frame_2.width-(54+15),54>,<frame_2.y+30,16>,26 ; save
  285. ;-----------------------------------
  286. ; draw buttons text
  287.         mov     ecx, [sc.work_button_text]
  288.         or      ecx, 80000000h
  289.         mcall   4, <frame_1.x+frame_1.width-(68+15+50+15)+10,frame_1.y+35>, , button_text.1
  290.         mcall   , <frame_1.x+frame_1.width-(50+15)+15,frame_1.y+35>, , button_text.2
  291.         mcall   , <frame_2.x+frame_2.width-(54+15)+10,frame_2.y+35>, , button_text.3
  292.        
  293.         mcall   13,<frame_2.x+17,frame_2.width-15*2>,<frame_2.y+10,15>,0xffffff
  294.         push    dword PathShow_data_1
  295.         call    [PathShow_draw]
  296.        
  297.         mcall   12, 2   ; end window redraw
  298.  
  299.         ret
  300. ;---------------------------------------------------------------------
  301. ; Data area
  302. ;-----------------------------------------------------------------------------
  303. align   4
  304. @IMPORT:
  305.  
  306. library lib_http,       'http.obj', \
  307.         box_lib,        'box_lib.obj', \
  308.         proc_lib,       'proc_lib.obj', \
  309.         archiver,       'archiver.obj'
  310.  
  311. import  lib_http, \
  312.         HTTP_get,       'get' , \
  313.         HTTP_receive,   'receive', \
  314.         HTTP_find_header, 'find_header_field', \
  315.         HTTP_free,      'free'
  316.  
  317. import  box_lib, \
  318.         edit_box_draw,    'edit_box_draw', \
  319.         edit_box_key,     'edit_box_key', \
  320.         edit_box_mouse,   'edit_box_mouse', \
  321.         PathShow_prepare, 'PathShow_prepare', \
  322.         PathShow_draw,    'PathShow_draw', \
  323.         Frame_draw,       'frame_draw'
  324.  
  325. import  proc_lib, \
  326.         OpenDialog_Init,  'OpenDialog_init', \
  327.         OpenDialog_Start, 'OpenDialog_start'
  328.  
  329. import  archiver,\
  330.         deflate_unpack,   'deflate_unpack'
  331. ;---------------------------------------------------------------------
  332. fileinfo        dd 2, 0, 0
  333. final_size      dd 0
  334. final_buffer    dd 0
  335.                 db 0
  336.                 dd fname_buf
  337. ;---------------------------------------------------------------------
  338.  
  339. mouse_dd        dd 0
  340. edit1           edit_box 295, 48, (frame_1.y+10), 0xffffff, 0xff, 0x80ff, 0, 0x8000, URLMAXLEN, document_user, mouse_dd, ed_focus+ed_always_focus, 7, 7
  341. editboxes_end:
  342.  
  343. ;---------------------------------------------------------------------
  344.  
  345. include_debug_strings
  346.  
  347. ;---------------------------------------------------------------------
  348.  
  349. type_pls        db 'URL:', 0
  350. button_text:
  351. .1:             db 'DOWNLOAD',0
  352. .2:             db 'STOP',0
  353. .3:             db 'SELECT', 0
  354. download_complete db 'FILE SAVED!', 0
  355. title           db 'HTTP Downloader', 0
  356. silently        db 0
  357.  
  358. sz_add_header   db 'Accept-Encoding: deflate', 13, 10, 0        ; archiver.obj doesnt export unpack method for gzip yet :(
  359. sz_content_enc  db 'content-encoding', 0
  360.  
  361. ;---------------------------------------------------------------------
  362. select_addr_text db ' NETWORK ADDRESS: ',0
  363. select_path_text db ' PATH TO SAVE FILE: ',0
  364. ;---------------------------------------------------------------------
  365. frame_data:
  366. .type                   dd 0 ;+0
  367. .x:
  368. .x_size                 dw 0 ;+4
  369. .x_start                dw 0 ;+6
  370. .y:
  371. .y_size                 dw 0 ;+8
  372. .y_start                dw 0 ;+10
  373. .ext_fr_col             dd 0x0 ;+12
  374. .int_fr_col             dd 0xffffff ;+16
  375. .draw_text_flag         dd 1 ;+20
  376. .text_pointer           dd 0 ;+24
  377. .text_position          dd 0 ;+28
  378. .font_number            dd 0 ;+32
  379. .font_size_y            dd 9 ;+36
  380. .font_color             dd 0x0 ;+40
  381. .font_backgr_color      dd 0xffffff ;+44
  382. ;---------------------------------------------------------------------
  383. PathShow_data_1:
  384. .type                   dd 0    ;+0
  385. .start_y                dw frame_2.y+14   ;+4
  386. .start_x                dw frame_2.x+20   ;+6
  387. .font_size_x            dw 6    ;+8     ; 6 - for font 0, 8 - for font 1
  388. .area_size_x            dw frame_2.width-35  ;+10
  389. .font_number            dd 0    ;+12    ; 0 - monospace, 1 - variable
  390. .background_flag        dd 0    ;+16
  391. .font_color             dd 0    ;+20
  392. .background_color       dd 0    ;+24
  393. .text_pointer           dd fname_buf    ;+28
  394. .work_area_pointer      dd text_work_area       ;+32
  395. .temp_text_length       dd 0    ;+36
  396. ;---------------------------------------------------------------------
  397. OpenDialog_data:
  398. .type                   dd 1    ; Save
  399. .procinfo               dd procinfo     ;+4
  400. .com_area_name          dd communication_area_name      ;+8
  401. .com_area               dd 0    ;+12
  402. .opendir_path           dd temp_dir_path        ;+16
  403. .dir_default_path       dd communication_area_default_path      ;+20
  404. .start_path             dd open_dialog_path     ;+24
  405. .draw_window            dd draw_window  ;+28
  406. .status                 dd 0    ;+32
  407. .openfile_pach          dd fname_buf    ;+36
  408. .filename_area          dd filename_area        ;+40
  409. .filter_area            dd Filter
  410. .x:
  411. .x_size                 dw 420 ;+48 ; Window X size
  412. .x_start                dw 200 ;+50 ; Window X position
  413. .y:
  414. .y_size                 dw 320 ;+52 ; Window y size
  415. .y_start                dw 120 ;+54 ; Window Y position
  416.  
  417. communication_area_name:
  418.         db 'FFFFFFFF_open_dialog',0
  419. open_dialog_path:
  420.     db '/sys/File Managers/opendial',0
  421. communication_area_default_path:
  422.         db '/sys',0
  423.  
  424. Filter:
  425. dd      Filter.end - Filter
  426. .1:
  427. db      'IMG',0
  428. db      'IMA',0
  429. .end:
  430. db      0
  431.  
  432. start_temp_file_name:   db 'some.garbage',0
  433.  
  434. start_file_path:  db '/sys/.download', 0
  435. ;---------------------------------------------------------------------
  436. document_user   db 'http://'
  437. ;---------------------------------------------------------------------
  438. IM_END:
  439. ;---------------------------------------------------------------------
  440. params          rb URLMAXLEN
  441. ;---------------------------------------------------------------------
  442.                 sc system_colors
  443. ;---------------------------------------------------------------------
  444. identifier      dd ?
  445. ;---------------------------------------------------------------------
  446. filename_area:
  447.                 rb 256
  448. ;---------------------------------------------------------------------
  449. temp_dir_path:
  450.                 rb 4096
  451. ;---------------------------------------------------------------------
  452. procinfo:
  453.                 rb 1024
  454. ;---------------------------------------------------------------------
  455. fname_buf:
  456.                 rb 4096
  457. ;---------------------------------------------------------------------
  458. text_work_area:
  459.                 rb 1024
  460. ;---------------------------------------------------------------------
  461.                 rb 4096
  462. stacktop:
  463. ;---------------------------------------------------------------------
  464. I_END:
  465. ;---------------------------------------------------------------------
  466.  
  467.  
  468.