lib/http/client.rb in http-0.3.0 vs lib/http/client.rb in http-0.4.0
- old
+ new
@@ -1,8 +1,8 @@
require 'uri'
-module Http
+module HTTP
# Clients make requests and receive responses
class Client
include Chainable
BUFFER_SIZE = 4096 # Input buffer size
@@ -23,21 +23,27 @@
end
# Make an HTTP request
def request(method, uri, options = {})
opts = @default_options.merge(options)
+ host = URI.parse(uri).host
+ opts.headers["Host"] = host
headers = opts.headers
proxy = opts.proxy
method_body = body(opts, headers)
- puts method_body
request = Request.new method, uri, headers, proxy, method_body
if opts.follow
code = 302
while code == 302 or code == 301
- puts uri
+ # if the uri isn't fully formed complete it
+ if not uri.match /\./
+ uri = "http://#{host}#{uri}"
+ end
+ host = URI.parse(uri).host
+ opts.headers["Host"] = host
method_body = body(opts, headers)
request = Request.new method, uri, headers, proxy, method_body
response = perform request, opts
code = response.code
uri = response.headers["Location"]
@@ -55,10 +61,15 @@
parser = Http::Response::Parser.new
uri, proxy = request.uri, request.proxy
socket = options[:socket_class].open(uri.host, uri.port) # TODO: proxy support
if uri.is_a?(URI::HTTPS)
- socket = options[:ssl_socket_class].open(socket, options[:ssl_context])
+ if options[:ssl_context] == nil
+ context = OpenSSL::SSL::SSLContext.new
+ else
+ context = options[:ssl_context]
+ end
+ socket = options[:ssl_socket_class].new(socket, context)
socket.connect
end
request.stream socket