The built-in functions in the Web package offers a set of Web-related format translation and file services. The major functionalities offered in the Web Package are the following:

  • File(), Write(), and WriteHeaders() to write web content to a file or socket.
  • Escape() and Unescape() to convert a String to and from URL-encoded format.
  • EscapeJSON() and UnescapeJSON() to convert a String to and from JSON-encoded format.
  • ToJSON() and FromJSON() to convert a value to and from JSON-encoded string.
  • EscapeForJS() to convert a string to the format that is compatible with the JavaScript escape functions.
  • EncodeForURL() and DecodeForURL() to convert a String to and from new URL-encoded format.
  • EscapeHTML() to convert a string to HTML format.
  • EscapeXML() to convert a string to XML compatible format.
  • Format() to substitute parameters into a format string with various escaping options.
  • CookieEncode()(deprecated) and CookieDecode()(deprecated) to encode or decode a cookie.

  • Class Attributes Index

     o CRLF
    A String consisting of carriage return (ASCII 13) and line feed (ASCII 10).
     o JS_ESCAPE
    An integer that is used for selecting the JavaScript escape() function.
     o JS_ENCODEURI
    An integer that is used for selecting the JavaScript encodeURI() function.
     o JS_ENCODEURICOMPONENT
    An integer that is used for selecting the JavaScript encodeURIComponent() function.

    Class Methods Index

     o CookieDecode( String cookie, String ipAddress, [Integer salt] )
    Deprecated - Decodes a string which was encoded using CookieEncode.
     o CookieEncode( String cookie, String ipAddress, [Integer salt] )
    Deprecated - Encodes a cookie for security purposes.
     o DecodeForURL( String value )
    Returns an unescaped decoding of the specified new URL-encoded String.
     o EncodeForURL( String value )
    Returns a new URL-encoded conversion of the specified String by converting non-ASCII characters to UTF-8 then escaping non-alphanumeric characters.
     o Escape( String value )
    Returns a URL-encoded conversion of the specified String by escaping non-alphanumeric characters.
     o EscapeForJS( String value, Integer mode )
    Returns a new encoded conversion of the specified String by converting some non-ASCII characters, depending on the mode selected, to UTF-8 then escaping non-alphanumeric characters.
     o EscapeJSON( String value )
    Returns a copy of the given string that has been escaped for JSON.
     o EscapeHTML( String value )
    Converts a specified string to HTML format by escaping special characters.
     o EscapeXML( String value )
    Converts a specified string to XML compatible format by escaping special characters.
     o File( Dynamic context, String filePath, [String mimeType] )
    Writes the specified file to the output context.
     o Format( String format, [List value] )
    Formats a list of values based on a specified formatting string.
     o FromJSON( String value )
    Deserializes a value from a given JSON string.
     o ToJSON( Dynamic value )
    Serializes the given value to a JSON string.
     o Unescape( String value )
    Returns an unescaped decoding of the specified URL-encoded String.
     o UnescapeJSON( String value )
    Returns a copy of the given JSON string that has been unescaped.
     o Write( Dynamic context, Dynamic value, [List valueList] )
    Writes the specified value to the output context.
     o WriteHeaders( Dynamic context, String status, String headers )
    Writes a valid HTTP status line and headers to the output context.

    Class Attributes

     o CRLF
     String CRLF
    

    A String consisting of carriage return (ASCII 13) and line feed (ASCII 10). It is generally used with HTTP headers.

     o JS_ESCAPE
     String JS_ESCAPE
    

    An integer (1) that is used for selecting the JavaScript escape() function.

     o JS_ENCODEURI
     String JS_ENCODEURI
    

    An integer (2) that is used for selecting the JavaScript encodeURI() function.

     o JS_ENCODEURICOMPONENT
     String JS_ENCODEURICOMPONENT
    

    An integer (3) that is used for selecting the JavaScript encodeURIComponent() function.

    Class Methods

     o CookieDecode
     String CookieDecode(
                   String cookie,
                   String ipAddress,
                  [Integer salt] ) deprecated
    

    This is a deprecated function and will be removed in a future version. Decodes a string which was encoded using CookieEncode. The ipAddress and salt arguments must contain the same values as were passed to CookieEncode.

    Parameters:
    cookie  -  The cookie string to be decoded.
    ipAddress  -  The IP address of the workstation from which this cookie was received.
    salt  -  An optional unique integer; must be the same as was passed to CookieEncode.
    Returns:
    The decoded cookie string.
     o CookieEncode
     String CookieEncode(
                   String cookie,
                   String ipAddress,
                  [Integer salt] ) deprecated
    

    This is a deprecated function and will be removed in a future version. Encodes a cookie for security purposes.

    Parameters:
    cookie  -  The cookie string to be encoded.
    ipAddress  -  The IP address of the workstation to which this cookie will be sent.
    salt  -  An optional unique integer which is factored into the encoding algorithm.
    Returns:
    The encoded cookie string.
     o DecodeForURL
     String DecodeForURL(
                 String value )
    

    Decodes a previously encoded string. The plus-sign character ("+") will be converted to a space and hexadecimal non-alphanumeric characters (represented by "%xx", where xx is hexadecimal notation) will be converted to their ASCII equivalent. In addition if the server is not using UTF-8, the UTF-8 encoded characters will be converted.

    Parameters:
    value  -  The String to be unescaped.
    Returns:
    The converted string.

    Here is an example:

     echo( Web.Unescape ( "http%3A%2F%2Fjoe%2Efred%2Ecom" ) )

    The output of the example is:

    http://joe.fred.com
     o EncodeForURL
     String EncodeForURL(
                String value )
    

    Converts a specified String to new URL format. First represent each non-ASCII character in UTF-8 then by escaping non-alphanumeric characters into URL hexadecimal sequences and spaces in to the plus-sign ("+") character.

    Parameters:
    value  -  The string to convert.
    Returns:
    The converted string.

    When passing arguments to a CGI script, this function can be used to make the arguments URL compatible by escaping non-alphanumeric characters to a format recognizable by a CGI script. This is equivalent to the Javascript EncodeURIComponent and to the Java URLDecoder.encode(). Here is an example:

    String	text = "A ü URL_example"
    String	site = "http://www.bogus.com/somefile"
    String	query = "/search/query?site=%1&text=%2"
    
    Echo( "Escaped String:	", Web.EncodeForURL( text ) )
    Echo( "Encoded URL:	", Str.Format( query, Web.EncodeForURL( site ), web.EncodeForURL( text ) ) )

    The output is:

    Escaped String:	A+%C3%BC+URL_example
    Escaped URL:	/search/query?site=http%3A%2F%2Fwww%2Ebogus%2Ecom%2Fsomefile&text=A+%C3%BC+URL_example
    
     o Escape
     String Escape(
                String value )
    

    Converts a specified String to URL format by escaping non-alphanumeric characters into URL hexadecimal sequences and spaces in to the plus-sign ("+") character.

    Parameters:
    value  -  The string to convert.
    Returns:
    The converted string.

    When passing arguments to a CGI script, this function can be used to make the arguments URL compatible by escaping non-alphanumeric characters to a format recognizable by a CGI script. Here is an example:

    String	text = "A URL_example"
    String	site = "http://www.bogus.com/somefile"
    String	query = "/search/query?site=%1&text=%2"
    
    Echo( "Escaped String:	", Web.Escape( text ) )
    Echo( "Escaped URL:	", Str.Format( query, Web.Escape( site ), web.Escape( text ) ) )

    The output is:

    Escaped String:	A+URL%5Fexample
    Escaped URL:	/search/query?site=http%3A%2F%2Fwww%2Ebogus%2Ecom%2Fsomefile&text=A+URL%5Fexample
    
     o EscapeForJS
     String EscapeForJS(
                  String value,
                  Integer mode )
    

    Converts a specified string to the format that is compatible with one of the JavaScript escape methods: escape(), escapeURI, and escapeURIComponent.

    Parameters:
    value  -  The string to be escaped.
    mode  -  The mode for selecting which JavaScript escape fuction to encode the string specified.
    mode = WEB.JS_ESCAPE for JavaScript's escape(), which does not encode: @*/+
    mode = WEB.JS_ENCODEURI for JavaScript's encodeURI(), which does not encode: ~!@#$&*()=:/,;?+'
    mode = WEB.JS_ENCODEURICOMPONENT JavaScript's encodeURIComponent(), which does not encode: ~!*()'.
    Returns:
    The converted string.

    Here are some examples:

    Echo( Web.EscapeForJS('"joe+B,smith"@example.com', WEB.JS_ESCAPE) )
    Echo( Web.EscapeForJS('"joe+B,smith"@example.com', WEB.JS_ENCODEURI) )
    Echo( Web.EscapeForJS('"joe+B,smith"@example.com', WEB.JS_ENCODEURICOMPONENT ) )

    The outputs are:

    %22joe+B%2Csmith%22@example%2Ecom
    %22joe+B,smith%22@example%2Ecom
    %22joe%2BB%2Csmith%22%40example%2Ecom

     

     o EscapeJSON
     String EscapeJSON(
                String value )
    

    Converts a specified String to JSON format.

    Parameters:
    value  -  The string to convert.
    Returns:
    The converted string.
     o EscapeHTML
     String EscapeHTML(
                  String value )
    

    Converts a specified string to HTML format by escaping special characters, such as greater-than, less-than, and ampersand.

    Parameters:
    value  -  The string to be escaped.
    Returns:
    The converted string.

    Here is an example:

    Echo( Web.EscapeHTML("<Hello>") )

    The output is:

    &lt;Hello&gt;

     

     o EscapeXML
     String EscapeXML(
                  String value )
    

    Converts a specified string to XML compatible format by escaping special characters, such as greater-than, less-than, ampersand and apostrophe.
    The difference between EscapeHTML and EscapeXML is a single quotation ' (or apostrophe) is escaped as &apos;

    Parameters:
    value  -  The string to be escaped.
    Returns:
    The converted string.

    Here is an example:

    Echo( Web.EscapeXML("<JOE>"This '&' That"</JOE>") )

    The output is:

    &lt;JOE&gt;&quot;This &apos;&amp;&apos; That&quot;&lt;/JOE&gt;

     

     o File
     Boolean File(
                Dynamic context,
                String filePath,
               [String mimeType] )
    

    Writes the specified file to the output context. The output context may either be a socket, which will then send the file to the Web browser, or a file.

    Parameters:
    context  -  The output context.
    filePath  -  The server file path of the file to be sent to the output context.
    mimeType  -  An optional data string specifying the MIME type of the data in filePath. If this parameter is specified, the HTTP headers are also sent as part of the file.
    Returns:
    TRUE if successful; FALSE if not.

    Here is a short example:

    File	fin = File.Open( "in.txt", File.WriteMode )
    File	fout = File.Open( "out.txt", File.WriteMode )
    
    File.Write( fin, "Some string of text data." )
    File.Close( fin )
    
    Web.File( fout, "in.txt", "text/plain" )
    File.Close( fout )

    The output for the example in file "out.txt" is the following (where each end-of-line before the file data, "Some string..." is actually a Web.CRLF):

    HTTP/1.0 200 
    Content-Type: text/plain
    
    Some string of text data.
     o Format
     String Format(
                String format,
               [List substitutions] )
    

    Web-formats a list of values based on a specified formatting string. This is useful for generating HTML, since Format() applies either HTML-escaping, URL-escaping, or no escaping (literal) to the substitution strings upon substitution. Formatting is marked in the format string by the backtick (`), instead of the percent symbol (%) as usual, since the percent symbol is used to escape URLs.

    Parameters:
    format  - 

    The text and placeholders. Format strings are:

    Value   Description
    `   A backtick (`), consumes no substitution String.
    `L   Literal (String is not escaped).
    `H   HTML (calls EscapeHTML() on the String)
    'U   URL (calls Escape() on the String)
    substitutions  -  A list of Strings to substitute into the format.
    Returns:
    The formatted String.

    A maximum of 20 format specifications can be made. Here is a short example:

    Echo( Web.Format( "` `L `H `U", {"<Joe1>", "<Joe2>", "<Joe3>"} ) ) )

    The output is:

    ` <Joe1> &lt;Joe2&gt; %3CJoe3%3E

     

     o FromJSON
     Dynamic FromJSON(
                String value )
    

    Deserializes an object from the specified string in JSON format.

    Parameters:
    value  -  The string to deserialize from JSON.
    Returns:
    The deserialized object.
     o ToJSON
     String ToJSON(
                Dynamic value )
    

    Serializes the specified object to JSON format.

    Parameters:
    value  -  The object to serialize to JSON.
    Returns:
    The JSON representation of the object.
     o Unescape
     String Unescape(
                 String value )
    

    Unescapes a previously escaped string. The plus-sign character ("+") will be converted to a space and hexadecimal non-alphanumeric characters (represented by "%xx", where xx is hexadecimal notation) will be converted to their ASCII equivalent.

    Parameters:
    value  -  The String to be unescaped.
    Returns:
    The converted string.

    Here is an example:

     echo( Web.Unescape ( "http%3A%2F%2Fjoe%2Efred%2Ecom" ) )

    The output of the example is:

    http://joe.fred.com
     o UnescapeJSON
     String UnescapeJSON(
                String value )
    

    Converts a specified String from JSON format.

    Parameters:
    value  -  The string to convert.
    Returns:
    The converted string.
     o Write
     Boolean Write(
                Dynamic context,
                Dynamic value,
               [List valueList] )
    

    Writes the specified value to the output context. The output context may either be a socket, as connected to a Web browser, or a file.

    Parameters:
    context  -  The output context.
    value  -  The value to write. The value must be type Bytes or String.
    valueList  -  An optional format. If this parameter is specified, the second and third parameters are passed to the Web.Format function and the resulting string is written to the output context.
    Returns:
    TRUE if successful; FALSE if not.

    Here is a short example of File.Write() and File.WriteHeaders():

    String	fname = "c:\tmp\s.txt"
    File	f = File.Open( fname, File.WriteMode )
    String	headers = "Content-type: text/plain" + Web.CRLF + \
                      "Cookie: This is a cookie" + Web.CRLF
    
    Web.WriteHeaders( f, "s.txt", headers )
    Web.Write( f, "This is some text." )
    File.Close( f )
    
    Echo( Str.FileToString( fname ) )

    The output of the example is:

    HTTP/1.0 s.txt
    Content-type: text/plain
    Cookie: This is a cookie
    
    This is some text.
     o WriteHeaders
     Boolean WriteHeaders(
                    Dynamic context,
                    String status,
                    String headers )
    

    Writes a valid HTTP status line and headers to the output context (either a socket or a file).

    Parameters:
    context  -  The output context.
    status  -  The return header status.
    headers  -  Additional HTTP headers; this must include carriage returns and line feeds (Web.CRLF).
    Returns:
    TRUE if successful; FALSE if not.

    See File.Write() for an example.