Subversion Repositories Kolibri OS

Rev

Rev 4222 | Rev 4241 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4222 Rev 4233
Line 4... Line 4...
4
;; Distributed under terms of the GNU General Public License       ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
5
;;                                                                 ;;
6
;;  HTTP library for KolibriOS                                     ;;
6
;;  HTTP library for KolibriOS                                     ;;
7
;;                                                                 ;;
7
;;                                                                 ;;
8
;;   Written by hidnplayr@kolibrios.org                            ;;
8
;;   Written by hidnplayr@kolibrios.org                            ;;
-
 
9
;;   Proxy code written by CleverMouse                             ;;
9
;;                                                                 ;;
10
;;                                                                 ;;
10
;;         GNU GENERAL PUBLIC LICENSE                              ;;
11
;;         GNU GENERAL PUBLIC LICENSE                              ;;
11
;;          Version 2, June 1991                                   ;;
12
;;          Version 2, June 1991                                   ;;
12
;;                                                                 ;;
13
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 150... Line 151...
150
        jz      .error
151
        jz      .error
151
        mov     [hostname], eax
152
        mov     [hostname], eax
152
        mov     [pageaddr], ebx
153
        mov     [pageaddr], ebx
153
        mov     [port], ecx
154
        mov     [port], ecx
Line 154... Line -...
154
 
-
 
155
; Do we need to use a proxy?
-
 
156
        cmp     [proxyAddr], 0
-
 
157
        jne     .proxy_done
-
 
158
 
-
 
159
  .proxy_done:
-
 
160
 
-
 
161
;;;;
-
 
162
 
155
 
163
; Connect to the other side.
156
; Connect to the other side.
164
        stdcall open_connection, [hostname], [port]
157
        stdcall open_connection, [hostname], [port]
165
        test    eax, eax
158
        test    eax, eax
166
        jz      .error
159
        jz      .error
Line 175... Line 168...
175
        DEBUGF  1, "Buffer has been allocated.\n"
168
        DEBUGF  1, "Buffer has been allocated.\n"
Line 176... Line 169...
176
 
169
 
177
        mov     esi, str_get
170
        mov     esi, str_get
Line -... Line 171...
-
 
171
        copy_till_zero
-
 
172
 
-
 
173
; If we are using a proxy, send complete URL, otherwise send only page address.
-
 
174
        cmp     [proxyAddr], 0
-
 
175
        je      .no_proxy
-
 
176
        mov     esi, str_http           ; prepend 'http://'
-
 
177
        copy_till_zero
-
 
178
        mov     esi, [hostname]
178
        copy_till_zero
179
        copy_till_zero
179
 
180
  .no_proxy:
Line 180... Line 181...
180
        mov     esi, [pageaddr]
181
        mov     esi, [pageaddr]
181
        copy_till_zero
182
        copy_till_zero
Line 191... Line 192...
191
        test    esi, esi
192
        test    esi, esi
192
        jz      @f
193
        jz      @f
193
        copy_till_zero
194
        copy_till_zero
194
  @@:
195
  @@:
Line -... Line 196...
-
 
196
 
-
 
197
        cmp     byte[proxyUser], 0
-
 
198
        je      @f
-
 
199
        call    append_proxy_auth_header
-
 
200
  @@:
195
 
201
 
196
        mov     esi, str_close
202
        mov     esi, str_close
197
        mov     ecx, str_close.length
203
        mov     ecx, str_close.length
Line 198... Line 204...
198
        rep     movsb
204
        rep     movsb
Line 256... Line 262...
256
        jz      .error
262
        jz      .error
257
        mov     [hostname], eax
263
        mov     [hostname], eax
258
        mov     [pageaddr], ebx
264
        mov     [pageaddr], ebx
259
        mov     [port], ecx
265
        mov     [port], ecx
Line 260... Line -...
260
 
-
 
261
; Do we need to use a proxy?
-
 
