Class: ExperellaProxy::Request

Inherits:
Object
  • Object
show all
Includes:
Globals
Defined in:
lib/experella-proxy/request.rb

Overview

Request is used to store incoming (HTTP) requests and parsed data

Every Request belongs to a client Connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Globals

#config, #connection_manager, #event, #logger

Constructor Details

#initialize(conn) ⇒ Request

The constructor

Parameters:

  • conn (Connection)

    Connection the request belongs to



16
17
18
19
20
21
22
23
24
# File 'lib/experella-proxy/request.rb', line 16

def initialize(conn)
  @conn = conn
  @header = {}
  @chunked = false # if true the parsed body will be chunked
  @uri = {} # contains port, path and query information for faster backend selection
  @keep_alive = true
  @send_buffer = ''
  @response = Response.new(self)
end

Instance Attribute Details

#chunkedObject

Returns the value of attribute chunked.



10
11
12
# File 'lib/experella-proxy/request.rb', line 10

def chunked
  @chunked
end

#connObject (readonly)

Returns the value of attribute conn.



11
12
13
# File 'lib/experella-proxy/request.rb', line 11

def conn
  @conn
end

#headerObject (readonly)

Returns the value of attribute header.



11
12
13
# File 'lib/experella-proxy/request.rb', line 11

def header
  @header
end

#keep_aliveObject

Returns the value of attribute keep_alive.



10
11
12
# File 'lib/experella-proxy/request.rb', line 10

def keep_alive
  @keep_alive
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/experella-proxy/request.rb', line 11

def response
  @response
end

#uriObject (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/experella-proxy/request.rb', line 11

def uri
  @uri
end

Instance Method Details

#<<(str) ⇒ Object

Adds data to the request object

data must be formatted as string

Parameters:

  • str (String)

    data as string



31
32
33
# File 'lib/experella-proxy/request.rb', line 31

def <<(str)
  @send_buffer << str
end

#add_uri(hsh) ⇒ Object

Adds a hash with uri information to #uri

duplicate key values will be overwritten with hsh values

Parameters:

  • hsh (Hash)

    hash with keys :port :path :query containing URI information



40
41
42
43
# File 'lib/experella-proxy/request.rb', line 40

def add_uri(hsh)
  @uri.update(hsh)
  event(:request_add_uri, :uri => hsh)
end

#flushString

Returns the data in send_buffer and empties the send_buffer

Returns:

  • (String)

    data to send



48
49
50
# File 'lib/experella-proxy/request.rb', line 48

def flush
  @send_buffer.slice!(0, @send_buffer.length)
end

#flushed?Boolean

Returns if the send_buffer is flushed? (empty)

Returns:

  • (Boolean)


55
56
57
# File 'lib/experella-proxy/request.rb', line 55

def flushed?
  @send_buffer.empty?
end

#reconstruct_headerObject

Reconstructs modified http request in send_buffer

Reconstructed request must be a valid request according to the HTTP Protocol

Folded/unfolded headers will go out as they came in

First Header after Startline will always be “Host: ”, after that order is determined by #header.each



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
# File 'lib/experella-proxy/request.rb', line 67

def reconstruct_header
  # split send_buffer into header and body part
  buf = @send_buffer.split(/\r\n\r\n/, 2) unless flushed?
  @send_buffer = ""
  # start line
  @send_buffer << @header[:http_method] + ' '
  @send_buffer << @header[:request_url] + ' '
  @send_buffer << "HTTP/1.1\r\n"
  @send_buffer << "Host: " + @header[:Host] + "\r\n" # add Host first for better header readability
  # header fields
  @header.each do |key, value|
    unless  key == :http_method || key == :request_url || key == :http_version || key == :Host # exclude startline parameters
      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
  end
  @send_buffer << "\r\n"
  # reconstruction complete
  @send_buffer << buf[1] unless buf.nil? # append buffered body
  event(:request_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

Parameters:

  • hsh (Hash)

    hash with HTTP header Key:Value pairs



99
100
101
102
# File 'lib/experella-proxy/request.rb', line 99

def update_header(hsh)
  hsh = hsh.reduce({}) { |memo, (k, v)| memo[k.to_sym] = v; memo }
  @header.update(hsh)
end