Class: ICAPrb::Server::ICAPRequestParser

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

Overview

This class is used to parse ICAP requests. It includes the Parser module.

Instance Method Summary collapse

Methods included from Parser

#parse_header

Constructor Details

#initialize(io) ⇒ ICAPRequestParser

initializes a new ICAP request parser params:

io

the Socket



69
70
71
# File 'lib/icaprb/server/request_parser.rb', line 69

def initialize(io)
  @io = io
end

Instance Method Details

#parseObject

parse all headers



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/icaprb/server/request_parser.rb', line 99

def parse
  line = "\r\n"
  while line == "\r\n"
    line = @io.gets
  end
  return nil unless line
  icap_req_line = parse_icap_request_line(line)
  icap_headers = {}
  until (line = @io.gets) == "\r\n"
    parsed_header = parse_header(line)
    icap_headers[parsed_header[0]] = parsed_header[1]
  end
  {request_line: icap_req_line, header: icap_headers}
end

#parse_icap_request_line(line) ⇒ Object

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

  • icap_method

    can be :req_mod, :resp_mod or :options

  • uri

    an URI

  • version

    the used version of ICAP

Raises:

  • ICAP_Parse_Error if something is invalid



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/icaprb/server/request_parser.rb', line 78

def parse_icap_request_line(line)
  str_method, str_uri,_,_,_, str_version = line.scan(/(REQMOD|RESPMOD|OPTIONS) (((icap[s]?:)?[^\s]+)|(\*)) ICAP\/([\d\.]+)/i).first
  raise ICAP_Parse_Error.new "invalid icap Method in RequestLine #{line}" if str_method.nil?
  case str_method.upcase
    when 'REQMOD'
      icap_method = :request_mod
    when 'RESPMOD'
      icap_method = :response_mod
    when 'OPTIONS'
      icap_method = :options
    else
      raise ICAP_Parse_Error.new 'The request type is not known'
  end
  uri = URI(str_uri)
  unless icap_method && uri && str_version
    raise ICAP_Parse_Error.new 'The request line is not complete.'
  end
  {icap_method: icap_method, uri: uri, version: str_version}
end