Class: ICAPrb::Server::RequestParser

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

Overview

The request parser uses the ICAP and HTTP parsers to parse the complete request. It is the main parser used by the server which gets the socket to read the ICAP headers which are sent by the client. Depending on the headers and the values we got, we will decide, which other parsers we need and how to read it. the parsed request will be returned and depending on the data, the server will decide what it will do with the request.

Instance Method Summary collapse

Constructor Details

#initialize(io, ip, server) ⇒ RequestParser

create a new instance of a RequestParser params:

io

a socket to communicate

ip

the peer ip address

server

the instance of the server which uses the parser to get the service names.



195
196
197
198
199
# File 'lib/icaprb/server/request_parser.rb', line 195

def initialize(io,ip,server)
  @io = io
  @ip = ip
  @server = server
end

Instance Method Details

#parseObject

Parses the complete request and returns the parsed result. It will return the parsed result.

if an Encapsulated header is set, it will also parse the encapsulated HTTP header and the body if available



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/icaprb/server/request_parser.rb', line 206

def parse
  # parse ICAP headers
  icap_parser = ICAPRequestParser.new(@io)
  icap_data = icap_parser.parse
  return nil unless icap_data
  if icap_data[:header]['Encapsulated']
    encapsulation = icap_data[:header]['Encapsulated']
    encapsulated_parts = encapsulation.split(',').map do |part|
      part.split('=').map(&:strip)
    end
  else
    encapsulated_parts = []
  end
  parsed_data = {icap_data: icap_data, encapsulated: encapsulated_parts}
  parts = []
  service_name = icap_data[:request_line][:uri].path
  service_name = service_name[1...service_name.length]
  service = @server.services[service_name]
  if service
    disable_preview = !service.supports_preview?
    preview_size = service.preview_size
  else
    disable_preview = true
    preview_size = nil
  end
  encapsulated_parts.each do |ep|
    parts << case ep[0]
      when 'null-body'
        NullBody.new
      when 'req-hdr'
        http_parser = HTTPHeaderParser.new(@io,true)
        parsed_data[:http_request_header] = http_parser.parse
      when 'res-hdr'
        http_parser = HTTPHeaderParser.new(@io,false)
        parsed_data[:http_response_header] = http_parser.parse
      when 'req-body'
        bp = BodyParser.new(@io, disable_preview, (icap_data[:header]['Preview'] || nil))
        p_data = bp.parse
        parsed_data[:http_request_body] = RequestBody.new(p_data[0],p_data[1])
      when 'res-body'
        bp = BodyParser.new(@io, disable_preview, (icap_data[:header]['Preview'] || nil))
        p_data = bp.parse
        parsed_data[:http_response_body] = ResponseBody.new(p_data[0],p_data[1])
      else
        nil
    end
  end

  parsed_data
end