Subversion Repositories Kolibri OS

Rev

Rev 3566 | Rev 3618 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3566 Rev 3572
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2009-2013. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2009-2013. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
5
;;                                                                 ;;
6
;;  downloader.asm - HTTP client for KolibriOS                     ;;
6
;;  downloader.asm - HTTP client for KolibriOS                     ;;
7
;;                                                                 ;;
7
;;                                                                 ;;
8
;;  Based on HTTPC.asm for menuetos by ville turjanmaa             ;;
8
;;  Based on HTTPC.asm for menuetos by ville turjanmaa             ;;
9
;;                                                                 ;;
9
;;                                                                 ;;
10
;;  Programmers: Barsuk, Clevermouse, Marat Zakiyanov,             ;;
10
;;  Programmers: Barsuk, Clevermouse, Marat Zakiyanov,             ;;
11
;;      Kirill Lipatov, dunkaist, HidnPlayr                        ;;
11
;;      Kirill Lipatov, dunkaist, HidnPlayr                        ;;
12
;;                                                                 ;;
12
;;                                                                 ;;
13
;;          GNU GENERAL PUBLIC LICENSE                             ;;
13
;;          GNU GENERAL PUBLIC LICENSE                             ;;
14
;;             Version 2, June 1991                                ;;
14
;;             Version 2, June 1991                                ;;
15
;;                                                                 ;;
15
;;                                                                 ;;
16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
17
 
17
 
18
URLMAXLEN               = 1024
18
URLMAXLEN               = 1024
19
BUFFERSIZE              = 4096
19
BUFFERSIZE              = 4096
20
 
20
 
21
__DEBUG__       = 1
21
__DEBUG__       = 1
22
__DEBUG_LEVEL__ = 2
22
__DEBUG_LEVEL__ = 2
23
 
23
 
24
format binary as ""
24
format binary as ""
25
 
25
 
26
use32
26
use32
27
        org     0x0
27
        org     0x0
28
 
28
 
29
        db      'MENUET01'      ; header
29
        db      'MENUET01'      ; header
30
        dd      0x01            ; header version
30
        dd      0x01            ; header version
31
        dd      START           ; entry point
31
        dd      START           ; entry point
32
        dd      IM_END          ; image size
32
        dd      IM_END          ; image size
33
        dd      I_END+0x100     ; required memory
33
        dd      I_END+0x100     ; required memory
34
        dd      I_END+0x100     ; esp
34
        dd      I_END+0x100     ; esp
35
        dd      params          ; I_PARAM
35
        dd      params          ; I_PARAM
36
        dd      0x0             ; I_Path
36
        dd      0x0             ; I_Path
37
 
37
 
38
include '../macros.inc'
38
include '../macros.inc'
39
include '../proc32.inc'
39
include '../proc32.inc'
40
include '../network.inc'
40
include '../network.inc'
41
include '../../develop/libraries/box_lib/trunk/box_lib.mac'
41
include '../../develop/libraries/box_lib/trunk/box_lib.mac'
42
include '../dll.inc'
42
include '../dll.inc'
43
include '../debug-fdo.inc'
43
include '../debug-fdo.inc'
44
 
44
 
45
START:
45
START:
46
 
46
 
47
        mcall   68, 11                  ; init heap so we can allocate memory dynamically
47
        mcall   68, 11                  ; init heap so we can allocate memory dynamically
48
 
48
 
49
        mcall   40, EV_STACK
49
        mcall   40, EV_STACK
50
 
50
 
51
; load libraries
51
; load libraries
52
        stdcall dll.Load, @IMPORT
52
        stdcall dll.Load, @IMPORT
53
        test    eax, eax
53
        test    eax, eax
54
        jnz     exit
54
        jnz     exit
55
 
55
 
56
; prepare webAddr area
56
; prepare webAddr area
57
        mov     al, ' '
57
        mov     al, ' '
58
        mov     edi, webAddr
58
        mov     edi, webAddr
59
        mov     ecx, URLMAXLEN
59
        mov     ecx, URLMAXLEN
60
        rep     stosb
60
        rep     stosb
61
        xor     eax, eax
61
        xor     eax, eax
62
        stosb
62
        stosb
63
 
63
 
64
; prepare document area 
64
; prepare document area 
65
        mov     al, '/'
65
        mov     al, '/'
66
        mov     edi, document
66
        mov     edi, document
67
        stosb
67
        stosb
68
        mov     al, ' '
68
        mov     al, ' '
69
        mov     ecx, URLMAXLEN-1
69
        mov     ecx, URLMAXLEN-1
70
        rep     stosb
70
        rep     stosb
71
 
71
 
72
        call    load_settings
72
        call    load_settings
73
        cmp     byte[params], 0
73
        cmp     byte[params], 0
74
        je      prepare_event   ;red
74
        je      prepare_event   ;red
75
 
75
 
76
; we have an url
76
; we have an url
77
        mov     edi, document_user
77
        mov     edi, document_user
78
        mov     al, ' '
78
        mov     al, ' '
79
        mov     ecx, URLMAXLEN
79
        mov     ecx, URLMAXLEN
80
        rep     stosb
80
        rep     stosb
81
        
81
        
82
        mov     esi, params
82
        mov     esi, params
83
        mov     edi, document_user
83
        mov     edi, document_user
84
 
84
 
85
; copy untill space or 0
85
; copy untill space or 0
86
  .copy_param:
86
  .copy_param:
87
        mov     al, [esi]
87
        mov     al, [esi]
88
        test    al, al
88
        test    al, al
89
        jz      .done
89
        jz      .done
90
 
90
 
91
        cmp     al, ' '
91
        cmp     al, ' '
92
        jz      .done_inc
92
        jz      .done_inc
93
 
93
 
94
        mov     [edi], al
94
        mov     [edi], al
95
        inc     esi
95
        inc     esi
96
        inc     edi
96
        inc     edi
97
        jmp     .copy_param
97
        jmp     .copy_param
98
 
98
 
99
  .done_inc:
99
  .done_inc:
100
 
100
 
101
; url is followed by shared memory name.
101
; url is followed by shared memory name.
102
        inc     esi
102
        inc     esi
103
  .done:
103
  .done:
104
        mov     [shared_name], esi
104
        mov     [shared_name], esi
105
        jmp     still
105
        jmp     download
106
 
106
 
107
prepare_event:
107
prepare_event:
108
; Report events
108
; Report events
109
; Stack 8 + defaults
109
; Stack 8 + defaults
110
        mcall   40, 10100111b
110
        mcall   40, 10100111b
111
 
111
 
112
red:    ; redraw
112
red:    ; redraw
113
        call    draw_window
113
        call    draw_window
114
 
114
 
115
still:
115
still:
-
 
116
        cmp     byte [params], 0
-
 
117
        jne     exit
-
 
118
 
116
        mcall   10      ; wait here for event
119
        mcall   10      ; wait here for event
117
        cmp     eax, 1  ; redraw request ?
120
        cmp     eax, 1  ; redraw request ?
118
        je      red
121
        je      red
119
 
122
 
120
        cmp     eax, 2  ; key in buffer ?
123
        cmp     eax, 2  ; key in buffer ?
121
        je      key
124
        je      key
122
 
125
 
123
        cmp     eax, 3  ; button in buffer ?
126
        cmp     eax, 3  ; button in buffer ?
124
        je      button
127
        je      button
125
        
128
        
126
        cmp     eax, 6  ; mouse in buffer ?
129
        cmp     eax, 6  ; mouse in buffer ?
127
        je      mouse
130
        je      mouse
128
 
131
 
129
        jmp     still
132
        jmp     still
130
 
133
 
131
 
134
 
132
key:
135
key:
133
        mcall   2       ; read key
136
        mcall   2       ; read key
134
 
137
 
135
        stdcall [edit_box_key], dword edit1
138
        stdcall [edit_box_key], dword edit1
136
 
139
 
137
        cmp     ax, 13 shl 8
140
        cmp     ax, 13 shl 8
138
        je      download
141
        je      download
139
        
142
        
140
        jmp     still
143
        jmp     still
141
        
144
        
142
button:
145
button:
143
 
146
 
144
        mcall   17      ; get id
147
        mcall   17      ; get id
145
        cmp     ah, 26
148
        cmp     ah, 26
146
        jne     @f
149
        jne     @f
147
        call    save_to_file
