Class: Carnivore::Http::App::Request

Inherits:
Rack::Request
  • Object
show all
Defined in:
lib/carnivore-http/app.rb

Overview

Customized request

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_) ⇒ Request

Override to setup synchronization support



128
129
130
131
132
# File 'lib/carnivore-http/app.rb', line 128

def initialize(*_)
  super
  @locker = Mutex.new
  @responder = ConditionVariable.new
end

Instance Attribute Details

#lockerMutex (readonly)

Returns:

  • (Mutex)


123
124
125
# File 'lib/carnivore-http/app.rb', line 123

def locker
  @locker
end

#responderConditionVariable (readonly)

Returns:

  • (ConditionVariable)


125
126
127
# File 'lib/carnivore-http/app.rb', line 125

def responder
  @responder
end

#response_valueResponse (readonly)

Returns:



121
122
123
# File 'lib/carnivore-http/app.rb', line 121

def response_value
  @response_value
end

Instance Method Details

#headersSmash

Returns:

  • (Smash)


166
167
168
169
170
171
172
# File 'lib/carnivore-http/app.rb', line 166

def headers
  Smash[
    env.map do |k,v|
      k.start_with?('rack.') ? nil : [k.downcase.sub(/^http_/, '').to_sym,v]
    end.compact
  ]
end

#methodSymbol

Returns:

  • (Symbol)


180
181
182
# File 'lib/carnivore-http/app.rb', line 180

def method
  request_method.to_s.downcase.to_sym
end

#remote_addrString

Returns:

  • (String)


175
176
177
# File 'lib/carnivore-http/app.rb', line 175

def remote_addr
  headers['REMOTE_ADDR']
end

#respond(code, string_or_args = '') ⇒ TrueClass, FalseClass

Respond to the request

Parameters:

  • code (String, Symbol, Integer)

    response status code

  • string_or_args (String, Hash) (defaults to: '')

Returns:

  • (TrueClass, FalseClass)


139
140
141
142
143
144
145
146
# File 'lib/carnivore-http/app.rb', line 139

def respond(code, string_or_args='')
  unless(@response_value)
    @response_value = Response.new(code, string_or_args)
    locker.synchronize{ responder.signal }
  else
    raise 'Response was already set!'
  end
end

#response(timeout = 5) ⇒ Response

Response to this request

Parameters:

  • timeout (Integer) (defaults to: 5)

    maximum number of seconds to wait

Returns:



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/carnivore-http/app.rb', line 152

def response(timeout=5)
  unless(@response_value)
    begin
      Timeout.timeout(timeout) do
        locker.synchronize{ responder.wait(locker) }
      end
    rescue Timeout::Error
      @response_value = Response.new(:internal_server_error, 'Timeout waiting for response')
    end
  end
  @response_value
end