Debian Codegraph @

ruby/lib/core/uri.rb

raw
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing,
12 # software distributed under the License is distributed on an
13 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 # KIND, either express or implied. See the License for the
15 # specific language governing permissions and limitations
16 # under the License.
17
18
19 require 'uri'
20
21 # Extend the standard ruby {URI} with AMQP and AMQPS schemes
22 module URI
23 # monkey patch register_scheme for earlier versions of ruby
24 if !self.respond_to? :register_scheme
25 def self.register_scheme (scheme, klass)
26 @@schemes[scheme] = klass
27 end
28 end
29
30 # AMQP URI scheme for the AMQP protocol
31 class AMQP < Generic
32 DEFAULT_PORT = 5672
33
34 # @return [String] The AMQP address is the {#path} stripped of any leading "/"
35 def amqp_address() path[0] == "/" ? path[1..-1] : path; end
36 end
37 register_scheme 'AMQP', AMQP
38
39 # AMQPS URI scheme for the AMQP protocol over TLS
40 class AMQPS < AMQP
41 DEFAULT_PORT = 5671
42 end
43 register_scheme 'AMQPS', AMQPS
44 end
45
46 module Qpid::Proton
47 private
48 # Make sure to allow empty hostnames, Ruby 2.0.0 does not.
49 DEFAULT_URI_PARSER = URI::Parser.new(:HOSTNAME => /(?:#{URI::PATTERN::HOSTNAME})|/)
50
51 public
52
53 # Convert +s+ to an amqp: or amqps: URI
54 #
55 # This does not give the same result as the standard URI parser in all cases.
56 # Short-cut strings like "host:port" are allowed, an "amqp://" prefix is added if +s+ does
57 # not already look like an 'amqp:' or 'amqps:' URI.
58 #
59 # @param s [String,URI] String to convert to a URI, or a URI object.
60 # @return [URI] A valid AMQP or AMQPS URI
61 # @raise [URI::BadURIError] s is a URI object with a non-AMQP scheme
62 # @raise [URI::InvalidURIError] s cannot be parsed as a URI or shortcut
63 # @raise [::ArgumentError] s is not a string or URI
64 #
65 def self.uri(s)
66 case s
67 when URI::AMQP then s # This is already an AMQP or AMQPS URL.
68 when URI::Generic # Re-parse a generic URI that was not parsed as AMQP/AMQPS class
69 s.scheme ||= 'amqp' # Default to amqp: scheme
70 u = DEFAULT_URI_PARSER.parse(s.to_s)
71 raise URI::BadURIError, "Not an AMQP URI: '#{u}'" unless u.is_a? URI::AMQP
72 u
73 else
74 s = String.try_convert s
75 raise ::ArgumentError, "bad argument (expected URI object or URI string)" unless s
76 case s
77 when %r{^amqps?:} then DEFAULT_URI_PARSER.parse(s) # Looks like an AMQP URI
78 when %r{^//} then DEFAULT_URI_PARSER.parse("amqp:#{s}") # Looks like an authority with no scheme
79 else DEFAULT_URI_PARSER.parse("amqp://#{s}") # Treat as a bare host:port/path string
80 end
81 end
82 rescue =>e
83 raise e.class, "#{self}.#{__method__}(#{s.inspect}): #{e}"
84 end
85 end