|
| 1 | +# frozen_string_literal: false |
| 2 | +# = uri/ws.rb |
| 3 | +# |
| 4 | +# Author:: Matt Muller <mamuller@amazon.com> |
| 5 | +# License:: You can redistribute it and/or modify it under the same term as Ruby. |
| 6 | +# Revision:: $Id$ |
| 7 | +# |
| 8 | +# See URI for general documentation |
| 9 | +# |
| 10 | + |
| 11 | +require_relative 'generic' |
| 12 | + |
| 13 | +module URI |
| 14 | + |
| 15 | + # |
| 16 | + # The syntax of WS URIs is defined in RFC6455 section 3. |
| 17 | + # |
| 18 | + # Note that the Ruby URI library allows WS URLs containing usernames and |
| 19 | + # passwords. This is not legal as per the RFC, but used to be |
| 20 | + # supported in Internet Explorer 5 and 6, before the MS04-004 security |
| 21 | + # update. See <URL:http://support.microsoft.com/kb/834489>. |
| 22 | + # |
| 23 | + class WS < Generic |
| 24 | + # A Default port of 80 for URI::WS. |
| 25 | + DEFAULT_PORT = 80 |
| 26 | + |
| 27 | + # An Array of the available components for URI::WS. |
| 28 | + COMPONENT = %i[ |
| 29 | + scheme |
| 30 | + userinfo host port |
| 31 | + path |
| 32 | + query |
| 33 | + ].freeze |
| 34 | + |
| 35 | + # |
| 36 | + # == Description |
| 37 | + # |
| 38 | + # Creates a new URI::WS object from components, with syntax checking. |
| 39 | + # |
| 40 | + # The components accepted are userinfo, host, port, path, and query. |
| 41 | + # |
| 42 | + # The components should be provided either as an Array, or as a Hash |
| 43 | + # with keys formed by preceding the component names with a colon. |
| 44 | + # |
| 45 | + # If an Array is used, the components must be passed in the |
| 46 | + # order <code>[userinfo, host, port, path, query]</code>. |
| 47 | + # |
| 48 | + # Example: |
| 49 | + # |
| 50 | + # uri = URI::WS.build(host: 'www.example.com', path: '/foo/bar') |
| 51 | + # |
| 52 | + # uri = URI::WS.build([nil, "www.example.com", nil, "/path", "query"]) |
| 53 | + # |
| 54 | + # Currently, if passed userinfo components this method generates |
| 55 | + # invalid WS URIs as per RFC 1738. |
| 56 | + # |
| 57 | + def self.build(args) |
| 58 | + tmp = Util.make_components_hash(self, args) |
| 59 | + super(tmp) |
| 60 | + end |
| 61 | + |
| 62 | + # |
| 63 | + # == Description |
| 64 | + # |
| 65 | + # Returns the full path for a WS URI, as required by Net::HTTP::Get. |
| 66 | + # |
| 67 | + # If the URI contains a query, the full path is URI#path + '?' + URI#query. |
| 68 | + # Otherwise, the path is simply URI#path. |
| 69 | + # |
| 70 | + # Example: |
| 71 | + # |
| 72 | + # uri = URI::WS.build(path: '/foo/bar', query: 'test=true') |
| 73 | + # uri.request_uri # => "/foo/bar?test=true" |
| 74 | + # |
| 75 | + def request_uri |
| 76 | + return unless @path |
| 77 | + |
| 78 | + url = @query ? "#@path?#@query" : @path.dup |
| 79 | + url.start_with?(?/.freeze) ? url : ?/ + url |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + @@schemes['WS'] = WS |
| 84 | + |
| 85 | +end |
0 commit comments