Class: HttpTask

Inherits:
BaseTask show all
Defined in:
lib/libisi/task/http.rb

Constant Summary collapse

DEFAULT_CONTEXT_NAME_ARG_REGEXP =

Splits the rest of the uri path into context, function name and arguments.

First regular expression match as context Second regular expression match as function name the rest of the matches as arguments

/^\/?([^\/]+)\/([^\/]+)\/(.+)$/

Instance Attribute Summary

Attributes inherited from BaseTask

#function, #parameter

Class Method Summary collapse

Methods inherited from BaseTask

#initialize

Constructor Details

This class inherits a constructor from BaseTask

Class Method Details

.from_browser(io, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/libisi/task/http.rb', line 65

def HttpTask.from_browser(io, options = {})
  p "gets"
  request_line = io.gets
  p "request_line: #{request_line}"
  # base from webrick/httprequest.rb read_request_line(socket)
  if /^(\S+)\s+(\S+)(?:\s+HTTP\/(\d+\.\d+))?\r?\n/mo =~ request_line
    request_method = $1
    unparsed_uri   = $2
    http_version   = ($3 ? $3 : "0.9")

    request = HttpTask.from_path_with_parameters(unparsed_uri)
    request.parameter.options[:http_version] = http_version
    request.parameter.options[:request_method] = $1
    request.parameter.options[:input_stream] = io
    request.parameter.options[:output_stream] = io
    request

    # TODO: read parameters
  else
    rl = request_line.sub(/\x0d?\x0a\z/o, '')
    raise "Bad request #{rl.inspect}"
  end
end

.from_path(path, options = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/libisi/task/http.rb', line 33

def HttpTask.from_path(path, options = {})
  unless path =~ DEFAULT_CONTEXT_NAME_ARG_REGEXP
    raise "context name arg regular expression did not match #{path.inspect}"
  end
  context = $1
  function_name = $2
  arguments = $3.split("/")

  # function
  func = BaseFunction.new(context, function_name)

  # parameter
  params = BaseParameter.new
  params.arguments = arguments

  HttpTask.new(func, params)
end

.from_path_with_parameters(full_path, options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/libisi/task/http.rb', line 51

def HttpTask.from_path_with_parameters(full_path, options = {})
  path, query = full_path.split("?",2)
  task = HttpTask.from_path(path, options)
  if query
    n_opts = {}
    CGI::parse(query).each {|key,val|
	n_opts[key.to_sym] = val
    }
    task.parameter.options = task.parameter.options.merge(n_opts)
  end
    
  task
end