From 226908b5baa1dc888d2cf9fe5e44f16b0c8723a6 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Tue, 17 Aug 2021 01:03:21 +0300 Subject: [PATCH 1/2] follow redirects --- spec/redirect_spec.cr | 17 +++++++++++++++++ src/mechanize/http/agent.cr | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 spec/redirect_spec.cr diff --git a/spec/redirect_spec.cr b/spec/redirect_spec.cr new file mode 100644 index 0000000..89c56b9 --- /dev/null +++ b/spec/redirect_spec.cr @@ -0,0 +1,17 @@ +require "./spec_helper" + +WebMock.stub(:get, "http://example.com/redirect").to_return(body: "success") + +WebMock.stub(:post, "http://example.com/post") + .with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}) + .to_return(status: 302, body: "redirect", headers: {"Location" => "http://example.com/redirect"}) + +describe "Mechanize redirect test" do + it "redirect" do + agent = Mechanize.new + query = {"email" => "foobar"} + page = agent.post("http://example.com/post", query: query) + page.body.should eq("success") + page.code.should eq(200) + end +end diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 1bb7874..4b57f2a 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -30,9 +30,26 @@ module MechanizeCr page = response_parse(response, body, uri) # save cookies save_response_cookies(response, uri, page) + + if response && response.status.redirection? + return follow_redirect(response, headers) + end + page end + private def follow_redirect(response, headers) + redirect_url = response.headers["location"] + uri = URI.parse(redirect_url) + + # Make sure we are not copying over the POST headers from the original request + headers.delete("Content-MD5") + headers.delete("Content-Type") + headers.delete("Content-Length") + + @context.not_nil!.get(uri) + end + def http_request(uri, method, params) case uri.scheme.not_nil!.downcase when "http", "https" then From 810f9ae430d3790b84dd8cbcc9aa3b4ee1be32f6 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Tue, 17 Aug 2021 14:30:08 +0900 Subject: [PATCH 2/2] complete url if location header is relative path --- spec/redirect_spec.cr | 12 ++++++++++++ src/mechanize/http/agent.cr | 6 +++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/spec/redirect_spec.cr b/spec/redirect_spec.cr index 89c56b9..27f3e32 100644 --- a/spec/redirect_spec.cr +++ b/spec/redirect_spec.cr @@ -6,6 +6,10 @@ WebMock.stub(:post, "http://example.com/post") .with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}) .to_return(status: 302, body: "redirect", headers: {"Location" => "http://example.com/redirect"}) +WebMock.stub(:post, "http://example.com/post2") + .with(body: "email=foobar", headers: {"Content-Type" => "application/x-www-form-urlencoded"}) + .to_return(status: 302, body: "redirect", headers: {"Location" => "/redirect"}) + describe "Mechanize redirect test" do it "redirect" do agent = Mechanize.new @@ -14,4 +18,12 @@ describe "Mechanize redirect test" do page.body.should eq("success") page.code.should eq(200) end + + it "redirect with relative path" do + agent = Mechanize.new + query = {"email" => "foobar"} + page = agent.post("http://example.com/post2", query: query) + page.body.should eq("success") + page.code.should eq(200) + end end diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 4b57f2a..e1e80b3 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -32,15 +32,15 @@ module MechanizeCr save_response_cookies(response, uri, page) if response && response.status.redirection? - return follow_redirect(response, headers) + return follow_redirect(response, headers, page) end page end - private def follow_redirect(response, headers) + private def follow_redirect(response, headers, referer) redirect_url = response.headers["location"] - uri = URI.parse(redirect_url) + uri = resolve_url(redirect_url, referer) # Make sure we are not copying over the POST headers from the original request headers.delete("Content-MD5")