Subversion Repositories Kolibri OS

Rev

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

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