Class: IceJade::Cradle::Server::HTTPRequest
- Inherits:
-
Object
- Object
- IceJade::Cradle::Server::HTTPRequest
- Defined in:
- lib/ice_jade/cradle/server.rb
Overview
=================================================================
HTTP 请求解析
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#content_type ⇒ Object
readonly
Returns the value of attribute content_type.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#route_params ⇒ Object
Returns the value of attribute route_params.
Class Method Summary collapse
Instance Method Summary collapse
-
#files ⇒ Object
multipart 解析结果.
- #form_data ⇒ Object
-
#initialize(method, path, query, headers, body) ⇒ HTTPRequest
constructor
A new instance of HTTPRequest.
- #multipart? ⇒ Boolean
- #parsed_body ⇒ Object
Constructor Details
#initialize(method, path, query, headers, body) ⇒ HTTPRequest
Returns a new instance of HTTPRequest.
267 268 269 270 271 272 273 274 275 |
# File 'lib/ice_jade/cradle/server.rb', line 267 def initialize(method, path, query, headers, body) @method = method @path = path @query = query @headers = headers @body = body @content_type = headers['Content-Type']&.split(';')&.first&.strip @route_params = {} end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
264 265 266 |
# File 'lib/ice_jade/cradle/server.rb', line 264 def body @body end |
#content_type ⇒ Object (readonly)
Returns the value of attribute content_type.
264 265 266 |
# File 'lib/ice_jade/cradle/server.rb', line 264 def content_type @content_type end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
264 265 266 |
# File 'lib/ice_jade/cradle/server.rb', line 264 def headers @headers end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
264 265 266 |
# File 'lib/ice_jade/cradle/server.rb', line 264 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
264 265 266 |
# File 'lib/ice_jade/cradle/server.rb', line 264 def path @path end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
264 265 266 |
# File 'lib/ice_jade/cradle/server.rb', line 264 def query @query end |
#route_params ⇒ Object
Returns the value of attribute route_params.
265 266 267 |
# File 'lib/ice_jade/cradle/server.rb', line 265 def route_params @route_params end |
Class Method Details
.parse(socket) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/ice_jade/cradle/server.rb', line 277 def self.parse(socket) line = read_line(socket) return nil unless line method, full_path, _protocol = line.split(' ', 3) return nil unless method && full_path uri = URI.parse(full_path) query = URI.decode_www_form(uri.query || '').to_h headers = {} while (header_line = read_line(socket)) break if header_line == "\r\n" || header_line == "\n" || header_line.empty? key, value = header_line.split(':', 2) headers[key.strip] = value.strip if key && value end body = '' if headers['Content-Length'] length = headers['Content-Length'].to_i body = socket.read(length) if length > 0 end new(method, uri.path, query, headers, body) rescue StandardError nil end |
.read_line(socket) ⇒ Object
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/ice_jade/cradle/server.rb', line 334 def self.read_line(socket) line = +"" while (char = socket.getc) line << char break if line.end_with?("\r\n") end line rescue StandardError nil end |
Instance Method Details
#files ⇒ Object
multipart 解析结果
317 318 319 320 |
# File 'lib/ice_jade/cradle/server.rb', line 317 def files return {} unless multipart? parse_multipart[:files] end |
#form_data ⇒ Object
322 323 324 325 |
# File 'lib/ice_jade/cradle/server.rb', line 322 def form_data return {} unless multipart? parse_multipart[:fields] end |
#multipart? ⇒ Boolean
327 328 329 330 |
# File 'lib/ice_jade/cradle/server.rb', line 327 def multipart? content_type == 'multipart/form-data' || headers['Content-Type']&.include?('multipart/form-data') end |
#parsed_body ⇒ Object
305 306 307 308 309 310 311 312 313 314 |
# File 'lib/ice_jade/cradle/server.rb', line 305 def parsed_body case content_type when 'application/json' JSON.parse(body) rescue body when 'application/x-www-form-urlencoded' URI.decode_www_form(body).to_h rescue body else body end end |