-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow non-RFC 3986-compliant URLs #45
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'singleton' | ||
|
||
class HTTP::Cookie::URIParser | ||
include Singleton | ||
|
||
REGEXP = { | ||
ABS_PATH: /\A[^?#]*\z/ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constant does not seem to be used in any way. |
||
|
||
def parse(uri) | ||
m = / | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
\A | ||
(?<scheme>https?) | ||
:\/\/ | ||
((?<userinfo>.*)@)? | ||
(?<host>[^\/]+) | ||
(:(?<port>\d+))? | ||
(?<path>[^?#]*) | ||
(\?(?<query>[^#]*))? | ||
(\#(?<fragment>.*))? | ||
/xi.match(uri.to_s) | ||
|
||
# Not an absolute HTTP/HTTPS URI | ||
return URI::DEFAULT_PARSER.parse(uri) unless m | ||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should try the default URI.parse() first and then our custom parser only if it fails. |
||
else | ||
raise ArgumentError, "bad argument (expected URI object or URI string)" | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need a class or instance for providing these methods. Make it a module and provide module functions.