1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
# frozen_string_literal: true
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# 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 Protocol
module HTTP
# A relative reference, excluding any authority. The path part of an HTTP request.
class Reference
include Comparable
# Generate a reference from a path and user parameters. The path may contain a `#fragment` or `?query=parameters`.
def self.parse(path = '/', parameters = nil)
base, fragment = path.split('#', 2)
path, query_string = base.split('?', 2)
self.new(path, query_string, fragment, parameters)
end
def initialize(path = '/', query_string = nil, fragment = nil, parameters = nil)
@path = path
@query_string = query_string
@fragment = fragment
@parameters = parameters
end
# The path component, e.g. /foo/bar/index.html
attr_accessor :path
# The un-parsed query string, e.g. 'x=10&y=20'
attr_accessor :query_string
# A fragment, the part after the '#'
attr_accessor :fragment
# User supplied parameters that will be appended to the query part.
attr_accessor :parameters
def freeze
return self if frozen?
@path.freeze
@query_string.freeze
@fragment.freeze
@parameters.freeze
super
end
def to_ary
[@path, @query_string, @fragment, @parameters]
end
def <=> other
to_ary <=> other.to_ary
end
def self.[] reference
if reference.is_a? self
return reference
else
return self.parse(reference)
end
end
def parameters?
@parameters and !@parameters.empty?
end
def query_string?
@query_string and !@query_string.empty?
end
def fragment?
@fragment and !@fragment.empty?
end
def append(buffer)
if query_string?
buffer << URL.escape_path(@path) << '?' << @query_string
buffer << '&' << URL.encode(@parameters) if parameters?
else
buffer << URL.escape_path(@path)
buffer << '?' << URL.encode(@parameters) if parameters?
end
if fragment?
buffer << '#' << URL.escape(@fragment)
end
return buffer
end
def to_s
append(String.new)
end
# Merges two references as specified by RFC2396, similar to `URI.join`.
def + other
other = self.class[other]
self.class.new(
expand_path(self.path, other.path, true),
other.query_string,
other.fragment,
other.parameters,
)
end
# Just the base path, without any query string, parameters or fragment.
def base
self.class.new(@path, nil, nil, nil)
end
# @option path [String] Append the string to this reference similar to `File.join`.
# @option parameters [Hash] Append the parameters to this reference.
# @option fragment [String] Set the fragment to this value.
def with(path: nil, parameters: nil, fragment: @fragment)
if @parameters
if parameters
parameters = @parameters.merge(parameters)
else
parameters = @parameters
end
end
if path
path = expand_path(@path, path, false)
else
path = @path
end
self.class.new(path, @query_string, fragment, parameters)
end
# The arguments to this function are legacy, prefer to use `with`.
def dup(path = nil, parameters = nil, merge_parameters = true)
if merge_parameters
with(path: path, parameters: parameters)
else
self.base.with(path: path, parameters: parameters)
end
end
private
def split(path)
if path.empty?
[path]
else
path.split('/', -1)
end
end
# @param pop [Boolean] whether to remove the last path component of the base path, to conform to URI merging behaviour, as defined by RFC2396.
def expand_path(base, relative, pop = true)
if relative.start_with? '/'
return relative
else
path = split(base)
# RFC2396 Section 5.2:
# 6) a) All but the last segment of the base URI's path component is
# copied to the buffer. In other words, any characters after the
# last (right-most) slash character, if any, are excluded.
path.pop if pop or path.last == ''
parts = split(relative)
parts.each do |part|
if part == '..'
path.pop
elsif part == '.'
# Do nothing.
else
path << part
end
end
return path.join('/')
end
end
end
end
end
|