comparison modules/http.tp @ 159:d81de309a51f

Add support for chunked encoding to HTTP client
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Aug 2013 15:51:31 -0700
parents 55e0dca7d3d7
children 729dc894e61c
comparison
equal deleted inserted replaced
158:38140b7dbe3d 159:d81de309a51f
1 { 1 {
2 parseHex <- :text {
3 }
2 response <- :_headers _status _sock _data { 4 response <- :_headers _status _sock _data {
3 _open? <- true 5 _open? <- true
4 _body <- "" 6 _body <- ""
5 _length <- int32: (_headers get: "Content-Length" withDefault: "-1") 7 _length <- int32: (_headers get: "Content-Length" withDefault: "-1")
6 _chunked? <- (_headers get: "Transfer-Encoding" withDefault: "") = "chunked" 8 _chunked? <- (_headers get: "Transfer-Encoding" withDefault: "") = "chunked"
10 status <- { _status } 12 status <- { _status }
11 statusCode <- { _code } 13 statusCode <- { _code }
12 body <- { 14 body <- {
13 if: _open? { 15 if: _open? {
14 if: _chunked? { 16 if: _chunked? {
15 17 chunkSize <- 0
18 while: {
19 canReceive <- true
20 while: {
21 pos <- _data find: "\r\n" else: { -1 }
22 if: pos >= 0 {
23 chunkSize <- (_data from: 0 withLength: pos) parseHex32
24 _data <- _data from: pos + 2
25 false
26 } else: {
27 canReceive
28 }
29 } do: {
30 r <- (_sock recv: 4096)
31 if: (r length) > 0 {
32 _data <- _data . r
33 } else: {
34 canReceive <- false
35 }
36 }
37 chunkSize > 0
38 } do: {
39 while: { (_data length) < chunkSize } do: {
40 r <- _sock recv: 4096
41 if: (r length) > 0 {
42 _data <- _data . r
43 } else: {
44 chunkSize <- _data length
45 }
46 }
47 _body <- _body . (_data from: 0 withLength: chunkSize)
48 }
16 } else: { 49 } else: {
17 if: _length >= 0 { 50 if: _length >= 0 {
18 _body <- _data . (_sock recvAll: (_length - (_data byte_length))) 51 _body <- _data . (_sock recvAll: (_length - (_data byte_length)))
19 } else: { 52 } else: {
20 chunk <- "" 53 chunk <- ""