add submit

master
Kanezoh 2021-05-30 18:33:11 +09:00
parent fbd99a854f
commit 813ba7248b
4 changed files with 27 additions and 4 deletions

View File

@ -60,6 +60,13 @@ class Mechanize
@agent.request_headers = request_headers @agent.request_headers = request_headers
end end
def submit(form)
case form.method.upcase
when "POST"
post_form(form.action, form, request_headers)
end
end
def parse(uri, response, body) def parse(uri, response, body)
code = response.not_nil!.status_code code = response.not_nil!.status_code
MechanizeCr::Page.new(uri, response, body, code) MechanizeCr::Page.new(uri, response, body, code)

View File

@ -1,8 +1,8 @@
require "http/client" require "http/client"
class MechanizeCr::File class MechanizeCr::File
#property :body, :filename #property :body, :filename
property :body, :code property :body, :code, uri
def initialize(uri : URI | Nil, response : ::HTTP::Client::Response | Nil, body : String , code : Int32 | Nil) def initialize(uri : URI, response : ::HTTP::Client::Response | Nil, body : String , code : Int32 | Nil)
@uri = uri @uri = uri
@body = body @body = body
@code = code @code = code

View File

@ -5,6 +5,7 @@ class MechanizeCr::Form
getter fields : Array(MechanizeCr::FormContent::Field) getter fields : Array(MechanizeCr::FormContent::Field)
getter checkboxes : Array(MechanizeCr::FormContent::CheckBox) getter checkboxes : Array(MechanizeCr::FormContent::CheckBox)
getter enctype : String getter enctype : String
getter method : String
property action : String property action : String
def initialize(node : Node | Myhtml::Node) def initialize(node : Node | Myhtml::Node)
@ -14,7 +15,7 @@ class MechanizeCr::Form
@checkboxes = Array(MechanizeCr::FormContent::CheckBox).new @checkboxes = Array(MechanizeCr::FormContent::CheckBox).new
#@action = Mechanize::Util.html_unescape(node['action']) #@action = Mechanize::Util.html_unescape(node['action'])
@action = node["action"] @action = node["action"]
#@method = (node['method'] || 'GET').upcase @method = (node["method"] || "GET").upcase
#@name = node['name'] #@name = node['name']
#@clicked_buttons = [] #@clicked_buttons = []
#@page = page #@page = page

View File

@ -13,7 +13,7 @@ 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)
uri = URI.parse(uri) uri = resolve(uri)
set_request_headers(headers) set_request_headers(headers)
uri, params = resolve_parameters(uri, method, params) uri, params = resolve_parameters(uri, method, params)
response = http_request(uri, method, params) response = http_request(uri, method, params)
@ -72,6 +72,21 @@ module MechanizeCr
header_cookies.not_nil!.add_request_headers(request_headers) header_cookies.not_nil!.add_request_headers(request_headers)
end end
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 end
end end