262
        cmp     [proxyAddr], 0
-
 
263
        jne     .proxy_done
-
 
264
 
-
 
265
        ; TODO: set hostname to that of the
-
 
266
  .proxy_done:
-
 
267
 
-
 
268
;;;;
-
 
269
 
266
 
270
; Connect to the other side.
267
; Connect to the other side.
271
        stdcall open_connection, [hostname], [port]
268
        stdcall open_connection, [hostname], [port]
272
        test    eax, eax
269
        test    eax, eax
273
        jz      .error
270
        jz      .error
Line 282... Line 279...
282
        DEBUGF  1, "Buffer has been allocated.\n"
279
        DEBUGF  1, "Buffer has been allocated.\n"
Line 283... Line 280...
283
 
280
 
284
        mov     esi, str_head
281
        mov     esi, str_head
Line -... Line 282...
-
 
282
        copy_till_zero
-
 
283
 
-
 
284
; If we are using a proxy, send complete URL, otherwise send only page address.
-
 
285
        cmp     [proxyAddr], 0
-
 
286
        je      .no_proxy
-
 
287
        mov     esi, str_http           ; prepend 'http://'
-
 
288
        copy_till_zero
-
 
289
        mov     esi, [hostname]
285
        copy_till_zero
290
        copy_till_zero
286
 
291
  .no_proxy:
Line 287... Line 292...
287
        mov     esi, [pageaddr]
292
        mov     esi, [pageaddr]
288
        copy_till_zero
293
        copy_till_zero
Line 298... Line 303...
298
        test    esi, esi
303
        test    esi, esi
299
        jz      @f
304
        jz      @f
300
        copy_till_zero
305
        copy_till_zero
301
  @@:
306
  @@:
Line -... Line 307...
-
 
307
 
-
 
308
        cmp     byte[proxyUser], 0
-
 
309
        je      @f
-
 
310
        call    append_proxy_auth_header
-
 
311
  @@:
302
 
312
 
303
        mov     esi, str_close
313
        mov     esi, str_close
304
        mov     ecx, str_close.length
314
        mov     ecx, str_close.length
Line 305... Line 315...
305
        rep     movsb
315
        rep     movsb
Line 365... Line 375...
365
        jz      .error
375
        jz      .error
366
        mov     [hostname], eax
376
        mov     [hostname], eax
367
        mov     [pageaddr], ebx
377
        mov     [pageaddr], ebx
368
        mov     [port], ecx
378
        mov     [port], ecx
Line 369... Line -...
369
 
-
 
370
; Do we need to use a proxy?
-
 
371
        cmp     [proxyAddr], 0
-
 
372
        jne     .proxy_done
-
 
373
 
-
 
374
        ; TODO: set hostname to that of the
-
 
375
  .proxy_done:
-
 
376
 
-
 
377
;;;;
-
 
378
 
379
 
379
; Connect to the other side.
380
; Connect to the other side.
380
        stdcall open_connection, [hostname], [port]
381
        stdcall open_connection, [hostname], [port]
381
        test    eax, eax
382
        test    eax, eax
382
        jz      .error
383
        jz      .error
Line 391... Line 392...
391
        DEBUGF  1, "Buffer has been allocated.\n"
392
        DEBUGF  1, "Buffer has been allocated.\n"
Line 392... Line 393...
392
 
393
 
393
        mov     esi, str_post
394
        mov     esi, str_post
Line -... Line 395...
-
 
395
        copy_till_zero
-
 
396
 
-
 
397
; If we are using a proxy, send complete URL, otherwise send only page address.
-
 
398
        cmp     [proxyAddr], 0
-
 
399
        je      .no_proxy
-
 
400
        mov     esi, str_http           ; prepend 'http://'
-
 
401
        copy_till_zero
-
 
402
        mov     esi, [hostname]
394
        copy_till_zero
403
        copy_till_zero
395
 
404
  .no_proxy:
