Rev 6773 | Rev 7092 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3561 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
6977 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2014-2017. All rights reserved. ;; |
3561 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; downloader.asm - HTTP client for KolibriOS ;; |
||
7 | ;; ;; |
||
5540 | hidnplayr | 8 | ;; hidnplayr@kolibrios.org ;; |
3561 | hidnplayr | 9 | ;; ;; |
10 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
11 | ;; Version 2, June 1991 ;; |
||
12 | ;; ;; |
||
13 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
14 | |||
5540 | hidnplayr | 15 | URLMAXLEN = 65535 |
3561 | hidnplayr | 16 | |
17 | __DEBUG__ = 1 |
||
3618 | hidnplayr | 18 | __DEBUG_LEVEL__ = 1 |
3561 | hidnplayr | 19 | |
5540 | hidnplayr | 20 | |
3561 | hidnplayr | 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 |
||
3623 | hidnplayr | 29 | dd I_END+0x1000 ; required memory |
30 | dd I_END+0x1000 ; esp |
||
5540 | hidnplayr | 31 | dd url |
3561 | hidnplayr | 32 | dd 0x0 ; I_Path |
33 | |||
5540 | hidnplayr | 34 | |
3618 | hidnplayr | 35 | include '../../macros.inc' |
36 | include '../../proc32.inc' |
||
37 | include '../../dll.inc' |
||
38 | include '../../debug-fdo.inc' |
||
5540 | hidnplayr | 39 | include '../../develop/libraries/box_lib/trunk/box_lib.mac' |
40 | include '../../develop/libraries/http/http.inc' |
||
3561 | hidnplayr | 41 | |
5540 | hidnplayr | 42 | virtual at 0 |
43 | http_msg http_msg |
||
44 | end virtual |
||
45 | |||
46 | |||
3561 | hidnplayr | 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 |
||
5543 | hidnplayr | 53 | jnz mainloop.exit |
3561 | hidnplayr | 54 | |
5540 | hidnplayr | 55 | ; wanted events |
56 | mcall 40, EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER |
||
3561 | hidnplayr | 57 | |
5540 | hidnplayr | 58 | ; prepare filename buffers |
59 | mov edi, fname_buf |
||
60 | mov esi, download_file_path |
||
61 | @@: |
||
62 | lodsb |
||
3561 | hidnplayr | 63 | stosb |
64 | test al, al |
||
5540 | hidnplayr | 65 | jnz @r |
3561 | hidnplayr | 66 | |
5540 | hidnplayr | 67 | ; Initialise OpenDialog |
68 | invoke OpenDialog_Init, OpenDialog_data |
||
3561 | hidnplayr | 69 | |
5540 | hidnplayr | 70 | ; If user provided parameters, start download right away! |
71 | cmp byte[url], 0 |
||
6977 | hidnplayr | 72 | jne display_url_and_download |
3561 | hidnplayr | 73 | |
5540 | hidnplayr | 74 | mov [OpenDialog_data.draw_window], draw_window |
3561 | hidnplayr | 75 | |
3623 | hidnplayr | 76 | redraw: |
3561 | hidnplayr | 77 | call draw_window |
78 | |||
5540 | hidnplayr | 79 | mainloop: |
3566 | hidnplayr | 80 | mcall 10 ; wait here for event |
3623 | hidnplayr | 81 | cmp eax, EV_REDRAW |
82 | je redraw |
||
83 | cmp eax, EV_KEY |
||
5540 | hidnplayr | 84 | je .key |
3623 | hidnplayr | 85 | cmp eax, EV_BUTTON |
5540 | hidnplayr | 86 | je .button |
3623 | hidnplayr | 87 | cmp eax, EV_MOUSE |
5540 | hidnplayr | 88 | je .mouse |
89 | jmp mainloop |
||
3561 | hidnplayr | 90 | |
5540 | hidnplayr | 91 | .key: |
3561 | hidnplayr | 92 | mcall 2 ; read key |
5540 | hidnplayr | 93 | invoke edit_box_key, edit1 |
3566 | hidnplayr | 94 | cmp ax, 13 shl 8 |
95 | je download |
||
5540 | hidnplayr | 96 | jmp mainloop |
3561 | hidnplayr | 97 | |
5540 | hidnplayr | 98 | .button: |
3561 | hidnplayr | 99 | mcall 17 ; get id |
100 | cmp ah, 1 ; button id=1 ? |
||
5543 | hidnplayr | 101 | je .exit |
3561 | hidnplayr | 102 | |
5540 | hidnplayr | 103 | cmp [btn_text], sz_download |
104 | je download |
||
3561 | hidnplayr | 105 | |
5540 | hidnplayr | 106 | cmp [btn_text], sz_open |
107 | je open_file |
||
3566 | hidnplayr | 108 | |
5543 | hidnplayr | 109 | .exit: |
110 | mcall -1 ; exit |
||
111 | |||
5540 | hidnplayr | 112 | .mouse: |
113 | invoke edit_box_mouse, edit1 |
||
114 | jmp mainloop |
||
3566 | hidnplayr | 115 | |
5543 | hidnplayr | 116 | |
5540 | hidnplayr | 117 | open_file: |
5543 | hidnplayr | 118 | mcall 70, fileopen |
5540 | hidnplayr | 119 | jmp mainloop |
3566 | hidnplayr | 120 | |
6977 | hidnplayr | 121 | display_url_and_download: |
122 | xor al, al |
||
123 | mov ecx, 4096 |
||
124 | mov edi, url |
||
125 | repne scasb |
||
126 | sub edi, url+1 |
||
127 | mov [edit1.size], edi |
||
3566 | hidnplayr | 128 | |
5540 | hidnplayr | 129 | download: |
130 | ; Extract the filename from URL |
||
131 | mov edi, url |
||
132 | xor al, al |
||
133 | mov ecx, URLMAXLEN |
||
134 | repne scasb |
||
135 | mov esi, edi |
||
136 | dec esi |
||
137 | dec esi |
||
138 | std |
||
139 | .loop: |
||
3561 | hidnplayr | 140 | lodsb |
5540 | hidnplayr | 141 | cmp al, '/' |
3561 | hidnplayr | 142 | je .done |
5540 | hidnplayr | 143 | test al, al |
144 | jnz .loop |
||
3561 | hidnplayr | 145 | .done: |
5540 | hidnplayr | 146 | cld |
147 | mov ecx, edi |
||
148 | sub ecx, esi |
||
3561 | hidnplayr | 149 | inc esi |
150 | inc esi |
||
5540 | hidnplayr | 151 | mov edi, filename_area |
152 | rep movsb |
||
3561 | hidnplayr | 153 | |
5540 | hidnplayr | 154 | ; invoke OpenDialog |
155 | invoke OpenDialog_Start, OpenDialog_data |
||
156 | mcall 40, EVM_REDRAW + EVM_BUTTON + EVM_STACK |
||
157 | call draw_window |
||
3561 | hidnplayr | 158 | |
5543 | hidnplayr | 159 | ; Create the local file |
5540 | hidnplayr | 160 | mov [fileinfo], 2 ; create/write to file |
3561 | hidnplayr | 161 | xor eax, eax |
5540 | hidnplayr | 162 | mov [fileinfo.offset], eax |
163 | mov [fileinfo.offset+4], eax |
||
164 | mov [fileinfo.size], eax |
||
165 | mcall 70, fileinfo |
||
166 | test eax, eax |
||
5543 | hidnplayr | 167 | jnz create_error |
3561 | hidnplayr | 168 | |
5543 | hidnplayr | 169 | ; Start the download |
5540 | hidnplayr | 170 | invoke HTTP_get, url, 0, FLAG_STREAM or FLAG_REUSE_BUFFER, 0 |
171 | test eax, eax |
||
5543 | hidnplayr | 172 | jz get_error |
173 | |||
5540 | hidnplayr | 174 | mov [identifier], eax |
175 | mov [offset], 0 |
||
176 | mov [btn_text], sz_cancel |
||
5543 | hidnplayr | 177 | mov [status], sz_downloading |
5540 | hidnplayr | 178 | or [edit1.flags], ed_figure_only |
5543 | hidnplayr | 179 | and [edit1.flags], not ed_focus |
180 | push [sc.work] |
||
181 | pop [edit1.color] |
||
5540 | hidnplayr | 182 | call draw_window |
3561 | hidnplayr | 183 | |
5543 | hidnplayr | 184 | jmp download_loop |
185 | |||
186 | get_error: |
||
187 | mov [btn_text], sz_exit |
||
188 | mov [status], sz_err_http |
||
189 | jmp redraw |
||
190 | |||
191 | create_error: |
||
192 | mov [btn_text], sz_exit |
||
193 | mov [status], sz_err_create |
||
194 | jmp redraw |
||
195 | |||
5540 | hidnplayr | 196 | download_loop: |
197 | mcall 10 |
||
198 | cmp eax, EV_REDRAW |
||
199 | je .redraw |
||
200 | cmp eax, EV_BUTTON |
||
201 | je .button |
||
3623 | hidnplayr | 202 | |
5540 | hidnplayr | 203 | invoke HTTP_receive, [identifier] |
4431 | hidnplayr | 204 | test eax, eax |
5543 | hidnplayr | 205 | jz got_data |
5540 | hidnplayr | 206 | jmp download_loop |
3736 | hidnplayr | 207 | |
5540 | hidnplayr | 208 | .redraw: |
209 | call draw_window |
||
210 | jmp download_loop |
||
3561 | hidnplayr | 211 | |
5540 | hidnplayr | 212 | .button: |
213 | jmp http_free |
||
3561 | hidnplayr | 214 | |
5543 | hidnplayr | 215 | got_data: |
5540 | hidnplayr | 216 | mov ebp, [identifier] |
217 | test [ebp + http_msg.flags], 0xffff0000 ; error? |
||
5543 | hidnplayr | 218 | jnz http_error |
4431 | hidnplayr | 219 | |
5543 | hidnplayr | 220 | cmp [fileinfo], 3 ; Did we write before? |
221 | je .write |
||
222 | |||
223 | test [ebp + http_msg.flags], FLAG_CONTENT_LENGTH |
||
224 | jz .first_write |
||
225 | |||
226 | mov eax, [ebp + http_msg.content_length] |
||
227 | mov [pb.max], eax |
||
228 | |||
229 | DEBUGF 1, "new file size=%u\n", eax |
||
5540 | hidnplayr | 230 | mov [fileinfo], 4 ; set end of file |
231 | mov [fileinfo.offset], eax ; new file size |
||
232 | mcall 70, fileinfo |
||
5543 | hidnplayr | 233 | test eax, eax |
234 | jnz write_error |
||
4431 | hidnplayr | 235 | |
5543 | hidnplayr | 236 | |
237 | .first_write: |
||
5540 | hidnplayr | 238 | mov [fileinfo], 3 ; write to existing file |
5543 | hidnplayr | 239 | .write: |
5540 | hidnplayr | 240 | mov ecx, [ebp + http_msg.content_received] |
241 | sub ecx, [offset] |
||
5543 | hidnplayr | 242 | jz download_loop ; more then 0 data bytes? |
5540 | hidnplayr | 243 | mov [fileinfo.size], ecx |
244 | mov eax, [ebp + http_msg.content_ptr] |
||
245 | mov [fileinfo.buffer], eax |
||
246 | mov ebx, [offset] |
||
247 | mov [fileinfo.offset], ebx |
||
248 | DEBUGF 1, "Writing to disk: size=%u offset=%u\n", ecx, ebx |
||
249 | mcall 70, fileinfo |
||
5543 | hidnplayr | 250 | test eax, eax ; check error code |
251 | jnz write_error |
||
252 | cmp ebx, ecx ; check if all bytes were written to disk |
||
253 | jne write_error |
||
3561 | hidnplayr | 254 | |
5540 | hidnplayr | 255 | mov eax, [ebp + http_msg.content_received] |
256 | mov [offset], eax |
||
5543 | hidnplayr | 257 | mov [pb.value], eax |
3561 | hidnplayr | 258 | |
5543 | hidnplayr | 259 | invoke progressbar_draw, pb |
260 | |||
5540 | hidnplayr | 261 | test [ebp + http_msg.flags], FLAG_GOT_ALL_DATA |
262 | jz download_loop |
||
3561 | hidnplayr | 263 | |
5543 | hidnplayr | 264 | ; Download completed successfully |
265 | mov [status], sz_complete |
||
5540 | hidnplayr | 266 | mov [pb.progress_color], 0x0000c800 ; green |
5543 | hidnplayr | 267 | mov [btn_text], sz_open |
268 | jmp http_free |
||
3561 | hidnplayr | 269 | |
5543 | hidnplayr | 270 | write_error: |
271 | mov [status], sz_err_full |
||
272 | mov [pb.progress_color], 0x00c80000 ; red |
||
273 | mov [btn_text], sz_exit |
||
274 | jmp http_free |
||
275 | |||
276 | http_error: |
||
277 | mov [status], sz_err_http |
||
278 | mov [pb.progress_color], 0x00c80000 ; red |
||
279 | mov [btn_text], sz_exit |
||
280 | ; jmp http_free |
||
281 | |||
5540 | hidnplayr | 282 | http_free: |
283 | mcall 40, EVM_REDRAW + EVM_BUTTON |
||
284 | push [ebp + http_msg.content_received] |
||
285 | pop [pb.value] |
||
3561 | hidnplayr | 286 | |
5540 | hidnplayr | 287 | mov ecx, [ebp + http_msg.content_ptr] |
288 | test ecx, ecx |
||
289 | jz @f |
||
290 | mcall 68, 13 ; free the buffer |
||
291 | @@: |
||
292 | invoke HTTP_free, [identifier] ; free headers and connection |
||
5543 | hidnplayr | 293 | jmp redraw |
3561 | hidnplayr | 294 | |
295 | draw_window: |
||
5540 | hidnplayr | 296 | mcall 12, 1 ; start window draw |
3561 | hidnplayr | 297 | |
5540 | hidnplayr | 298 | ; get system colors |
299 | mcall 48, 3, sc, 40 |
||
3561 | hidnplayr | 300 | |
5540 | hidnplayr | 301 | ; draw window |
3561 | hidnplayr | 302 | mov edx, [sc.work] |
303 | or edx, 0x34000000 |
||
6773 | leency | 304 | mcall 0, <50, 420>, <350, 120>, , 0, title |
3561 | hidnplayr | 305 | |
5540 | hidnplayr | 306 | ; draw button |
6773 | leency | 307 | mcall 8, <320,75>, <50,24>, 22, [sc.work_button] |
3561 | hidnplayr | 308 | |
5540 | hidnplayr | 309 | ; draw button text |
3561 | hidnplayr | 310 | mov ecx, [sc.work_button_text] |
6773 | leency | 311 | or ecx, 90000000h |
312 | mcall 4, <325,56>, , [btn_text] |
||
3561 | hidnplayr | 313 | |
5543 | hidnplayr | 314 | ; draw status text |
315 | mov ecx, [sc.work_text] |
||
6773 | leency | 316 | or ecx, 90000000h |
317 | mcall 4, <10,70>, , [status] |
||
5543 | hidnplayr | 318 | |
5540 | hidnplayr | 319 | ; draw editbox |
320 | edit_boxes_set_sys_color edit1, editboxes_end, sc |
||
321 | invoke edit_box_draw, edit1 |
||
3623 | hidnplayr | 322 | |
5540 | hidnplayr | 323 | cmp [identifier], 0 |
324 | je @f |
||
325 | ; draw progressbar |
||
326 | invoke progressbar_draw, pb |
||
327 | @@: |
||
328 | mcall 12, 2 ; end window draw |
||
3566 | hidnplayr | 329 | |
5540 | hidnplayr | 330 | dont_draw: |
331 | ret |
||
332 | |||
333 | ;--------------------------------------------------------------------- |
||
3561 | hidnplayr | 334 | ; Data area |
335 | ;----------------------------------------------------------------------------- |
||
336 | align 4 |
||
337 | @IMPORT: |
||
338 | |||
5540 | hidnplayr | 339 | library lib_http, 'http.obj', \ |
340 | box_lib, 'box_lib.obj', \ |
||
341 | proc_lib, 'proc_lib.obj' |
||
3561 | hidnplayr | 342 | |
5540 | hidnplayr | 343 | import lib_http, \ |
344 | HTTP_get, 'get', \ |
||
345 | HTTP_receive, 'receive', \ |
||
346 | HTTP_free, 'free' |
||
3561 | hidnplayr | 347 | |
348 | import box_lib, \ |
||
5540 | hidnplayr | 349 | edit_box_draw, 'edit_box', \ |
350 | edit_box_key, 'edit_box_key', \ |
||
351 | edit_box_mouse, 'edit_box_mouse', \ |
||
352 | progressbar_draw, 'progressbar_draw', \ |
||
353 | progressbar_prog, 'progressbar_progress' |
||
3561 | hidnplayr | 354 | |
5540 | hidnplayr | 355 | import proc_lib, \ |
356 | OpenDialog_Init, 'OpenDialog_init', \ |
||
357 | OpenDialog_Start, 'OpenDialog_start' |
||
3561 | hidnplayr | 358 | |
359 | |||
5540 | hidnplayr | 360 | fileinfo dd 2 |
361 | .offset dd 0, 0 |
||
362 | .size dd 0 |
||
363 | .buffer dd 0 |
||
3561 | hidnplayr | 364 | db 0 |
5540 | hidnplayr | 365 | dd fname_buf |
3561 | hidnplayr | 366 | |
5543 | hidnplayr | 367 | fileopen dd 7 |
368 | dd 0 ; flags |
||
369 | dd fname_buf ; parameters |
||
370 | dd 0 ; reserved |
||
371 | dd 0 ; reserved |
||
372 | db "/sys/@open", 0 ; path |
||
373 | |||
6773 | leency | 374 | edit1 edit_box 400, 5, 10, 0xffffff, 0x0000ff, 0x0080ff, 0x000000, 0x90000000, URLMAXLEN, url, mouse_dd, ed_focus+ed_always_focus, 0, 0 |
3561 | hidnplayr | 375 | editboxes_end: |
376 | |||
5540 | hidnplayr | 377 | identifier dd 0 |
378 | btn_text dd sz_download |
||
5543 | hidnplayr | 379 | status dd sz_null |
5540 | hidnplayr | 380 | sz_download db 'Download', 0 |
381 | sz_cancel db ' Cancel ', 0 |
||
382 | sz_open db ' Open ', 0 |
||
5543 | hidnplayr | 383 | sz_exit db ' Exit ', 0 |
384 | |||
385 | sz_null db 0 |
||
386 | sz_downloading db 'Downloading..', 0 |
||
387 | sz_complete db 'Download completed', 0 |
||
388 | sz_err_create db 'Could not create the local file!', 0 |
||
389 | sz_err_full db 'Disk full!', 0 |
||
390 | sz_err_http db 'HTTP error!', 0 |
||
3561 | hidnplayr | 391 | title db 'HTTP Downloader', 0 |
392 | |||
5540 | hidnplayr | 393 | OpenDialog_data: |
394 | .type dd 1 ; Save |
||
395 | .procinfo dd procinfo |
||
396 | .com_area_name dd communication_area_name |
||
397 | .com_area dd 0 |
||
398 | .opendir_path dd temp_dir_path |
||
399 | .dir_default_path dd communication_area_default_path |
||
400 | .start_path dd open_dialog_path |
||
401 | .draw_window dd dont_draw |
||
402 | .status dd 0 |
||
403 | .openfile_patch dd fname_buf |
||
404 | .filename_area dd filename_area |
||
405 | .filter_area dd filter |
||
406 | .x: |
||
407 | .x_size dw 420 ; Window X size |
||
408 | .x_start dw 200 ; Window X position |
||
409 | .y: |
||
410 | .y_size dw 320 ; Window y size |
||
6773 | leency | 411 | .y_start dw 140 ; Window Y position |
3561 | hidnplayr | 412 | |
5540 | hidnplayr | 413 | communication_area_name db 'FFFFFFFF_open_dialog',0 |
414 | open_dialog_path db '/sys/File Managers/opendial',0 |
||
415 | communication_area_default_path db '/sys',0 |
||
3561 | hidnplayr | 416 | |
5540 | hidnplayr | 417 | filter: |
418 | dd 0 |
||
419 | db 0 |
||
3561 | hidnplayr | 420 | |
5540 | hidnplayr | 421 | pb: |
422 | .value dd 0 |
||
423 | .left dd 5 |
||
6773 | leency | 424 | .top dd 45 |
5540 | hidnplayr | 425 | .width dd 300 |
426 | .height dd 16 |
||
427 | .style dd 1 |
||
428 | .min dd 0 |
||
429 | .max dd 0 |
||
430 | .back_color dd 0xefefef |
||
431 | .progress_color dd 0xc8c8c8 |
||
432 | .frame_color dd 0x94aece |
||
433 | .frame_color2 dd 0xffffff |
||
3561 | hidnplayr | 434 | |
5540 | hidnplayr | 435 | include_debug_strings |
3561 | hidnplayr | 436 | |
5540 | hidnplayr | 437 | download_file_path db '/tmp0/1/', 0 |
3561 | hidnplayr | 438 | |
439 | IM_END: |
||
440 | |||
5540 | hidnplayr | 441 | url rb URLMAXLEN |
442 | sc system_colors |
||
443 | offset dd ? |
||
444 | mouse_dd dd ? |
||
3561 | hidnplayr | 445 | |
5540 | hidnplayr | 446 | filename_area rb 256 |
447 | temp_dir_path rb 4096 |
||
448 | procinfo rb 1024 |
||
449 | fname_buf rb 4096 |
||
450 | text_work_area rb 1024 |
||
3561 | hidnplayr | 451 | |
5540 | hidnplayr | 452 | I_END:10,70>325,56>50,24>320,75>350,>50,> |