-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the custom URI parser to be a bit more conservative
First try the default URI(), and if it fails relax the restrictions on the path component as a fallback.
- Loading branch information
Showing
5 changed files
with
44 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,36 @@ | ||
require 'singleton' | ||
module HTTP::Cookie::URIParser | ||
module_function | ||
|
||
class HTTP::Cookie::URIParser | ||
include Singleton | ||
# Regular Expression taken from RFC 3986 Appendix B | ||
URIREGEX = %r{ | ||
\A | ||
(?: (?<scheme> [^:/?\#]+ ) : )? | ||
(?: // (?<authority> [^/?\#]* ) )? | ||
(?<path> [^?\#]* ) | ||
(?: \? (?<query> [^\#]* ) )? | ||
(?: \# (?<fragment> .* ) )? | ||
\z | ||
}x | ||
|
||
REGEXP = { | ||
ABS_PATH: /\A[^?#]*\z/ | ||
} | ||
# Escape RFC 3986 "reserved" characters minus valid characters for path | ||
# More specifically, gen-delims minus "/" / "?" / "#" | ||
def escape_path(path) | ||
path.sub(/\A[^?#]+/) { |p| p.gsub(/[:\[\]@]+/) { |r| CGI.escape(r) } } | ||
end | ||
|
||
# Parse a URI string or object, relaxing the constraints on the path component | ||
def parse(uri) | ||
m = / | ||
\A | ||
(?<scheme>https?) | ||
:\/\/ | ||
((?<userinfo>.*)@)? | ||
(?<host>[^\/]+) | ||
(:(?<port>\d+))? | ||
(?<path>[^?#]*) | ||
(\?(?<query>[^#]*))? | ||
(\#(?<fragment>.*))? | ||
/xi.match(uri.to_s) | ||
URI(uri) | ||
rescue URI::InvalidURIError | ||
str = String.try_convert(uri) or | ||
raise ArgumentError, "bad argument (expected URI object or URI string)" | ||
|
||
# Not an absolute HTTP/HTTPS URI | ||
return URI::DEFAULT_PARSER.parse(uri) unless m | ||
m = URIREGEX.match(str) or raise | ||
|
||
URI.scheme_list[m['scheme'].upcase].new( | ||
m['scheme'], | ||
m['userinfo'], | ||
m['host'], | ||
m['port'], | ||
nil, # registry | ||
m['path'], | ||
nil, # opaque | ||
m['query'], | ||
m['fragment'], | ||
self | ||
) | ||
end | ||
|
||
def convert_to_uri(uri) | ||
if uri.is_a?(URI::Generic) | ||
uri | ||
elsif uri = String.try_convert(uri) | ||
parse(uri) | ||
else | ||
raise ArgumentError, "bad argument (expected URI object or URI string)" | ||
end | ||
path = m[:path] | ||
str[m.begin(:path)...m.end(:path)] = escape_path(path) | ||
uri = URI.parse(str) | ||
uri.__send__(:set_path, path) | ||
uri | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters