Subversion Repositories Kolibri OS

Rev

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

Rev 4161 Rev 4162
Line 234... Line 234...
234
;;------------------------------------------------------------------------------------------------;;
234
;;------------------------------------------------------------------------------------------------;;
235
;< eax = -1 (not finished) / 0 finished                                                           ;;
235
;< eax = -1 (not finished) / 0 finished                                                           ;;
236
;;================================================================================================;;
236
;;================================================================================================;;
237
        pusha
237
        pusha
238
        mov     ebp, [identifier]
238
        mov     ebp, [identifier]
-
 
239
 
-
 
240
; Receive some data
239
        mcall   recv, [ebp + http_msg.socket], [ebp + http_msg.write_ptr], \
241
        mcall   recv, [ebp + http_msg.socket], [ebp + http_msg.write_ptr], \
240
                      [ebp + http_msg.buffer_length], MSG_DONTWAIT
242
                      [ebp + http_msg.buffer_length], MSG_DONTWAIT
241
        cmp     eax, 0xffffffff
243
        cmp     eax, 0xffffffff
242
        je      .check_socket
244
        je      .check_socket
243
        DEBUGF  1, "Received %u bytes\n", eax
245
        DEBUGF  1, "Received %u bytes\n", eax
Line -... Line 246...
-
 
246
 
244
 
247
; Update pointers
245
        mov     edi, [ebp + http_msg.write_ptr]
248
        mov     edi, [ebp + http_msg.write_ptr]
246
        add     [ebp + http_msg.write_ptr], eax
249
        add     [ebp + http_msg.write_ptr], eax
247
        sub     [ebp + http_msg.buffer_length], eax
250
        sub     [ebp + http_msg.buffer_length], eax
-
 
251
        jz      .got_all_data
-
 
252
 
-
 
253
; If data is chunked, combine chunks into contiguous data.
-
 
254
        test    [ebp + http_msg.flags], FLAG_CHUNKED
-
 
255
        jnz     .chunk_loop
-
 
256
 
248
        jz      .got_all_data
257
; Did we detect the header yet?
249
        test    [ebp + http_msg.flags], FLAG_GOT_HEADER
258
        test    [ebp + http_msg.flags], FLAG_GOT_HEADER
Line -... Line 259...
-
 
259
        jnz     .header_parsed
250
        jnz     .header_parsed
260
 
251
 
261
; We havent found the header yet, search for it..
252
        sub     eax, 4
262
        sub     eax, 4
253
        jl      .no_header
263
        jl      .need_more_data
254
  .scan:
264
  .scan:
255
        ; scan for end of header (empty line)
265
        ; scan for end of header (empty line)
256
        cmp     dword[edi], 0x0a0d0a0d                  ; end of header
266
        cmp     dword[edi], 0x0a0d0a0d                  ; end of header
257
        je      .end_of_header
267
        je      .end_of_header
258
        cmp     word[edi+2], 0x0a0a
268
        cmp     word[edi+2], 0x0a0a
259
        je      .end_of_header
269
        je      .end_of_header
260
        inc     edi
270
        inc     edi
Line 261... Line -...
261
        dec     eax
-
 
262
        jnz     .scan
-
 
263
 
-
 
264
  .no_header:
-
 
265
        popa
-
 
266
        xor     eax, eax
-
 
267
        dec     eax
271
        dec     eax
268
        ret
272
        jnz     .scan
269
 
273
 
270
  .end_of_header:
274
  .end_of_header:
271
        add     edi, 4 - http_msg.data
275
        add     edi, 4 - http_msg.data
Line 395... Line 399...
395
        stdcall find_header_field, ebp, str_te
399
        stdcall find_header_field, ebp, str_te
396
        test    eax, eax
400
        test    eax, eax
397
        jz      .invalid_header
401
        jz      .invalid_header
Line 398... Line 402...
398
 
402
 
399
        mov     ebx, dword[eax]
403
        mov     ebx, dword[eax]
400
        or      eax, 0x20202020
404
        or      ebx, 0x20202020
401
        cmp     ebx, 'chun'
405
        cmp     ebx, 'chun'
402
        jne     .invalid_header
406
        jne     .invalid_header
403
        mov     ebx, dword[eax+4]
407
        mov     ebx, dword[eax+4]
