Subversion Repositories Kolibri OS

Rev

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

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