Subversion Repositories Kolibri OS

Rev

Rev 5540 | Rev 6773 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3561 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
5540 hidnplayr 3
;; Copyright (C) KolibriOS team 2014-2015. 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
72
        jne     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
 
121
 
5540 hidnplayr 122
download:
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:
3561 hidnplayr 133
        lodsb
5540 hidnplayr 134
        cmp     al, '/'
3561 hidnplayr 135
        je      .done
5540 hidnplayr 136
        test    al, al
137
        jnz     .loop
3561 hidnplayr 138
  .done:
5540 hidnplayr 139
        cld
140
        mov     ecx, edi
141
        sub     ecx, esi
3561 hidnplayr 142
        inc     esi
143
        inc     esi
5540 hidnplayr 144
        mov     edi, filename_area
145
        rep movsb
3561 hidnplayr 146
 
5540 hidnplayr 147
; invoke OpenDialog
148
        invoke  OpenDialog_Start, OpenDialog_data
149
        mcall   40, EVM_REDRAW + EVM_BUTTON + EVM_STACK
150
        call    draw_window
3561 hidnplayr 151
 
5543 hidnplayr 152
; Create the local file
5540 hidnplayr 153
        mov     [fileinfo], 2           ; create/write to file
3561 hidnplayr 154
        xor     eax, eax
5540 hidnplayr 155
        mov     [fileinfo.offset], eax
156
        mov     [fileinfo.offset+4], eax
157
        mov     [fileinfo.size], eax
158
        mcall   70, fileinfo
159
        test    eax, eax
5543 hidnplayr 160
        jnz     create_error
3561 hidnplayr 161
 
5543 hidnplayr 162
; Start the download
5540 hidnplayr 163
        invoke  HTTP_get, url, 0, FLAG_STREAM or FLAG_REUSE_BUFFER, 0
164
        test    eax, eax
5543 hidnplayr 165
        jz      get_error
166
 
5540 hidnplayr 167
        mov     [identifier], eax
168
        mov     [offset], 0
169
        mov     [btn_text], sz_cancel
5543 hidnplayr 170
        mov     [status], sz_downloading
5540 hidnplayr 171
        or      [edit1.flags], ed_figure_only
5543 hidnplayr 172
        and     [edit1.flags], not ed_focus
173
        push    [sc.work]
174
        pop     [edit1.color]
5540 hidnplayr 175
        call    draw_window
3561 hidnplayr 176
 
5543 hidnplayr 177
        jmp     download_loop
178
 
179
get_error:
180
        mov     [btn_text], sz_exit
181
        mov     [status], sz_err_http
182
        jmp     redraw
183
 
184
create_error:
185
        mov     [btn_text], sz_exit
186
        mov     [status], sz_err_create
187
        jmp     redraw
188
 
5540 hidnplayr 189
download_loop:
190
        mcall   10
191
        cmp     eax, EV_REDRAW
192
        je      .redraw
193
        cmp     eax, EV_BUTTON
194
        je      .button
3623 hidnplayr 195
 
5540 hidnplayr 196
        invoke  HTTP_receive, [identifier]
4431 hidnplayr 197
        test    eax, eax
5543 hidnplayr 198
        jz      got_data
5540 hidnplayr 199
        jmp     download_loop
3736 hidnplayr 200
 
5540 hidnplayr 201
  .redraw:
202
        call    draw_window
203
        jmp     download_loop
3561 hidnplayr 204
 
5540 hidnplayr 205
  .button:
206
        jmp     http_free
3561 hidnplayr 207
 
5543 hidnplayr 208
got_data:
5540 hidnplayr 209
        mov     ebp, [identifier]
210
        test    [ebp + http_msg.flags], 0xffff0000      ; error?
5543 hidnplayr 211
        jnz     http_error
4431 hidnplayr 212
 
5543 hidnplayr 213
        cmp     [fileinfo], 3                           ; Did we write before?
214
        je      .write
215
 
216
        test    [ebp + http_msg.flags], FLAG_CONTENT_LENGTH
217
        jz      .first_write
218
 
219
        mov     eax, [ebp + http_msg.content_length]
220
        mov     [pb.max], eax
221
 
222
        DEBUGF  1, "new file size=%u\n", eax
5540 hidnplayr 223
        mov     [fileinfo], 4                           ; set end of file
224
        mov     [fileinfo.offset], eax                  ; new file size
225
        mcall   70, fileinfo
5543 hidnplayr 226
        test    eax, eax
227
        jnz     write_error
4431 hidnplayr 228
 
5543 hidnplayr 229
 
230
  .first_write:
5540 hidnplayr 231
        mov     [fileinfo], 3                           ; write to existing file
5543 hidnplayr 232
  .write:
5540 hidnplayr 233
        mov     ecx, [ebp + http_msg.content_received]
