PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/multisketchup/ruby-1.8.0-win32-lib/lib/ruby/1.8/uri/common.rb

http://multisketchup.googlecode.com/
Ruby | 424 lines | 321 code | 39 blank | 64 comment | 15 complexity | ed2994797da11186fbbbd8b57804d445 MD5 | raw file
  1. #
  2. # $Id: common.rb,v 1.10 2003/03/29 06:39:50 akr Exp $
  3. #
  4. # Copyright (c) 2001 akira yamada <akira@ruby-lang.org>
  5. # You can redistribute it and/or modify it under the same term as Ruby.
  6. #
  7. =begin
  8. == URI
  9. =end
  10. module URI
  11. module REGEXP
  12. module PATTERN
  13. # RFC 2396 (URI Generic Syntax)
  14. # RFC 2732 (IPv6 Literal Addresses in URL's)
  15. # RFC 2373 (IPv6 Addressing Architecture)
  16. # alpha = lowalpha | upalpha
  17. ALPHA = "a-zA-Z"
  18. # alphanum = alpha | digit
  19. ALNUM = "#{ALPHA}\\d"
  20. # hex = digit | "A" | "B" | "C" | "D" | "E" | "F" |
  21. # "a" | "b" | "c" | "d" | "e" | "f"
  22. HEX = "a-fA-F\\d"
  23. # escaped = "%" hex hex
  24. ESCAPED = "%[#{HEX}]{2}"
  25. # mark = "-" | "_" | "." | "!" | "~" | "*" | "'" |
  26. # "(" | ")"
  27. # unreserved = alphanum | mark
  28. UNRESERVED = "-_.!~*'()#{ALNUM}"
  29. # reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
  30. # "$" | ","
  31. # reserved = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |
  32. # "$" | "," | "[" | "]" (RFC 2732)
  33. RESERVED = ";/?:@&=+$,\\[\\]"
  34. # uric = reserved | unreserved | escaped
  35. URIC = "(?:[#{UNRESERVED}#{RESERVED}]|#{ESCAPED})"
  36. # uric_no_slash = unreserved | escaped | ";" | "?" | ":" | "@" |
  37. # "&" | "=" | "+" | "$" | ","
  38. URIC_NO_SLASH = "(?:[#{UNRESERVED};?:@&=+$,]|#{ESCAPED})"
  39. # query = *uric
  40. QUERY = "#{URIC}*"
  41. # fragment = *uric
  42. FRAGMENT = "#{URIC}*"
  43. # domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
  44. DOMLABEL = "(?:[#{ALNUM}](?:[-#{ALNUM}]*[#{ALNUM}])?)"
  45. # toplabel = alpha | alpha *( alphanum | "-" ) alphanum
  46. TOPLABEL = "(?:[#{ALPHA}](?:[-#{ALNUM}]*[#{ALNUM}])?)"
  47. # hostname = *( domainlabel "." ) toplabel [ "." ]
  48. HOSTNAME = "(?:#{DOMLABEL}\\.)*#{TOPLABEL}\\.?"
  49. # RFC 2373, APPENDIX B:
  50. # IPv6address = hexpart [ ":" IPv4address ]
  51. # IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
  52. # hexpart = hexseq | hexseq "::" [ hexseq ] | "::" [ hexseq ]
  53. # hexseq = hex4 *( ":" hex4)
  54. # hex4 = 1*4HEXDIG
  55. #
  56. # XXX: This definition has a flaw. "::" + IPv4address must be
  57. # allowed too. Here is a replacement.
  58. #
  59. # IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT
  60. IPV4ADDR = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"
  61. # hex4 = 1*4HEXDIG
  62. HEX4 = "[#{HEX}]{1,4}"
  63. # lastpart = hex4 | IPv4address
  64. LASTPART = "(?:#{HEX4}|#{IPV4ADDR})"
  65. # hexseq1 = *( hex4 ":" ) hex4
  66. HEXSEQ1 = "(?:#{HEX4}:)*#{HEX4}"
  67. # hexseq2 = *( hex4 ":" ) lastpart
  68. HEXSEQ2 = "(?:#{HEX4}:)*#{LASTPART}"
  69. # IPv6address = hexseq2 | [ hexseq1 ] "::" [ hexseq2 ]
  70. IPV6ADDR = "(?:#{HEXSEQ2}|(?:#{HEXSEQ1})?::(?:#{HEXSEQ2})?)"
  71. # IPv6prefix = ( hexseq1 | [ hexseq1 ] "::" [ hexseq1 ] ) "/" 1*2DIGIT
  72. # unused
  73. # ipv6reference = "[" IPv6address "]" (RFC 2732)
  74. IPV6REF = "\\[#{IPV6ADDR}\\]"
  75. # host = hostname | IPv4address
  76. # host = hostname | IPv4address | IPv6reference (RFC 2732)
  77. HOST = "(?:#{HOSTNAME}|#{IPV4ADDR}|#{IPV6REF})"
  78. # port = *digit
  79. PORT = "\d*"
  80. # hostport = host [ ":" port ]
  81. HOSTPORT = "#{HOST}(?:#{PORT})?"
  82. # userinfo = *( unreserved | escaped |
  83. # ";" | ":" | "&" | "=" | "+" | "$" | "," )
  84. USERINFO = "(?:[#{UNRESERVED};:&=+$,]|#{ESCAPED})*"
  85. # pchar = unreserved | escaped |
  86. # ":" | "@" | "&" | "=" | "+" | "$" | ","
  87. PCHAR = "(?:[#{UNRESERVED}:@&=+$,]|#{ESCAPED})"
  88. # param = *pchar
  89. PARAM = "#{PCHAR}*"
  90. # segment = *pchar *( ";" param )
  91. SEGMENT = "#{PCHAR}*(?:;#{PARAM})*"
  92. # path_segments = segment *( "/" segment )
  93. PATH_SEGMENTS = "#{SEGMENT}(?:/#{SEGMENT})*"
  94. # server = [ [ userinfo "@" ] hostport ]
  95. SERVER = "(?:#{USERINFO}@)?#{HOSTPORT}"
  96. # reg_name = 1*( unreserved | escaped | "$" | "," |
  97. # ";" | ":" | "@" | "&" | "=" | "+" )
  98. REG_NAME = "(?:[#{UNRESERVED}$,;+@&=+]|#{ESCAPED})+"
  99. # authority = server | reg_name
  100. AUTHORITY = "(?:#{SERVER}|#{REG_NAME})"
  101. # rel_segment = 1*( unreserved | escaped |
  102. # ";" | "@" | "&" | "=" | "+" | "$" | "," )
  103. REL_SEGMENT = "(?:[#{UNRESERVED};@&=+$,]|#{ESCAPED})+"
  104. # scheme = alpha *( alpha | digit | "+" | "-" | "." )
  105. SCHEME = "[#{ALPHA}][-+.#{ALPHA}\\d]*"
  106. # abs_path = "/" path_segments
  107. ABS_PATH = "/#{PATH_SEGMENTS}"
  108. # rel_path = rel_segment [ abs_path ]
  109. REL_PATH = "#{REL_SEGMENT}(?:#{ABS_PATH})?"
  110. # net_path = "//" authority [ abs_path ]
  111. NET_PATH = "//#{AUTHORITY}(?:#{ABS_PATH})?"
  112. # hier_part = ( net_path | abs_path ) [ "?" query ]
  113. HIER_PART = "(?:#{NET_PATH}|#{ABS_PATH})(?:\\?(?:#{QUERY}))?"
  114. # opaque_part = uric_no_slash *uric
  115. OPAQUE_PART = "#{URIC_NO_SLASH}#{URIC}*"
  116. # absoluteURI = scheme ":" ( hier_part | opaque_part )
  117. ABS_URI = "#{SCHEME}:(?:#{HIER_PART}|#{OPAQUE_PART})"
  118. # relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
  119. REL_URI = "(?:#{NET_PATH}|#{ABS_PATH}|#{REL_PATH})(?:\\?#{QUERY})?"
  120. # URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  121. URI_REF = "(?:#{ABS_URI}|#{REL_URI})?(?:##{FRAGMENT})?"
  122. # XXX:
  123. X_ABS_URI = "
  124. (#{PATTERN::SCHEME}): (?# 1: scheme)
  125. (?:
  126. (#{PATTERN::OPAQUE_PART}) (?# 2: opaque)
  127. |
  128. (?:(?:
  129. //(?:
  130. (?:(?:(#{PATTERN::USERINFO})@)? (?# 3: userinfo)
  131. (?:(#{PATTERN::HOST})(?::(\\d*))?))?(?# 4: host, 5: port)
  132. |
  133. (#{PATTERN::REG_NAME}) (?# 6: registry)
  134. )
  135. |
  136. (?!//)) (?# XXX: '//' is the mark for hostport)
  137. (#{PATTERN::ABS_PATH})? (?# 7: path)
  138. )(?:\\?(#{PATTERN::QUERY}))? (?# 8: query)
  139. )
  140. (?:\\#(#{PATTERN::FRAGMENT}))? (?# 9: fragment)
  141. "
  142. X_REL_URI = "
  143. (?:
  144. (?:
  145. //
  146. (?:
  147. (?:(#{PATTERN::USERINFO})@)? (?# 1: userinfo)
  148. (#{PATTERN::HOST})?(?::(\\d*))? (?# 2: host, 3: port)
  149. |
  150. (#{PATTERN::REG_NAME}) (?# 4: registry)
  151. )
  152. )
  153. |
  154. (#{PATTERN::REL_SEGMENT}) (?# 5: rel_segment)
  155. )?
  156. (#{PATTERN::ABS_PATH})? (?# 6: abs_path)
  157. (?:\\?(#{PATTERN::QUERY}))? (?# 7: query)
  158. (?:\\#(#{PATTERN::FRAGMENT}))? (?# 8: fragment)
  159. "
  160. end # PATTERN
  161. # for URI::split
  162. ABS_URI = Regexp.new('^' + PATTERN::X_ABS_URI + '$', #'
  163. Regexp::EXTENDED, 'N').freeze
  164. REL_URI = Regexp.new('^' + PATTERN::X_REL_URI + '$', #'
  165. Regexp::EXTENDED, 'N').freeze
  166. # for URI::extract
  167. URI_REF = Regexp.new(PATTERN::URI_REF, false, 'N').freeze
  168. ABS_URI_REF = Regexp.new(PATTERN::X_ABS_URI, Regexp::EXTENDED, 'N').freeze
  169. REL_URI_REF = Regexp.new(PATTERN::X_REL_URI, Regexp::EXTENDED, 'N').freeze
  170. # for URI::escape/unescape
  171. ESCAPED = Regexp.new(PATTERN::ESCAPED, false, 'N').freeze
  172. UNSAFE = Regexp.new("[^#{PATTERN::UNRESERVED}#{PATTERN::RESERVED}]",
  173. false, 'N').freeze
  174. # for Generic#initialize
  175. SCHEME = Regexp.new("^#{PATTERN::SCHEME}$", false, 'N').freeze #"
  176. USERINFO = Regexp.new("^#{PATTERN::USERINFO}$", false, 'N').freeze #"
  177. HOST = Regexp.new("^#{PATTERN::HOST}$", false, 'N').freeze #"
  178. PORT = Regexp.new("^#{PATTERN::PORT}$", false, 'N').freeze #"
  179. OPAQUE = Regexp.new("^#{PATTERN::OPAQUE_PART}$", false, 'N').freeze #"
  180. REGISTRY = Regexp.new("^#{PATTERN::REG_NAME}$", false, 'N').freeze #"
  181. ABS_PATH = Regexp.new("^#{PATTERN::ABS_PATH}$", false, 'N').freeze #"
  182. REL_PATH = Regexp.new("^#{PATTERN::REL_PATH}$", false, 'N').freeze #"
  183. QUERY = Regexp.new("^#{PATTERN::QUERY}$", false, 'N').freeze #"
  184. FRAGMENT = Regexp.new("^#{PATTERN::FRAGMENT}$", false, 'N').freeze #"
  185. end # REGEXP
  186. module Util
  187. def make_components_hash(klass, array_hash)
  188. tmp = {}
  189. if array_hash.kind_of?(Array) &&
  190. array_hash.size == klass.component.size - 1
  191. klass.component[1..-1].each_index do |i|
  192. begin
  193. tmp[klass.component[i + 1]] = array_hash[i].clone
  194. rescue TypeError
  195. tmp[klass.component[i + 1]] = array_hash[i]
  196. end
  197. end
  198. elsif array_hash.kind_of?(Hash)
  199. array_hash.each do |key, value|
  200. begin
  201. tmp[key] = value.clone
  202. rescue TypeError
  203. tmp[key] = value
  204. end
  205. end
  206. else
  207. raise ArgumentError,
  208. "expected Array of or Hash of components of #{klass.to_s} (#{klass.component[1..-1].join(', ')})"
  209. end
  210. tmp[:scheme] = klass.to_s.sub(/\A.*::/, '').downcase
  211. return tmp
  212. end
  213. module_function :make_components_hash
  214. end
  215. module Escape
  216. include REGEXP
  217. def escape(str, unsafe = UNSAFE)
  218. unless unsafe.kind_of?(Regexp)
  219. # perhaps unsafe is String object
  220. unsafe = Regexp.new(Regexp.quote(unsafe), false, 'N')
  221. end
  222. str.gsub(unsafe) do |us|
  223. tmp = ''
  224. us.each_byte do |uc|
  225. tmp << sprintf('%%%02X', uc)
  226. end
  227. tmp
  228. end
  229. end
  230. alias encode escape
  231. def unescape(str)
  232. str.gsub(ESCAPED) do
  233. $&[1,2].hex.chr
  234. end
  235. end
  236. alias decode unescape
  237. end
  238. include REGEXP
  239. extend Escape
  240. @@schemes = {}
  241. class Error < StandardError; end
  242. class InvalidURIError < Error; end # it is not URI.
  243. class InvalidComponentError < Error; end # it is not component of URI.
  244. class BadURIError < Error; end # the URI is valid but it is bad for the position.
  245. =begin
  246. === Methods
  247. --- URI::split(uri)
  248. =end
  249. def self.split(uri)
  250. case uri
  251. when ''
  252. # null uri
  253. when ABS_URI
  254. scheme, opaque, userinfo, host, port,
  255. registry, path, query, fragment = $~[1..-1]
  256. # URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  257. # absoluteURI = scheme ":" ( hier_part | opaque_part )
  258. # hier_part = ( net_path | abs_path ) [ "?" query ]
  259. # opaque_part = uric_no_slash *uric
  260. # abs_path = "/" path_segments
  261. # net_path = "//" authority [ abs_path ]
  262. # authority = server | reg_name
  263. # server = [ [ userinfo "@" ] hostport ]
  264. if !scheme
  265. raise InvalidURIError,
  266. "bad URI(absolute but no scheme): #{uri}"
  267. end
  268. if !opaque && (!path && (!host && !registry))
  269. raise InvalidURIError,
  270. "bad URI(absolute but no path): #{uri}"
  271. end
  272. when REL_URI
  273. scheme = nil
  274. opaque = nil
  275. userinfo, host, port, registry,
  276. rel_segment, abs_path, query, fragment = $~[1..-1]
  277. if rel_segment && abs_path
  278. path = rel_segment + abs_path
  279. elsif rel_segment
  280. path = rel_segment
  281. elsif abs_path
  282. path = abs_path
  283. end
  284. # URI-reference = [ absoluteURI | relativeURI ] [ "#" fragment ]
  285. # relativeURI = ( net_path | abs_path | rel_path ) [ "?" query ]
  286. # net_path = "//" authority [ abs_path ]
  287. # abs_path = "/" path_segments
  288. # rel_path = rel_segment [ abs_path ]
  289. # authority = server | reg_name
  290. # server = [ [ userinfo "@" ] hostport ]
  291. else
  292. raise InvalidURIError, "bad URI(is not URI?): #{uri}"
  293. end
  294. path = '' if !path && !opaque # (see RFC2396 Section 5.2)
  295. ret = [
  296. scheme,
  297. userinfo, host, port, # X
  298. registry, # X
  299. path, # Y
  300. opaque, # Y
  301. query,
  302. fragment
  303. ]
  304. return ret
  305. end
  306. =begin
  307. --- URI::parse(uri_str)
  308. =end
  309. def self.parse(uri)
  310. scheme, userinfo, host, port,
  311. registry, path, opaque, query, fragment = self.split(uri)
  312. if scheme && @@schemes.include?(scheme.upcase)
  313. @@schemes[scheme.upcase].new(scheme, userinfo, host, port,
  314. registry, path, opaque, query,
  315. fragment)
  316. else
  317. Generic.new(scheme, userinfo, host, port,
  318. registry, path, opaque, query,
  319. fragment)
  320. end
  321. end
  322. =begin
  323. --- URI::join(str[, str, ...])
  324. =end
  325. def self.join(*str)
  326. u = self.parse(str[0])
  327. str[1 .. -1].each do |x|
  328. u = u.merge(x)
  329. end
  330. u
  331. end
  332. =begin
  333. --- URI::extract(str[, schemes])
  334. =end
  335. def self.extract(str, schemes = [])
  336. urls = []
  337. regexp = ABS_URI_REF
  338. unless schemes.empty?
  339. regexp = Regexp.new('(?=' + schemes.collect{|s|
  340. Regexp.quote(s + ':')
  341. }.join('|') + ')' + PATTERN::X_ABS_URI,
  342. Regexp::EXTENDED, 'N')
  343. end
  344. str.scan(regexp) {
  345. if block_given?
  346. yield($&)
  347. else
  348. urls << $&
  349. end
  350. }
  351. if block_given?
  352. return nil
  353. else
  354. return urls
  355. end
  356. end
  357. end # URI