Class

class Form

public class Form {
    public init()
    public init(queryComponent: String)
}

Description: Stores the form information of HTTP requests in key-value pairs. Generally, the form is the query component of the request URL.

A key can correspond to multiple values, which are stored as arrays. The ampersand & is used to separate multiple key-value pairs. The left part of the equal sign (=) is the key value, and the right part is the value. (There may be no equal sign (=) or the value may be empty)

init()

public init()

Description: Constructs a default Form instance.

init(String)

public init(queryComponent: String)

Description: Constructs a Form instance according to a query string encoded by URL, that is, the query component of the URL instance.

Parses the query string encoded by URL to obtain several key-value pairs and adds them to the newly constructed Form instance.

Parameters:

  • queryComponent: String: a string of the query component of URL, excluding the question mark (?) before the component

Throws:

func add(String, String)

public func add(key: String, value: String): Unit

Description: Adds a key-value mapping. If the key already exists, the value is added to the end of the original value array.

Parameters:

  • key: String: the specified key, which can be a new one
  • value: String: Adds the value to the value array corresponding to the specified key

func clone()

public func clone(): Form

Description: Clones Form

Returns:

func get(String)

public func get(key: String): Option<String>

Description: Obtains the first value according to the key.

Examples:

  • When the query component is a=b, form.get("a") obtains Some(b).
  • When the query component is a=, form.get("a") obtains Some().
  • When the query component is a, form.get("a") obtains Some().
  • When the query component is a, form.get("c") obtains None.

Parameters:

  • key: String: the specified key

Returns:

func getAll(String)

public func getAll(key: String): ArrayList<String>

Description: Obtains all values corresponding to a specified key.

Parameters:

  • key: String: the key specified by a user, which is used to obtain the corresponding value

Returns:

  • ArrayList<String>: the array corresponding to all values obtained according to the specified key If the specified key does not exist, an empty array is returned.

func isEmpty()

public func isEmpty(): Bool

Description: Checks whether Form is empty.

Returns:

  • Bool: If yes, true is returned. Otherwise, false is returned.

func remove(String)

public func remove(key: String): Unit

Description: Deletes a key and its value.

Parameters:

  • key: String: the key to be deleted

func set(String, String)

public func set(key: String, value: String): Unit

Description: Resets the value of a specified key.

Parameters:

  • key: String: the specified key
  • value: String: Sets the value of the specified key to value.

func toEncodeString()

public func toEncodeString(): String

Description: Encodes the key-value pairs in a form using percent-encoding.

Unreserved characters are not encoded. A space is encoded as '+'.

Note:

RFC 3986 defines unreserved characters as follows: unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"

Returns:

  • String: string after encoding

class URL

public class URL <: ToString {
    public init(scheme!: String, hostName!: String, path!: String)
}

Description: Provides functions used to parse URL and other related functions.

The content encoded by the percent sign (%) in a string is decoded and stored in the corresponding component. The initial value is stored in the corresponding raw property. The username and password (if any) in URL are also parsed according to RFC 3986.

Note:

RFC 3986 clearly states that user information stored in plaintext may be disclosed in any scenario. Therefore, it is recommended that user information not stored in plaintext in URLs.

Parent Type:

prop fragment

public prop fragment: ?String

Description: Obtains an anchor component after decoding, which is represented by a string.

Type: ?String

prop hostName

public prop hostName: String

Description: Obtains a host name after decoding, which is represented by a string.

Type: String

prop opaque

public prop opaque: String

Description: Obtains an opaque part in URL, which is represented by a string.

Type: String

prop path

public prop path: String

Description: Obtains a path after decoding, which is represented by a string.

Type: String

prop port

public prop port: String

Description: Obtains a port number, which is represented by a string. An empty string indicates that there is no port number.

Type: String

prop query

public prop query: ?String

Description: Obtains a query component after decoding, which is represented by a string.

Type: ?String

prop queryForm

public prop queryForm: Form

Description: Obtains a query component after decoding, which is represented by a Form instance.

Type: Form

prop rawFragment

public prop rawFragment: ?String

Description: Obtains an anchor component before decoding, which is represented by a string.

Type: ?String

prop rawPath

public prop rawPath: String

Description: Obtains a path before decoding, which is represented by a string.

Type: String

prop rawQuery

public prop rawQuery: ?String

Description: Obtains a query component before decoding, which is represented by a string.

Type: ?String

prop rawUserInfo

public prop rawUserInfo: UserInfo

Description: Obtains username and password information before decoding, which is represented by a UserInfo instance.

Type: UserInfo

prop scheme

public prop scheme: String

Description: Obtains the scheme in URL, which is represented by a string.

Type: String

prop userInfo

public prop userInfo: UserInfo

Description: Obtains username and password information after decoding, which is represented by a UserInfo instance.

Type: UserInfo

init(String, String, String)

public init(scheme!: String, hostName!: String, path!: String)

Description: Constructs a URL instance.

The following requirements must be met during instance construction:

  • If the hostName is specified, a scheme is required.
  • The instance cannot contain only one scheme parameter.
  • If the scheme and path exist, the path must be an absolute path.

