mirror of
https://github.com/lloeki/nanoserve.git
synced 2025-12-06 11:14:40 +01:00
Compare commits
No commits in common. "master" and "v0.1.0" have entirely different histories.
3 changed files with 16 additions and 137 deletions
103
lib/nanoserve.rb
103
lib/nanoserve.rb
|
|
@ -11,15 +11,14 @@ module NanoServe
|
||||||
@port = port
|
@port = port
|
||||||
@block = block
|
@block = block
|
||||||
@thr = nil
|
@thr = nil
|
||||||
@srv = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def start(y)
|
def start(y)
|
||||||
@srv = TCPServer.new(@port)
|
server = TCPServer.new(@port)
|
||||||
|
|
||||||
@thr = Thread.new do
|
@thr = Thread.new do
|
||||||
Thread.abort_on_exception = true
|
Thread.abort_on_exception = true
|
||||||
conn = @srv.accept
|
conn = server.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"
|
||||||
|
|
@ -28,9 +27,8 @@ module NanoServe
|
||||||
@block.call(conn, y)
|
@block.call(conn, y)
|
||||||
rescue EOFError
|
rescue EOFError
|
||||||
logger.debug "#{client}: disconnected"
|
logger.debug "#{client}: disconnected"
|
||||||
else
|
|
||||||
logger.debug "#{client}: closed"
|
|
||||||
ensure
|
ensure
|
||||||
|
logger.debug "#{client}: closed"
|
||||||
conn.close
|
conn.close
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -40,22 +38,18 @@ 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
|
||||||
|
|
@ -65,18 +59,11 @@ module NanoServe
|
||||||
buf = +''
|
buf = +''
|
||||||
loop do
|
loop do
|
||||||
line = conn.readline
|
line = conn.readline
|
||||||
|
break if line.chomp == ''
|
||||||
req << line
|
req << line
|
||||||
buf << line if logger.debug?
|
buf << line
|
||||||
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'
|
||||||
|
|
@ -95,47 +82,10 @@ 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
|
||||||
query.merge(form)
|
Hash[*@uri.query.split('&').map { |kv| kv.split('=') }.flatten]
|
||||||
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)
|
||||||
|
|
@ -144,28 +94,10 @@ module NanoServe
|
||||||
elsif @sep.nil?
|
elsif @sep.nil?
|
||||||
parse_header(line.chomp)
|
parse_header(line.chomp)
|
||||||
else
|
else
|
||||||
parse_body(line)
|
@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)
|
||||||
|
|
@ -179,7 +111,7 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_method(str)
|
def parse_method(str)
|
||||||
str.upcase
|
str
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_path(str)
|
def parse_path(str)
|
||||||
|
|
@ -191,20 +123,13 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_header(str)
|
def parse_header(str)
|
||||||
if str == ''
|
(@sep = '' && return) if str == ''
|
||||||
@sep = true
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
unless (m = str.match(/(?<header>[A-Za-z][-A-Za-z]*):\s+(?<value>.+)$/))
|
unless (m = str.match(/(?<header>[A-Z][-A-Za-z]*):\s+(?<value>.+)$/))
|
||||||
raise RequestError, "cannot parse header: '#{str}'"
|
raise RequestError, "cannot parse header: '#{str}'"
|
||||||
end
|
end
|
||||||
|
|
||||||
@headers[m[:header].downcase] = m[:value]
|
@headers[m[:header]] = m[:value]
|
||||||
end
|
|
||||||
|
|
||||||
def parse_body(line)
|
|
||||||
@body << line
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -226,7 +151,7 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def body=(value)
|
def body=(value)
|
||||||
@body = value.tap { @content_length = value.bytes.count.to_s }
|
@body = value.tap { @content_length = body.bytes.count.to_s }
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|
@ -256,7 +181,7 @@ module NanoServe
|
||||||
end
|
end
|
||||||
|
|
||||||
def content_length
|
def content_length
|
||||||
@content_length || '0'
|
@content_length ||= body.bytes.count.to_s
|
||||||
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.3.0'
|
s.version = '0.1.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,12 +24,10 @@ 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_get
|
def test_http_responder
|
||||||
uuid = SecureRandom.uuid.encode('UTF-8')
|
uuid = SecureRandom.uuid.encode('UTF-8')
|
||||||
uri = URI('http://localhost:2000')
|
uri = URI('http://localhost:2000')
|
||||||
|
|
||||||
|
|
@ -52,50 +50,6 @@ 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