Class: Itsi::HttpRequest

Inherits:
Object
  • Object
show all
Includes:
ResponseStatusShortcodes, Server::TypedHandlers::ParamParser
Defined in:
lib/itsi/http_request.rb,
lib/itsi/http_request/response_status_shortcodes.rb

Defined Under Namespace

Modules: ResponseStatusShortcodes

Constant Summary collapse

EMPTY_IO =
StringIO.new("").tap { |io| io.set_encoding(Encoding::ASCII_8BIT) }
RACK_HEADER_MAP =
StandardHeaders::ALL.map do |header|
  rack_form = if header == "content-type"
                "CONTENT_TYPE"
              elsif header == "content-length"
                "CONTENT_LENGTH"
              else
                "HTTP_#{header.upcase.gsub(/-/, "_")}"
              end
  [header, rack_form]
end.to_h.tap do |hm|
  hm.default_proc = proc { |_, key| "HTTP_#{key.upcase.gsub(/-/, "_")}" }
end
RACK_ENV_TEMPLATE =
{
  "SERVER_SOFTWARE" => "Itsi",
  "rack.errors" => $stderr,
  "rack.multithread" => true,
  "rack.multiprocess" => true,
  "rack.run_once" => false,
  "rack.hijack?" => true,
  "rack.multipart.buffer_size" => 16_384,
  "SCRIPT_NAME" => "",
  "REQUEST_METHOD" => "",
  "PATH_INFO" => "",
  "REQUEST_PATH" => "",
  "QUERY_STRING" => "",
  "REMOTE_ADDR" => "",
  "SERVER_PORT" => "",
  "SERVER_NAME" => "",
  "SERVER_PROTOCOL" => "",
  "HTTP_HOST" => "",
  "HTTP_VERSION" => "",
  "itsi.request" => "",
  "itsi.response" => "",
  "rack.version" => nil,
  "rack.url_scheme" => "",
  "rack.input" => "",
  "rack.hijack" => ""
}.freeze

Constants included from ResponseStatusShortcodes

ResponseStatusShortcodes::HTTP_STATUS_CODES, ResponseStatusShortcodes::HTTP_STATUS_NAME_TO_CODE_MAP

Constants included from Server::TypedHandlers::ParamParser

Server::TypedHandlers::ParamParser::CONVERSION_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Server::TypedHandlers::ParamParser

#apply_schema!, #cast_value!, #format_path, #processed_schema

Instance Attribute Details

#hijackedObject

Returns the value of attribute hijacked.



12
13
14
# File 'lib/itsi/http_request.rb', line 12

def hijacked
  @hijacked
end

Instance Method Details

#bodyObject



149
150
151
# File 'lib/itsi/http_request.rb', line 149

def body
  @body ||= build_input_io
end

#build_input_ioObject



153
154
155
156
157
158
159
160
# File 'lib/itsi/http_request.rb', line 153

def build_input_io
  case body_parts
  when nil then EMPTY_IO
  when String then StringIO.new(body_parts).tap { |io| io.set_encoding(Encoding::ASCII_8BIT) }
  when Array then File.open(body_parts.first, "rb").tap { |io| io.set_encoding(Encoding::ASCII_8BIT) }
  else body_parts
  end
end

#clean_temp_files(params) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/itsi/http_request.rb', line 215

def clean_temp_files(params)
  case params
  when Hash
    if params.key?(:tempfile)
      params[:tempfile].unlink
    else
      params.each_value { |v| clean_temp_files(v) }
    end
  when Array then params.each { |v| clean_temp_files(v) }
  end
end

#hijackObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/itsi/http_request.rb', line 138

def hijack
  self.hijacked = true
  UNIXSocket.pair.yield_self do |(server_sock, app_sock)|
    server_sock.autoclose = false
    response.hijack(server_sock.fileno)
    server_sock.sync = true
    app_sock.sync = true
    app_sock
  end
end

#params(schema = nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/itsi/http_request.rb', line 166

