Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
Factor request/response classes into appropriate folders
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Oct 20, 2013
1 parent f4f73b2 commit 49b7678
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 88 deletions.
4 changes: 0 additions & 4 deletions lib/reel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@
require 'reel/mixins'
require 'reel/connection'
require 'reel/logger'
require 'reel/request_info'
require 'reel/request'
require 'reel/request_body'
require 'reel/request_parser'
require 'reel/response'
require 'reel/response_writer'
require 'reel/server'
require 'reel/ssl_server'
require 'reel/websocket'
Expand Down
6 changes: 5 additions & 1 deletion lib/reel/request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'forwardable'

require 'reel/request/body'
require 'reel/request/info'
require 'reel/request/parser'

module Reel
class Request
extend Forwardable
Expand All @@ -18,7 +22,7 @@ def initialize(request_info, connection = nil)
@connection = connection
@finished = false
@buffer = ""
@body = RequestBody.new(self)
@body = Body.new(self)
@finished_read = false
@websocket = nil
@response_writer = Response::Writer.new(connection.socket)
Expand Down
64 changes: 64 additions & 0 deletions lib/reel/request/body.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module Reel
class Request
# Represents the bodies of Requests
class Body
include Enumerable

def initialize(request)
@request = request
@streaming = nil
@contents = nil
end

# Read exactly the given amount of data
def read(length)
stream!
@request.read(length)
end

# Read up to length bytes, but return any data that's available
def readpartial(length = nil)
stream!
@request.readpartial(length)
end

# Iterate over the body, allowing it to be enumerable
def each
while chunk = readpartial
yield chunk
end
end

# Eagerly consume the entire body as a string
def to_s
return @contents if @contents
raise StateError, "body is being streamed" unless @streaming.nil?

begin
@streaming = false
@contents = ""
while chunk = @request.readpartial
@contents << chunk
end
rescue
@contents = nil
raise
end

@contents
end

# Easier to interpret string inspect
def inspect
"#<#{self.class}:#{object_id.to_s(16)} @streaming=#{!!@streaming}>"
end

# Assert that the body is actively being streamed
def stream!
raise StateError, "body has already been consumed" if @streaming == false
@streaming = true
end
private :stream!
end
end
end
21 changes: 21 additions & 0 deletions lib/reel/request/info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Reel
class Request
class Info
attr_reader :http_method, :url, :http_version, :headers

def initialize(http_method, url, http_version, headers)
@http_method = http_method
@url = url
@http_version = http_version
@headers = headers
end

UPGRADE = 'Upgrade'.freeze
WEBSOCKET = 'websocket'.freeze

def websocket_request?
headers[UPGRADE] && headers[UPGRADE].downcase == WEBSOCKET
end
end
end
end
2 changes: 1 addition & 1 deletion lib/reel/request_parser.rb → lib/reel/request/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def readpartial(size = @buffer_size)
# HTTP::Parser callbacks
#
def on_headers_complete(headers)
info = RequestInfo.new(http_method, url, http_version, headers)
info = Info.new(http_method, url, http_version, headers)
req = Request.new(info, connection)

if @currently_reading
Expand Down
62 changes: 0 additions & 62 deletions lib/reel/request_body.rb

This file was deleted.

19 changes: 0 additions & 19 deletions lib/reel/request_info.rb

This file was deleted.

2 changes: 2 additions & 0 deletions lib/reel/response.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'reel/response/writer'

module Reel
class Response
include HTTP::Header
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'spec_helper'

describe Reel::Response::Writer do
let(:fixture_path) { File.expand_path("../../fixtures/example.txt", __FILE__) }
let(:fixture_path) { File.expand_path("../../../fixtures/example.txt", __FILE__) }
let(:expected_response) { "HTTP/1.1 200 OK\r\nContent-Length: 56\r\n\r\n#{File.read(fixture_path)}" }

it "streams static files" do
with_socket_pair do |socket, peer|
writer = described_class.new(socket)
Expand Down

0 comments on commit 49b7678

Please sign in to comment.