|
| 1 | +local co_wrap_iter = require("resty.coroutines").co_wrap_iter |
| 2 | +local co_yield = coroutine._yield |
| 3 | + |
| 4 | +local _M = { |
| 5 | +} |
| 6 | + |
| 7 | +local cr_lf = "\r\n" |
| 8 | +local default_max_chunk_size = 32 * 1024 -- 32K |
| 9 | + |
| 10 | +local function send(socket, data) |
| 11 | + if not data or data == '' then |
| 12 | + ngx.log(ngx.DEBUG, 'skipping sending nil') |
| 13 | + return |
| 14 | + end |
| 15 | + |
| 16 | + return socket:send(data) |
| 17 | +end |
| 18 | + |
| 19 | +local function print_err(error_code, ...) |
| 20 | + ngx.log(ngx.ERR, ...) |
| 21 | + return ngx.exit(error_code) |
| 22 | +end |
| 23 | + |
| 24 | +-- TODO: support Trailers |
| 25 | +-- chunked_reader return a body reader that translates the data read from sock |
| 26 | +-- out of HTTP "chunked" format before returning it |
| 27 | +-- |
| 28 | +-- The chunked reader return nil when the final 0-length chunk is read |
| 29 | +function _M.chunked_reader(sock, max_chunk_size) |
| 30 | + max_chunk_size = max_chunk_size or default_max_chunk_size |
| 31 | + |
| 32 | + if not sock then |
| 33 | + return nil, "chunked_reader: invalid sock" |
| 34 | + end |
| 35 | + |
| 36 | + return co_wrap_iter(function() |
| 37 | + local eof = false |
| 38 | + local remaining = 0 |
| 39 | + local size = 0 |
| 40 | + repeat |
| 41 | + -- If we still have data on this chunk |
| 42 | + if max_chunk_size and remaining > 0 then |
| 43 | + if remaining > max_chunk_size then |
| 44 | + -- Consume up to max_chunk_size |
| 45 | + size = max_chunk_size |
| 46 | + remaining = remaining - max_chunk_size |
| 47 | + else |
| 48 | + -- Consume all remaining |
| 49 | + size = remaining |
| 50 | + remaining = 0 |
| 51 | + end |
| 52 | + else |
| 53 | + -- read a line from socket |
| 54 | + -- chunk-size CRLF |
| 55 | + local line, err = sock:receive() |
| 56 | + if not line then |
| 57 | + co_yield(nil, "chunked_reader: failed to receive chunk size, err: " .. err) |
| 58 | + end |
| 59 | + |
| 60 | + size = tonumber(line, 16) |
| 61 | + if not size then |
| 62 | + co_yield(nil, "chunked_reader: unable to read chunksize") |
| 63 | + end |
| 64 | + |
| 65 | + if max_chunk_size and size > max_chunk_size then |
| 66 | + -- Consume up to max_chunk_size |
| 67 | + remaining = size - max_chunk_size |
| 68 | + size = max_chunk_size |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + |
| 73 | + if size > 0 then |
| 74 | + -- Receive the chunk |
| 75 | + local chunk, err = sock:receive(size) |
| 76 | + if not chunk then |
| 77 | + co_yield(nil, "chunked_reader: failed to receive chunk of size " .. size .. " err: " .. err) |
| 78 | + end |
| 79 | + |
| 80 | + -- We're at the end of a chunk, read the next two bytes |
| 81 | + -- and verify they are "\r\n" |
| 82 | + local data, err = sock:receive(2) |
| 83 | + if not data then |
| 84 | + co_yield(nil, "chunked_reader: failed to receive chunk terminator, err: " .. err) |
| 85 | + end |
| 86 | + |
| 87 | + chunk = string.format("%x\r\n", size) .. chunk .. data |
| 88 | + |
| 89 | + co_yield(chunk) |
| 90 | + else |
| 91 | + -- we're at the end of a chunk, read the next two |
| 92 | + -- bytes to verify they are "\r\n". |
| 93 | + local chunk, err = sock:receive(2) |
| 94 | + if not chunk then |
| 95 | + co_yield(nil, "chunked_reader: failed to receive chunk terminator, err: " .. err) |
| 96 | + end |
| 97 | + |
| 98 | + if chunk ~= "\r\n" then |
| 99 | + co_yield(nil, "chunked_reader: bad chunk terminator: " .. chunk) |
| 100 | + end |
| 101 | + |
| 102 | + eof = true |
| 103 | + co_yield("0\r\n\r\n") |
| 104 | + break |
| 105 | + end |
| 106 | + until eof |
| 107 | + end) |
| 108 | +end |
| 109 | + |
| 110 | +-- chunked_writer writes response body reader to sock in the HTTP/1.x server response format, |
| 111 | +-- including the status line, headers, body, and optional trailer. |
| 112 | +function _M.chunked_writer(sock, res, chunksize) |
| 113 | + local bytes, err |
| 114 | + chunksize = chunksize or 65536 |
| 115 | + |
| 116 | + -- Status line |
| 117 | + -- FIXME: should get protocol version from res? |
| 118 | + local status = "HTTP/1.1 " .. res.status .. " " .. res.reason .. cr_lf |
| 119 | + bytes, err = send(sock, status) |
| 120 | + if not bytes then |
| 121 | + print_err(503, "chunked_writer: failed to send status line, err: ", err) |
| 122 | + end |
| 123 | + |
| 124 | + -- Rest of header |
| 125 | + for k, v in pairs(res.headers) do |
| 126 | + local header = k .. ": " .. v .. cr_lf |
| 127 | + bytes, err = send(sock, header) |
| 128 | + if not bytes then |
| 129 | + print_err(503, "chunked_writer: failed to send header, err: ", err) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + -- End-of-header |
| 134 | + bytes, err = send(sock, cr_lf) |
| 135 | + if not bytes then |
| 136 | + print_err(503, "chunked_writer: failed to send end of header, err: ", err) |
| 137 | + end |
| 138 | + |
| 139 | + -- Write body and trailer |
| 140 | + -- TODO: handle trailer |
| 141 | + if res.has_body then |
| 142 | + local reader = res.body_reader |
| 143 | + repeat |
| 144 | + local chunk, read_err |
| 145 | + |
| 146 | + chunk, read_err = reader(chunksize) |
| 147 | + if read_err then |
| 148 | + print_err(503, "chunked_writer: failed to read body, err: ", err) |
| 149 | + end |
| 150 | + |
| 151 | + if chunk then |
| 152 | + bytes, err = send(sock, chunk) |
| 153 | + if not bytes then |
| 154 | + print_err("chunked_writer: failed to send body, err: ", err) |
| 155 | + end |
| 156 | + end |
| 157 | + until not chunk |
| 158 | + end |
| 159 | +end |
| 160 | + |
| 161 | +return _M |
0 commit comments