Subversion Repositories Kolibri OS

Rev

Rev 4431 | Rev 5543 | 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
53
        jnz     exit
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 ?
3566 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
 
5540 hidnplayr 109
  .mouse:
110
        invoke  edit_box_mouse, edit1
111
        jmp     mainloop
3566 hidnplayr 112
 
5540 hidnplayr 113
open_file:
114
;        mcall   70, ...
115
        jmp     mainloop
3566 hidnplayr 116
 
5540 hidnplayr 117
exit:
118
error:
119
        mcall   -1      ; exit
3566 hidnplayr 120
 
5540 hidnplayr 121
download:
122
;        int 3
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
 
5540 hidnplayr 152
; Create the file
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
160
        jnz     error                   ; TODO: print error message
3561 hidnplayr 161
 
5540 hidnplayr 162
; And start the download
163
        invoke  HTTP_get, url, 0, FLAG_STREAM or FLAG_REUSE_BUFFER, 0
164
        test    eax, eax
165
        jz      error                   ; TODO: print error message
166
        mov     [identifier], eax
167
        mov     [offset], 0
168
        mov     [btn_text], sz_cancel
3561 hidnplayr 169
 
5540 hidnplayr 170
        or      [edit1.flags], ed_figure_only
171
        call    draw_window
3561 hidnplayr 172
 
5540 hidnplayr 173
download_loop:
174
        mcall   10
175
        cmp     eax, EV_REDRAW
176
        je      .redraw
177
        cmp     eax, EV_BUTTON
178
        je      .button
3623 hidnplayr 179
 
5540 hidnplayr 180
        invoke  HTTP_receive, [identifier]
4431 hidnplayr 181
        test    eax, eax
5540 hidnplayr 182
        jz      save_chunk
3561 hidnplayr 183
 
5540 hidnplayr 184
        mov     eax, [identifier]
185
        push    [eax + http_msg.content_length]
186
        pop     [pb.max]
187
        push    [eax + http_msg.content_received]
188
        pop     [pb.value]
3561 hidnplayr 189
 
5540 hidnplayr 190
        invoke  progressbar_draw, pb
191
        jmp     download_loop
3736 hidnplayr 192
 
5540 hidnplayr 193
  .redraw:
194
        call    draw_window
195
        jmp     download_loop
3561 hidnplayr 196
 
5540 hidnplayr 197
  .button:
198
        jmp     http_free
3561 hidnplayr 199
 
5540 hidnplayr 200
save_chunk:
201
        mov     ebp, [identifier]
202
        test    [ebp + http_msg.flags], 0xffff0000      ; error?
203
        jnz     http_free
4431 hidnplayr 204
 
5540 hidnplayr 205
        cmp     [fileinfo], 3
206
        je      @f
207
        DEBUGF  1, "new file size=%u\n", [ebp + http_msg.content_length]
208
        mov     [fileinfo], 4                           ; set end of file
209
        mov     eax, [ebp + http_msg.content_length]
210
        mov     [fileinfo.offset], eax                  ; new file size
211
        mcall   70, fileinfo
4431 hidnplayr 212
 
5540 hidnplayr 213
        mov     [fileinfo], 3                           ; write to existing file
214
  @@:
215
        mov     ecx, [ebp + http_msg.content_received]
216
        sub     ecx, [offset]
217
        mov     [fileinfo.size], ecx
218
        mov     eax, [ebp + http_msg.content_ptr]
219
        mov     [fileinfo.buffer], eax
220
        mov     ebx, [offset]
221
        mov     [fileinfo.offset], ebx
222
        DEBUGF  1, "Writing to disk: size=%u offset=%u\n", ecx, ebx
223
        mcall   70, fileinfo
3561 hidnplayr 224
 
5540 hidnplayr 225
        mov     eax, [ebp + http_msg.content_received]
226
        mov     [offset], eax
3561 hidnplayr 227
 
5540 hidnplayr 228
        test    [ebp + http_msg.flags], FLAG_GOT_ALL_DATA
229
        jz      download_loop
3561 hidnplayr 230
 
5540 hidnplayr 231
        mov     [pb.progress_color], 0x0000c800         ; green
3561 hidnplayr 232
 
5540 hidnplayr 233
http_free:
234
        mcall   40, EVM_REDRAW + EVM_BUTTON
235
        push    [ebp + http_msg.content_received]
236
        pop     [pb.value]
3561 hidnplayr 237
 
5540 hidnplayr 238
        mov     ecx, [ebp + http_msg.content_ptr]
239
        test    ecx, ecx
240
        jz      @f
241
        mcall   68, 13                                  ; free the buffer
242
  @@:
3561 hidnplayr 243
 
5540 hidnplayr 244
        invoke  HTTP_free, [identifier]                 ; free headers and connection
3561 hidnplayr 245
 
5540 hidnplayr 246
        mov     [btn_text], sz_open
247
        call    draw_window
248
        jmp     mainloop
3561 hidnplayr 249
 
250
 
251
 
252
draw_window:
5540 hidnplayr 253
        mcall   12, 1   ; start window draw
3561 hidnplayr 254
 
5540 hidnplayr 255
; get system colors
256
        mcall   48, 3, sc, 40
3561 hidnplayr 257
 
5540 hidnplayr 258
; draw window
3561 hidnplayr 259
        mov     edx, [sc.work]
260
        or      edx, 0x34000000
5540 hidnplayr 261
        mcall   0, <50, 320>, <350, 110>, , 0, title
3561 hidnplayr 262
 
5540 hidnplayr 263
; draw button
264
        mcall   8, <229,75>, <60,16>, 22, [sc.work_button]      ; download
3561 hidnplayr 265
 
5540 hidnplayr 266
; draw button text
3561 hidnplayr 267
        mov     ecx, [sc.work_button_text]
268
        or      ecx, 80000000h
5540 hidnplayr 269
        mcall   4, <240,65>, , [btn_text]
3561 hidnplayr 270
 
5540 hidnplayr 271
; draw editbox
272
        edit_boxes_set_sys_color edit1, editboxes_end, sc
273
        invoke  edit_box_draw, edit1
3623 hidnplayr 274
 
5540 hidnplayr 275
        cmp     [identifier], 0
276
        je     @f
277
; draw progressbar
278
        invoke  progressbar_draw, pb
279
  @@:
280
        mcall   12, 2   ; end window draw
3561 hidnplayr 281
        ret
3566 hidnplayr 282
 
283
 
5540 hidnplayr 284
dont_draw:
285
 
286
        ret
287
 
288
;---------------------------------------------------------------------
3561 hidnplayr 289
; Data area
290
;-----------------------------------------------------------------------------
291
align   4
292
@IMPORT:
293
 
5540 hidnplayr 294
library lib_http,       'http.obj', \
295
        box_lib,        'box_lib.obj', \
296
        proc_lib,       'proc_lib.obj'
3561 hidnplayr 297
 
5540 hidnplayr 298
import  lib_http, \
299
        HTTP_get,       'get', \
300
        HTTP_receive,   'receive', \
301
        HTTP_free,      'free'
3561 hidnplayr 302
 
303
import  box_lib, \
5540 hidnplayr 304
        edit_box_draw,    'edit_box', \
305
        edit_box_key,     'edit_box_key', \
306
        edit_box_mouse,   'edit_box_mouse', \
307
        progressbar_draw, 'progressbar_draw', \
308
        progressbar_prog, 'progressbar_progress'
3561 hidnplayr 309
 
5540 hidnplayr 310
import  proc_lib, \
311
        OpenDialog_Init,  'OpenDialog_init', \
312
        OpenDialog_Start, 'OpenDialog_start'
3561 hidnplayr 313
 
314
 
5540 hidnplayr 315
fileinfo        dd 2
316
  .offset       dd 0, 0
317
  .size         dd 0
318
  .buffer       dd 0
3561 hidnplayr 319
                db 0
5540 hidnplayr 320
                dd fname_buf
3561 hidnplayr 321
 
5540 hidnplayr 322
edit1           edit_box 299, 5, 10, 0xffffff, 0x0000ff, 0x0080ff, 0x000000, 0x8000, URLMAXLEN, url, mouse_dd, ed_focus+ed_always_focus, 0, 0
3561 hidnplayr 323
editboxes_end:
324
 
5540 hidnplayr 325
identifier      dd 0
326
btn_text        dd sz_download
327
sz_download     db 'Download', 0
328
sz_cancel       db ' Cancel ', 0
329
sz_open         db '  Open  ', 0
3561 hidnplayr 330
title           db 'HTTP Downloader', 0
331
 
5540 hidnplayr 332
OpenDialog_data:
333
.type                   dd 1    ; Save
334
.procinfo               dd procinfo
335
.com_area_name          dd communication_area_name
336
.com_area               dd 0
337
.opendir_path           dd temp_dir_path
338
.dir_default_path       dd communication_area_default_path
339
.start_path             dd open_dialog_path
340
.draw_window            dd dont_draw
341
.status                 dd 0
342
.openfile_patch         dd fname_buf
343
.filename_area          dd filename_area
344
.filter_area            dd filter
345
.x:
346
.x_size                 dw 420  ; Window X size
347
.x_start                dw 200  ; Window X position
348
.y:
349
.y_size                 dw 320  ; Window y size
350
.y_start                dw 120  ; Window Y position
3561 hidnplayr 351
 
5540 hidnplayr 352
communication_area_name         db 'FFFFFFFF_open_dialog',0
353
open_dialog_path                db '/sys/File Managers/opendial',0
354
communication_area_default_path db '/sys',0
3561 hidnplayr 355
 
5540 hidnplayr 356
filter:
357
dd      0
358
db      0
3561 hidnplayr 359
 
5540 hidnplayr 360
pb:
361
.value          dd 0
362
.left           dd 5
363
.top            dd 35
364
.width          dd 300
365
.height         dd 16
366
.style          dd 1
367
.min            dd 0
368
.max            dd 0
369
.back_color     dd 0xefefef
370
.progress_color dd 0xc8c8c8
371
.frame_color    dd 0x94aece
372
.frame_color2   dd 0xffffff
3561 hidnplayr 373
 
5540 hidnplayr 374
include_debug_strings
3561 hidnplayr 375
 
5540 hidnplayr 376
download_file_path db '/tmp0/1/', 0
3561 hidnplayr 377
 
378
IM_END:
379
 
5540 hidnplayr 380
url             rb URLMAXLEN
381
sc              system_colors
382
offset          dd ?
383
mouse_dd        dd ?
3561 hidnplayr 384
 
5540 hidnplayr 385
filename_area   rb 256
386
temp_dir_path   rb 4096
387
procinfo        rb 1024
388
fname_buf       rb 4096
389
text_work_area  rb 1024
3561 hidnplayr 390
 
5540 hidnplayr 391
I_END: