class Midori::Request

Request class for midori @attr [String] ip client ip address @attr [Integer] port client port @attr [String] protocol protocol version of HTTP request @attr [Symbol] method HTTP method @attr [String] path request path @attr [Hash] query_param parameter parsed from query string @attr [String | nil] query_string request query string @attr [Hash] header request header @attr [String] body request body @attr [Hash] cookie cookie hash coming from request @attr [Boolean] parsed whether the request header parsed @attr [Boolean] body_parsed whether the request body parsed @attr [Hash] params params in the url

Attributes

body[RW]
body_parsed[RW]
header[RW]
ip[RW]
method[RW]
params[RW]
parsed[RW]
path[RW]
port[RW]
protocol[RW]
query_params[RW]
query_string[RW]

Public Class Methods

new() click to toggle source

Init Request

# File lib/midori/request.rb, line 22
def initialize
  @header = {}
  @parsed = false
  @body_parsed = false
  @is_websocket = false
  @is_eventsource = false
  @parser = Http::Parser.new
  @params = {}
  @query_params = Hash.new(Array.new)
  @cookie = {}
  @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(/\?(.*?)$/)
    unless @query_string.nil?
      @query_string = @query_string[1]
      @query_params = CGI::parse(@query_string)
    end

    @cookie = CGI::Cookie.parse(@header['Cookie']) unless @header['Cookie'].nil?
    @path.gsub!(/\?(.*?)$/, '')
    @method = @method.to_sym
    @parsed = true
    :stop
  end
end

Public Instance Methods

body_parsed?() click to toggle source

Syntactic sugar for whether a request body is parsed @return [Boolean] parsed or not

# File lib/midori/request.rb, line 100
def body_parsed?
  @body_parsed
end
eventsource?() click to toggle source

Syntactic sugar for whether a request is an eventsource request @return [Boolean] eventsource or not

# File lib/midori/request.rb, line 112
def eventsource?
  @is_eventsource
end
parse(data) click to toggle source

Init an request with String data @param [String] data @return [nil] nil

# File lib/midori/request.rb, line 56
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 @parsed && (@header['Content-Length'].to_i || 0) <= @body.bytesize
    @body_parsed = true
    pre_proceed
  end
  nil
end
parsed?() click to toggle source

Syntactic sugar for whether a request header is parsed @return [Boolean] parsed or not

# File lib/midori/request.rb, line 94
def parsed?
  @parsed
end
pre_proceed() click to toggle source

Preproceed the request after parsed @return [nil] nil

# File lib/midori/request.rb, line 75
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?() click to toggle source

Syntactic sugar for whether a request is a websocket request @return [Boolean] websocket or not

# File lib/midori/request.rb, line 106
def websocket?
  @is_websocket
end