Class: Yast::URLClass
- Inherits:
-
Module
- Object
- Module
- Yast::URLClass
- Defined in:
- ../../src/modules/URL.rb
Instance Method Summary (collapse)
-
- (String) Build(tokens)
Build URL from tokens as parsed with Parse.
-
- (Object) Check(url)
Check URL.
-
- (String) EscapeString(_in, transform)
Escape reserved characters in string used as a part of URL (e.g. '%' => '%25', '@' => '%40'…).
-
- (Object) FormatURL(tokens, len)
-
Format URL - truncate the middle part of the directory to fit to the requested lenght.
-
-
- (String) HidePassword(url)
Hide password in an URL - replaces the password in the URL by 'PASSWORD' string.
-
- (Hash) HidePasswordToken(tokens)
Hide password token in parsed URL (by URL::Parse()) - the password is replaced by 'PASSWORD' string.
- - (Object) main
-
- (Hash{String => String}) MakeMapFromParams(params)
Reads list of HTTP params and returns them as map.
-
- (Object) MakeParamsFromMap(params_map)
Returns string made of HTTP params.
-
- (Object) Parse(url)
Tokenize URL.
-
- (String) UnEscapeString(_in, transform)
Escape reserved characters in string used as a part of URL (e.g. '%25' => '%', '%40' => '@'…).
Instance Method Details
- (String) Build(tokens)
Build URL from tokens as parsed with Parse
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
# File '../../src/modules/URL.rb', line 341 def Build(tokens) tokens = deep_copy(tokens) url = "" userpass = "" Builtins.y2debug("URL::Build(): input: %1", tokens) if Builtins.regexpmatch( Ops.get_string(tokens, "scheme", ""), "^[[:alpha:]]*$" ) # if (tokens["scheme"]:"" == "samba") url="smb"; # else url = Ops.get_string(tokens, "scheme", "") end Builtins.y2debug("url: %1", url) if Ops.get_string(tokens, "user", "") != "" userpass = URLRecode.EscapePassword(Ops.get_string(tokens, "user", "")) Builtins.y2milestone( "Escaped username '%1' => '%2'", Ops.get_string(tokens, "user", ""), userpass ) end if Builtins.size(userpass) != 0 && Ops.get_string(tokens, "pass", "") != "" userpass = Builtins.sformat( "%1:%2", userpass, URLRecode.EscapePassword(Ops.get_string(tokens, "pass", "")) ) end if Ops.greater_than(Builtins.size(userpass), 0) userpass = Ops.add(userpass, "@") end url = Builtins.sformat("%1://%2", url, userpass) Builtins.y2debug("url: %1", url) if Hostname.CheckFQ(Ops.get_string(tokens, "host", "")) || IP.Check(Ops.get_string(tokens, "host", "")) # enclose an IPv6 address in square brackets url = IP.Check6(Ops.get_string(tokens, "host", "")) ? Builtins.sformat("%1[%2]", url, Ops.get_string(tokens, "host", "")) : Builtins.sformat("%1%2", url, Ops.get_string(tokens, "host", "")) end Builtins.y2debug("url: %1", url) if Builtins.regexpmatch(Ops.get_string(tokens, "port", ""), "^[0-9]*$") && Ops.get_string(tokens, "port", "") != "" url = Builtins.sformat("%1:%2", url, Ops.get_string(tokens, "port", "")) end Builtins.y2debug("url: %1", url) # path is not empty and doesn't start with "/" if Ops.get_string(tokens, "path", "") != "" && !Builtins.regexpmatch(Ops.get_string(tokens, "path", ""), "^/") url = Builtins.sformat( "%1/%2", url, URLRecode.EscapePath(Ops.get_string(tokens, "path", "")) ) # patch is not empty and starts with "/" elsif Ops.get_string(tokens, "path", "") != "" && Builtins.regexpmatch(Ops.get_string(tokens, "path", ""), "^/") while Builtins.substring(Ops.get_string(tokens, "path", ""), 0, 2) == "//" Ops.set( tokens, "path", Builtins.substring(Ops.get_string(tokens, "path", ""), 1) ) end if Ops.get_string(tokens, "scheme", "") == "ftp" url = Builtins.sformat( "%1/%%2f%2", url, Builtins.substring( URLRecode.EscapePath(Ops.get_string(tokens, "path", "")), 1 ) ) else url = Builtins.sformat( "%1%2", url, URLRecode.EscapePath(Ops.get_string(tokens, "path", "")) ) end end Builtins.y2debug("url: %1", url) query_map = MakeMapFromParams(Ops.get_string(tokens, "query", "")) if Ops.get_string(tokens, "scheme", "") == "smb" && Ops.greater_than( Builtins.size(Ops.get_string(tokens, "domain", "")), 0 ) && Ops.get(query_map, "workgroup", "") != Ops.get_string(tokens, "domain", "") Ops.set(query_map, "workgroup", Ops.get_string(tokens, "domain", "")) Ops.set(tokens, "query", MakeParamsFromMap(query_map)) end if Ops.get_string(tokens, "query", "") != "" url = Builtins.sformat( "%1?%2", url, URLRecode.EscapeQuery(Ops.get_string(tokens, "query", "")) ) end if Ops.get_string(tokens, "fragment", "") != "" url = Builtins.sformat( "%1#%2", url, URLRecode.EscapePassword(Ops.get_string(tokens, "fragment", "")) ) end Builtins.y2debug("url: %1", url) if !Check(url) Builtins.y2error("Invalid URL: %1", url) return "" end Builtins.y2debug("URL::Build(): result: %1", url) url end |
- (Object) Check(url)
Check URL
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File '../../src/modules/URL.rb', line 291 def Check(url) # We don't allow empty URLs return false if url == nil || Ops.less_than(Builtins.size(url), 1) # We don't allow URLs with spaces return false if Builtins.search(url, " ") != nil tokens = Parse(url) Builtins.y2debug("tokens: %1", tokens) # Check "scheme" : "http" if !Builtins.regexpmatch( Ops.get_string(tokens, "scheme", ""), "^[[:alpha:]]*$" ) return false end # Check "host" : "www.suse.cz" if !Hostname.CheckFQ(Ops.get_string(tokens, "host", "")) && !IP.Check(Ops.get_string(tokens, "host", "")) && Ops.get_string(tokens, "host", "") != "" return false end # Check "path" : /path/index.html" # Check "port" : "80" if !Builtins.regexpmatch(Ops.get_string(tokens, "port", ""), "^[0-9]*$") return false end # Check "user" : "name" # Check "pass" : "pass" # Check "query" : "question" # Check "fragment": "part" true end |
- (String) EscapeString(_in, transform)
Escape reserved characters in string used as a part of URL (e.g. '%' => '%25', '@' => '%40'…)
URL::EscapeString (“some.nice.url/:with:/$p#ci&l/ch@rs/”, URL::transform_map_passwd) -> http%3a%2f%2fsome.nice.url%2f%3awith%3a%2f%24p#ci%26l%2fch%40rs%2f
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File '../../src/modules/URL.rb', line 143 def EscapeString(_in, transform) transform = deep_copy(transform) ret = "" return ret if _in == nil || _in == "" # replace % at first ret = Builtins.mergestring(Builtins.splitstring(_in, "%"), "%25") # replace the other reserved characters Builtins.foreach(transform) do |src, tgt| ret = Builtins.mergestring(Builtins.splitstring(ret, src), tgt) end ret end |
- (Object) FormatURL(tokens, len)
-
Format URL - truncate the middle part of the directory to fit to the requested lenght. *
-
Elements in the middle of the path specification are replaced by ellipsis (…).
-
The result migth be longer that requested size if other URL parts are longer than the requested size.
-
If the requested size is greater than size of the full URL then full URL is returned.
-
Only path element of the URL is changed the other parts are not modified (e.g. protocol name) *
-
@example FormatURL(“download.opensuse.org/very/log/path/which/will/be/truncated/target_file”, 45) -> "download.opensuse.org/.../target_file"
-
@example FormatURL(“download.opensuse.org/very/log/path/which/will/be/truncated/target_file”, 60) -> "download.opensuse.org/very/.../be/truncated/target_file" *
-
@param tokens parsed URL
-
@see Parse should be used to convert URL string to a map (tokens parameter)
-
@param len requested maximum lenght of the output string
-
@return string Truncated URL
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File '../../src/modules/URL.rb', line 490 def FormatURL(tokens, len) tokens = deep_copy(tokens) ret = Build(tokens) # full URL is shorter than requested, no truncation needed return ret if Ops.less_or_equal(Builtins.size(ret), len) # it's too long, some parts must be removed pth = Ops.get_string(tokens, "path", "") Ops.set(tokens, "path", "") no_path = Build(tokens) # size for the directory part dir_size = Ops.subtract(len, Builtins.size(no_path)) # remove the path in the middle new_path = String.FormatFilename(pth, dir_size) # build the url with the new path Ops.set(tokens, "path", new_path) Build(tokens) end |
- (String) HidePassword(url)
Hide password in an URL - replaces the password in the URL by 'PASSWORD' string. If there is no password in the URL the original URL is returned. It should be used when an URL is logged to y2log or when it is displayed to user.
614 615 616 617 618 619 620 621 622 623 |
# File '../../src/modules/URL.rb', line 614 def HidePassword(url) # Url::Build(Url::Parse) transforms the URL too much, see #247249#c41 # replace ://user:password@ by ://user:PASSWORD@ subd = Builtins.regexpsub( url, "(.*)(://[^/:]*):[^/@]*@(.*)", "\\1\\2:PASSWORD@\\3" ) subd == nil ? url : subd end |
- (Hash) HidePasswordToken(tokens)
Hide password token in parsed URL (by URL::Parse()) - the password is replaced by 'PASSWORD' string. Similar to HidePassword() but uses a parsed URL as the input.
629 630 631 632 633 634 635 636 637 638 639 640 |
# File '../../src/modules/URL.rb', line 629 def HidePasswordToken(tokens) tokens = deep_copy(tokens) ret = deep_copy(tokens) # hide the password if it's there if Builtins.haskey(ret, "pass") && Ops.greater_than(Builtins.size(Ops.get_string(ret, "pass", "")), 0) Ops.set(ret, "pass", "PASSWORD") end deep_copy(ret) end |
- (Object) main
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 |
# File '../../src/modules/URL.rb', line 36 def main textdomain "base" Yast.import "Hostname" Yast.import "String" Yast.import "IP" Yast.import "URLRecode" # TODO: # - read URI(3) # - esp. compare the regex mentioned in the URI(3) with ours: # my($scheme, $authority, $path, $query, $fragment) = # $uri =~ m|^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|; # Valid characters in URL # # bnc#694582 - addedd @ as it is allowed in authority part of URI. # for details see RFC2616 and RFC2396 # @ValidChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:_-/%@" # Transform map used for (un)escaping characters in username/password part of an URL. # It doesn't contain '%' because this character must be used in a particular # order (the first or the last) during processing @transform_map_passwd = { ";" => "%3b", "/" => "%2f", "?" => "%3f", ":" => "%3a", "@" => "%40", "&" => "%26", "=" => "%3d", "+" => "%2b", "$" => "%24", "," => "%2c", " " => "%20" } # Transform map used for (un)escaping characters in file location part of an URL. # It doesn't contain '%' because this character must be used in a particular # order (the first or the last) during processing @transform_map_filename = { ";" => "%3b", "?" => "%3f", ":" => "%3a", "@" => "%40", "&" => "%26", "=" => "%3d", "+" => "%2b", "$" => "%24", "," => "%2c", " " => "%20" } # Transform map used for (un)escaping characters in query part of a URL. # It doesn't contain '%' because this character must be used in a particular # order (the first or the last) during processing @transform_map_query = { ";" => "%3b", "?" => "%3f", "@" => "%40", "+" => "%2b", "$" => "%24", "," => "%2c", " " => "%20" } end |
- (Hash{String => String}) MakeMapFromParams(params)
Reads list of HTTP params and returns them as map. (Useful also for cd:/, dvd:/, nfs:/ ... params) Neither keys nor values are HTML-unescaped, see UnEscapeString().
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File '../../src/modules/URL.rb', line 539 def MakeMapFromParams(params) # Error if params == nil Builtins.y2error("Erroneous (nil) params!") return nil # Empty elsif params == "" return {} end params_list = Builtins.splitstring(params, "&") params_list = Builtins.filter(params_list) do |one_param| one_param != "" && one_param != nil end ret = {} eq_pos = nil opt = "" val = "" Builtins.foreach(params_list) do |one_param| eq_pos = Builtins.search(one_param, "=") if eq_pos == nil Ops.set(ret, one_param, "") else opt = Builtins.substring(one_param, 0, eq_pos) val = Builtins.substring(one_param, Ops.add(eq_pos, 1)) Ops.set(ret, opt, val) end end deep_copy(ret) end |
- (Object) MakeParamsFromMap(params_map)
Returns string made of HTTP params. It's a reverse function to MakeMapFromParams(). Neither keys nor values are HTML-escaped, use EscapeString() if needed.
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File '../../src/modules/URL.rb', line 588 def MakeParamsFromMap(params_map) params_map = deep_copy(params_map) # ["key1=value1", "key2=value2", ...] -> "key1=value1&key2=value2" Builtins.mergestring( # ["key" : "value", ...] -> ["key=value", ...] Builtins.maplist(params_map) do |key, value| if value == nil Builtins.y2warning("Empty value for key %1", key) value = "" end if key == nil || key == "" Builtins.y2error("Empty key (will be skipped)") next "" end # "key=value" Builtins.sformat("%1=%2", key, value) end, "&" ) end |
- (Object) Parse(url)
Tokenize URL
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File '../../src/modules/URL.rb', line 175 def Parse(url) Builtins.y2debug("url=%1", url) # We don't parse empty URLs return {} if url == nil || Ops.less_than(Builtins.size(url), 1) # Extract basic URL parts: scheme://host/path?question#part rawtokens = Builtins.regexptokenize( url, "^" + # 0,1: http:// "(([^:/?#]+):[/]{0,2})?" + # 2: user:pass@www.suse.cz:23 "([^/?#]*)?" + # 3: /some/path "([^?#]*)?" + # 4,5: ?question "(\\?([^#]*))?" + # 6,7: #fragment "(#(.*))?" ) Builtins.y2debug("rawtokens=%1", rawtokens) tokens = {} Ops.set(tokens, "scheme", Ops.get_string(rawtokens, 1, "")) pth = Ops.get_string(rawtokens, 3, "") if Ops.get_string(tokens, "scheme", "") == "ftp" if Builtins.substring(pth, 0, 4) == "/%2f" pth = Ops.add("/", Builtins.substring(pth, 4)) elsif pth != "" pth = Builtins.substring(pth, 1) end end Ops.set(tokens, "path", URLRecode.UnEscape(pth)) Ops.set( tokens, "query", URLRecode.UnEscape(Ops.get_string(rawtokens, 5, "")) ) Ops.set( tokens, "fragment", URLRecode.UnEscape(Ops.get_string(rawtokens, 7, "")) ) # Extract username:pass@host:port userpass = Builtins.regexptokenize( Ops.get_string(rawtokens, 2, ""), "^" + # 0,1,2,3: user:pass@ "(([^@:]+)(:([^@:]+))?@)?" + # 4,5,6,7: hostname|[xxx] "(([^:@]+))" + # FIXME "(([^:@]+)|(\\[([^]]+)\\]))" + # 8,9: port "(:([^:@]+))?" ) Builtins.y2debug("userpass=%1", userpass) Ops.set( tokens, "user", URLRecode.UnEscape(Ops.get_string(userpass, 1, "")) ) Ops.set( tokens, "pass", URLRecode.UnEscape(Ops.get_string(userpass, 3, "")) ) Ops.set(tokens, "port", Ops.get_string(userpass, 7, "")) if Ops.get_string(userpass, 5, "") != "" Ops.set(tokens, "host", Ops.get_string(userpass, 5, "")) else Ops.set(tokens, "host", Ops.get_string(userpass, 7, "")) end hostport6 = Builtins.substring( Ops.get_string(rawtokens, 2, ""), Builtins.size(Ops.get_string(userpass, 0, "")) ) Builtins.y2debug("hostport6: %1", hostport6) # check if there is an IPv6 address host6 = Builtins.regexpsub(hostport6, "^\\[(.*)\\]", "\\1") if host6 != nil && host6 != "" Builtins.y2milestone("IPv6 host detected: %1", host6) Ops.set(tokens, "host", host6) port6 = Builtins.regexpsub(hostport6, "^\\[.*\\]:(.*)", "\\1") Builtins.y2debug("port: %1", port6) Ops.set(tokens, "port", port6 != nil ? port6 : "") end # some exceptions for samba scheme (there is optional extra option "domain") if Ops.get_string(tokens, "scheme", "") == "samba" || Ops.get_string(tokens, "scheme", "") == "smb" # Note: CUPS uses different URL syntax for Samba printers: # smb://username:password@workgroup/server/printer # Fortunately yast2-printer does not use URL.ycp, so we can safely support libzypp syntax only: # smb://username:passwd@servername/share/path/on/the/share?workgroup=mygroup = MakeMapFromParams(Ops.get_string(tokens, "query", "")) if Builtins.haskey(, "workgroup") Ops.set(tokens, "domain", Ops.get(, "workgroup", "")) end end Builtins.y2debug("tokens=%1", tokens) deep_copy(tokens) end |
- (String) UnEscapeString(_in, transform)
Escape reserved characters in string used as a part of URL (e.g. '%25' => '%', '%40' => '@'…)
URL::UnEscapeString (“http%3a%2f%2fsome.nice.url%2f%3awith%3a%2f%24p#ci%26l%2fch%40rs%2f”, URL::transform_map_passwd) -> some.nice.url/:with:/$p#ci&l/ch@rs/
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File '../../src/modules/URL.rb', line 115 def UnEscapeString(_in, transform) transform = deep_copy(transform) return "" if _in == nil || _in == "" # replace the other reserved characters Builtins.foreach(transform) do |tgt, src| # replace both upper and lower case escape sequences _in = String.Replace(_in, Builtins.tolower(src), tgt) _in = String.Replace(_in, Builtins.toupper(src), tgt) end # replace % at the end _in = String.Replace(_in, "%25", "%") _in end |