Class: ICAPrb::Server::HTTPHeaderParser

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/icaprb/server/request_parser.rb

Overview

parses HTTP Headers

Instance Method Summary collapse

Methods included from Parser

#parse_header

Constructor Details

#initialize(io, is_request = true) ⇒ HTTPHeaderParser

initializes a new HTTPHeaderParser params:

io

the socket

is_request

value to say if it is an response or an request because the request line / status line look different



124
125
126
127
128
# File 'lib/icaprb/server/request_parser.rb', line 124

def initialize(io,is_request = true)
  @io = io
  @length_read = 0
  @is_request = is_request
end

Instance Method Details

#parseObject

parse all headers



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/icaprb/server/request_parser.rb', line 166

def parse
  if @is_request
    http_req_line = parse_http_request_line(@io.gets)
    header = RequestHeader.new(http_req_line[:http_method],http_req_line[:uri],http_req_line[:version])
  else
    http_response_line = parse_http_response_line(@io.gets)
    header = ResponseHeader.new(http_response_line[:version],http_response_line[:code])
  end
  until (line = @io.gets) == "\r\n"
    parsed_header = parse_header(line)
    header[parsed_header[0]] = parsed_header[1]
  end
  header
end

#parse_http_request_line(line) ⇒ Object

This method parses the request line and returns an Hash with the components

  • http_method

    a string

  • uri

    an URI

  • version

    the used version of HTTP

Raises:

  • HTTP_Parse_Error if something is invalid



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/icaprb/server/request_parser.rb', line 134

def parse_http_request_line(line)
  @length_read += line.length
  str_method, str_uri, str_version = line.scan(/(GET|POST|PUT|DELETE|PATCH|OPTIONS|TRACE|HEAD|CONNECT) (\S+) HTTP\/([\d\.]+)/i).first
  raise HTTP_Parse_Error.new 'invalid http Method' if str_method.nil?
  unless str_method == 'CONNECT'
    uri = URI(str_uri)
  else
    host,port = str_uri.split(':')
    uri = URI::Generic.new(nil,nil,host,port,nil,nil,"#{host}:#{port}",nil,nil).to_s
  end

  unless str_method && uri && str_version
    raise HTTP_Parse_Error.new 'The request line is not complete.'
  end
  {http_method: str_method, uri: uri, version: str_version}
end

#parse_http_response_line(line) ⇒ Object

This method parses the response line and returns an Hash with the components

  • status

    an integer

  • version

    the used version of HTTP

Raises:

  • HTTP_Parse_Error if something is invalid



154
155
156
157
158
159
160
161
162
163
# File 'lib/icaprb/server/request_parser.rb', line 154

def parse_http_response_line(line)
  @length_read += line.length
  str_version, str_code, _ = line.scan(/HTTP\/([\d\.]+) (\d+) ([A-Za-z0-9 \-]+)\r\n/i).first
  raise HTTP_Parse_Error.new 'invalid Code' if str_code.nil?
  code = str_code.to_i
  unless code && str_version
    raise HTTP_Parse_Error.new 'The request line is not complete.'
  end
  {code: code, version: str_version}
end