lib/http/protocol/reference.rb in http-protocol-0.12.2 vs lib/http/protocol/reference.rb in http-protocol-0.13.0
- old
+ new
@@ -16,10 +16,12 @@
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
+require_relative 'url'
+
module HTTP
module Protocol
# A relative reference, excluding any authority.
class Reference
def initialize(path, query_string, fragment, parameters)
@@ -69,19 +71,19 @@
@fragment and !@fragment.empty?
end
def append(buffer)
if query_string?
- buffer << escape_path(@path) << '?' << @query_string
- buffer << '&' << encode(@parameters) if parameters?
+ buffer << URL.escape_path(@path) << '?' << @query_string
+ buffer << '&' << URL.encode(@parameters) if parameters?
else
- buffer << escape_path(@path)
- buffer << '?' << encode(@parameters) if parameters?
+ buffer << URL.escape_path(@path)
+ buffer << '?' << URL.encode(@parameters) if parameters?
end
if fragment?
- buffer << '#' << escape(@fragment)
+ buffer << '#' << URL.escape(@fragment)
end
return buffer
end
@@ -140,48 +142,9 @@
path << part
end
end
return path.join('/')
- end
- end
-
- # Escapes a generic string, using percent encoding.
- def escape(string)
- encoding = string.encoding
- string.b.gsub(/([^a-zA-Z0-9_.\-]+)/) do |m|
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
- end.force_encoding(encoding)
- end
-
- # According to https://tools.ietf.org/html/rfc3986#section-3.3, we escape non-pchar.
- NON_PCHAR = /([^a-zA-Z0-9_\-\.~!$&'()*+,;=:@\/]+)/.freeze
-
- # Escapes a path
- def escape_path(path)
- encoding = path.encoding
- path.b.gsub(NON_PCHAR) do |m|
- '%' + m.unpack('H2' * m.bytesize).join('%').upcase
- end.force_encoding(encoding)
- end
-
- # Encodes a hash or array into a query string
- def encode(value, prefix = nil)
- case value
- when Array
- return value.map {|v|
- encode(v, "#{prefix}[]")
- }.join("&")
- when Hash
- return value.map {|k, v|
- encode(v, prefix ? "#{prefix}[#{escape(k.to_s)}]" : escape(k.to_s))
- }.reject(&:empty?).join('&')
- when nil
- return prefix
- else
- raise ArgumentError, "value must be a Hash" if prefix.nil?
-
- return "#{prefix}=#{escape(value.to_s)}"
end
end
end
end
end