Class: DaemonicThreads::HTTP::HttpRequest
- Inherits:
-
Object
- Object
- DaemonicThreads::HTTP::HttpRequest
- Defined in:
- lib/ruby-daemonic-threads/http/request.rb
Instance Attribute Summary collapse
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#requested_action ⇒ Object
readonly
nil or string.
-
#requested_format ⇒ Object
readonly
nil or string.
-
#requested_id ⇒ Object
readonly
nil or string.
Instance Method Summary collapse
- #[](param) ⇒ Object
- #[]=(param, value) ⇒ Object
- #body ⇒ Object
- #correct? ⇒ Boolean
- #delete? ⇒ Boolean
- #env ⇒ Object
- #error(status, body = nil) ⇒ Object
- #get? ⇒ Boolean
-
#head(*args) ⇒ Object
Based on ActionController::Base.
- #head? ⇒ Boolean
-
#initialize(request, response) ⇒ HttpRequest
constructor
initialize() happens outside daemon exception handling, so it must be lightweight.
- #log!(logger, severity = :fatal, title = nil) ⇒ Object
- #parse ⇒ Object
- #post? ⇒ Boolean
- #put? ⇒ Boolean
- #request_method ⇒ Object
-
#response(options, extra_options = {}) ⇒ Object
Based on ActionController::Base.
- #response_sent? ⇒ Boolean
-
#url_for(options = {}, extra_options = {}) ⇒ Object
Based on ActionController::Base.
Constructor Details
#initialize(request, response) ⇒ HttpRequest
initialize() happens outside daemon exception handling, so it must be lightweight. More robust processing goes to parse() Mongrel handles exceptions too, but it does not logs request details.
45 46 47 48 49 50 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 45 def initialize request, response @request = request @response = response @mutex = Mutex.new end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
52 53 54 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 52 def mutex @mutex end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
52 53 54 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 52 def params @params end |
#requested_action ⇒ Object (readonly)
nil or string
182 183 184 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 182 def requested_action @requested_action end |
#requested_format ⇒ Object (readonly)
nil or string
182 183 184 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 182 def requested_format @requested_format end |
#requested_id ⇒ Object (readonly)
nil or string
182 183 184 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 182 def requested_id @requested_id end |
Instance Method Details
#[](param) ⇒ Object
54 55 56 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 54 def [] param @params[param] end |
#[]=(param, value) ⇒ Object
58 59 60 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 58 def []= param, value @params[param] = value end |
#body ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 66 def body @mutex.synchronize do result = @request.body.read(@request.params["CONTENT_LENGTH"].to_i) @request.body.rewind if @request.body.respond_to?(:rewind) result end end |
#correct? ⇒ Boolean
184 185 186 187 188 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 184 def correct? [Mime::XML, Mime::JSON, nil].include?(requested_format) && (@requested_id.nil? || !@requested_id.blank?) && (@requested_action.nil? || !@requested_action.blank?) end |
#delete? ⇒ Boolean
210 211 212 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 210 def delete? @request.params["REQUEST_METHOD"] == "DELETE" end |
#env ⇒ Object
62 63 64 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 62 def env @request.params end |
#error(status, body = nil) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 228 def error(status, body = nil) @mutex.synchronize do return if @response_sent @response_sent = true @response.start(normalize_status_code status) do |head, out| head["Content-Type"] = "text/plain" out.write("ERROR: #{body || status}") end end end |
#get? ⇒ Boolean
198 199 200 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 198 def get? @request.params["REQUEST_METHOD"] == "GET" end |
#head(*args) ⇒ Object
Based on ActionController::Base
Return a response that has no content (merely headers). The options argument is interpreted to be a hash of header names and values. This allows you to easily return a response that consists only of significant headers:
request.head :created, :location => url_for(person)
It can also be used to return exceptional conditions:
return request.head(:method_not_allowed) unless request.post?
return request.head(:bad_request) unless valid_request?
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 255 def head(*args) if args.length > 2 raise ArgumentError, "too many arguments to head" elsif args.empty? raise ArgumentError, "too few arguments to head" end = args. status_code = normalize_status_code(args.shift || .delete(:status) || :ok) @mutex.synchronize do raise(DaemonicThreads::HTTP::DoubleResponseError, "Can only response once per request") if @response_sent @response_sent = true @response.start(status_code) do |head, out| .each do |key, value| head[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s end end end end |
#head? ⇒ Boolean
194 195 196 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 194 def head? @request.params["REQUEST_METHOD"] == "HEAD" end |
#log!(logger, severity = :fatal, title = nil) ⇒ Object
215 216 217 218 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 215 def log! logger, severity = :fatal, title = nil logger.__send__(severity) {"#{title} -- #{self.inspect rescue "EXCEPTION CALLING inspect()"}\n -- Request body: #{body.inspect rescue "EXCEPTION CALLING body()"}"} logger.flush if logger.respond_to?(:flush) end |
#parse ⇒ Object
74 75 76 77 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 74 def parse @params = parse_rails_style_params parse_path_info end |
#post? ⇒ Boolean
202 203 204 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 202 def post? @request.params["REQUEST_METHOD"] == "POST" end |
#put? ⇒ Boolean
206 207 208 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 206 def put? @request.params["REQUEST_METHOD"] == "PUT" end |
#request_method ⇒ Object
190 191 192 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 190 def request_method @request.params["REQUEST_METHOD"] end |
#response(options, extra_options = {}) ⇒ Object
Based on ActionController::Base
response object||string [, options] response :object => object||string [, options] – Then response is hash response :xml => object||string [, options] response :json => object||string [, options] response :text => string [, options]
Options
:status
:location
:callback
:content_type
TODO: response :update do TODO: response :js => TODO: response :file => filename [, options]
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 298 def response , = {} if .kind_of?(Hash) .update() if data = [:xml] format = Mime::XML elsif data = [:json] format = Mime::JSON elsif data = [:object] format = requested_format || Mime::XML elsif data = [:text] data = data.to_s format = Mime::HTML else raise "You must response with something!" end else data = = if requested_format format = requested_format elsif data.kind_of?(String) format = Mime::HTML else format = Mime::XML end end raise "You force response format `#{format}' but user requests `#{requested_format}'" if format && requested_format && format != requested_format case format when Mime::XML data = data.to_xml unless data.kind_of?(String) when Mime::JSON data = data.to_json unless data.kind_of?(String) data = "#{[:callback]}(#{data})" unless [:callback].blank? end status_code = normalize_status_code([:status] || :ok) if location = [:location] location = url_for(location) unless location.kind_of?(String) end @mutex.synchronize do raise(DaemonicThreads::HTTP::DoubleResponseError, "Can only response once per request") if @response_sent @response_sent = true @response.start(status_code) do |head, out| head["Location"] = location if location head["Content-Type"] = ([:content_type] || format).to_s unless head? Rails.logger.debug { "HTTP RESPONSE:\n#{data}" } out.write(data) end end end end |
#response_sent? ⇒ Boolean
221 222 223 224 225 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 221 def response_sent? @mutex.synchronize do @response_sent end end |
#url_for(options = {}, extra_options = {}) ⇒ Object
Based on ActionController::Base
url_for [Object respond_to?(:id)], options
:controller -- full absolute path, can be found in .uri()
:id
:action
:format
Without any options it returns full URL of current controller
374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/ruby-daemonic-threads/http/request.rb', line 374 def url_for = {}, = {} = {:id => .id} unless .kind_of?(Hash) .update() url = "http://#{env["HTTP_HOST"]}" url += [:controller] ? [:controller] : env["SCRIPT_NAME"] url += "/#{[:id]}" if [:id] url += "/#{[:action]}" if [:action] url += ".#{[:format]}" if [:format] url end |