-
Notifications
You must be signed in to change notification settings - Fork 1k
/
http.rb
95 lines (80 loc) 路 2.58 KB
/
http.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
95
# frozen_string_literal: true
require "net/https"
require "uri"
module Postal
module HTTP
def self.get(url, options = {})
request(Net::HTTP::Get, url, options)
end
def self.post(url, options = {})
request(Net::HTTP::Post, url, options)
end
def self.request(method, url, options = {})
options[:headers] ||= {}
uri = URI.parse(url)
request = method.new((uri.path.length == 0 ? "/" : uri.path) + (uri.query ? "?" + uri.query : ""))
options[:headers].each { |k, v| request.add_field k, v }
if options[:username] || uri.user
request.basic_auth(options[:username] || uri.user, options[:password] || uri.password)
end
if options[:params].is_a?(Hash)
# If params has been provided, sent it them as form encoded values
request.set_form_data(options[:params])
elsif options[:json].is_a?(String)
# If we have a JSON string, set the content type and body to be the JSON
# data
request.add_field "Content-Type", "application/json"
request.body = options[:json]
elsif options[:text_body]
# Add a plain text body if we have one
request.body = options[:text_body]
end
if options[:sign]
signature = EncryptoSigno.sign(Postal.signing_key, request.body.to_s).gsub("\n", "")
request.add_field "X-Postal-Signature", signature
end
request["User-Agent"] = options[:user_agent] || "Postal/#{Postal.version}"
connection = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
connection.use_ssl = true
connection.verify_mode = OpenSSL::SSL::VERIFY_PEER
ssl = true
else
ssl = false
end
begin
timeout = options[:timeout] || 60
Timeout.timeout(timeout) do
result = connection.request(request)
{
code: result.code.to_i,
body: result.body,
headers: result.to_hash,
secure: ssl
}
end
rescue OpenSSL::SSL::SSLError
{
code: -3,
body: "Invalid SSL certificate",
headers: {},
secure: ssl
}
rescue SocketError, Errno::ECONNRESET, EOFError, Errno::EINVAL, Errno::ENETUNREACH, Errno::EHOSTUNREACH, Errno::ECONNREFUSED => e
{
code: -2,
body: e.message,
headers: {},
secure: ssl
}
rescue Timeout::Error
{
code: -1,
body: "Timed out after #{timeout}s",
headers: {},
secure: ssl
}
end
end
end
end