Skip to content

Commit

Permalink
merge modification from yegor256-master
Browse files Browse the repository at this point in the history
  • Loading branch information
doc75 committed Oct 25, 2016
2 parents d1eadfe + b6b73a1 commit ca9f0aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 41 deletions.
58 changes: 29 additions & 29 deletions lib/w3c_validators/markup_validator.rb
@@ -1,15 +1,15 @@
module W3CValidators
class MarkupValidator < Validator
MARKUP_VALIDATOR_URI = 'http://validator.w3.org/check'
MARKUP_VALIDATOR_URI = 'https://validator.w3.org/check'

# Create a new instance of the MarkupValidator.
#
# ==== Options
# The +options+ hash allows you to set request parameters (see
# http://validator.w3.org/docs/api.html#requestformat) quickly. Request
# The +options+ hash allows you to set request parameters (see
# http://validator.w3.org/docs/api.html#requestformat) quickly. Request
# parameters can also be set using set_charset!, set_debug! and set_doctype!.
#
# You can pass in your own validator's URI (i.e.
# You can pass in your own validator's URI (i.e.
# <tt>MarkupValidator.new(:validator_uri => 'http://localhost/check')</tt>).
#
# See Validator#new for proxy server options.
Expand All @@ -22,14 +22,14 @@ def initialize(options = {})
end
super(options)
end
# Specify the character encoding to use when parsing the document.

# Specify the character encoding to use when parsing the document.
#
# When +only_as_fallback+ is +true+, the given encoding will only be
# used as a fallback value, in case the +charset+ is absent or unrecognized.
# When +only_as_fallback+ is +true+, the given encoding will only be
# used as a fallback value, in case the +charset+ is absent or unrecognized.
#
# +charset+ can be a string (e.g. <tt>set_charset!('utf-8')</tt>) or
# a symbol (e.g. <tt>set_charset!(:utf_8)</tt>) from the
# +charset+ can be a string (e.g. <tt>set_charset!('utf-8')</tt>) or
# a symbol (e.g. <tt>set_charset!(:utf_8)</tt>) from the
# W3CValidators::CHARSETS hash.
#
# Has no effect when using validate_uri_quickly.
Expand All @@ -45,14 +45,14 @@ def set_charset!(charset, only_as_fallback = false)
@options[:fbc] = only_as_fallback
end

# Specify the Document Type (+DOCTYPE+) to use when parsing the document.
# Specify the Document Type (+DOCTYPE+) to use when parsing the document.
#
# When +only_as_fallback+ is +true+, the given document type will only be
# used as a fallback value, in case the document's +DOCTYPE+ declaration
# When +only_as_fallback+ is +true+, the given document type will only be
# used as a fallback value, in case the document's +DOCTYPE+ declaration
# is missing or unrecognized.
#
# +doctype+ can be a string (e.g. <tt>set_doctype!('HTML 3.2')</tt>) or
# a symbol (e.g. <tt>set_doctype!(:html32)</tt>) from the
# +doctype+ can be a string (e.g. <tt>set_doctype!('HTML 3.2')</tt>) or
# a symbol (e.g. <tt>set_doctype!(:html32)</tt>) from the
# W3CValidators::DOCTYPES hash.
#
# Has no effect when using validate_uri_quickly.
Expand All @@ -68,11 +68,11 @@ def set_doctype!(doctype, only_as_fallback = false)
@options[:fbd] = only_as_fallback
end

# When set the validator will output some extra debugging information on
# the validated resource (such as HTTP headers) and validation process
# When set the validator will output some extra debugging information on
# the validated resource (such as HTTP headers) and validation process
# (such as parser used, parse mode, etc.).
#
# Debugging information is stored in the Results +debug_messages+ hash.
# Debugging information is stored in the Results +debug_messages+ hash.
# Custom debugging messages can be set with Results#add_debug_message.
#
# Has no effect when using validate_uri_quickly.
Expand Down Expand Up @@ -100,7 +100,7 @@ def validate_uri_quickly(uri)
def validate_text(text)
return validate({:fragment => text}, false)
end

# Validate the markup of a local file.
#
# +file_path+ may be either the fully-expanded path to the file or
Expand All @@ -112,7 +112,7 @@ def validate_file(file_path)
src = file_path.read
else
src = read_local_file(file_path)
end
end

return validate({:uploaded_file => src, :file_path => file_path}, false)
end
Expand All @@ -138,9 +138,9 @@ def validate(options, quick = false) # :nodoc:
# Perform sanity checks on request params
def get_request_options(options) # :nodoc:
options = @options.merge(options)

options[:output] = SOAP_OUTPUT_PARAM

unless options[:uri] or options[:uploaded_file] or options[:fragment]
raise ArgumentError, "an uri, uploaded file or fragment is required."
end
Expand All @@ -150,7 +150,7 @@ def get_request_options(options) # :nodoc:
if options[:uri] and not options[:uri].kind_of?(String)
options[:uri] = options[:uri].to_s
end

