mirror of
https://github.com/lloeki/nanoserve.git
synced 2025-12-06 11:14:40 +01:00
Compare commits
17 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2edc861a06 | |||
| 8a5c956a24 | |||
| 66a2a20663 | |||
| dd5001cb7c | |||
| dc8739c692 | |||
| 8f20ccdd60 | |||
| ea6406f4b8 | |||
| 88d0824dc8 | |||
| f3aebdcf0f | |||
| 5c1717d8ee | |||
| df5525854e | |||
| 803d910a3e | |||
| c3a69ac1ed | |||
| f70c28abd9 | |||
| 324a052913 | |||
| 53988be5ee | |||
| eea9588742 |
3 changed files with 137 additions and 16 deletions
103
lib/nanoserve.rb
103
lib/nanoserve.rb
|
|
@ -11,14 +11,15 @@ module NanoServe
|
||||||
@port = port
|
@port = port
|
||||||
@block = block
|
@block = block
|
||||||
@thr = nil
|
@thr = nil
|
||||||
|
@srv = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def start(y)
|
def start(y)
|
||||||
server = TCPServer.new(@port)
|
@srv = TCPServer.new(@port)
|
||||||
|
|
||||||
@thr = Thread.new do
|
@thr = Thread.new do
|
||||||
Thread.abort_on_exception = true
|
Thread.abort_on_exception = true
|
||||||
conn = server.accept
|
conn = @srv.accept
|
||||||
port, host = conn.peeraddr[1, 2]
|
port, host = conn.peeraddr[1, 2]
|
||||||
client = "#{host}:#{port}"
|
client = "#{host}:#{port}"
|
||||||
logger.debug "#{client}: connected"
|
logger.debug "#{client}: connected"
|
||||||
|
|
@ -27,8 +28,9 @@ module NanoServe
|
||||||
@block.call(conn, y)
|
@block.call(conn, y)
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
logger.debug "#{client}: disconnected"
|
logger.debug "#{client}: disconnected"
|
||||||
ensure
|
else
|
||||||
logger.debug "#{client}: closed"
|
logger.debug "#{client}: closed"
|
||||||
|
ensure
|
||||||
conn.close
|
conn.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -38,18 +40,22 @@ module NanoServe
|
||||||
yield
|
yield
|
||||||
|
|
||||||
@thr.join
|
@thr.join
|
||||||
server.close
|
|
||||||
|
|
||||||
y
|
y
|
||||||
end
|
end
|
||||||
|
|
||||||
def stop
|
def stop
|
||||||
|
@srv.close
|
||||||
@thr.kill
|
@thr.kill
|
||||||
end
|
end
|
||||||
|
|
||||||
def logger
|
def logger
|
||||||
@logger ||= Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
|
@logger ||= Logger.new(STDOUT).tap { |l| l.level = Logger::INFO }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def logger=(logger)
|
||||||
|
@logger = logger
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class HTTPResponder < TCPResponder
|
class HTTPResponder < TCPResponder
|
||||||
|
|
@ -59,11 +65,18 @@ module NanoServe
|
||||||
buf = +''
|
buf = +''
|
||||||
loop do
|
loop do
|
||||||
line = conn.readline
|
line = conn.readline
|
||||||
break if line.chomp == ''
|
|
||||||
req << line
|
req << line
|
||||||
buf << line
|
buf << line if logger.debug?
|
||||||
|
break if req.headers?
|
||||||
end
|
end
|
||||||
logger.debug "request:\n" + buf.gsub(/^/, ' ')
|
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
|
res = Response.new
|
||||||
logger.debug 'calling'
|
logger.debug 'calling'
|
||||||
|
|
@ -82,10 +95,47 @@ module NanoServe
|
||||||
@http_version = nil
|
@http_version = nil
|
||||||
@sep = nil
|
@sep = nil
|
||||||
@headers = {}
|
@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
|
end
|
||||||
|
|
||||||
def params
|
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
|
end
|
||||||
|
|
||||||
def <<(line)
|
def <<(line)
|
||||||
|
|
@ -94,10 +144,28 @@ module NanoServe
|
||||||
elsif @sep.nil?
|
elsif @sep.nil?
|
||||||
parse_header(line.chomp)
|
parse_header(line.chomp)
|
||||||
else
|
else
|
||||||
@body << line
|
parse_body(line)
|
||||||
end
|
end
|
||||||
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+)$}
|
REQ_RE = %r{(?<method>[A-Z]+)\s+(?<path>\S+)\s+(?<version>HTTP/\d+.\d+)$}
|
||||||
|
|
||||||
def parse_request(str)
|
def parse_request(str)
|
||||||
|
|
@ -111,7 +179,7 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_method(str)
|
def parse_method(str)
|
||||||
str
|
str.upcase
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_path(str)
|
def parse_path(str)
|
||||||
|
|
@ -123,13 +191,20 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_header(str)
|
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}'"
|
raise RequestError, "cannot parse header: '#{str}'"
|
||||||
end
|
end
|
||||||
|
|
||||||
@headers[m[:header]] = m[:value]
|
@headers[m[:header].downcase] = m[:value]
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_body(line)
|
||||||
|
@body << line
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -151,7 +226,7 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def body=(value)
|
def body=(value)
|
||||||
@body = value.tap { @content_length = body.bytes.count.to_s }
|
@body = value.tap { @content_length = value.bytes.count.to_s }
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|
@ -181,7 +256,7 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_length
|
def content_length
|
||||||
@content_length ||= body.bytes.count.to_s
|
@content_length || '0'
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_type
|
def content_type
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = 'nanoserve'
|
s.name = 'nanoserve'
|
||||||
s.version = '0.1.0'
|
s.version = '0.3.0'
|
||||||
s.licenses = ['3BSD']
|
s.licenses = ['3BSD']
|
||||||
s.summary = 'Listen to one-shot connections'
|
s.summary = 'Listen to one-shot connections'
|
||||||
s.authors = ['Loic Nageleisen']
|
s.authors = ['Loic Nageleisen']
|
||||||
|
|
|
||||||
|
|
@ -24,10 +24,12 @@ class TestNanoServe < MiniTest::Test
|
||||||
s.close
|
s.close
|
||||||
end
|
end
|
||||||
|
|
||||||
|
r.stop
|
||||||
|
|
||||||
assert_equal(uuid, buf)
|
assert_equal(uuid, buf)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_http_responder
|
def test_http_responder_get
|
||||||
uuid = SecureRandom.uuid.encode('UTF-8')
|
uuid = SecureRandom.uuid.encode('UTF-8')
|
||||||
uri = URI('http://localhost:2000')
|
uri = URI('http://localhost:2000')
|
||||||
|
|
||||||
|
|
@ -50,6 +52,50 @@ class TestNanoServe < MiniTest::Test
|
||||||
Net::HTTP.get(uri + "test?uuid=#{uuid}")
|
Net::HTTP.get(uri + "test?uuid=#{uuid}")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
r.stop
|
||||||
|
|
||||||
assert_equal(uuid, req.first.params['uuid'])
|
assert_equal(uuid, req.first.params['uuid'])
|
||||||
end
|
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
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue