Class: Rakie::HttpRequest
- Inherits:
-
Object
- Object
- Rakie::HttpRequest
- Includes:
- Proto
- Defined in:
- lib/rakie/http_proto.rb
Defined Under Namespace
Classes: Head
Constant Summary collapse
- PARSE_HEAD =
PARSE_BEGIN
- PARSE_HEADERS =
1
- PARSE_CONTENT =
2
Constants included from Proto
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#head ⇒ Object
Returns the value of attribute head.
-
#headers ⇒ Object
Returns the value of attribute headers.
Instance Method Summary collapse
- #deserialize(source) ⇒ Integer
-
#initialize ⇒ HttpRequest
constructor
A new instance of HttpRequest.
- #parse_source_content(source) ⇒ Object
- #parse_source_head(source) ⇒ Object
- #parse_source_header_item(header) ⇒ Object
- #parse_source_headers(source) ⇒ Object
- #serialize ⇒ String
Methods included from Proto
#parse, #parse_offset, #parse_offset=, #parse_state, #parse_state=, #parse_status, #to_s
Constructor Details
#initialize ⇒ HttpRequest
Returns a new instance of HttpRequest.
27 28 29 30 31 |
# File 'lib/rakie/http_proto.rb', line 27 def initialize @head = Head.new @headers = {} @content = '' end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
11 12 13 |
# File 'lib/rakie/http_proto.rb', line 11 def content @content end |
#head ⇒ Object
Returns the value of attribute head.
11 12 13 |
# File 'lib/rakie/http_proto.rb', line 11 def head @head end |
#headers ⇒ Object
Returns the value of attribute headers.
11 12 13 |
# File 'lib/rakie/http_proto.rb', line 11 def headers @headers end |
Instance Method Details
#deserialize(source) ⇒ Integer
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rakie/http_proto.rb', line 121 def deserialize(source) current_state = parse_state case current_state when PARSE_HEAD return parse_source_head(source) when PARSE_HEADERS return parse_source_headers(source) when PARSE_CONTENT return parse_source_content(source) end end |
#parse_source_content(source) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rakie/http_proto.rb', line 97 def parse_source_content(source) len = headers["content-length"] offset = parse_offset if len == nil return ParseStatus::COMPLETE end len = len.to_i if source.length >= len + offset self.content = source[offset .. (offset + len - 1)] self.parse_state = PARSE_HEAD self.parse_offset = offset + len Log.debug("Rakie::HttpRequest parse source content ok") return ParseStatus::COMPLETE end return ParseStatus::PENDING end |
#parse_source_head(source) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/rakie/http_proto.rb', line 34 def parse_source_head(source) offset = parse_offset if eol_offset = source.index("\r\n") head_s = source[0 .. eol_offset] head_method, head_path, head_version = head_s.split(' ') head.method = head_method head.path = head_path head.version = head_version self.parse_state = PARSE_HEADERS self.parse_offset = offset + 2 Log.debug("Rakie::HttpRequest parse source head ok") return ParseStatus::CONTINUE end return ParseStatus::PENDING end |
#parse_source_header_item(header) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rakie/http_proto.rb', line 55 def parse_source_header_item(header) if semi_offset = header.index(':') return [ header[0 .. (semi_offset - 1)].downcase, header[(semi_offset + 1) .. -1].strip ] end return nil end |
#parse_source_headers(source) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rakie/http_proto.rb', line 67 def parse_source_headers(source) offset = parse_offset while eol_offset = source.index("\r\n", offset) header = source[offset .. (eol_offset - 1)] if header.length == 0 self.parse_state = PARSE_CONTENT self.parse_offset = eol_offset + 2 Log.debug("Rakie::HttpRequest parse source header done") return ParseStatus::CONTINUE end header_key, header_value = self.parse_source_header_item(header) if header_key headers[header_key] = header_value Log.debug("Rakie::HttpRequest parse source #{header_key}, #{header_value} header ok") end offset = eol_offset + 2 end self.parse_offset = offset return ParseStatus::PENDING end |
#serialize ⇒ String
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/rakie/http_proto.rb', line 137 def serialize data = "" data += "#{head.method} #{head.path} #{head.version}" data += "\r\n" headers_list = [] headers.each do |k, v| headers_list << "#{k}: #{v}" end data += headers_list.join("\r\n") data += "\r\n\r\n" data += content end |