Class: Yast::URLClass
- Inherits:
-
Module
- Object
- Module
- Yast::URLClass
- Defined in:
- library/types/src/modules/URL.rb
Overview
This is legacy code ported from the
A module for dealing with URLs
YCP era. Its use is highly discouraged in favor of the URI standard library or the new Y2Packager::ZyppUrl (available from SLE-15-SP3 on) when working with libzypp URLs.
Instance Method Summary collapse
-
#Build(tokens) ⇒ String
Build URL from tokens as parsed with Parse.
-
#Check(url) ⇒ Object
Check URL.
-
#EscapeString(in_, transform) ⇒ String
Escape reserved characters in string used as a part of URL (e.g. '%' => '%25', '@' => '%40'...).
-
#FormatURL(tokens, len) ⇒ Object
- Format URL - truncate the middle part of the directory to fit to the requested lenght.
-
#HidePassword(url) ⇒ String
Hide password in an URL - replaces the password in the URL by 'PASSWORD' string.
-
#HidePasswordToken(tokens) ⇒ Hash
Hide password token in parsed URL (by URL::Parse()) - the password is replaced by 'PASSWORD' string.
- #main ⇒ Object
-
#MakeMapFromParams(params) ⇒ Hash{String => String}
Reads list of HTTP params and returns them as map.
-
#MakeParamsFromMap(params_map) ⇒ Object
Returns string made of HTTP params.
-
#Parse(url) ⇒ Object
Tokenize URL.
-
#UnEscapeString(in_, transform) ⇒ String
Escape reserved characters in string used as a part of URL (e.g. '%25' => '%', '%40' => '@'...).
Instance Method Details
#Build(tokens) ⇒ String
Build URL from tokens as parsed with Parse
338 339 340 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 |
# File 'library/types/src/modules/URL.rb', line 338 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 userpass = Ops.add(userpass, "@") if Ops.greater_than(Builtins.size(userpass), 0) 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 = if IP.Check6(Ops.get_string(tokens, "host", "")) Builtins.sformat("%1[%2]", url, Ops.get_string(tokens, "host", "")) else Builtins.sformat("%1%2", url, Ops.get_string(tokens, "host", "")) end 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 url = if Ops.get_string(tokens, "scheme", "") == "ftp" Builtins.sformat( "%1/%%2f%2", url, Builtins.substring( URLRecode.EscapePath(Ops.get_string(tokens, "path", "")), 1 ) ) else 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 |
#Check(url) ⇒ Object
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 |
# File 'library/types/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 url.include?(" ") 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" return false if !Builtins.regexpmatch(Ops.get_string(tokens, "port", ""), "^[0-9]*$") # Check "user" : "name" # Check "pass" : "pass" # Check "query" : "question" # Check "fragment": "part" true end |
#EscapeString(in_, transform) ⇒ String
Escape reserved characters in string used as a part of URL (e.g. '%' => '%25', '@' => '%40'...)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'library/types/src/modules/URL.rb', line 142 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 |
#FormatURL(tokens, len) ⇒ Object
- 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("http://download.opensuse.org/very/log/path/which/will/be/truncated/target_file", 45) -> "http://download.opensuse.org/.../target_file"
- @example FormatURL("http://download.opensuse.org/very/log/path/which/will/be/truncated/target_file", 60) -> "http://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
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'library/types/src/modules/URL.rb', line 486 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 |
#HidePassword(url) ⇒ String
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.
610 611 612 613 614 615 616 617 618 619 |
# File 'library/types/src/modules/URL.rb', line 610 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 |
#HidePasswordToken(tokens) ⇒ Hash
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.
625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'library/types/src/modules/URL.rb', line 625 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 |
#main ⇒ Object
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 |
# File 'library/types/src/modules/URL.rb', line 41 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 |
#MakeMapFromParams(params) ⇒ Hash{String => String}
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().
535 536 537 538 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 |
# File 'library/types/src/modules/URL.rb', line 535 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 |
#MakeParamsFromMap(params_map) ⇒ Object
Returns string made of HTTP params. It's a reverse function to MakeMapFromParams(). Neither keys nor values are HTML-escaped, use EscapeString() if needed.
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'library/types/src/modules/URL.rb', line 584 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 |
#Parse(url) ⇒ Object
Tokenize URL
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 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 'library/types/src/modules/URL.rb', line 173 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:// # 2: user:[email protected]:23 # 3: /some/path # 4,5: ?question # 6,7: #fragment "^" \ "(([^:/?#]+):[/]{0,2})?" \ "([^/?#]*)?" \ "([^?#]*)?" \ "(\\?([^#]*))?" \ "(#(.*))?" ) 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, 7, "")) else Ops.set(tokens, "host", Ops.get_string(userpass, 5, "")) 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 || "") 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", "")) Ops.set(tokens, "domain", Ops.get(, "workgroup", "")) if Builtins.haskey(, "workgroup") end # merge host and path if the scheme does not allow a host (bsc#991935) tokens = merge_host_and_path(tokens) if SCHEMES_WO_HOST.include?(tokens["scheme"].downcase) Builtins.y2debug("tokens=%1", tokens) deep_copy(tokens) end |
#UnEscapeString(in_, transform) ⇒ String
Escape reserved characters in string used as a part of URL (e.g. '%25' => '%', '%40' => '@'...)
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'library/types/src/modules/URL.rb', line 117 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 String.Replace(in_, "%25", "%") end |