addressable 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,12 @@
1
+ === Addressable 1.0.0
2
+ * heuristic parse method added
3
+ * parsing is slightly more strict
4
+ * replaced to_h with to_hash
5
+ * fixed routing methods
6
+ * improved specifications
7
+ * improved heckle rake task
8
+ * no surviving heckle mutations
9
+
1
10
  === Addressable 0.1.2
2
11
  * improved normalization
3
12
  * fixed bug in joining algorithm
@@ -151,7 +151,7 @@ table.report tr.dark {
151
151
  </head>
152
152
  <body>
153
153
  <h3>C0 code coverage information</h3>
154
- <p>Generated on Sun Oct 28 17:27:26 -0400 2007 with <a href='http://eigenclass.org/hiki.rb?rcov'>rcov 0.8.0</a>
154
+ <p>Generated on Sat Nov 03 20:48:57 -0400 2007 with <a href='http://eigenclass.org/hiki.rb?rcov'>rcov 0.8.0</a>
155
155
  </p>
156
156
  <hr /> <table class='report'>
157
157
  <thead>
@@ -167,10 +167,10 @@ table.report tr.dark {
167
167
  <tr class='light'>
168
168
  <td>TOTAL</td>
169
169
  <td class='lines_total'>
170
- <tt>1238</tt>
170
+ <tt>1251</tt>
171
171
  </td>
172
172
  <td class='lines_code'>
173
- <tt>870</tt>
173
+ <tt>885</tt>
174
174
  </td>
175
175
  <td>
176
176
  <table cellspacing='0' cellpadding='0' align='right'>
@@ -210,10 +210,10 @@ table.report tr.dark {
210
210
  <a href='lib-addressable-uri_rb.html'>lib/addressable/uri.rb</a>
211
211
  </td>
212
212
  <td class='lines_total'>
213
- <tt>1206</tt>
213
+ <tt>1219</tt>
214
214
  </td>
215
215
  <td class='lines_code'>
216
- <tt>862</tt>
216
+ <tt>877</tt>
217
217
  </td>
218
218
  <td>
219
219
  <table cellspacing='0' cellpadding='0' align='right'>
@@ -556,7 +556,7 @@ span.run100 {
556
556
  </head>
557
557
  <body>
558
558
  <h3>C0 code coverage information</h3>
559
- <p>Generated on Sun Oct 28 17:27:34 -0400 2007 with <a href='http://eigenclass.org/hiki.rb?rcov'>rcov 0.8.0</a>
559
+ <p>Generated on Sat Nov 03 20:49:04 -0400 2007 with <a href='http://eigenclass.org/hiki.rb?rcov'>rcov 0.8.0</a>
560
560
  </p>
561
561
  <hr /><pre><span class='marked0'>Code reported as executed by Ruby looks like this...
562
562
  </span><span class='marked1'>and this: this line is also marked as covered.
@@ -580,10 +580,10 @@ span.run100 {
580
580
  <a href='lib-addressable-uri_rb.html'>lib/addressable/uri.rb</a>
581
581
  </td>
582
582
  <td class='lines_total'>
583
- <tt>1206</tt>
583
+ <tt>1219</tt>
584
584
  </td>
585
585
  <td class='lines_code'>
586
- <tt>862</tt>
586
+ <tt>877</tt>
587
587
  </td>
588
588
  <td>
589
589
  <table cellspacing='0' cellpadding='0' align='right'>
@@ -714,1117 +714,1130 @@ span.run100 {
714
714
  </span><span class="marked1"><a name="line93" /> 93 host = authority.gsub(/^([^\[\]]*)@/, &quot;&quot;).gsub(/:([^:@\[\]]*?)$/, &quot;&quot;)
715
715
  </span><span class="marked0"><a name="line94" /> 94 port = authority.scan(/:([^:@\[\]]*?)$/).flatten[0]
716
716
  </span><span class="inferred1"><a name="line95" /> 95 end
717
- </span><span class="marked0"><a name="line96" /> 96 if port.nil? || port == &quot;&quot;
717
+ </span><span class="marked0"><a name="line96" /> 96 if port == nil || port == &quot;&quot;
718
718
  </span><span class="marked1"><a name="line97" /> 97 port = nil
719
719
  </span><span class="inferred0"><a name="line98" /> 98 end
720
720
  </span><span class="inferred1"><a name="line99" /> 99
721
- </span><span class="inferred0"><a name="line100" /> 100 # WARNING: Not standards-compliant, but follows the theme
722
- </span><span class="inferred1"><a name="line101" /> 101 # of Postel's law:
723
- </span><span class="inferred0"><a name="line102" /> 102 #
724
- </span><span class="inferred1"><a name="line103" /> 103 # Special exception for dealing with the retarded idea of the
725
- </span><span class="inferred0"><a name="line104" /> 104 # feed pseudo-protocol. Without this exception, the parser will read
726
- </span><span class="inferred1"><a name="line105" /> 105 # the URI as having a blank port number, instead of as having a second
727
- </span><span class="inferred0"><a name="line106" /> 106 # URI embedded within. This exception translates these broken URIs
728
- </span><span class="inferred1"><a name="line107" /> 107 # and instead treats the inner URI as opaque.
729
- </span><span class="marked0"><a name="line108" /> 108 if scheme == &quot;feed&quot; &amp;&amp; host == &quot;http&quot;
730
- </span><span class="marked1"><a name="line109" /> 109 userinfo = nil
731
- </span><span class="marked0"><a name="line110" /> 110 user = nil
732
- </span><span class="marked1"><a name="line111" /> 111 password = nil
733
- </span><span class="marked0"><a name="line112" /> 112 host = nil
734
- </span><span class="marked1"><a name="line113" /> 113 port = nil
735
- </span><span class="marked0"><a name="line114" /> 114 path = authority + path
736
- </span><span class="inferred1"><a name="line115" /> 115 end
737
- </span><span class="inferred0"><a name="line116" /> 116
738
- </span><span class="marked1"><a name="line117" /> 117 return Addressable::URI.new(
739
- </span><span class="marked0"><a name="line118" /> 118 scheme, user, password, host, port, path, query, fragment)
740
- </span><span class="inferred1"><a name="line119" /> 119 end
741
- </span><span class="inferred0"><a name="line120" /> 120
742
- </span><span class="inferred1"><a name="line121" /> 121 # Converts a path to a file protocol URI. If the path supplied is
743
- </span><span class="inferred0"><a name="line122" /> 122 # relative, it will be returned as a relative URI. If the path supplied
744
- </span><span class="inferred1"><a name="line123" /> 123 # is actually a URI, it will return the parsed URI.
745
- </span><span class="marked0"><a name="line124" /> 124 def self.convert_path(path)
746
- </span><span class="marked1"><a name="line125" /> 125 return nil if path.nil?
747
- </span><span class="inferred0"><a name="line126" /> 126
748
- </span><span class="marked1"><a name="line127" /> 127 converted_uri = path.strip
749
- </span><span class="marked0"><a name="line128" /> 128 if converted_uri.length &gt; 0 &amp;&amp; converted_uri[0..0] == &quot;/&quot;
750
- </span><span class="marked1"><a name="line129" /> 129 converted_uri = &quot;file://&quot; + converted_uri
751
- </span><span class="inferred0"><a name="line130" /> 130 end
752
- </span><span class="inferred1"><a name="line131" /> 131 if converted_uri.length &gt; 0 &amp;&amp;
753
- </span><span class="marked0"><a name="line132" /> 132 converted_uri.scan(/^[a-zA-Z]:[\\\/]/).size &gt; 0
754
- </span><span class="marked1"><a name="line133" /> 133 converted_uri = &quot;file:///&quot; + converted_uri
755
- </span><span class="inferred0"><a name="line134" /> 134 end
756
- </span><span class="marked1"><a name="line135" /> 135 converted_uri.gsub!(/^file:\/*/i, &quot;file:///&quot;)
757
- </span><span class="marked0"><a name="line136" /> 136 if converted_uri =~ /^file:/i
758
- </span><span class="inferred1"><a name="line137" /> 137 # Adjust windows-style uris
759
- </span><span class="marked0"><a name="line138" /> 138 converted_uri.gsub!(/^file:\/\/\/([a-zA-Z])\|/i, 'file:///\1:')
760
- </span><span class="marked1"><a name="line139" /> 139 converted_uri.gsub!(/\\/, '/')
761
- </span><span class="marked0"><a name="line140" /> 140 converted_uri = self.parse(converted_uri).normalize
762
- </span><span class="marked1"><a name="line141" /> 141 if File.exists?(converted_uri.path) &amp;&amp;
763
- </span><span class="marked0"><a name="line142" /> 142 File.stat(converted_uri.path).directory?
764
- </span><span class="marked1"><a name="line143" /> 143 converted_uri.path.gsub!(/\/$/, &quot;&quot;)
765
- </span><span class="marked0"><a name="line144" /> 144 converted_uri.path = converted_uri.path + '/'
766
- </span><span class="inferred1"><a name="line145" /> 145 end
767
- </span><span class="inferred0"><a name="line146" /> 146 else
768
- </span><span class="marked1"><a name="line147" /> 147 converted_uri = self.parse(converted_uri)
769
- </span><span class="inferred0"><a name="line148" /> 148 end
770
- </span><span class="inferred1"><a name="line149" /> 149
771
- </span><span class="marked0"><a name="line150" /> 150 return converted_uri
772
- </span><span class="inferred1"><a name="line151" /> 151 end
773
- </span><span class="inferred0"><a name="line152" /> 152
774
- </span><span class="inferred1"><a name="line153" /> 153 # Expands a URI template into a full URI.
775
- </span><span class="inferred0"><a name="line154" /> 154 #
776
- </span><span class="inferred1"><a name="line155" /> 155 # An optional processor object may be supplied. The object should
777
- </span><span class="inferred0"><a name="line156" /> 156 # respond to either the :validate or :transform messages or both.
778
- </span><span class="inferred1"><a name="line157" /> 157 # Both the :validate and :transform methods should take two parameters:
779
- </span><span class="inferred0"><a name="line158" /> 158 # :name and :value. The :validate method should return true or false;
780
- </span><span class="inferred1"><a name="line159" /> 159 # true if the value of the variable is valid, false otherwise. The
781
- </span><span class="inferred0"><a name="line160" /> 160 # :transform method should return the transformed variable value as a
782
- </span><span class="inferred1"><a name="line161" /> 161 # string.
783
- </span><span class="inferred0"><a name="line162" /> 162 #
784
- </span><span class="inferred1"><a name="line163" /> 163 # An example:
785
- </span><span class="inferred0"><a name="line164" /> 164 #
786
- </span><span class="inferred1"><a name="line165" /> 165 # class ExampleProcessor
787
- </span><span class="inferred0"><a name="line166" /> 166 # def self.validate(name, value)
788
- </span><span class="inferred1"><a name="line167" /> 167 # return !!(value =~ /^[\w ]+$/) if name == &quot;query&quot;
789
- </span><span class="inferred0"><a name="line168" /> 168 # return true
790
- </span><span class="inferred1"><a name="line169" /> 169 # end
791
- </span><span class="inferred0"><a name="line170" /> 170 #
792
- </span><span class="inferred1"><a name="line171" /> 171 # def self.transform(name, value)
793
- </span><span class="inferred0"><a name="line172" /> 172 # return value.gsub(/ /, &quot;+&quot;) if name == &quot;query&quot;
794
- </span><span class="inferred1"><a name="line173" /> 173 # return value
795
- </span><span class="inferred0"><a name="line174" /> 174 # end
796
- </span><span class="inferred1"><a name="line175" /> 175 # end
797
- </span><span class="inferred0"><a name="line176" /> 176 #
798
- </span><span class="inferred1"><a name="line177" /> 177 # Addressable::URI.expand_template(
799
- </span><span class="inferred0"><a name="line178" /> 178 # &quot;http://example.com/search/{query}/&quot;,
800
- </span><span class="inferred1"><a name="line179" /> 179 # {&quot;query&quot; =&gt; &quot;an example search query&quot;},
801
- </span><span class="inferred0"><a name="line180" /> 180 # ExampleProcessor).to_s
802
- </span><span class="inferred1"><a name="line181" /> 181 # =&gt; &quot;http://example.com/search/an+example+search+query/&quot;
803
- </span><span class="marked0"><a name="line182" /> 182 def self.expand_template(pattern, mapping, processor=nil)
804
- </span><span class="marked1"><a name="line183" /> 183 result = pattern.dup
805
- </span><span class="marked0"><a name="line184" /> 184 for name, value in mapping
806
- </span><span class="marked1"><a name="line185" /> 185 transformed_value = value
807
- </span><span class="marked0"><a name="line186" /> 186 if processor != nil
808
- </span><span class="marked1"><a name="line187" /> 187 if processor.respond_to?(:validate)
809
- </span><span class="marked0"><a name="line188" /> 188 if !processor.validate(name, value)
810
- </span><span class="marked1"><a name="line189" /> 189 raise InvalidTemplateValue,
811
- </span><span class="inferred0"><a name="line190" /> 190 &quot;(#{name}, #{value}) is an invalid template value.&quot;
812
- </span><span class="inferred1"><a name="line191" /> 191 end
813
- </span><span class="inferred0"><a name="line192" /> 192 end
814
- </span><span class="marked1"><a name="line193" /> 193 if processor.respond_to?(:transform)
815
- </span><span class="marked0"><a name="line194" /> 194 transformed_value = processor.transform(name, value)
816
- </span><span class="inferred1"><a name="line195" /> 195 end
817
- </span><span class="inferred0"><a name="line196" /> 196 end
818
- </span><span class="inferred1"><a name="line197" /> 197
819
- </span><span class="inferred0"><a name="line198" /> 198 # Handle percent escaping
820
- </span><span class="marked1"><a name="line199" /> 199 transformed_value = self.encode_segment(transformed_value,
821
- </span><span class="inferred0"><a name="line200" /> 200 Addressable::URI::CharacterClasses::RESERVED +
822
- </span><span class="inferred1"><a name="line201" /> 201 Addressable::URI::CharacterClasses::UNRESERVED)
823
- </span><span class="inferred0"><a name="line202" /> 202
824
- </span><span class="marked1"><a name="line203" /> 203 result.gsub!(/\{#{Regexp.escape(name)}\}/, transformed_value)
825
- </span><span class="inferred0"><a name="line204" /> 204 end
826
- </span><span class="marked1"><a name="line205" /> 205 result.gsub!(
827
- </span><span class="inferred0"><a name="line206" /> 206 /\{[#{Addressable::URI::CharacterClasses::UNRESERVED}]+\}/, &quot;&quot;)
828
- </span><span class="marked1"><a name="line207" /> 207 return Addressable::URI.parse(result)
829
- </span><span class="inferred0"><a name="line208" /> 208 end
830
- </span><span class="inferred1"><a name="line209" /> 209
831
- </span><span class="inferred0"><a name="line210" /> 210 # Extracts a mapping from the URI using a URI Template pattern.
832
- </span><span class="inferred1"><a name="line211" /> 211 # Returns nil if the pattern doesn't match the URI.
833
- </span><span class="inferred0"><a name="line212" /> 212 #
834
- </span><span class="inferred1"><a name="line213" /> 213 # An optional processor object may be supplied. The object should
835
- </span><span class="inferred0"><a name="line214" /> 214 # respond to either the :restore or :match messages or both.
836
- </span><span class="inferred1"><a name="line215" /> 215 # The :restore method should take two parameters: :name and :value.
837
- </span><span class="inferred0"><a name="line216" /> 216 # The :restore method should reverse any transformations that have been
838
- </span><span class="inferred1"><a name="line217" /> 217 # performed on the value to ensure a valid URI. The :match method
839
- </span><span class="inferred0"><a name="line218" /> 218 # should take a single parameter: :name. The :match method should
840
- </span><span class="inferred1"><a name="line219" /> 219 # return a String containing a regular expression capture group for
841
- </span><span class="inferred0"><a name="line220" /> 220 # matching on that particular variable. The default value is &quot;.*&quot;.
842
- </span><span class="inferred1"><a name="line221" /> 221 #
843
- </span><span class="inferred0"><a name="line222" /> 222 # An example:
844
- </span><span class="inferred1"><a name="line223" /> 223 #
845
- </span><span class="inferred0"><a name="line224" /> 224 # class ExampleProcessor
846
- </span><span class="inferred1"><a name="line225" /> 225 # def self.restore(name, value)
847
- </span><span class="inferred0"><a name="line226" /> 226 # return value.gsub(/\+/, &quot; &quot;) if name == &quot;query&quot;
848
- </span><span class="inferred1"><a name="line227" /> 227 # return value
849
- </span><span class="inferred0"><a name="line228" /> 228 # end
850
- </span><span class="inferred1"><a name="line229" /> 229 #
851
- </span><span class="inferred0"><a name="line230" /> 230 # def self.match(name)
852
- </span><span class="inferred1"><a name="line231" /> 231 # return &quot;.*?&quot; if name == &quot;first&quot;
853
- </span><span class="inferred0"><a name="line232" /> 232 # return &quot;.*&quot;
854
- </span><span class="inferred1"><a name="line233" /> 233 # end
855
- </span><span class="inferred0"><a name="line234" /> 234 # end
856
- </span><span class="inferred1"><a name="line235" /> 235 #
857
- </span><span class="inferred0"><a name="line236" /> 236 # uri = Addressable::URI.parse(
858
- </span><span class="inferred1"><a name="line237" /> 237 # &quot;http://example.com/search/an+example+search+query/&quot;)
859
- </span><span class="inferred0"><a name="line238" /> 238 # uri.extract_mapping(&quot;http://example.com/search/{query}/&quot;,
860
- </span><span class="inferred1"><a name="line239" /> 239 # ExampleProcessor)
861
- </span><span class="inferred0"><a name="line240" /> 240 # =&gt; {&quot;query&quot; =&gt; &quot;an example search query&quot;}
862
- </span><span class="inferred1"><a name="line241" /> 241 #
863
- </span><span class="inferred0"><a name="line242" /> 242 # uri = Addressable::URI.parse(
864
- </span><span class="inferred1"><a name="line243" /> 243 # &quot;http://example.com/a/b/c/&quot;)
865
- </span><span class="inferred0"><a name="line244" /> 244 # uri.extract_mapping(&quot;http://example.com/{first}/{second}/&quot;,
866
- </span><span class="inferred1"><a name="line245" /> 245 # ExampleProcessor)
867
- </span><span class="inferred0"><a name="line246" /> 246 # =&gt; {&quot;first&quot; =&gt; &quot;a&quot;, &quot;second&quot; =&gt; &quot;b/c&quot;}
868
- </span><span class="marked1"><a name="line247" /> 247 def extract_mapping(pattern, processor=nil)
869
- </span><span class="marked0"><a name="line248" /> 248 mapping = {}
870
- </span><span class="marked1"><a name="line249" /> 249 variable_regexp =
871
- </span><span class="inferred0"><a name="line250" /> 250 /\{([#{Addressable::URI::CharacterClasses::UNRESERVED}]+)\}/
872
- </span><span class="marked1"><a name="line251" /> 251 variables = pattern.scan(variable_regexp).flatten
873
- </span><span class="marked0"><a name="line252" /> 252 variables.each { |v| mapping[v] = &quot;&quot; }
874
- </span><span class="marked1"><a name="line253" /> 253 escaped_pattern =
875
- </span><span class="inferred0"><a name="line254" /> 254 Regexp.escape(pattern).gsub(/\\\{/, &quot;{&quot;).gsub(/\\\}/, &quot;}&quot;)
876
- </span><span class="marked1"><a name="line255" /> 255 regexp = Regexp.new(escaped_pattern.gsub(variable_regexp) do |v|
877
- </span><span class="marked0"><a name="line256" /> 256 capture_group = &quot;(.*)&quot;
878
- </span><span class="inferred1"><a name="line257" /> 257
879
- </span><span class="marked0"><a name="line258" /> 258 if processor != nil
880
- </span><span class="marked1"><a name="line259" /> 259 if processor.respond_to?(:match)
881
- </span><span class="marked0"><a name="line260" /> 260 name = v.scan(variable_regexp).flatten[0]
882
- </span><span class="marked1"><a name="line261" /> 261 capture_group = &quot;(#{processor.match(name)})&quot;
883
- </span><span class="inferred0"><a name="line262" /> 262 end
884
- </span><span class="inferred1"><a name="line263" /> 263 end
885
- </span><span class="inferred0"><a name="line264" /> 264
886
- </span><span class="marked1"><a name="line265" /> 265 capture_group
887
- </span><span class="inferred0"><a name="line266" /> 266 end)
888
- </span><span class="marked1"><a name="line267" /> 267 values = self.to_s.scan(regexp).flatten
889
- </span><span class="marked0"><a name="line268" /> 268 if variables.size == values.size
890
- </span><span class="marked1"><a name="line269" /> 269 for i in 0...variables.size
891
- </span><span class="marked0"><a name="line270" /> 270 name = variables[i]
892
- </span><span class="marked1"><a name="line271" /> 271 value = values[i]
893
- </span><span class="inferred0"><a name="line272" /> 272
894
- </span><span class="marked1"><a name="line273" /> 273 if processor != nil
895
- </span><span class="marked0"><a name="line274" /> 274 if processor.respond_to?(:restore)
896
- </span><span class="marked1"><a name="line275" /> 275 value = processor.restore(name, value)
897
- </span><span class="inferred0"><a name="line276" /> 276 end
898
- </span><span class="inferred1"><a name="line277" /> 277 end
899
- </span><span class="inferred0"><a name="line278" /> 278
900
- </span><span class="marked1"><a name="line279" /> 279 mapping[name] = value
901
- </span><span class="inferred0"><a name="line280" /> 280 end
902
- </span><span class="marked1"><a name="line281" /> 281 return mapping
903
- </span><span class="inferred0"><a name="line282" /> 282 else
904
- </span><span class="inferred1"><a name="line283" /> 283 # Pattern failed to match URI.
905
- </span><span class="marked0"><a name="line284" /> 284 return nil
906
- </span><span class="inferred1"><a name="line285" /> 285 end
907
- </span><span class="inferred0"><a name="line286" /> 286 end
908
- </span><span class="inferred1"><a name="line287" /> 287
909
- </span><span class="inferred0"><a name="line288" /> 288 # Joins several uris together.
910
- </span><span class="marked1"><a name="line289" /> 289 def self.join(*uris)
911
- </span><span class="marked0"><a name="line290" /> 290 uri_objects = uris.collect do |uri|
912
- </span><span class="marked1"><a name="line291" /> 291 uri.kind_of?(self) ? uri : self.parse(uri.to_s)
913
- </span><span class="inferred0"><a name="line292" /> 292 end
914
- </span><span class="marked1"><a name="line293" /> 293 result = uri_objects.shift.dup
915
- </span><span class="marked0"><a name="line294" /> 294 for uri in uri_objects
916
- </span><span class="marked1"><a name="line295" /> 295 result.merge!(uri)
917
- </span><span class="inferred0"><a name="line296" /> 296 end
918
- </span><span class="marked1"><a name="line297" /> 297 return result
919
- </span><span class="inferred0"><a name="line298" /> 298 end
920
- </span><span class="inferred1"><a name="line299" /> 299
921
- </span><span class="inferred0"><a name="line300" /> 300 # Percent encodes a URI segment. Returns a string. Takes an optional
922
- </span><span class="inferred1"><a name="line301" /> 301 # character class parameter, which should be specified as a string
923
- </span><span class="inferred0"><a name="line302" /> 302 # containing a regular expression character class (not including the
924
- </span><span class="inferred1"><a name="line303" /> 303 # surrounding square brackets). The character class parameter defaults
925
- </span><span class="inferred0"><a name="line304" /> 304 # to the reserved plus unreserved character classes specified in
926
- </span><span class="inferred1"><a name="line305" /> 305 # RFC 3986. Usage of the constants within the CharacterClasses module is
927
- </span><span class="inferred0"><a name="line306" /> 306 # highly recommended when using this method.
928
- </span><span class="inferred1"><a name="line307" /> 307 #
929
- </span><span class="inferred0"><a name="line308" /> 308 # An example:
930
- </span><span class="inferred1"><a name="line309" /> 309 #
931
- </span><span class="inferred0"><a name="line310" /> 310 # Addressable::URI.escape_segment(&quot;simple-example&quot;, &quot;b-zB-Z0-9&quot;)
932
- </span><span class="inferred1"><a name="line311" /> 311 # =&gt; &quot;simple%2Dex%61mple&quot;
933
- </span><span class="marked0"><a name="line312" /> 312 def self.encode_segment(segment, character_class=
934
- </span><span class="inferred1"><a name="line313" /> 313 Addressable::URI::CharacterClasses::RESERVED +
935
- </span><span class="marked0"><a name="line314" /> 314 Addressable::URI::CharacterClasses::UNRESERVED)
936
- </span><span class="marked1"><a name="line315" /> 315 return nil if segment.nil?
937
- </span><span class="inferred0"><a name="line316" /> 316 return segment.gsub(
938
- </span><span class="inferred1"><a name="line317" /> 317 /[^#{character_class}]/
939
- </span><span class="inferred0"><a name="line318" /> 318 ) do |sequence|
940
- </span><span class="marked1"><a name="line319" /> 319 (&quot;%&quot; + sequence.unpack('C')[0].to_s(16).upcase)
941
- </span><span class="marked0"><a name="line320" /> 320 end
942
- </span><span class="inferred1"><a name="line321" /> 321 end
943
- </span><span class="inferred0"><a name="line322" /> 322
944
- </span><span class="inferred1"><a name="line323" /> 323 # Unencodes any percent encoded characters within a URI segment.
945
- </span><span class="inferred0"><a name="line324" /> 324 # Returns a string.
946
- </span><span class="marked1"><a name="line325" /> 325 def self.unencode_segment(segment)
947
- </span><span class="marked0"><a name="line326" /> 326 return nil if segment.nil?
948
- </span><span class="inferred1"><a name="line327" /> 327 return segment.to_s.gsub(/%[0-9a-f]{2}/i) do |sequence|
949
- </span><span class="marked0"><a name="line328" /> 328 sequence[1..3].to_i(16).chr
950
- </span><span class="marked1"><a name="line329" /> 329 end
951
- </span><span class="inferred0"><a name="line330" /> 330 end
952
- </span><span class="inferred1"><a name="line331" /> 331
953
- </span><span class="inferred0"><a name="line332" /> 332 # Percent encodes any special characters in the URI. This method does
954
- </span><span class="inferred1"><a name="line333" /> 333 # not take IRIs or IDNs into account.
955
- </span><span class="marked0"><a name="line334" /> 334 def self.encode(uri)
956
- </span><span class="marked1"><a name="line335" /> 335 uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_s)
957
- </span><span class="marked0"><a name="line336" /> 336 return Addressable::URI.new(
958
- </span><span class="marked1"><a name="line337" /> 337 self.encode_segment(uri_object.scheme,
959
- </span><span class="inferred0"><a name="line338" /> 338 Addressable::URI::CharacterClasses::SCHEME),
960
- </span><span class="marked1"><a name="line339" /> 339 self.encode_segment(uri_object.user,
961
- </span><span class="inferred0"><a name="line340" /> 340 Addressable::URI::CharacterClasses::AUTHORITY),
962
- </span><span class="marked1"><a name="line341" /> 341 self.encode_segment(uri_object.password,
963
- </span><span class="inferred0"><a name="line342" /> 342 Addressable::URI::CharacterClasses::AUTHORITY),
964
- </span><span class="marked1"><a name="line343" /> 343 self.encode_segment(uri_object.host,
965
- </span><span class="inferred0"><a name="line344" /> 344 Addressable::URI::CharacterClasses::AUTHORITY),
966
- </span><span class="marked1"><a name="line345" /> 345 self.encode_segment(uri_object.specified_port,
967
- </span><span class="inferred0"><a name="line346" /> 346 Addressable::URI::CharacterClasses::AUTHORITY),
968
- </span><span class="marked1"><a name="line347" /> 347 self.encode_segment(uri_object.path,
969
- </span><span class="inferred0"><a name="line348" /> 348 Addressable::URI::CharacterClasses::PATH),
970
- </span><span class="marked1"><a name="line349" /> 349 self.encode_segment(uri_object.query,
971
- </span><span class="inferred0"><a name="line350" /> 350 Addressable::URI::CharacterClasses::QUERY),
972
- </span><span class="marked1"><a name="line351" /> 351 self.encode_segment(uri_object.fragment,
973
- </span><span class="inferred0"><a name="line352" /> 352 Addressable::URI::CharacterClasses::FRAGMENT)
974
- </span><span class="marked1"><a name="line353" /> 353 ).to_s
975
- </span><span class="inferred0"><a name="line354" /> 354 end
976
- </span><span class="inferred1"><a name="line355" /> 355
977
- </span><span class="marked0"><a name="line356" /> 356 class &lt;&lt; self
978
- </span><span class="marked1"><a name="line357" /> 357 alias_method :escape, :encode
979
- </span><span class="inferred0"><a name="line358" /> 358 end
980
- </span><span class="inferred1"><a name="line359" /> 359
981
- </span><span class="inferred0"><a name="line360" /> 360 # Normalizes the encoding of a URI. Characters within a hostname are
982
- </span><span class="inferred1"><a name="line361" /> 361 # not percent encoded to allow for internationalized domain names.
983
- </span><span class="marked0"><a name="line362" /> 362 def self.normalized_encode(uri)
984
- </span><span class="marked1"><a name="line363" /> 363 uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_s)
985
- </span><span class="marked0"><a name="line364" /> 364 segments = {
986
- </span><span class="marked1"><a name="line365" /> 365 :scheme =&gt; self.unencode_segment(uri_object.scheme),
987
- </span><span class="marked0"><a name="line366" /> 366 :user =&gt; self.unencode_segment(uri_object.user),
988
- </span><span class="marked1"><a name="line367" /> 367 :password =&gt; self.unencode_segment(uri_object.password),
989
- </span><span class="marked0"><a name="line368" /> 368 :host =&gt; self.unencode_segment(uri_object.host),
990
- </span><span class="marked1"><a name="line369" /> 369 :port =&gt; self.unencode_segment(uri_object.specified_port),
991
- </span><span class="marked0"><a name="line370" /> 370 :path =&gt; self.unencode_segment(uri_object.path),
992
- </span><span class="marked1"><a name="line371" /> 371 :query =&gt; self.unencode_segment(uri_object.query),
993
- </span><span class="marked0"><a name="line372" /> 372 :fragment =&gt; self.unencode_segment(uri_object.fragment)
994
- </span><span class="inferred1"><a name="line373" /> 373 }
995
- </span><span class="marked0"><a name="line374" /> 374 if URI::IDNA.send(:use_libidn?)
996
- </span><span class="marked1"><a name="line375" /> 375 segments.each do |key, value|
997
- </span><span class="marked0"><a name="line376" /> 376 if value != nil
998
- </span><span class="marked1"><a name="line377" /> 377 segments[key] = IDN::Stringprep.nfkc_normalize(value.to_s)
999
- </span><span class="inferred0"><a name="line378" /> 378 end
1000
- </span><span class="inferred1"><a name="line379" /> 379 end
1001
- </span><span class="inferred0"><a name="line380" /> 380 end
1002
- </span><span class="marked1"><a name="line381" /> 381 return Addressable::URI.new(
1003
- </span><span class="marked0"><a name="line382" /> 382 self.encode_segment(segments[:scheme],
1004
- </span><span class="inferred1"><a name="line383" /> 383 Addressable::URI::CharacterClasses::SCHEME),
1005
- </span><span class="marked0"><a name="line384" /> 384 self.encode_segment(segments[:user],
1006
- </span><span class="inferred1"><a name="line385" /> 385 Addressable::URI::CharacterClasses::AUTHORITY),
1007
- </span><span class="marked0"><a name="line386" /> 386 self.encode_segment(segments[:password],
1008
- </span><span class="inferred1"><a name="line387" /> 387 Addressable::URI::CharacterClasses::AUTHORITY),
1009
- </span><span class="inferred0"><a name="line388" /> 388 segments[:host],
1010
- </span><span class="inferred1"><a name="line389" /> 389 segments[:port],
1011
- </span><span class="marked0"><a name="line390" /> 390 self.encode_segment(segments[:path],
1012
- </span><span class="inferred1"><a name="line391" /> 391 Addressable::URI::CharacterClasses::PATH),
1013
- </span><span class="marked0"><a name="line392" /> 392 self.encode_segment(segments[:query],
1014
- </span><span class="inferred1"><a name="line393" /> 393 Addressable::URI::CharacterClasses::QUERY),
1015
- </span><span class="marked0"><a name="line394" /> 394 self.encode_segment(segments[:fragment],
1016
- </span><span class="inferred1"><a name="line395" /> 395 Addressable::URI::CharacterClasses::FRAGMENT)
1017
- </span><span class="marked0"><a name="line396" /> 396 ).to_s
1018
- </span><span class="inferred1"><a name="line397" /> 397 end
1019
- </span><span class="inferred0"><a name="line398" /> 398
1020
- </span><span class="inferred1"><a name="line399" /> 399 # Extracts uris from an arbitrary body of text.
1021
- </span><span class="marked0"><a name="line400" /> 400 def self.extract(text, options={})
1022
- </span><span class="marked1"><a name="line401" /> 401 defaults = {:base =&gt; nil, :parse =&gt; false}
1023
- </span><span class="marked0"><a name="line402" /> 402 options = defaults.merge(options)
1024
- </span><span class="marked1"><a name="line403" /> 403 raise InvalidOptionError unless (options.keys - defaults.keys).empty?
1025
- </span><span class="inferred0"><a name="line404" /> 404 # This regular expression needs to be less forgiving or else it would
1026
- </span><span class="inferred1"><a name="line405" /> 405 # match virtually all text. Which isn't exactly what we're going for.
1027
- </span><span class="marked0"><a name="line406" /> 406 extract_regex = /((([a-z\+]+):)[^ \n\&lt;\&gt;\&quot;\\]+[\w\/])/
1028
- </span><span class="marked1"><a name="line407" /> 407 extracted_uris =
1029
- </span><span class="marked0"><a name="line408" /> 408 text.scan(extract_regex).collect { |match| match[0] }
1030
- </span><span class="marked1"><a name="line409" /> 409 sgml_extract_regex = /&lt;[^&gt;]+href=\&quot;([^\&quot;]+?)\&quot;[^&gt;]*&gt;/
1031
- </span><span class="marked0"><a name="line410" /> 410 sgml_extracted_uris =
1032
- </span><span class="inferred1"><a name="line411" /> 411 text.scan(sgml_extract_regex).collect { |match| match[0] }
1033
- </span><span class="marked0"><a name="line412" /> 412 extracted_uris.concat(sgml_extracted_uris - extracted_uris)
1034
- </span><span class="marked1"><a name="line413" /> 413 textile_extract_regex = /\&quot;.+?\&quot;:([^ ]+\/[^ ]+)[ \,\.\;\:\?\!\&lt;\&gt;\&quot;]/i
1035
- </span><span class="marked0"><a name="line414" /> 414 textile_extracted_uris =
1036
- </span><span class="inferred1"><a name="line415" /> 415 text.scan(textile_extract_regex).collect { |match| match[0] }
1037
- </span><span class="marked0"><a name="line416" /> 416 extracted_uris.concat(textile_extracted_uris - extracted_uris)
1038
- </span><span class="marked1"><a name="line417" /> 417 parsed_uris = []
1039
- </span><span class="marked0"><a name="line418" /> 418 base_uri = nil
1040
- </span><span class="marked1"><a name="line419" /> 419 if options[:base] != nil
1041
- </span><span class="marked0"><a name="line420" /> 420 base_uri = options[:base] if options[:base].kind_of?(self)
1042
- </span><span class="marked1"><a name="line421" /> 421 base_uri = self.parse(options[:base].to_s) if base_uri == nil
1043
- </span><span class="inferred0"><a name="line422" /> 422 end
1044
- </span><span class="marked1"><a name="line423" /> 423 for uri_string in extracted_uris
1045
- </span><span class="marked0"><a name="line424" /> 424 begin
1046
- </span><span class="marked1"><a name="line425" /> 425 if base_uri == nil
1047
- </span><span class="marked0"><a name="line426" /> 426 parsed_uris &lt;&lt; self.parse(uri_string)
1048
- </span><span class="inferred1"><a name="line427" /> 427 else
1049
- </span><span class="marked0"><a name="line428" /> 428 parsed_uris &lt;&lt; (base_uri + self.parse(uri_string))
1050
- </span><span class="inferred1"><a name="line429" /> 429 end
1051
- </span><span class="inferred0"><a name="line430" /> 430 rescue Exception
1052
- </span><span class="marked1"><a name="line431" /> 431 nil
1053
- </span><span class="inferred0"><a name="line432" /> 432 end
1054
- </span><span class="inferred1"><a name="line433" /> 433 end
1055
- </span><span class="marked0"><a name="line434" /> 434 parsed_uris.reject! do |uri|
1056
- </span><span class="marked1"><a name="line435" /> 435 (uri.scheme =~ /T\d+/ ||
1057
- </span><span class="marked0"><a name="line436" /> 436 uri.scheme == &quot;xmlns&quot; ||
1058
- </span><span class="marked1"><a name="line437" /> 437 uri.scheme == &quot;xml&quot; ||
1059
- </span><span class="marked0"><a name="line438" /> 438 uri.scheme == &quot;thr&quot; ||
1060
- </span><span class="marked1"><a name="line439" /> 439 uri.scheme == &quot;this&quot; ||
1061
- </span><span class="marked0"><a name="line440" /> 440 uri.scheme == &quot;float&quot; ||
1062
- </span><span class="marked1"><a name="line441" /> 441 uri.scheme == &quot;user&quot; ||
1063
- </span><span class="marked0"><a name="line442" /> 442 uri.scheme == &quot;username&quot; ||
1064
- </span><span class="marked1"><a name="line443" /> 443 uri.scheme == &quot;out&quot;)
1065
- </span><span class="inferred0"><a name="line444" /> 444 end
1066
- </span><span class="marked1"><a name="line445" /> 445 if options[:parse]
1067
- </span><span class="marked0"><a name="line446" /> 446 return parsed_uris
1068
- </span><span class="inferred1"><a name="line447" /> 447 else
1069
- </span><span class="marked0"><a name="line448" /> 448 return parsed_uris.collect { |uri| uri.to_s }
1070
- </span><span class="inferred1"><a name="line449" /> 449 end
1071
- </span><span class="inferred0"><a name="line450" /> 450 end
1072
- </span><span class="inferred1"><a name="line451" /> 451
1073
- </span><span class="inferred0"><a name="line452" /> 452 # Creates a new uri object from component parts. Passing nil for
1074
- </span><span class="inferred1"><a name="line453" /> 453 # any of these parameters is acceptable.
1075
- </span><span class="marked0"><a name="line454" /> 454 def initialize(scheme, user, password, host, port, path, query, fragment)
1076
- </span><span class="marked1"><a name="line455" /> 455 @scheme = scheme
1077
- </span><span class="marked0"><a name="line456" /> 456 @scheme = nil if @scheme.to_s.strip == &quot;&quot;
1078
- </span><span class="marked1"><a name="line457" /> 457 @user = user
1079
- </span><span class="marked0"><a name="line458" /> 458 @password = password
1080
- </span><span class="marked1"><a name="line459" /> 459 @host = host
1081
- </span><span class="marked0"><a name="line460" /> 460 @specified_port = port.to_s
1082
- </span><span class="marked1"><a name="line461" /> 461 @port = port
1083
- </span><span class="marked0"><a name="line462" /> 462 @port = @port.to_s if @port.kind_of?(Fixnum)
1084
- </span><span class="marked1"><a name="line463" /> 463 if @port != nil &amp;&amp; !(@port =~ /^\d+$/)
1085
- </span><span class="marked0"><a name="line464" /> 464 raise InvalidURIError,
1086
- </span><span class="inferred1"><a name="line465" /> 465 &quot;Invalid port number: #{@port.inspect}&quot;
1087
- </span><span class="inferred0"><a name="line466" /> 466 end
1088
- </span><span class="marked1"><a name="line467" /> 467 @port = @port.to_i
1089
- </span><span class="marked0"><a name="line468" /> 468 @port = nil if @port == 0
1090
- </span><span class="marked1"><a name="line469" /> 469 @path = path
1091
- </span><span class="marked0"><a name="line470" /> 470 @query = query
1092
- </span><span class="marked1"><a name="line471" /> 471 @fragment = fragment
1093
- </span><span class="inferred0"><a name="line472" /> 472
1094
- </span><span class="marked1"><a name="line473" /> 473 validate()
1095
- </span><span class="inferred0"><a name="line474" /> 474 end
1096
- </span><span class="inferred1"><a name="line475" /> 475
1097
- </span><span class="inferred0"><a name="line476" /> 476 # Returns the scheme (protocol) for this URI.
1098
- </span><span class="marked1"><a name="line477" /> 477 def scheme
1099
- </span><span class="marked0"><a name="line478" /> 478 return @scheme
1100
- </span><span class="marked1"><a name="line479" /> 479 end
1101
- </span><span class="inferred0"><a name="line480" /> 480
1102
- </span><span class="inferred1"><a name="line481" /> 481 # Sets the scheme (protocol for this URI.)
1103
- </span><span class="marked0"><a name="line482" /> 482 def scheme=(new_scheme)
1104
- </span><span class="marked1"><a name="line483" /> 483 @scheme = new_scheme
1105
- </span><span class="marked0"><a name="line484" /> 484 end
1106
- </span><span class="inferred1"><a name="line485" /> 485
1107
- </span><span class="inferred0"><a name="line486" /> 486 # Returns the user for this URI.
1108
- </span><span class="marked1"><a name="line487" /> 487 def user
1109
- </span><span class="marked0"><a name="line488" /> 488 return @user
1110
- </span><span class="marked1"><a name="line489" /> 489 end
1111
- </span><span class="inferred0"><a name="line490" /> 490
1112
- </span><span class="inferred1"><a name="line491" /> 491 # Sets the user for this URI.
1113
- </span><span class="marked0"><a name="line492" /> 492 def user=(new_user)
1114
- </span><span class="marked1"><a name="line493" /> 493 @user = new_user
1115
- </span><span class="inferred0"><a name="line494" /> 494
1116
- </span><span class="inferred1"><a name="line495" /> 495 # You can't have a nil user with a non-nil password
1117
- </span><span class="marked0"><a name="line496" /> 496 if @password != nil
1118
- </span><span class="marked1"><a name="line497" /> 497 @user = &quot;&quot; if @user.nil?
1119
- </span><span class="inferred0"><a name="line498" /> 498 end
1120
- </span><span class="inferred1"><a name="line499" /> 499
1121
- </span><span class="inferred0"><a name="line500" /> 500 # Reset dependant values
1122
- </span><span class="marked1"><a name="line501" /> 501 @userinfo = nil
1123
- </span><span class="marked0"><a name="line502" /> 502 @authority = nil
1124
- </span><span class="inferred1"><a name="line503" /> 503
1125
- </span><span class="inferred0"><a name="line504" /> 504 # Ensure we haven't created an invalid URI
1126
- </span><span class="marked1"><a name="line505" /> 505 validate()
1127
- </span><span class="inferred0"><a name="line506" /> 506 end
1128
- </span><span class="inferred1"><a name="line507" /> 507
1129
- </span><span class="inferred0"><a name="line508" /> 508 # Returns the password for this URI.
1130
- </span><span class="marked1"><a name="line509" /> 509 def password
1131
- </span><span class="marked0"><a name="line510" /> 510 return @password
1132
- </span><span class="marked1"><a name="line511" /> 511 end
1133
- </span><span class="inferred0"><a name="line512" /> 512
1134
- </span><span class="inferred1"><a name="line513" /> 513 # Sets the password for this URI.
1135
- </span><span class="marked0"><a name="line514" /> 514 def password=(new_password)
1136
- </span><span class="marked1"><a name="line515" /> 515 @password = new_password
1137
- </span><span class="inferred0"><a name="line516" /> 516
1138
- </span><span class="inferred1"><a name="line517" /> 517 # You can't have a nil user with a non-nil password
1139
- </span><span class="marked0"><a name="line518" /> 518 if @password != nil
1140
- </span><span class="marked1"><a name="line519" /> 519 @user = &quot;&quot; if @user.nil?
1141
- </span><span class="inferred0"><a name="line520" /> 520 end
1142
- </span><span class="inferred1"><a name="line521" /> 521
1143
- </span><span class="inferred0"><a name="line522" /> 522 # Reset dependant values
1144
- </span><span class="marked1"><a name="line523" /> 523 @userinfo = nil
1145
- </span><span class="marked0"><a name="line524" /> 524 @authority = nil
1146
- </span><span class="inferred1"><a name="line525" /> 525
1147
- </span><span class="inferred0"><a name="line526" /> 526 # Ensure we haven't created an invalid URI
1148
- </span><span class="marked1"><a name="line527" /> 527 validate()
1149
- </span><span class="inferred0"><a name="line528" /> 528 end
1150
- </span><span class="inferred1"><a name="line529" /> 529
1151
- </span><span class="inferred0"><a name="line530" /> 530 # Returns the username and password segment of this URI.
1152
- </span><span class="marked1"><a name="line531" /> 531 def userinfo
1153
- </span><span class="marked0"><a name="line532" /> 532 if !defined?(@userinfo) || @userinfo.nil?
1154
- </span><span class="marked1"><a name="line533" /> 533 current_user = self.user
1155
- </span><span class="marked0"><a name="line534" /> 534 current_password = self.password
1156
- </span><span class="marked1"><a name="line535" /> 535 if current_user == nil &amp;&amp; current_password == nil
1157
- </span><span class="marked0"><a name="line536" /> 536 @userinfo = nil
1158
- </span><span class="marked1"><a name="line537" /> 537 elsif current_user != nil &amp;&amp; current_password == nil
1159
- </span><span class="marked0"><a name="line538" /> 538 @userinfo = &quot;#{current_user}&quot;
1160
- </span><span class="marked1"><a name="line539" /> 539 elsif current_user != nil &amp;&amp; current_password != nil
1161
- </span><span class="marked0"><a name="line540" /> 540 @userinfo = &quot;#{current_user}:#{current_password}&quot;
1162
- </span><span class="inferred1"><a name="line541" /> 541 end
1163
- </span><span class="inferred0"><a name="line542" /> 542 end
1164
- </span><span class="marked1"><a name="line543" /> 543 return @userinfo
1165
- </span><span class="inferred0"><a name="line544" /> 544 end
1166
- </span><span class="inferred1"><a name="line545" /> 545
1167
- </span><span class="inferred0"><a name="line546" /> 546 # Sets the username and password segment of this URI.
1168
- </span><span class="marked1"><a name="line547" /> 547 def userinfo=(new_userinfo)
1169
- </span><span class="marked0"><a name="line548" /> 548 new_user = new_userinfo.to_s.strip.scan(/^(.*):/).flatten[0]
1170
- </span><span class="marked1"><a name="line549" /> 549 new_password = new_userinfo.to_s.strip.scan(/:(.*)$/).flatten[0]
1171
- </span><span class="inferred0"><a name="line550" /> 550
1172
- </span><span class="inferred1"><a name="line551" /> 551 # Password assigned first to ensure validity in case of nil
1173
- </span><span class="marked0"><a name="line552" /> 552 self.password = new_password
1174
- </span><span class="marked1"><a name="line553" /> 553 self.user = new_user
1175
- </span><span class="inferred0"><a name="line554" /> 554
1176
- </span><span class="inferred1"><a name="line555" /> 555 # Reset dependant values
1177
- </span><span class="marked0"><a name="line556" /> 556 @authority = nil
1178
- </span><span class="inferred1"><a name="line557" /> 557
1179
- </span><span class="inferred0"><a name="line558" /> 558 # Ensure we haven't created an invalid URI
1180
- </span><span class="marked1"><a name="line559" /> 559 validate()
1181
- </span><span class="inferred0"><a name="line560" /> 560 end
1182
- </span><span class="inferred1"><a name="line561" /> 561
1183
- </span><span class="inferred0"><a name="line562" /> 562 # Returns the host for this URI.
1184
- </span><span class="marked1"><a name="line563" /> 563 def host
1185
- </span><span class="marked0"><a name="line564" /> 564 return @host
1186
- </span><span class="marked1"><a name="line565" /> 565 end
1187
- </span><span class="inferred0"><a name="line566" /> 566
1188
- </span><span class="inferred1"><a name="line567" /> 567 # Sets the host for this URI.
1189
- </span><span class="marked0"><a name="line568" /> 568 def host=(new_host)
1190
- </span><span class="marked1"><a name="line569" /> 569 @host = new_host
1191
- </span><span class="inferred0"><a name="line570" /> 570
1192
- </span><span class="inferred1"><a name="line571" /> 571 # Reset dependant values
1193
- </span><span class="marked0"><a name="line572" /> 572 @authority = nil
721
+ </span><span class="marked0"><a name="line100" /> 100 return Addressable::URI.new(
722
+ </span><span class="marked1"><a name="line101" /> 101 scheme, user, password, host, port, path, query, fragment)
723
+ </span><span class="inferred0"><a name="line102" /> 102 end
724
+ </span><span class="inferred1"><a name="line103" /> 103
725
+ </span><span class="inferred0"><a name="line104" /> 104 # Converts an input to a URI. The input does not have to be a valid
726
+ </span><span class="inferred1"><a name="line105" /> 105 # URI -- the method will use heuristics to guess what URI was intended.
727
+ </span><span class="inferred0"><a name="line106" /> 106 # This is not standards compliant, merely user-friendly.
728
+ </span><span class="marked1"><a name="line107" /> 107 def self.heuristic_parse(input, hints={})
729
+ </span><span class="marked0"><a name="line108" /> 108 input = input.dup
730
+ </span><span class="marked1"><a name="line109" /> 109 hints = {
731
+ </span><span class="inferred0"><a name="line110" /> 110 :scheme =&gt; &quot;http&quot;
732
+ </span><span class="inferred1"><a name="line111" /> 111 }.merge(hints)
733
+ </span><span class="marked0"><a name="line112" /> 112 case input
734
+ </span><span class="marked1"><a name="line113" /> 113 when /^http:\/+/
735
+ </span><span class="marked0"><a name="line114" /> 114 input.gsub!(/^http:\/+/, &quot;http://&quot;)
736
+ </span><span class="marked1"><a name="line115" /> 115 when /^feed:\/+http:\/+/
737
+ </span><span class="marked0"><a name="line116" /> 116 input.gsub!(/^feed:\/+http:\/+/, &quot;feed:http://&quot;)
738
+ </span><span class="marked1"><a name="line117" /> 117 when /^feed:\/+/
739
+ </span><span class="marked0"><a name="line118" /> 118 input.gsub!(/^feed:\/+/, &quot;feed://&quot;)
740
+ </span><span class="marked1"><a name="line119" /> 119 when /^file:\/+/
741
+ </span><span class="marked0"><a name="line120" /> 120 input.gsub!(/^file:\/+/, &quot;file:///&quot;)
742
+ </span><span class="inferred1"><a name="line121" /> 121 end
743
+ </span><span class="marked0"><a name="line122" /> 122 parsed = self.parse(input)
744
+ </span><span class="marked1"><a name="line123" /> 123 if parsed.scheme =~ /^[^\/?#\.]+\.[^\/?#]+$/
745
+ </span><span class="marked0"><a name="line124" /> 124 parsed = self.parse(hints[:scheme] + &quot;://&quot; + input)
746
+ </span><span class="inferred1"><a name="line125" /> 125 end
747
+ </span><span class="marked0"><a name="line126" /> 126 if parsed.authority == nil
748
+ </span><span class="marked1"><a name="line127" /> 127 if parsed.path =~ /^[^\/]+\./
749
+ </span><span class="marked0"><a name="line128" /> 128 new_host = parsed.path.scan(/^([^\/]+\.[^\/]*)/).flatten[0]
750
+ </span><span class="marked1"><a name="line129" /> 129 if new_host
751
+ </span><span class="marked0"><a name="line130" /> 130 new_path = parsed.path.gsub(
752
+ </span><span class="inferred1"><a name="line131" /> 131 Regexp.new(&quot;^&quot; + Regexp.escape(new_host)), &quot;&quot;)
753
+ </span><span class="marked0"><a name="line132" /> 132 parsed.host = new_host
754
+ </span><span class="marked1"><a name="line133" /> 133 parsed.path = new_path
755
+ </span><span class="marked0"><a name="line134" /> 134 parsed.scheme = hints[:scheme]
756
+ </span><span class="inferred1"><a name="line135" /> 135 end
757
+ </span><span class="inferred0"><a name="line136" /> 136 end
758
+ </span><span class="inferred1"><a name="line137" /> 137 end
759
+ </span><span class="marked0"><a name="line138" /> 138 return parsed
760
+ </span><span class="inferred1"><a name="line139" /> 139 end
761
+ </span><span class="inferred0"><a name="line140" /> 140
762
+ </span><span class="inferred1"><a name="line141" /> 141 # Converts a path to a file protocol URI. If the path supplied is
763
+ </span><span class="inferred0"><a name="line142" /> 142 # relative, it will be returned as a relative URI. If the path supplied
764
+ </span><span class="inferred1"><a name="line143" /> 143 # is actually a URI, it will return the parsed URI.
765
+ </span><span class="marked0"><a name="line144" /> 144 def self.convert_path(path)
766
+ </span><span class="marked1"><a name="line145" /> 145 return nil if path.nil?
767
+ </span><span class="inferred0"><a name="line146" /> 146
768
+ </span><span class="marked1"><a name="line147" /> 147 converted_uri = path.strip
769
+ </span><span class="marked0"><a name="line148" /> 148 if converted_uri.length &gt; 0 &amp;&amp; converted_uri[0..0] == &quot;/&quot;
770
+ </span><span class="marked1"><a name="line149" /> 149 converted_uri = &quot;file://&quot; + converted_uri
771
+ </span><span class="inferred0"><a name="line150" /> 150 end
772
+ </span><span class="inferred1"><a name="line151" /> 151 if converted_uri.length &gt; 0 &amp;&amp;
773
+ </span><span class="marked0"><a name="line152" /> 152 converted_uri.scan(/^[a-zA-Z]:[\\\/]/).size &gt; 0
774
+ </span><span class="marked1"><a name="line153" /> 153 converted_uri = &quot;file:///&quot; + converted_uri
775
+ </span><span class="inferred0"><a name="line154" /> 154 end
776
+ </span><span class="marked1"><a name="line155" /> 155 converted_uri.gsub!(/^file:\/*/i, &quot;file:///&quot;)
777
+ </span><span class="marked0"><a name="line156" /> 156 if converted_uri =~ /^file:/i
778
+ </span><span class="inferred1"><a name="line157" /> 157 # Adjust windows-style uris
779
+ </span><span class="marked0"><a name="line158" /> 158 converted_uri.gsub!(/^file:\/\/\/([a-zA-Z])\|/i, 'file:///\1:')
780
+ </span><span class="marked1"><a name="line159" /> 159 converted_uri.gsub!(/\\/, '/')
781
+ </span><span class="marked0"><a name="line160" /> 160 converted_uri = self.parse(converted_uri).normalize
782
+ </span><span class="marked1"><a name="line161" /> 161 if File.exists?(converted_uri.path) &amp;&amp;
783
+ </span><span class="marked0"><a name="line162" /> 162 File.stat(converted_uri.path).directory?
784
+ </span><span class="marked1"><a name="line163" /> 163 converted_uri.path.gsub!(/\/$/, &quot;&quot;)
785
+ </span><span class="marked0"><a name="line164" /> 164 converted_uri.path = converted_uri.path + '/'
786
+ </span><span class="inferred1"><a name="line165" /> 165 end
787
+ </span><span class="inferred0"><a name="line166" /> 166 else
788
+ </span><span class="marked1"><a name="line167" /> 167 converted_uri = self.parse(converted_uri)
789
+ </span><span class="inferred0"><a name="line168" /> 168 end
790
+ </span><span class="inferred1"><a name="line169" /> 169
791
+ </span><span class="marked0"><a name="line170" /> 170 return converted_uri
792
+ </span><span class="inferred1"><a name="line171" /> 171 end
793
+ </span><span class="inferred0"><a name="line172" /> 172
794
+ </span><span class="inferred1"><a name="line173" /> 173 # Expands a URI template into a full URI.
795
+ </span><span class="inferred0"><a name="line174" /> 174 #
796
+ </span><span class="inferred1"><a name="line175" /> 175 # An optional processor object may be supplied. The object should
797
+ </span><span class="inferred0"><a name="line176" /> 176 # respond to either the :validate or :transform messages or both.
798
+ </span><span class="inferred1"><a name="line177" /> 177 # Both the :validate and :transform methods should take two parameters:
799
+ </span><span class="inferred0"><a name="line178" /> 178 # :name and :value. The :validate method should return true or false;
800
+ </span><span class="inferred1"><a name="line179" /> 179 # true if the value of the variable is valid, false otherwise. The
801
+ </span><span class="inferred0"><a name="line180" /> 180 # :transform method should return the transformed variable value as a
802
+ </span><span class="inferred1"><a name="line181" /> 181 # string.
803
+ </span><span class="inferred0"><a name="line182" /> 182 #
804
+ </span><span class="inferred1"><a name="line183" /> 183 # An example:
805
+ </span><span class="inferred0"><a name="line184" /> 184 #
806
+ </span><span class="inferred1"><a name="line185" /> 185 # class ExampleProcessor
807
+ </span><span class="inferred0"><a name="line186" /> 186 # def self.validate(name, value)
808
+ </span><span class="inferred1"><a name="line187" /> 187 # return !!(value =~ /^[\w ]+$/) if name == &quot;query&quot;
809
+ </span><span class="inferred0"><a name="line188" /> 188 # return true
810
+ </span><span class="inferred1"><a name="line189" /> 189 # end
811
+ </span><span class="inferred0"><a name="line190" /> 190 #
812
+ </span><span class="inferred1"><a name="line191" /> 191 # def self.transform(name, value)
813
+ </span><span class="inferred0"><a name="line192" /> 192 # return value.gsub(/ /, &quot;+&quot;) if name == &quot;query&quot;
814
+ </span><span class="inferred1"><a name="line193" /> 193 # return value
815
+ </span><span class="inferred0"><a name="line194" /> 194 # end
816
+ </span><span class="inferred1"><a name="line195" /> 195 # end
817
+ </span><span class="inferred0"><a name="line196" /> 196 #
818
+ </span><span class="inferred1"><a name="line197" /> 197 # Addressable::URI.expand_template(
819
+ </span><span class="inferred0"><a name="line198" /> 198 # &quot;http://example.com/search/{query}/&quot;,
820
+ </span><span class="inferred1"><a name="line199" /> 199 # {&quot;query&quot; =&gt; &quot;an example search query&quot;},
821
+ </span><span class="inferred0"><a name="line200" /> 200 # ExampleProcessor).to_s
822
+ </span><span class="inferred1"><a name="line201" /> 201 # =&gt; &quot;http://example.com/search/an+example+search+query/&quot;
823
+ </span><span class="marked0"><a name="line202" /> 202 def self.expand_template(pattern, mapping, processor=nil)
824
+ </span><span class="marked1"><a name="line203" /> 203 result = pattern.dup
825
+ </span><span class="marked0"><a name="line204" /> 204 for name, value in mapping
826
+ </span><span class="marked1"><a name="line205" /> 205 transformed_value = value
827
+ </span><span class="marked0"><a name="line206" /> 206 if processor != nil
828
+ </span><span class="marked1"><a name="line207" /> 207 if processor.respond_to?(:validate)
829
+ </span><span class="marked0"><a name="line208" /> 208 if !processor.validate(name, value)
830
+ </span><span class="marked1"><a name="line209" /> 209 raise InvalidTemplateValue,
831
+ </span><span class="inferred0"><a name="line210" /> 210 &quot;(#{name}, #{value}) is an invalid template value.&quot;
832
+ </span><span class="inferred1"><a name="line211" /> 211 end
833
+ </span><span class="inferred0"><a name="line212" /> 212 end
834
+ </span><span class="marked1"><a name="line213" /> 213 if processor.respond_to?(:transform)
835
+ </span><span class="marked0"><a name="line214" /> 214 transformed_value = processor.transform(name, value)
836
+ </span><span class="inferred1"><a name="line215" /> 215 end
837
+ </span><span class="inferred0"><a name="line216" /> 216 end
838
+ </span><span class="inferred1"><a name="line217" /> 217
839
+ </span><span class="inferred0"><a name="line218" /> 218 # Handle percent escaping
840
+ </span><span class="marked1"><a name="line219" /> 219 transformed_value = self.encode_segment(transformed_value,
841
+ </span><span class="inferred0"><a name="line220" /> 220 Addressable::URI::CharacterClasses::RESERVED +
842
+ </span><span class="inferred1"><a name="line221" /> 221 Addressable::URI::CharacterClasses::UNRESERVED)
843
+ </span><span class="inferred0"><a name="line222" /> 222
844
+ </span><span class="marked1"><a name="line223" /> 223 result.gsub!(/\{#{Regexp.escape(name)}\}/, transformed_value)
845
+ </span><span class="inferred0"><a name="line224" /> 224 end
846
+ </span><span class="marked1"><a name="line225" /> 225 result.gsub!(
847
+ </span><span class="inferred0"><a name="line226" /> 226 /\{[#{Addressable::URI::CharacterClasses::UNRESERVED}]+\}/, &quot;&quot;)
848
+ </span><span class="marked1"><a name="line227" /> 227 return Addressable::URI.parse(result)
849
+ </span><span class="inferred0"><a name="line228" /> 228 end
850
+ </span><span class="inferred1"><a name="line229" /> 229
851
+ </span><span class="inferred0"><a name="line230" /> 230 # Extracts a mapping from the URI using a URI Template pattern.
852
+ </span><span class="inferred1"><a name="line231" /> 231 # Returns nil if the pattern doesn't match the URI.
853
+ </span><span class="inferred0"><a name="line232" /> 232 #
854
+ </span><span class="inferred1"><a name="line233" /> 233 # An optional processor object may be supplied. The object should
855
+ </span><span class="inferred0"><a name="line234" /> 234 # respond to either the :restore or :match messages or both.
856
+ </span><span class="inferred1"><a name="line235" /> 235 # The :restore method should take two parameters: :name and :value.
857
+ </span><span class="inferred0"><a name="line236" /> 236 # The :restore method should reverse any transformations that have been
858
+ </span><span class="inferred1"><a name="line237" /> 237 # performed on the value to ensure a valid URI. The :match method
859
+ </span><span class="inferred0"><a name="line238" /> 238 # should take a single parameter: :name. The :match method should
860
+ </span><span class="inferred1"><a name="line239" /> 239 # return a String containing a regular expression capture group for
861
+ </span><span class="inferred0"><a name="line240" /> 240 # matching on that particular variable. The default value is &quot;.*&quot;.
862
+ </span><span class="inferred1"><a name="line241" /> 241 #
863
+ </span><span class="inferred0"><a name="line242" /> 242 # An example:
864
+ </span><span class="inferred1"><a name="line243" /> 243 #
865
+ </span><span class="inferred0"><a name="line244" /> 244 # class ExampleProcessor
866
+ </span><span class="inferred1"><a name="line245" /> 245 # def self.restore(name, value)
867
+ </span><span class="inferred0"><a name="line246" /> 246 # return value.gsub(/\+/, &quot; &quot;) if name == &quot;query&quot;
868
+ </span><span class="inferred1"><a name="line247" /> 247 # return value
869
+ </span><span class="inferred0"><a name="line248" /> 248 # end
870
+ </span><span class="inferred1"><a name="line249" /> 249 #
871
+ </span><span class="inferred0"><a name="line250" /> 250 # def self.match(name)
872
+ </span><span class="inferred1"><a name="line251" /> 251 # return &quot;.*?&quot; if name == &quot;first&quot;
873
+ </span><span class="inferred0"><a name="line252" /> 252 # return &quot;.*&quot;
874
+ </span><span class="inferred1"><a name="line253" /> 253 # end
875
+ </span><span class="inferred0"><a name="line254" /> 254 # end
876
+ </span><span class="inferred1"><a name="line255" /> 255 #
877
+ </span><span class="inferred0"><a name="line256" /> 256 # uri = Addressable::URI.parse(
878
+ </span><span class="inferred1"><a name="line257" /> 257 # &quot;http://example.com/search/an+example+search+query/&quot;)
879
+ </span><span class="inferred0"><a name="line258" /> 258 # uri.extract_mapping(&quot;http://example.com/search/{query}/&quot;,
880
+ </span><span class="inferred1"><a name="line259" /> 259 # ExampleProcessor)
881
+ </span><span class="inferred0"><a name="line260" /> 260 # =&gt; {&quot;query&quot; =&gt; &quot;an example search query&quot;}
882
+ </span><span class="inferred1"><a name="line261" /> 261 #
883
+ </span><span class="inferred0"><a name="line262" /> 262 # uri = Addressable::URI.parse(
884
+ </span><span class="inferred1"><a name="line263" /> 263 # &quot;http://example.com/a/b/c/&quot;)
885
+ </span><span class="inferred0"><a name="line264" /> 264 # uri.extract_mapping(&quot;http://example.com/{first}/{second}/&quot;,
886
+ </span><span class="inferred1"><a name="line265" /> 265 # ExampleProcessor)
887
+ </span><span class="inferred0"><a name="line266" /> 266 # =&gt; {&quot;first&quot; =&gt; &quot;a&quot;, &quot;second&quot; =&gt; &quot;b/c&quot;}
888
+ </span><span class="marked1"><a name="line267" /> 267 def extract_mapping(pattern, processor=nil)
889
+ </span><span class="marked0"><a name="line268" /> 268 mapping = {}
890
+ </span><span class="marked1"><a name="line269" /> 269 variable_regexp =
891
+ </span><span class="inferred0"><a name="line270" /> 270 /\{([#{Addressable::URI::CharacterClasses::UNRESERVED}]+)\}/
892
+ </span><span class="marked1"><a name="line271" /> 271 variables = pattern.scan(variable_regexp).flatten
893
+ </span><span class="marked0"><a name="line272" /> 272 variables.each { |v| mapping[v] = &quot;&quot; }
894
+ </span><span class="marked1"><a name="line273" /> 273 escaped_pattern =
895
+ </span><span class="inferred0"><a name="line274" /> 274 Regexp.escape(pattern).gsub(/\\\{/, &quot;{&quot;).gsub(/\\\}/, &quot;}&quot;)
896
+ </span><span class="marked1"><a name="line275" /> 275 regexp = Regexp.new(escaped_pattern.gsub(variable_regexp) do |v|
897
+ </span><span class="marked0"><a name="line276" /> 276 capture_group = &quot;(.*)&quot;
898
+ </span><span class="inferred1"><a name="line277" /> 277
899
+ </span><span class="marked0"><a name="line278" /> 278 if processor != nil
900
+ </span><span class="marked1"><a name="line279" /> 279 if processor.respond_to?(:match)
901
+ </span><span class="marked0"><a name="line280" /> 280 name = v.scan(variable_regexp).flatten[0]
902
+ </span><span class="marked1"><a name="line281" /> 281 capture_group = &quot;(#{processor.match(name)})&quot;
903
+ </span><span class="inferred0"><a name="line282" /> 282 end
904
+ </span><span class="inferred1"><a name="line283" /> 283 end
905
+ </span><span class="inferred0"><a name="line284" /> 284
906
+ </span><span class="marked1"><a name="line285" /> 285 capture_group
907
+ </span><span class="inferred0"><a name="line286" /> 286 end)
908
+ </span><span class="marked1"><a name="line287" /> 287 values = self.to_s.scan(regexp).flatten
909
+ </span><span class="marked0"><a name="line288" /> 288 if variables.size == values.size
910
+ </span><span class="marked1"><a name="line289" /> 289 for i in 0...variables.size
911
+ </span><span class="marked0"><a name="line290" /> 290 name = variables[i]
912
+ </span><span class="marked1"><a name="line291" /> 291 value = values[i]
913
+ </span><span class="inferred0"><a name="line292" /> 292
914
+ </span><span class="marked1"><a name="line293" /> 293 if processor != nil
915
+ </span><span class="marked0"><a name="line294" /> 294 if processor.respond_to?(:restore)
916
+ </span><span class="marked1"><a name="line295" /> 295 value = processor.restore(name, value)
917
+ </span><span class="inferred0"><a name="line296" /> 296 end
918
+ </span><span class="inferred1"><a name="line297" /> 297 end
919
+ </span><span class="inferred0"><a name="line298" /> 298
920
+ </span><span class="marked1"><a name="line299" /> 299 mapping[name] = value
921
+ </span><span class="inferred0"><a name="line300" /> 300 end
922
+ </span><span class="marked1"><a name="line301" /> 301 return mapping
923
+ </span><span class="inferred0"><a name="line302" /> 302 else
924
+ </span><span class="inferred1"><a name="line303" /> 303 # Pattern failed to match URI.
925
+ </span><span class="marked0"><a name="line304" /> 304 return nil
926
+ </span><span class="inferred1"><a name="line305" /> 305 end
927
+ </span><span class="inferred0"><a name="line306" /> 306 end
928
+ </span><span class="inferred1"><a name="line307" /> 307
929
+ </span><span class="inferred0"><a name="line308" /> 308 # Joins several uris together.
930
+ </span><span class="marked1"><a name="line309" /> 309 def self.join(*uris)
931
+ </span><span class="marked0"><a name="line310" /> 310 uri_objects = uris.collect do |uri|
932
+ </span><span class="marked1"><a name="line311" /> 311 uri.kind_of?(self) ? uri : self.parse(uri.to_s)
933
+ </span><span class="inferred0"><a name="line312" /> 312 end
934
+ </span><span class="marked1"><a name="line313" /> 313 result = uri_objects.shift.dup
935
+ </span><span class="marked0"><a name="line314" /> 314 for uri in uri_objects
936
+ </span><span class="marked1"><a name="line315" /> 315 result.merge!(uri)
937
+ </span><span class="inferred0"><a name="line316" /> 316 end
938
+ </span><span class="marked1"><a name="line317" /> 317 return result
939
+ </span><span class="inferred0"><a name="line318" /> 318 end
940
+ </span><span class="inferred1"><a name="line319" /> 319
941
+ </span><span class="inferred0"><a name="line320" /> 320 # Percent encodes a URI segment. Returns a string. Takes an optional
942
+ </span><span class="inferred1"><a name="line321" /> 321 # character class parameter, which should be specified as a string
943
+ </span><span class="inferred0"><a name="line322" /> 322 # containing a regular expression character class (not including the
944
+ </span><span class="inferred1"><a name="line323" /> 323 # surrounding square brackets). The character class parameter defaults
945
+ </span><span class="inferred0"><a name="line324" /> 324 # to the reserved plus unreserved character classes specified in
946
+ </span><span class="inferred1"><a name="line325" /> 325 # RFC 3986. Usage of the constants within the CharacterClasses module is
947
+ </span><span class="inferred0"><a name="line326" /> 326 # highly recommended when using this method.
948
+ </span><span class="inferred1"><a name="line327" /> 327 #
949
+ </span><span class="inferred0"><a name="line328" /> 328 # An example:
950
+ </span><span class="inferred1"><a name="line329" /> 329 #
951
+ </span><span class="inferred0"><a name="line330" /> 330 # Addressable::URI.escape_segment(&quot;simple-example&quot;, &quot;b-zB-Z0-9&quot;)
952
+ </span><span class="inferred1"><a name="line331" /> 331 # =&gt; &quot;simple%2Dex%61mple&quot;
953
+ </span><span class="marked0"><a name="line332" /> 332 def self.encode_segment(segment, character_class=
954
+ </span><span class="inferred1"><a name="line333" /> 333 Addressable::URI::CharacterClasses::RESERVED +
955
+ </span><span class="marked0"><a name="line334" /> 334 Addressable::URI::CharacterClasses::UNRESERVED)
956
+ </span><span class="marked1"><a name="line335" /> 335 return nil if segment.nil?
957
+ </span><span class="inferred0"><a name="line336" /> 336 return segment.gsub(
958
+ </span><span class="inferred1"><a name="line337" /> 337 /[^#{character_class}]/
959
+ </span><span class="inferred0"><a name="line338" /> 338 ) do |sequence|
960
+ </span><span class="marked1"><a name="line339" /> 339 (&quot;%&quot; + sequence.unpack('C')[0].to_s(16).upcase)
961
+ </span><span class="marked0"><a name="line340" /> 340 end
962
+ </span><span class="inferred1"><a name="line341" /> 341 end
963
+ </span><span class="inferred0"><a name="line342" /> 342
964
+ </span><span class="inferred1"><a name="line343" /> 343 # Unencodes any percent encoded characters within a URI segment.
965
+ </span><span class="inferred0"><a name="line344" /> 344 # Returns a string.
966
+ </span><span class="marked1"><a name="line345" /> 345 def self.unencode_segment(segment)
967
+ </span><span class="marked0"><a name="line346" /> 346 return nil if segment.nil?
968
+ </span><span class="inferred1"><a name="line347" /> 347 return segment.to_s.gsub(/%[0-9a-f]{2}/i) do |sequence|
969
+ </span><span class="marked0"><a name="line348" /> 348 sequence[1..3].to_i(16).chr
970
+ </span><span class="marked1"><a name="line349" /> 349 end
971
+ </span><span class="inferred0"><a name="line350" /> 350 end
972
+ </span><span class="inferred1"><a name="line351" /> 351
973
+ </span><span class="inferred0"><a name="line352" /> 352 # Percent encodes any special characters in the URI. This method does
974
+ </span><span class="inferred1"><a name="line353" /> 353 # not take IRIs or IDNs into account.
975
+ </span><span class="marked0"><a name="line354" /> 354 def self.encode(uri)
976
+ </span><span class="marked1"><a name="line355" /> 355 uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_s)
977
+ </span><span class="marked0"><a name="line356" /> 356 return Addressable::URI.new(
978
+ </span><span class="marked1"><a name="line357" /> 357 self.encode_segment(uri_object.scheme,
979
+ </span><span class="inferred0"><a name="line358" /> 358 Addressable::URI::CharacterClasses::SCHEME),
980
+ </span><span class="marked1"><a name="line359" /> 359 self.encode_segment(uri_object.user,
981
+ </span><span class="inferred0"><a name="line360" /> 360 Addressable::URI::CharacterClasses::AUTHORITY),
982
+ </span><span class="marked1"><a name="line361" /> 361 self.encode_segment(uri_object.password,
983
+ </span><span class="inferred0"><a name="line362" /> 362 Addressable::URI::CharacterClasses::AUTHORITY),
984
+ </span><span class="marked1"><a name="line363" /> 363 self.encode_segment(uri_object.host,
985
+ </span><span class="inferred0"><a name="line364" /> 364 Addressable::URI::CharacterClasses::AUTHORITY),
986
+ </span><span class="marked1"><a name="line365" /> 365 self.encode_segment(uri_object.specified_port,
987
+ </span><span class="inferred0"><a name="line366" /> 366 Addressable::URI::CharacterClasses::AUTHORITY),
988
+ </span><span class="marked1"><a name="line367" /> 367 self.encode_segment(uri_object.path,
989
+ </span><span class="inferred0"><a name="line368" /> 368 Addressable::URI::CharacterClasses::PATH),
990
+ </span><span class="marked1"><a name="line369" /> 369 self.encode_segment(uri_object.query,
991
+ </span><span class="inferred0"><a name="line370" /> 370 Addressable::URI::CharacterClasses::QUERY),
992
+ </span><span class="marked1"><a name="line371" /> 371 self.encode_segment(uri_object.fragment,
993
+ </span><span class="inferred0"><a name="line372" /> 372 Addressable::URI::CharacterClasses::FRAGMENT)
994
+ </span><span class="marked1"><a name="line373" /> 373 ).to_s
995
+ </span><span class="inferred0"><a name="line374" /> 374 end
996
+ </span><span class="inferred1"><a name="line375" /> 375
997
+ </span><span class="marked0"><a name="line376" /> 376 class &lt;&lt; self
998
+ </span><span class="marked1"><a name="line377" /> 377 alias_method :escape, :encode
999
+ </span><span class="inferred0"><a name="line378" /> 378 end
1000
+ </span><span class="inferred1"><a name="line379" /> 379
1001
+ </span><span class="inferred0"><a name="line380" /> 380 # Normalizes the encoding of a URI. Characters within a hostname are
1002
+ </span><span class="inferred1"><a name="line381" /> 381 # not percent encoded to allow for internationalized domain names.
1003
+ </span><span class="marked0"><a name="line382" /> 382 def self.normalized_encode(uri)
1004
+ </span><span class="marked1"><a name="line383" /> 383 uri_object = uri.kind_of?(self) ? uri : self.parse(uri.to_s)
1005
+ </span><span class="marked0"><a name="line384" /> 384 segments = {
1006
+ </span><span class="marked1"><a name="line385" /> 385 :scheme =&gt; self.unencode_segment(uri_object.scheme),
1007
+ </span><span class="marked0"><a name="line386" /> 386 :user =&gt; self.unencode_segment(uri_object.user),
1008
+ </span><span class="marked1"><a name="line387" /> 387 :password =&gt; self.unencode_segment(uri_object.password),
1009
+ </span><span class="marked0"><a name="line388" /> 388 :host =&gt; self.unencode_segment(uri_object.host),
1010
+ </span><span class="marked1"><a name="line389" /> 389 :port =&gt; self.unencode_segment(uri_object.specified_port),
1011
+ </span><span class="marked0"><a name="line390" /> 390 :path =&gt; self.unencode_segment(uri_object.path),
1012
+ </span><span class="marked1"><a name="line391" /> 391 :query =&gt; self.unencode_segment(uri_object.query),
1013
+ </span><span class="marked0"><a name="line392" /> 392 :fragment =&gt; self.unencode_segment(uri_object.fragment)
1014
+ </span><span class="inferred1"><a name="line393" /> 393 }
1015
+ </span><span class="marked0"><a name="line394" /> 394 if URI::IDNA.send(:use_libidn?)
1016
+ </span><span class="marked1"><a name="line395" /> 395 segments.each do |key, value|
1017
+ </span><span class="marked0"><a name="line396" /> 396 if value != nil
1018
+ </span><span class="marked1"><a name="line397" /> 397 segments[key] = IDN::Stringprep.nfkc_normalize(value.to_s)
1019
+ </span><span class="inferred0"><a name="line398" /> 398 end
1020
+ </span><span class="inferred1"><a name="line399" /> 399 end
1021
+ </span><span class="inferred0"><a name="line400" /> 400 end
1022
+ </span><span class="marked1"><a name="line401" /> 401 return Addressable::URI.new(
1023
+ </span><span class="marked0"><a name="line402" /> 402 self.encode_segment(segments[:scheme],
1024
+ </span><span class="inferred1"><a name="line403" /> 403 Addressable::URI::CharacterClasses::SCHEME),
1025
+ </span><span class="marked0"><a name="line404" /> 404 self.encode_segment(segments[:user],
1026
+ </span><span class="inferred1"><a name="line405" /> 405 Addressable::URI::CharacterClasses::AUTHORITY),
1027
+ </span><span class="marked0"><a name="line406" /> 406 self.encode_segment(segments[:password],
1028
+ </span><span class="inferred1"><a name="line407" /> 407 Addressable::URI::CharacterClasses::AUTHORITY),
1029
+ </span><span class="inferred0"><a name="line408" /> 408 segments[:host],
1030
+ </span><span class="inferred1"><a name="line409" /> 409 segments[:port],
1031
+ </span><span class="marked0"><a name="line410" /> 410 self.encode_segment(segments[:path],
1032
+ </span><span class="inferred1"><a name="line411" /> 411 Addressable::URI::CharacterClasses::PATH),
1033
+ </span><span class="marked0"><a name="line412" /> 412 self.encode_segment(segments[:query],
1034
+ </span><span class="inferred1"><a name="line413" /> 413 Addressable::URI::CharacterClasses::QUERY),
1035
+ </span><span class="marked0"><a name="line414" /> 414 self.encode_segment(segments[:fragment],
1036
+ </span><span class="inferred1"><a name="line415" /> 415 Addressable::URI::CharacterClasses::FRAGMENT)
1037
+ </span><span class="marked0"><a name="line416" /> 416 ).to_s
1038
+ </span><span class="inferred1"><a name="line417" /> 417 end
1039
+ </span><span class="inferred0"><a name="line418" /> 418
1040
+ </span><span class="inferred1"><a name="line419" /> 419 # Extracts uris from an arbitrary body of text.
1041
+ </span><span class="marked0"><a name="line420" /> 420 def self.extract(text, options={})
1042
+ </span><span class="marked1"><a name="line421" /> 421 defaults = {:base =&gt; nil, :parse =&gt; false}
1043
+ </span><span class="marked0"><a name="line422" /> 422 options = defaults.merge(options)
1044
+ </span><span class="marked1"><a name="line423" /> 423 raise InvalidOptionError unless (options.keys - defaults.keys).empty?
1045
+ </span><span class="inferred0"><a name="line424" /> 424 # This regular expression needs to be less forgiving or else it would
1046
+ </span><span class="inferred1"><a name="line425" /> 425 # match virtually all text. Which isn't exactly what we're going for.
1047
+ </span><span class="marked0"><a name="line426" /> 426 extract_regex = /((([a-z\+]+):)[^ \n\&lt;\&gt;\&quot;\\]+[\w\/])/
1048
+ </span><span class="marked1"><a name="line427" /> 427 extracted_uris =
1049
+ </span><span class="marked0"><a name="line428" /> 428 text.scan(extract_regex).collect { |match| match[0] }
1050
+ </span><span class="marked1"><a name="line429" /> 429 sgml_extract_regex = /&lt;[^&gt;]+href=\&quot;([^\&quot;]+?)\&quot;[^&gt;]*&gt;/
1051
+ </span><span class="marked0"><a name="line430" /> 430 sgml_extracted_uris =
1052
+ </span><span class="inferred1"><a name="line431" /> 431 text.scan(sgml_extract_regex).collect { |match| match[0] }
1053
+ </span><span class="marked0"><a name="line432" /> 432 extracted_uris.concat(sgml_extracted_uris - extracted_uris)
1054
+ </span><span class="marked1"><a name="line433" /> 433 textile_extract_regex = /\&quot;.+?\&quot;:([^ ]+\/[^ ]+)[ \,\.\;\:\?\!\&lt;\&gt;\&quot;]/i
1055
+ </span><span class="marked0"><a name="line434" /> 434 textile_extracted_uris =
1056
+ </span><span class="inferred1"><a name="line435" /> 435 text.scan(textile_extract_regex).collect { |match| match[0] }
1057
+ </span><span class="marked0"><a name="line436" /> 436 extracted_uris.concat(textile_extracted_uris - extracted_uris)
1058
+ </span><span class="marked1"><a name="line437" /> 437 parsed_uris = []
1059
+ </span><span class="marked0"><a name="line438" /> 438 base_uri = nil
1060
+ </span><span class="marked1"><a name="line439" /> 439 if options[:base] != nil
1061
+ </span><span class="marked0"><a name="line440" /> 440 base_uri = options[:base] if options[:base].kind_of?(self)
1062
+ </span><span class="marked1"><a name="line441" /> 441 base_uri = self.parse(options[:base].to_s) if base_uri == nil
1063
+ </span><span class="inferred0"><a name="line442" /> 442 end
1064
+ </span><span class="marked1"><a name="line443" /> 443 for uri_string in extracted_uris
1065
+ </span><span class="marked0"><a name="line444" /> 444 begin
1066
+ </span><span class="marked1"><a name="line445" /> 445 if base_uri == nil
1067
+ </span><span class="marked0"><a name="line446" /> 446 parsed_uris &lt;&lt; self.parse(uri_string)
1068
+ </span><span class="inferred1"><a name="line447" /> 447 else
1069
+ </span><span class="marked0"><a name="line448" /> 448 parsed_uris &lt;&lt; (base_uri + self.parse(uri_string))
1070
+ </span><span class="inferred1"><a name="line449" /> 449 end
1071
+ </span><span class="inferred0"><a name="line450" /> 450 rescue Exception
1072
+ </span><span class="marked1"><a name="line451" /> 451 nil
1073
+ </span><span class="inferred0"><a name="line452" /> 452 end
1074
+ </span><span class="inferred1"><a name="line453" /> 453 end
1075
+ </span><span class="marked0"><a name="line454" /> 454 parsed_uris.reject! do |uri|
1076
+ </span><span class="marked1"><a name="line455" /> 455 (uri.scheme =~ /T\d+/ ||
1077
+ </span><span class="marked0"><a name="line456" /> 456 uri.scheme == &quot;xmlns&quot; ||
1078
+ </span><span class="marked1"><a name="line457" /> 457 uri.scheme == &quot;xml&quot; ||
1079
+ </span><span class="marked0"><a name="line458" /> 458 uri.scheme == &quot;thr&quot; ||
1080
+ </span><span class="marked1"><a name="line459" /> 459 uri.scheme == &quot;this&quot; ||
1081
+ </span><span class="marked0"><a name="line460" /> 460 uri.scheme == &quot;float&quot; ||
1082
+ </span><span class="marked1"><a name="line461" /> 461 uri.scheme == &quot;user&quot; ||
1083
+ </span><span class="marked0"><a name="line462" /> 462 uri.scheme == &quot;username&quot; ||
1084
+ </span><span class="marked1"><a name="line463" /> 463 uri.scheme == &quot;out&quot;)
1085
+ </span><span class="inferred0"><a name="line464" /> 464 end
1086
+ </span><span class="marked1"><a name="line465" /> 465 if options[:parse]
1087
+ </span><span class="marked0"><a name="line466" /> 466 return parsed_uris
1088
+ </span><span class="inferred1"><a name="line467" /> 467 else
1089
+ </span><span class="marked0"><a name="line468" /> 468 return parsed_uris.collect { |uri| uri.to_s }
1090
+ </span><span class="inferred1"><a name="line469" /> 469 end
1091
+ </span><span class="inferred0"><a name="line470" /> 470 end
1092
+ </span><span class="inferred1"><a name="line471" /> 471
1093
+ </span><span class="inferred0"><a name="line472" /> 472 # Creates a new uri object from component parts. Passing nil for
1094
+ </span><span class="inferred1"><a name="line473" /> 473 # any of these parameters is acceptable.
1095
+ </span><span class="marked0"><a name="line474" /> 474 def initialize(scheme, user, password, host, port, path, query, fragment)
1096
+ </span><span class="marked1"><a name="line475" /> 475 @scheme = scheme
1097
+ </span><span class="marked0"><a name="line476" /> 476 @scheme = nil if @scheme.to_s.strip == &quot;&quot;
1098
+ </span><span class="marked1"><a name="line477" /> 477 @user = user
1099
+ </span><span class="marked0"><a name="line478" /> 478 @password = password
1100
+ </span><span class="marked1"><a name="line479" /> 479 @host = host
1101
+ </span><span class="marked0"><a name="line480" /> 480 @specified_port = port.to_s
1102
+ </span><span class="marked1"><a name="line481" /> 481 @port = port.kind_of?(Fixnum) ? port.to_s : port
1103
+ </span><span class="marked0"><a name="line482" /> 482 if @port != nil &amp;&amp; !(@port =~ /^\d+$/)
1104
+ </span><span class="marked1"><a name="line483" /> 483 raise InvalidURIError,
1105
+ </span><span class="inferred0"><a name="line484" /> 484 &quot;Invalid port number: #{@port.inspect}&quot;
1106
+ </span><span class="inferred1"><a name="line485" /> 485 end
1107
+ </span><span class="marked0"><a name="line486" /> 486 @port = @port.to_i
1108
+ </span><span class="marked1"><a name="line487" /> 487 @port = nil if @port == 0
1109
+ </span><span class="marked0"><a name="line488" /> 488 @path = (path || &quot;&quot;)
1110
+ </span><span class="marked1"><a name="line489" /> 489 @query = query
1111
+ </span><span class="marked0"><a name="line490" /> 490 @fragment = fragment
1112
+ </span><span class="inferred1"><a name="line491" /> 491
1113
+ </span><span class="marked0"><a name="line492" /> 492 validate()
1114
+ </span><span class="inferred1"><a name="line493" /> 493 end
1115
+ </span><span class="inferred0"><a name="line494" /> 494
1116
+ </span><span class="inferred1"><a name="line495" /> 495 # Returns the scheme (protocol) for this URI.
1117
+ </span><span class="marked0"><a name="line496" /> 496 def scheme
1118
+ </span><span class="marked1"><a name="line497" /> 497 return @scheme
1119
+ </span><span class="marked0"><a name="line498" /> 498 end
1120
+ </span><span class="inferred1"><a name="line499" /> 499
1121
+ </span><span class="inferred0"><a name="line500" /> 500 # Sets the scheme (protocol for this URI.)
1122
+ </span><span class="marked1"><a name="line501" /> 501 def scheme=(new_scheme)
1123
+ </span><span class="marked0"><a name="line502" /> 502 @scheme = new_scheme
1124
+ </span><span class="marked1"><a name="line503" /> 503 end
1125
+ </span><span class="inferred0"><a name="line504" /> 504
1126
+ </span><span class="inferred1"><a name="line505" /> 505 # Returns the user for this URI.
1127
+ </span><span class="marked0"><a name="line506" /> 506 def user
1128
+ </span><span class="marked1"><a name="line507" /> 507 return @user
1129
+ </span><span class="marked0"><a name="line508" /> 508 end
1130
+ </span><span class="inferred1"><a name="line509" /> 509
1131
+ </span><span class="inferred0"><a name="line510" /> 510 # Sets the user for this URI.
1132
+ </span><span class="marked1"><a name="line511" /> 511 def user=(new_user)
1133
+ </span><span class="marked0"><a name="line512" /> 512 @user = new_user
1134
+ </span><span class="inferred1"><a name="line513" /> 513
1135
+ </span><span class="inferred0"><a name="line514" /> 514 # You can't have a nil user with a non-nil password
1136
+ </span><span class="marked1"><a name="line515" /> 515 if @password != nil
1137
+ </span><span class="marked0"><a name="line516" /> 516 @user = &quot;&quot; if @user.nil?
1138
+ </span><span class="inferred1"><a name="line517" /> 517 end
1139
+ </span><span class="inferred0"><a name="line518" /> 518
1140
+ </span><span class="inferred1"><a name="line519" /> 519 # Reset dependant values
1141
+ </span><span class="marked0"><a name="line520" /> 520 @userinfo = nil
1142
+ </span><span class="marked1"><a name="line521" /> 521 @authority = nil
1143
+ </span><span class="inferred0"><a name="line522" /> 522
1144
+ </span><span class="inferred1"><a name="line523" /> 523 # Ensure we haven't created an invalid URI
1145
+ </span><span class="marked0"><a name="line524" /> 524 validate()
1146
+ </span><span class="inferred1"><a name="line525" /> 525 end
1147
+ </span><span class="inferred0"><a name="line526" /> 526
1148
+ </span><span class="inferred1"><a name="line527" /> 527 # Returns the password for this URI.
1149
+ </span><span class="marked0"><a name="line528" /> 528 def password
1150
+ </span><span class="marked1"><a name="line529" /> 529 return @password
1151
+ </span><span class="marked0"><a name="line530" /> 530 end
1152
+ </span><span class="inferred1"><a name="line531" /> 531
1153
+ </span><span class="inferred0"><a name="line532" /> 532 # Sets the password for this URI.
1154
+ </span><span class="marked1"><a name="line533" /> 533 def password=(new_password)
1155
+ </span><span class="marked0"><a name="line534" /> 534 @password = new_password
1156
+ </span><span class="inferred1"><a name="line535" /> 535
1157
+ </span><span class="inferred0"><a name="line536" /> 536 # You can't have a nil user with a non-nil password
1158
+ </span><span class="marked1"><a name="line537" /> 537 if @password != nil
1159
+ </span><span class="marked0"><a name="line538" /> 538 @user = &quot;&quot; if @user.nil?
1160
+ </span><span class="inferred1"><a name="line539" /> 539 end
1161
+ </span><span class="inferred0"><a name="line540" /> 540
1162
+ </span><span class="inferred1"><a name="line541" /> 541 # Reset dependant values
1163
+ </span><span class="marked0"><a name="line542" /> 542 @userinfo = nil
1164
+ </span><span class="marked1"><a name="line543" /> 543 @authority = nil
1165
+ </span><span class="inferred0"><a name="line544" /> 544
1166
+ </span><span class="inferred1"><a name="line545" /> 545 # Ensure we haven't created an invalid URI
1167
+ </span><span class="marked0"><a name="line546" /> 546 validate()
1168
+ </span><span class="inferred1"><a name="line547" /> 547 end
1169
+ </span><span class="inferred0"><a name="line548" /> 548
1170
+ </span><span class="inferred1"><a name="line549" /> 549 # Returns the username and password segment of this URI.
1171
+ </span><span class="marked0"><a name="line550" /> 550 def userinfo
1172
+ </span><span class="marked1"><a name="line551" /> 551 if !defined?(@userinfo) || @userinfo == nil
1173
+ </span><span class="marked0"><a name="line552" /> 552 current_user = self.user
1174
+ </span><span class="marked1"><a name="line553" /> 553 current_password = self.password
1175
+ </span><span class="marked0"><a name="line554" /> 554 if !current_user &amp;&amp; !current_password
1176
+ </span><span class="marked1"><a name="line555" /> 555 @userinfo = nil
1177
+ </span><span class="marked0"><a name="line556" /> 556 elsif current_user &amp;&amp; current_password
1178
+ </span><span class="marked1"><a name="line557" /> 557 @userinfo = &quot;#{current_user}:#{current_password}&quot;
1179
+ </span><span class="marked0"><a name="line558" /> 558 elsif current_user &amp;&amp; !current_password
1180
+ </span><span class="marked1"><a name="line559" /> 559 @userinfo = &quot;#{current_user}&quot;
1181
+ </span><span class="inferred0"><a name="line560" /> 560 end
1182
+ </span><span class="inferred1"><a name="line561" /> 561 end
1183
+ </span><span class="marked0"><a name="line562" /> 562 return @userinfo
1184
+ </span><span class="inferred1"><a name="line563" /> 563 end
1185
+ </span><span class="inferred0"><a name="line564" /> 564
1186
+ </span><span class="inferred1"><a name="line565" /> 565 # Sets the username and password segment of this URI.
1187
+ </span><span class="marked0"><a name="line566" /> 566 def userinfo=(new_userinfo)
1188
+ </span><span class="marked1"><a name="line567" /> 567 new_user = new_userinfo.to_s.strip.scan(/^(.*):/).flatten[0]
1189
+ </span><span class="marked0"><a name="line568" /> 568 new_password = new_userinfo.to_s.strip.scan(/:(.*)$/).flatten[0]
1190
+ </span><span class="inferred1"><a name="line569" /> 569
1191
+ </span><span class="inferred0"><a name="line570" /> 570 # Password assigned first to ensure validity in case of nil
1192
+ </span><span class="marked1"><a name="line571" /> 571 self.password = new_password
1193
+ </span><span class="marked0"><a name="line572" /> 572 self.user = new_user
1194
1194
  </span><span class="inferred1"><a name="line573" /> 573
1195
- </span><span class="inferred0"><a name="line574" /> 574 # Ensure we haven't created an invalid URI
1196
- </span><span class="marked1"><a name="line575" /> 575 validate()
1197
- </span><span class="inferred0"><a name="line576" /> 576 end
1198
- </span><span class="inferred1"><a name="line577" /> 577
1199
- </span><span class="inferred0"><a name="line578" /> 578 # Returns the authority segment of this URI.
1200
- </span><span class="marked1"><a name="line579" /> 579 def authority
1201
- </span><span class="marked0"><a name="line580" /> 580 if !defined?(@authority) || @authority.nil?
1202
- </span><span class="marked1"><a name="line581" /> 581 return nil if self.host.nil?
1203
- </span><span class="marked0"><a name="line582" /> 582 @authority = &quot;&quot;
1204
- </span><span class="marked1"><a name="line583" /> 583 if self.userinfo != nil
1205
- </span><span class="marked0"><a name="line584" /> 584 @authority &lt;&lt; &quot;#{self.userinfo}@&quot;
1206
- </span><span class="inferred1"><a name="line585" /> 585 end
1207
- </span><span class="marked0"><a name="line586" /> 586 @authority &lt;&lt; self.host
1208
- </span><span class="marked1"><a name="line587" /> 587 if self.specified_port != nil
1209
- </span><span class="marked0"><a name="line588" /> 588 @authority &lt;&lt; &quot;:#{self.specified_port}&quot;
1210
- </span><span class="inferred1"><a name="line589" /> 589 end
1211
- </span><span class="inferred0"><a name="line590" /> 590 end
1212
- </span><span class="marked1"><a name="line591" /> 591 return @authority
1213
- </span><span class="inferred0"><a name="line592" /> 592 end
1214
- </span><span class="inferred1"><a name="line593" /> 593
1215
- </span><span class="inferred0"><a name="line594" /> 594 # Sets the authority segment of this URI.
1216
- </span><span class="marked1"><a name="line595" /> 595 def authority=(new_authority)
1217
- </span><span class="marked0"><a name="line596" /> 596 if new_authority != nil
1218
- </span><span class="marked1"><a name="line597" /> 597 new_userinfo = new_authority.scan(/^([^\[\]]*)@/).flatten[0]
1219
- </span><span class="marked0"><a name="line598" /> 598 if new_userinfo != nil
1220
- </span><span class="marked1"><a name="line599" /> 599 new_user = new_userinfo.strip.scan(/^([^:]*):?/).flatten[0]
1221
- </span><span class="marked0"><a name="line600" /> 600 new_password = new_userinfo.strip.scan(/:(.*)$/).flatten[0]
1222
- </span><span class="inferred1"><a name="line601" /> 601 end
1223
- </span><span class="marked0"><a name="line602" /> 602 new_host =
1224
- </span><span class="inferred1"><a name="line603" /> 603 new_authority.gsub(/^([^\[\]]*)@/, &quot;&quot;).gsub(/:([^:@\[\]]*?)$/, &quot;&quot;)
1225
- </span><span class="marked0"><a name="line604" /> 604 new_port =
1226
- </span><span class="inferred1"><a name="line605" /> 605 new_authority.scan(/:([^:@\[\]]*?)$/).flatten[0]
1227
- </span><span class="inferred0"><a name="line606" /> 606 end
1228
- </span><span class="marked1"><a name="line607" /> 607 new_port = nil if new_port == &quot;&quot;
1229
- </span><span class="inferred0"><a name="line608" /> 608
1230
- </span><span class="inferred1"><a name="line609" /> 609 # Password assigned first to ensure validity in case of nil
1231
- </span><span class="marked0"><a name="line610" /> 610 self.password = new_password
1232
- </span><span class="marked1"><a name="line611" /> 611 self.user = new_user
1233
- </span><span class="marked0"><a name="line612" /> 612 self.host = new_host
1234
- </span><span class="inferred1"><a name="line613" /> 613
1235
- </span><span class="inferred0"><a name="line614" /> 614 # Port reset to allow port normalization
1236
- </span><span class="marked1"><a name="line615" /> 615 @port = nil
1237
- </span><span class="marked0"><a name="line616" /> 616 @specified_port = new_port
1238
- </span><span class="inferred1"><a name="line617" /> 617
1239
- </span><span class="inferred0"><a name="line618" /> 618 # Ensure we haven't created an invalid URI
1240
- </span><span class="marked1"><a name="line619" /> 619 validate()
1241
- </span><span class="inferred0"><a name="line620" /> 620 end
1242
- </span><span class="inferred1"><a name="line621" /> 621
1243
- </span><span class="inferred0"><a name="line622" /> 622 # Returns an array of known ip-based schemes. These schemes typically
1244
- </span><span class="inferred1"><a name="line623" /> 623 # use a similar URI form:
1245
- </span><span class="inferred0"><a name="line624" /> 624 # //&lt;user&gt;:&lt;password&gt;@&lt;host&gt;:&lt;port&gt;/&lt;url-path&gt;
1246
- </span><span class="marked1"><a name="line625" /> 625 def self.ip_based_schemes
1247
- </span><span class="marked0"><a name="line626" /> 626 return self.scheme_mapping.keys
1248
- </span><span class="marked1"><a name="line627" /> 627 end
1249
- </span><span class="inferred0"><a name="line628" /> 628
1250
- </span><span class="inferred1"><a name="line629" /> 629 # Returns a hash of common IP-based schemes and their default port
1251
- </span><span class="inferred0"><a name="line630" /> 630 # numbers. Adding new schemes to this hash, as necessary, will allow
1252
- </span><span class="inferred1"><a name="line631" /> 631 # for better URI normalization.
1253
- </span><span class="marked0"><a name="line632" /> 632 def self.scheme_mapping
1254
- </span><span class="marked1"><a name="line633" /> 633 if !defined?(@protocol_mapping) || @protocol_mapping.nil?
1255
- </span><span class="marked0"><a name="line634" /> 634 @protocol_mapping = {
1256
- </span><span class="inferred1"><a name="line635" /> 635 &quot;http&quot; =&gt; 80,
1257
- </span><span class="inferred0"><a name="line636" /> 636 &quot;https&quot; =&gt; 443,
1258
- </span><span class="inferred1"><a name="line637" /> 637 &quot;ftp&quot; =&gt; 21,
1259
- </span><span class="inferred0"><a name="line638" /> 638 &quot;tftp&quot; =&gt; 69,
1260
- </span><span class="inferred1"><a name="line639" /> 639 &quot;ssh&quot; =&gt; 22,
1261
- </span><span class="inferred0"><a name="line640" /> 640 &quot;svn+ssh&quot; =&gt; 22,
1262
- </span><span class="inferred1"><a name="line641" /> 641 &quot;telnet&quot; =&gt; 23,
1263
- </span><span class="inferred0"><a name="line642" /> 642 &quot;nntp&quot; =&gt; 119,
1264
- </span><span class="inferred1"><a name="line643" /> 643 &quot;gopher&quot; =&gt; 70,
1265
- </span><span class="inferred0"><a name="line644" /> 644 &quot;wais&quot; =&gt; 210,
1266
- </span><span class="inferred1"><a name="line645" /> 645 &quot;ldap&quot; =&gt; 389,
1267
- </span><span class="inferred0"><a name="line646" /> 646 &quot;prospero&quot; =&gt; 1525
1268
- </span><span class="inferred1"><a name="line647" /> 647 }
1269
- </span><span class="inferred0"><a name="line648" /> 648 end
1270
- </span><span class="marked1"><a name="line649" /> 649 return @protocol_mapping
1271
- </span><span class="inferred0"><a name="line650" /> 650 end
1272
- </span><span class="inferred1"><a name="line651" /> 651
1273
- </span><span class="inferred0"><a name="line652" /> 652 # Returns the port number for this URI. This method will normalize to the
1274
- </span><span class="inferred1"><a name="line653" /> 653 # default port for the URI's scheme if the port isn't explicitly specified
1275
- </span><span class="inferred0"><a name="line654" /> 654 # in the URI.
1276
- </span><span class="marked1"><a name="line655" /> 655 def port
1277
- </span><span class="marked0"><a name="line656" /> 656 if @port.to_i == 0
1278
- </span><span class="marked1"><a name="line657" /> 657 if self.scheme.nil?
1279
- </span><span class="marked0"><a name="line658" /> 658 @port = nil
1280
- </span><span class="inferred1"><a name="line659" /> 659 else
1281
- </span><span class="marked0"><a name="line660" /> 660 @port = self.class.scheme_mapping[self.scheme.strip.downcase]
1282
- </span><span class="inferred1"><a name="line661" /> 661 end
1283
- </span><span class="marked0"><a name="line662" /> 662 return @port
1284
- </span><span class="inferred1"><a name="line663" /> 663 else
1285
- </span><span class="marked0"><a name="line664" /> 664 @port = @port.to_i
1286
- </span><span class="marked1"><a name="line665" /> 665 return @port
1195
+ </span><span class="inferred0"><a name="line574" /> 574 # Reset dependant values
1196
+ </span><span class="marked1"><a name="line575" /> 575 @authority = nil
1197
+ </span><span class="inferred0"><a name="line576" /> 576
1198
+ </span><span class="inferred1"><a name="line577" /> 577 # Ensure we haven't created an invalid URI
1199
+ </span><span class="marked0"><a name="line578" /> 578 validate()
1200
+ </span><span class="inferred1"><a name="line579" /> 579 end
1201
+ </span><span class="inferred0"><a name="line580" /> 580
1202
+ </span><span class="inferred1"><a name="line581" /> 581 # Returns the host for this URI.
1203
+ </span><span class="marked0"><a name="line582" /> 582 def host
1204
+ </span><span class="marked1"><a name="line583" /> 583 return @host
1205
+ </span><span class="marked0"><a name="line584" /> 584 end
1206
+ </span><span class="inferred1"><a name="line585" /> 585
1207
+ </span><span class="inferred0"><a name="line586" /> 586 # Sets the host for this URI.
1208
+ </span><span class="marked1"><a name="line587" /> 587 def host=(new_host)
1209
+ </span><span class="marked0"><a name="line588" /> 588 @host = new_host
1210
+ </span><span class="inferred1"><a name="line589" /> 589
1211
+ </span><span class="inferred0"><a name="line590" /> 590 # Reset dependant values
1212
+ </span><span class="marked1"><a name="line591" /> 591 @authority = nil
1213
+ </span><span class="inferred0"><a name="line592" /> 592
1214
+ </span><span class="inferred1"><a name="line593" /> 593 # Ensure we haven't created an invalid URI
1215
+ </span><span class="marked0"><a name="line594" /> 594 validate()
1216
+ </span><span class="inferred1"><a name="line595" /> 595 end
1217
+ </span><span class="inferred0"><a name="line596" /> 596
1218
+ </span><span class="inferred1"><a name="line597" /> 597 # Returns the authority segment of this URI.
1219
+ </span><span class="marked0"><a name="line598" /> 598 def authority
1220
+ </span><span class="marked1"><a name="line599" /> 599 if !defined?(@authority) || @authority.nil?
1221
+ </span><span class="marked0"><a name="line600" /> 600 return nil if self.host.nil?
1222
+ </span><span class="marked1"><a name="line601" /> 601 @authority = &quot;&quot;
1223
+ </span><span class="marked0"><a name="line602" /> 602 if self.userinfo != nil
1224
+ </span><span class="marked1"><a name="line603" /> 603 @authority &lt;&lt; &quot;#{self.userinfo}@&quot;
1225
+ </span><span class="inferred0"><a name="line604" /> 604 end
1226
+ </span><span class="marked1"><a name="line605" /> 605 @authority &lt;&lt; self.host
1227
+ </span><span class="marked0"><a name="line606" /> 606 if self.specified_port != nil
1228
+ </span><span class="marked1"><a name="line607" /> 607 @authority &lt;&lt; &quot;:#{self.specified_port}&quot;
1229
+ </span><span class="inferred0"><a name="line608" /> 608 end
1230
+ </span><span class="inferred1"><a name="line609" /> 609 end
1231
+ </span><span class="marked0"><a name="line610" /> 610 return @authority
1232
+ </span><span class="inferred1"><a name="line611" /> 611 end
1233
+ </span><span class="inferred0"><a name="line612" /> 612
1234
+ </span><span class="inferred1"><a name="line613" /> 613 # Sets the authority segment of this URI.
1235
+ </span><span class="marked0"><a name="line614" /> 614 def authority=(new_authority)
1236
+ </span><span class="marked1"><a name="line615" /> 615 if new_authority
1237
+ </span><span class="marked0"><a name="line616" /> 616 new_userinfo = new_authority.scan(/^([^\[\]]*)@/).flatten[0]
1238
+ </span><span class="marked1"><a name="line617" /> 617 if new_userinfo
1239
+ </span><span class="marked0"><a name="line618" /> 618 new_user = new_userinfo.strip.scan(/^([^:]*):?/).flatten[0]
1240
+ </span><span class="marked1"><a name="line619" /> 619 new_password = new_userinfo.strip.scan(/:(.*)$/).flatten[0]
1241
+ </span><span class="inferred0"><a name="line620" /> 620 end
1242
+ </span><span class="marked1"><a name="line621" /> 621 new_host =
1243
+ </span><span class="inferred0"><a name="line622" /> 622 new_authority.gsub(/^([^\[\]]*)@/, &quot;&quot;).gsub(/:([^:@\[\]]*?)$/, &quot;&quot;)
1244
+ </span><span class="marked1"><a name="line623" /> 623 new_port =
1245
+ </span><span class="inferred0"><a name="line624" /> 624 new_authority.scan(/:([^:@\[\]]*?)$/).flatten[0]
1246
+ </span><span class="inferred1"><a name="line625" /> 625 end
1247
+ </span><span class="inferred0"><a name="line626" /> 626
1248
+ </span><span class="inferred1"><a name="line627" /> 627 # Password assigned first to ensure validity in case of nil
1249
+ </span><span class="marked0"><a name="line628" /> 628 self.password = new_password
1250
+ </span><span class="marked1"><a name="line629" /> 629 self.user = new_user
1251
+ </span><span class="marked0"><a name="line630" /> 630 self.host = new_host
1252
+ </span><span class="inferred1"><a name="line631" /> 631
1253
+ </span><span class="inferred0"><a name="line632" /> 632 # Port reset to allow port normalization
1254
+ </span><span class="marked1"><a name="line633" /> 633 @port = nil
1255
+ </span><span class="marked0"><a name="line634" /> 634 @specified_port = new_port
1256
+ </span><span class="inferred1"><a name="line635" /> 635
1257
+ </span><span class="inferred0"><a name="line636" /> 636 # Ensure we haven't created an invalid URI
1258
+ </span><span class="marked1"><a name="line637" /> 637 validate()
1259
+ </span><span class="inferred0"><a name="line638" /> 638 end
1260
+ </span><span class="inferred1"><a name="line639" /> 639
1261
+ </span><span class="inferred0"><a name="line640" /> 640 # Returns an array of known ip-based schemes. These schemes typically
1262
+ </span><span class="inferred1"><a name="line641" /> 641 # use a similar URI form:
1263
+ </span><span class="inferred0"><a name="line642" /> 642 # //&lt;user&gt;:&lt;password&gt;@&lt;host&gt;:&lt;port&gt;/&lt;url-path&gt;
1264
+ </span><span class="marked1"><a name="line643" /> 643 def self.ip_based_schemes
1265
+ </span><span class="marked0"><a name="line644" /> 644 return self.scheme_mapping.keys
1266
+ </span><span class="marked1"><a name="line645" /> 645 end
1267
+ </span><span class="inferred0"><a name="line646" /> 646
1268
+ </span><span class="inferred1"><a name="line647" /> 647 # Returns a hash of common IP-based schemes and their default port
1269
+ </span><span class="inferred0"><a name="line648" /> 648 # numbers. Adding new schemes to this hash, as necessary, will allow
1270
+ </span><span class="inferred1"><a name="line649" /> 649 # for better URI normalization.
1271
+ </span><span class="marked0"><a name="line650" /> 650 def self.scheme_mapping
1272
+ </span><span class="marked1"><a name="line651" /> 651 if !defined?(@protocol_mapping) || @protocol_mapping.nil?
1273
+ </span><span class="marked0"><a name="line652" /> 652 @protocol_mapping = {
1274
+ </span><span class="inferred1"><a name="line653" /> 653 &quot;http&quot; =&gt; 80,
1275
+ </span><span class="inferred0"><a name="line654" /> 654 &quot;https&quot; =&gt; 443,
1276
+ </span><span class="inferred1"><a name="line655" /> 655 &quot;ftp&quot; =&gt; 21,
1277
+ </span><span class="inferred0"><a name="line656" /> 656 &quot;tftp&quot; =&gt; 69,
1278
+ </span><span class="inferred1"><a name="line657" /> 657 &quot;ssh&quot; =&gt; 22,
1279
+ </span><span class="inferred0"><a name="line658" /> 658 &quot;svn+ssh&quot; =&gt; 22,
1280
+ </span><span class="inferred1"><a name="line659" /> 659 &quot;telnet&quot; =&gt; 23,
1281
+ </span><span class="inferred0"><a name="line660" /> 660 &quot;nntp&quot; =&gt; 119,
1282
+ </span><span class="inferred1"><a name="line661" /> 661 &quot;gopher&quot; =&gt; 70,
1283
+ </span><span class="inferred0"><a name="line662" /> 662 &quot;wais&quot; =&gt; 210,
1284
+ </span><span class="inferred1"><a name="line663" /> 663 &quot;ldap&quot; =&gt; 389,
1285
+ </span><span class="inferred0"><a name="line664" /> 664 &quot;prospero&quot; =&gt; 1525
1286
+ </span><span class="inferred1"><a name="line665" /> 665 }
1287
1287
  </span><span class="inferred0"><a name="line666" /> 666 end
1288
- </span><span class="marked1"><a name="line667" /> 667 end
1289
- </span><span class="inferred0"><a name="line668" /> 668
1290
- </span><span class="inferred1"><a name="line669" /> 669 # Sets the port for this URI.
1291
- </span><span class="marked0"><a name="line670" /> 670 def port=(new_port)
1292
- </span><span class="marked1"><a name="line671" /> 671 @port = new_port.to_s.to_i
1293
- </span><span class="marked0"><a name="line672" /> 672 @specified_port = @port
1294
- </span><span class="marked1"><a name="line673" /> 673 @authority = nil
1295
- </span><span class="inferred0"><a name="line674" /> 674 end
1296
- </span><span class="inferred1"><a name="line675" /> 675
1297
- </span><span class="inferred0"><a name="line676" /> 676 # Returns the port number that was actually specified in the URI string.
1298
- </span><span class="marked1"><a name="line677" /> 677 def specified_port
1299
- </span><span class="marked0"><a name="line678" /> 678 port = @specified_port.to_s.to_i
1300
- </span><span class="marked1"><a name="line679" /> 679 if port == 0
1301
- </span><span class="marked0"><a name="line680" /> 680 return nil
1288
+ </span><span class="marked1"><a name="line667" /> 667 return @protocol_mapping
1289
+ </span><span class="inferred0"><a name="line668" /> 668 end
1290
+ </span><span class="inferred1"><a name="line669" /> 669
1291
+ </span><span class="inferred0"><a name="line670" /> 670 # Returns the port number for this URI. This method will normalize to the
1292
+ </span><span class="inferred1"><a name="line671" /> 671 # default port for the URI's scheme if the port isn't explicitly specified
1293
+ </span><span class="inferred0"><a name="line672" /> 672 # in the URI.
1294
+ </span><span class="marked1"><a name="line673" /> 673 def port
1295
+ </span><span class="marked0"><a name="line674" /> 674 if @port.to_i == 0
1296
+ </span><span class="marked1"><a name="line675" /> 675 if self.scheme
1297
+ </span><span class="marked0"><a name="line676" /> 676 @port = self.class.scheme_mapping[self.scheme.strip.downcase]
1298
+ </span><span class="inferred1"><a name="line677" /> 677 else
1299
+ </span><span class="marked0"><a name="line678" /> 678 @port = nil
1300
+ </span><span class="inferred1"><a name="line679" /> 679 end
1301
+ </span><span class="marked0"><a name="line680" /> 680 return @port
1302
1302
  </span><span class="inferred1"><a name="line681" /> 681 else
1303
- </span><span class="marked0"><a name="line682" /> 682 return port
1304
- </span><span class="inferred1"><a name="line683" /> 683 end
1305
- </span><span class="inferred0"><a name="line684" /> 684 end
1306
- </span><span class="inferred1"><a name="line685" /> 685
1307
- </span><span class="inferred0"><a name="line686" /> 686 # Returns the path for this URI.
1308
- </span><span class="marked1"><a name="line687" /> 687 def path
1309
- </span><span class="marked0"><a name="line688" /> 688 return @path
1310
- </span><span class="marked1"><a name="line689" /> 689 end
1311
- </span><span class="inferred0"><a name="line690" /> 690
1312
- </span><span class="inferred1"><a name="line691" /> 691 # Sets the path for this URI.
1313
- </span><span class="marked0"><a name="line692" /> 692 def path=(new_path)
1314
- </span><span class="marked1"><a name="line693" /> 693 @path = new_path
1315
- </span><span class="marked0"><a name="line694" /> 694 end
1316
- </span><span class="inferred1"><a name="line695" /> 695
1317
- </span><span class="inferred0"><a name="line696" /> 696 # Returns the basename, if any, of the file at the path being referenced.
1318
- </span><span class="inferred1"><a name="line697" /> 697 # Returns nil if there is no path component.
1319
- </span><span class="marked0"><a name="line698" /> 698 def basename
1320
- </span><span class="marked1"><a name="line699" /> 699 return nil if self.path == nil
1321
- </span><span class="marked0"><a name="line700" /> 700 return File.basename(self.path).gsub(/;[^\/]*$/, &quot;&quot;)
1322
- </span><span class="inferred1"><a name="line701" /> 701 end
1323
- </span><span class="inferred0"><a name="line702" /> 702
1324
- </span><span class="inferred1"><a name="line703" /> 703 # Returns the extension, if any, of the file at the path being referenced.
1325
- </span><span class="inferred0"><a name="line704" /> 704 # Returns &quot;&quot; if there is no extension or nil if there is no path
1326
- </span><span class="inferred1"><a name="line705" /> 705 # component.
1327
- </span><span class="marked0"><a name="line706" /> 706 def extname
1328
- </span><span class="marked1"><a name="line707" /> 707 return nil if self.path == nil
1329
- </span><span class="marked0"><a name="line708" /> 708 return File.extname(self.basename.gsub(/;[^\/]*$/, &quot;&quot;))
1330
- </span><span class="inferred1"><a name="line709" /> 709 end
1331
- </span><span class="inferred0"><a name="line710" /> 710
1332
- </span><span class="inferred1"><a name="line711" /> 711 # Returns the query string for this URI.
1333
- </span><span class="marked0"><a name="line712" /> 712 def query
1334
- </span><span class="marked1"><a name="line713" /> 713 return @query
1335
- </span><span class="marked0"><a name="line714" /> 714 end
1336
- </span><span class="inferred1"><a name="line715" /> 715
1337
- </span><span class="inferred0"><a name="line716" /> 716 # Sets the query string for this URI.
1338
- </span><span class="marked1"><a name="line717" /> 717 def query=(new_query)
1339
- </span><span class="marked0"><a name="line718" /> 718 @query = new_query
1303
+ </span><span class="marked0"><a name="line682" /> 682 @port = @port.to_i
1304
+ </span><span class="marked1"><a name="line683" /> 683 return @port
1305
+ </span><span class="inferred0"><a name="line684" /> 684 end
1306
+ </span><span class="marked1"><a name="line685" /> 685 end
1307
+ </span><span class="inferred0"><a name="line686" /> 686
1308
+ </span><span class="inferred1"><a name="line687" /> 687 # Sets the port for this URI.
1309
+ </span><span class="marked0"><a name="line688" /> 688 def port=(new_port)
1310
+ </span><span class="marked1"><a name="line689" /> 689 @port = new_port.to_s.to_i
1311
+ </span><span class="marked0"><a name="line690" /> 690 @specified_port = @port
1312
+ </span><span class="marked1"><a name="line691" /> 691 @authority = nil
1313
+ </span><span class="inferred0"><a name="line692" /> 692 end
1314
+ </span><span class="inferred1"><a name="line693" /> 693
1315
+ </span><span class="inferred0"><a name="line694" /> 694 # Returns the port number that was actually specified in the URI string.
1316
+ </span><span class="marked1"><a name="line695" /> 695 def specified_port
1317
+ </span><span class="marked0"><a name="line696" /> 696 port = @specified_port.to_s.to_i
1318
+ </span><span class="marked1"><a name="line697" /> 697 if port == 0
1319
+ </span><span class="marked0"><a name="line698" /> 698 return nil
1320
+ </span><span class="inferred1"><a name="line699" /> 699 else
1321
+ </span><span class="marked0"><a name="line700" /> 700 return port
1322
+ </span><span class="inferred1"><a name="line701" /> 701 end
1323
+ </span><span class="inferred0"><a name="line702" /> 702 end
1324
+ </span><span class="inferred1"><a name="line703" /> 703
1325
+ </span><span class="inferred0"><a name="line704" /> 704 # Returns the path for this URI.
1326
+ </span><span class="marked1"><a name="line705" /> 705 def path
1327
+ </span><span class="marked0"><a name="line706" /> 706 return @path
1328
+ </span><span class="marked1"><a name="line707" /> 707 end
1329
+ </span><span class="inferred0"><a name="line708" /> 708
1330
+ </span><span class="inferred1"><a name="line709" /> 709 # Sets the path for this URI.
1331
+ </span><span class="marked0"><a name="line710" /> 710 def path=(new_path)
1332
+ </span><span class="marked1"><a name="line711" /> 711 @path = (new_path || &quot;&quot;)
1333
+ </span><span class="marked0"><a name="line712" /> 712 end
1334
+ </span><span class="inferred1"><a name="line713" /> 713
1335
+ </span><span class="inferred0"><a name="line714" /> 714 # Returns the basename, if any, of the file at the path being referenced.
1336
+ </span><span class="inferred1"><a name="line715" /> 715 # Returns nil if there is no path component.
1337
+ </span><span class="marked0"><a name="line716" /> 716 def basename
1338
+ </span><span class="inferred1"><a name="line717" /> 717 # Path cannot be nil
1339
+ </span><span class="marked0"><a name="line718" /> 718 return File.basename(self.path).gsub(/;[^\/]*$/, &quot;&quot;)
1340
1340
  </span><span class="marked1"><a name="line719" /> 719 end
1341
- </span><span class="inferred0"><a name="line720" /> 720
1342
- </span><span class="inferred1"><a name="line721" /> 721 # Returns the fragment for this URI.
1343
- </span><span class="marked0"><a name="line722" /> 722 def fragment
1344
- </span><span class="marked1"><a name="line723" /> 723 return @fragment
1345
- </span><span class="marked0"><a name="line724" /> 724 end
1346
- </span><span class="inferred1"><a name="line725" /> 725
1347
- </span><span class="inferred0"><a name="line726" /> 726 # Sets the fragment for this URI.
1348
- </span><span class="marked1"><a name="line727" /> 727 def fragment=(new_fragment)
1349
- </span><span class="marked0"><a name="line728" /> 728 @fragment = new_fragment
1350
- </span><span class="marked1"><a name="line729" /> 729 end
1351
- </span><span class="inferred0"><a name="line730" /> 730
1352
- </span><span class="inferred1"><a name="line731" /> 731 # Returns true if the URI uses an IP-based protocol.
1353
- </span><span class="marked0"><a name="line732" /> 732 def ip_based?
1354
- </span><span class="marked1"><a name="line733" /> 733 return false if self.scheme.nil?
1355
- </span><span class="marked0"><a name="line734" /> 734 return self.class.ip_based_schemes.include?(self.scheme.strip.downcase)
1356
- </span><span class="inferred1"><a name="line735" /> 735 end
1357
- </span><span class="inferred0"><a name="line736" /> 736
1358
- </span><span class="inferred1"><a name="line737" /> 737 # Returns true if this URI is known to be relative.
1359
- </span><span class="marked0"><a name="line738" /> 738 def relative?
1360
- </span><span class="marked1"><a name="line739" /> 739 return self.scheme.nil?
1361
- </span><span class="marked0"><a name="line740" /> 740 end
1362
- </span><span class="inferred1"><a name="line741" /> 741
1363
- </span><span class="inferred0"><a name="line742" /> 742 # Returns true if this URI is known to be absolute.
1364
- </span><span class="marked1"><a name="line743" /> 743 def absolute?
1365
- </span><span class="marked0"><a name="line744" /> 744 return !relative?
1366
- </span><span class="marked1"><a name="line745" /> 745 end
1367
- </span><span class="inferred0"><a name="line746" /> 746
1368
- </span><span class="inferred1"><a name="line747" /> 747 # Joins two URIs together.
1369
- </span><span class="marked0"><a name="line748" /> 748 def +(uri)
1370
- </span><span class="marked1"><a name="line749" /> 749 if !uri.kind_of?(self.class)
1371
- </span><span class="marked0"><a name="line750" /> 750 uri = URI.parse(uri.to_s)
1372
- </span><span class="inferred1"><a name="line751" /> 751 end
1373
- </span><span class="marked0"><a name="line752" /> 752 if uri.to_s == &quot;&quot;
1374
- </span><span class="marked1"><a name="line753" /> 753 return self.dup
1341
+ </span><span class="inferred0"><a name="line720" /> 720
1342
+ </span><span class="inferred1"><a name="line721" /> 721 # Returns the extension, if any, of the file at the path being referenced.
1343
+ </span><span class="inferred0"><a name="line722" /> 722 # Returns &quot;&quot; if there is no extension or nil if there is no path
1344
+ </span><span class="inferred1"><a name="line723" /> 723 # component.
1345
+ </span><span class="marked0"><a name="line724" /> 724 def extname
1346
+ </span><span class="marked1"><a name="line725" /> 725 return nil unless self.path
1347
+ </span><span class="marked0"><a name="line726" /> 726 return File.extname(self.basename)
1348
+ </span><span class="inferred1"><a name="line727" /> 727 end
1349
+ </span><span class="inferred0"><a name="line728" /> 728
1350
+ </span><span class="inferred1"><a name="line729" /> 729 # Returns the query string for this URI.
1351
+ </span><span class="marked0"><a name="line730" /> 730 def query
1352
+ </span><span class="marked1"><a name="line731" /> 731 return @query
1353
+ </span><span class="marked0"><a name="line732" /> 732 end
1354
+ </span><span class="inferred1"><a name="line733" /> 733
1355
+ </span><span class="inferred0"><a name="line734" /> 734 # Sets the query string for this URI.
1356
+ </span><span class="marked1"><a name="line735" /> 735 def query=(new_query)
1357
+ </span><span class="marked0"><a name="line736" /> 736 @query = new_query
1358
+ </span><span class="marked1"><a name="line737" /> 737 end
1359
+ </span><span class="inferred0"><a name="line738" /> 738
1360
+ </span><span class="inferred1"><a name="line739" /> 739 # Returns the fragment for this URI.
1361
+ </span><span class="marked0"><a name="line740" /> 740 def fragment
1362
+ </span><span class="marked1"><a name="line741" /> 741 return @fragment
1363
+ </span><span class="marked0"><a name="line742" /> 742 end
1364
+ </span><span class="inferred1"><a name="line743" /> 743
1365
+ </span><span class="inferred0"><a name="line744" /> 744 # Sets the fragment for this URI.
1366
+ </span><span class="marked1"><a name="line745" /> 745 def fragment=(new_fragment)
1367
+ </span><span class="marked0"><a name="line746" /> 746 @fragment = new_fragment
1368
+ </span><span class="marked1"><a name="line747" /> 747 end
1369
+ </span><span class="inferred0"><a name="line748" /> 748
1370
+ </span><span class="inferred1"><a name="line749" /> 749 # Returns true if the URI uses an IP-based protocol.
1371
+ </span><span class="marked0"><a name="line750" /> 750 def ip_based?
1372
+ </span><span class="marked1"><a name="line751" /> 751 if self.scheme
1373
+ </span><span class="marked0"><a name="line752" /> 752 return self.class.ip_based_schemes.include?(
1374
+ </span><span class="marked1"><a name="line753" /> 753 self.scheme.strip.downcase)
1375
1375
  </span><span class="inferred0"><a name="line754" /> 754 end
1376
- </span><span class="inferred1"><a name="line755" /> 755
1377
- </span><span class="marked0"><a name="line756" /> 756 joined_scheme = nil
1378
- </span><span class="marked1"><a name="line757" /> 757 joined_user = nil
1379
- </span><span class="marked0"><a name="line758" /> 758 joined_password = nil
1380
- </span><span class="marked1"><a name="line759" /> 759 joined_host = nil
1381
- </span><span class="marked0"><a name="line760" /> 760 joined_port = nil
1382
- </span><span class="marked1"><a name="line761" /> 761 joined_path = nil
1383
- </span><span class="marked0"><a name="line762" /> 762 joined_query = nil
1384
- </span><span class="marked1"><a name="line763" /> 763 joined_fragment = nil
1385
- </span><span class="inferred0"><a name="line764" /> 764
1386
- </span><span class="inferred1"><a name="line765" /> 765 # Section 5.2.2 of RFC 3986
1387
- </span><span class="marked0"><a name="line766" /> 766 if uri.scheme != nil
1388
- </span><span class="marked1"><a name="line767" /> 767 joined_scheme = uri.scheme
1389
- </span><span class="marked0"><a name="line768" /> 768 joined_user = uri.user
1390
- </span><span class="marked1"><a name="line769" /> 769 joined_password = uri.password
1391
- </span><span class="marked0"><a name="line770" /> 770 joined_host = uri.host
1392
- </span><span class="marked1"><a name="line771" /> 771 joined_port = uri.specified_port
1393
- </span><span class="marked0"><a name="line772" /> 772 joined_path = self.class.normalize_path(uri.path)
1394
- </span><span class="marked1"><a name="line773" /> 773 joined_query = uri.query
1395
- </span><span class="inferred0"><a name="line774" /> 774 else
1396
- </span><span class="marked1"><a name="line775" /> 775 if uri.authority != nil
1397
- </span><span class="marked0"><a name="line776" /> 776 joined_user = uri.user
1398
- </span><span class="marked1"><a name="line777" /> 777 joined_password = uri.password
1399
- </span><span class="marked0"><a name="line778" /> 778 joined_host = uri.host
1400
- </span><span class="marked1"><a name="line779" /> 779 joined_port = uri.specified_port
1401
- </span><span class="marked0"><a name="line780" /> 780 joined_path = self.class.normalize_path(uri.path)
1402
- </span><span class="marked1"><a name="line781" /> 781 joined_query = uri.query
1403
- </span><span class="inferred0"><a name="line782" /> 782 else
1404
- </span><span class="marked1"><a name="line783" /> 783 if uri.path == nil || uri.path == &quot;&quot;
1405
- </span><span class="marked0"><a name="line784" /> 784 joined_path = self.path
1406
- </span><span class="marked1"><a name="line785" /> 785 if uri.query != nil
1407
- </span><span class="marked0"><a name="line786" /> 786 joined_query = uri.query
1408
- </span><span class="inferred1"><a name="line787" /> 787 else
1409
- </span><span class="marked0"><a name="line788" /> 788 joined_query = self.query
1410
- </span><span class="inferred1"><a name="line789" /> 789 end
1411
- </span><span class="inferred0"><a name="line790" /> 790 else
1412
- </span><span class="marked1"><a name="line791" /> 791 if uri.path[0..0] == &quot;/&quot;
1413
- </span><span class="marked0"><a name="line792" /> 792 joined_path = self.class.normalize_path(uri.path)
1414
- </span><span class="inferred1"><a name="line793" /> 793 else
1415
- </span><span class="marked0"><a name="line794" /> 794 base_path = self.path.dup
1416
- </span><span class="marked1"><a name="line795" /> 795 base_path = &quot;&quot; if base_path == nil
1417
- </span><span class="marked0"><a name="line796" /> 796 base_path = self.class.normalize_path(base_path)
1418
- </span><span class="inferred1"><a name="line797" /> 797
1419
- </span><span class="inferred0"><a name="line798" /> 798 # Section 5.2.3 of RFC 3986
1420
- </span><span class="inferred1"><a name="line799" /> 799 #
1421
- </span><span class="inferred0"><a name="line800" /> 800 # Removes the right-most path segment from the base path.
1422
- </span><span class="marked1"><a name="line801" /> 801 if base_path =~ /\//
1423
- </span><span class="marked0"><a name="line802" /> 802 base_path.gsub!(/\/[^\/]+$/, &quot;/&quot;)
1424
- </span><span class="inferred1"><a name="line803" /> 803 else
1425
- </span><span class="marked0"><a name="line804" /> 804 base_path = &quot;&quot;
1426
- </span><span class="inferred1"><a name="line805" /> 805 end
1427
- </span><span class="inferred0"><a name="line806" /> 806
1428
- </span><span class="inferred1"><a name="line807" /> 807 # If the base path is empty and an authority segment has been
1429
- </span><span class="inferred0"><a name="line808" /> 808 # defined, use a base path of &quot;/&quot;
1430
- </span><span class="marked1"><a name="line809" /> 809 if base_path == &quot;&quot; &amp;&amp; self.authority != nil
1431
- </span><span class="marked0"><a name="line810" /> 810 base_path = &quot;/&quot;
1432
- </span><span class="inferred1"><a name="line811" /> 811 end
1433
- </span><span class="inferred0"><a name="line812" /> 812
1434
- </span><span class="marked1"><a name="line813" /> 813 joined_path = self.class.normalize_path(base_path + uri.path)
1435
- </span><span class="inferred0"><a name="line814" /> 814 end
1436
- </span><span class="marked1"><a name="line815" /> 815 joined_query = uri.query
1437
- </span><span class="inferred0"><a name="line816" /> 816 end
1438
- </span><span class="marked1"><a name="line817" /> 817 joined_user = self.user
1439
- </span><span class="marked0"><a name="line818" /> 818 joined_password = self.password
1440
- </span><span class="marked1"><a name="line819" /> 819 joined_host = self.host
1441
- </span><span class="marked0"><a name="line820" /> 820 joined_port = self.specified_port
1442
- </span><span class="inferred1"><a name="line821" /> 821 end
1443
- </span><span class="marked0"><a name="line822" /> 822 joined_scheme = self.scheme
1444
- </span><span class="inferred1"><a name="line823" /> 823 end
1445
- </span><span class="marked0"><a name="line824" /> 824 joined_fragment = uri.fragment
1446
- </span><span class="inferred1"><a name="line825" /> 825
1447
- </span><span class="marked0"><a name="line826" /> 826 return Addressable::URI.new(
1448
- </span><span class="inferred1"><a name="line827" /> 827 joined_scheme,
1449
- </span><span class="inferred0"><a name="line828" /> 828 joined_user,
1450
- </span><span class="inferred1"><a name="line829" /> 829 joined_password,
1451
- </span><span class="inferred0"><a name="line830" /> 830 joined_host,
1452
- </span><span class="inferred1"><a name="line831" /> 831 joined_port,
1453
- </span><span class="inferred0"><a name="line832" /> 832 joined_path,
1454
- </span><span class="inferred1"><a name="line833" /> 833 joined_query,
1455
- </span><span class="inferred0"><a name="line834" /> 834 joined_fragment
1456
- </span><span class="marked1"><a name="line835" /> 835 )
1457
- </span><span class="inferred0"><a name="line836" /> 836 end
1458
- </span><span class="inferred1"><a name="line837" /> 837
1459
- </span><span class="inferred0"><a name="line838" /> 838 # Merges two URIs together.
1460
- </span><span class="marked1"><a name="line839" /> 839 def merge(uri)
1461
- </span><span class="marked0"><a name="line840" /> 840 return self + uri
1462
- </span><span class="marked1"><a name="line841" /> 841 end
1463
- </span><span class="marked0"><a name="line842" /> 842 alias_method :join, :merge
1464
- </span><span class="inferred1"><a name="line843" /> 843
1465
- </span><span class="inferred0"><a name="line844" /> 844 # Destructive form of merge.
1466
- </span><span class="marked1"><a name="line845" /> 845 def merge!(uri)
1467
- </span><span class="marked0"><a name="line846" /> 846 replace_self(self.merge(uri))
1468
- </span><span class="marked1"><a name="line847" /> 847 end
1469
- </span><span class="marked0"><a name="line848" /> 848 alias_method :join!, :merge!
1470
- </span><span class="inferred1"><a name="line849" /> 849
1471
- </span><span class="inferred0"><a name="line850" /> 850 # Returns the shortest normalized relative form of this URI that uses the
1472
- </span><span class="inferred1"><a name="line851" /> 851 # supplied URI as a base for resolution. Returns an absolute URI if
1473
- </span><span class="inferred0"><a name="line852" /> 852 # necessary.
1474
- </span><span class="marked1"><a name="line853" /> 853 def route_from(uri)
1475
- </span><span class="marked0"><a name="line854" /> 854 uri = uri.kind_of?(self.class) ? uri : self.class.parse(uri.to_s)
1476
- </span><span class="marked1"><a name="line855" /> 855 uri = uri.normalize
1477
- </span><span class="marked0"><a name="line856" /> 856 normalized_self = self.normalize
1478
- </span><span class="marked1"><a name="line857" /> 857 if normalized_self.relative?
1479
- </span><span class="marked0"><a name="line858" /> 858 raise ArgumentError, &quot;Expected absolute URI, got: #{self.to_s}&quot;
1480
- </span><span class="inferred1"><a name="line859" /> 859 end
1481
- </span><span class="marked0"><a name="line860" /> 860 if uri.relative?
1482
- </span><span class="marked1"><a name="line861" /> 861 raise ArgumentError, &quot;Expected absolute URI, got: #{self.to_s}&quot;
1483
- </span><span class="inferred0"><a name="line862" /> 862 end
1484
- </span><span class="marked1"><a name="line863" /> 863 if normalized_self == uri
1485
- </span><span class="marked0"><a name="line864" /> 864 return Addressable::URI.parse(&quot;##{normalized_self.fragment}&quot;)
1486
- </span><span class="inferred1"><a name="line865" /> 865 end
1487
- </span><span class="marked0"><a name="line866" /> 866 segments = normalized_self.to_h
1488
- </span><span class="marked1"><a name="line867" /> 867 if normalized_self.scheme == uri.scheme
1489
- </span><span class="marked0"><a name="line868" /> 868 segments[:scheme] = nil
1490
- </span><span class="marked1"><a name="line869" /> 869 if normalized_self.authority == uri.authority
1491
- </span><span class="marked0"><a name="line870" /> 870 segments[:user] = nil
1492
- </span><span class="marked1"><a name="line871" /> 871 segments[:password] = nil
1493
- </span><span class="marked0"><a name="line872" /> 872 segments[:host] = nil
1494
- </span><span class="marked1"><a name="line873" /> 873 segments[:port] = nil
1495
- </span><span class="marked0"><a name="line874" /> 874 if normalized_self.path == uri.path
1496
- </span><span class="marked1"><a name="line875" /> 875 segments[:path] = nil
1497
- </span><span class="marked0"><a name="line876" /> 876 if normalized_self.query == uri.query
1498
- </span><span class="marked1"><a name="line877" /> 877 segments[:query] = nil
1499
- </span><span class="inferred0"><a name="line878" /> 878 end
1500
- </span><span class="inferred1"><a name="line879" /> 879 end
1501
- </span><span class="inferred0"><a name="line880" /> 880 end
1502
- </span><span class="inferred1"><a name="line881" /> 881 end
1503
- </span><span class="inferred0"><a name="line882" /> 882 # Avoid network-path references.
1504
- </span><span class="marked1"><a name="line883" /> 883 if segments[:scheme] == nil &amp;&amp; segments[:host] != nil
1505
- </span><span class="marked0"><a name="line884" /> 884 segments[:scheme] = normalized_self.scheme
1376
+ </span><span class="marked1"><a name="line755" /> 755 return false
1377
+ </span><span class="inferred0"><a name="line756" /> 756 end
1378
+ </span><span class="inferred1"><a name="line757" /> 757
1379
+ </span><span class="inferred0"><a name="line758" /> 758 # Returns true if this URI is known to be relative.
1380
+ </span><span class="marked1"><a name="line759" /> 759 def relative?
1381
+ </span><span class="marked0"><a name="line760" /> 760 return self.scheme.nil?
1382
+ </span><span class="marked1"><a name="line761" /> 761 end
1383
+ </span><span class="inferred0"><a name="line762" /> 762
1384
+ </span><span class="inferred1"><a name="line763" /> 763 # Returns true if this URI is known to be absolute.
1385
+ </span><span class="marked0"><a name="line764" /> 764 def absolute?
1386
+ </span><span class="marked1"><a name="line765" /> 765 return !relative?
1387
+ </span><span class="marked0"><a name="line766" /> 766 end
1388
+ </span><span class="inferred1"><a name="line767" /> 767
1389
+ </span><span class="inferred0"><a name="line768" /> 768 # Joins two URIs together.
1390
+ </span><span class="marked1"><a name="line769" /> 769 def +(uri)
1391
+ </span><span class="marked0"><a name="line770" /> 770 if !uri.kind_of?(self.class)
1392
+ </span><span class="marked1"><a name="line771" /> 771 uri = URI.parse(uri.to_s)
1393
+ </span><span class="inferred0"><a name="line772" /> 772 end
1394
+ </span><span class="marked1"><a name="line773" /> 773 if uri.to_s == &quot;&quot;
1395
+ </span><span class="marked0"><a name="line774" /> 774 return self.dup
1396
+ </span><span class="inferred1"><a name="line775" /> 775 end
1397
+ </span><span class="inferred0"><a name="line776" /> 776
1398
+ </span><span class="marked1"><a name="line777" /> 777 joined_scheme = nil
1399
+ </span><span class="marked0"><a name="line778" /> 778 joined_user = nil
1400
+ </span><span class="marked1"><a name="line779" /> 779 joined_password = nil
1401
+ </span><span class="marked0"><a name="line780" /> 780 joined_host = nil
1402
+ </span><span class="marked1"><a name="line781" /> 781 joined_port = nil
1403
+ </span><span class="marked0"><a name="line782" /> 782 joined_path = nil
1404
+ </span><span class="marked1"><a name="line783" /> 783 joined_query = nil
1405
+ </span><span class="marked0"><a name="line784" /> 784 joined_fragment = nil
1406
+ </span><span class="inferred1"><a name="line785" /> 785
1407
+ </span><span class="inferred0"><a name="line786" /> 786 # Section 5.2.2 of RFC 3986
1408
+ </span><span class="marked1"><a name="line787" /> 787 if uri.scheme != nil
1409
+ </span><span class="marked0"><a name="line788" /> 788 joined_scheme = uri.scheme
1410
+ </span><span class="marked1"><a name="line789" /> 789 joined_user = uri.user
1411
+ </span><span class="marked0"><a name="line790" /> 790 joined_password = uri.password
1412
+ </span><span class="marked1"><a name="line791" /> 791 joined_host = uri.host
1413
+ </span><span class="marked0"><a name="line792" /> 792 joined_port = uri.specified_port
1414
+ </span><span class="marked1"><a name="line793" /> 793 joined_path = self.class.normalize_path(uri.path)
1415
+ </span><span class="marked0"><a name="line794" /> 794 joined_query = uri.query
1416
+ </span><span class="inferred1"><a name="line795" /> 795 else
1417
+ </span><span class="marked0"><a name="line796" /> 796 if uri.authority != nil
1418
+ </span><span class="marked1"><a name="line797" /> 797 joined_user = uri.user
1419
+ </span><span class="marked0"><a name="line798" /> 798 joined_password = uri.password
1420
+ </span><span class="marked1"><a name="line799" /> 799 joined_host = uri.host
1421
+ </span><span class="marked0"><a name="line800" /> 800 joined_port = uri.specified_port
1422
+ </span><span class="marked1"><a name="line801" /> 801 joined_path = self.class.normalize_path(uri.path)
1423
+ </span><span class="marked0"><a name="line802" /> 802 joined_query = uri.query
1424
+ </span><span class="inferred1"><a name="line803" /> 803 else
1425
+ </span><span class="marked0"><a name="line804" /> 804 if uri.path == nil || uri.path == &quot;&quot;
1426
+ </span><span class="marked1"><a name="line805" /> 805 joined_path = self.path
1427
+ </span><span class="marked0"><a name="line806" /> 806 if uri.query != nil
1428
+ </span><span class="marked1"><a name="line807" /> 807 joined_query = uri.query
1429
+ </span><span class="inferred0"><a name="line808" /> 808 else
1430
+ </span><span class="marked1"><a name="line809" /> 809 joined_query = self.query
1431
+ </span><span class="inferred0"><a name="line810" /> 810 end
1432
+ </span><span class="inferred1"><a name="line811" /> 811 else
1433
+ </span><span class="marked0"><a name="line812" /> 812 if uri.path[0..0] == &quot;/&quot;
1434
+ </span><span class="marked1"><a name="line813" /> 813 joined_path = self.class.normalize_path(uri.path)
1435
+ </span><span class="inferred0"><a name="line814" /> 814 else
1436
+ </span><span class="marked1"><a name="line815" /> 815 base_path = self.path.dup
1437
+ </span><span class="marked0"><a name="line816" /> 816 base_path = &quot;&quot; if base_path == nil
1438
+ </span><span class="marked1"><a name="line817" /> 817 base_path = self.class.normalize_path(base_path)
1439
+ </span><span class="inferred0"><a name="line818" /> 818
1440
+ </span><span class="inferred1"><a name="line819" /> 819 # Section 5.2.3 of RFC 3986
1441
+ </span><span class="inferred0"><a name="line820" /> 820 #
1442
+ </span><span class="inferred1"><a name="line821" /> 821 # Removes the right-most path segment from the base path.
1443
+ </span><span class="marked0"><a name="line822" /> 822 if base_path =~ /\//
1444
+ </span><span class="marked1"><a name="line823" /> 823 base_path.gsub!(/\/[^\/]+$/, &quot;/&quot;)
1445
+ </span><span class="inferred0"><a name="line824" /> 824 else
1446
+ </span><span class="marked1"><a name="line825" /> 825 base_path = &quot;&quot;
1447
+ </span><span class="inferred0"><a name="line826" /> 826 end
1448
+ </span><span class="inferred1"><a name="line827" /> 827
1449
+ </span><span class="inferred0"><a name="line828" /> 828 # If the base path is empty and an authority segment has been
1450
+ </span><span class="inferred1"><a name="line829" /> 829 # defined, use a base path of &quot;/&quot;
1451
+ </span><span class="marked0"><a name="line830" /> 830 if base_path == &quot;&quot; &amp;&amp; self.authority != nil
1452
+ </span><span class="marked1"><a name="line831" /> 831 base_path = &quot;/&quot;
1453
+ </span><span class="inferred0"><a name="line832" /> 832 end
1454
+ </span><span class="inferred1"><a name="line833" /> 833
1455
+ </span><span class="marked0"><a name="line834" /> 834 joined_path = self.class.normalize_path(base_path + uri.path)
1456
+ </span><span class="inferred1"><a name="line835" /> 835 end
1457
+ </span><span class="marked0"><a name="line836" /> 836 joined_query = uri.query
1458
+ </span><span class="inferred1"><a name="line837" /> 837 end
1459
+ </span><span class="marked0"><a name="line838" /> 838 joined_user = self.user
1460
+ </span><span class="marked1"><a name="line839" /> 839 joined_password = self.password
1461
+ </span><span class="marked0"><a name="line840" /> 840 joined_host = self.host
1462
+ </span><span class="marked1"><a name="line841" /> 841 joined_port = self.specified_port
1463
+ </span><span class="inferred0"><a name="line842" /> 842 end
1464
+ </span><span class="marked1"><a name="line843" /> 843 joined_scheme = self.scheme
1465
+ </span><span class="inferred0"><a name="line844" /> 844 end
1466
+ </span><span class="marked1"><a name="line845" /> 845 joined_fragment = uri.fragment
1467
+ </span><span class="inferred0"><a name="line846" /> 846
1468
+ </span><span class="marked1"><a name="line847" /> 847 return Addressable::URI.new(
1469
+ </span><span class="inferred0"><a name="line848" /> 848 joined_scheme,
1470
+ </span><span class="inferred1"><a name="line849" /> 849 joined_user,
1471
+ </span><span class="inferred0"><a name="line850" /> 850 joined_password,
1472
+ </span><span class="inferred1"><a name="line851" /> 851 joined_host,
1473
+ </span><span class="inferred0"><a name="line852" /> 852 joined_port,
1474
+ </span><span class="inferred1"><a name="line853" /> 853 joined_path,
1475
+ </span><span class="inferred0"><a name="line854" /> 854 joined_query,
1476
+ </span><span class="inferred1"><a name="line855" /> 855 joined_fragment
1477
+ </span><span class="marked0"><a name="line856" /> 856 )
1478
+ </span><span class="inferred1"><a name="line857" /> 857 end
1479
+ </span><span class="inferred0"><a name="line858" /> 858
1480
+ </span><span class="inferred1"><a name="line859" /> 859 # Merges two URIs together.
1481
+ </span><span class="marked0"><a name="line860" /> 860 def merge(uri)
1482
+ </span><span class="marked1"><a name="line861" /> 861 return self + uri
1483
+ </span><span class="marked0"><a name="line862" /> 862 end
1484
+ </span><span class="marked1"><a name="line863" /> 863 alias_method :join, :merge
1485
+ </span><span class="inferred0"><a name="line864" /> 864
1486
+ </span><span class="inferred1"><a name="line865" /> 865 # Destructive form of merge.
1487
+ </span><span class="marked0"><a name="line866" /> 866 def merge!(uri)
1488
+ </span><span class="marked1"><a name="line867" /> 867 replace_self(self.merge(uri))
1489
+ </span><span class="marked0"><a name="line868" /> 868 end
1490
+ </span><span class="marked1"><a name="line869" /> 869 alias_method :join!, :merge!
1491
+ </span><span class="inferred0"><a name="line870" /> 870
1492
+ </span><span class="inferred1"><a name="line871" /> 871 # Returns the shortest normalized relative form of this URI that uses the
1493
+ </span><span class="inferred0"><a name="line872" /> 872 # supplied URI as a base for resolution. Returns an absolute URI if
1494
+ </span><span class="inferred1"><a name="line873" /> 873 # necessary.
1495
+ </span><span class="marked0"><a name="line874" /> 874 def route_from(uri)
1496
+ </span><span class="marked1"><a name="line875" /> 875 uri = self.class.parse(uri).normalize
1497
+ </span><span class="marked0"><a name="line876" /> 876 normalized_self = self.normalize
1498
+ </span><span class="marked1"><a name="line877" /> 877 if normalized_self.relative?
1499
+ </span><span class="marked0"><a name="line878" /> 878 raise ArgumentError, &quot;Expected absolute URI, got: #{self.to_s}&quot;
1500
+ </span><span class="inferred1"><a name="line879" /> 879 end
1501
+ </span><span class="marked0"><a name="line880" /> 880 if uri.relative?
1502
+ </span><span class="marked1"><a name="line881" /> 881 raise ArgumentError, &quot;Expected absolute URI, got: #{uri.to_s}&quot;
1503
+ </span><span class="inferred0"><a name="line882" /> 882 end
1504
+ </span><span class="marked1"><a name="line883" /> 883 if normalized_self == uri
1505
+ </span><span class="marked0"><a name="line884" /> 884 return Addressable::URI.parse(&quot;##{normalized_self.fragment}&quot;)
1506
1506
  </span><span class="inferred1"><a name="line885" /> 885 end
1507
- </span><span class="marked0"><a name="line886" /> 886 return Addressable::URI.new(
1508
- </span><span class="inferred1"><a name="line887" /> 887 segments[:scheme],
1509
- </span><span class="inferred0"><a name="line888" /> 888 segments[:user],
1510
- </span><span class="inferred1"><a name="line889" /> 889 segments[:password],
1511
- </span><span class="inferred0"><a name="line890" /> 890 segments[:host],
1512
- </span><span class="inferred1"><a name="line891" /> 891 segments[:port],
1513
- </span><span class="inferred0"><a name="line892" /> 892 segments[:path],
1514
- </span><span class="inferred1"><a name="line893" /> 893 segments[:query],
1515
- </span><span class="inferred0"><a name="line894" /> 894 segments[:fragment]
1516
- </span><span class="marked1"><a name="line895" /> 895 )
1517
- </span><span class="inferred0"><a name="line896" /> 896 end
1518
- </span><span class="inferred1"><a name="line897" /> 897
1519
- </span><span class="inferred0"><a name="line898" /> 898 # Returns the shortest normalized relative form of the supplied URI that
1520
- </span><span class="inferred1"><a name="line899" /> 899 # uses this URI as a base for resolution. Returns an absolute URI if
1521
- </span><span class="inferred0"><a name="line900" /> 900 # necessary.
1522
- </span><span class="marked1"><a name="line901" /> 901 def route_to(uri)
1523
- </span><span class="marked0"><a name="line902" /> 902 uri = uri.kind_of?(self.class) ? uri : self.class.parse(uri.to_s)
1524
- </span><span class="marked1"><a name="line903" /> 903 return uri.route_from(self)
1525
- </span><span class="inferred0"><a name="line904" /> 904 end
1526
- </span><span class="inferred1"><a name="line905" /> 905
1527
- </span><span class="inferred0"><a name="line906" /> 906 # Returns a normalized URI object.
1528
- </span><span class="inferred1"><a name="line907" /> 907 #
1529
- </span><span class="inferred0"><a name="line908" /> 908 # NOTE: This method does not attempt to fully conform to specifications.
1530
- </span><span class="inferred1"><a name="line909" /> 909 # It exists largely to correct other people's failures to read the
1531
- </span><span class="inferred0"><a name="line910" /> 910 # specifications, and also to deal with caching issues since several
1532
- </span><span class="inferred1"><a name="line911" /> 911 # different URIs may represent the same resource and should not be
1533
- </span><span class="inferred0"><a name="line912" /> 912 # cached multiple times.
1534
- </span><span class="marked1"><a name="line913" /> 913 def normalize
1535
- </span><span class="marked0"><a name="line914" /> 914 normalized_scheme = nil
1536
- </span><span class="marked1"><a name="line915" /> 915 normalized_scheme = self.scheme.strip.downcase if self.scheme != nil
1537
- </span><span class="marked0"><a name="line916" /> 916 normalized_scheme = &quot;svn+ssh&quot; if normalized_scheme == &quot;ssh+svn&quot;
1538
- </span><span class="marked1"><a name="line917" /> 917 if normalized_scheme == &quot;feed&quot;
1539
- </span><span class="marked0"><a name="line918" /> 918 if self.to_s =~ /^feed:\/*http:\/*/
1540
- </span><span class="marked1"><a name="line919" /> 919 return self.class.parse(
1541
- </span><span class="marked0"><a name="line920" /> 920 self.to_s.scan(/^feed:\/*(http:\/*.*)/).flatten[0]).normalize
1542
- </span><span class="inferred1"><a name="line921" /> 921 end
1543
- </span><span class="inferred0"><a name="line922" /> 922 end
1544
- </span><span class="marked1"><a name="line923" /> 923 normalized_user = nil
1545
- </span><span class="marked0"><a name="line924" /> 924 normalized_user = self.user.strip if self.user != nil
1546
- </span><span class="marked1"><a name="line925" /> 925 normalized_password = nil
1547
- </span><span class="marked0"><a name="line926" /> 926 normalized_password = self.password.strip if self.password != nil
1548
- </span><span class="inferred1"><a name="line927" /> 927
1549
- </span><span class="marked0"><a name="line928" /> 928 if normalized_scheme =~ /https?/ &amp;&amp; normalized_user == &quot;&quot; &amp;&amp;
1550
- </span><span class="marked1"><a name="line929" /> 929 (normalized_password == nil || normalized_password == &quot;&quot;)
1551
- </span><span class="marked0"><a name="line930" /> 930 normalized_user = nil
1552
- </span><span class="marked1"><a name="line931" /> 931 normalized_password = nil
1553
- </span><span class="inferred0"><a name="line932" /> 932 end
1554
- </span><span class="inferred1"><a name="line933" /> 933
1555
- </span><span class="marked0"><a name="line934" /> 934 normalized_host = nil
1556
- </span><span class="marked1"><a name="line935" /> 935 normalized_host = self.host.strip.downcase if self.host != nil
1557
- </span><span class="marked0"><a name="line936" /> 936 if normalized_host != nil
1558
- </span><span class="marked1"><a name="line937" /> 937 begin
1559
- </span><span class="marked0"><a name="line938" /> 938 normalized_host = URI::IDNA.to_ascii(normalized_host)
1560
- </span><span class="inferred1"><a name="line939" /> 939 rescue Exception
1561
- </span><span class="marked0"><a name="line940" /> 940 nil
1562
- </span><span class="inferred1"><a name="line941" /> 941 end
1563
- </span><span class="marked0"><a name="line942" /> 942 if normalized_host[-1..-1] == &quot;.&quot;
1564
- </span><span class="marked1"><a name="line943" /> 943 normalized_host = normalized_host[0...-1]
1565
- </span><span class="inferred0"><a name="line944" /> 944 end
1566
- </span><span class="inferred1"><a name="line945" /> 945 end
1567
- </span><span class="inferred0"><a name="line946" /> 946
1568
- </span><span class="marked1"><a name="line947" /> 947 normalized_port = self.port
1569
- </span><span class="marked0"><a name="line948" /> 948 if self.class.scheme_mapping[normalized_scheme] == normalized_port
1570
- </span><span class="marked1"><a name="line949" /> 949 normalized_port = nil
1571
- </span><span class="inferred0"><a name="line950" /> 950 end
1572
- </span><span class="marked1"><a name="line951" /> 951 normalized_path = nil
1573
- </span><span class="marked0"><a name="line952" /> 952 normalized_path = self.path.strip if self.path != nil
1574
- </span><span class="inferred1"><a name="line953" /> 953 if normalized_path == nil &amp;&amp;
1575
- </span><span class="marked0"><a name="line954" /> 954 normalized_scheme != nil &amp;&amp;
1576
- </span><span class="inferred1"><a name="line955" /> 955 normalized_host != nil
1577
- </span><span class="marked0"><a name="line956" /> 956 normalized_path = &quot;/&quot;
1578
- </span><span class="inferred1"><a name="line957" /> 957 end
1579
- </span><span class="marked0"><a name="line958" /> 958 if normalized_path != nil
1580
- </span><span class="marked1"><a name="line959" /> 959 normalized_path = self.class.normalize_path(normalized_path)
1581
- </span><span class="inferred0"><a name="line960" /> 960 end
1582
- </span><span class="marked1"><a name="line961" /> 961 if normalized_path == &quot;&quot;
1583
- </span><span class="marked0"><a name="line962" /> 962 if [&quot;http&quot;, &quot;https&quot;, &quot;ftp&quot;, &quot;tftp&quot;].include?(normalized_scheme)
1584
- </span><span class="marked1"><a name="line963" /> 963 normalized_path = &quot;/&quot;
1585
- </span><span class="inferred0"><a name="line964" /> 964 end
1586
- </span><span class="inferred1"><a name="line965" /> 965 end
1587
- </span><span class="inferred0"><a name="line966" /> 966
1588
- </span><span class="marked1"><a name="line967" /> 967 normalized_query = nil
1589
- </span><span class="marked0"><a name="line968" /> 968 normalized_query = self.query.strip if self.query != nil
1590
- </span><span class="inferred1"><a name="line969" /> 969
1591
- </span><span class="marked0"><a name="line970" /> 970 normalized_fragment = nil
1592
- </span><span class="marked1"><a name="line971" /> 971 normalized_fragment = self.fragment.strip if self.fragment != nil
1593
- </span><span class="marked0"><a name="line972" /> 972 return Addressable::URI.parse(
1594
- </span><span class="marked1"><a name="line973" /> 973 Addressable::URI.normalized_encode(Addressable::URI.new(
1595
- </span><span class="inferred0"><a name="line974" /> 974 normalized_scheme,
1596
- </span><span class="inferred1"><a name="line975" /> 975 normalized_user,
1597
- </span><span class="inferred0"><a name="line976" /> 976 normalized_password,
1598
- </span><span class="inferred1"><a name="line977" /> 977 normalized_host,
1599
- </span><span class="inferred0"><a name="line978" /> 978 normalized_port,
1600
- </span><span class="inferred1"><a name="line979" /> 979 normalized_path,
1601
- </span><span class="inferred0"><a name="line980" /> 980 normalized_query,
1602
- </span><span class="inferred1"><a name="line981" /> 981 normalized_fragment
1603
- </span><span class="marked0"><a name="line982" /> 982 )))
1604
- </span><span class="inferred1"><a name="line983" /> 983 end
1605
- </span><span class="inferred0"><a name="line984" /> 984
1606
- </span><span class="inferred1"><a name="line985" /> 985 # Destructively normalizes this URI object.
1607
- </span><span class="marked0"><a name="line986" /> 986 def normalize!
1608
- </span><span class="marked1"><a name="line987" /> 987 replace_self(self.normalize)
1609
- </span><span class="marked0"><a name="line988" /> 988 end
1610
- </span><span class="inferred1"><a name="line989" /> 989
1611
- </span><span class="inferred0"><a name="line990" /> 990 # Creates a URI suitable for display to users. If semantic attacks are
1612
- </span><span class="inferred1"><a name="line991" /> 991 # likely, the application should try to detect these and warn the user.
1613
- </span><span class="inferred0"><a name="line992" /> 992 # See RFC 3986 section 7.6 for more information.
1614
- </span><span class="marked1"><a name="line993" /> 993 def display_uri
1615
- </span><span class="marked0"><a name="line994" /> 994 display_uri = self.normalize
1616
- </span><span class="marked1"><a name="line995" /> 995 begin
1617
- </span><span class="marked0"><a name="line996" /> 996 display_uri.instance_variable_set(&quot;@host&quot;,
1618
- </span><span class="marked1"><a name="line997" /> 997 URI::IDNA.to_unicode(display_uri.host))
1619
- </span><span class="inferred0"><a name="line998" /> 998 rescue Exception
1620
- </span><span class="marked1"><a name="line999" /> 999 nil
1621
- </span><span class="inferred0"><a name="line1000" />1000 end
1622
- </span><span class="marked1"><a name="line1001" />1001 return display_uri
1623
- </span><span class="inferred0"><a name="line1002" />1002 end
1624
- </span><span class="inferred1"><a name="line1003" />1003
1625
- </span><span class="inferred0"><a name="line1004" />1004 # Returns true if the URI objects are equal. This method normalizes
1626
- </span><span class="inferred1"><a name="line1005" />1005 # both URIs before doing the comparison, and allows comparison against
1627
- </span><span class="inferred0"><a name="line1006" />1006 # strings.
1628
- </span><span class="marked1"><a name="line1007" />1007 def ===(uri)
1629
- </span><span class="marked0"><a name="line1008" />1008 uri_string = nil
1630
- </span><span class="marked1"><a name="line1009" />1009 if uri.respond_to?(:normalize)
1631
- </span><span class="marked0"><a name="line1010" />1010 uri_string = uri.normalize.to_s
1632
- </span><span class="inferred1"><a name="line1011" />1011 else
1633
- </span><span class="marked0"><a name="line1012" />1012 begin
1634
- </span><span class="marked1"><a name="line1013" />1013 uri_string = URI.parse(uri.to_s).normalize.to_s
1635
- </span><span class="inferred0"><a name="line1014" />1014 rescue Exception
1636
- </span><span class="marked1"><a name="line1015" />1015 return false
1637
- </span><span class="inferred0"><a name="line1016" />1016 end
1638
- </span><span class="inferred1"><a name="line1017" />1017 end
1639
- </span><span class="marked0"><a name="line1018" />1018 return self.normalize.to_s == uri_string
1640
- </span><span class="inferred1"><a name="line1019" />1019 end
1641
- </span><span class="inferred0"><a name="line1020" />1020
1642
- </span><span class="inferred1"><a name="line1021" />1021 # Returns true if the URI objects are equal. This method normalizes
1643
- </span><span class="inferred0"><a name="line1022" />1022 # both URIs before doing the comparison.
1644
- </span><span class="marked1"><a name="line1023" />1023 def ==(uri)
1645
- </span><span class="marked0"><a name="line1024" />1024 return false unless uri.kind_of?(self.class)
1646
- </span><span class="marked1"><a name="line1025" />1025 return self.normalize.to_s == uri.normalize.to_s
1647
- </span><span class="inferred0"><a name="line1026" />1026 end
1648
- </span><span class="inferred1"><a name="line1027" />1027
1649
- </span><span class="inferred0"><a name="line1028" />1028 # Returns true if the URI objects are equal. This method does NOT
1650
- </span><span class="inferred1"><a name="line1029" />1029 # normalize either URI before doing the comparison.
1651
- </span><span class="marked0"><a name="line1030" />1030 def eql?(uri)
1652
- </span><span class="marked1"><a name="line1031" />1031 return false unless uri.kind_of?(self.class)
1653
- </span><span class="marked0"><a name="line1032" />1032 return self.to_s == uri.to_s
1654
- </span><span class="inferred1"><a name="line1033" />1033 end
1655
- </span><span class="inferred0"><a name="line1034" />1034
1656
- </span><span class="inferred1"><a name="line1035" />1035 # Clones the URI object.
1657
- </span><span class="marked0"><a name="line1036" />1036 def dup
1658
- </span><span class="marked1"><a name="line1037" />1037 duplicated_scheme = nil
1659
- </span><span class="marked0"><a name="line1038" />1038 duplicated_scheme = self.scheme.dup if self.scheme != nil
1660
- </span><span class="marked1"><a name="line1039" />1039 duplicated_user = nil
1661
- </span><span class="marked0"><a name="line1040" />1040 duplicated_user = self.user.dup if self.user != nil
1662
- </span><span class="marked1"><a name="line1041" />1041 duplicated_password = nil
1663
- </span><span class="marked0"><a name="line1042" />1042 duplicated_password = self.password.dup if self.password != nil
1664
- </span><span class="marked1"><a name="line1043" />1043 duplicated_host = nil
1665
- </span><span class="marked0"><a name="line1044" />1044 duplicated_host = self.host.dup if self.host != nil
1666
- </span><span class="marked1"><a name="line1045" />1045 duplicated_port = self.port
1667
- </span><span class="marked0"><a name="line1046" />1046 duplicated_path = nil
1668
- </span><span class="marked1"><a name="line1047" />1047 duplicated_path = self.path.dup if self.path != nil
1669
- </span><span class="marked0"><a name="line1048" />1048 duplicated_query = nil
1670
- </span><span class="marked1"><a name="line1049" />1049 duplicated_query = self.query.dup if self.query != nil
1671
- </span><span class="marked0"><a name="line1050" />1050 duplicated_fragment = nil
1672
- </span><span class="marked1"><a name="line1051" />1051 duplicated_fragment = self.fragment.dup if self.fragment != nil
1673
- </span><span class="marked0"><a name="line1052" />1052 duplicated_uri = Addressable::URI.new(
1674
- </span><span class="inferred1"><a name="line1053" />1053 duplicated_scheme,
1675
- </span><span class="inferred0"><a name="line1054" />1054 duplicated_user,
1676
- </span><span class="inferred1"><a name="line1055" />1055 duplicated_password,
1677
- </span><span class="inferred0"><a name="line1056" />1056 duplicated_host,
1678
- </span><span class="inferred1"><a name="line1057" />1057 duplicated_port,
1679
- </span><span class="inferred0"><a name="line1058" />1058 duplicated_path,
1680
- </span><span class="inferred1"><a name="line1059" />1059 duplicated_query,
1681
- </span><span class="inferred0"><a name="line1060" />1060 duplicated_fragment
1682
- </span><span class="inferred1"><a name="line1061" />1061 )
1683
- </span><span class="marked0"><a name="line1062" />1062 @specified_port = nil if !defined?(@specified_port)
1684
- </span><span class="marked1"><a name="line1063" />1063 duplicated_uri.instance_variable_set(&quot;@specified_port&quot;, @specified_port)
1685
- </span><span class="marked0"><a name="line1064" />1064 return duplicated_uri
1686
- </span><span class="inferred1"><a name="line1065" />1065 end
1687
- </span><span class="inferred0"><a name="line1066" />1066
1688
- </span><span class="inferred1"><a name="line1067" />1067 # Returns the assembled URI as a string.
1689
- </span><span class="marked0"><a name="line1068" />1068 def to_s
1690
- </span><span class="marked1"><a name="line1069" />1069 uri_string = &quot;&quot;
1691
- </span><span class="marked0"><a name="line1070" />1070 uri_string &lt;&lt; &quot;#{self.scheme}:&quot; if self.scheme != nil
1692
- </span><span class="marked1"><a name="line1071" />1071 uri_string &lt;&lt; &quot;//#{self.authority}&quot; if self.authority != nil
1693
- </span><span class="marked0"><a name="line1072" />1072 uri_string &lt;&lt; self.path.to_s
1694
- </span><span class="marked1"><a name="line1073" />1073 uri_string &lt;&lt; &quot;?#{self.query}&quot; if self.query != nil
1695
- </span><span class="marked0"><a name="line1074" />1074 uri_string &lt;&lt; &quot;##{self.fragment}&quot; if self.fragment != nil
1696
- </span><span class="marked1"><a name="line1075" />1075 return uri_string
1507
+ </span><span class="marked0"><a name="line886" /> 886 segments = normalized_self.to_hash
1508
+ </span><span class="marked1"><a name="line887" /> 887 if normalized_self.scheme == uri.scheme
1509
+ </span><span class="marked0"><a name="line888" /> 888 segments[:scheme] = nil
1510
+ </span><span class="marked1"><a name="line889" /> 889 if normalized_self.authority == uri.authority
1511
+ </span><span class="marked0"><a name="line890" /> 890 segments[:user] = nil
1512
+ </span><span class="marked1"><a name="line891" /> 891 segments[:password] = nil
1513
+ </span><span class="marked0"><a name="line892" /> 892 segments[:host] = nil
1514
+ </span><span class="marked1"><a name="line893" /> 893 segments[:port] = nil
1515
+ </span><span class="marked0"><a name="line894" /> 894 if normalized_self.path == uri.path
1516
+ </span><span class="marked1"><a name="line895" /> 895 segments[:path] = nil
1517
+ </span><span class="marked0"><a name="line896" /> 896 if normalized_self.query == uri.query
1518
+ </span><span class="marked1"><a name="line897" /> 897 segments[:query] = nil
1519
+ </span><span class="inferred0"><a name="line898" /> 898 end
1520
+ </span><span class="inferred1"><a name="line899" /> 899 else
1521
+ </span><span class="marked0"><a name="line900" /> 900 if uri.path != &quot;/&quot;
1522
+ </span><span class="marked1"><a name="line901" /> 901 segments[:path].gsub!(
1523
+ </span><span class="marked0"><a name="line902" /> 902 Regexp.new(&quot;^&quot; + Regexp.escape(uri.path)), &quot;&quot;)
1524
+ </span><span class="inferred1"><a name="line903" /> 903 end
1525
+ </span><span class="inferred0"><a name="line904" /> 904 end
1526
+ </span><span class="inferred1"><a name="line905" /> 905 end
1527
+ </span><span class="inferred0"><a name="line906" /> 906 end
1528
+ </span><span class="inferred1"><a name="line907" /> 907 # Avoid network-path references.
1529
+ </span><span class="marked0"><a name="line908" /> 908 if segments[:host] != nil
1530
+ </span><span class="marked1"><a name="line909" /> 909 segments[:scheme] = normalized_self.scheme
1531
+ </span><span class="inferred0"><a name="line910" /> 910 end
1532
+ </span><span class="marked1"><a name="line911" /> 911 return Addressable::URI.new(
1533
+ </span><span class="inferred0"><a name="line912" /> 912 segments[:scheme],
1534
+ </span><span class="inferred1"><a name="line913" /> 913 segments[:user],
1535
+ </span><span class="inferred0"><a name="line914" /> 914 segments[:password],
1536
+ </span><span class="inferred1"><a name="line915" /> 915 segments[:host],
1537
+ </span><span class="inferred0"><a name="line916" /> 916 segments[:port],
1538
+ </span><span class="inferred1"><a name="line917" /> 917 segments[:path],
1539
+ </span><span class="inferred0"><a name="line918" /> 918 segments[:query],
1540
+ </span><span class="inferred1"><a name="line919" /> 919 segments[:fragment]
1541
+ </span><span class="marked0"><a name="line920" /> 920 )
1542
+ </span><span class="inferred1"><a name="line921" /> 921 end
1543
+ </span><span class="inferred0"><a name="line922" /> 922
1544
+ </span><span class="inferred1"><a name="line923" /> 923 # Returns the shortest normalized relative form of the supplied URI that
1545
+ </span><span class="inferred0"><a name="line924" /> 924 # uses this URI as a base for resolution. Returns an absolute URI if
1546
+ </span><span class="inferred1"><a name="line925" /> 925 # necessary.
1547
+ </span><span class="marked0"><a name="line926" /> 926 def route_to(uri)
1548
+ </span><span class="marked1"><a name="line927" /> 927 return self.class.parse(uri).route_from(self)
1549
+ </span><span class="marked0"><a name="line928" /> 928 end
1550
+ </span><span class="inferred1"><a name="line929" /> 929
1551
+ </span><span class="inferred0"><a name="line930" /> 930 # Returns a normalized URI object.
1552
+ </span><span class="inferred1"><a name="line931" /> 931 #
1553
+ </span><span class="inferred0"><a name="line932" /> 932 # NOTE: This method does not attempt to fully conform to specifications.
1554
+ </span><span class="inferred1"><a name="line933" /> 933 # It exists largely to correct other people's failures to read the
1555
+ </span><span class="inferred0"><a name="line934" /> 934 # specifications, and also to deal with caching issues since several
1556
+ </span><span class="inferred1"><a name="line935" /> 935 # different URIs may represent the same resource and should not be
1557
+ </span><span class="inferred0"><a name="line936" /> 936 # cached multiple times.
1558
+ </span><span class="marked1"><a name="line937" /> 937 def normalize
1559
+ </span><span class="marked0"><a name="line938" /> 938 normalized_scheme = nil
1560
+ </span><span class="marked1"><a name="line939" /> 939 normalized_scheme = self.scheme.strip.downcase if self.scheme != nil
1561
+ </span><span class="marked0"><a name="line940" /> 940 normalized_scheme = &quot;svn+ssh&quot; if normalized_scheme == &quot;ssh+svn&quot;
1562
+ </span><span class="marked1"><a name="line941" /> 941 if normalized_scheme == &quot;feed&quot;
1563
+ </span><span class="marked0"><a name="line942" /> 942 if self.to_s =~ /^feed:\/*http:\/*/
1564
+ </span><span class="marked1"><a name="line943" /> 943 return self.class.parse(
1565
+ </span><span class="marked0"><a name="line944" /> 944 self.to_s.scan(/^feed:\/*(http:\/*.*)/).flatten[0]).normalize
1566
+ </span><span class="inferred1"><a name="line945" /> 945 end
1567
+ </span><span class="inferred0"><a name="line946" /> 946 end
1568
+ </span><span class="marked1"><a name="line947" /> 947 normalized_user = nil
1569
+ </span><span class="marked0"><a name="line948" /> 948 normalized_user = self.user.strip if self.user != nil
1570
+ </span><span class="marked1"><a name="line949" /> 949 normalized_password = nil
1571
+ </span><span class="marked0"><a name="line950" /> 950 normalized_password = self.password.strip if self.password != nil
1572
+ </span><span class="inferred1"><a name="line951" /> 951
1573
+ </span><span class="inferred0"><a name="line952" /> 952 # If we are using http or https and user/password are blank,
1574
+ </span><span class="inferred1"><a name="line953" /> 953 # then we remove them
1575
+ </span><span class="marked0"><a name="line954" /> 954 if normalized_scheme =~ /https?/ &amp;&amp; normalized_user == &quot;&quot; &amp;&amp;
1576
+ </span><span class="marked1"><a name="line955" /> 955 (!normalized_password || normalized_password == &quot;&quot;)
1577
+ </span><span class="marked0"><a name="line956" /> 956 normalized_user = nil
1578
+ </span><span class="marked1"><a name="line957" /> 957 normalized_password = nil
1579
+ </span><span class="inferred0"><a name="line958" /> 958 end
1580
+ </span><span class="inferred1"><a name="line959" /> 959
1581
+ </span><span class="marked0"><a name="line960" /> 960 normalized_host = nil
1582
+ </span><span class="marked1"><a name="line961" /> 961 normalized_host = self.host.strip.downcase if self.host != nil
1583
+ </span><span class="marked0"><a name="line962" /> 962 if normalized_host != nil
1584
+ </span><span class="marked1"><a name="line963" /> 963 begin
1585
+ </span><span class="marked0"><a name="line964" /> 964 normalized_host = URI::IDNA.to_ascii(normalized_host)
1586
+ </span><span class="inferred1"><a name="line965" /> 965 rescue Exception
1587
+ </span><span class="marked0"><a name="line966" /> 966 nil
1588
+ </span><span class="inferred1"><a name="line967" /> 967 end
1589
+ </span><span class="marked0"><a name="line968" /> 968 if normalized_host[-1..-1] == &quot;.&quot;
1590
+ </span><span class="marked1"><a name="line969" /> 969 normalized_host = normalized_host[0...-1]
1591
+ </span><span class="inferred0"><a name="line970" /> 970 end
1592
+ </span><span class="inferred1"><a name="line971" /> 971 end
1593
+ </span><span class="inferred0"><a name="line972" /> 972
1594
+ </span><span class="marked1"><a name="line973" /> 973 normalized_port = self.port
1595
+ </span><span class="marked0"><a name="line974" /> 974 if self.class.scheme_mapping[normalized_scheme] == normalized_port
1596
+ </span><span class="marked1"><a name="line975" /> 975 normalized_port = nil
1597
+ </span><span class="inferred0"><a name="line976" /> 976 end
1598
+ </span><span class="marked1"><a name="line977" /> 977 normalized_path = nil
1599
+ </span><span class="marked0"><a name="line978" /> 978 normalized_path = self.path.strip if self.path != nil
1600
+ </span><span class="marked1"><a name="line979" /> 979 if normalized_path != nil
1601
+ </span><span class="marked0"><a name="line980" /> 980 normalized_path = self.class.normalize_path(normalized_path)
1602
+ </span><span class="inferred1"><a name="line981" /> 981 end
1603
+ </span><span class="marked0"><a name="line982" /> 982 if normalized_path == &quot;&quot;
1604
+ </span><span class="marked1"><a name="line983" /> 983 if [&quot;http&quot;, &quot;https&quot;, &quot;ftp&quot;, &quot;tftp&quot;].include?(normalized_scheme)
1605
+ </span><span class="marked0"><a name="line984" /> 984 normalized_path = &quot;/&quot;
1606
+ </span><span class="inferred1"><a name="line985" /> 985 end
1607
+ </span><span class="inferred0"><a name="line986" /> 986 end
1608
+ </span><span class="inferred1"><a name="line987" /> 987
1609
+ </span><span class="marked0"><a name="line988" /> 988 normalized_query = nil
1610
+ </span><span class="marked1"><a name="line989" /> 989 normalized_query = self.query.strip if self.query != nil
1611
+ </span><span class="inferred0"><a name="line990" /> 990
1612
+ </span><span class="marked1"><a name="line991" /> 991 normalized_fragment = nil
1613
+ </span><span class="marked0"><a name="line992" /> 992 normalized_fragment = self.fragment.strip if self.fragment != nil
1614
+ </span><span class="marked1"><a name="line993" /> 993 return Addressable::URI.parse(
1615
+ </span><span class="marked0"><a name="line994" /> 994 Addressable::URI.normalized_encode(Addressable::URI.new(
1616
+ </span><span class="inferred1"><a name="line995" /> 995 normalized_scheme,
1617
+ </span><span class="inferred0"><a name="line996" /> 996 normalized_user,
1618
+ </span><span class="inferred1"><a name="line997" /> 997 normalized_password,
1619
+ </span><span class="inferred0"><a name="line998" /> 998 normalized_host,
1620
+ </span><span class="inferred1"><a name="line999" /> 999 normalized_port,
1621
+ </span><span class="inferred0"><a name="line1000" />1000 normalized_path,
1622
+ </span><span class="inferred1"><a name="line1001" />1001 normalized_query,
1623
+ </span><span class="inferred0"><a name="line1002" />1002 normalized_fragment
1624
+ </span><span class="marked1"><a name="line1003" />1003 )))
1625
+ </span><span class="inferred0"><a name="line1004" />1004 end
1626
+ </span><span class="inferred1"><a name="line1005" />1005
1627
+ </span><span class="inferred0"><a name="line1006" />1006 # Destructively normalizes this URI object.
1628
+ </span><span class="marked1"><a name="line1007" />1007 def normalize!
1629
+ </span><span class="marked0"><a name="line1008" />1008 replace_self(self.normalize)
1630
+ </span><span class="marked1"><a name="line1009" />1009 end
1631
+ </span><span class="inferred0"><a name="line1010" />1010
1632
+ </span><span class="inferred1"><a name="line1011" />1011 # Creates a URI suitable for display to users. If semantic attacks are
1633
+ </span><span class="inferred0"><a name="line1012" />1012 # likely, the application should try to detect these and warn the user.
1634
+ </span><span class="inferred1"><a name="line1013" />1013 # See RFC 3986 section 7.6 for more information.
1635
+ </span><span class="marked0"><a name="line1014" />1014 def display_uri
1636
+ </span><span class="marked1"><a name="line1015" />1015 display_uri = self.normalize
1637
+ </span><span class="marked0"><a name="line1016" />1016 begin
1638
+ </span><span class="marked1"><a name="line1017" />1017 display_uri.instance_variable_set(&quot;@host&quot;,
1639
+ </span><span class="marked0"><a name="line1018" />1018 URI::IDNA.to_unicode(display_uri.host))
1640
+ </span><span class="inferred1"><a name="line1019" />1019 rescue Exception
1641
+ </span><span class="marked0"><a name="line1020" />1020 nil
1642
+ </span><span class="inferred1"><a name="line1021" />1021 end
1643
+ </span><span class="marked0"><a name="line1022" />1022 return display_uri
1644
+ </span><span class="inferred1"><a name="line1023" />1023 end
1645
+ </span><span class="inferred0"><a name="line1024" />1024
1646
+ </span><span class="inferred1"><a name="line1025" />1025 # Returns true if the URI objects are equal. This method normalizes
1647
+ </span><span class="inferred0"><a name="line1026" />1026 # both URIs before doing the comparison, and allows comparison against
1648
+ </span><span class="inferred1"><a name="line1027" />1027 # strings.
1649
+ </span><span class="marked0"><a name="line1028" />1028 def ===(uri)
1650
+ </span><span class="marked1"><a name="line1029" />1029 if uri.respond_to?(:normalize)
1651
+ </span><span class="marked0"><a name="line1030" />1030 uri_string = uri.normalize.to_s
1652
+ </span><span class="inferred1"><a name="line1031" />1031 else
1653
+ </span><span class="marked0"><a name="line1032" />1032 begin
1654
+ </span><span class="marked1"><a name="line1033" />1033 uri_string = URI.parse(uri.to_s).normalize.to_s
1655
+ </span><span class="inferred0"><a name="line1034" />1034 rescue InvalidURIError
1656
+ </span><span class="marked1"><a name="line1035" />1035 return false
1657
+ </span><span class="inferred0"><a name="line1036" />1036 end
1658
+ </span><span class="inferred1"><a name="line1037" />1037 end
1659
+ </span><span class="marked0"><a name="line1038" />1038 return self.normalize.to_s == uri_string
1660
+ </span><span class="inferred1"><a name="line1039" />1039 end
1661
+ </span><span class="inferred0"><a name="line1040" />1040
1662
+ </span><span class="inferred1"><a name="line1041" />1041 # Returns true if the URI objects are equal. This method normalizes
1663
+ </span><span class="inferred0"><a name="line1042" />1042 # both URIs before doing the comparison.
1664
+ </span><span class="marked1"><a name="line1043" />1043 def ==(uri)
1665
+ </span><span class="marked0"><a name="line1044" />1044 return false unless uri.kind_of?(self.class)
1666
+ </span><span class="marked1"><a name="line1045" />1045 return self.normalize.to_s == uri.normalize.to_s
1667
+ </span><span class="inferred0"><a name="line1046" />1046 end
1668
+ </span><span class="inferred1"><a name="line1047" />1047
1669
+ </span><span class="inferred0"><a name="line1048" />1048 # Returns true if the URI objects are equal. This method does NOT
1670
+ </span><span class="inferred1"><a name="line1049" />1049 # normalize either URI before doing the comparison.
1671
+ </span><span class="marked0"><a name="line1050" />1050 def eql?(uri)
1672
+ </span><span class="marked1"><a name="line1051" />1051 return false unless uri.kind_of?(self.class)
1673
+ </span><span class="marked0"><a name="line1052" />1052 return self.to_s == uri.to_s
1674
+ </span><span class="inferred1"><a name="line1053" />1053 end
1675
+ </span><span class="inferred0"><a name="line1054" />1054
1676
+ </span><span class="inferred1"><a name="line1055" />1055 # Clones the URI object.
1677
+ </span><span class="marked0"><a name="line1056" />1056 def dup
1678
+ </span><span class="marked1"><a name="line1057" />1057 duplicated_scheme = self.scheme ? self.scheme.dup : nil
1679
+ </span><span class="marked0"><a name="line1058" />1058 duplicated_user = self.user ? self.user.dup : nil
1680
+ </span><span class="marked1"><a name="line1059" />1059 duplicated_password = self.password ? self.password.dup : nil
1681
+ </span><span class="marked0"><a name="line1060" />1060 duplicated_host = self.host ? self.host.dup : nil
1682
+ </span><span class="marked1"><a name="line1061" />1061 duplicated_port = self.specified_port
1683
+ </span><span class="marked0"><a name="line1062" />1062 duplicated_path = self.path ? self.path.dup : nil
1684
+ </span><span class="marked1"><a name="line1063" />1063 duplicated_query = self.query ? self.query.dup : nil
1685
+ </span><span class="marked0"><a name="line1064" />1064 duplicated_fragment = self.fragment ? self.fragment.dup : nil
1686
+ </span><span class="marked1"><a name="line1065" />1065 duplicated_uri = Addressable::URI.new(
1687
+ </span><span class="inferred0"><a name="line1066" />1066 duplicated_scheme,
1688
+ </span><span class="inferred1"><a name="line1067" />1067 duplicated_user,
1689
+ </span><span class="inferred0"><a name="line1068" />1068 duplicated_password,
1690
+ </span><span class="inferred1"><a name="line1069" />1069 duplicated_host,
1691
+ </span><span class="inferred0"><a name="line1070" />1070 duplicated_port,
1692
+ </span><span class="inferred1"><a name="line1071" />1071 duplicated_path,
1693
+ </span><span class="inferred0"><a name="line1072" />1072 duplicated_query,
1694
+ </span><span class="inferred1"><a name="line1073" />1073 duplicated_fragment
1695
+ </span><span class="inferred0"><a name="line1074" />1074 )
1696
+ </span><span class="marked1"><a name="line1075" />1075 return duplicated_uri
1697
1697
  </span><span class="inferred0"><a name="line1076" />1076 end
1698
1698
  </span><span class="inferred1"><a name="line1077" />1077
1699
- </span><span class="inferred0"><a name="line1078" />1078 # Returns a Hash of the URI segments.
1700
- </span><span class="marked1"><a name="line1079" />1079 def to_h
1701
- </span><span class="inferred0"><a name="line1080" />1080 return {
1702
- </span><span class="marked1"><a name="line1081" />1081 :scheme =&gt; self.scheme,
1703
- </span><span class="marked0"><a name="line1082" />1082 :user =&gt; self.user,
1704
- </span><span class="marked1"><a name="line1083" />1083 :password =&gt; self.password,
1705
- </span><span class="marked0"><a name="line1084" />1084 :host =&gt; self.host,
1706
- </span><span class="marked1"><a name="line1085" />1085 :port =&gt; self.specified_port,
1707
- </span><span class="marked0"><a name="line1086" />1086 :path =&gt; self.path,
1708
- </span><span class="marked1"><a name="line1087" />1087 :query =&gt; self.query,
1709
- </span><span class="marked0"><a name="line1088" />1088 :fragment =&gt; self.fragment
1710
- </span><span class="marked1"><a name="line1089" />1089 }
1711
- </span><span class="marked0"><a name="line1090" />1090 end
1712
- </span><span class="inferred1"><a name="line1091" />1091
1713
- </span><span class="inferred0"><a name="line1092" />1092 # Returns a string representation of the URI object's state.
1714
- </span><span class="marked1"><a name="line1093" />1093 def inspect
1715
- </span><span class="marked0"><a name="line1094" />1094 sprintf(&quot;#&lt;%s:%#0x URI:%s&gt;&quot;, self.class.to_s, self.object_id, self.to_s)
1716
- </span><span class="marked1"><a name="line1095" />1095 end
1717
- </span><span class="inferred0"><a name="line1096" />1096
1718
- </span><span class="inferred1"><a name="line1097" />1097 # This module handles internationalized domain names. When Ruby has an
1719
- </span><span class="inferred0"><a name="line1098" />1098 # implementation of nameprep, stringprep, punycode, etc, this
1720
- </span><span class="inferred1"><a name="line1099" />1099 # module should contain an actual implementation of IDNA instead of
1721
- </span><span class="inferred0"><a name="line1100" />1100 # returning nil if libidn can't be used.
1722
- </span><span class="marked1"><a name="line1101" />1101 module IDNA
1723
- </span><span class="inferred0"><a name="line1102" />1102 # Returns the ascii representation of the label.
1724
- </span><span class="marked1"><a name="line1103" />1103 def self.to_ascii(label)
1725
- </span><span class="marked0"><a name="line1104" />1104 return nil if label.nil?
1726
- </span><span class="marked1"><a name="line1105" />1105 if self.use_libidn?
1727
- </span><span class="marked0"><a name="line1106" />1106 return IDN::Idna.toASCII(label)
1728
- </span><span class="inferred1"><a name="line1107" />1107 else
1729
- </span><span class="marked0"><a name="line1108" />1108 raise NotImplementedError,
1730
- </span><span class="inferred1"><a name="line1109" />1109 &quot;There is no available pure-ruby implementation. &quot; +
1731
- </span><span class="inferred0"><a name="line1110" />1110 &quot;Install libidn bindings.&quot;
1732
- </span><span class="inferred1"><a name="line1111" />1111 end
1733
- </span><span class="inferred0"><a name="line1112" />1112 end
1734
- </span><span class="inferred1"><a name="line1113" />1113
1735
- </span><span class="inferred0"><a name="line1114" />1114 # Returns the unicode representation of the label.
1736
- </span><span class="marked1"><a name="line1115" />1115 def self.to_unicode(label)
1737
- </span><span class="marked0"><a name="line1116" />1116 return nil if label.nil?
1738
- </span><span class="marked1"><a name="line1117" />1117 if self.use_libidn?
1739
- </span><span class="marked0"><a name="line1118" />1118 return IDN::Idna.toUnicode(label)
1740
- </span><span class="inferred1"><a name="line1119" />1119 else
1741
- </span><span class="marked0"><a name="line1120" />1120 raise NotImplementedError,
1742
- </span><span class="inferred1"><a name="line1121" />1121 &quot;There is no available pure-ruby implementation. &quot; +
1743
- </span><span class="inferred0"><a name="line1122" />1122 &quot;Install libidn bindings.&quot;
1744
- </span><span class="inferred1"><a name="line1123" />1123 end
1745
- </span><span class="inferred0"><a name="line1124" />1124 end
1746
- </span><span class="inferred1"><a name="line1125" />1125
1747
- </span><span class="marked0"><a name="line1126" />1126 private
1748
- </span><span class="inferred1"><a name="line1127" />1127 # Determines if the libidn bindings are available and able to be used.
1749
- </span><span class="marked0"><a name="line1128" />1128 def self.use_libidn?
1750
- </span><span class="marked1"><a name="line1129" />1129 if !defined?(@use_libidn) || @use_libidn.nil?
1751
- </span><span class="marked0"><a name="line1130" />1130 begin
1752
- </span><span class="marked1"><a name="line1131" />1131 require 'rubygems'
1753
- </span><span class="inferred0"><a name="line1132" />1132 rescue LoadError
1754
- </span><span class="marked1"><a name="line1133" />1133 nil
1755
- </span><span class="inferred0"><a name="line1134" />1134 end
1756
- </span><span class="marked1"><a name="line1135" />1135 begin
1757
- </span><span class="marked0"><a name="line1136" />1136 require 'idn'
1758
- </span><span class="inferred1"><a name="line1137" />1137 rescue LoadError
1759
- </span><span class="marked0"><a name="line1138" />1138 nil
1760
- </span><span class="inferred1"><a name="line1139" />1139 end
1761
- </span><span class="marked0"><a name="line1140" />1140 @use_libidn = !!(defined?(IDN::Idna))
1762
- </span><span class="inferred1"><a name="line1141" />1141 end
1763
- </span><span class="marked0"><a name="line1142" />1142 return @use_libidn
1764
- </span><span class="inferred1"><a name="line1143" />1143 end
1765
- </span><span class="inferred0"><a name="line1144" />1144 end
1766
- </span><span class="inferred1"><a name="line1145" />1145
1767
- </span><span class="marked0"><a name="line1146" />1146 private
1768
- </span><span class="inferred1"><a name="line1147" />1147 # Resolves paths to their simplest form.
1769
- </span><span class="marked0"><a name="line1148" />1148 def self.normalize_path(path)
1770
- </span><span class="inferred1"><a name="line1149" />1149 # Section 5.2.4 of RFC 3986
1771
- </span><span class="inferred0"><a name="line1150" />1150
1772
- </span><span class="marked1"><a name="line1151" />1151 return nil if path.nil?
1773
- </span><span class="marked0"><a name="line1152" />1152 normalized_path = path.dup
1774
- </span><span class="marked1"><a name="line1153" />1153 previous_state = normalized_path.dup
1775
- </span><span class="inferred0"><a name="line1154" />1154 begin
1776
- </span><span class="marked1"><a name="line1155" />1155 previous_state = normalized_path.dup
1777
- </span><span class="marked0"><a name="line1156" />1156 normalized_path.gsub!(/\/\.\//, &quot;/&quot;)
1778
- </span><span class="marked1"><a name="line1157" />1157 normalized_path.gsub!(/\/\.$/, &quot;/&quot;)
1779
- </span><span class="marked0"><a name="line1158" />1158 parent = normalized_path.scan(/\/([^\/]+)\/\.\.\//).flatten[0]
1780
- </span><span class="marked1"><a name="line1159" />1159 if parent != &quot;.&quot; &amp;&amp; parent != &quot;..&quot;
1781
- </span><span class="marked0"><a name="line1160" />1160 normalized_path.gsub!(/\/#{parent}\/\.\.\//, &quot;/&quot;)
1782
- </span><span class="inferred1"><a name="line1161" />1161 end
1783
- </span><span class="marked0"><a name="line1162" />1162 parent = normalized_path.scan(/\/([^\/]+)\/\.\.$/).flatten[0]
1784
- </span><span class="marked1"><a name="line1163" />1163 if parent != &quot;.&quot; &amp;&amp; parent != &quot;..&quot;
1785
- </span><span class="marked0"><a name="line1164" />1164 normalized_path.gsub!(/\/#{parent}\/\.\.$/, &quot;/&quot;)
1786
- </span><span class="inferred1"><a name="line1165" />1165 end
1787
- </span><span class="marked0"><a name="line1166" />1166 normalized_path.gsub!(/^\.\.?\/?/, &quot;&quot;)
1788
- </span><span class="marked1"><a name="line1167" />1167 normalized_path.gsub!(/^\/\.\.?\//, &quot;/&quot;)
1789
- </span><span class="marked0"><a name="line1168" />1168 end until previous_state == normalized_path
1790
- </span><span class="marked1"><a name="line1169" />1169 return normalized_path
1791
- </span><span class="inferred0"><a name="line1170" />1170 end
1792
- </span><span class="inferred1"><a name="line1171" />1171
1793
- </span><span class="inferred0"><a name="line1172" />1172 # Ensures that the URI is valid.
1794
- </span><span class="marked1"><a name="line1173" />1173 def validate
1795
- </span><span class="marked0"><a name="line1174" />1174 if self.scheme == nil &amp;&amp; self.user == nil &amp;&amp; self.password == nil &amp;&amp;
1796
- </span><span class="marked1"><a name="line1175" />1175 self.host == nil &amp;&amp; self.port == nil &amp;&amp; self.path == nil &amp;&amp;
1797
- </span><span class="marked0"><a name="line1176" />1176 self.query == nil &amp;&amp; self.fragment == nil
1798
- </span><span class="marked1"><a name="line1177" />1177 raise InvalidURIError, &quot;All segments were nil.&quot;
1799
- </span><span class="inferred0"><a name="line1178" />1178 end
1800
- </span><span class="marked1"><a name="line1179" />1179 if self.scheme != nil &amp;&amp;
1801
- </span><span class="marked0"><a name="line1180" />1180 (self.host == nil || self.host == &quot;&quot;) &amp;&amp;
1802
- </span><span class="marked1"><a name="line1181" />1181 (self.path == nil || self.path == &quot;&quot;)
1803
- </span><span class="marked0"><a name="line1182" />1182 raise InvalidURIError,
1804
- </span><span class="inferred1"><a name="line1183" />1183 &quot;Absolute URI missing hierarchical segment.&quot;
1805
- </span><span class="inferred0"><a name="line1184" />1184 end
1806
- </span><span class="inferred1"><a name="line1185" />1185 end
1807
- </span><span class="inferred0"><a name="line1186" />1186
1808
- </span><span class="inferred1"><a name="line1187" />1187 # Replaces the internal state of self with the specified URI's state.
1809
- </span><span class="inferred0"><a name="line1188" />1188 # Used in destructive operations to avoid massive code repetition.
1810
- </span><span class="marked1"><a name="line1189" />1189 def replace_self(uri)
1811
- </span><span class="inferred0"><a name="line1190" />1190 # Reset dependant values
1812
- </span><span class="marked1"><a name="line1191" />1191 @userinfo = nil
1813
- </span><span class="marked0"><a name="line1192" />1192 @authority = nil
1814
- </span><span class="inferred1"><a name="line1193" />1193
1815
- </span><span class="marked0"><a name="line1194" />1194 @scheme = uri.scheme
1816
- </span><span class="marked1"><a name="line1195" />1195 @user = uri.user
1817
- </span><span class="marked0"><a name="line1196" />1196 @password = uri.password
1818
- </span><span class="marked1"><a name="line1197" />1197 @host = uri.host
1819
- </span><span class="marked0"><a name="line1198" />1198 @specified_port = uri.instance_variable_get(&quot;@specified_port&quot;)
1820
- </span><span class="marked1"><a name="line1199" />1199 @port = @specified_port.to_s.to_i
1821
- </span><span class="marked0"><a name="line1200" />1200 @path = uri.path
1822
- </span><span class="marked1"><a name="line1201" />1201 @query = uri.query
1823
- </span><span class="marked0"><a name="line1202" />1202 @fragment = uri.fragment
1824
- </span><span class="marked1"><a name="line1203" />1203 return self
1825
- </span><span class="inferred0"><a name="line1204" />1204 end
1826
- </span><span class="inferred1"><a name="line1205" />1205 end
1827
- </span><span class="inferred0"><a name="line1206" />1206 end
1699
+ </span><span class="inferred0"><a name="line1078" />1078 # Returns the assembled URI as a string.
1700
+ </span><span class="marked1"><a name="line1079" />1079 def to_s
1701
+ </span><span class="marked0"><a name="line1080" />1080 uri_string = &quot;&quot;
1702
+ </span><span class="marked1"><a name="line1081" />1081 uri_string &lt;&lt; &quot;#{self.scheme}:&quot; if self.scheme != nil
1703
+ </span><span class="marked0"><a name="line1082" />1082 uri_string &lt;&lt; &quot;//#{self.authority}&quot; if self.authority != nil
1704
+ </span><span class="marked1"><a name="line1083" />1083 uri_string &lt;&lt; self.path.to_s
1705
+ </span><span class="marked0"><a name="line1084" />1084 uri_string &lt;&lt; &quot;?#{self.query}&quot; if self.query != nil
1706
+ </span><span class="marked1"><a name="line1085" />1085 uri_string &lt;&lt; &quot;##{self.fragment}&quot; if self.fragment != nil
1707
+ </span><span class="marked0"><a name="line1086" />1086 return uri_string
1708
+ </span><span class="inferred1"><a name="line1087" />1087 end
1709
+ </span><span class="inferred0"><a name="line1088" />1088
1710
+ </span><span class="inferred1"><a name="line1089" />1089 # Returns a Hash of the URI segments.
1711
+ </span><span class="marked0"><a name="line1090" />1090 def to_hash
1712
+ </span><span class="inferred1"><a name="line1091" />1091 return {
1713
+ </span><span class="marked0"><a name="line1092" />1092 :scheme =&gt; self.scheme,
1714
+ </span><span class="marked1"><a name="line1093" />1093 :user =&gt; self.user,
1715
+ </span><span class="marked0"><a name="line1094" />1094 :password =&gt; self.password,
1716
+ </span><span class="marked1"><a name="line1095" />1095 :host =&gt; self.host,
1717
+ </span><span class="marked0"><a name="line1096" />1096 :port =&gt; self.specified_port,
1718
+ </span><span class="marked1"><a name="line1097" />1097 :path =&gt; self.path,
1719
+ </span><span class="marked0"><a name="line1098" />1098 :query =&gt; self.query,
1720
+ </span><span class="marked1"><a name="line1099" />1099 :fragment =&gt; self.fragment
1721
+ </span><span class="marked0"><a name="line1100" />1100 }
1722
+ </span><span class="marked1"><a name="line1101" />1101 end
1723
+ </span><span class="inferred0"><a name="line1102" />1102
1724
+ </span><span class="inferred1"><a name="line1103" />1103 # Returns a string representation of the URI object's state.
1725
+ </span><span class="marked0"><a name="line1104" />1104 def inspect
1726
+ </span><span class="marked1"><a name="line1105" />1105 sprintf(&quot;#&lt;%s:%#0x URI:%s&gt;&quot;, self.class.to_s, self.object_id, self.to_s)
1727
+ </span><span class="marked0"><a name="line1106" />1106 end
1728
+ </span><span class="inferred1"><a name="line1107" />1107
1729
+ </span><span class="inferred0"><a name="line1108" />1108 # This module handles internationalized domain names. When Ruby has an
1730
+ </span><span class="inferred1"><a name="line1109" />1109 # implementation of nameprep, stringprep, punycode, etc, this
1731
+ </span><span class="inferred0"><a name="line1110" />1110 # module should contain an actual implementation of IDNA instead of
1732
+ </span><span class="inferred1"><a name="line1111" />1111 # returning nil if libidn can't be used.
1733
+ </span><span class="marked0"><a name="line1112" />1112 module IDNA
1734
+ </span><span class="inferred1"><a name="line1113" />1113 # Returns the ascii representation of the label.
1735
+ </span><span class="marked0"><a name="line1114" />1114 def self.to_ascii(label)
1736
+ </span><span class="marked1"><a name="line1115" />1115 return nil if label.nil?
1737
+ </span><span class="marked0"><a name="line1116" />1116 if self.use_libidn?
1738
+ </span><span class="marked1"><a name="line1117" />1117 return IDN::Idna.toASCII(label)
1739
+ </span><span class="inferred0"><a name="line1118" />1118 else
1740
+ </span><span class="marked1"><a name="line1119" />1119 raise NotImplementedError,
1741
+ </span><span class="inferred0"><a name="line1120" />1120 &quot;There is no available pure-ruby implementation. &quot; +
1742
+ </span><span class="inferred1"><a name="line1121" />1121 &quot;Install libidn bindings.&quot;
1743
+ </span><span class="inferred0"><a name="line1122" />1122 end
1744
+ </span><span class="inferred1"><a name="line1123" />1123 end
1745
+ </span><span class="inferred0"><a name="line1124" />1124
1746
+ </span><span class="inferred1"><a name="line1125" />1125 # Returns the unicode representation of the label.
1747
+ </span><span class="marked0"><a name="line1126" />1126 def self.to_unicode(label)
1748
+ </span><span class="marked1"><a name="line1127" />1127 return nil if label.nil?
1749
+ </span><span class="marked0"><a name="line1128" />1128 if self.use_libidn?
1750
+ </span><span class="marked1"><a name="line1129" />1129 return IDN::Idna.toUnicode(label)
1751
+ </span><span class="inferred0"><a name="line1130" />1130 else
1752
+ </span><span class="marked1"><a name="line1131" />1131 raise NotImplementedError,
1753
+ </span><span class="inferred0"><a name="line1132" />1132 &quot;There is no available pure-ruby implementation. &quot; +
1754
+ </span><span class="inferred1"><a name="line1133" />1133 &quot;Install libidn bindings.&quot;
1755
+ </span><span class="inferred0"><a name="line1134" />1134 end
1756
+ </span><span class="inferred1"><a name="line1135" />1135 end
1757
+ </span><span class="inferred0"><a name="line1136" />1136
1758
+ </span><span class="marked1"><a name="line1137" />1137 private
1759
+ </span><span class="inferred0"><a name="line1138" />1138 # Determines if the libidn bindings are available and able to be used.
1760
+ </span><span class="marked1"><a name="line1139" />1139 def self.use_libidn?
1761
+ </span><span class="marked0"><a name="line1140" />1140 if !defined?(@use_libidn) || @use_libidn.nil?
1762
+ </span><span class="marked1"><a name="line1141" />1141 begin
1763
+ </span><span class="marked0"><a name="line1142" />1142 require 'rubygems'
1764
+ </span><span class="inferred1"><a name="line1143" />1143 rescue LoadError
1765
+ </span><span class="marked0"><a name="line1144" />1144 nil
1766
+ </span><span class="inferred1"><a name="line1145" />1145 end
1767
+ </span><span class="marked0"><a name="line1146" />1146 begin
1768
+ </span><span class="marked1"><a name="line1147" />1147 require 'idn'
1769
+ </span><span class="inferred0"><a name="line1148" />1148 rescue LoadError
1770
+ </span><span class="marked1"><a name="line1149" />1149 nil
1771
+ </span><span class="inferred0"><a name="line1150" />1150 end
1772
+ </span><span class="marked1"><a name="line1151" />1151 @use_libidn = !!(defined?(IDN::Idna))
1773
+ </span><span class="inferred0"><a name="line1152" />1152 end
1774
+ </span><span class="marked1"><a name="line1153" />1153 return @use_libidn
1775
+ </span><span class="inferred0"><a name="line1154" />1154 end
1776
+ </span><span class="inferred1"><a name="line1155" />1155 end
1777
+ </span><span class="inferred0"><a name="line1156" />1156
1778
+ </span><span class="marked1"><a name="line1157" />1157 private
1779
+ </span><span class="inferred0"><a name="line1158" />1158 # Resolves paths to their simplest form.
1780
+ </span><span class="marked1"><a name="line1159" />1159 def self.normalize_path(path)
1781
+ </span><span class="inferred0"><a name="line1160" />1160 # Section 5.2.4 of RFC 3986
1782
+ </span><span class="inferred1"><a name="line1161" />1161
1783
+ </span><span class="marked0"><a name="line1162" />1162 return nil if path.nil?
1784
+ </span><span class="marked1"><a name="line1163" />1163 normalized_path = path.dup
1785
+ </span><span class="marked0"><a name="line1164" />1164 previous_state = normalized_path.dup
1786
+ </span><span class="inferred1"><a name="line1165" />1165 begin
1787
+ </span><span class="marked0"><a name="line1166" />1166 previous_state = normalized_path.dup
1788
+ </span><span class="marked1"><a name="line1167" />1167 normalized_path.gsub!(/\/\.\//, &quot;/&quot;)
1789
+ </span><span class="marked0"><a name="line1168" />1168 normalized_path.gsub!(/\/\.$/, &quot;/&quot;)
1790
+ </span><span class="marked1"><a name="line1169" />1169 parent = normalized_path.scan(/\/([^\/]+)\/\.\.\//).flatten[0]
1791
+ </span><span class="marked0"><a name="line1170" />1170 if parent != &quot;.&quot; &amp;&amp; parent != &quot;..&quot;
1792
+ </span><span class="marked1"><a name="line1171" />1171 normalized_path.gsub!(/\/#{parent}\/\.\.\//, &quot;/&quot;)
1793
+ </span><span class="inferred0"><a name="line1172" />1172 end
1794
+ </span><span class="marked1"><a name="line1173" />1173 parent = normalized_path.scan(/\/([^\/]+)\/\.\.$/).flatten[0]
1795
+ </span><span class="marked0"><a name="line1174" />1174 if parent != &quot;.&quot; &amp;&amp; parent != &quot;..&quot;
1796
+ </span><span class="marked1"><a name="line1175" />1175 normalized_path.gsub!(/\/#{parent}\/\.\.$/, &quot;/&quot;)
1797
+ </span><span class="inferred0"><a name="line1176" />1176 end
1798
+ </span><span class="marked1"><a name="line1177" />1177 normalized_path.gsub!(/^\.\.?\/?/, &quot;&quot;)
1799
+ </span><span class="marked0"><a name="line1178" />1178 normalized_path.gsub!(/^\/\.\.?\//, &quot;/&quot;)
1800
+ </span><span class="marked1"><a name="line1179" />1179 end until previous_state == normalized_path
1801
+ </span><span class="marked0"><a name="line1180" />1180 return normalized_path
1802
+ </span><span class="inferred1"><a name="line1181" />1181 end
1803
+ </span><span class="inferred0"><a name="line1182" />1182
1804
+ </span><span class="inferred1"><a name="line1183" />1183 # Ensures that the URI is valid.
1805
+ </span><span class="marked0"><a name="line1184" />1184 def validate
1806
+ </span><span class="marked1"><a name="line1185" />1185 if self.scheme != nil &amp;&amp;
1807
+ </span><span class="marked0"><a name="line1186" />1186 (self.host == nil || self.host == &quot;&quot;) &amp;&amp;
1808
+ </span><span class="marked1"><a name="line1187" />1187 (self.path == nil || self.path == &quot;&quot;)
1809
+ </span><span class="marked0"><a name="line1188" />1188 raise InvalidURIError,
1810
+ </span><span class="inferred1"><a name="line1189" />1189 &quot;Absolute URI missing hierarchical segment.&quot;
1811
+ </span><span class="inferred0"><a name="line1190" />1190 end
1812
+ </span><span class="marked1"><a name="line1191" />1191 if self.host == nil
1813
+ </span><span class="marked0"><a name="line1192" />1192 if self.specified_port != nil ||
1814
+ </span><span class="marked1"><a name="line1193" />1193 self.user != nil ||
1815
+ </span><span class="marked0"><a name="line1194" />1194 self.password != nil
1816
+ </span><span class="marked1"><a name="line1195" />1195 raise InvalidURIError, &quot;Hostname not supplied.&quot;
1817
+ </span><span class="inferred0"><a name="line1196" />1196 end
1818
+ </span><span class="inferred1"><a name="line1197" />1197 end
1819
+ </span><span class="inferred0"><a name="line1198" />1198 end
1820
+ </span><span class="inferred1"><a name="line1199" />1199
1821
+ </span><span class="inferred0"><a name="line1200" />1200 # Replaces the internal state of self with the specified URI's state.
1822
+ </span><span class="inferred1"><a name="line1201" />1201 # Used in destructive operations to avoid massive code repetition.
1823
+ </span><span class="marked0"><a name="line1202" />1202 def replace_self(uri)
1824
+ </span><span class="inferred1"><a name="line1203" />1203 # Reset dependant values
1825
+ </span><span class="marked0"><a name="line1204" />1204 @userinfo = nil
1826
+ </span><span class="marked1"><a name="line1205" />1205 @authority = nil
1827
+ </span><span class="inferred0"><a name="line1206" />1206
1828
+ </span><span class="marked1"><a name="line1207" />1207 @scheme = uri.scheme
1829
+ </span><span class="marked0"><a name="line1208" />1208 @user = uri.user
1830
+ </span><span class="marked1"><a name="line1209" />1209 @password = uri.password
1831
+ </span><span class="marked0"><a name="line1210" />1210 @host = uri.host
1832
+ </span><span class="marked1"><a name="line1211" />1211 @specified_port = uri.instance_variable_get(&quot;@specified_port&quot;)
1833
+ </span><span class="marked0"><a name="line1212" />1212 @port = @specified_port.to_s.to_i
1834
+ </span><span class="marked1"><a name="line1213" />1213 @path = uri.path
1835
+ </span><span class="marked0"><a name="line1214" />1214 @query = uri.query
1836
+ </span><span class="marked1"><a name="line1215" />1215 @fragment = uri.fragment
1837
+ </span><span class="marked0"><a name="line1216" />1216 return self
1838
+ </span><span class="inferred1"><a name="line1217" />1217 end
1839
+ </span><span class="inferred0"><a name="line1218" />1218 end
1840
+ </span><span class="inferred1"><a name="line1219" />1219 end
1828
1841
  </span></pre><hr /> <p>Generated using the <a href='http://eigenclass.org/hiki.rb?rcov'>rcov code coverage analysis tool for Ruby</a> version 0.8.0.</p><p>
1829
1842
  <a href='http://validator.w3.org/check/referer'>
1830
1843
  <img src='http://www.w3.org/Icons/valid-xhtml10' height='31' alt='Valid XHTML 1.0!' width='88' />