lib/reel/request.rb in reel-0.1.0 vs lib/reel/request.rb in reel-0.2.0.pre
- old
+ new
@@ -1,25 +1,63 @@
+require 'uri'
+
module Reel
class Request
attr_accessor :method, :version, :url, :headers
- METHODS = [:get, :head, :post, :put, :delete, :trace, :options, :connect, :patch]
-
+
+ def self.read(connection)
+ parser = connection.parser
+
+ begin
+ data = connection.socket.readpartial(Connection::BUFFER_SIZE)
+ parser << data
+ end until parser.headers
+
+ headers = {}
+ parser.headers.each do |field, value|
+ headers[Http.canonicalize_header(field)] = value
+ end
+
+ upgrade = headers['Upgrade']
+ if upgrade && upgrade.downcase == 'websocket'
+ WebSocket.new(connection.socket, parser.url, headers)
+ else
+ Request.new(parser.http_method, parser.url, parser.http_version, headers, connection)
+ end
+ end
+
def initialize(method, url, version = "1.1", headers = {}, connection = nil)
@method = method.to_s.downcase.to_sym
- raise UnsupportedArgumentError, "unknown method: #{method}" unless METHODS.include? @method
-
+ raise UnsupportedArgumentError, "unknown method: #{method}" unless Http::METHODS.include? @method
+
@url, @version, @headers, @connection = url, version, headers, connection
end
-
+
def [](header)
@headers[header]
end
-
+
+ def uri
+ @uri ||= URI(url)
+ end
+
+ def path
+ uri.path
+ end
+
+ def query_string
+ uri.query
+ end
+
+ def fragment
+ uri.fragment
+ end
+
def body
@body ||= begin
raise "no connection given" unless @connection
-
+
body = "" unless block_given?
while (chunk = @connection.readpartial)
if block_given?
yield chunk
else
@@ -28,6 +66,6 @@
end
body unless block_given?
end
end
end
-end
\ No newline at end of file
+end