Line 396... Line 405...
396
        mov     esi, [pageaddr]
405
        mov     esi, [pageaddr]
397
        copy_till_zero
406
        copy_till_zero
Line 406... Line 415...
406
        mov     esi, str_post_cl
415
        mov     esi, str_post_cl
407
        mov     ecx, str_post_cl.length
416
        mov     ecx, str_post_cl.length
408
        rep     movsb
417
        rep     movsb
Line 409... Line 418...
409
 
418
 
410
        mov     eax, [content_length]
419
        mov     eax, [content_length]
Line 411... Line 420...
411
        call    ascii_dec
420
        call    eax_ascii_dec
412
 
421
 
413
        mov     esi, str_post_ct
422
        mov     esi, str_post_ct
Line 421... Line 430...
421
        test    esi, esi
430
        test    esi, esi
422
        jz      @f
431
        jz      @f
423
        copy_till_zero
432
        copy_till_zero
424
  @@:
433
  @@:
Line -... Line 434...
-
 
434
 
-
 
435
        cmp     byte[proxyUser], 0
-
 
436
        je      @f
-
 
437
        call    append_proxy_auth_header
-
 
438
  @@:
425
 
439
 
426
        mov     esi, str_close
440
        mov     esi, str_close
427
        mov     ecx, str_close.length
441
        mov     ecx, str_close.length
Line 428... Line 442...
428
        rep     movsb
442
        rep     movsb
Line 1145... Line 1159...
1145
locals
1159
locals
1146
        sockaddr        dd ?
1160
        sockaddr        dd ?
1147
        socketnum       dd ?
1161
        socketnum       dd ?
1148
endl
1162
endl
Line -... Line 1163...
-
 
1163
 
-
 
1164
        cmp     [proxyAddr], 0
-
 
1165
        je      .no_proxy
-
 
1166
 
-
 
1167
        mov     [hostname], proxyAddr
-
 
1168
 
-
 
1169
        push    [proxyPort]
-
 
1170
        pop     [port]
-
 
1171
  .no_proxy:
1149
 
1172
 
1150
; Resolve the hostname
1173
; Resolve the hostname
1151
        DEBUGF  1, "Resolving hostname\n"
1174
        DEBUGF  1, "Resolving hostname\n"
1152
        push    esp     ; reserve stack place
1175
        push    esp     ; reserve stack place
1153
        push    esp     ; fourth parameter
1176
        push    esp     ; fourth parameter
Line 1214... Line 1237...
1214
;;------------------------------------------------------------------------------------------------;;
1237
;;------------------------------------------------------------------------------------------------;;
1215
;> URL = ptr to ASCIIZ URL                                                                        ;;
1238
;> URL = ptr to ASCIIZ URL                                                                        ;;
1216
;;------------------------------------------------------------------------------------------------;;
1239
;;------------------------------------------------------------------------------------------------;;
1217
;< eax = 0 (error) / ptr to ASCIIZ hostname                                                       ;;
1240
;< eax = 0 (error) / ptr to ASCIIZ hostname                                                       ;;
1218
;< ebx = ptr to ASCIIZ pageaddr                                                                   ;;
1241
;< ebx = ptr to ASCIIZ pageaddr                                                                   ;;
-
 
1242
;< ecx = port number                                                                              ;;
1219
;;================================================================================================;;
1243
;;================================================================================================;;
Line 1220... Line 1244...
1220
 
1244
 
1221
locals
1245
locals
1222
        urlsize         dd ?
1246
        urlsize         dd ?
1223
        hostname        dd ?
1247
        hostname        dd ?
-
 
1248
        pageaddr        dd ?
1224
        pageaddr        dd ?
1249
        port            dd ?
Line 1225... Line 1250...
1225
endl
1250
endl
Line 1226... Line 1251...
1226
 
1251
 
Line 1236... Line 1261...
1236
        cmp     [esi], ax