Parameters:

  • scheme!: String: scheme type
  • hostName!: String: host name without a port number
  • path!: String: path of the requested resource

Throws:

  • UrlSyntaxException: If the constructed instance does not meet the requirements, this exception is thrown.

static func mergePaths(String, String)

public static func mergePaths(basePath: String, refPath: String): String

Description: Combines two paths.

Combination rule: Appends the reference path refPath to the last segment of the base path basePath. If refPath is an absolute path, the value after combination is the original value of refPath. If refPath is not an absolute path, it is appended to the end of the last slash (/) in basePath. All results are standardized (the dot (.), double dots (..), and consecutive slashes (/) in the path are optimized). For a specific behavior, refer to the following example. For details, refer to RFC 3986.

To combine URLs, use resolveURL.

Examples:

  • If /a/b/c and /d are combined, /d is output.
  • If /a/b/c and d are combined, /a/b/d is output.
  • If /a/b/ and d/e/../f are combined, /a/b/d/f is output.
  • If /a/b/c/ and ./../../g are combined, /a/g is output.

Parameters:

  • basePath: String: base path
  • refPath: String: reference path

Returns:

  • String: the path after combination and standardization

static func parse(String)

public static func parse(rawUrl: String): URL

Description: Parses an original URL string into a URL object.

This function decomposes URL by component, decodes it, and stores it in the corresponding component property. The rawXXX properties (URL properties whose prefix is raw) store the original values which are not encoded or decoded.

For examples, see "Usage of the URL Parsing Function parse" (./../url_samples/url_parse.md#url-Usage of the Parsing Function parse).

Note:

This function can be used to parse the username and password (if any) in the URL, and it complies with the parsing function of RFC 3986. However, RFC 3986 also clearly states that user information stored in plaintext may be disclosed in any scenario. Therefore, it is recommended that user information not stored in plaintext in URLs.

Parameters:

Returns:

  • URL: the URL instance obtained after the string is parsed

Throws:

func isAbsoluteURL()

public func isAbsoluteURL(): Bool

Description: Checks whether URL is an absolute URL (when a scheme exists, URL is an absolute URL).

Returns:

  • Bool: If a scheme exists, true is returned. Otherwise, false is returned.

func replace(Option<String>, Option<String>, Option<String>, Option<String>, Option<String>, Option<String>, Option<String>)

public func replace(scheme!: Option<String> = None, userInfo!: Option<String> = None,
 hostName!: Option<String> = None, port!: Option<String> = None, path!: Option<String> = None, 
 query!: Option<String> = None, fragment!: Option<String> = None): URL

Description: Replaces the components of a URL object and returns a new URL object.

The following conditions must be met during replacement:

  • If the scheme is empty, the host name must be empty.
  • If the host name is empty, the user information or port number must be empty.
  • If the scheme is not empty, the host name and path cannot be empty at the same time.
  • If the scheme is not empty, the path must be an absolute path.
  • All components must be valid characters.

Parameters:

Returns:

Throws:

  • UrlSyntaxException: If the conditions for replacement are not met, this exception is thrown.

func resolveURL(URL)

public func resolveURL(ref: URL): URL

Description: Generates a new URL instance with the current URL instance as the basic URL instance according to RFC 3986 and by referring to the passed URL instance.

For example, as shown below, http://a/b/c/d;p?q is the basic URL, the reference URL is on the left of the equal sign (=), and the new URL is on the right of the equal sign (=).

For details about URL generation rules, see RFC 3968.

Parameters:

  • ref: URL: reference URL object

Returns:

func toString()

public func toString(): String

Description: Obtains the string value of the current URL instance.

The hostName is encoded, and the other part is the value of the rawXXX property (the URL property whose prefix is raw). The value is combined based on the URL component composition sequence to obtain the return value of the function.

Returns:

  • String: string value of the current URL instance

class UserInfo

public class UserInfo <: ToString {
    public init()
    public init(userName: String)
    public init(userName: String, passWord: String)
    public init(userName: String, passWord: Option<String>)
}

Description: Indicates the username and password information in a URL.

Note:

RFC 3986 clearly states that user information stored in plaintext may be disclosed in any scenario. Therefore, it is recommended that user information not stored in plaintext in URLs.

Parent Type:

init()

public init()

Description: Creates a UserInfo instance.

init(String)

public init(userName: String)

Description: Creates a UserInfo instance according to the username.

Parameters:

init(String, Option<String>)

public init(userName: String, passWord: Option<String>)

Description: Creates a UserInfo instance according to the username and password. Parameters:

init(String, String)

public init(userName: String, passWord: String)

Description: Creates a UserInfo instance according to the username and password. Parameters:

func password()

public func password(): Option<String>

Description: Obtains the password.

Note:

RFC 3986 clearly states that user information stored in plaintext may be disclosed in any scenario. Therefore, it is recommended that user information not stored in plaintext in URLs.

Returns:

func toString()

public func toString(): String

Description: Converts the current UserInfo instance to a string.

Returns:

func username()

public func username(): String

Description: Obtains the username.

Returns:

  • String: the username in string format