Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Chunked Encoding now working.

 Need some more testing and review.
 Perhaps a bit of cleanup, Use registers instead of variables.
  • Loading branch information...
commit d22eea0ccaf4a4876d1d011b2d6a291dbe68895d 1 parent bbf140f
David Czech authored

Showing 1 changed file with 51 additions and 17 deletions. Show diff stats Hide diff stats

  1. 68  runtime/parrot/library/LWP/Protocol.pir
68  runtime/parrot/library/LWP/Protocol.pir
@@ -419,6 +419,20 @@ see http://search.cpan.org/~gaas/libwww-perl/
419 419
     .param pmc response
420 420
     .param string str
421 421
     .param int chunked
  422
+
  423
+    .local int    chunk_len
  424
+    .local int    chunk_sum
  425
+    .local int    chunk_len_idx
  426
+
  427
+    .local string data   
  428
+    .local string chunk
  429
+    .local string chunk_len_hex
  430
+    .local string final_buffer
  431
+
  432
+    chunk_sum = 0
  433
+    final_buffer  = ''
  434
+    
  435
+    $S0 = str
422 436
     $I0 = index str, "\r\n\r\n"
423 437
     if $I0 < 0 goto L1
424 438
     $I0 += 4
@@ -427,26 +441,48 @@ see http://search.cpan.org/~gaas/libwww-perl/
427 441
     $I0 = index str, "\n\n"
428 442
     if $I0 < 0 goto L4
429 443
     $I0 += 2
  444
+
430 445
   L2:
  446
+    $S0 = substr str, $I0
431 447
     if chunked == 0 goto L3
432  
-    # Proccess chunks into a single block
433  
-    say "Chunking together"
434  
-    $S0 = substr str, $I0 # 1000\r\n.data etc
435  
-    $I1 = index $S0, "\r\n"
436  
-    $S1 = substr $S0, 0, $I1
  448
+    data = $S0
  449
+  Lchunki:
  450
+    # Step 1) Get Chunk Length
  451
+    # Find first delimiter, then extract data in between.    
  452
+    chunk_len_idx = index data, "\r\n"
  453
+    chunk_len_hex = substr data, 0, chunk_len_idx
  454
+    
  455
+    # Box chunk_len_hex, and convert to integer (hex digits so radix 16).
  456
+    $P0 = box chunk_len_hex
  457
+    chunk_len = $P0.'to_int'(16)
  458
+        
  459
+    # Step 1a) If length is 0, return (L22 cleans up and returns)
  460
+    if chunk_len == 0 goto L22
  461
+    
  462
+    # Step 1b) Chunk-Sum (you heard right, Chunk-Sum)
  463
+    chunk_sum += chunk_len
437 464
     
438  
-    $P1 = box $S1
439  
-    #$S3 = get_number $P1, 16
440  
-    say $S3
441  
-    $S2 = "Chunk " . $S1
442  
-    say $S2
  465
+    # Step 2) Extract chunk into final_buffer
  466
+    $I1 = chunk_len_idx + 2
  467
+    chunk = substr data, $I1, chunk_len
  468
+    final_buffer = final_buffer . chunk
  469
+    # Update $S0 for final result (L3 sets $S0 as .content)
  470
+    $S0 = final_buffer
443 471
     
444  
-    $I2 = $P1.'to_int'(16)
445  
-    say $I2
446  
-    $I0 += $I2
447  
-    if $I2 != 0 goto L2
  472
+    # Increment data pointer, then Rinse and Repeat
  473
+    # We must increment chunk_len by len(chunk_len_hex) + 4
  474
+    # to skip over current chunk information and its delimiters,
  475
+    # so data always points the next chunk_length
  476
+    chunk_len += chunk_len_idx # The length of the hex string. (the CHUNK_HEX in \r\nCHUNK_HEX\r\n)
  477
+    chunk_len += 4             # The length of the delimters (the \r\n's in \r\nCHUNK_HEX\r\n)
  478
+    data = substr data, chunk_len
  479
+    goto Lchunki
  480
+  L22:
  481
+    # Chunk-Sum Sanity check 
  482
+    $I0 = length $S0
  483
+    if $I0 == chunk_sum goto L3
  484
+    say "Something has gone terribly wrong..."    
448 485
   L3:
449  
-    $S0 = substr str, $I0
450 486
     $P0 = box $S0
451 487
     setattribute response, 'content', $P0
452 488
     .return ($S0)
@@ -555,8 +591,6 @@ see http://search.cpan.org/~gaas/libwww-perl/
555 591
     goto L23
556 592
   Lchunked:
557 593
     # Chunked encoding, so we keep reading until we see a "0\r\n"
558  
-    say "Handling Chunked Encoding..."
559  
-    # Chunked=True
560 594
     chunked = 1
561 595
     $S0 = buf
562 596
     $I0 = index $S0, "\r\n0\r\n"

0 notes on commit d22eea0

Please sign in to comment.
Something went wrong with that request. Please try again.