Class: ProconBypassMan::ProconDisplay::HttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/procon_bypass_man/procon_display/http_request.rb

Overview

NOTE Support GET only

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers) ⇒ HttpRequest

Returns a new instance of HttpRequest.



16
17
18
# File 'lib/procon_bypass_man/procon_display/http_request.rb', line 16

def initialize(headers)
  @headers = headers
end

Class Method Details

.parse(conn) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/procon_bypass_man/procon_display/http_request.rb', line 4

def self.parse(conn)
  headers = {}
  loop do
    line = conn.gets("\n")&.strip
    break if line.nil? || line.strip.empty?
    key, value = line.split(/:\s/, 2)
    headers[key] = value
  end

  new(headers)
end

Instance Method Details

#pathObject



20
21
22
23
24
25
# File 'lib/procon_bypass_man/procon_display/http_request.rb', line 20

def path
  request_method_and_path = @headers.detect { |key, _value| key.start_with?("GET") }.first
  if request_method_and_path =~ /(?:GET) ([^ ]+)/ && (path = $1)
    return path
  end
end

#to_hashObject



27
28
29
# File 'lib/procon_bypass_man/procon_display/http_request.rb', line 27

def to_hash
  { "PATH" => path }
end