Class: ExperellaProxy::Response
- Inherits:
-
Object
- Object
- ExperellaProxy::Response
- Includes:
- Globals
- Defined in:
- lib/experella-proxy/response.rb
Overview
Response is used to store incoming (HTTP) responses and parsed data
Every Response belongs to a request Request
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
-
#<<(str) ⇒ Object
Adds data to the response object.
-
#flush ⇒ String
Returns the data in send_buffer and empties the send_buffer.
-
#flushed? ⇒ Boolean
Returns if the send_buffer is flushed? (empty).
-
#initialize(request) ⇒ Response
constructor
The constructor.
-
#reconstruct_header ⇒ Object
Reconstructs modified http response in send_buffer.
-
#update_header(hsh) ⇒ Object
Adds a hash to #header.
Methods included from Globals
#config, #connection_manager, #event, #logger
Constructor Details
#initialize(request) ⇒ Response
The constructor
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/experella-proxy/response.rb', line 15 def initialize(request) @request = request @conn = request.conn @header = {} @status_code = 500 @no_length = false # set true if no content-length or transfer-encoding given @keep_parsing = true # used for special no length case @chunked = false # if true the parsed body will be chunked @buffer = false # default is false, so incoming data will be streamed, # used for http1.0 clients and transfer-encoding chunked backend responses @send_buffer = '' @response_parser = Http::Parser.new init_http_parser end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
10 11 12 |
# File 'lib/experella-proxy/response.rb', line 10 def header @header end |
Instance Method Details
#<<(str) ⇒ Object
Adds data to the response object
data must be formatted as string
On Http::Parser::Error parsing gets interrupted and the connection closed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/experella-proxy/response.rb', line 37 def <<(str) if @keep_parsing offset = (@response_parser << str) # edge case for message without content-length and transfer encoding @conn.send_data str[offset..-1] unless @keep_parsing else @conn.send_data str end rescue Http::Parser::Error event(:response_add, :signature => @conn.signature, :error => true, :description => "parser error caused by invalid response data") # on error unbind response_parser object, so additional data doesn't get parsed anymore # # assigning a string to the parser variable, will cause incoming data to get buffered # imho this is a better solution than adding a condition for this rare error case @response_parser = "" @conn.close end |
#flush ⇒ String
Returns the data in send_buffer and empties the send_buffer
59 60 61 62 |
# File 'lib/experella-proxy/response.rb', line 59 def flush event(:response_flush, :data => @send_buffer) @send_buffer.slice!(0, @send_buffer.length) end |
#flushed? ⇒ Boolean
Returns if the send_buffer is flushed? (empty)
67 68 69 |
# File 'lib/experella-proxy/response.rb', line 67 def flushed? @send_buffer.empty? end |
#reconstruct_header ⇒ Object
Reconstructs modified http response in send_buffer
Reconstructed response must be a valid response according to the HTTP Protocol
Folded/unfolded headers will go out as they came in
Header order is determined by #header.each
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/experella-proxy/response.rb', line 79 def reconstruct_header @send_buffer = "" # start line @send_buffer << "HTTP/1.1 " @send_buffer << @status_code.to_s + ' ' @send_buffer << HTTP_STATUS_CODES.fetch(@status_code, "Status-Code unknown to experella") + "\r\n" # header fields @header.each do |key, value| key_val = key.to_s + ": " values = Array(value) values.each do |val| @send_buffer << key_val @send_buffer << val.strip @send_buffer << "\r\n" end end @send_buffer << "\r\n" # reconstruction complete event(:response_reconstruct_header, :data => @send_buffer) end |
#update_header(hsh) ⇒ Object
Adds a hash to #header
symbolizes hsh keys, duplicate key values will be overwritten with hsh values
105 106 107 108 |
# File 'lib/experella-proxy/response.rb', line 105 def update_header(hsh) hsh = hsh.reduce({}){ |memo, (k, v)| memo[k.to_sym] = v; memo } @header.update(hsh) end |