|
|
local cqueues = require "cqueues" |
|
|
local monotime = cqueues.monotime |
|
|
local cc = require "cqueues.condition" |
|
|
local ce = require "cqueues.errno" |
|
|
local new_fifo = require "fifo" |
|
|
local lpeg = require "lpeg" |
|
|
local http_patts = require "lpeg_patterns.http" |
|
|
local new_headers = require "http.headers".new |
|
|
local reason_phrases = require "http.h1_reason_phrases" |
|
|
local stream_common = require "http.stream_common" |
|
|
local util = require "http.util" |
|
|
local has_zlib, zlib = pcall(require, "http.zlib") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local clean_shutdown_limit = 500*1024 |
|
|
|
|
|
local EOF = lpeg.P(-1) |
|
|
local Connection = lpeg.Ct(http_patts.Connection) * EOF |
|
|
local Content_Encoding = lpeg.Ct(http_patts.Content_Encoding) * EOF |
|
|
local Transfer_Encoding = lpeg.Ct(http_patts.Transfer_Encoding) * EOF |
|
|
local TE = lpeg.Ct(http_patts.TE) * EOF |
|
|
|
|
|
local function has(list, val) |
|
|
if list then |
|
|
for i=1, #list do |
|
|
if list[i] == val then |
|
|
return true |
|
|
end |
|
|
end |
|
|
end |
|
|
return false |
|
|
end |
|
|
|
|
|
local function has_any(list, val, ...) |
|
|
if has(list, val) then |
|
|
return true |
|
|
elseif (...) then |
|
|
return has(list, ...) |
|
|
else |
|
|
return false |
|
|
end |
|
|
end |
|
|
|
|
|
local stream_methods = { |
|
|
use_zlib = has_zlib; |
|
|
max_header_lines = 100; |
|
|
} |
|
|
for k,v in pairs(stream_common.methods) do |
|
|
stream_methods[k] = v |
|
|
end |
|
|
local stream_mt = { |
|
|
__name = "http.h1_stream"; |
|
|
__index = stream_methods; |
|
|
} |
|
|
|
|
|
function stream_mt:__tostring() |
|
|
return string.format("http.h1_stream{connection=%s;state=%q}", |
|
|
tostring(self.connection), self.state) |
|
|
end |
|
|
|
|
|
local function new_stream(connection) |
|
|
local self = setmetatable({ |
|
|
connection = connection; |
|
|
type = connection.type; |
|
|
|
|
|
state = "idle"; |
|
|
stats_sent = 0; |
|
|
stats_recv = 0; |
|
|
|
|
|
pipeline_cond = cc.new(); |
|
|
|
|
|
req_method = nil; |
|
|
peer_version = nil; |
|
|
has_main_headers = false; |
|
|
headers_in_progress = nil; |
|
|
headers_fifo = new_fifo(); |
|
|
headers_cond = cc.new(); |
|
|
chunk_fifo = new_fifo(); |
|
|
chunk_cond = cc.new(); |
|
|
body_write_type = nil; |
|
|
body_write_left = nil; |
|
|
body_write_deflate_encoding = nil; |
|
|
body_write_deflate = nil; |
|
|
body_read_type = nil; |
|
|
body_read_inflate = nil; |
|
|
close_when_done = nil; |
|
|
}, stream_mt) |
|
|
return self |
|
|
end |
|
|
|
|
|
local valid_states = { |
|
|
["idle"] = 1; |
|
|
["open"] = 2; |
|
|
["half closed (local)"] = 3; |
|
|
["half closed (remote)"] = 3; |
|
|
["closed"] = 4; |
|
|
} |
|
|
function stream_methods:set_state(new) |
|
|
local new_order = assert(valid_states[new]) |
|
|
local old = self.state |
|
|
if new_order <= valid_states[old] then |
|
|
error("invalid state progression ('"..old.."' to '"..new.."')") |
|
|
end |
|
|
local have_lock, want_no_lock |
|
|
local blocking_pipeline, notify_pipeline |
|
|
if self.type == "server" then |
|
|
|
|
|
have_lock = old == "idle" or old == "open" or old == "half closed (local)" |
|
|
want_no_lock = new == "half closed (remote)" or new == "closed" |
|
|
|
|
|
blocking_pipeline = old == "idle" or old == "open" or old == "half closed (remote)" |
|
|
notify_pipeline = blocking_pipeline and (new == "half closed (local)" or new == "closed") |
|
|
else |
|
|
|
|
|
have_lock = old == "open" or old == "half closed (remote)" |
|
|
want_no_lock = new == "half closed (local)" or new == "closed" |
|
|
|
|
|
blocking_pipeline = old == "idle" or old == "open" or old == "half closed (local)" |
|
|
notify_pipeline = blocking_pipeline and (new == "half closed (remote)" or new == "closed") |
|
|
end |
|
|
self.state = new |
|
|
if have_lock then |
|
|
assert(self.connection.req_locked == self) |
|
|
if want_no_lock then |
|
|
self.connection.req_locked = nil |
|
|
self.connection.req_cond:signal(1) |
|
|
end |
|
|
end |
|
|
local pipeline_empty |
|
|
if notify_pipeline then |
|
|
assert(self.connection.pipeline:pop() == self) |
|
|
local next_stream = self.connection.pipeline:peek() |
|
|
if next_stream then |
|
|
pipeline_empty = false |
|
|
next_stream.pipeline_cond:signal() |
|
|
else |
|
|
pipeline_empty = true |
|
|
end |
|
|
else |
|
|
pipeline_empty = not blocking_pipeline |
|
|
end |
|
|
if self.close_when_done then |
|
|
if new == "half closed (remote)" then |
|
|
self.connection:shutdown("r") |
|
|
elseif new == "half closed (local)" and self.type == "server" then |
|
|
|
|
|
|
|
|
|
|
|
self.connection:shutdown("w") |
|
|
elseif new == "closed" then |
|
|
self.connection:shutdown() |
|
|
end |
|
|
end |
|
|
if want_no_lock and pipeline_empty then |
|
|
self.connection:onidle()(self.connection) |
|
|
end |
|
|
end |
|
|
|
|
|
local bad_request_headers = new_headers() |
|
|
bad_request_headers:append(":status", "400") |
|
|
local server_error_headers = new_headers() |
|
|
server_error_headers:append(":status", "503") |
|
|
function stream_methods:shutdown() |
|
|
if self.state == "idle" then |
|
|
self:set_state("closed") |
|
|
else |
|
|
if self.type == "server" and (self.state == "open" or self.state == "half closed (remote)") then |
|
|
|
|
|
if self.connection.pipeline:peek() ~= self then |
|
|
|
|
|
self.pipeline_cond:wait() |
|
|
assert(self.connection.pipeline:peek() == self) |
|
|
end |
|
|
if not self.body_write_type then |
|
|
|
|
|
local error_headers |
|
|
if self.connection:error("r") == ce.EILSEQ then |
|
|
error_headers = bad_request_headers |
|
|
else |
|
|
error_headers = server_error_headers |
|
|
end |
|
|
self:write_headers(error_headers, true, 0) |
|
|
end |
|
|
end |
|
|
|
|
|
local start = self.stats_recv |
|
|
while (self.state == "open" or self.state == "half closed (local)") and (self.stats_recv - start) < clean_shutdown_limit do |
|
|
if not self:step(0) then |
|
|
break |
|
|
end |
|
|
end |
|
|
|
|
|
if self.state ~= "closed" then |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.connection.socket then |
|
|
self.connection.socket:shutdown() |
|
|
end |
|
|
self:set_state("closed") |
|
|
end |
|
|
end |
|
|
return true |
|
|
end |
|
|
|
|
|
function stream_methods:step(timeout) |
|
|
if self.state == "open" or self.state == "half closed (local)" or (self.state == "idle" and self.type == "server") then |
|
|
if self.connection.socket == nil then |
|
|
return nil, ce.strerror(ce.EPIPE), ce.EPIPE |
|
|
end |
|
|
if not self.has_main_headers then |
|
|
local headers, err, errno = self:read_headers(timeout) |
|
|
if headers == nil then |
|
|
return nil, err, errno |
|
|
end |
|
|
self.headers_fifo:push(headers) |
|
|
self.headers_cond:signal(1) |
|
|
return true |
|
|
end |
|
|
if self.body_read_left ~= 0 then |
|
|
local chunk, err, errno = self:read_next_chunk(timeout) |
|
|
if chunk == nil then |
|
|
if err == nil then |
|
|
return true |
|
|
end |
|
|
return nil, err, errno |
|
|
end |
|
|
self.chunk_fifo:push(chunk) |
|
|
self.chunk_cond:signal() |
|
|
return true |
|
|
end |
|
|
if self.body_read_type == "chunked" then |
|
|
local trailers, err, errno = self:read_headers(timeout) |
|
|
if trailers == nil then |
|
|
return nil, err, errno |
|
|
end |
|
|
self.headers_fifo:push(trailers) |
|
|
self.headers_cond:signal(1) |
|
|
return true |
|
|
end |
|
|
end |
|
|
if self.state == "half closed (remote)" then |
|
|
return nil, ce.strerror(ce.EIO), ce.EIO |
|
|
end |
|
|
return true |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function stream_methods:read_headers(timeout) |
|
|
local deadline = timeout and (monotime()+timeout) |
|
|
if self.state == "closed" or self.state == "half closed (remote)" then |
|
|
return nil |
|
|
end |
|
|
local status_code |
|
|
local is_trailers = self.body_read_type == "chunked" |
|
|
local headers = self.headers_in_progress |
|
|
if not headers then |
|
|
if is_trailers then |
|
|
headers = new_headers() |
|
|
elseif self.type == "server" then |
|
|
if self.state == "half closed (local)" then |
|
|
return nil |
|
|
end |
|
|
local method, target, httpversion = self.connection:read_request_line(0) |
|
|
if method == nil then |
|
|
if httpversion == ce.ETIMEDOUT then |
|
|
timeout = deadline and deadline-monotime() |
|
|
if cqueues.poll(self.connection.socket, timeout) ~= timeout then |
|
|
return self:read_headers(deadline and deadline-monotime()) |
|
|
end |
|
|
end |
|
|
return nil, target, httpversion |
|
|
end |
|
|
self.req_method = method |
|
|
self.peer_version = httpversion |
|
|
headers = new_headers() |
|
|
headers:append(":method", method) |
|
|
if method == "CONNECT" then |
|
|
headers:append(":authority", target) |
|
|
else |
|
|
headers:append(":path", target) |
|
|
end |
|
|
headers:append(":scheme", self:checktls() and "https" or "http") |
|
|
self:set_state("open") |
|
|
else |
|
|
|
|
|
if self.connection.pipeline:peek() ~= self then |
|
|
assert(cqueues.running(), "cannot wait for condition if not within a cqueues coroutine") |
|
|
if cqueues.poll(self.pipeline_cond, timeout) == timeout then |
|
|
return nil, ce.strerror(ce.ETIMEDOUT), ce.ETIMEDOUT |
|
|
end |
|
|
assert(self.connection.pipeline:peek() == self) |
|
|
end |
|
|
local httpversion, reason_phrase |
|
|
httpversion, status_code, reason_phrase = self.connection:read_status_line(0) |
|
|
if httpversion == nil then |
|
|
if reason_phrase == ce.ETIMEDOUT then |
|
|
timeout = deadline and deadline-monotime() |
|
|
if cqueues.poll(self.connection.socket, timeout) ~= timeout then |
|
|
return self:read_headers(deadline and deadline-monotime()) |
|
|
end |
|
|
elseif status_code == nil then |
|
|
return nil, ce.strerror(ce.EPIPE), ce.EPIPE |
|
|
end |
|
|
return nil, status_code, reason_phrase |
|
|
end |
|
|
self.peer_version = httpversion |
|
|
headers = new_headers() |
|
|
headers:append(":status", status_code) |
|
|
|
|
|
end |
|
|
self.headers_in_progress = headers |
|
|
else |
|
|
if not is_trailers and self.type == "client" then |
|
|
status_code = headers:get(":status") |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
while true do |
|
|
if headers:len() >= self.max_header_lines then |
|
|
return nil, ce.strerror(ce.E2BIG), ce.E2BIG |
|
|
end |
|
|
local k, v, errno = self.connection:read_header(0) |
|
|
if k == nil then |
|
|
if v ~= nil then |
|
|
if errno == ce.ETIMEDOUT then |
|
|
timeout = deadline and deadline-monotime() |
|
|
if cqueues.poll(self.connection.socket, timeout) ~= timeout then |
|
|
return self:read_headers(deadline and deadline-monotime()) |
|
|
end |
|
|
end |
|
|
return nil, v, errno |
|
|
end |
|
|
break |
|
|
end |
|
|
k = k:lower() |
|
|
if k == "host" and not is_trailers then |
|
|
k = ":authority" |
|
|
end |
|
|
headers:append(k, v) |
|
|
end |
|
|
|
|
|
do |
|
|
local ok, err, errno = self.connection:read_headers_done(0) |
|
|
if ok == nil then |
|
|
if errno == ce.ETIMEDOUT then |
|
|
timeout = deadline and deadline-monotime() |
|
|
if cqueues.poll(self.connection.socket, timeout) ~= timeout then |
|
|
return self:read_headers(deadline and deadline-monotime()) |
|
|
end |
|
|
elseif err == nil then |
|
|
return nil, ce.strerror(ce.EPIPE), ce.EPIPE |
|
|
end |
|
|
return nil, err, errno |
|
|
end |
|
|
self.headers_in_progress = nil |
|
|
self.has_main_headers = status_code == nil or status_code:sub(1,1) ~= "1" or status_code == "101" |
|
|
end |
|
|
|
|
|
do |
|
|
local h = headers:get_comma_separated("connection") |
|
|
if h then |
|
|
local connection_header = Connection:match(h) |
|
|
if connection_header and has(connection_header, "close") then |
|
|
self.close_when_done = true |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local no_body |
|
|
if is_trailers then |
|
|
|
|
|
no_body = true |
|
|
elseif self.type == "client" and ( |
|
|
self.req_method == "HEAD" |
|
|
or status_code == "204" |
|
|
or status_code == "304" |
|
|
) then |
|
|
no_body = true |
|
|
elseif self.type == "client" and ( |
|
|
status_code:sub(1,1) == "1" |
|
|
) then |
|
|
|
|
|
|
|
|
|
|
|
no_body = false |
|
|
if status_code == "101" then |
|
|
self.body_read_type = "close" |
|
|
end |
|
|
elseif headers:has("transfer-encoding") then |
|
|
no_body = false |
|
|
local transfer_encoding = Transfer_Encoding:match(headers:get_comma_separated("transfer-encoding")) |
|
|
local n = #transfer_encoding |
|
|
local last_transfer_encoding = transfer_encoding[n][1] |
|
|
if last_transfer_encoding == "chunked" then |
|
|
self.body_read_type = "chunked" |
|
|
n = n - 1 |
|
|
if n == 0 then |
|
|
last_transfer_encoding = nil |
|
|
else |
|
|
last_transfer_encoding = transfer_encoding[n][1] |
|
|
end |
|
|
else |
|
|
self.body_read_type = "close" |
|
|
end |
|
|
if last_transfer_encoding == "gzip" or last_transfer_encoding == "deflate" or last_transfer_encoding == "x-gzip" then |
|
|
self.body_read_inflate = zlib.inflate() |
|
|
n = n - 1 |
|
|
end |
|
|
if n > 0 then |
|
|
return nil, "unknown transfer-encoding" |
|
|
end |
|
|
elseif headers:has("content-length") then |
|
|
local cl = tonumber(headers:get("content-length"), 10) |
|
|
if cl == nil then |
|
|
return nil, "invalid content-length" |
|
|
end |
|
|
if cl == 0 then |
|
|
no_body = true |
|
|
else |
|
|
no_body = false |
|
|
self.body_read_type = "length" |
|
|
self.body_read_left = cl |
|
|
end |
|
|
elseif self.type == "server" then |
|
|
|
|
|
no_body = true |
|
|
else |
|
|
no_body = false |
|
|
self.body_read_type = "close" |
|
|
end |
|
|
if self.use_zlib and self.type == "server" and self.state == "open" and not is_trailers and headers:has("te") then |
|
|
local te = TE:match(headers:get_comma_separated("te")) |
|
|
for _, v in ipairs(te) do |
|
|
local tcoding = v[1] |
|
|
if (tcoding == "gzip" or tcoding == "x-gzip" or tcoding == "deflate") and v.q ~= 0 then |
|
|
v.q = nil |
|
|
self.body_write_deflate_encoding = v |
|
|
self.body_write_deflate = zlib.deflate() |
|
|
break |
|
|
end |
|
|
end |
|
|
end |
|
|
if no_body then |
|
|
if self.state == "open" then |
|
|
self:set_state("half closed (remote)") |
|
|
else |
|
|
self:set_state("closed") |
|
|
end |
|
|
end |
|
|
return headers |
|
|
end |
|
|
|
|
|
function stream_methods:get_headers(timeout) |
|
|
if self.headers_fifo:length() > 0 then |
|
|
return self.headers_fifo:pop() |
|
|
else |
|
|
if self.state == "closed" or self.state == "half closed (remote)" then |
|
|
return nil |
|
|
end |
|
|
local deadline = timeout and monotime()+timeout |
|
|
local ok, err, errno = self:step(timeout) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
return self:get_headers(deadline and deadline-monotime()) |
|
|
end |
|
|
end |
|
|
|
|
|
local ignore_fields = { |
|
|
[":authority"] = true; |
|
|
[":method"] = true; |
|
|
[":path"] = true; |
|
|
[":scheme"] = true; |
|
|
[":status"] = true; |
|
|
[":protocol"] = true; |
|
|
|
|
|
["connection"] = true; |
|
|
["content-length"] = true; |
|
|
["transfer-encoding"] = true; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function stream_methods:write_headers(headers, end_stream, timeout) |
|
|
local deadline = timeout and (monotime()+timeout) |
|
|
assert(headers, "missing argument: headers") |
|
|
|
|
|
local connection_header do |
|
|
local h = headers:get_comma_separated("connection") |
|
|
if h then |
|
|
connection_header = Connection:match(h) |
|
|
if not connection_header then |
|
|
error("invalid connection header") |
|
|
end |
|
|
else |
|
|
connection_header = {} |
|
|
end |
|
|
end |
|
|
local transfer_encoding_header do |
|
|
local h = headers:get_comma_separated("transfer-encoding") |
|
|
if h then |
|
|
transfer_encoding_header = Transfer_Encoding:match(h) |
|
|
if not transfer_encoding_header then |
|
|
error("invalid transfer-encoding header") |
|
|
end |
|
|
end |
|
|
end |
|
|
assert(type(end_stream) == "boolean", "'end_stream' MUST be a boolean") |
|
|
if self.state == "closed" or self.state == "half closed (local)" or self.connection.socket == nil then |
|
|
return nil, ce.strerror(ce.EPIPE), ce.EPIPE |
|
|
end |
|
|
local status_code, method |
|
|
local is_trailers |
|
|
if self.body_write_type == "chunked" then |
|
|
|
|
|
is_trailers = true |
|
|
local ok, err, errno = self.connection:write_body_last_chunk(nil, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
elseif self.type == "server" then |
|
|
if self.state == "idle" then |
|
|
error("cannot write headers when stream is idle") |
|
|
end |
|
|
status_code = headers:get(":status") |
|
|
|
|
|
|
|
|
if status_code and status_code:sub(1,1) == "1" and self.peer_version < 1.1 then |
|
|
error("a server MUST NOT send a 1xx response to an HTTP/1.0 client") |
|
|
end |
|
|
|
|
|
if self.connection.pipeline:peek() ~= self then |
|
|
assert(cqueues.running(), "cannot wait for condition if not within a cqueues coroutine") |
|
|
headers = headers:clone() |
|
|
if cqueues.poll(self.pipeline_cond, timeout) == timeout then |
|
|
return nil, ce.strerror(ce.ETIMEDOUT), ce.ETIMEDOUT |
|
|
end |
|
|
assert(self.connection.pipeline:peek() == self) |
|
|
end |
|
|
if status_code then |
|
|
|
|
|
local reason_phrase = reason_phrases[status_code] |
|
|
local version = math.min(self.connection.version, self.peer_version) |
|
|
local ok, err, errno = self.connection:write_status_line(version, status_code, reason_phrase, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
else |
|
|
if self.state == "idle" then |
|
|
method = assert(headers:get(":method"), "missing method") |
|
|
self.req_method = method |
|
|
local target |
|
|
if method == "CONNECT" then |
|
|
target = assert(headers:get(":authority"), "missing authority") |
|
|
assert(not headers:has(":path"), "CONNECT requests should not have a path") |
|
|
else |
|
|
|
|
|
assert(self.connection.version < 1.1 or headers:has(":authority"), "missing authority") |
|
|
target = assert(headers:get(":path"), "missing path") |
|
|
end |
|
|
if self.connection.req_locked then |
|
|
|
|
|
assert(cqueues.running(), "cannot wait for condition if not within a cqueues coroutine") |
|
|
headers = headers:clone() |
|
|
if cqueues.poll(self.connection.req_cond, timeout) == timeout then |
|
|
return nil, ce.strerror(ce.ETIMEDOUT), ce.ETIMEDOUT |
|
|
end |
|
|
assert(self.connection.req_locked == nil) |
|
|
end |
|
|
self.connection.pipeline:push(self) |
|
|
self.connection.req_locked = self |
|
|
|
|
|
local ok, err, errno = self.connection:write_request_line(method, target, self.connection.version, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
self:set_state("open") |
|
|
else |
|
|
assert(self.state == "open") |
|
|
end |
|
|
end |
|
|
|
|
|
local cl = headers:get("content-length") |
|
|
local add_te_gzip = false |
|
|
if self.req_method == "CONNECT" and (self.type == "client" or status_code == "200") then |
|
|
|
|
|
self.body_write_type = "close" |
|
|
self.close_when_done = true |
|
|
if self.type == "server" and (cl or transfer_encoding_header) then |
|
|
|
|
|
|
|
|
|
|
|
error("Content-Length and Transfer-Encoding not allowed with successful CONNECT response") |
|
|
end |
|
|
elseif self.type == "server" and status_code and status_code:sub(1, 1) == "1" then |
|
|
assert(not end_stream, "cannot end stream directly after 1xx status code") |
|
|
|
|
|
|
|
|
if cl then |
|
|
error("Content-Length not allowed in response with 1xx status code") |
|
|
end |
|
|
if status_code == "101" then |
|
|
self.body_write_type = "switched protocol" |
|
|
end |
|
|
elseif not self.body_write_type then |
|
|
if self.close_when_done == nil then |
|
|
if self.connection.version == 1.0 or (self.type == "server" and self.peer_version == 1.0) then |
|
|
self.close_when_done = not has(connection_header, "keep-alive") |
|
|
else |
|
|
self.close_when_done = has(connection_header, "close") |
|
|
end |
|
|
end |
|
|
if cl then |
|
|
|
|
|
|
|
|
|
|
|
if transfer_encoding_header then |
|
|
error("Content-Length not allowed in message with a transfer-encoding") |
|
|
elseif self.type == "server" then |
|
|
|
|
|
|
|
|
if status_code == "204" then |
|
|
error("Content-Length not allowed in response with 204 status code") |
|
|
end |
|
|
end |
|
|
end |
|
|
if end_stream then |
|
|
|
|
|
if self.type == "server" and (self.req_method == "HEAD" or status_code == "304") then |
|
|
self.body_write_type = "missing" |
|
|
elseif transfer_encoding_header then |
|
|
if transfer_encoding_header[#transfer_encoding_header][1] == "chunked" then |
|
|
|
|
|
self.body_write_type = "chunked" |
|
|
else |
|
|
error("unknown transfer-encoding") |
|
|
end |
|
|
else |
|
|
|
|
|
|
|
|
if cl then |
|
|
assert(cl:match("^ *0+ *$"), "cannot end stream after headers if you have a non-zero content-length") |
|
|
elseif self.type ~= "client" or (method ~= "GET" and method ~= "HEAD") then |
|
|
cl = "0" |
|
|
end |
|
|
self.body_write_type = "length" |
|
|
self.body_write_left = 0 |
|
|
end |
|
|
else |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if transfer_encoding_header and transfer_encoding_header[#transfer_encoding_header][1] == "chunked" then |
|
|
self.body_write_type = "chunked" |
|
|
elseif cl then |
|
|
self.body_write_type = "length" |
|
|
self.body_write_left = assert(tonumber(cl, 10), "invalid content-length") |
|
|
elseif self.close_when_done then |
|
|
self.body_write_type = "close" |
|
|
elseif self.connection.version == 1.1 and (self.type == "client" or self.peer_version == 1.1) then |
|
|
self.body_write_type = "chunked" |
|
|
|
|
|
if not transfer_encoding_header then |
|
|
transfer_encoding_header = {nil} |
|
|
end |
|
|
table.insert(transfer_encoding_header, {"chunked"}) |
|
|
elseif self.type == "server" then |
|
|
|
|
|
self.body_write_type = "close" |
|
|
self.close_when_done = true |
|
|
else |
|
|
error("a client cannot send a body with connection: keep-alive without indicating body delimiter in headers") |
|
|
end |
|
|
end |
|
|
|
|
|
if self.close_when_done and not has(connection_header, "close") then |
|
|
table.insert(connection_header, "close") |
|
|
end |
|
|
if self.use_zlib then |
|
|
if self.type == "client" then |
|
|
|
|
|
add_te_gzip = true |
|
|
else |
|
|
|
|
|
if self.body_write_deflate |
|
|
and not cl |
|
|
and not end_stream |
|
|
and not has_any(Content_Encoding:match(headers:get_comma_separated("content-encoding") or ""), "gzip", "x-gzip", "deflate") |
|
|
|
|
|
|
|
|
then |
|
|
if transfer_encoding_header then |
|
|
local n = #transfer_encoding_header |
|
|
|
|
|
if transfer_encoding_header[n][1] == "chunked" then |
|
|
transfer_encoding_header[n+1] = transfer_encoding_header[n] |
|
|
transfer_encoding_header[n] = self.body_write_deflate_encoding |
|
|
else |
|
|
transfer_encoding_header[n+1] = self.body_write_deflate_encoding |
|
|
end |
|
|
else |
|
|
transfer_encoding_header = {self.body_write_deflate_encoding} |
|
|
end |
|
|
else |
|
|
|
|
|
self.body_write_deflate_encoding = nil |
|
|
self.body_write_deflate = nil |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
for name, value in headers:each() do |
|
|
if not ignore_fields[name] then |
|
|
local ok, err, errno = self.connection:write_header(name, value, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
elseif name == ":authority" then |
|
|
|
|
|
if self.req_method ~= "CONNECT" then |
|
|
|
|
|
local ok, err, errno = self.connection:write_header("host", value, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
if add_te_gzip then |
|
|
|
|
|
if not has(connection_header, "te") then |
|
|
table.insert(connection_header, "te") |
|
|
end |
|
|
local ok, err, errno = self.connection:write_header("te", "gzip, deflate", 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
|
|
|
if transfer_encoding_header and transfer_encoding_header[1] then |
|
|
|
|
|
if not has(connection_header, "transfer-encoding") then |
|
|
table.insert(connection_header, "transfer-encoding") |
|
|
end |
|
|
local value = {} |
|
|
for i, v in ipairs(transfer_encoding_header) do |
|
|
local params = {v[1]} |
|
|
for k, vv in pairs(v) do |
|
|
if type(k) == "string" then |
|
|
params[#params+1] = k .. "=" .. util.maybe_quote(vv) |
|
|
end |
|
|
end |
|
|
value[i] = table.concat(params, ";") |
|
|
end |
|
|
value = table.concat(value, ",") |
|
|
local ok, err, errno = self.connection:write_header("transfer-encoding", value, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
elseif cl then |
|
|
local ok, err, errno = self.connection:write_header("content-length", cl, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
if connection_header and connection_header[1] then |
|
|
local value = table.concat(connection_header, ",") |
|
|
local ok, err, errno = self.connection:write_header("connection", value, 0) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
|
|
|
do |
|
|
local ok, err, errno = self.connection:write_headers_done(deadline and (deadline-monotime())) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
|
|
|
if end_stream then |
|
|
if is_trailers then |
|
|
if self.state == "half closed (remote)" then |
|
|
self:set_state("closed") |
|
|
else |
|
|
self:set_state("half closed (local)") |
|
|
end |
|
|
else |
|
|
local ok, err, errno = self:write_chunk("", true) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
end |
|
|
end |
|
|
|
|
|
return true |
|
|
end |
|
|
|
|
|
function stream_methods:read_next_chunk(timeout) |
|
|
if self.state == "closed" or self.state == "half closed (remote)" then |
|
|
return nil |
|
|
end |
|
|
local end_stream |
|
|
local chunk, err, errno |
|
|
if self.body_read_type == "chunked" then |
|
|
local deadline = timeout and (monotime()+timeout) |
|
|
if self.body_read_left == 0 then |
|
|
chunk = false |
|
|
else |
|
|
chunk, err, errno = self.connection:read_body_chunk(timeout) |
|
|
end |
|
|
if chunk == false then |
|
|
|
|
|
self.body_read_left = 0 |
|
|
|
|
|
local ok |
|
|
ok, err, errno = self:step(deadline and deadline-monotime()) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
return nil |
|
|
else |
|
|
end_stream = false |
|
|
if chunk == nil and err == nil then |
|
|
return nil, ce.strerror(ce.EPIPE), ce.EPIPE |
|
|
end |
|
|
end |
|
|
elseif self.body_read_type == "length" then |
|
|
local length_n = self.body_read_left |
|
|
if length_n > 0 then |
|
|
|
|
|
|
|
|
chunk, err, errno = self.connection:read_body_by_length(-length_n, timeout) |
|
|
if chunk ~= nil then |
|
|
self.body_read_left = length_n - #chunk |
|
|
end_stream = (self.body_read_left == 0) |
|
|
end |
|
|
elseif length_n == 0 then |
|
|
chunk = "" |
|
|
end_stream = true |
|
|
else |
|
|
error("invalid length: "..tostring(length_n)) |
|
|
end |
|
|
elseif self.body_read_type == "close" then |
|
|
|
|
|
chunk, err, errno = self.connection:read_body_by_length(-0x80000000, timeout) |
|
|
end_stream = chunk == nil and err == nil |
|
|
elseif self.body_read_type == nil then |
|
|
|
|
|
local deadline = timeout and (monotime()+timeout) |
|
|
local headers |
|
|
headers, err, errno = self:read_headers(timeout) |
|
|
if not headers then |
|
|
return nil, err, errno |
|
|
end |
|
|
self.headers_fifo:push(headers) |
|
|
self.headers_cond:signal(1) |
|
|
return self:get_next_chunk(deadline and deadline-monotime()) |
|
|
else |
|
|
error("unknown body read type") |
|
|
end |
|
|
if chunk then |
|
|
if self.body_read_inflate then |
|
|
chunk = self.body_read_inflate(chunk, end_stream) |
|
|
end |
|
|
self.stats_recv = self.stats_recv + #chunk |
|
|
end |
|
|
if end_stream then |
|
|
if self.state == "half closed (local)" then |
|
|
self:set_state("closed") |
|
|
else |
|
|
self:set_state("half closed (remote)") |
|
|
end |
|
|
end |
|
|
return chunk, err, errno |
|
|
end |
|
|
|
|
|
function stream_methods:get_next_chunk(timeout) |
|
|
if self.chunk_fifo:length() > 0 then |
|
|
return self.chunk_fifo:pop() |
|
|
end |
|
|
return self:read_next_chunk(timeout) |
|
|
end |
|
|
|
|
|
function stream_methods:unget(str) |
|
|
self.chunk_fifo:insert(1, str) |
|
|
self.chunk_cond:signal() |
|
|
return true |
|
|
end |
|
|
|
|
|
local empty_headers = new_headers() |
|
|
function stream_methods:write_chunk(chunk, end_stream, timeout) |
|
|
if self.state == "idle" then |
|
|
error("cannot write chunk when stream is " .. self.state) |
|
|
elseif self.state == "closed" or self.state == "half closed (local)" or self.connection.socket == nil then |
|
|
return nil, ce.strerror(ce.EPIPE), ce.EPIPE |
|
|
elseif self.body_write_type == nil then |
|
|
error("cannot write body before headers") |
|
|
end |
|
|
if self.type == "client" then |
|
|
assert(self.connection.req_locked == self) |
|
|
else |
|
|
assert(self.connection.pipeline:peek() == self) |
|
|
end |
|
|
local orig_size = #chunk |
|
|
if self.body_write_deflate then |
|
|
chunk = self.body_write_deflate(chunk, end_stream) |
|
|
end |
|
|
if #chunk > 0 then |
|
|
if self.body_write_type == "chunked" then |
|
|
local deadline = timeout and monotime()+timeout |
|
|
local ok, err, errno = self.connection:write_body_chunk(chunk, nil, timeout) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
timeout = deadline and (deadline-monotime()) |
|
|
elseif self.body_write_type == "length" then |
|
|
assert(self.body_write_left >= #chunk, "invalid content-length") |
|
|
local ok, err, errno = self.connection:write_body_plain(chunk, timeout) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
self.body_write_left = self.body_write_left - #chunk |
|
|
elseif self.body_write_type == "close" then |
|
|
local ok, err, errno = self.connection:write_body_plain(chunk, timeout) |
|
|
if not ok then |
|
|
return nil, err, errno |
|
|
end |
|
|
elseif self.body_write_type ~= "missing" then |
|
|
error("unknown body writing method") |
|
|
end |
|
|
end |
|
|
self.stats_sent = self.stats_sent + orig_size |
|
|
if end_stream then |
|
|
if self.body_write_type == "chunked" then |
|
|
return self:write_headers(empty_headers, true, timeout) |
|
|
elseif self.body_write_type == "length" then |
|
|
assert(self.body_write_left == 0, "invalid content-length") |
|
|
end |
|
|
if self.state == "half closed (remote)" then |
|
|
self:set_state("closed") |
|
|
else |
|
|
self:set_state("half closed (local)") |
|
|
end |
|
|
end |
|
|
return true |
|
|
end |
|
|
|
|
|
return { |
|
|
new = new_stream; |
|
|
methods = stream_methods; |
|
|
mt = stream_mt; |
|
|
} |
|
|
|