From 813ba7248b4d9b3b74f21f4d8e8febe9e2080748 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Sun, 30 May 2021 18:33:11 +0900 Subject: [PATCH] add submit --- src/mechanize.cr | 7 +++++++ src/mechanize/file.cr | 4 ++-- src/mechanize/form.cr | 3 ++- src/mechanize/http/agent.cr | 17 ++++++++++++++++- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/mechanize.cr b/src/mechanize.cr index e52d4bb..77da83f 100644 --- a/src/mechanize.cr +++ b/src/mechanize.cr @@ -60,6 +60,13 @@ class Mechanize @agent.request_headers = request_headers end + def submit(form) + case form.method.upcase + when "POST" + post_form(form.action, form, request_headers) + end + end + def parse(uri, response, body) code = response.not_nil!.status_code MechanizeCr::Page.new(uri, response, body, code) diff --git a/src/mechanize/file.cr b/src/mechanize/file.cr index 72aa084..1e6b22a 100644 --- a/src/mechanize/file.cr +++ b/src/mechanize/file.cr @@ -1,8 +1,8 @@ require "http/client" class MechanizeCr::File #property :body, :filename - property :body, :code - def initialize(uri : URI | Nil, response : ::HTTP::Client::Response | Nil, body : String , code : Int32 | Nil) + property :body, :code, uri + def initialize(uri : URI, response : ::HTTP::Client::Response | Nil, body : String , code : Int32 | Nil) @uri = uri @body = body @code = code diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index 4034a9a..439b021 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -5,6 +5,7 @@ class MechanizeCr::Form getter fields : Array(MechanizeCr::FormContent::Field) getter checkboxes : Array(MechanizeCr::FormContent::CheckBox) getter enctype : String + getter method : String property action : String def initialize(node : Node | Myhtml::Node) @@ -14,7 +15,7 @@ class MechanizeCr::Form @checkboxes = Array(MechanizeCr::FormContent::CheckBox).new #@action = Mechanize::Util.html_unescape(node['action']) @action = node["action"] - #@method = (node['method'] || 'GET').upcase + @method = (node["method"] || "GET").upcase #@name = node['name'] #@clicked_buttons = [] #@page = page diff --git a/src/mechanize/http/agent.cr b/src/mechanize/http/agent.cr index 738d9f4..fa04905 100644 --- a/src/mechanize/http/agent.cr +++ b/src/mechanize/http/agent.cr @@ -13,7 +13,7 @@ module MechanizeCr end def fetch(uri, method = :get, headers = HTTP::Headers.new, params = Hash(String,String).new) - uri = URI.parse(uri) + uri = resolve(uri) set_request_headers(headers) uri, params = resolve_parameters(uri, method, params) response = http_request(uri, method, params) @@ -72,6 +72,21 @@ module MechanizeCr header_cookies.not_nil!.add_request_headers(request_headers) end end + + private def resolve(uri) : URI + if uri.class == URI || uri.to_s.includes?("http") + URI.parse(uri) + else + referer_uri = current_page.uri + host = referer_uri.host + scheme = referer_uri.scheme + new_uri = URI.new(scheme: scheme, host: host, path: uri) + end + end + + private def current_page + @history.last + end end end end