# Convert booleans to integers
[:fbc, :fbd, :verbose, :debug, :ss, :outline].each do |k|
if options.has_key?(k) and not options[k].kind_of?(Fixnum)
Expand All @@ -169,12 +169,12 @@ def get_request_options(options) # :nodoc:
# Returns W3CValidators::Results.
def parse_soap_response(response) # :nodoc:
doc = Nokogiri::XML(response)
doc.remove_namespaces!
doc.remove_namespaces!

result_params = {}

{:doctype => 'doctype', :uri => 'uri', :charset => 'charset',
:checked_by => 'checkedby', :validity => 'validity'}.each do |local_key, remote_key|
{:doctype => 'doctype', :uri => 'uri', :charset => 'charset',
:checked_by => 'checkedby', :validity => 'validity'}.each do |local_key, remote_key|
if val = doc.css(remote_key)
result_params[local_key] = val.text
end
Expand All @@ -195,7 +195,7 @@ def parse_soap_response(response) # :nodoc:
doc.css("Fault Reason Text").each do |message|
results.add_message(:error, {:mesage => message.text})
end

doc.css("markupvalidationresponse debug").each do |debug|
results.add_debug_message(debug.attribute('name').value, debug.text)
end
Expand All @@ -212,7 +212,7 @@ def parse_soap_response(response) # :nodoc:
# Returns Results.
def parse_head_response(response, validated_uri = nil) # :nodoc:
validity = (response[HEAD_STATUS_HEADER].downcase == 'valid')

results = Results.new(:uri => validated_uri, :validity => validity)

# Fill the results with empty error messages so we can count them
Expand All @@ -222,6 +222,6 @@ def parse_head_response(response, validated_uri = nil) # :nodoc:
results
end


end
end
25 changes: 13 additions & 12 deletions lib/w3c_validators/validator.rb
Expand Up @@ -28,7 +28,7 @@ class Validator
# - +proxy_user+
# - +proxy_pass+
def initialize(options = {})
@options = {:proxy_host => nil,
@options = {:proxy_host => nil,
:proxy_port => nil,
:proxy_user => nil,
:proxy_pass => nil}.merge(options)
Expand All @@ -45,18 +45,18 @@ def send_request(options, request_mode = :get, following_redirect = false, param

Net::HTTP::Proxy(@options[:proxy_host],
@options[:proxy_port],
@options[:proxy_user],
@options[:proxy_pass]).start(@validator_uri.host, @validator_uri.port) do |http|
@options[:proxy_user],
@options[:proxy_pass]).start(@validator_uri.host, @validator_uri.port) do |http|

case request_mode
when :head
# perform a HEAD request
raise ArgumentError, "a URI must be provided for HEAD requests." unless options[:uri]
query = create_query_string_data(options)
response = http.request_head(@validator_uri.path + '?' + query)
when :get
when :get
# send a GET request
query = create_query_string_data(options)
query = create_query_string_data(options)
response = http.get(@validator_uri.path + '?' + query)
when :post
# send a multipart form request
Expand All @@ -69,10 +69,11 @@ def send_request(options, request_mode = :get, following_redirect = false, param
post = options
options = {}
end

qs = create_query_string_data(options)

query, boundary = create_multipart_data(post)
http.use_ssl = true if @validator_uri.port == 443
response = http.post2(@validator_uri.path + '?' + qs, query, "Content-type" => "multipart/form-data; boundary=" + boundary)
else
raise ArgumentError, "request_mode must be either :get, :head or :post"
Expand Down Expand Up @@ -110,7 +111,7 @@ def create_multipart_data(options) # :nodoc:
options.delete(:uploaded_file)
options.delete(:file_path)
end

if options[:content]
last_params << "Content-Disposition: form-data; name=\"#{CGI::escape('content')}\"\r\n" + "\r\n" + "#{options[:content]}\r\n"
end
Expand All @@ -124,14 +125,14 @@ def create_multipart_data(options) # :nodoc:

params = misc_params + last_params

multipart_query = params.collect {|p| '--' + boundary + "\r\n" + p}.join('') + "--" + boundary + "--\r\n"
multipart_query = params.collect {|p| '--' + boundary + "\r\n" + p}.join('') + "--" + boundary + "--\r\n"

[multipart_query, boundary]
end

def create_query_string_data(options) # :nodoc:
qs = ''
options.each do |key, value|
options.each do |key, value|
if value
qs += "#{key}=" + CGI::escape(value.to_s) + "&"
end
Expand All @@ -148,7 +149,7 @@ def read_local_file(file_path) # :nodoc:
# Big thanks to ara.t.howard and Joel VanderWerf on Ruby-Talk for the exception handling help.
#++
def handle_exception(e, msg = '') # :nodoc:
case e
case e
when Net::HTTPServerException, SocketError
msg = "unable to connect to the validator at #{@validator_uri} (response was #{e.message})."
raise ValidatorUnavailable, msg, caller
Expand Down Expand Up @@ -182,6 +183,6 @@ def handle_exception(e, msg = '') # :nodoc:
exit_status(( exit_failure )) if exit_status == exit_success
exit_status(( Integer(exit_status) rescue(exit_status ? 0 : 1) ))
exit exit_status
end
end
end
end

0 comments on commit ca9f0aa

Please sign in to comment.