Class: Code::Object::Http

Inherits:
Code::Object show all
Defined in:
lib/code/object/http.rb

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Http",
  description:
    "sends http requests and returns dictionaries with request and response details.",
  examples: [
    "Http.get(\"http://httpbin.org/status/200\").success?",
    "Http.post(\"http://httpbin.org/status/200\", body: :hello).request.body",
    "Http.fetch(:get, \"http://httpbin.org/status/204\").code"
  ]
}.freeze
CLASS_FUNCTIONS =
{
  "get" => {
    name: "get",
    description:
      "sends a get request and returns the response dictionary.",
    examples: [
      "Http.get(\"http://httpbin.org/status/200\").code",
      "Http.get(\"http://httpbin.org/status/200\", query: { q: :ruby }).url",
      "Http.get(\"http://httpbin.org/status/200\", headers: { accept: \"application/json\" }).request.headers"
    ]
  },
  "head" => {
    name: "head",
    description:
      "sends a head request and returns the response dictionary.",
    examples: [
      "Http.head(\"http://httpbin.org/status/200\").code",
      "Http.head(\"http://httpbin.org/status/204\").status",
      "Http.head(\"http://httpbin.org/status/200\", headers: { accept: \"text/html\" }).request.headers"
    ]
  },
  "post" => {
    name: "post",
    description:
      "sends a post request with optional body, form data, and headers.",
    examples: [
      "Http.post(\"http://httpbin.org/status/200\", body: :hello).request.body",
      "Http.post(\"http://httpbin.org/status/200\", data: { a: 1 }).success?",
      "Http.post(\"http://httpbin.org/status/200\", headers: { content_type: \"text/plain\" }).request.headers"
    ]
  },
  "put" => {
    name: "put",
    description: "sends a put request with optional body or form data.",
    examples: [
      "Http.put(\"http://httpbin.org/status/200\", body: :hello).request.body",
      "Http.put(\"http://httpbin.org/status/200\", data: { a: 1 }).method",
      "Http.put(\"http://httpbin.org/status/204\").code"
    ]
  },
  "delete" => {
    name: "delete",
    description:
      "sends a delete request and returns the response dictionary.",
    examples: [
      "Http.delete(\"http://httpbin.org/status/200\").success?",
      "Http.delete(\"http://httpbin.org/status/200\", body: :hello).request.body",
      "Http.delete(\"http://httpbin.org/status/204\").status"
    ]
  },
  "options" => {
    name: "options",
    description:
      "sends an options request and returns the response dictionary.",
    examples: [
      "Http.options(\"http://httpbin.org/status/200\").method",
      "Http.options(\"http://httpbin.org/status/204\").code",
      "Http.options(\"http://httpbin.org/status/200\", headers: { accept: \"*/*\" }).request.headers"
    ]
  },
  "trace" => {
    name: "trace",
    description:
      "sends a trace request and returns the response dictionary.",
    examples: [
      "Http.trace(\"http://httpbin.org/status/200\").method",
      "Http.trace(\"http://httpbin.org/status/204\").code",
      "Http.trace(\"http://httpbin.org/status/200\", headers: { accept: \"message/http\" }).request.headers"
    ]
  },
  "patch" => {
    name: "patch",
    description: "sends a patch request with optional body or form data.",
    examples: [
      "Http.patch(\"http://httpbin.org/status/200\", body: :hello).request.body",
      "Http.patch(\"http://httpbin.org/status/200\", data: { a: 1 }).method",
      "Http.patch(\"http://httpbin.org/status/204\").code"
    ]
  },
  "fetch" => {
    name: "fetch",
    description: "sends an http request using the given method name.",
    examples: [
      "Http.fetch(:get, \"http://httpbin.org/status/200\").success?",
      "Http.fetch(:post, \"http://httpbin.org/status/200\", body: :hello).request.body",
      "Http.fetch(:patch, \"http://httpbin.org/status/200\", data: { a: 1 }).method"
    ]
  }
}.freeze
SIG =
[
  String,
  {
    headers: Dictionary.maybe,
    query: Dictionary.maybe,
    body: String.maybe,
    username: String.maybe,
    password: String.maybe,
    data: Dictionary.maybe,
    timeout: (Integer | Decimal).maybe,
    open_timeout: (Integer | Decimal).maybe,
    read_timeout: (Integer | Decimal).maybe,
    write_timeout: (Integer | Decimal).maybe
  }
].freeze
STATUS_CODES =
{
  continue: 100,
  switching_protocols: 101,
  processing: 102,
  early_hints: 103,
  ok: 200,
  created: 201,
  accepted: 202,
  non_authoritative_information: 203,
  no_content: 204,
  reset_content: 205,
  partial_content: 206,
  multi_status: 207,
  already_reported: 208,
  im_used: 226,
  multiple_choices: 300,
  moved_permanently: 301,
  found: 302,
  see_other: 303,
  not_modified: 304,
  use_proxy: 305,
  reserved: 306,
  temporary_redirect: 307,
  permanent_redirect: 308,
  bad_request: 400,
  unauthorized: 401,
  payment_required: 402,
  forbidden: 403,
  not_found: 404,
  method_not_allowed: 405,
  not_acceptable: 406,
  proxy_authentication_required: 407,
  request_timeout: 408,
  conflict: 409,
  gone: 410,
  length_required: 411,
  precondition_failed: 412,
  request_entity_too_large: 413,
  request_uri_too_long: 414,
  unsupported_media_type: 415,
  requested_range_not_satisfiable: 416,
  expectation_failed: 417,
  misdirected_request: 421,
  unprocessable_entity: 422,
  locked: 423,
  failed_dependency: 424,
  too_early: 425,
  upgrade_required: 426,
  precondition_required: 428,
  too_many_requests: 429,
  request_header_fields_too_large: 431,
  unavailable_for_legal_reasons: 451,
  internal_server_error: 500,
  not_implemented: 501,
  bad_gateway: 502,
  service_unavailable: 503,
  gateway_timeout: 504,
  http_version_not_supported: 505,
  variant_also_negotiates: 506,
  insufficient_storage: 507,
  loop_detected: 508,
  bandwidth_limit_exceeded: 509,
  not_extended: 510,
  network_authentication_required: 511
}.freeze
DEFAULT_TIMEOUT =
1.hour.to_f
MAX_HEADER_BYTES =
32.kilobytes
HEADER_NAME =
/\A[A-Za-z0-9!#$%&'*+\-.^_`|~]+\z/
RESTRICTED_HEADERS =
%w[
  connection
  content-length
  host
  proxy-authorization
  te
  trailer
  transfer-encoding
  upgrade
].freeze

Constants inherited from Code::Object

INSTANCE_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, #initialize, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #call, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, #code_fetch, #code_functions, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #present?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s, #truthy?

Constructor Details

This class inherits a constructor from Code::Object

Class Method Details

.call(**args) ⇒ Object



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
# File 'lib/code/object/http.rb', line 207

def self.call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code

  case code_operator.to_s
  when "get"
    sig(args) { SIG }
    code_get(*code_arguments.raw)
  when "head"
    sig(args) { SIG }
    code_head(*code_arguments.raw)
  when "post"
    sig(args) { SIG }
    code_post(*code_arguments.raw)
  when "put"
    sig(args) { SIG }
    code_put(*code_arguments.raw)
  when "delete"
    sig(args) { SIG }
    code_delete(*code_arguments.raw)
  when "options"
    sig(args) { SIG }
    code_options(*code_arguments.raw)
  when "trace"
    sig(args) { SIG }
    code_trace(*code_arguments.raw)
  when "patch"
    sig(args) { SIG }
    code_patch(*code_arguments.raw)
  when "fetch"
    sig(args) { [String] + SIG }
    code_fetch(*code_arguments.raw)
  else
    super
  end
end

.code_deleteObject



260
261
262
# File 'lib/code/object/http.rb', line 260

def self.code_delete(...)
  code_fetch("delete", ...)
end

.code_fetch(*arguments, redirects: 10) ⇒ Object



276
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# File 'lib/code/object/http.rb', line 276

def self.code_fetch(*arguments, redirects: 10)
  verb = arguments.first.to_code.to_s.downcase
  original_url = arguments.second.to_code.to_s
  options = arguments.third.to_code
  options = Dictionary.new if options.nothing?
  username = options.code_get("username").to_s
  password = options.code_get("password").to_s
  body = options.code_get("body").to_s
  headers = sanitized_headers(options.code_get("headers").raw || {})
  data = options.code_get("data").raw || {}
  timeout = options.code_get("timeout")
  open_timeout = options.code_get("open_timeout")
  read_timeout = options.code_get("read_timeout")
  write_timeout = options.code_get("write_timeout")
  query = options.code_get("query").raw || {}
  query = query.to_a.flatten.map(&:to_s).each_slice(2).to_h.to_query

  url = original_url
  url = "#{url}?#{query}" if query.present?

  if username.present? || password.present?
    authorization = ::Base64.strict_encode64("#{username}:#{password}")
    headers["Authorization"] = "Basic #{authorization}"
    validate_header_size!("Authorization", headers["Authorization"])
  end

  uri = parse_uri(url)
  resolved_ip = ::Code::Network.validate_public_uri!(uri, service: "http")

  http = ::Net::HTTP.new(uri.hostname, uri.port, nil)
  http.ipaddr = resolved_ip
  http.use_ssl = true if uri.scheme == "https"
  default_timeout = http_timeout(timeout, DEFAULT_TIMEOUT)
  open_timeout_value = http_timeout(open_timeout, default_timeout)
  read_timeout_value = http_timeout(read_timeout, default_timeout)
  write_timeout_value = http_timeout(write_timeout, default_timeout)

  http.open_timeout = open_timeout_value if open_timeout_value
  http.read_timeout = read_timeout_value if read_timeout_value
  http.write_timeout = write_timeout_value if write_timeout_value

  request_class =
    case verb
    when "head"
      ::Net::HTTP::Head
    when "post"
      ::Net::HTTP::Post
    when "put"
      ::Net::HTTP::Put
    when "delete"
      ::Net::HTTP::Delete
    when "options"
      ::Net::HTTP::Options
    when "trace"
      ::Net::HTTP::Trace
    when "patch"
      ::Net::HTTP::Patch
    else
      ::Net::HTTP::Get
    end

  request = request_class.new(uri)
  headers.each { |key, value| request[key.to_s] = value.to_s }
  request.body = body if body.present?
  request.set_form_data(**data.as_json) if data.present?

  begin
    response_body = +""
    response =
      http.request(request) do |http_response|
        validate_response_headers!(http_response)

        http_response.read_body do |chunk|
          response_body << chunk
        end
      end
  rescue ::Timeout::Error, ::Errno::ETIMEDOUT
    raise ::Code::Error, "http timeout"
  rescue OpenSSL::SSL::SSLError, IOError, SystemCallError, SocketError
    raise ::Code::Error, "http error"
  end

  code = response.code.to_i
  location = response["location"].to_s

  if (300..399).cover?(code) && location.present? && redirects.positive?
    new_uri = parse_uri(::URI.join(uri, location).to_s)
    ::Code::Network.validate_public_uri!(new_uri, service: "http")

    if same_origin?(new_uri, uri)
      code_fetch(
        "get",
        new_uri.to_s,
        { username: username, password: password, headers: headers },
        redirects: redirects - 1
      )
    else
      code_fetch("get", new_uri.to_s, redirects: redirects - 1)
    end
  else
    status = STATUS_CODES.key(code) || :ok
    response_headers = response.each_header.to_h
    request_headers =
      request.to_hash.transform_values { |values| List.new(values) }
    body = response_body.to_s

    Dictionary.new(
      :code => code,
      :status => status,
      :body => body,
      :headers => response_headers,
      :method => verb,
      :url => url,
      "success?" => code.between?(200, 299),
      "redirect?" => code.between?(300, 399),
      :request => {
        method: verb,
        url: url,
        headers: request_headers,
        body: request.body.to_s
      },
      :response => {
        code: code,
        status: status,
        headers: response_headers,
        body: body
      }
    )
  end
end

.code_getObject



244
245
246
# File 'lib/code/object/http.rb', line 244

def self.code_get(...)
  code_fetch("get", ...)
end

.code_headObject



248
249
250
# File 'lib/code/object/http.rb', line 248

def self.code_head(...)
  code_fetch("head", ...)
end

.code_optionsObject



264
265
266
# File 'lib/code/object/http.rb', line 264

def self.code_options(...)
  code_fetch("options", ...)
end

.code_patchObject



272
273
274
# File 'lib/code/object/http.rb', line 272

def self.code_patch(...)
  code_fetch("patch", ...)
end

.code_postObject



252
253
254
# File 'lib/code/object/http.rb', line 252

def self.code_post(...)
  code_fetch("post", ...)
end

.code_putObject



256
257
258
# File 'lib/code/object/http.rb', line 256

def self.code_put(...)
  code_fetch("put", ...)
end

.code_traceObject



268
269
270
# File 'lib/code/object/http.rb', line 268

def self.code_trace(...)
  code_fetch("trace", ...)
end

.function_documentation(scope) ⇒ Object



106
107
108
109
110
# File 'lib/code/object/http.rb', line 106

def self.function_documentation(scope)
  return CLASS_FUNCTIONS if scope == :class

  {}
end

.http_timeout(value, default) ⇒ Object



467
468
469
# File 'lib/code/object/http.rb', line 467

def self.http_timeout(value, default)
  ::Code.normalize_timeout!(value.nothing? ? default : value)
end

.parse_uri(url) ⇒ Object



407
408
409
410
411
# File 'lib/code/object/http.rb', line 407

def self.parse_uri(url)
  ::URI.parse(url)
rescue ::URI::InvalidURIError
  raise ::Code::Error, "http: invalid url"
end

.same_origin?(new_uri, original_uri) ⇒ Boolean

Returns:



413
414
415
416
417
418
# File 'lib/code/object/http.rb', line 413

def self.same_origin?(new_uri, original_uri)
  new_uri.scheme == original_uri.scheme &&
    new_uri.hostname.to_s.downcase ==
      original_uri.hostname.to_s.downcase &&
    new_uri.port == original_uri.port
end

.sanitized_headers(headers) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/code/object/http.rb', line 420

def self.sanitized_headers(headers)
  headers.to_h.to_h do |key, value|
    name = key.to_s
    header_value = value.to_s

    validate_header_name!(name)
    validate_header_value!(header_value)
    validate_header_size!(name, header_value)

    [name, header_value]
  end
end

.validate_header_name!(name) ⇒ Object

Raises:



433
434
435
436
437
438
439
440
441
442
443
# File 'lib/code/object/http.rb', line 433

def self.validate_header_name!(name)
  normalized_name = name.downcase

  unless HEADER_NAME.match?(name)
    raise ::Code::Error, "http: invalid header name"
  end

  return unless RESTRICTED_HEADERS.include?(normalized_name)

  raise ::Code::Error, "http: restricted header #{name.inspect}"
end

.validate_header_size!(name, value) ⇒ Object

Raises:



451
452
453
454
455
# File 'lib/code/object/http.rb', line 451

def self.validate_header_size!(name, value)
  return if name.bytesize + value.bytesize <= MAX_HEADER_BYTES

  raise ::Code::Error, "http: request headers are too large"
end

.validate_header_value!(value) ⇒ Object

Raises:



445
446
447
448
449
# File 'lib/code/object/http.rb', line 445

def self.validate_header_value!(value)
  return unless /[\r\n\0]/.match?(value)

  raise ::Code::Error, "http: invalid header value"
end

.validate_response_headers!(response) ⇒ Object

Raises:



457
458
459
460
461
462
463
464
465
# File 'lib/code/object/http.rb', line 457

def self.validate_response_headers!(response)
  size =
    response.each_header.sum do |name, value|
      name.to_s.bytesize + value.to_s.bytesize
    end
  return if size <= MAX_HEADER_BYTES

  raise ::Code::Error, "http response headers are too large"
end