Class: Midori::Request

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

Overview

Request class for midori

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Init Request



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/midori/request.rb', line 20

def initialize
  @parsed = false
  @body_parsed = false
  @is_websocket = false
  @is_eventsource = false
  @parser = Http::Parser.new
  @params = {}
  @body = ''
  @parser.on_headers_complete = proc do
    @protocol = @parser.http_version
    @method = @parser.http_method
    @path = @parser.request_url
    @header = @parser.headers

    @query_string = @path.match(/\?(.*?)$/)
    @query_string = @query_string[1] unless @query_string.nil?
    @path.gsub!(/\?(.*?)$/, '')
    @method = @method.to_sym
    @parsed = true
    :stop
  end
end

Instance Attribute Details

#bodyString

request body

Returns:

  • (String)

    the current value of body



14
15
16
# File 'lib/midori/request.rb', line 14

def body
  @body
end

#body_parsedBoolean

whether the request body parsed

Returns:

  • (Boolean)

    the current value of body_parsed



14
15
16
# File 'lib/midori/request.rb', line 14

def body_parsed
  @body_parsed
end

#headerHash

request header

Returns:

  • (Hash)

    the current value of header



14
15
16
# File 'lib/midori/request.rb', line 14

def header
  @header
end

#ipString

client ip address

Returns:

  • (String)

    the current value of ip



14
15
16
# File 'lib/midori/request.rb', line 14

def ip
  @ip
end

#methodString

HTTP method

Returns:

  • (String)

    the current value of method



14
15
16
# File 'lib/midori/request.rb', line 14

def method
  @method
end

#paramsHash

params in the url

Returns:

  • (Hash)

    the current value of params



14
15
16
# File 'lib/midori/request.rb', line 14

def params
  @params
end

#parsedBoolean

whether the request header parsed

Returns:

  • (Boolean)

    the current value of parsed



14
15
16
# File 'lib/midori/request.rb', line 14

def parsed
  @parsed
end

#pathString

request path

Returns:

  • (String)

    the current value of path



14
15
16
# File 'lib/midori/request.rb', line 14

def path
  @path
end

#portInteger

client port

Returns:

  • (Integer)

    the current value of port



14
15
16
# File 'lib/midori/request.rb', line 14

def port
  @port
end

#protocolString

protocol version of HTTP request

Returns:

  • (String)

    the current value of protocol



14
15
16
# File 'lib/midori/request.rb', line 14

def protocol
  @protocol
end

#query_stringString

request query string

Returns:

  • (String)

    the current value of query_string



14
15
16
# File 'lib/midori/request.rb', line 14

def query_string
  @query_string
end

Instance Method Details

#body_parsed?Boolean

Syntactic sugar for whether a request body is parsed

Returns:

  • (Boolean)

    parsed or not



90
91
92
# File 'lib/midori/request.rb', line 90

def body_parsed?
  @body_parsed
end

#eventsource?Boolean

Syntactic sugar for whether a request is an eventsource request

Returns:

  • (Boolean)

    eventsource or not



102
103
104
# File 'lib/midori/request.rb', line 102

def eventsource?
  @is_eventsource
end

#parse(data) ⇒ nil

Init an request with String data

Parameters:

Returns:

  • (nil)

    nil



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/midori/request.rb', line 46

def parse(data)
  # Call parser if header not parsed
  if @parsed
    @body += data
  else
    offset = @parser << data
    @body += data[offset..-1] if @parsed
  end

  # Set body parsed if body reaches content length
  if (@header['Content-Length'].to_i || 0) <= @body.bytesize
    @body_parsed = true
    pre_proceed
  end
  nil
end

#parsed?Boolean

Syntactic sugar for whether a request header is parsed

Returns:

  • (Boolean)

    parsed or not



84
85
86
# File 'lib/midori/request.rb', line 84

def parsed?
  @parsed
end

#pre_proceednil

Preproceed the request after parsed

Returns:

  • (nil)

    nil



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/midori/request.rb', line 65

def pre_proceed
  # 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

  @method = @method.to_sym
  nil
end

#websocket?Boolean

Syntactic sugar for whether a request is a websocket request

Returns:

  • (Boolean)

    websocket or not



96
97
98
# File 'lib/midori/request.rb', line 96

def websocket?
  @is_websocket
end