Skip to content

Commit a27692b

Browse files
committed
adding some docs
1 parent 41641f6 commit a27692b

5 files changed

Lines changed: 76 additions & 25 deletions

File tree

‎lib/goliath/connection.rb‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
require 'goliath/env'
33

44
module Goliath
5+
# The Goliath::Connection class handles sending and receiving data
6+
# from the client.
7+
#
58
# @private
69
class Connection < EM::Connection
710
include Constants
@@ -80,6 +83,5 @@ def remote_address
8083
rescue Exception
8184
nil
8285
end
83-
8486
end
8587
end

‎lib/goliath/constants.rb‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module Goliath
2+
# Constants used by the system to access data.
23
module Constants
34
INITIAL_BODY = ''
45
# Force external_encoding of request's body to ASCII_8BIT
@@ -44,7 +45,5 @@ module Constants
4445
CONNECTION = 'CONNECTION'
4546

4647
GOLIATH_ENV = 'goliath.env'
47-
48-
HOST_PORT_REGEXP = /(?<host>.*?):(?<port>.*)/
4948
end
5049
end

‎lib/goliath/request.rb‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module Goliath
2+
# Goliath::Request is responsible for processing a request and returning
3+
# the result back to the client.
4+
#
25
# @private
36
class Request
47
include EM::Deferrable
@@ -31,6 +34,9 @@ def initialize(app, conn, env)
3134
# Invoked by connection when header parsing is complete.
3235
# This method is invoked only once per request.
3336
#
37+
# @param h [Hash] Request headers
38+
# @param parser [Http::Parser] The parsed used to parse the request
39+
# @return [Nil]
3440
def parse_header(h, parser)
3541
h.each do |k, v|
3642
@env[HTTP_PREFIX + k.gsub('-','_').upcase] = v
@@ -50,23 +56,29 @@ def parse_header(h, parser)
5056
# Invoked by connection when new body data is
5157
# parsed from the existing TCP stream.
5258
#
53-
# Note: in theory, we can make this stream the
59+
# @note In theory, we can make this stream the
5460
# data into the processing step for async
5561
# uploads, etc. This would also require additional
5662
# callbacks for headers, etc.. Maybe something to
5763
# explore later.
5864
#
65+
# @param data [String] The received data
66+
# @return [Nil]
5967
def parse(data)
6068
@body << data
6169
end
6270

71+
# Called to determine if the request has received all data from the client
72+
#
73+
# @return [Boolean] True if all data is received, false otherwise
6374
def finished?
6475
@state == :finished
6576
end
6677

6778
# Invoked by connection when upstream client
6879
# terminates the current TCP session.
6980
#
81+
# @return [Nil]
7082
def close
7183
@response.close rescue nil
7284
@env[ASYNC_CLOSE].succeed if @env[ASYNC_CLOSE]
@@ -78,6 +90,7 @@ def close
7890
# an immediate response is served, or an async
7991
# response is indicated.
8092
#
93+
# @return [Nil]
8194
def process
8295
begin
8396
@state = :finished
@@ -105,6 +118,8 @@ def process
105118
# so the actual time to serve both requests in scenario
106119
# above, should be ~1s + data transfer time.
107120
#
121+
# @param results [Array] The status, headers and body to return to the client
122+
# @return [Nil]
108123
def post_process(results)
109124
begin
110125
status, headers, body = results
@@ -131,24 +146,30 @@ def post_process(results)
131146

132147
private
133148

134-
def server_exception(e)
135-
@env[LOGGER].error("#{e.message}\n#{e.backtrace.join("\n")}")
136-
post_process([500, {}, 'An error happened'])
137-
end
149+
# Handles logging server exceptions
150+
#
151+
# @param e [Exception] The exception to log
152+
# @return [Nil]
153+
def server_exception(e)
154+
@env[LOGGER].error("#{e.message}\n#{e.backtrace.join("\n")}")
155+
post_process([500, {}, 'An error happened'])
156+
end
138157

139-
def keep_alive
140-
case @env[HTTP_VERSION]
141-
# HTTP 1.1: all requests are persistent requests, client
142-
# must send a Connection:close header to indicate otherwise
143-
when '1.1' then
144-
(@env[HTTP_PREFIX + CONNECTION].downcase != 'close') rescue true
145-
146-
# HTTP 1.0: all requests are non keep-alive, client must
147-
# send a Connection: Keep-Alive to indicate otherwise
148-
when '1.0' then
149-
(@env[HTTP_PREFIX + CONNECTION].downcase == 'keep-alive') rescue false
150-
end
158+
# Used to determine if the connection should be kept open
159+
#
160+
# @return [Boolean] True to keep the connection open, false otherwise
161+
def keep_alive
162+
case @env[HTTP_VERSION]
163+
# HTTP 1.1: all requests are persistent requests, client
164+
# must send a Connection:close header to indicate otherwise
165+
when '1.1' then
166+
(@env[HTTP_PREFIX + CONNECTION].downcase != 'close') rescue true
167+
168+
# HTTP 1.0: all requests are non keep-alive, client must
169+
# send a Connection: Keep-Alive to indicate otherwise
170+
when '1.0' then
171+
(@env[HTTP_PREFIX + CONNECTION].downcase == 'keep-alive') rescue false
151172
end
152-
173+
end
153174
end
154175
end

‎lib/goliath/response.rb‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,52 @@
44
require 'time'
55

66
module Goliath
7+
# Goliath::Response holds the information that will be sent back
8+
# to the client.
9+
#
710
# @private
811
class Response
9-
attr_accessor :status, :headers, :body
12+
# The status code to send
13+
attr_accessor :status
1014

11-
SERVER = 'Server'.freeze
12-
DATE = 'Date'.freeze
15+
# The headers to send
16+
attr_accessor :headers
1317

14-
STREAMING = :goliath_stream_response
18+
# The body to send
19+
attr_accessor :body
20+
21+
SERVER = 'Server'.freeze
22+
DATE = 'Date'.freeze
23+
24+
# Used to signal that a response is a streaming response
25+
STREAMING = :goliath_stream_response
1526

1627
def initialize
1728
@headers = Goliath::Headers.new
1829
@status = 200
1930
end
2031

32+
# Creates the header line for the response
33+
#
34+
# @return [String] The HTTP header line
2135
def head
2236
"HTTP/1.1 #{status} #{HTTP_STATUS_CODES[status.to_i]}\r\n"
2337
end
2438

39+
# Creats the headers to be returned to the client
40+
#
41+
# @return [String] The HTTP headers
2542
def headers_output
2643
headers[SERVER] = Goliath::Request::SERVER
2744
headers[DATE] = Time.now.httpdate
2845

2946
"#{headers.to_s}\r\n"
3047
end
3148

49+
# Sets a set of key value pairs into the headers
50+
#
51+
# @param key_value_pairs [Hash] The key/value pairs to set as headers
52+
# @return [Nil]
3253
def headers=(key_value_pairs)
3354
return unless key_value_pairs
3455

@@ -47,10 +68,17 @@ def headers=(key_value_pairs)
4768
end
4869
end
4970

71+
# Used to signal that the response is closed
72+
#
73+
# @return [Nil]
5074
def close
5175
body.close if body.respond_to?(:close)
5276
end
5377

78+
# Yields each portion of the response
79+
#
80+
# @yields [String] The header line, headers and body content
81+
# @return [Nil]
5482
def each
5583
yield head
5684
yield headers_output

‎lib/goliath/version.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module Goliath
2+
# The current version of Goliath
23
VERSION = '0.9.0'
34
end

0 commit comments

Comments
 (0)