Subversion Repositories Kolibri OS

Rev

Rev 6977 | 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]
7092 hidnplayr 242
        jz      .no_data                                ; more then 0 data bytes?
243
 
5540 hidnplayr 244
        mov     [fileinfo.size], ecx
245
        mov     eax, [ebp + http_msg.content_ptr]
246
        mov     [fileinfo.buffer], eax
247
        mov     ebx, [offset]
248
        mov     [fileinfo.offset], ebx
249
        DEBUGF  1, "Writing to disk: size=%u offset=%u\n", ecx, ebx
250
        mcall   70, fileinfo
5543 hidnplayr 251
        test    eax, eax                                ; check error code
252
        jnz     write_error
253
        cmp     ebx, ecx                                ; check if all bytes were written to disk
254
        jne     write_error
3561 hidnplayr 255
 
5540 hidnplayr 256
        mov     eax, [ebp + http_msg.content_received]
257
        mov     [offset], eax
5543 hidnplayr 258
        mov     [pb.value], eax
3561 hidnplayr 259
 
5543 hidnplayr 260
        invoke  progressbar_draw, pb
261
 
7092 hidnplayr 262
  .no_data:
5540 hidnplayr 263
        test    [ebp + http_msg.flags], FLAG_GOT_ALL_DATA
264
        jz      download_loop
3561 hidnplayr 265
 
5543 hidnplayr 266
; Download completed successfully
267
        mov     [status], sz_complete
5540 hidnplayr 268
        mov     [pb.progress_color], 0x0000c800         ; green
5543 hidnplayr 269
        mov     [btn_text], sz_open
270
        jmp     http_free
3561 hidnplayr 271
 
5543 hidnplayr 272
write_error:
273
        mov     [status], sz_err_full
274
        mov     [pb.progress_color], 0x00c80000         ; red
275
        mov     [btn_text], sz_exit
276
        jmp     http_free
277
 
278
http_error:
279
        mov     [status], sz_err_http
280
        mov     [pb.progress_color], 0x00c80000         ; red
281
        mov     [btn_text], sz_exit
282
;        jmp     http_free
283
 
5540 hidnplayr 284
http_free:
285
        mcall   40, EVM_REDRAW + EVM_BUTTON
286
        push    [ebp + http_msg.content_received]
287
        pop     [pb.value]
3561 hidnplayr 288
 
5540 hidnplayr 289
        mov     ecx, [ebp + http_msg.content_ptr]
290
        test    ecx, ecx
291
        jz      @f
292
        mcall   68, 13                                  ; free the buffer
293
  @@:
294
        invoke  HTTP_free, [identifier]                 ; free headers and connection
5543 hidnplayr 295
        jmp     redraw
3561 hidnplayr 296
 
297
draw_window:
5540 hidnplayr 298
        mcall   12, 1   ; start window draw
3561 hidnplayr 299
 
5540 hidnplayr 300
; get system colors
301
        mcall   48, 3, sc, 40
3561 hidnplayr 302
 
5540 hidnplayr 303
; draw window
3561 hidnplayr 304
        mov     edx, [sc.work]
305
        or      edx, 0x34000000
6773 leency 306
        mcall   0, <50, 420>, <350, 120>, , 0, title
3561 hidnplayr 307
 
5540 hidnplayr 308
; draw button
6773 leency 309
        mcall   8, <320,75>, <50,24>, 22, [sc.work_button]
3561 hidnplayr 310
 
5540 hidnplayr 311
; draw button text
3561 hidnplayr 312
        mov     ecx, [sc.work_button_text]
6773 leency 313
        or      ecx, 90000000h
314
        mcall   4, <325,56>, , [btn_text]
3561 hidnplayr 315
 
5543 hidnplayr 316
; draw status text
317
        mov     ecx, [sc.work_text]
6773 leency 318
        or      ecx, 90000000h
319
        mcall   4, <10,70>, , [status]
5543 hidnplayr 320
 
5540 hidnplayr 321
; draw editbox
322
        edit_boxes_set_sys_color edit1, editboxes_end, sc
323
        invoke  edit_box_draw, edit1
3623 hidnplayr 324
 
5540 hidnplayr 325
        cmp     [identifier], 0
326
        je     @f
327
; draw progressbar
328
        invoke  progressbar_draw, pb
329
  @@:
330
        mcall   12, 2   ; end window draw
3566 hidnplayr 331
 
5540 hidnplayr 332
dont_draw:
333
        ret
334
 
335
;---------------------------------------------------------------------
3561 hidnplayr 336
; Data area
337
;-----------------------------------------------------------------------------
338
align   4
339
@IMPORT:
340
 
5540 hidnplayr 341
library lib_http,       'http.obj', \
342
        box_lib,        'box_lib.obj', \
343
        proc_lib,       'proc_lib.obj'
3561 hidnplayr 344
 
5540 hidnplayr 345
import  lib_http, \
346
        HTTP_get,       'get', \
347
        HTTP_receive,   'receive', \
348
        HTTP_free,      'free'
3561 hidnplayr 349
 
350
import  box_lib, \
5540 hidnplayr 351
        edit_box_draw,    'edit_box', \
352
        edit_box_key,     'edit_box_key', \
353
        edit_box_mouse,   'edit_box_mouse', \
354
        progressbar_draw, 'progressbar_draw', \
355
        progressbar_prog, 'progressbar_progress'
3561 hidnplayr 356
 
5540 hidnplayr 357
import  proc_lib, \
358
        OpenDialog_Init,  'OpenDialog_init', \
359
        OpenDialog_Start, 'OpenDialog_start'
3561 hidnplayr 360
 
361
 
5540 hidnplayr 362
fileinfo        dd 2
363
  .offset       dd 0, 0
364
  .size         dd 0
365
  .buffer       dd 0
3561 hidnplayr 366
                db 0
5540 hidnplayr 367
                dd fname_buf
3561 hidnplayr 368
 
5543 hidnplayr 369
fileopen        dd 7
370
                dd 0                    ; flags
371
                dd fname_buf            ; parameters
372
                dd 0                    ; reserved
373
                dd 0                    ; reserved
374
                db "/sys/@open", 0      ; path
375
 
6773 leency 376
edit1           edit_box 400, 5, 10, 0xffffff, 0x0000ff, 0x0080ff, 0x000000, 0x90000000, URLMAXLEN, url, mouse_dd, ed_focus+ed_always_focus, 0, 0
3561 hidnplayr 377
editboxes_end:
378
 
5540 hidnplayr 379
identifier      dd 0
380
btn_text        dd sz_download
5543 hidnplayr 381
status          dd sz_null
5540 hidnplayr 382
sz_download     db 'Download', 0
383
sz_cancel       db ' Cancel ', 0
384
sz_open         db '  Open  ', 0
5543 hidnplayr 385
sz_exit         db '  Exit  ', 0
386
 
387
sz_null         db 0
388
sz_downloading  db 'Downloading..', 0
389
sz_complete     db 'Download completed', 0
390
sz_err_create   db 'Could not create the local file!', 0
391
sz_err_full     db 'Disk full!', 0
392
sz_err_http     db 'HTTP error!', 0
3561 hidnplayr 393
title           db 'HTTP Downloader', 0
394
 
5540 hidnplayr 395
OpenDialog_data:
396
.type                   dd 1    ; Save
397
.procinfo               dd procinfo
398
.com_area_name          dd communication_area_name
399
.com_area               dd 0
400
.opendir_path           dd temp_dir_path
401
.dir_default_path       dd communication_area_default_path
402
.start_path             dd open_dialog_path
403
.draw_window            dd dont_draw
404
.status                 dd 0
405
.openfile_patch         dd fname_buf
406
.filename_area          dd filename_area
407
.filter_area            dd filter
408
.x:
409
.x_size                 dw 420  ; Window X size
410
.x_start                dw 200  ; Window X position
411
.y:
412
.y_size                 dw 320  ; Window y size
6773 leency 413
.y_start                dw 140  ; Window Y position
3561 hidnplayr 414
 
5540 hidnplayr 415
communication_area_name         db 'FFFFFFFF_open_dialog',0
416
open_dialog_path                db '/sys/File Managers/opendial',0
417
communication_area_default_path db '/sys',0
3561 hidnplayr 418
 
5540 hidnplayr 419
filter:
420
dd      0
421
db      0
3561 hidnplayr 422
 
5540 hidnplayr 423
pb:
424
.value          dd 0
425
.left           dd 5
6773 leency 426
.top            dd 45
5540 hidnplayr 427
.width          dd 300
428
.height         dd 16
429
.style          dd 1
430
.min            dd 0
431
.max            dd 0
432
.back_color     dd 0xefefef
433
.progress_color dd 0xc8c8c8
434
.frame_color    dd 0x94aece
435
.frame_color2   dd 0xffffff
3561 hidnplayr 436
 
5540 hidnplayr 437
include_debug_strings
3561 hidnplayr 438
 
5540 hidnplayr 439
download_file_path db '/tmp0/1/', 0
3561 hidnplayr 440
 
441
IM_END:
442
 
5540 hidnplayr 443
url             rb URLMAXLEN
444
sc              system_colors
445
offset          dd ?
446
mouse_dd        dd ?
3561 hidnplayr 447
 
5540 hidnplayr 448
filename_area   rb 256
449
temp_dir_path   rb 4096
450
procinfo        rb 1024
451
fname_buf       rb 4096
452
text_work_area  rb 1024
3561 hidnplayr 453
 
5540 hidnplayr 454
I_END: