280. URL

Manipulate and Parse URLs

280.1. Authors

  • Michal Svec <msvec@suse.cz>

  • Anas Nashif <nashif@suse.cz>

280.2. Summary of Module Globals

List of Global Functions

  • Build - Build URL from tokens as parsed with Parse

  • Check - Check URL

  • EscapeString - Escape reserved characters in string used as a part of URL (e.g. '%' => '%25', '@' => '%40'...)

  • FormatURL - Format URL - truncate the middle part of the directory to fit to the requested lenght.

  • HidePassword - 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.

  • HidePasswordToken - 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.

  • MakeMapFromParams - 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().

  • MakeParamsFromMap - Returns string made of HTTP params. It's a reverse function to MakeMapFromParams(). Neither keys nor values are HTML-escaped, use EscapeString() if needed.

  • Parse - Tokenize URL

  • UnEscapeString - Escape reserved characters in string used as a part of URL (e.g. '%25' => '%', '%40' => '@'...)

List of Global Variables

  • transform_map_filename - 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_passwd - 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_query - 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

280.3. Global Functions

280.3.1. Build

Build URL from tokens as parsed with Parse

Function parameters

  • map tokens

Return value

  • string - url, empty string if invalid data is used to build the url.

See also:

  • RFC 2396 (updated by RFC 2732)

  • also perl-URI: URI(3)

280.3.2. Check

Check URL

Function parameters

  • string url

Return value

  • boolean - true if correct

See also:

  • RFC 2396 (updated by RFC 2732)

  • also perl-URI: URI(3)

280.3.3. EscapeString

Escape reserved characters in string used as a part of URL (e.g. '%' => '%25', '@' => '%40'...)

Function parameters

  • string in

  • map<string,string> transform

Return value

  • string - escaped string

Example 139. 

	URL::EscapeString ("http://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


280.3.4. FormatURL

Format URL - truncate the middle part of the directory to fit to the requested lenght.

Function parameters

  • map tokens

  • integer len

Return value

  • string - Truncated URL

Example 140. 

 FormatURL("http://download.opensuse.org/very/log/path/which/will/be/truncated/target_file", 45)
&nbsp;&nbsp;&nbsp;&nbsp;-> "http://download.opensuse.org/.../target_file"
 FormatURL("http://download.opensuse.org/very/log/path/which/will/be/truncated/target_file", 60)
&nbsp;&nbsp;&nbsp;&nbsp;-> "http://download.opensuse.org/very/.../be/truncated/target_file"


See also:

  • Parse should be used to convert URL string to a map (tokens parameter)

280.3.5. HidePassword

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.

Function parameters

  • string url

Return value

  • string - new URL with 'PASSWORD' password or unmodified URL if there is no password

280.3.6. HidePasswordToken

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.

Function parameters

  • map tokens

Return value

  • map - map with replaced password

280.3.7. MakeMapFromParams

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().

Function parameters

  • string params

Return value

  • map <string, string> - params

Example 141. 

      MakeMapFromParams ("device=sda3&login=aaa&password=bbb") -> $[
              "device"   : "sda3",
              "login"    : "aaa",
              "password" : "bbb"
      ]


280.3.8. MakeParamsFromMap

Returns string made of HTTP params. It's a reverse function to MakeMapFromParams(). Neither keys nor values are HTML-escaped, use EscapeString() if needed.

Function parameters

  • map <string, string> params_map

Return value

  • string

Example 142. 

   MakeMapFromParams ($[
     "param1" : "a",
     "param2" : "b",
     "param3" : "c",
   ]) -> "param1=a&param2=b&param3=c"


See also:

  • MakeMapFromParams

280.3.9. Parse

Tokenize URL

Function parameters

  • string url

Return value

  • map - URL split to tokens

Example 143. 

 Parse("http://name:pass@www.suse.cz:80/path/index.html?question#part") ->
     $[
         "scheme"  : "http",
         "host"    : "www.suse.cz"
         "port"    : "80",
         "path"    : /path/index.html",
         "user"    : "name",
         "pass"    : "pass",
         "query"   : "question",
         "fragment": "part"
     ]


280.3.10. UnEscapeString

Escape reserved characters in string used as a part of URL (e.g. '%25' => '%', '%40' => '@'...)

Function parameters

  • string in

  • map<string,string> transform

Return value

  • string - unescaped string

Example 144. 

	URL::UnEscapeString ("http%3a%2f%2fsome.nice.url%2f%3awith%3a%2f%24p#ci%26l%2fch%40rs%2f", URL::transform_map_passwd)
		-> http://some.nice.url/:with:/$p#ci&l/ch@rs/


280.4. Global Variables

280.4.1. transform_map_filename

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

280.4.2. transform_map_passwd

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

280.4.3. transform_map_query

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

280.5. Module Requirements

280.5.1. Module Imports

  • Hostname
  • IP
  • String

280.5.2. Module Includes