1261
        cmp     [esi], ax
1237
        je      .skip_proto
1262
        je      .skip_proto
1238
        inc     esi
1263
        inc     esi
1239
        dec     ecx
1264
        dec     ecx
1240
        jnz     .loop1
1265
        jnz     .loop1
1241
 
-
 
1242
        DEBUGF  1, "Invalid URL\n"
-
 
1243
        xor     eax, eax
1266
        jmp     .invalid
1244
        ret
-
 
Line 1245... Line 1267...
1245
 
1267
 
1246
  .skip_proto:
1268
  .skip_proto:
1247
        inc     esi                     ; skip the two '/'
1269
        inc     esi                     ; skip the two '/'
1248
        inc     esi
1270
        inc     esi
Line 1264... Line 1286...
1264
        inc     ecx                     ;
1286
        inc     ecx                     ;
1265
  @@:
1287
  @@:
1266
        push    ecx edi                 ; remember the pointer and length of pageaddr
1288
        push    ecx edi                 ; remember the pointer and length of pageaddr
Line 1267... Line 1289...
1267
 
1289
 
1268
 
1290
 
1269
; Create new buffer and put hostname in it
1291
; Create new buffer and put hostname in it.
1270
        mov     ecx, edi
1292
        mov     ecx, edi
1271
        sub     ecx, [URL]
1293
        sub     ecx, [URL]
1272
        inc     ecx                     ; we will add a 0 byte at the end
1294
        inc     ecx                     ; we will add a 0 byte at the end
Line 1280... Line 1302...
1280
        dec     ecx
1302
        dec     ecx
1281
        rep     movsb
1303
        rep     movsb
1282
        xor     al, al
1304
        xor     al, al
1283
        stosb
1305
        stosb
Line -... Line 1306...
-
 
1306
 
-
 
1307
; Check if user provided a port, and convert it if so.
-
 
1308
        mov     esi, [hostname]
-
 
1309
        mov     [port], 80              ; default port if user didnt provide one
-
 
1310
  .portloop:
-
 
1311
        lodsb
-
 
1312
        test    al, al
-
 
1313
        jz      .no_port
-
 
1314
        cmp     al, ':'
-
 
1315
        jne     .portloop
-
 
1316
 
-
 
1317
        push    esi
-
 
1318
        call    ascii_dec_ebx
-
 
1319
        pop     edi
-
 
1320
        cmp     byte[esi-1], 0
-
 
1321
        jne     .invalid
-
 
1322
        cmp     [proxyAddr], 0          ; remove port number from hostname
-
 
1323
        jne     @f                      ; unless when we are using proxy
-
 
1324
        mov     byte[edi-1], 0
-
 
1325
  @@:
-
 
1326
        test    ebx, ebx
-
 
1327
        je      .invalid
-
 
1328
        cmp     ebx, 0xffff
-
 
1329
        ja      .invalid
-
 
1330
        mov     [port], ebx
-
 
1331
  .no_port:
-
 
1332
 
-
 
1333
 
1284
 
1334
; Did user provide a pageaddr?
1285
        mov     [pageaddr], str_slash   ; assume there is no pageaddr
1335
        mov     [pageaddr], str_slash   ; assume there is no pageaddr
1286
        pop     esi ecx
1336
        pop     esi ecx
1287
        test    ecx, ecx
1337
        test    ecx, ecx
-
 
1338
        jz      .no_page
-
 
1339
 
1288
        jz      .no_page
1340
; Create new buffer and put pageaddr into it.
1289
        inc     ecx                     ; we will add a 0 byte at the end
1341
        inc     ecx                     ; we will add a 0 byte at the end
1290
        invoke  mem.alloc, ecx
1342
        invoke  mem.alloc, ecx
1291
        or      eax, eax
1343
        or      eax, eax
Line 1295... Line 1347...
1295
        mov     edi, eax
1347
        mov     edi, eax