404
        or      eax, 0x00202020
408
        or      ebx, 0x00202020
405
        and     eax, 0x00ffffff
409
        and     ebx, 0x00ffffff
406
        cmp     ebx, 'ked'
410
        cmp     ebx, 'ked'
Line 407... Line 411...
407
        jne     .invalid_header
411
        jne     .invalid_header
408
 
-
 
409
        or      [ebp + http_msg.flags], FLAG_CHUNKED
412
 
Line 410... Line 413...
410
 
413
        or      [ebp + http_msg.flags], FLAG_CHUNKED
411
        DEBUGF  1, "Transfer type is: chunked\n"
414
        DEBUGF  1, "Transfer type is: chunked\n"
412
 
415
 
413
; Set chunk pointer where first chunk should begin.
416
; Set chunk pointer where first chunk should begin.
Line 414... Line -...
414
        mov     eax, [ebp + http_msg.header_length]
-
 
415
        add     eax, http_msg.data
-
 
416
        mov     [ebp + http_msg.chunk_ptr], eax
-
 
417
 
-
 
418
  .header_parsed:
-
 
419
; If data is chunked, combine chunks into contiguous data if so.
417
        lea     eax, [ebp + http_msg.data]
420
        test    [ebp + http_msg.flags], FLAG_CHUNKED
418
        add     eax, [ebp + http_msg.header_length]
421
        jz      .not_chunked
419
        mov     [ebp + http_msg.chunk_ptr], eax
422
 
420
 
Line -... Line 421...
-
 
421
  .chunk_loop:
423
  .chunkloop:
422
        mov     ecx, [ebp + http_msg.write_ptr]
424
        mov     ecx, [ebp + http_msg.write_ptr]
423
        sub     ecx, [ebp + http_msg.chunk_ptr]
425
        sub     ecx, [ebp + http_msg.chunk_ptr]
424
        jb      .need_more_data_chunked
426
        jb      .not_finished
425
 
427
 
426
; TODO: make sure we have the complete chunkline header
428
        mov     esi, [ebp + http_msg.chunk_ptr]
427
        mov     esi, [ebp + http_msg.chunk_ptr]
429
        xor     ebx, ebx
428
        xor     ebx, ebx
430
  .chunk_hexloop:
429
  .chunk_hexloop:
431
        lodsb
430
        lodsb
432
        sub     al, '0'
431
        sub     al, '0'
433
        jb      .chunk_
432
        jb      .chunk_
434
        cmp     al, 9
433
        cmp     al, 9
435
        jbe     .chunk_hex
434
        jbe     .chunk_hex
436
        sub     al, 'A' - '0'
435
        sub     al, 'A' - '0' - 10
437
        jb      .chunk_
436
        jb      .chunk_
438
        cmp     al, 5
437
        cmp     al, 15
439
        jbe     .chunk_hex
438
        jbe     .chunk_hex
440
        sub     al, 'a' - 'A'
439
        sub     al, 'a' - 'A'
441
        cmp     al, 5
440
        cmp     al, 15
442
        ja      .chunk_
441
        ja      .chunk_
443
  .chunk_hex:
442
  .chunk_hex:
444
        shl     ebx, 4
443
        shl     ebx, 4
445
        add     bl, al
444
        add     bl, al
446
        jmp     .chunk_hexloop
445
        jmp     .chunk_hexloop
-
 
446
  .chunk_:
447
  .chunk_:
447
        DEBUGF  1, "got chunk of %u bytes\n", ebx
Line 448... Line 448...
448
        DEBUGF  1, "got chunk of %u bytes\n", ebx
448
; If chunk size is 0, all chunks have been received.
449
; If chunk size is 0, all chunks have been received.
449
        test    ebx, ebx
450
        test    ebx, ebx
450
        jz      .got_all_data_chunked           ; last chunk, hooray! FIXME: what if it wasnt a valid hex number???
451
        jz      .got_all_data   ; last chunk, hooray! FIXME: what if it wasnt a valid hex number???
451
        mov     edi, [ebp + http_msg.chunk_ptr] ; we'll need this in about 25 lines...
452
        add     [ebp + http_msg.chunk_ptr], ebx
452
        add     [ebp + http_msg.chunk_ptr], ebx
453
 
