Handle request body according to Content-Length

This commit is contained in:
Loic Nageleisen 2017-09-19 14:06:07 +02:00
parent f3aebdcf0f
commit 88d0824dc8

View file

@ -70,6 +70,13 @@ module NanoServe
break if req.headers?
end
logger.debug "request:\n" + buf.gsub(/^/, ' ')
length = 0
while req.content_length? && length < req.content_length
data = conn.readpartial(1024)
length += data.size
req << data
end
logger.debug "request body: #{length} bytes read"
res = Response.new
logger.debug 'calling'
@ -88,6 +95,7 @@ module NanoServe
@http_version = nil
@sep = nil
@headers = {}
@body = +''.encode('ASCII-8BIT')
end
def params
@ -100,7 +108,7 @@ module NanoServe
elsif @sep.nil?
parse_header(line.chomp)
else
@body << line
parse_body(line)
end
end
@ -108,6 +116,14 @@ module NanoServe
@sep
end
def content_length
@headers['content-length'].to_i
end
def content_length?
@headers.key?('content-length')
end
private
REQ_RE = %r{(?<method>[A-Z]+)\s+(?<path>\S+)\s+(?<version>HTTP/\d+.\d+)$}
@ -146,6 +162,10 @@ module NanoServe
@headers[m[:header].downcase] = m[:value]
end
def parse_body(line)
@body << line
end
end
class Response