add comment
parent
69edb6e5ea
commit
6f71f4c928
|
@ -50,7 +50,7 @@ class Mechanize
|
||||||
headers.merge!(content_headers)
|
headers.merge!(content_headers)
|
||||||
|
|
||||||
# fetch the page
|
# fetch the page
|
||||||
page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data })#, cur_page
|
page = @agent.fetch(uri, :post, headers: headers, params: {"value" => request_data }, referer: cur_page)
|
||||||
headers.delete("Content-Type")
|
headers.delete("Content-Type")
|
||||||
headers.delete("Content-Length")
|
headers.delete("Content-Length")
|
||||||
add_to_history(page)
|
add_to_history(page)
|
||||||
|
|
|
@ -19,9 +19,8 @@ module MechanizeCr
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new,
|
def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new,
|
||||||
)#referer = current_page)
|
referer = (current_page unless history.empty?))
|
||||||
#referer_uri = referer.uri
|
uri = resolve_url(uri, referer)
|
||||||
uri = resolve(uri)
|
|
||||||
set_request_headers(uri, headers)
|
set_request_headers(uri, headers)
|
||||||
set_user_agent
|
set_user_agent
|
||||||
uri, params = resolve_parameters(uri, method, params)
|
uri, params = resolve_parameters(uri, method, params)
|
||||||
|
@ -104,16 +103,35 @@ module MechanizeCr
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def resolve(uri) : URI
|
private def resolve_url(uri, referer) : URI
|
||||||
if uri.class == URI || uri.to_s.includes?("http")
|
if uri.is_a?(URI)
|
||||||
URI.parse(uri)
|
target_url = uri.dup
|
||||||
else
|
else
|
||||||
referer_uri = current_page.uri
|
target_url = uri.to_s.strip
|
||||||
host = referer_uri.host
|
referer_uri = referer.uri if referer
|
||||||
scheme = referer_uri.scheme
|
if target_url.empty?
|
||||||
uri = "/" + uri unless uri[0] == '/'
|
raise ArgumentError.new("absolute URL needed")
|
||||||
new_uri = URI.new(scheme: scheme, host: host, path: uri)
|
end
|
||||||
|
# escape non-ascii character
|
||||||
|
target_url = target_url.gsub(/[^#{0.chr}-#{126.chr}]/) { |match|
|
||||||
|
URI.encode(match)
|
||||||
|
}
|
||||||
|
target_url = URI.parse(target_url)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# fill host if host isn't set
|
||||||
|
if target_url.host.nil? && referer && referer_uri.try &.host
|
||||||
|
target_url.host = referer_uri.not_nil!.host
|
||||||
|
end
|
||||||
|
# fill scheme if scheme isn't set
|
||||||
|
if target_url.relative?
|
||||||
|
target_url.scheme = "http"
|
||||||
|
end
|
||||||
|
# fill path's slash if there's no slash.
|
||||||
|
if target_url.path && (target_url.path.empty? || target_url.path[0] != '/')
|
||||||
|
target_url.path = "/#{target_url.path}"
|
||||||
|
end
|
||||||
|
target_url
|
||||||
end
|
end
|
||||||
|
|
||||||
# reset request cookie before setting headers.
|
# reset request cookie before setting headers.
|
||||||
|
|
Loading…
Reference in New Issue