234
        sub     ecx, [offset]
5543 hidnplayr 235
        jz      download_loop                           ; more then 0 data bytes?
5540 hidnplayr 236
        mov     [fileinfo.size], ecx
237
        mov     eax, [ebp + http_msg.content_ptr]
238
        mov     [fileinfo.buffer], eax
239
        mov     ebx, [offset]
240
        mov     [fileinfo.offset], ebx
241
        DEBUGF  1, "Writing to disk: size=%u offset=%u\n", ecx, ebx
242
        mcall   70, fileinfo
5543 hidnplayr 243
        test    eax, eax                                ; check error code
244
        jnz     write_error
245
        cmp     ebx, ecx                                ; check if all bytes were written to disk
246
        jne     write_error
3561 hidnplayr 247
 
5540 hidnplayr 248
        mov     eax, [ebp + http_msg.content_received]
249
        mov     [offset], eax
5543 hidnplayr 250
        mov     [pb.value], eax
3561 hidnplayr 251
 
5543 hidnplayr 252
        invoke  progressbar_draw, pb
253
 
5540 hidnplayr 254
        test    [ebp + http_msg.flags], FLAG_GOT_ALL_DATA
255
        jz      download_loop
3561 hidnplayr 256
 
5543 hidnplayr 257
; Download completed successfully
258
        mov     [status], sz_complete
5540 hidnplayr 259
        mov     [pb.progress_color], 0x0000c800         ; green
5543 hidnplayr 260
        mov     [btn_text], sz_open
261
        jmp     http_free
3561 hidnplayr 262
 
5543 hidnplayr 263
write_error:
264
        mov     [status], sz_err_full
265
        mov     [pb.progress_color], 0x00c80000         ; red
266
        mov     [btn_text], sz_exit
267
        jmp     http_free
268
 
269
http_error:
270
        mov     [status], sz_err_http
271
        mov     [pb.progress_color], 0x00c80000         ; red
272
        mov     [btn_text], sz_exit
273
;        jmp     http_free
274
 
5540 hidnplayr 275
http_free:
276
        mcall   40, EVM_REDRAW + EVM_BUTTON
277
        push    [ebp + http_msg.content_received]
278
        pop     [pb.value]
3561 hidnplayr 279
 
5540 hidnplayr 280
        mov     ecx, [ebp + http_msg.content_ptr]
281
        test    ecx, ecx
282
        jz      @f
283
        mcall   68, 13                                  ; free the buffer
284
  @@:
285
        invoke  HTTP_free, [identifier]                 ; free headers and connection
5543 hidnplayr 286
        jmp     redraw
3561 hidnplayr 287
 
288
draw_window:
5540 hidnplayr 289
        mcall   12, 1   ; start window draw
3561 hidnplayr 290
 
5540 hidnplayr 291
; get system colors
292
        mcall   48, 3, sc, 40
3561 hidnplayr 293
 
5540 hidnplayr 294
; draw window
3561 hidnplayr 295
        mov     edx, [sc.work]
296
        or      edx, 0x34000000
5540 hidnplayr 297
        mcall   0, <50, 320>, <350, 110>, , 0, title
3561 hidnplayr 298
 
5540 hidnplayr 299
; draw button
5543 hidnplayr 300
        mcall   8, <229,75>, <60,16>, 22, [sc.work_button]
3561 hidnplayr 301
 
5540 hidnplayr 302
; draw button text
3561 hidnplayr 303
        mov     ecx, [sc.work_button_text]
304
        or      ecx, 80000000h
5540 hidnplayr 305
        mcall   4, <240,65>, , [btn_text]
3561 hidnplayr 306
 
5543 hidnplayr 307
; draw status text
308
        mov     ecx, [sc.work_text]
309
        or      ecx, 80000000h
310
        mcall   4, <10,65>, , [status]
311
 
5540 hidnplayr 312
; draw editbox
313
        edit_boxes_set_sys_color edit1, editboxes_end, sc
314
        invoke  edit_box_draw, edit1
3623 hidnplayr 315
 
5540 hidnplayr 316
        cmp     [identifier], 0
317
        je     @f
318
; draw progressbar
319
        invoke  progressbar_draw, pb
320
  @@:
321
        mcall   12, 2   ; end window draw
3566 hidnplayr 322
 
5540 hidnplayr 323
dont_draw:
324
        ret
325
 
326
;---------------------------------------------------------------------
3561 hidnplayr 327
; Data area
328
;-----------------------------------------------------------------------------
329
align   4
330
@IMPORT:
331
 
5540 hidnplayr 332
library lib_http,       'http.obj', \
333
        box_lib,        'box_lib.obj', \
334
        proc_lib,       'proc_lib.obj'