453
 
Line 454... Line 454...
454
; Chunkline ends with a CR, LF or simply LF
454
; Chunkline ends with a CR, LF or simply LF
455
  .end_of_chunkline?:           ; FIXME: buffer overflow possible!
455
  .end_of_chunkline?:           ; FIXME: buffer overflow possible!
456
        cmp     al, 10
456
        cmp     al, 10
457
        je      .end_of_chunkline
457
        je      .end_of_chunkline
458
        lodsb
458
        lodsb
459
        jmp     .end_of_chunkline?
459
        jmp     .end_of_chunkline?
-
 
460
 
-
 
461
  .end_of_chunkline:
-
 
462
; Realloc buffer, make it 'chunksize' bigger.
-
 
463
        mov     eax, [ebp + http_msg.buffer_length]
460
 
464
        add     eax, ebx
461
  .end_of_chunkline:
465
        invoke  mem.realloc, ebp, eax
462
; Now move all received data to the left (remove chunk header).
466
        or      eax, eax
-
 
467
        jz      .no_ram
-
 
468
        add     [ebp + http_msg.buffer_length], ebx
-
 
469
 
-
 
470
; Update write ptr
-
 
471
        mov     eax, esi
463
; Meanwhile, update write_ptr and content_length accordingly.
472
        sub     eax, edi
464
        mov     edi, [ebp + http_msg.chunk_ptr]
473
        sub     [ebp + http_msg.write_ptr], eax
465
        mov     ecx, [ebp + http_msg.write_ptr]
474
 
Line 466... Line -...
466
        sub     ecx, esi
-
 
467
        mov     eax, esi
475
; Now move all received data to the left (remove chunk header).
-
 
476
; Update content_length accordingly.
468
        sub     eax, edi
477
        mov     ecx, [ebp + http_msg.write_ptr]
469
        sub     [ebp + http_msg.write_ptr], eax
478
        sub     ecx, esi
470
        add     [ebp + http_msg.content_length], ecx
479
        add     [ebp + http_msg.content_length], ecx
471
        rep     movsb
480
        rep     movsb
-
 
481
        jmp     .chunk_loop
-
 
482
 
-
 
483
; Check if we got all the data.
-
 
484
  .header_parsed:
-
 
485
        mov     eax, [ebp + http_msg.header_length]
Line 472... Line 486...
472
        jmp     .chunkloop
486
        add     eax, [ebp + http_msg.content_length]
473
 
487
        cmp     eax, [ebp + http_msg.buffer_length]
474
  .not_chunked:
488
        je      .got_all_data
475
; Check if we got all the data.
489
  .need_more_data:
476
        mov     eax, [ebp + http_msg.header_length]
490
        popa
477
        add     eax, [ebp + http_msg.content_length]
491
        xor     eax, eax
Line -... Line 492...
-
 
492
        dec     eax
-
 
493
        ret
-
 
494
 
-
 
495
  .need_more_data_chunked:
-
 
496
        add     [ebp + http_msg.content_length], eax
-
 
497
        popa
-
 
498
        xor     eax, eax
478
        cmp     eax, [ebp + http_msg.buffer_length]
499
        dec     eax
479
        je      .got_all_data
500
        ret
480
 
501
 
481
  .not_finished:
502
  .got_all_data_chunked:
482
;        DEBUGF  1, "Needs more processing...\n"
503
        mov     eax, [ebp + http_msg.chunk_ptr]
483
        popa
504
        sub     eax, [ebp + http_msg.header_length]
484
        xor     eax, eax
505
        sub     eax, http_msg.data
Line 485... Line 506...
485
        dec     eax
506
        sub     eax, ebp
486
        ret
507
        mov     [ebp + http_msg.content_length], eax
487
 
508
 
488
  .got_all_data:
509
  .got_all_data:
Line 489... Line 510...
489
        DEBUGF  1, "We got all the data!\n"
510
        DEBUGF  1, "We got all the data! (%u bytes)\n", [ebp + http_msg.content_length]
490
        or      [ebp + http_msg.flags], FLAG_GOT_DATA
511
        or      [ebp + http_msg.flags], FLAG_GOT_DATA
491
        mcall   close, [ebp + http_msg.socket]
512
        mcall   close, [ebp + http_msg.socket]