Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2009-2013. 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. 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      params          ; I_PARAM
  32.         dd      0x0             ; I_Path
  33.  
  34. include '../../macros.inc'
  35. include '../../proc32.inc'
  36. include '../../develop/libraries/box_lib/trunk/box_lib.mac'
  37. include '../../dll.inc'
  38. include '../../debug-fdo.inc'
  39. include '../../develop/libraries/http/http.inc'
  40.  
  41. virtual at 0
  42.         http_msg http_msg
  43. end virtual
  44.  
  45. START:
  46.  
  47.         mcall   68, 11                  ; init heap so we can allocate memory dynamically
  48.  
  49. ; load libraries
  50.         stdcall dll.Load, @IMPORT
  51.         test    eax, eax
  52.         jnz     exit
  53.  
  54. ; check parameters
  55.         cmp     byte[params], 0         ; no parameters ?
  56.         je      reset_events            ; load the GUI
  57.  
  58. download:
  59.  
  60.         DEBUGF  1, "Starting download\n"
  61.  
  62.         invoke  HTTP_get, params
  63.         test    eax, eax
  64.         jz      fail
  65.         mov     [identifier], eax
  66.  
  67.   .loop:
  68.         invoke  HTTP_process, [identifier]
  69.         test    eax, eax
  70.         jnz     .loop
  71.  
  72. reset_events:
  73.         DEBUGF  1, "resetting events\n"
  74.  
  75. ; Report events
  76. ; defaults + mouse
  77.         mcall   40,EVM_REDRAW+EVM_KEY+EVM_BUTTON+EVM_MOUSE+EVM_MOUSE_FILTER
  78.  
  79. redraw:
  80.         call    draw_window
  81.  
  82. still:
  83. ;;        DEBUGF  1, "waiting for events\n"
  84.  
  85.         mcall   10      ; wait here for event
  86.  
  87.         cmp     eax, EV_REDRAW
  88.         je      redraw
  89.  
  90.         cmp     eax, EV_KEY
  91.         je      key
  92.  
  93.         cmp     eax, EV_BUTTON
  94.         je      button
  95.        
  96.         cmp     eax, EV_MOUSE
  97.         je      mouse
  98.  
  99.         jmp     still
  100.  
  101. key:
  102.         mcall   2       ; read key
  103.  
  104.         stdcall [edit_box_key], dword edit1
  105.  
  106.         cmp     ax, 13 shl 8
  107.         je      download
  108.        
  109.         jmp     still
  110.        
  111. button:
  112.  
  113.         mcall   17      ; get id
  114.  
  115.         cmp     ah, 26
  116.         jne     @f
  117.         call    save
  118.         jmp     still
  119.   @@:
  120.         cmp     ah, 1   ; button id=1 ?
  121.         je      exit
  122.  
  123.         jmp     download
  124.  
  125. mouse:
  126.         stdcall [edit_box_mouse], edit1
  127.         jmp     still
  128.  
  129. exit:
  130.         DEBUGF  1, "Exiting\n"
  131.         mcall   68, 13, [identifier]    ; free buffer
  132. fail:
  133.         or      eax, -1 ; close this program
  134.         mcall
  135.  
  136.  
  137. save:
  138.         mov     ebp, [identifier]
  139.         mov     eax, [ebp + http_msg.content_length]
  140.         mov     [final_size], eax
  141.         lea     ebx, [ebp + http_msg.data]
  142.         add     ebx, [ebp + http_msg.header_length]
  143.         mov     [final_buffer], ebx
  144.         mcall   70, fileinfo
  145.  
  146.   .done:
  147.  
  148. ; TODO: if called from command line, then exit
  149.  
  150.         mov     ecx, [sc.work_text]
  151.         or      ecx, 0x80000000
  152.         mcall   4, <10, 93>, , download_complete
  153.  
  154.         ret
  155.  
  156. ;   *********************************************
  157. ;   *******  WINDOW DEFINITIONS AND DRAW ********
  158. ;   *********************************************
  159.  
  160. draw_window:
  161.  
  162.         mcall   12, 1   ; start window draw
  163.  
  164. ; get system colors
  165.         mcall   48, 3, sc, 40
  166.  
  167. ; draw window
  168.         mov     edx, [sc.work]
  169.         or      edx, 0x34000000
  170.         mcall   0, <50, 370>, <350, 140>, , 0, title
  171.  
  172. ; draw "url:" text
  173.         mov     ecx, [sc.work_text]
  174.         or      ecx, 80000000h
  175.         mcall   4, <14, 14>, , type_pls
  176.  
  177. ; draw editbox
  178.         edit_boxes_set_sys_color edit1, editboxes_end, sc
  179.         stdcall [edit_box_draw], edit1
  180.  
  181. ; draw buttons
  182.         mcall   8, <90, 68>, <54, 16>, 22, [sc.work_button]     ; reload
  183.         mcall   , <166, 50>, <54, 16>, 24                       ; stop
  184.         mcall   , <224, 54>, , 26                               ; save
  185.  
  186. ; draw buttons text
  187.         mov     ecx, [sc.work_button_text]
  188.         or      ecx, 80000000h
  189.         mcall   4, <102, 59>, , button_text
  190.  
  191.         mcall   12, 2   ; end window redraw
  192.  
  193.         ret
  194.  
  195.  
  196. ;-----------------------------------------------------------------------------
  197. ; Data area
  198. ;-----------------------------------------------------------------------------
  199. align   4
  200. @IMPORT:
  201.  
  202. library lib_http,       'http.obj', \
  203.         box_lib,        'box_lib.obj'
  204.  
  205. import  lib_http, \
  206.         HTTP_get                , 'get' , \
  207.         find_header_field       , 'find_header_field' , \
  208.         HTTP_process            , 'process'
  209.  
  210. import  box_lib, \
  211.         edit_box_draw,  'edit_box', \
  212.         edit_box_key,   'edit_box_key', \
  213.         edit_box_mouse, 'edit_box_mouse'
  214.  
  215. ;---------------------------------------------------------------------
  216. fileinfo        dd 2, 0, 0
  217. final_size      dd 0
  218. final_buffer    dd 0
  219.                 db '/rd/1/.download', 0
  220.        
  221. ;---------------------------------------------------------------------
  222.  
  223. mouse_dd        dd 0
  224. edit1           edit_box 295, 48, 10, 0xffffff, 0xff, 0x80ff, 0, 0x8000, URLMAXLEN, document_user, mouse_dd, ed_focus+ed_always_focus, 7, 7
  225. editboxes_end:
  226.  
  227. ;---------------------------------------------------------------------
  228.  
  229. include_debug_strings
  230.  
  231. ;---------------------------------------------------------------------
  232.  
  233. type_pls        db 'URL:', 0
  234. button_text     db 'DOWNLOAD     STOP     RESAVE', 0
  235. download_complete db 'File saved as /rd/1/.download', 0
  236. title           db 'HTTP Downloader', 0
  237.  
  238. ;---------------------------------------------------------------------
  239. document_user   db 'http://'
  240. ;---------------------------------------------------------------------
  241. IM_END:
  242. ;---------------------------------------------------------------------
  243. params          rb URLMAXLEN
  244. ;---------------------------------------------------------------------
  245.                 sc system_colors
  246. ;---------------------------------------------------------------------
  247. identifier      dd ?
  248. ;---------------------------------------------------------------------
  249.  
  250. I_END:
  251.  
  252.  
  253.  
  254.