Merge pull request #27 from mamantoha/set-proxy

add proxy support
This commit is contained in:
Kanezoh 2024-04-27 08:00:58 +09:00 committed by GitHub
commit d4f7054a88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 52 additions and 0 deletions

View File

@ -11,6 +11,8 @@ license: MIT
dependencies: dependencies:
lexbor: lexbor:
github: kostya/lexbor github: kostya/lexbor
http_proxy:
github: mamantoha/http_proxy
development_dependencies: development_dependencies:
webmock: webmock:

17
spec/proxy_spec.cr Normal file
View File

@ -0,0 +1,17 @@
require "./spec_helper"
WebMock.stub(:get, "http://example.com/with_proxy").to_return(body: "success")
describe "Mechanize proxy test" do
it "set proxy" do
with_proxy_server do |host, port, wants_close|
agent = Mechanize.new
agent.set_proxy("127.0.0.1", 8080)
page = agent.get("http://example.com/with_proxy")
page.body.should eq("success")
page.code.should eq(200)
ensure
wants_close.send(nil)
end
end
end

View File

@ -1,5 +1,6 @@
require "spec" require "spec"
require "webmock" require "webmock"
require "http_proxy"
require "../src/mechanize" require "../src/mechanize"
WebMock.stub(:get, "example.com") WebMock.stub(:get, "example.com")
@ -46,3 +47,22 @@ WebMock.stub(:post, "example.com/post_path")
WebMock.stub(:post, "example.com/post_path") WebMock.stub(:post, "example.com/post_path")
.with(body: "name=foo&email=bar&commit=submit", headers: {"Content-Type" => "application/x-www-form-urlencoded"}) .with(body: "name=foo&email=bar&commit=submit", headers: {"Content-Type" => "application/x-www-form-urlencoded"})
.to_return(body: "success with button") .to_return(body: "success with button")
def with_proxy_server(host = "127.0.0.1", port = 8080, &)
wants_close = Channel(Nil).new
server = HTTP::Proxy::Server.new
spawn do
server.bind_tcp(host, port)
server.listen
end
spawn do
wants_close.receive
server.close
end
Fiber.yield
yield host, port, wants_close
end

View File

@ -2,6 +2,7 @@ require "log"
require "uri" require "uri"
require "http/client" require "http/client"
require "lexbor" require "lexbor"
require "http_proxy"
require "./mechanize/http/agent" require "./mechanize/http/agent"
require "./mechanize/form" require "./mechanize/form"
require "./mechanize/node" require "./mechanize/node"
@ -305,6 +306,11 @@ class Mechanize
@agent.add_auth(uri, user, pass) @agent.add_auth(uri, user, pass)
end end
# Sets the proxy +address+ at +port+ with an optional +user+ and +password+
def set_proxy(address : String, port : Int32, user : String? = nil, password : String? = nil)
@agent.set_proxy(address, port, user, password)
end
# Runs given block, then resets the page history as it was before. # Runs given block, then resets the page history as it was before.
private def transact private def transact
# save the previous history status. # save the previous history status.

View File

@ -16,6 +16,8 @@ class Mechanize
getter authenticate_methods : Hash(URI, Hash(String, Array(AuthRealm))) getter authenticate_methods : Hash(URI, Hash(String, Array(AuthRealm)))
getter authenticate_parser : WWWAuthenticateParser getter authenticate_parser : WWWAuthenticateParser
@proxy : ::HTTP::Proxy::Client?
def initialize(@context : Mechanize? = nil) def initialize(@context : Mechanize? = nil)
@history = History.new @history = History.new
@request_headers = ::HTTP::Headers.new @request_headers = ::HTTP::Headers.new
@ -43,6 +45,7 @@ class Mechanize
uri, params = resolve_parameters(uri, method, params) uri, params = resolve_parameters(uri, method, params)
client = ::HTTP::Client.new(uri) client = ::HTTP::Client.new(uri)
request_auth client, uri request_auth client, uri
client.set_proxy(@proxy) if @proxy
response = http_request(client, uri, method, params, body) response = http_request(client, uri, method, params, body)
body = response.not_nil!.body body = response.not_nil!.body
page = response_parse(response, body, uri) page = response_parse(response, body, uri)
@ -170,6 +173,10 @@ class Mechanize
@auth_store.add_auth(uri, user, pass) @auth_store.add_auth(uri, user, pass)
end end
def set_proxy(address : String, port : Int32, user : String? = nil, password : String? = nil)
@proxy = ::HTTP::Proxy::Client.new(address, port, username: user, password: password)
end
private def set_request_headers(uri, headers) private def set_request_headers(uri, headers)
reset_request_header_cookies reset_request_header_cookies
headers.each do |k, v| headers.each do |k, v|