Class: Midori::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/em-midori/request.rb

Overview

Request class for midori

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Init Request


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/em-midori/request.rb', line 17

def initialize
  @parsed = false
  @is_websocket = false
  @is_eventsource = false
  @parser = Http::Parser.new
  @parser.on_headers_complete = proc do
    @protocol = @parser.http_version
    @method = @parser.http_method
    @path = @parser.request_url
    @header = @parser.headers
    :stop
  end
end

Instance Attribute Details

#bodyString

request body

Returns:

  • (String)

    the current value of body


11
12
13
# File 'lib/em-midori/request.rb', line 11

def body
  @body
end

#headerHash

request header

Returns:

  • (Hash)

    the current value of header


11
12
13
# File 'lib/em-midori/request.rb', line 11

def header
  @header
end

#ipString

client ip address

Returns:

  • (String)

    the current value of ip


11
12
13
# File 'lib/em-midori/request.rb', line 11

def ip
  @ip
end

#methodObject

Returns the value of attribute method


12
13
14
# File 'lib/em-midori/request.rb', line 12

def method
  @method
end

#parsedBoolean

whether the request parsed

Returns:

  • (Boolean)

    the current value of parsed


11
12
13
# File 'lib/em-midori/request.rb', line 11

def parsed
  @parsed
end

#pathString

request path

Returns:

  • (String)

    the current value of path


11
12
13
# File 'lib/em-midori/request.rb', line 11

def path
  @path
end

#portFixnum

client port

Returns:

  • (Fixnum)

    the current value of port


11
12
13
# File 'lib/em-midori/request.rb', line 11

def port
  @port
end

#protocolString

protocol version of HTTP request

Returns:

  • (String)

    the current value of protocol


11
12
13
# File 'lib/em-midori/request.rb', line 11

def protocol
  @protocol
end

#query_stringString

request query string

Returns:

  • (String)

    the current value of query_string


11
12
13
# File 'lib/em-midori/request.rb', line 11

def query_string
  @query_string
end

Instance Method Details

#eventsource?Boolean

Syntatic sugur for whether a request is an eventsource request

Returns:

  • (Boolean)

    eventsource or not


69
70
71
# File 'lib/em-midori/request.rb', line 69

def eventsource?
  @is_eventsource
end

#parse(data) ⇒ Object

Init an request with StringIO data


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/em-midori/request.rb', line 32

def parse(data)

  offset = @parser << data
  @body = data[offset..-1]
  @query_string = @path.match(/\?(.*?)$/)
  @query_string = @query_string[1] unless @query_string.nil?
  @path.gsub!(/\?(.*?)$/, '')

  # Deal with WebSocket
  if @header['Upgrade'] == 'websocket' && @header['Connection'] == 'Upgrade'
    @method = 'WEBSOCKET'
    @is_websocket = true
  end

  # Deal with EventSource
  if @header['Accept'] == 'text/event-stream'
    @method = 'EVENTSOURCE'
    @is_eventsource = true
  end

  @parsed = true
end

#parsed?Boolean

Syntatic sugur for whether a request is parsed

Returns:

  • (Boolean)

    parsed or not


57
58
59
# File 'lib/em-midori/request.rb', line 57

def parsed?
  @parsed
end

#websocket?Boolean

Syntatic sugur for whether a request is a websocket request

Returns:

  • (Boolean)

    websocket or not


63
64
65
# File 'lib/em-midori/request.rb', line 63

def websocket?
  @is_websocket
end