http-request 1.1.3 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/http-request/connection.rb +20 -13
- data/lib/http-request/version.rb +1 -1
- metadata +1 -1
@@ -12,7 +12,7 @@ module HttpRequest
|
|
12
12
|
@keep_alive = options[:keep_alive].nil? ? true : options[:keep_alive]
|
13
13
|
|
14
14
|
# Let's do this
|
15
|
-
@uri =
|
15
|
+
@uri = parse_uri(url)
|
16
16
|
open
|
17
17
|
end
|
18
18
|
|
@@ -57,30 +57,29 @@ module HttpRequest
|
|
57
57
|
|
58
58
|
def request(verb, path, headers={}, body=nil)
|
59
59
|
if path =~ /^http/i
|
60
|
-
new_uri =
|
60
|
+
new_uri = parse_uri(path)
|
61
61
|
if new_uri.origin != uri.origin
|
62
62
|
# TODO.. get an error class for this..
|
63
|
-
raise "Ummm... you're trying to talk to another server
|
63
|
+
raise "Ummm... you're trying to talk to another server"
|
64
64
|
end
|
65
|
-
uri
|
65
|
+
@uri = new_uri
|
66
|
+
else
|
67
|
+
# Update the path and query
|
68
|
+
# Note: also normalize the path in case its malformed.. dum dums
|
69
|
+
uri.join! parse_uri(path.gsub('//','/')).request_uri
|
66
70
|
end
|
67
71
|
|
68
72
|
# Open the connection if its closed or hasn't been started..
|
69
73
|
open if closed?
|
70
74
|
|
71
|
-
# headers
|
75
|
+
# Default headers
|
72
76
|
headers['User-Agent'] ||= user_agent
|
73
77
|
headers['Connection'] ||= 'keep-alive' if keep_alive
|
74
78
|
headers['Content-Length'] ||= body.bytesize.to_s if body
|
75
79
|
|
76
|
-
# TODO.. link traversal..
|
77
|
-
# ..will our host change tho... like the thing we actually connect to?
|
78
|
-
# it could very well... so we should check that out..
|
79
|
-
# maybe we have a "host" or "host_id" variable for this stuff..?
|
80
|
-
|
81
80
|
# Build request
|
82
81
|
req_klass = instance_eval("Net::HTTP::"+verb.to_s.capitalize)
|
83
|
-
req = req_klass.new(
|
82
|
+
req = req_klass.new(uri.request_uri, headers)
|
84
83
|
req.body = body if !body.nil? && !body.empty?
|
85
84
|
|
86
85
|
# Make the HTTP request
|
@@ -108,12 +107,20 @@ module HttpRequest
|
|
108
107
|
|
109
108
|
# Handle URL Redirection
|
110
109
|
if auto_redirect && raw_response.kind_of?(Net::HTTPRedirection) && !response.header['location'].nil?
|
111
|
-
@uri =
|
112
|
-
|
110
|
+
@uri = parse_uri(response.header['location'])
|
111
|
+
close # close existing connection..
|
112
|
+
request(verb, uri.request_uri, headers, body)
|
113
113
|
else
|
114
114
|
response
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
|
119
|
+
private
|
120
|
+
|
121
|
+
def parse_uri(uri)
|
122
|
+
Addressable::URI.parse(Addressable::URI.encode(Addressable::URI.unencode(uri)))
|
123
|
+
end
|
124
|
+
|
118
125
|
end
|
119
126
|
end
|
data/lib/http-request/version.rb
CHANGED