66 lines
No EOL
2.2 KiB
Ruby
66 lines
No EOL
2.2 KiB
Ruby
# Request class used as instance object of an incoming TCP request to manage that request. Splits the request into it's method, resource, version, headers and params to be read by the router.
|
|
class Request
|
|
attr_reader :method, :resource, :version, :headers, :params
|
|
|
|
# @param input [String] the string of the request
|
|
def initialize(input)
|
|
variable_definer(input)
|
|
end
|
|
|
|
private
|
|
|
|
# Defines required class variables by splitting up the input text
|
|
#
|
|
# @param input [String] the string of the request
|
|
def variable_definer(input)
|
|
rows = input.split(/\r\n/)
|
|
@method, @resource, @version = rows[0].split(' ')
|
|
@headers, @params = {}, {}
|
|
if rows.find_index("") != nil then
|
|
limit = rows.find_index("")
|
|
else
|
|
limit = rows.length
|
|
end
|
|
header_constructor(rows, limit)
|
|
if limit != rows.length
|
|
param_constructor(rows)
|
|
end
|
|
if @resource.include?("?")
|
|
resource_deconstructor
|
|
end
|
|
end
|
|
|
|
# Reorganizes the information in the header based on the format
|
|
#
|
|
# @param rows [Array] A list of the rows in the header
|
|
# @param limit [Int] The limit where the header ends.
|
|
def header_constructor(rows, limit)
|
|
for row in rows[1..limit-1]
|
|
context, information = row.split(' ')
|
|
context = context.sub(/:/, "")
|
|
@headers[context] = information
|
|
end
|
|
end
|
|
|
|
# Organizes the parameter information into a dict of parameters
|
|
#
|
|
# @param rows [Array] A list of the rows of parameters
|
|
def param_constructor(rows)
|
|
for row in rows[rows.find_index("")+1..rows.length]
|
|
information = row.split(/&/)
|
|
for info in information
|
|
context, content = info.split('=')
|
|
@params[context] = content
|
|
end
|
|
end
|
|
end
|
|
|
|
# Organizes the resource information into a dict of resource parameters
|
|
def resource_deconstructor
|
|
information = @resource.split('?')[1].split(/&/)
|
|
for info in information
|
|
context, content = info.split('=')
|
|
@params[context] = content
|
|
end
|
|
end
|
|
end |