module HTTPX::Utils

  1. lib/httpx/utils.rb

Constants

FILENAME_EXTENSION_REGEX = /\s*filename\*=(#{VALUE})/.freeze  
FILENAME_REGEX = /\s*filename=(#{VALUE})/.freeze  
TOKEN = %r{[^\s()<>,;:\\"/\[\]?=]+}.freeze  
URIParser = URI::RFC2396_Parser.new  
VALUE = /"(?:\\"|[^"])*"|#{TOKEN}/.freeze  

Public Instance methods

elapsed_time(monotonic_timestamp)
[show source]
   # File lib/httpx/utils.rb
18 def elapsed_time(monotonic_timestamp)
19   Process.clock_gettime(Process::CLOCK_MONOTONIC) - monotonic_timestamp
20 end
get_filename(header, _prefix_regex = nil)
[show source]
   # File lib/httpx/utils.rb
33 def get_filename(header, _prefix_regex = nil)
34   filename = nil
35   case header
36   when FILENAME_REGEX
37     filename = Regexp.last_match(1)
38     filename = Regexp.last_match(1) if filename =~ /^"(.*)"$/
39   when FILENAME_EXTENSION_REGEX
40     filename = Regexp.last_match(1)
41     encoding, _, filename = filename.split("'", 3)
42   end
43 
44   return unless filename
45 
46   filename = URI::DEFAULT_PARSER.unescape(filename) if filename.scan(/%.?.?/).all? { |s| /%[0-9a-fA-F]{2}/.match?(s) }
47 
48   filename.scrub!
49 
50   filename = filename.gsub(/\\(.)/, '\1') unless /\\[^\\"]/.match?(filename)
51 
52   filename.force_encoding ::Encoding.find(encoding) if encoding
53 
54   filename
55 end
now()
[show source]
   # File lib/httpx/utils.rb
14 def now
15   Process.clock_gettime(Process::CLOCK_MONOTONIC)
16 end
parse_retry_after(retry_after)

The value of this field can be either an HTTP-date or a number of seconds to delay after the response is received.

[show source]
   # File lib/httpx/utils.rb
24 def parse_retry_after(retry_after)
25   # first: bet on it being an integer
26   Integer(retry_after)
27 rescue ArgumentError
28   # Then it's a datetime
29   time = Time.httpdate(retry_after)
30   time - Time.now
31 end
to_uri(uri)
[show source]
   # File lib/httpx/utils.rb
59 def to_uri(uri)
60   return URI(uri) unless uri.is_a?(String) && !uri.ascii_only?
61 
62   uri = URI(URIParser.escape(uri))
63 
64   non_ascii_hostname = URIParser.unescape(uri.host)
65 
66   non_ascii_hostname.force_encoding(Encoding::UTF_8)
67 
68   idna_hostname = Punycode.encode_hostname(non_ascii_hostname)
69 
70   uri.host = idna_hostname
71   uri.non_ascii_hostname = non_ascii_hostname
72   uri
73 end