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:
String CRLF
A String consisting of carriage return (ASCII 13) and line feed (ASCII 10). It is generally used with HTTP headers.
JS_ESCAPEString JS_ESCAPE
An integer (1) that is used for selecting the JavaScript escape() function.
JS_ENCODEURIString JS_ENCODEURI
An integer (2) that is used for selecting the JavaScript encodeURI() function.
JS_ENCODEURICOMPONENTString JS_ENCODEURICOMPONENT
An integer (3) that is used for selecting the JavaScript encodeURIComponent() function.
CookieDecodeString 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.
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. |
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.
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. |
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.
value | - | The String to be unescaped. |
Here is an example:
echo( Web.Unescape ( "http%3A%2F%2Fjoe%2Efred%2Ecom" ) )
The output of the example is:
http://joe.fred.com
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.
value | - | The string to convert. |
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
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.
value | - | The string to convert. |
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
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.
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: ~!*()'. |
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
String EscapeJSON( String value )
Converts a specified String to JSON format.
value | - | The string to convert. |
String EscapeHTML( String value )
Converts a specified string to HTML format by
escaping special characters, such as greater-than, less-than, and ampersand.
value | - | The string to be escaped. |
Here is an example:
Echo( Web.EscapeHTML("<Hello>") )
The output is:
<Hello>
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 '
value | - | The string to be escaped. |
Here is an example:
Echo( Web.EscapeXML("<JOE>"This '&' That"</JOE>") )
The output is:
<JOE>"This '&' That"</JOE>
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.
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. |
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.
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.
format | - |
The text and placeholders. Format strings are:
|
|||||||||||||||
substitutions | - | A list of Strings to substitute into the format. |
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> <Joe2> %3CJoe3%3E
Dynamic FromJSON( String value )
Deserializes an object from the specified string in JSON format.
value | - | The string to deserialize from JSON. |
String ToJSON( Dynamic value )
Serializes the specified object to JSON format.
value | - | The object to serialize to JSON. |
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.
value | - | The String to be unescaped. |
Here is an example:
echo( Web.Unescape ( "http%3A%2F%2Fjoe%2Efred%2Ecom" ) )
The output of the example is:
http://joe.fred.com
String UnescapeJSON( String value )
Converts a specified String from JSON format.
value | - | The string to convert. |
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.
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. |
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.
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).
context | - | The output context. |
status | - | The return header status. |
headers | - | Additional HTTP headers; this must include carriage returns and line feeds (Web.CRLF). |
See File.Write() for an example.