def params(schema = nil)
  params = if url_encoded?
             URI.decode_www_form(build_input_io.read).to_h
           elsif json?
             JSON.parse(build_input_io.read)
           elsif multipart?
             Rack::Multipart::Parser.parse(
               build_input_io,
               content_length,
               content_type,
               Rack::Multipart::Parser::TEMPFILE_FACTORY,
               Rack::Multipart::Parser::BUFSIZE,
               Rack::Utils.default_query_parser
             ).params
           else
             {}
           end

  params.merge!(query_params).merge!(url_params)
  validated = schema ? apply_schema!(params, schema) : params
  if block_given?
    yield validated
  else
    raise "#params must take a block for multipart requests" if multipart?

    validated

  end
rescue ValidationError => e
  if response.json?
    respond(json: { error: e.message }, status: 400)
  else
    respond(e.message, 400)
  end
rescue StandardError => e
  Itsi.log_error e.message
  puts e.backtrace

  # Unexpected error.
  # Don't reveal potential sensitive information to client.
  if response.json?
    respond(json: { error: "Internal Server Error" }, status: 500)
  else
    respond("Internal Server Error", 500)
  end
ensure
  clean_temp_files(params)
end

#query_paramsObject



227
228
229
# File 'lib/itsi/http_request.rb', line 227

def query_params
  URI.decode_www_form(query_string).to_h
end

#respond(_body = nil, _status = 200, _headers = nil, json: nil, html: nil, text: nil, xml: nil, hijack: false, as: nil, status: _status, headers: _headers, body: _body, &blk) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/itsi/http_request.rb', line 96

def respond(
  _body = nil, _status = 200, _headers = nil, # rubocop:disable Lint/UnderscorePrefixedVariableName
  json: nil,
  html: nil,
  text: nil,
  xml: nil,
  hijack: false,
  as: nil,
  status: _status,
  headers: _headers,
  body: _body,
  &blk
)
  if json
    if as
      begin
        validate!(json, as: as)
      rescue ValidationError => e
        json = { type: "error", message: "Validation Error: #{e.message}" }
        status = 400
      end
    end
    body = json.to_json
    headers ||= {}
    headers["Content-Type"] ||= "application/json"
  elsif html
    body = html
    headers ||= {}
    headers["Content-Type"] ||= "text/html"
  elsif xml
    body = xml
    headers ||= {}
    headers["Content-Type"] ||= "application/xml"
  elsif text
    body = text
    headers ||= {}
    headers["Content-Type"] ||= "text/plain"
  end

  response.respond(status: status, headers: headers, body: body, hijack: hijack, &blk)
end

#to_rack_envObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/itsi/http_request.rb', line 56

def to_rack_env
  path = self.path
  host = self.host
  version = self.version
  env = RACK_ENV_TEMPLATE.dup
  env["SCRIPT_NAME"] = script_name
  env["REQUEST_METHOD"] = request_method
  env["REQUEST_PATH"] = env["PATH_INFO"] = path
  env["QUERY_STRING"] = query_string
  env["REMOTE_ADDR"] = remote_addr
  env["SERVER_PORT"] = port.to_s
  env["HTTP_HOST"] = env["SERVER_NAME"] = host
  env["HTTP_VERSION"] = env["SERVER_PROTOCOL"] = version
  env["itsi.request"] = self
  env["itsi.response"] = response
  env["rack.version"] = [version]
  env["rack.url_scheme"] = scheme
  env["rack.input"] = build_input_io
  env["rack.hijack"] = method(:hijack)
  headers.each do |(k, v)|
    env[case k
        when "content-type" then "CONTENT_TYPE"
        when "content-length" then "CONTENT_LENGTH"
        when "accept" then "HTTP_ACCEPT"
        when "accept-encoding" then "HTTP_ACCEPT_ENCODING"
        when "accept-language" then "HTTP_ACCEPT_LANGUAGE"
        when "user-agent" then "HTTP_USER_AGENT"
        when "referer" then "HTTP_REFERER"
        when "origin" then "HTTP_ORIGIN"
        when "cookie" then "HTTP_COOKIE"
        when "authorization" then "HTTP_AUTHORIZATION"
        when "x-forwarded-for" then "HTTP_X_FORWARDED_FOR"
        when "x-forwarded-proto" then "HTTP_X_FORWARDED_PROTO"
        else RACK_HEADER_MAP[k]
        end
    ] = v
  end
  env
end

#validate!(params, as: nil) ⇒ Object



162
163
164
# File 'lib/itsi/http_request.rb', line 162

def validate!(params, as: nil)
  as ? apply_schema!(params, as) : params
end