150
        call    save_to_file
148
        jmp     still
151
        jmp     still
149
  @@:
152
  @@:
150
        cmp     ah, 1   ; button id=1 ?
153
        cmp     ah, 1   ; button id=1 ?
151
        je      exit
154
        je      exit
152
 
155
 
153
        jmp     download
156
        jmp     download
154
 
157
 
155
mouse:
158
mouse:
156
        stdcall [edit_box_mouse], edit1
159
        stdcall [edit_box_mouse], edit1
157
        jmp     still
160
        jmp     still
158
 
161
 
159
exit:
162
exit:
160
        or      eax, -1  ; close this program
163
        or      eax, -1  ; close this program
161
        mcall
164
        mcall
162
 
165
 
163
download:
166
download:
164
 
167
 
165
        DEBUGF  1, "Starting download\n"
168
        DEBUGF  1, "Starting download\n"
166
 
169
 
167
        call    parse_url
170
        call    parse_url
168
        call    open_socket
171
        call    open_socket
169
        call    send_request
172
        call    send_request
170
 
173
 
171
        mcall   68, 12, BUFFERSIZE
174
        mcall   68, 12, BUFFERSIZE
172
        mov     [buf_ptr], eax
175
        mov     [buf_ptr], eax
173
        mov     [buf_size], 0
176
        mov     [buf_size], 0
174
 
177
 
175
        call    read_incoming_data
178
        call    read_incoming_data
176
 
179
 
177
        mcall   close, [socketnum]
180
        mcall   close, [socketnum]
178
 
181
 
179
        call    parse_result
182
        call    parse_result
180
        call    save
183
        call    save
181
 
184
 
182
        mcall   68, 13, [final_buffer]
185
        mcall   68, 13, [final_buffer]
183
 
186
 
184
        jmp     still
187
        jmp     still
185
 
188
 
186
 
189
 
187
save:
190
save:
188
 
191
 
189
        mov     ecx, [shared_name]
192
        mov     ecx, [shared_name]
190
        test    ecx, ecx
193
        test    ecx, ecx
191
        jz      @f
194
        jz      @f
192
        cmp     [ecx], byte 0
195
        cmp     [ecx], byte 0
193
        jnz     save_in_shared
196
        jnz     save_in_shared
194
    @@:
197
    @@:
195
 
198
 
196
        call    save_to_file
199
        call    save_to_file
197
 
200
 
198
; if called from command line, then exit
201
; if called from command line, then exit
199
        cmp     byte[params], 0
202
        cmp     byte[params], 0
200
        jne     exit
203
        jne     exit
201
 
204
 
202
        ret
205
        ret
203
 
206
 
204
save_in_shared:
207
save_in_shared:
205
 
208
 
206
        mov     esi, 1   ; SHM_OPEN+SHM_WRITE
209
        mov     esi, 1   ; SHM_OPEN+SHM_WRITE
207
        mcall   68, 22
210
        mcall   68, 22
208
        test    eax, eax
211
        test    eax, eax
209
        jz      exit
212
        jz      exit
210
 
213
 
211
        sub     edx, 4
214
        sub     edx, 4
212
        jbe     exit
215
        jbe     exit
213
 
216
 
214
        mov     ecx, [final_size]
217
        mov     ecx, [final_size]
215
        cmp     ecx, edx
218
        cmp     ecx, edx
216
        jb      @f
219
        jb      @f
217
 
220
 
218
        mov     ecx, edx
221
        mov     ecx, edx
219
    @@:
222
    @@:
220
        mov     [eax], ecx
223
        mov     [eax], ecx
221
        lea     edi, [eax+4]
224
        lea     edi, [eax+4]
222
        mov     esi, [final_buffer]
225
        mov     esi, [final_buffer]
223
        mov     edx, ecx
226
        mov     edx, ecx
224
        shr     ecx, 2
227
        shr     ecx, 2
225
        rep     movsd
228
        rep     movsd
226
        mov     ecx, edx
229
        mov     ecx, edx
227
        and     ecx, 3
230
        and     ecx, 3
228
        rep     movsb
231
        rep     movsb
229
 
232
 
230
        jmp     exit
233
        jmp     exit
231
 
234
 
232
 
235
 
233
;****************************************************************************
236
;****************************************************************************
234
;    Function
237
;    Function
235
;       save_to_file
238
;       save_to_file
236
;
239
;
237
;   Description
240
;   Description
238
;
241
;
239
;
242
;
240
;****************************************************************************
243
;****************************************************************************
241
 
244
 
242
save_to_file:
245
save_to_file:
243
 
246
 
244
        DEBUGF  2, "Saving to file\n"
247
        DEBUGF  2, "Saving to file\n"
245
        mcall   70, fileinfo
248
        mcall   70, fileinfo
246
 
249
 
247
        mov     ecx, [sc.work_text]
250
        mov     ecx, [sc.work_text]
248
        or      ecx, 0x80000000
251
        or      ecx, 0x80000000
249
        mcall   4, <10, 93>, , download_complete
252
        mcall   4, <10, 93>, , download_complete
250
 
253
 
251
        ret
254
        ret
252
 
255
 
253
 
256
 
254
;****************************************************************************
257
;****************************************************************************
255
;    Function
258
;    Function
256
;       send_request
259
;       send_request
257
;
260
;
258
;   Description
261
;   Description
259
;       Transmits the GET request to the server.
262
;       Transmits the GET request to the server.
260
;       This is done as GET then URL then HTTP/1.1', 13, 10, 13, 10 in 3 packets
263
;       This is done as GET then URL then HTTP/1.1', 13, 10, 13, 10 in 3 packets
261
;
264
;
262
;****************************************************************************
265
;****************************************************************************
263
send_request:
266
send_request:
264
 
267
 
265
        DEBUGF  1, "Sending request\n"
268
        DEBUGF  1, "Sending request\n"
266
 
269
 
267
        mov     esi, string0
270
        mov     esi, string0
268
        mov     edi, request
271
        mov     edi, request
269
        movsd
272
        movsd
270
; If proxy is used, make absolute URI - prepend http://
273
; If proxy is used, make absolute URI - prepend http://
271
        cmp     byte[proxyAddr], 0
274
        cmp     byte[proxyAddr], 0
272
        jz      .noproxy
275
        jz      .noproxy
273
        mov     dword[edi], 'http'
276
        mov     dword[edi], 'http'
274
        mov     byte[edi+4], ':'
277
        mov     byte[edi+4], ':'
275
        mov     word[edi+5], '//'
278
        mov     word[edi+5], '//'
276
        add     edi, 7
279
        add     edi, 7
277
        mov     esi, webAddr
280
        mov     esi, webAddr
278
 
281
 
279
  .copy_host_loop:
282
  .copy_host_loop:
280
        lodsb
283
        lodsb
281
        cmp     al, ' '
284
        cmp     al, ' '
282
        jz      .noproxy
285
        jz      .noproxy
283
        stosb
286
        stosb
284
        jmp     .copy_host_loop
287
        jmp     .copy_host_loop
285
 
288
 
286
  .noproxy:
289
  .noproxy:
287
        xor     edx, edx ; 0
290
        xor     edx, edx ; 0
288
 
291
 
289
  .next_edx:
292
  .next_edx:
290
; Determine the length of the url to send in the GET request
293
; Determine the length of the url to send in the GET request
291
        mov     al, [edx+document]
294
        mov     al, [edx+document]
292
        cmp     al, ' '
295
        cmp     al, ' '
293
        jbe     .document_done
296
        jbe     .document_done
294
        mov     [edi], al
297
        mov     [edi], al
295
        inc     edi
298
        inc     edi
296
        inc     edx
299
        inc     edx
297
        jmp     .next_edx
300
        jmp     .next_edx
298
 
301
 
299
  .document_done:
302
  .document_done:
300
        mov     esi, stringh
303
        mov     esi, stringh
301
        mov     ecx, stringh_end-stringh
304
        mov     ecx, stringh_end-stringh
302
        rep     movsb
305
        rep     movsb
303
        xor     edx, edx ; 0
306
        xor     edx, edx ; 0
304
 
307
 
305
  .webaddr_next:
308
  .webaddr_next:
306
        mov     al, [webAddr + edx]
309
        mov     al, [webAddr + edx]
307
        cmp     al, ' '
310
        cmp     al, ' '
308
        jbe     .webaddr_done
