summaryrefslogtreecommitdiff
path: root/lib/uri/http.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/uri/http.rb')
-rw-r--r--lib/uri/http.rb39
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/uri/http.rb b/lib/uri/http.rb
index 6e9c963ef1..900b132c8c 100644
--- a/lib/uri/http.rb
+++ b/lib/uri/http.rb
@@ -80,6 +80,45 @@ module URI
url = @query ? "#@path?#@query" : @path.dup
url.start_with?(?/.freeze) ? url : ?/ + url
end
+
+ #
+ # == Description
+ #
+ # Returns the authority for an HTTP uri, as defined in
+ # https://www.rfc-editor.org/rfc/rfc3986#section-3.2.
+ #
+ #
+ # Example:
+ #
+ # URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').authority #=> "www.example.com"
+ # URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').authority #=> "www.example.com:8000"
+ # URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').authority #=> "www.example.com"
+ #
+ def authority
+ if port == default_port
+ host
+ else
+ "#{host}:#{port}"
+ end
+ end
+
+ #
+ # == Description
+ #
+ # Returns the origin for an HTTP uri, as defined in
+ # https://www.rfc-editor.org/rfc/rfc6454.
+ #
+ #
+ # Example:
+ #
+ # URI::HTTP.build(host: 'www.example.com', path: '/foo/bar').origin #=> "http://www.example.com"
+ # URI::HTTP.build(host: 'www.example.com', port: 8000, path: '/foo/bar').origin #=> "http://www.example.com:8000"
+ # URI::HTTP.build(host: 'www.example.com', port: 80, path: '/foo/bar').origin #=> "http://www.example.com"
+ # URI::HTTPS.build(host: 'www.example.com', path: '/foo/bar').origin #=> "https://www.example.com"
+ #
+ def origin
+ "#{scheme}://#{authority}"
+ end
end
register_scheme 'HTTP', HTTP