# HG changeset patch # User Mike Pavone # Date 1376175091 25200 # Node ID d81de309a51f28335afaecea41889126e84fe742 # Parent 38140b7dbe3d53a6a30280799d308601ef18bb78 Add support for chunked encoding to HTTP client diff -r 38140b7dbe3d -r d81de309a51f modules/http.tp --- a/modules/http.tp Sat Aug 10 15:20:38 2013 -0700 +++ b/modules/http.tp Sat Aug 10 15:51:31 2013 -0700 @@ -1,4 +1,6 @@ { + parseHex <- :text { + } response <- :_headers _status _sock _data { _open? <- true _body <- "" @@ -12,7 +14,38 @@ body <- { if: _open? { if: _chunked? { - + chunkSize <- 0 + while: { + canReceive <- true + while: { + pos <- _data find: "\r\n" else: { -1 } + if: pos >= 0 { + chunkSize <- (_data from: 0 withLength: pos) parseHex32 + _data <- _data from: pos + 2 + false + } else: { + canReceive + } + } do: { + r <- (_sock recv: 4096) + if: (r length) > 0 { + _data <- _data . r + } else: { + canReceive <- false + } + } + chunkSize > 0 + } do: { + while: { (_data length) < chunkSize } do: { + r <- _sock recv: 4096 + if: (r length) > 0 { + _data <- _data . r + } else: { + chunkSize <- _data length + } + } + _body <- _body . (_data from: 0 withLength: chunkSize) + } } else: { if: _length >= 0 { _body <- _data . (_sock recvAll: (_length - (_data byte_length)))