311
        jbe     .webaddr_done
309
        mov     [edi], al
312
        mov     [edi], al
310
        inc     edi
313
        inc     edi
311
        inc     edx
314
        inc     edx
312
        jmp     .webaddr_next
315
        jmp     .webaddr_next
313
 
316
 
314
  .webaddr_done:
317
  .webaddr_done:
315
        cmp     byte[proxyUser], 0
318
        cmp     byte[proxyUser], 0
316
        jz      @f
319
        jz      @f
317
        call    append_proxy_auth_header
320
        call    append_proxy_auth_header
318
    @@:
321
    @@:
319
        mov     esi, connclose
322
        mov     esi, connclose
320
        mov     ecx, connclose_end-connclose
323
        mov     ecx, connclose_end-connclose
321
        rep     movsb
324
        rep     movsb
322
 
325
 
323
        pusha   
326
        pusha   
324
        mov     eax, 63
327
        mov     eax, 63
325
        mov     ebx, 1
328
        mov     ebx, 1
326
        mov     edx, request
329
        mov     edx, request
327
    @@:
330
    @@:
328
        mov     cl, [edx]
331
        mov     cl, [edx]
329
        cmp     edx, edi
332
        cmp     edx, edi
330
        jz      @f
333
        jz      @f
331
        mcall
334
        mcall
332
        inc     edx
335
        inc     edx
333
        jmp     @b
336
        jmp     @b
334
    @@:
337
    @@:
335
        popa
338
        popa
336
 
339
 
337
        mov     esi, edi
340
        mov     esi, edi
338
        sub     esi, request    ; length
341
        sub     esi, request    ; length
339
        xor     edi, edi        ; flags
342
        xor     edi, edi        ; flags
340
        mcall   send, [socketnum], request  ;' HTTP/1.1 .. '
343
        mcall   send, [socketnum], request  ;' HTTP/1.1 .. '
341
        ret
344
        ret
342
 
345
 
343
;****************************************************************************
346
;****************************************************************************
344
;    Function
347
;    Function
345
;       read_incoming_data
348
;       read_incoming_data
346
;
349
;
347
;   Description
350
;   Description
348
;       receive the web page from the server, storing it without processing
351
;       receive the web page from the server, storing it without processing
349
;
352
;
350
;****************************************************************************
353
;****************************************************************************
351
read_incoming_data:
354
read_incoming_data:
352
 
355
 
353
        DEBUGF  1, "Reading incoming data\n"
356
        DEBUGF  1, "Reading incoming data\n"
354
 
357
 
355
        mov     eax, [buf_ptr]
358
        mov     eax, [buf_ptr]
356
        mov     [pos], eax
359
        mov     [pos], eax
357
 
360
 
358
  .read:
361
  .read:
359
        mcall   23, 100         ; 1 second timeout
362
        mcall   23, 100         ; 1 second timeout
360
  .read_dontwait:
363
  .read_dontwait:
361
        mcall   recv, [socketnum], [pos], BUFFERSIZE, 0
364
        mcall   recv, [socketnum], [pos], BUFFERSIZE, 0
362
        inc     eax             ; -1 = error (socket closed?)
365
        inc     eax             ; -1 = error (socket closed?)
363
        jz      .no_more_data
366
        jz      .no_more_data
364
        dec     eax             ; 0 bytes...
367
        dec     eax             ; 0 bytes...
365
        jz      .read           ; timeout
368
        jz      .read           ; timeout
366
 
369
 
367
        DEBUGF  1, "Got chunk of %u bytes\n", eax
370
        DEBUGF  1, "Got chunk of %u bytes\n", eax
368
 
371
 
369
        add     [buf_size], eax
372
        add     [buf_size], eax
370
        add     [pos], eax
373
        add     [pos], eax
371
        push    eax
374
        push    eax
372
        mov     ecx, [buf_size]
375
        mov     ecx, [buf_size]
373
        add     ecx, BUFFERSIZE
376
        add     ecx, BUFFERSIZE
374
        mcall   68, 20, , [buf_ptr]     ; reallocate memory block (make bigger)
377
        mcall   68, 20, , [buf_ptr]     ; reallocate memory block (make bigger)
375
        ; TODO: parse header and resize buffer only once
378
        ; TODO: parse header and resize buffer only once
376
        pop     eax
379
        pop     eax
377
 
380
 
378
        cmp     eax, BUFFERSIZE
381
        cmp     eax, BUFFERSIZE
379
        je      .read_dontwait
382
        je      .read_dontwait
380
        jmp     .read
383
        jmp     .read
381
        
384
        
382
  .no_more_data:
385
  .no_more_data:
383
        mov     eax, [buf_ptr]
386
        mov     eax, [buf_ptr]
384
        sub     [pos], eax
387
        sub     [pos], eax
385
 
388
 
386
        DEBUGF  1, "No more data\n"
389
        DEBUGF  1, "No more data\n"
387
 
390
 
388
        ret
391
        ret
389
        
392
        
390
 
393
 
391
        
394
        
392
; this function cuts header, and removes chunk sizes if doc is chunked
395
; this function cuts header, and removes chunk sizes if doc is chunked
393
; in: buf_ptr, pos; out: buf_ptr, pos.
396
; in: buf_ptr, pos; out: buf_ptr, pos.
394
        
397
        
395
parse_result:
398
parse_result:
396
 
399
 
397
        mov     edi, [buf_ptr]
400
        mov     edi, [buf_ptr]
398
        mov     edx, [pos]
401
        mov     edx, [pos]
399
;        mov     [buf_size], edx
402
;        mov     [buf_size], edx
400
;       mcall   70, fileinfo_tmp
403
;       mcall   70, fileinfo_tmp
401
        DEBUGF  1, "Parsing result (%u bytes)\n", edx
404
        DEBUGF  1, "Parsing result (%u bytes)\n", edx
402
 
405
 
403
; first, find end of headers
406
; first, find end of headers
404
  .next_byte:
407
  .next_byte:
405
        cmp     dword[edi], 0x0d0a0d0a  ; ìíå ëåíü ÷èòàòü ñòàíäàðò, ïóñòü áóäóò îáà âàðèàíòà
408
        cmp     dword[edi], 0x0d0a0d0a  ; ìíå ëåíü ÷èòàòü ñòàíäàðò, ïóñòü áóäóò îáà âàðèàíòà
406
        je      .end_of_headers
409
        je      .end_of_headers
407
        cmp     dword[edi], 0x0a0d0a0d
410
        cmp     dword[edi], 0x0a0d0a0d
408
        je      .end_of_headers
411
        je      .end_of_headers
409
        inc     edi
412
        inc     edi
410
        dec     edx
413
        dec     edx
411
        jne     .next_byte
414
        jne     .next_byte
412
        DEBUGF  1, "Uh-oh, there's no end of header!\n"
415
        DEBUGF  1, "Uh-oh, there's no end of header!\n"
413
; no end of headers. it's an error. let client see all those headers.
416
; no end of headers. it's an error. let client see all those headers.
414
        ret
417
        ret
415
 
418
 
416
  .end_of_headers:
419
  .end_of_headers:
417
; here we look at headers and search content-length or transfer-encoding headers
420
; here we look at headers and search content-length or transfer-encoding headers
418
        DEBUGF  1, "Found end of header\n"
421
        DEBUGF  1, "Found end of header\n"
419
 
422
 
420
        sub     edi, [buf_ptr]
423
        sub     edi, [buf_ptr]
421
        add     edi, 4
424
        add     edi, 4
422
        mov     [body_pos], edi  ; store position where document body starts
425
        mov     [body_pos], edi  ; store position where document body starts
423
        mov     [is_chunked], 0
426
        mov     [is_chunked], 0
424
; find content-length in headers
427
; find content-length in headers
425
; not good method, but should work for 'Content-Length:'
428
; not good method, but should work for 'Content-Length:'
426
        mov     esi, [buf_ptr]
429
        mov     esi, [buf_ptr]
427
        mov     edi, s_contentlength
430
        mov     edi, s_contentlength
428
        mov     ebx, [body_pos]
431
        mov     ebx, [body_pos]
429
        xor     edx, edx ; 0
432
        xor     edx, edx ; 0
430
  .cl_next:
433
  .cl_next:
431
        mov     al, [esi]
434
        mov     al, [esi]
432
        cmp     al, [edi + edx]
