Class: M2R::Request

Inherits:
Object
  • Object
show all
Includes:
Base, HTTP::Close, Upload
Defined in:
lib/m2r/request.rb

Overview

Abstraction over Mongrel 2 request

Constant Summary collapse

TRUE_STRINGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Set.new(%w(true yes on 1).map(&:freeze)).freeze
MONGREL2_BASE_HEADERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Set.new(%w(pattern method path query url_scheme version).map(&:upcase).map(&:freeze)).freeze
MONGREL2_UPLOAD_HEADERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Set.new(%w(x-mongrel2-upload-start x-mongrel2-upload-done).map(&:downcase).map(&:freeze)).freeze
MONGREL2_HEADERS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

(MONGREL2_BASE_HEADERS + MONGREL2_UPLOAD_HEADERS).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTTP::Close

#close?

Methods included from Upload

#body_io, #free!, #upload?, #upload_done?, #upload_path, #upload_start?

Methods included from Base

#body_io, #free!

Constructor Details

#initialize(sender, conn_id, path, http_headers, mongrel_headers, body) ⇒ Request

Returns a new instance of Request.

Parameters:

  • sender (String)

    UUID of mongrel2 origin instance

  • conn_id (String)

    Mongrel2 connection id sending this request

  • path (String)

    HTTP Path of request

  • headers (M2R::Headers)

    HTTP headers of request

  • headers (M2R::Headers)

    Additional mongrel2 headers

  • body (String)

    HTTP Body of request



43
44
45
46
47
48
49
50
51
# File 'lib/m2r/request.rb', line 43

def initialize(sender, conn_id, path, http_headers, mongrel_headers, body)
  @sender           = sender
  @conn_id          = conn_id
  @path             = path
  @http_headers     = http_headers
  @mongrel_headers  = mongrel_headers
  @body             = body
  @data             = MultiJson.load(@body) if json?
end

Instance Attribute Details

#bodyString (readonly)

Returns HTTP Body of request.

Returns:

  • (String)

    HTTP Body of request



35
36
37
# File 'lib/m2r/request.rb', line 35

def body
  @body
end

#conn_idString (readonly)

Returns Mongrel2 connection id sending this request.

Returns:

  • (String)

    Mongrel2 connection id sending this request



29
30
31
# File 'lib/m2r/request.rb', line 29

def conn_id
  @conn_id
end

#pathString (readonly)

Returns HTTP Path of request.

Returns:

  • (String)

    HTTP Path of request



32
33
34
# File 'lib/m2r/request.rb', line 32

def path
  @path
end

#senderString (readonly)

Returns UUID of mongrel2 origin instance.

Returns:

  • (String)

    UUID of mongrel2 origin instance



26
27
28
# File 'lib/m2r/request.rb', line 26

def sender
  @sender
end

Class Method Details

.parse(msg) ⇒ Request

Parse Mongrel2 request received via ZMQ message

Parameters:

  • msg (String)

    Monrel2 Request message formatted according to rules of creating it described it m2 manual.

Returns:



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/m2r/request.rb', line 60

def self.parse(msg)
  sender, conn_id, path, rest = msg.split(' ', 4)

  headers, rest = TNetstring.parse(rest)
  body, _       = TNetstring.parse(rest)
  headers       = MultiJson.load(headers)
  headers, mong = split_headers(headers)
  headers       = Headers.new headers, true
  mong          = Headers.new mong, true
  self.new(sender, conn_id, path, headers, mong, body)
end

Instance Method Details

#disconnect?true, false

Returns Internal mongrel2 message to handler issued when message delivery is not possible because the client already disconnected and there is no connection with such #conn_id.

Returns:

  • (true, false)

    Internal mongrel2 message to handler issued when message delivery is not possible because the client already disconnected and there is no connection with such #conn_id



104
105
106
# File 'lib/m2r/request.rb', line 104

def disconnect?
  json? and @data['type'] == 'disconnect'
end

#headersM2R::Headers

Returns HTTP headers.

Returns:



73
74
75
# File 'lib/m2r/request.rb', line 73

def headers
  @http_headers
end

#http_versionObject



97
98
99
# File 'lib/m2r/request.rb', line 97

def http_version
  @mongrel_headers['version']
end

#methodString

Returns HTTP method.

Returns:

  • (String)

    HTTP method



83
84
85
# File 'lib/m2r/request.rb', line 83

def method
  @mongrel_headers['method']
end

#patternString

Returns Mongrel2 pattern used to match this request.

Returns:

  • (String)

    Mongrel2 pattern used to match this request



78
79
80
# File 'lib/m2r/request.rb', line 78

def pattern
  @mongrel_headers['pattern']
end

#queryString

Returns Request query string.

Returns:

  • (String)

    Request query string



88
89
90
# File 'lib/m2r/request.rb', line 88

def query
  @mongrel_headers['query']
end

#schemeObject

return [String] URL scheme



93
94
95
# File 'lib/m2r/request.rb', line 93

def scheme
  @mongrel_headers['url_scheme'] || mongrel17_scheme
end