1296
        dec     ecx
1348
        dec     ecx
1297
        rep     movsb
1349
        rep     movsb
1298
        xor     al, al
1350
        xor     al, al
1299
        stosb
1351
        stosb
1300
  .no_page:
-
 
Line -... Line 1352...
-
 
1352
 
1301
 
1353
  .no_page:
1302
        mov     eax, [hostname]
1354
        mov     eax, [hostname]
1303
        mov     ebx, [pageaddr]
1355
        mov     ebx, [pageaddr]
Line 1304... Line 1356...
1304
        mov     ecx, 80                 ;;;; FIXME
1356
        mov     ecx, [port]
1305
 
1357
 
1306
        DEBUGF  1, "hostname: %s\n", eax
1358
        DEBUGF  1, "hostname: %s\n", eax
Line 1307... Line 1359...
1307
        DEBUGF  1, "pageaddr: %s\n", ebx
1359
        DEBUGF  1, "pageaddr: %s\n", ebx
Line 1308... Line 1360...
1308
        DEBUGF  1, "port: %u\n", ecx
1360
        DEBUGF  1, "port: %u\n", ecx
-
 
1361
 
-
 
1362
        ret
-
 
1363
 
-
 
1364
  .no_mem:
-
 
1365
        DEBUGF  1, "Out of memory!\n"
-
 
1366
        xor     eax, eax
1309
 
1367
        ret
1310
        ret
1368
 
Line 1311... Line 1369...
1311
 
1369
  .invalid:
Line -... Line 1370...
-
 
1370
        DEBUGF  1, "Invalid URL!\n"
-
 
1371
        xor     eax, eax
-
 
1372
        ret
1312
  .no_mem:
1373
 
-
 
1374
endp
-
 
1375
 
-
 
1376
 
-
 
1377
 
-
 
1378
 
-
 
1379
 
-
 
1380
;;================================================================================================;;
-
 
1381
proc append_proxy_auth_header ;///////////////////////////////////////////////////////////////////;;
-
 
1382
;;------------------------------------------------------------------------------------------------;;
-
 
1383
;? Appends the proxy authentication header                                                        ;;
-
 
1384
;;------------------------------------------------------------------------------------------------;;
-
 
1385
;> /                                                                                              ;;
-
 
1386
;;------------------------------------------------------------------------------------------------;;
-
 
1387
;< /                                                                                              ;;
-
 
1388
;;================================================================================================;;
-
 
1389
        mov     esi, str_proxy_auth
-
 
1390
        mov     ecx, str_proxy_auth.length
-
 
1391
        rep     movsb
-
 
1392
; base64-encode string :
-
 
1393
        mov     esi, proxyUser
-
 
1394
 
-
 
1395
apah000:
-
 
1396
        lodsb
-
 
1397
        test    al, al
-
 
1398
        jz      apah001
-
 
1399
        call    encode_base64_byte
-
 
1400
        jmp     apah000
-
 
1401
 
-
 
1402
apah001:
-
 
1403
        mov     al, ':'
-
 
1404
        call    encode_base64_byte
-
 
1405
        mov     esi, proxyPassword
-
 
1406
 
-
 
1407
apah002:
-
 
1408
        lodsb
-
 
1409
        test    al, al
-
 
1410
        jz      apah003
-
 
1411
        call    encode_base64_byte
-
 
1412
        jmp     apah002
-
 
1413
 
-
 
1414
apah003:
-
 
1415
        call    encode_base64_final
-
 
1416
        ret
-
 
1417
 
-
 
1418
encode_base64_byte:
-
 
1419
        inc     ecx
-
 
1420
        shl     edx, 8
-
 
1421
        mov     dl, al
-
 
1422
        cmp     ecx, 3
-
 
1423
        je      ebb001
-
 
1424
        ret
-
 
1425
 
-
 
1426
ebb001:
-
 
1427
        shl     edx, 8
-
 