3561 hidnplayr 335
 
5540 hidnplayr 336
import  lib_http, \
337
        HTTP_get,       'get', \
338
        HTTP_receive,   'receive', \
339
        HTTP_free,      'free'
3561 hidnplayr 340
 
341
import  box_lib, \
5540 hidnplayr 342
        edit_box_draw,    'edit_box', \
343
        edit_box_key,     'edit_box_key', \
344
        edit_box_mouse,   'edit_box_mouse', \
345
        progressbar_draw, 'progressbar_draw', \
346
        progressbar_prog, 'progressbar_progress'
3561 hidnplayr 347
 
5540 hidnplayr 348
import  proc_lib, \
349
        OpenDialog_Init,  'OpenDialog_init', \
350
        OpenDialog_Start, 'OpenDialog_start'
3561 hidnplayr 351
 
352
 
5540 hidnplayr 353
fileinfo        dd 2
354
  .offset       dd 0, 0
355
  .size         dd 0
356
  .buffer       dd 0
3561 hidnplayr 357
                db 0
5540 hidnplayr 358
                dd fname_buf
3561 hidnplayr 359
 
5543 hidnplayr 360
fileopen        dd 7
361
                dd 0                    ; flags
362
                dd fname_buf            ; parameters
363
                dd 0                    ; reserved
364
                dd 0                    ; reserved
365
                db "/sys/@open", 0      ; path
366
 
5540 hidnplayr 367
edit1           edit_box 299, 5, 10, 0xffffff, 0x0000ff, 0x0080ff, 0x000000, 0x8000, URLMAXLEN, url, mouse_dd, ed_focus+ed_always_focus, 0, 0
3561 hidnplayr 368
editboxes_end:
369
 
5540 hidnplayr 370
identifier      dd 0
371
btn_text        dd sz_download
5543 hidnplayr 372
status          dd sz_null
5540 hidnplayr 373
sz_download     db 'Download', 0
374
sz_cancel       db ' Cancel ', 0
375
sz_open         db '  Open  ', 0
5543 hidnplayr 376
sz_exit         db '  Exit  ', 0
377
 
378
sz_null         db 0
379
sz_downloading  db 'Downloading..', 0
380
sz_complete     db 'Download completed', 0
381
sz_err_create   db 'Could not create the local file!', 0
382
sz_err_full     db 'Disk full!', 0
383
sz_err_http     db 'HTTP error!', 0
3561 hidnplayr 384
title           db 'HTTP Downloader', 0
385
 
5540 hidnplayr 386
OpenDialog_data:
387
.type                   dd 1    ; Save
388
.procinfo               dd procinfo
389
.com_area_name          dd communication_area_name
390
.com_area               dd 0
391
.opendir_path           dd temp_dir_path
392
.dir_default_path       dd communication_area_default_path
393
.start_path             dd open_dialog_path
394
.draw_window            dd dont_draw
395
.status                 dd 0
396
.openfile_patch         dd fname_buf
397
.filename_area          dd filename_area
398
.filter_area            dd filter
399
.x:
400
.x_size                 dw 420  ; Window X size
401
.x_start                dw 200  ; Window X position
402
.y:
403
.y_size                 dw 320  ; Window y size
404
.y_start                dw 120  ; Window Y position
3561 hidnplayr 405
 
5540 hidnplayr 406
communication_area_name         db 'FFFFFFFF_open_dialog',0
407
open_dialog_path                db '/sys/File Managers/opendial',0
408
communication_area_default_path db '/sys',0
3561 hidnplayr 409
 
5540 hidnplayr 410
filter:
411
dd      0
412
db      0
3561 hidnplayr 413
 
5540 hidnplayr 414
pb:
415
.value          dd 0
416
.left           dd 5
417
.top            dd 35
418
.width          dd 300
419
.height         dd 16
420
.style          dd 1
421
.min            dd 0
422
.max            dd 0
423
.back_color     dd 0xefefef
424
.progress_color dd 0xc8c8c8
425
.frame_color    dd 0x94aece
426
.frame_color2   dd 0xffffff
3561 hidnplayr 427
 
5540 hidnplayr 428
include_debug_strings
3561 hidnplayr 429
 
5540 hidnplayr 430
download_file_path db '/tmp0/1/', 0
3561 hidnplayr 431
 
432
IM_END:
433
 
5540 hidnplayr 434
url             rb URLMAXLEN
435
sc              system_colors
436
offset          dd ?
437
mouse_dd        dd ?
3561 hidnplayr 438
 
5540 hidnplayr 439
filename_area   rb 256
440
temp_dir_path   rb 4096
441
procinfo        rb 1024
442
fname_buf       rb 4096
443
text_work_area  rb 1024
3561 hidnplayr 444
 
5540 hidnplayr 445
I_END: