Compare commits

...

17 commits

Author SHA1 Message Date
2edc861a06 Release v0.3.0 2017-09-19 15:47:44 +02:00
8a5c956a24 Implement form-urlencoded support
Query and form return a Hash, folding duplicates with last-key-wins
strategy. *_array methods provide the seldom-used duplicate-preserving
counterparts.

Keep query and form separate, but provide params, which handles
both, with form having precedence over query.
2017-09-19 15:46:42 +02:00
66a2a20663 Implement request host, path and content_type access 2017-09-19 15:37:27 +02:00
dd5001cb7c Implement request body and normalized headers access 2017-09-19 15:35:55 +02:00
dc8739c692 Normalize request method verb 2017-09-19 15:34:02 +02:00
8f20ccdd60 Release v0.2.0 2017-09-19 14:12:00 +02:00
ea6406f4b8 Compute response's Content-Length once 2017-09-19 14:10:14 +02:00
88d0824dc8 Handle request body according to Content-Length 2017-09-19 14:06:07 +02:00
f3aebdcf0f Normalize header keys to lowercase 2017-09-19 14:02:57 +02:00
5c1717d8ee Privatise parse methods 2017-09-19 14:01:00 +02:00
df5525854e Factor end of headers processing in 2017-09-19 13:59:45 +02:00
803d910a3e Accumulate in buffer only for debug output 2017-09-19 13:57:34 +02:00
c3a69ac1ed Return after assign in guard clause 2017-09-19 13:57:01 +02:00
f70c28abd9 Allow overridable logger 2017-09-19 13:49:28 +02:00
324a052913 Silence double log on client disconnect 2017-09-19 13:49:16 +02:00
53988be5ee Close listening socket outside of start 2017-09-19 13:48:45 +02:00
eea9588742 Accept lowercase headers 2017-09-19 13:47:44 +02:00
3 changed files with 137 additions and 16 deletions

View file

@ -11,14 +11,15 @@ module NanoServe
@port = port
@block = block
@thr = nil
@srv = nil
end
def start(y)
server = TCPServer.new(@port)
@srv = TCPServer.new(@port)
@thr = Thread.new do
Thread.abort_on_exception = true
conn = server.accept
conn = @srv.accept
port, host = conn.peeraddr[1, 2]
client = "#{host}:#{port}"
logger.debug "#{client}: connected"
@ -27,8 +28,9 @@ module NanoServe
@block.call(conn, y)
rescue EOFError
logger.debug "#{client}: disconnected"
ensure
else
logger.debug "#{client}: closed"
ensure
conn.close
end
end
@ -38,18 +40,22 @@ module NanoServe
yield
@thr.join
server.close
y
end
def stop
@srv.close
@thr.kill
end
def logger
@logger ||= Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
end
def logger=(logger)
@logger = logger
end
end
class HTTPResponder < TCPResponder
@ -59,11 +65,18 @@ module NanoServe
buf = +''
loop do
line = conn.readline
break if line.chomp == ''
req << line
buf << line
buf << line if logger.debug?
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'
@ -82,10 +95,47 @@ module NanoServe
@http_version = nil
@sep = nil
@headers = {}
@body = +''.encode('ASCII-8BIT')
end
def host
@headers['host']
end
def path
@uri.path
end
def query_array
URI.decode_www_form(@uri.query || '')
end
def form_array
form? ? URI.decode_www_form(body) : []
end
def query
Hash[*query_array.flatten]
end
def form
Hash[*form_array.flatten]
end
def params
Hash[*@uri.query.split('&').map { |kv| kv.split('=') }.flatten]
query.merge(form)
end
def form?
content_type == 'application/x-www-form-urlencoded'
end
def body
@body
end
def [](key)
@headers[key.downcase]
end
def <<(line)
@ -94,10 +144,28 @@ module NanoServe
elsif @sep.nil?
parse_header(line.chomp)
else
@body << line
parse_body(line)
end
end
def headers?
@sep
end
def content_length
@headers['content-length'].to_i
end
def content_length?
@headers.key?('content-length')
end
def content_type
@headers['content-type']
end
private
REQ_RE = %r{(?<method>[A-Z]+)\s+(?<path>\S+)\s+(?<version>HTTP/\d+.\d+)$}
def parse_request(str)
@ -111,7 +179,7 @@ module NanoServe
end
def parse_method(str)
str
str.upcase
end
def parse_path(str)
@ -123,13 +191,20 @@ module NanoServe
end
def parse_header(str)
(@sep = '' && return) if str == ''
if str == ''
@sep = true
return
end
unless (m = str.match(/(?<header>[A-Z][-A-Za-z]*):\s+(?<value>.+)$/))
unless (m = str.match(/(?<header>[A-Za-z][-A-Za-z]*):\s+(?<value>.+)$/))
raise RequestError, "cannot parse header: '#{str}'"
end
@headers[m[:header]] = m[:value]
@headers[m[:header].downcase] = m[:value]
end
def parse_body(line)
@body << line
end
end
@ -151,7 +226,7 @@ module NanoServe
end
def body=(value)
@body = value.tap { @content_length = body.bytes.count.to_s }
@body = value.tap { @content_length = value.bytes.count.to_s }
end
def to_s
@ -181,7 +256,7 @@ module NanoServe
end
def content_length
@content_length ||= body.bytes.count.to_s
@content_length || '0'
end
def content_type

View file

@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = 'nanoserve'
s.version = '0.1.0'
s.version = '0.3.0'
s.licenses = ['3BSD']
s.summary = 'Listen to one-shot connections'
s.authors = ['Loic Nageleisen']

View file

@ -24,10 +24,12 @@ class TestNanoServe < MiniTest::Test
s.close
end
r.stop
assert_equal(uuid, buf)
end
def test_http_responder
def test_http_responder_get
uuid = SecureRandom.uuid.encode('UTF-8')
uri = URI('http://localhost:2000')
@ -50,6 +52,50 @@ class TestNanoServe < MiniTest::Test
Net::HTTP.get(uri + "test?uuid=#{uuid}")
end
r.stop
assert_equal(uuid, req.first.params['uuid'])
end
def test_http_responder_post
uuid = SecureRandom.uuid.encode('UTF-8')
uri = URI('http://localhost:2000')
r = NanoServe::HTTPResponder.new(uri.host, uri.port) do |res, req, y|
y << req
res.body = <<-EOS.gsub(/^ {8}/, '')
<html>
<head>
<title>An Example Page</title>
</head>
<body>
Hello World, this is a very simple HTML document.
</body>
</html>
EOS
end
req = r.start([]) do
Net::HTTP.post_form(
uri + "test?uuid=#{uuid}&p=query",
'p' => 'form',
'f' => 'foo',
)
end
r.stop
assert_equal(uuid, req.first.params['uuid'])
assert_equal(uuid, req.first.query['uuid'])
assert_nil(req.first.form['uuid'])
assert_equal('foo', req.first.params['f'])
assert_nil(req.first.query['f'])
assert_equal('foo', req.first.form['f'])
assert_equal('form', req.first.params['p'])
assert_equal('query', req.first.query['p'])
assert_equal('form', req.first.form['p'])
end
end