1428
        inc     ecx
-
 
1429
 
-
 
1430
ebb002:
-
 
1431
        rol     edx, 6
-
 
1432
        xor     eax, eax
-
 
1433
        xchg    al, dl
-
 
1434
        mov     al, [base64_table+eax]
-
 
1435
        stosb
-
 
1436
        loop    ebb002
-
 
1437
        ret
-
 
1438
 
-
 
1439
encode_base64_final:
-
 
1440
        mov     al, 0
-
 
1441
        test    ecx, ecx
-
 
1442
        jz      ebf000
-
 
1443
        call    encode_base64_byte
-
 
1444
        test    ecx, ecx
-
 
1445
        jz      ebf001
-
 
1446
        call    encode_base64_byte
-
 
1447
        mov     byte [edi-2], '='
-
 
1448
 
-
 
1449
ebf001:
-
 
1450
        mov     byte [edi-1], '='
-
 
1451
 
1313
        xor     eax, eax
1452
ebf000:
1314
        ret
1453
        ret
1315
 
1454
 
1316
endp
1455
endp
1317
 
1456
 
1318
 
1457
 
Line 1349... Line 1488...
1349
 
1488
 
Line 1350... Line 1489...
1350
endp
1489
endp
-
 
1490
 
-
 
1491
 
-
 
1492
;;================================================================================================;;
-
 
1493
proc ascii_dec_ebx ;//////////////////////////////////////////////////////////////////////////////;;
-
 
1494
;;------------------------------------------------------------------------------------------------;;
-
 
1495
;? Convert ASCII decimal number to ebx                                                            ;;
-
 
1496
;;------------------------------------------------------------------------------------------------;;
-
 
1497
;> esi = ptr where to read ASCII decimal number                                                   ;;
-
 
1498
;;------------------------------------------------------------------------------------------------;;
-
 
1499
;> ebx = number                                                                                   ;;
-
 
1500
;;================================================================================================;;
-
 
1501
 
-
 
1502
        xor     eax, eax
-
 
1503
        xor     ebx, ebx
-
 
1504
  .loop:
-
 
1505
        lodsb
-
 
1506
        sub     al, '0'
-
 
1507
        jb      .done
-
 
1508
        cmp     al, 9
-
 
1509
        ja      .done
-
 
1510
        lea     ebx, [ebx + 4*ebx]
-
 
1511
        shl     ebx, 1
-
 
1512
        add     ebx, eax
-
 
1513
        jmp     .loop
-
 
1514
  .done:
-
 
1515
 
-
 
1516
        ret
-
 
1517
 
-
 
1518
endp
1351
 
1519
 
1352
 
1520
 
1353
;;================================================================================================;;
1521
;;================================================================================================;;
1354
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
1522
;;////////////////////////////////////////////////////////////////////////////////////////////////;;
1355
;;================================================================================================;;
1523
;;================================================================================================;;
Line 1425... Line 1593...
1425
str_close       db 13, 10, 'User-Agent: KolibriOS libHTTP/1.0', 13, 10, 'Connection: Close', 13, 10, 13, 10
1593
str_close       db 13, 10, 'User-Agent: KolibriOS libHTTP/1.0', 13, 10, 'Connection: Close', 13, 10, 13, 10
1426
  .length       = $ - str_close
1594
  .length       = $ - str_close
1427
str_proxy_auth  db 13, 10, 'Proxy-Authorization: Basic '
1595
str_proxy_auth  db 13, 10, 'Proxy-Authorization: Basic '
1428
  .length       = $ - str_proxy_auth
1596
  .length       = $ - str_proxy_auth
Line -... Line 1597...
-
 
1597
 
-
 
1598
str_http        db 'http://', 0
1429
 
1599
 
1430
base64_table    db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
1600
base64_table    db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
Line 1431... Line 1601...
1431
                db '0123456789+/'
1601
                db '0123456789+/'
1432
 
1602