Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions httpi.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.add_dependency 'rack'
s.add_dependency 'socksify'

s.add_development_dependency 'rubyntlm', '~> 0.3.2'
s.add_development_dependency 'rake', '~> 10.0'
s.add_development_dependency 'rspec', '~> 2.14'
s.add_development_dependency 'mocha', '~> 0.13'
s.add_development_dependency 'puma', '~> 2.3.2'
s.add_development_dependency 'webmock'

s.files = `git ls-files`.split("\n")
s.require_path = 'lib'
Expand Down
8 changes: 7 additions & 1 deletion lib/httpi/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require "httpi/response"
require 'kconv'
require 'socket'
require "socksify"
require 'socksify/http'

module HTTPI
module Adapter
Expand Down Expand Up @@ -67,7 +69,11 @@ def perform(http, http_request, &block)

def create_client
proxy_url = @request.proxy || URI("")
proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_url.user, proxy_url.password)
if URI(proxy_url).scheme == 'socks'
proxy =Net::HTTP.SOCKSProxy(proxy_url.host, proxy_url.port)
else
proxy = Net::HTTP::Proxy(proxy_url.host, proxy_url.port, proxy_url.user, proxy_url.password)
end
proxy.new(@request.url.host, @request.url.port)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/httpi/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def cookie_store

# Expects a +url+, validates its validity and returns a +URI+ object.
def normalize_url!(url)
raise ArgumentError, "Invalid URL: #{url}" unless url.to_s =~ /^http/
raise ArgumentError, "Invalid URL: #{url}" unless url.to_s =~ /^http|socks/
url.kind_of?(URI) ? url : URI(url)
end

Expand Down
16 changes: 16 additions & 0 deletions spec/httpi/adapter/net_http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
@server.stop
end

context 'when socks is specified' do

let(:socks_client) { mock('socks_client') }
let(:request){HTTPI::Request.new(@server.url)}

it 'uses Net::HTTP.SOCKSProxy as client' do
socks_client.expects(:new).with(URI(@server.url).host, URI(@server.url).port).returns(:socks_client_instance)
Net::HTTP.expects(:SOCKSProxy).with('localhost', 8080).returns socks_client

request.proxy = 'socks://localhost:8080'
adapter = HTTPI::Adapter::NetHTTP.new(request)

expect(adapter.client).to eq(:socks_client_instance)
end
end

it "sends and receives HTTP headers" do
request = HTTPI::Request.new(@server.url + "x-header")
request.headers["X-Header"] = "HTTPI"
Expand Down
5 changes: 5 additions & 0 deletions spec/httpi/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
expect(request.proxy).to eq(URI("http://proxy.example.com"))
end

it 'also accepts the socks URL to use as a String' do
request.proxy ="socks://socks.example.com"
expect(request.proxy).to eq(URI("socks://socks.example.com"))
end

it "also accepts a URI object" do
request.proxy = URI("http://proxy.example.com")
expect(request.proxy).to eq(URI("http://proxy.example.com"))
Expand Down