Skip to content

Commit

Permalink
Fix Rack adapter (close #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
noteflakes committed Jul 26, 2021
1 parent df3b011 commit ab7b276
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 56 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PATH
http_parser.rb (~> 0.6.0)
msgpack (~> 1.4.2)
polyphony (~> 0.64)
qeweney (~> 0.10.0)
qeweney (~> 0.11)
rack (>= 2.0.8, < 2.3.0)
websocket (~> 1.2.8)

Expand Down Expand Up @@ -52,7 +52,7 @@ GEM
msgpack (1.4.2)
multipart-post (2.1.1)
polyphony (0.64)
qeweney (0.10)
qeweney (0.11)
escape_utils (~> 1.2.1)
rack (2.2.3)
rake (12.3.3)
Expand Down
55 changes: 2 additions & 53 deletions lib/tipi/rack_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,7 @@
require 'rack'

module Tipi
module RackAdapter
# Implements a rack input stream:
# https://www.rubydoc.info/github/rack/rack/master/file/SPEC#label-The+Input+Stream
class InputStream
def initialize(request)
@request = request
end

def gets; end

def read(length = nil, outbuf = nil); end

def each(&block)
@request.each_chunk(&block)
end

def rewind; end
end

module RackAdapter
class << self
def run(app)
->(req) { respond(req, app.(env(req))) }
Expand All @@ -32,41 +14,8 @@ def load(path)
instance_eval(src, path, 1)
end

RACK_ENV = {
'SCRIPT_NAME' => '',
'rack.version' => Rack::VERSION,
'SERVER_PORT' => '80', # ?
'rack.url_scheme' => 'http', # ?
'rack.errors' => STDERR, # ?
'rack.multithread' => false,
'rack.run_once' => false,
'rack.hijack?' => false,
'rack.hijack' => nil,
'rack.hijack_io' => nil,
'rack.session' => nil,
'rack.logger' => nil,
'rack.multipart.buffer_size' => nil,
'rack.multipar.tempfile_factory' => nil
}

def env(request)
Hash.new do |h, k|
h[k] = env_value_from_request(request, k)
end
end

HTTP_HEADER_RE = /^HTTP_(.+)$/.freeze

def env_value_from_request(request, key)
case key
when 'REQUEST_METHOD' then request.method
when 'PATH_INFO' then request.path
when 'QUERY_STRING' then request.query_string || ''
when 'SERVER_NAME' then request.headers['host']
when 'rack.input' then InputStream.new(request)
when HTTP_HEADER_RE then request.headers[$1.downcase]
else RACK_ENV[key]
end
Qeweney.rack_env_from_request(request)
end

def respond(request, (status_code, headers, body))
Expand Down
2 changes: 1 addition & 1 deletion tipi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |s|
s.executables = ['tipi']

s.add_runtime_dependency 'polyphony', '~>0.64'
s.add_runtime_dependency 'qeweney', '~>0.10.0'
s.add_runtime_dependency 'qeweney', '~>0.11'

s.add_runtime_dependency 'http_parser.rb', '~>0.6.0'
s.add_runtime_dependency 'http-2', '~>0.10.0'
Expand Down

0 comments on commit ab7b276

Please sign in to comment.