435
        cmp     al, [edi + edx]
433
        jne     .cl_fail
436
        jne     .cl_fail
434
        inc     edx
437
        inc     edx
435
        cmp     edx, len_contentlength
438
        cmp     edx, len_contentlength
436
        je      .cl_found
439
        je      .cl_found
437
        jmp     .cl_incr
440
        jmp     .cl_incr
438
  .cl_fail:
441
  .cl_fail:
439
        xor     edx, edx ; 0
442
        xor     edx, edx ; 0
440
  .cl_incr:
443
  .cl_incr:
441
        inc     esi
444
        inc     esi
442
        dec     ebx
445
        dec     ebx
443
        je      .cl_error
446
        je      .cl_error
444
        jmp     .cl_next
447
        jmp     .cl_next
445
  .cl_error:
448
  .cl_error:
446
        DEBUGF  1, "content-length not found\n"
449
        DEBUGF  1, "content-length not found\n"
447
 
450
 
448
; find 'chunked'
451
; find 'chunked'
449
; äà, ÿ êîïèðóþ êîä, ýòî óæàñíî, íî ìíå õî÷åòñÿ, ÷òîáû ïîñêîðåå çàðàáîòàëî
452
; äà, ÿ êîïèðóþ êîä, ýòî óæàñíî, íî ìíå õî÷åòñÿ, ÷òîáû ïîñêîðåå çàðàáîòàëî
450
; à òàì óæ îòðåôàêòîðþ
453
; à òàì óæ îòðåôàêòîðþ
451
        mov     esi, [buf_ptr]
454
        mov     esi, [buf_ptr]
452
        mov     edi, s_chunked
455
        mov     edi, s_chunked
453
        mov     ebx, [body_pos]
456
        mov     ebx, [body_pos]
454
        xor     edx, edx ; 0
457
        xor     edx, edx ; 0
455
 
458
 
456
  .ch_next:
459
  .ch_next:
457
        mov     al, [esi]
460
        mov     al, [esi]
458
        cmp     al, [edi + edx]
461
        cmp     al, [edi + edx]
459
        jne     .ch_fail
462
        jne     .ch_fail
460
        inc     edx
463
        inc     edx
461
        cmp     edx, len_chunked
464
        cmp     edx, len_chunked
462
        je      .ch_found
465
        je      .ch_found
463
        jmp     .ch_incr
466
        jmp     .ch_incr
464
 
467
 
465
  .ch_fail:
468
  .ch_fail:
466
        xor     edx, edx ; 0
469
        xor     edx, edx ; 0
467
 
470
 
468
  .ch_incr:
471
  .ch_incr:
469
        inc     esi
472
        inc     esi
470
        dec     ebx
473
        dec     ebx
471
        je      .ch_error
474
        je      .ch_error
472
        jmp     .ch_next
475
        jmp     .ch_next
473
 
476
 
474
  .ch_error:
477
  .ch_error:
475
; if neither of the 2 headers is found, it's an error
478
; if neither of the 2 headers is found, it's an error
476
;       DEBUGF  1, "transfer-encoding: chunked not found\n"
479
;       DEBUGF  1, "transfer-encoding: chunked not found\n"
477
        mov     eax, [pos]
480
        mov     eax, [pos]
478
        sub     eax, [body_pos]
481
        sub     eax, [body_pos]
479
        jmp     .write_final_size
482
        jmp     .write_final_size
480
 
483
 
481
  .ch_found:
484
  .ch_found:
482
        mov     [is_chunked], 1
485
        mov     [is_chunked], 1
483
        mov     eax, [body_pos]
486
        mov     eax, [body_pos]
484
        add     eax, [buf_ptr]
487
        add     eax, [buf_ptr]
485
        sub     eax, 2
488
        sub     eax, 2
486
        mov     [prev_chunk_end], eax
489
        mov     [prev_chunk_end], eax
487
        jmp     parse_chunks
490
        jmp     parse_chunks
488
        
491
        
489
  .cl_found:
492
  .cl_found:
490
        call    read_number     ; eax = number from *esi
493
        call    read_number     ; eax = number from *esi
491
        DEBUGF  1, "Content length: %u\n", eax
494
        DEBUGF  1, "Content length: %u\n", eax
492
 
495
 
493
  .write_final_size:
496
  .write_final_size:
494
        
497
        
495
        mov     ebx, [buf_size]
498
        mov     ebx, [buf_size]
496
        sub     ebx, [body_pos]
499
        sub     ebx, [body_pos]
497
        cmp     eax, ebx
500
        cmp     eax, ebx
498
        jbe     .size_ok
501
        jbe     .size_ok
499
        sub     eax, ebx
502
        sub     eax, ebx
500
        DEBUGF  2, "%u bytes of data are missing!\n", eax
503
        DEBUGF  2, "%u bytes of data are missing!\n", eax
501
        mov     eax, ebx
504
        mov     eax, ebx
502
  .size_ok:
505
  .size_ok:
503
        mov     [final_size], eax
506
        mov     [final_size], eax
504
 
507
 
505
        mov     ebx, [body_pos]
508
        mov     ebx, [body_pos]
506
        add     ebx, [buf_ptr]
509
        add     ebx, [buf_ptr]
507
        mov     [final_buffer], ebx
510
        mov     [final_buffer], ebx
508
 
511
 
509
        ret
512
        ret
510
        
513
        
511
parse_chunks:
514
parse_chunks:
512
        DEBUGF  1, "parse chunks\n"
515
        DEBUGF  1, "parse chunks\n"
513
        ; we have to look through the data and remove sizes of chunks we see
516
        ; we have to look through the data and remove sizes of chunks we see
514
        ; 1. read size of next chunk
517
        ; 1. read size of next chunk
515
        ; 2. if 0, it's end. if not, continue.
518
        ; 2. if 0, it's end. if not, continue.
516
        ; 3. make a good buffer and copy a chunk there
519
        ; 3. make a good buffer and copy a chunk there
517
        xor     eax, eax
520
        xor     eax, eax
518
        mov     [final_buffer], eax      ; 0
521
        mov     [final_buffer], eax      ; 0
519
        mov     [final_size], eax        ; 0
522
        mov     [final_size], eax        ; 0
520
        
523
        
521
.read_size:
524
.read_size:
522
        mov     eax, [prev_chunk_end]
525
        mov     eax, [prev_chunk_end]
523
        mov     ebx, eax
526
        mov     ebx, eax
524
        sub     ebx, [buf_ptr]
527
        sub     ebx, [buf_ptr]
525
        mov     edx, eax
528
        mov     edx, eax
526
        DEBUGF  1, "rs "
529
        DEBUGF  1, "rs "
527
        cmp     ebx, [pos]
530
        cmp     ebx, [pos]
528
        jae     chunks_end      ; not good
531
        jae     chunks_end      ; not good
529
        
532
        
530
        call    read_hex        ; in: eax=pointer to text. out:eax=hex number, ebx=end of text.
533
        call    read_hex        ; in: eax=pointer to text. out:eax=hex number, ebx=end of text.
531
        cmp     eax, 0
534
        cmp     eax, 0
532
        jz      chunks_end
535
        jz      chunks_end
533
 
536
 
534
        add     ebx, 1
537
        add     ebx, 1
535
        mov     edx, ebx ; edx = size of size of chunk
538
        mov     edx, ebx ; edx = size of size of chunk
536
        
539
        
537
        add     ebx, eax
540
        add     ebx, eax
538
        mov     [prev_chunk_end], ebx
541
        mov     [prev_chunk_end], ebx
539
        
542
        
540
        DEBUGF  1, "sz "
543
        DEBUGF  1, "sz "
541
 
544
 
542
; do copying: from buf_ptr+edx to final_buffer+prev_final_size count eax
545
; do copying: from buf_ptr+edx to final_buffer+prev_final_size count eax
543
; realloc final buffer
546
; realloc final buffer
544
        push    eax
547
        push    eax
545
        push    edx
548
        push    edx
546
        push    dword [final_size]
549
        push    dword [final_size]
547
        add     [final_size], eax
550
        add     [final_size], eax
548
        mcall   68, 20, [final_size], [final_buffer]
551
        mcall   68, 20, [final_size], [final_buffer]
549
        mov     [final_buffer], eax
552
        mov     [final_buffer], eax
550
        DEBUGF  1, "re "
553
        DEBUGF  1, "re "
551
        pop     edi
554
        pop     edi
552
        pop     esi
555
        pop     esi
553
        pop     ecx
556
        pop     ecx
554
;       add     [pos], ecx
557
;       add     [pos], ecx
555
        add     edi, [final_buffer]
558
        add     edi, [final_buffer]
556
        DEBUGF  1, "cp "
559
        DEBUGF  1, "cp "
557
 
560
 
558
        rep     movsb
561
        rep     movsb
559
        jmp     .read_size
562
        jmp     .read_size
560
        
563
        
561
chunks_end:
564
chunks_end:
562
        DEBUGF  1, "chunks end\n"
565
        DEBUGF  1, "chunks end\n"
563
        mcall   68, 13, [buf_ptr]       ; free old buffer
566
        mcall   68, 13, [buf_ptr]       ; free old buffer
564
 
567
 
565
        ret
568
        ret
566
 
569
 
567
; reads content-length from [edi+ecx], result in eax
570
; reads content-length from [edi+ecx], result in eax
568
read_number:
571
read_number:
569
        push    ebx
572
        push    ebx
570
        xor     eax, eax
573
        xor     eax, eax
571
        xor     ebx, ebx
574
        xor     ebx, ebx
572
 
575
 
573
  .next:
576
  .next:
574
        mov     bl, [esi]
577
        mov     bl, [esi]
575
 
578
 
576
        cmp     bl, '0'
579
        cmp     bl, '0'
577
        jb      .not_number
580
        jb      .not_number
578
        cmp     bl, '9'
581
        cmp     bl, '9'
579
        ja      .not_number
582
        ja      .not_number
580
        sub     bl, '0'
583
        sub     bl, '0'
581
        shl     eax, 1
584
        shl     eax, 1
582
        lea     eax, [eax + eax * 4]     ; eax *= 10
585
        lea     eax, [eax + eax * 4]     ; eax *= 10
583
        add     eax, ebx
586
        add     eax, ebx
584
 
587
 
585
  .not_number:
588
  .not_number:
586
        cmp     bl, 13
589
        cmp     bl, 13
587
        je      .done
590
        je      .done
588
        inc     esi
591
        inc     esi
589
        jmp     .next
592
        jmp     .next
590
 
593
 
591
  .done:
594
  .done:
592
        pop     ebx
595
        pop     ebx
593
        ret
596
        ret
594
        
597
        
595
; reads hex from eax, result in eax, end of text in ebx
598
; reads hex from eax, result in eax, end of text in ebx
596
read_hex:
599
read_hex:
597
        add     eax, 2
600
        add     eax, 2
598
        mov     ebx, eax
601
        mov     ebx, eax
599
        mov     eax, [ebx]
602
        mov     eax, [ebx]
600
        mov     [deba], eax
603
        mov     [deba], eax
601
 
604
 
602
        xor     eax, eax
605
        xor     eax, eax
603
        xor     ecx, ecx
606
        xor     ecx, ecx
604
.next:
607
.next:
605
        mov     cl, [ebx]
608
        mov     cl, [ebx]
606
        inc     ebx
609
        inc     ebx
607
        
610
        
608
        cmp     cl, 0x0d
611
        cmp     cl, 0x0d
609
        jz      .done
612
        jz      .done
610
 
613
 
611
        or      cl, 0x20
614
        or      cl, 0x20
612
        sub     cl, '0'
615
        sub     cl, '0'
613
        jb      .bad
616
        jb      .bad
614
 
617
 
615
        cmp     cl, 0x9
618
        cmp     cl, 0x9
616
        jbe     .adding
619
        jbe     .adding
617
 
620
 
618
        sub     cl, 'a'-'0'-10
621
        sub     cl, 'a'-'0'-10
619
        cmp     cl, 0x0a
622
        cmp     cl, 0x0a
620
        jb      .bad
623
        jb      .bad
621
 
624
 
622
        cmp     cl, 0x0f
625
        cmp     cl, 0x0f
623
        ja      .bad
626
        ja      .bad
624
 
627
 
625
.adding:
628
.adding:
626
        shl     eax, 4
629
        shl     eax, 4
627
        or      eax, ecx
630
        or      eax, ecx
628
;       jmp     .not_number
631
;       jmp     .not_number
629
;.bad:
632
;.bad:
630
.bad:
633
.bad:
631
        jmp     .next
634
        jmp     .next
632
.done:
635
.done:
633
 
636
 
634
        ret
637
        ret
635
 
638
 
636
;****************************************************************************
639
;****************************************************************************
637
;    Function
640
;    Function
638
;       open_socket
641
;       open_socket
639
;
642
;
640
;   Description
643
;   Description
641
;       opens the socket
644
;       opens the socket
642
;
645
;
643
;****************************************************************************
646
;****************************************************************************
644
open_socket:
647
open_socket:
645
 
648
 
646
        DEBUGF  1, "opening socket\n"
649
        DEBUGF  1, "opening socket\n"
647
 
650
 
648
        mov     edx, 80
651
        mov     edx, 80
649
        cmp     byte [proxyAddr], 0
652
        cmp     byte [proxyAddr], 0
650
        jz      @f
653
        jz      @f
651
        mov     eax, [proxyPort]
654
        mov     eax, [proxyPort]
652
        xchg    al, ah
655
        xchg    al, ah
653
        mov     [server_port], ax
656
        mov     [server_port], ax
654
    @@:
657
    @@:
655
 
658
 
656
        mcall   socket, AF_INET4, SOCK_STREAM, 0
659
        mcall   socket, AF_INET4, SOCK_STREAM, 0
657
        mov     [socketnum], eax
660
        mov     [socketnum], eax
658
        mcall   connect, [socketnum], sockaddr1, 18
661
        mcall   connect, [socketnum], sockaddr1, 18
659
 
662
 
660
        ret
663
        ret
661
 
664
 
662
 
665
 
663
;****************************************************************************
666
;****************************************************************************
664
;    Function
667
;    Function
665
;       parse_url
668
;       parse_url
666
;
669
;
667
;   Description
670
;   Description
668
;       parses the full url typed in by the user into a web address ( that
671
;       parses the full url typed in by the user into a web address ( that
669
;       can be turned into an IP address by DNS ) and the page to display
672
;       can be turned into an IP address by DNS ) and the page to display
670
;       DNS will be used to translate the web address into an IP address, if
673
;       DNS will be used to translate the web address into an IP address, if
671
;       needed.
674
;       needed.
672
;       url is at document_user and will be space terminated.
675
;       url is at document_user and will be space terminated.
673
;       web address goes to webAddr and is space terminated.
676
;       web address goes to webAddr and is space terminated.
674
;       ip address goes to server_ip
677
;       ip address goes to server_ip
675
;       page goes to document and is space terminated.
678
;       page goes to document and is space terminated.
676
;
679
;
677
;       Supported formats:
680
;       Supported formats:
678
;       address
681
;       address
679
;        is optional, removed and ignored - only http supported
682
;        is optional, removed and ignored - only http supported
680
;       
is required. It can be an ip address or web address
683
;       
is required. It can be an ip address or web address
681
;        is optional and must start with a leading / character
684
;        is optional and must start with a leading / character
682
;
685
;
683
;****************************************************************************
686
;****************************************************************************
684
parse_url:
687
parse_url:
685
; First, reset destination variables
688
; First, reset destination variables
686
        mov     al, ' '
689
        mov     al, ' '
687
        mov     edi, document
690
        mov     edi, document
688
        mov     ecx, URLMAXLEN
691
        mov     ecx, URLMAXLEN
689
        rep     stosb
692
        rep     stosb
690
        mov     edi, webAddr
693
        mov     edi, webAddr
691
        mov     ecx, URLMAXLEN
694
        mov     ecx, URLMAXLEN
692
        rep     stosb
695
        rep     stosb
693
 
696
 
694
        mov     al, '/'
697
        mov     al, '/'
695
        mov     [document], al
698
        mov     [document], al
696
 
699
 
697
        mov     esi, document_user
700
        mov     esi, document_user
698
; remove any leading protocol text
701
; remove any leading protocol text
699
        mov     ecx, URLMAXLEN
702
        mov     ecx, URLMAXLEN
700
        mov     ax, '//'
703
        mov     ax, '//'
701
 
704
 
702
pu_000:
705
pu_000:
703
        cmp     [esi], byte ' '         ; end of text?
706
        cmp     [esi], byte ' '         ; end of text?
704
        je      pu_002                  ; yep, so not found
707
        je      pu_002                  ; yep, so not found
705
        cmp     [esi], ax
708
        cmp     [esi], ax
706
        je      pu_001                  ; Found it, so esi+2 is start
709
        je      pu_001                  ; Found it, so esi+2 is start
707
        inc     esi
710
        inc     esi
708
        loop    pu_000
711
        loop    pu_000
709
 
712
 
710
pu_002:
713
pu_002:
711
; not found, so reset esi to start
714
; not found, so reset esi to start
712
        mov     esi, document_user-2
715
        mov     esi, document_user-2
713
 
716
 
714
pu_001:
717
pu_001:
715
        add     esi, 2
718
        add     esi, 2
716
        mov     ebx, esi ; save address of start of web address
719
        mov     ebx, esi ; save address of start of web address
717
        mov     edi, document_user + URLMAXLEN   ; end of string
720
        mov     edi, document_user + URLMAXLEN   ; end of string
718
; look for page delimiter - it's a '/' character
721
; look for page delimiter - it's a '/' character
719
pu_003:
722
pu_003:
720
        cmp     [esi], byte ' '  ; end of text?
723
        cmp     [esi], byte ' '  ; end of text?
721
        je      pu_004          ; yep, so none found
724
        je      pu_004          ; yep, so none found
722
        cmp     esi, edi         ; end of string?
725
        cmp     esi, edi         ; end of string?
723
        je      pu_004          ; yep, so none found
726
        je      pu_004          ; yep, so none found
724
        cmp     [esi], byte '/'  ; delimiter?
727
        cmp     [esi], byte '/'  ; delimiter?
725
        je      pu_005          ; yep - process it
728
        je      pu_005          ; yep - process it
726
        inc     esi
729
        inc     esi
727
        jmp     pu_003
730
        jmp     pu_003
728
 
731
 
729
pu_005:
732
pu_005:
730
; copy page to document address
733
; copy page to document address
731
; esi = delimiter
734
; esi = delimiter
732
        push    esi
735
        push    esi
733
        mov     ecx, edi         ; end of document_user
736
        mov     ecx, edi         ; end of document_user
734
        mov     edi, document
737
        mov     edi, document
735
 
738
 
736
pu_006:
739
pu_006:
737
        movsb
740
        movsb
738
        cmp     esi, ecx
741
        cmp     esi, ecx
739
        je      pu_007          ; end of string?
742
        je      pu_007          ; end of string?
740
        cmp     [esi], byte ' '  ; end of text
743
        cmp     [esi], byte ' '  ; end of text
741
;       je      pu_007          ; äçåí-àññåìáëåð
744
;       je      pu_007          ; äçåí-àññåìáëåð
742
;       jmp     pu_006          ; íå íàäî ïëîäèòü ñóùíîñòè ïî íàïðàñíó
745
;       jmp     pu_006          ; íå íàäî ïëîäèòü ñóùíîñòè ïî íàïðàñíó
743
        jne     pu_006
746
        jne     pu_006
744
 
747
 
745
pu_007:
748
pu_007:
746
        pop     esi     ; point esi to '/' delimiter
749
        pop     esi     ; point esi to '/' delimiter
747
 
750
 
748
pu_004:
751
pu_004:
749
; copy web address to webAddr
752
; copy web address to webAddr
750
; start in ebx, end in esi-1
753
; start in ebx, end in esi-1
751
        mov     ecx, esi
754
        mov     ecx, esi
752
        mov     esi, ebx
755
        mov     esi, ebx
753
        mov     edi, webAddr
756
        mov     edi, webAddr
754
  @@:
757
  @@:
755
        movsb
758
        movsb
756
        cmp     esi, ecx
759
        cmp     esi, ecx
757
        jne     @r
760
        jne     @r
758
        mov     byte [edi], 0
761
        mov     byte [edi], 0
759
 
762
 
760
pu_009:
763
pu_009:
761
; For debugging, display resulting strings
764
; For debugging, display resulting strings
762
        DEBUGF  2, "Downloadng %s\n", document_user
765
        DEBUGF  2, "Downloadng %s\n", document_user
763
 
766
 
764
; Look up the ip address, or was it specified?
767
; Look up the ip address, or was it specified?
765
        mov     al, [proxyAddr]
768
        mov     al, [proxyAddr]
766
        cmp     al, 0
769
        cmp     al, 0
767
        jnz     pu_015
770
        jnz     pu_015
768
        mov     al, [webAddr]
771
        mov     al, [webAddr]
769
pu_015:
772
pu_015:
770
        cmp     al, '0'
773
        cmp     al, '0'
771
        jb      pu_010  ; Resolve address
774
        jb      pu_010  ; Resolve address
772
        cmp     al, '9'
775
        cmp     al, '9'
773
        ja      pu_010  ; Resolve address
776
        ja      pu_010  ; Resolve address
774
 
777
 
775
        DEBUGF  1, "GotIP\n"
778
        DEBUGF  1, "GotIP\n"
776
 
779
 
777
; Convert address
780
; Convert address
778
; If proxy is given, get proxy address instead of server
781
; If proxy is given, get proxy address instead of server
779
        mov     esi, proxyAddr-1
782
        mov     esi, proxyAddr-1
780
        cmp     byte[esi+1], 0
783
        cmp     byte[esi+1], 0
781
        jne     pu_020
784
        jne     pu_020
782
        mov     esi, webAddr-1
785
        mov     esi, webAddr-1
783
 
786
 
784
pu_020:
787
pu_020:
785
        mov     edi, server_ip
788
        mov     edi, server_ip
786
        xor     eax, eax
789
        xor     eax, eax
787
 
790
 
788
ip1:
791
ip1:
789
        inc     esi
792
        inc     esi
790
        cmp     [esi], byte '0'
793
        cmp     [esi], byte '0'
791
        jb      ip2
794
        jb      ip2
792
        cmp     [esi], byte '9'
795
        cmp     [esi], byte '9'
793
        ja      ip2
796
        ja      ip2
794
        imul    eax, 10
797
        imul    eax, 10
795
        movzx   ebx, byte [esi]
798
        movzx   ebx, byte [esi]
796
        sub     ebx, 48
799
        sub     ebx, 48
797
        add     eax, ebx
800
        add     eax, ebx
798
        jmp     ip1
801
        jmp     ip1
799
 
802
 
800
ip2:
803
ip2:
801
        mov     [edi], al
804
        mov     [edi], al
802
        xor     eax, eax
805
        xor     eax, eax
803
        inc     edi
806
        inc     edi
804
        cmp     edi, server_ip+3
807
        cmp     edi, server_ip+3
805
        jbe     ip1
808
        jbe     ip1
806
        jmp     pu_011
809
        jmp     pu_011
807
 
810
 
808
pu_010:
811
pu_010:
809
        DEBUGF  1, "Resolving %s\n", webAddr
812
        DEBUGF  1, "Resolving %s\n", webAddr
810
 
813
 
811
; resolve name
814
; resolve name
812
        push    esp     ; reserve stack place
815
        push    esp     ; reserve stack place
813
        push    esp     ; fourth parameter
816
        push    esp     ; fourth parameter
814
        push    0       ; third parameter
817
        push    0       ; third parameter
815
        push    0       ; second parameter
818
        push    0       ; second parameter
816
        push    webAddr
819
        push    webAddr
817
        call    [getaddrinfo]
820
        call    [getaddrinfo]
818
        pop     esi
821
        pop     esi
819
; TODO: handle error
822
; TODO: handle error
820
;        test    eax, eax
823
;        test    eax, eax
821
;        jnz     .fail_dns
824
;        jnz     .fail_dns
822
 
825
 
823
; fill in ip
826
; fill in ip
824
        mov     eax, [esi + addrinfo.ai_addr]
827
        mov     eax, [esi + addrinfo.ai_addr]
825
        mov     eax, [eax + sockaddr_in.sin_addr]
828
        mov     eax, [eax + sockaddr_in.sin_addr]
826
        mov     [server_ip], eax
829
        mov     [server_ip], eax
827
 
830
 
828
        DEBUGF  1, "Resolved to %u.%u.%u.%u\n", [server_ip]:1, [server_ip + 1]:1, [server_ip + 2]:1, [server_ip + 3]:1
831
        DEBUGF  1, "Resolved to %u.%u.%u.%u\n", [server_ip]:1, [server_ip + 1]:1, [server_ip + 2]:1, [server_ip + 3]:1
829
 
832
 
830
pu_011:
833
pu_011:
831
 
834
 
832
        ret
835
        ret
833
 
836
 
834
;***************************************************************************
837
;***************************************************************************
835
;   Function
838
;   Function
836
;       load_settings
839
;       load_settings
837
;
840
;
838
;   Description
841
;   Description
839
;       Load settings from configuration file network.ini
842
;       Load settings from configuration file network.ini
840
;
843
;
841
;***************************************************************************
844
;***************************************************************************
842
load_settings:
845
load_settings:
843
 
846
 
844
        invoke  ini.get_str, inifile, sec_proxy, key_proxy, proxyAddr, 256, proxyAddr
847
        invoke  ini.get_str, inifile, sec_proxy, key_proxy, proxyAddr, 256, proxyAddr
845
        invoke  ini.get_int, inifile, sec_proxy, key_proxyport, 80
848
        invoke  ini.get_int, inifile, sec_proxy, key_proxyport, 80
846
        mov     [proxyPort], eax
849
        mov     [proxyPort], eax
847
        invoke  ini.get_str, inifile, sec_proxy, key_user, proxyUser, 256, proxyUser
850
        invoke  ini.get_str, inifile, sec_proxy, key_user, proxyUser, 256, proxyUser
848
        invoke  ini.get_str, inifile, sec_proxy, key_password, proxyPassword, 256, proxyPassword
851
        invoke  ini.get_str, inifile, sec_proxy, key_password, proxyPassword, 256, proxyPassword
849
 
852
 
850
        ret
853
        ret
851
 
854
 
852
;***************************************************************************
855
;***************************************************************************
853
;   Function
856
;   Function
854
;       append_proxy_auth_header
857
;       append_proxy_auth_header
855
;
858
;
856
;   Description
859
;   Description
857
;       Append header to HTTP request for proxy authentification
860
;       Append header to HTTP request for proxy authentification
858
;
861
;
859
;***************************************************************************
862
;***************************************************************************
860
append_proxy_auth_header:
863
append_proxy_auth_header:
861
        mov     esi, proxy_auth_basic
864
        mov     esi, proxy_auth_basic
862
        mov     ecx, proxy_auth_basic_end - proxy_auth_basic
865
        mov     ecx, proxy_auth_basic_end - proxy_auth_basic
863
        rep     movsb
866
        rep     movsb
864
; base64-encode string :
867
; base64-encode string :
865
        mov     esi, proxyUser
868
        mov     esi, proxyUser
866
 
869
 
867
apah000:
870
apah000:
868
        lodsb
871
        lodsb
869
        test    al, al
872
        test    al, al
870
        jz      apah001
873
        jz      apah001
871
        call    encode_base64_byte
874
        call    encode_base64_byte
872
        jmp     apah000
875
        jmp     apah000
873
 
876
 
874
apah001:
877
apah001:
875
        mov     al, ':'
878
        mov     al, ':'
876
        call    encode_base64_byte
879
        call    encode_base64_byte
877
        mov     esi, proxyPassword
880
        mov     esi, proxyPassword
878
 
881
 
879
apah002:
882
apah002:
880
        lodsb
883
        lodsb
881
        test    al, al
884
        test    al, al
882
        jz      apah003
885
        jz      apah003
883
        call    encode_base64_byte
886
        call    encode_base64_byte
884
        jmp     apah002
887
        jmp     apah002
885
 
888
 
886
apah003:
889
apah003:
887
        call    encode_base64_final
890
        call    encode_base64_final
888
        ret
891
        ret
889
 
892
 
890
encode_base64_byte:
893
encode_base64_byte:
891
        inc     ecx
894
        inc     ecx
892
        shl     edx, 8
895
        shl     edx, 8
893
        mov     dl, al
896
        mov     dl, al
894
        cmp     ecx, 3
897
        cmp     ecx, 3
895
        je      ebb001
898
        je      ebb001
896
        ret
899
        ret
897
 
900
 
898
ebb001:
901
ebb001:
899
        shl     edx, 8
902
        shl     edx, 8
900
        inc     ecx
903
        inc     ecx
901
 
904
 
902
ebb002:
905
ebb002:
903
        rol     edx, 6
906
        rol     edx, 6
904
        xor     eax, eax
907
        xor     eax, eax
905
        xchg    al, dl
908
        xchg    al, dl
906
        mov     al, [base64_table+eax]
909
        mov     al, [base64_table+eax]
907
        stosb
910
        stosb
908
        loop    ebb002
911
        loop    ebb002
909
        ret
912
        ret
910
 
913
 
911
encode_base64_final:
914
encode_base64_final:
912
        mov     al, 0
915
        mov     al, 0
913
        test    ecx, ecx
916
        test    ecx, ecx
914
        jz      ebf000
917
        jz      ebf000
915
        call    encode_base64_byte
918
        call    encode_base64_byte
916
        test    ecx, ecx
919
        test    ecx, ecx
917
        jz      ebf001
920
        jz      ebf001
918
        call    encode_base64_byte
921
        call    encode_base64_byte
919
        mov     byte [edi-2], '='
922
        mov     byte [edi-2], '='
920
 
923
 
921
ebf001:
924
ebf001:
922
        mov     byte [edi-1], '='
925
        mov     byte [edi-1], '='
923
 
926
 
924
ebf000:
927
ebf000:
925
        ret
928
        ret
926
 
929
 
927
;   *********************************************
930
;   *********************************************
928
;   *******  WINDOW DEFINITIONS AND DRAW ********
931
;   *******  WINDOW DEFINITIONS AND DRAW ********
929
;   *********************************************
932
;   *********************************************
930
 
933
 
931
draw_window:
934
draw_window:
932
 
935
 
933
        mcall   12, 1
936
        mcall   12, 1
934
 
937
 
935
        mcall   48, 3, sc, 40 ;get system colors
938
        mcall   48, 3, sc, 40 ;get system colors
936
 
939
 
937
        mov     edx, [sc.work]
940
        mov     edx, [sc.work]
938
        or      edx, 0x34000000
941
        or      edx, 0x34000000
939
        mcall   0, <50, 370>, <350, 140>, , 0, title   ;draw window
942
        mcall   0, <50, 370>, <350, 140>, , 0, title   ;draw window
940
        
943
        
941
        mov     ecx, [sc.work_text]
944
        mov     ecx, [sc.work_text]
942
        or      ecx, 80000000h
945
        or      ecx, 80000000h
943
        mcall   4, <14, 14>, , type_pls ;"URL:"
946
        mcall   4, <14, 14>, , type_pls ;"URL:"
944
 
947
 
945
        edit_boxes_set_sys_color edit1, editboxes_end, sc
948
        edit_boxes_set_sys_color edit1, editboxes_end, sc
946
        stdcall [edit_box_draw], edit1
949
        stdcall [edit_box_draw], edit1
947
 
950
 
948
; RELOAD
951
; RELOAD
949
        mcall   8, <90, 68>, <54, 16>, 22, [sc.work_button]
952
        mcall   8, <90, 68>, <54, 16>, 22, [sc.work_button]
950
; STOP
953
; STOP
951
        mcall   , <166, 50>, <54, 16>, 24
954
        mcall   , <166, 50>, <54, 16>, 24
952
; SAVE
955
; SAVE
953
        mcall   , <224, 54>, , 26
956
        mcall   , <224, 54>, , 26
954
; BUTTON TEXT
957
; BUTTON TEXT
955
        mov     ecx, [sc.work_button_text]
958
        mov     ecx, [sc.work_button_text]
956
        or      ecx, 80000000h
959
        or      ecx, 80000000h
957
        mcall   4, <102, 59>, , button_text
960
        mcall   4, <102, 59>, , button_text
958
 
961
 
959
        mcall   12, 2 ; end window redraw
962
        mcall   12, 2 ; end window redraw
960
        ret
963
        ret
961
 
964
 
962
 
965
 
963
;-----------------------------------------------------------------------------
966
;-----------------------------------------------------------------------------
964
; Data area
967
; Data area
965
;-----------------------------------------------------------------------------
968
;-----------------------------------------------------------------------------
966
align   4
969
align   4
967
@IMPORT:
970
@IMPORT:
968
 
971
 
969
library libini, 'libini.obj', \
972
library libini, 'libini.obj', \
970
        box_lib, 'box_lib.obj', \
973
        box_lib, 'box_lib.obj', \
971
        network, 'network.obj'
974
        network, 'network.obj'
972
 
975
 
973
import  libini, \
976
import  libini, \
974
        ini.get_str, 'ini_get_str', \
977
        ini.get_str, 'ini_get_str', \
975
        ini.get_int, 'ini_get_int'
978
        ini.get_int, 'ini_get_int'
976
 
979
 
977
import  box_lib, \
980
import  box_lib, \
978
        edit_box_draw, 'edit_box', \
981
        edit_box_draw, 'edit_box', \
979
        edit_box_key, 'edit_box_key', \
982
        edit_box_key, 'edit_box_key', \
980
        edit_box_mouse, 'edit_box_mouse'
983
        edit_box_mouse, 'edit_box_mouse'
981
 
984
 
982
import  network,\
985
import  network,\
983
        getaddrinfo,    'getaddrinfo',\
986
        getaddrinfo,    'getaddrinfo',\
984
        freeaddrinfo,   'freeaddrinfo',\
987
        freeaddrinfo,   'freeaddrinfo',\
985
        inet_ntoa,      'inet_ntoa'
988
        inet_ntoa,      'inet_ntoa'
986
 
989
 
987
;---------------------------------------------------------------------
990
;---------------------------------------------------------------------
988
fileinfo        dd 2, 0, 0
991
fileinfo        dd 2, 0, 0
989
final_size      dd 0
992
final_size      dd 0
990
final_buffer    dd 0
993
final_buffer    dd 0
991
                db '/rd/1/.download', 0
994
                db '/rd/1/.download', 0
992
        
995
        
993
body_pos        dd 0
996
body_pos        dd 0
994
buf_size        dd 0
997
buf_size        dd 0
995
buf_ptr         dd 0
998
buf_ptr         dd 0
996
 
999
 
997
deba            dd 0
1000
deba            dd 0
998
                db 0
1001
                db 0
999
 
1002
 
1000
;---------------------------------------------------------------------
1003
;---------------------------------------------------------------------
1001
 
1004
 
1002
mouse_dd        dd 0
1005
mouse_dd        dd 0
1003
edit1           edit_box 295, 48, 10, 0xffffff, 0xff, 0x80ff, 0, 0x8000, URLMAXLEN, document_user, mouse_dd, ed_focus+ed_always_focus, 7, 7
1006
edit1           edit_box 295, 48, 10, 0xffffff, 0xff, 0x80ff, 0, 0x8000, URLMAXLEN, document_user, mouse_dd, ed_focus+ed_always_focus, 7, 7
1004
editboxes_end:
1007
editboxes_end:
1005
 
1008
 
1006
;---------------------------------------------------------------------
1009
;---------------------------------------------------------------------
1007
 
1010
 
1008
include_debug_strings
1011
include_debug_strings
1009
 
1012
 
1010
;---------------------------------------------------------------------
1013
;---------------------------------------------------------------------
1011
 
1014
 
1012
type_pls        db 'URL:', 0
1015
type_pls        db 'URL:', 0
1013
button_text     db 'DOWNLOAD     STOP     RESAVE', 0
1016
button_text     db 'DOWNLOAD     STOP     RESAVE', 0
1014
download_complete db 'File saved as /rd/1/.download', 0
1017
download_complete db 'File saved as /rd/1/.download', 0
1015
title           db 'HTTP Downloader', 0
1018
title           db 'HTTP Downloader', 0
1016
 
1019
 
1017
;---------------------------------------------------------------------
1020
;---------------------------------------------------------------------
1018
s_contentlength db 'Content-Length:'
1021
s_contentlength db 'Content-Length:'
1019
len_contentlength = 15
1022
len_contentlength = 15
1020
 
1023
 
1021
s_chunked       db 'Transfer-Encoding: chunked'
1024
s_chunked       db 'Transfer-Encoding: chunked'
1022
len_chunked     = $ - s_chunked
1025
len_chunked     = $ - s_chunked
1023
 
1026
 
1024
string0:        db 'GET '
1027
string0:        db 'GET '
1025
 
1028
 
1026
stringh                 db ' HTTP/1.1', 13, 10, 'Host: '
1029
stringh                 db ' HTTP/1.1', 13, 10, 'Host: '
1027
stringh_end:
1030
stringh_end:
1028
proxy_auth_basic        db 13, 10, 'Proxy-Authorization: Basic '
1031
proxy_auth_basic        db 13, 10, 'Proxy-Authorization: Basic '
1029
proxy_auth_basic_end:
1032
proxy_auth_basic_end:
1030
connclose               db 13, 10, 'User-Agent: Kolibrios Downloader', 13, 10, 'Connection: Close', 13, 10, 13, 10
1033
connclose               db 13, 10, 'User-Agent: Kolibrios Downloader', 13, 10, 'Connection: Close', 13, 10, 13, 10
1031
connclose_end:
1034
connclose_end:
1032
 
1035
 
1033
base64_table    db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
1036
base64_table    db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
1034
                db '0123456789+/'
1037
                db '0123456789+/'
1035
 
1038
 
1036
inifile         db '/sys/network.ini', 0
1039
inifile         db '/sys/network.ini', 0
1037
 
1040
 
1038
sec_proxy:
1041
sec_proxy:
1039
key_proxy       db 'proxy', 0
1042
key_proxy       db 'proxy', 0
1040
key_proxyport   db 'port', 0
1043
key_proxyport   db 'port', 0
1041
key_user        db 'user', 0
1044
key_user        db 'user', 0
1042
key_password    db 'password', 0
1045
key_password    db 'password', 0
1043
 
1046
 
1044
sockaddr1:
1047
sockaddr1:
1045
                dw AF_INET4
1048
                dw AF_INET4
1046
server_port     dw 0x5000       ; 80
1049
server_port     dw 0x5000       ; 80
1047
server_ip       dd 0
1050
server_ip       dd 0
1048
                rb 10
1051
                rb 10
1049
 
1052
 
1050
proxyPort       dd 80
1053
proxyPort       dd 80
1051
 
1054
 
1052
shared_name     dd 0
1055
shared_name     dd 0
1053
 
1056
 
1054
;---------------------------------------------------------------------
1057
;---------------------------------------------------------------------
1055
document_user   db 'http://', 0
1058
document_user   db 'http://', 0
1056
;---------------------------------------------------------------------
1059
;---------------------------------------------------------------------
1057
IM_END:
1060
IM_END:
1058
;---------------------------------------------------------------------
1061
;---------------------------------------------------------------------
1059
                rb URLMAXLEN-(IM_END - document_user)
1062
                rb URLMAXLEN-(IM_END - document_user)
1060
;---------------------------------------------------------------------
1063
;---------------------------------------------------------------------
1061
                sc system_colors
1064
                sc system_colors
1062
;---------------------------------------------------------------------
1065
;---------------------------------------------------------------------
1063
align 4
1066
align 4
1064
document        rb URLMAXLEN
1067
document        rb URLMAXLEN
1065
;---------------------------------------------------------------------
1068
;---------------------------------------------------------------------
1066
align 4
1069
align 4
1067
webAddr         rb URLMAXLEN+1
1070
webAddr         rb URLMAXLEN+1
1068
;---------------------------------------------------------------------
1071
;---------------------------------------------------------------------
1069
pos             dd ?
1072
pos             dd ?
1070
socketnum       dd ?
1073
socketnum       dd ?
1071
is_chunked      dd ?
1074
is_chunked      dd ?
1072
prev_chunk_end  dd ?
1075
prev_chunk_end  dd ?
1073
cur_chunk_size  dd ?
1076
cur_chunk_size  dd ?
1074
;---------------------------------------------------------------------
1077
;---------------------------------------------------------------------
1075
 
1078
 
1076
params          rb 1024
1079
params          rb 1024
1077
 
1080
 
1078
request         rb 256
1081
request         rb 256
1079
 
1082
 
1080
proxyAddr       rb 256
1083
proxyAddr       rb 256
1081
proxyUser       rb 256
1084
proxyUser       rb 256
1082
proxyPassword   rb 256
1085
proxyPassword   rb 256
1083
 
1086
 
1084
I_END:
1087
I_END: