OScript API/Built-in Package Index

Class: Web

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:

Class Attributes

A String consisting of carriage return (ASCII 13) and line feed (ASCII 10).

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

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

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

Class Methods

BytesWritten( [Integer bytecount] )

Tracks the number of bytes written in the response to the browser.

CookieDecode( String cookie, String ipAddress, [Integer salt] ) Deprecated

Decodes a string which was encoded using CookieEncode.

CookieEncode( String cookie, String ipAddress, [Integer salt] ) Deprecated

Encodes a cookie for security purposes.

DecodeForURL( String value )

Returns an unescaped decoding of the specified new URL-encoded String.

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.

Escape( String value )

Returns a URL-encoded conversion of the specified String by escaping non-alphanumeric characters.

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.

EscapeHTML( String value )

Converts a specified string to HTML format by escaping special characters.

EscapeJSON( String value, [Boolean trimStrings] )

Returns a copy of the given string that has been escaped for JSON.

EscapeXML( String value )

Converts a specified string to XML compatible format by escaping special characters.

EscapeXMLNoCC( String value )

Converts a specified string to XML compatible format by escaping special and control characters.

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

Writes the specified file to the output context.

Format( String format, [List substitutions] )

Formats a list of values based on a specified formatting string.

FromJSON( String value )

Deserializes a value from a given JSON string.

ToJSON( Dynamic value, [Boolean trimStrings] )

Serializes the given value to a JSON string.

Unescape( String value )

Returns an unescaped decoding of the specified URL-encoded String.

UnescapeJSON( String value )

Returns a copy of the given JSON string that has been unescaped.

Write( Dynamic context, Dynamic value, [List valueList] )

Writes the specified value to the output context.

WriteHeaders( Dynamic context, String status, String headers )

Writes a valid HTTP status line and headers to the output context.

Class Attributes

String CRLF

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

String JS_ENCODEURI

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

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

String JS_ESCAPE

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

Class Methods

BytesWritten

Integer BytesWritten( [Integer bytecount] )

This function is used internally to track the size of the response written to the browser. An internal byte counter accumulates the number of bytes written by the Web.Write() function. This function returns the current value of the byte counter and reset the value of the byte counter with the optional parameter bytecount.

Parameters

bytecount

Optional parameter to set the byte counter to a new value

Returns:

Return the current byte count.

Example

        Integer responseSize

        // reset the byte counter to 0
        Web.BytesWritten( 0 )

        ...

        // process the request
        Web.Write( "This is the first response string" )
        Web.Write( "This is the second response string" )

        ...

        // retrieve the size of the response 
        responseSize = Web.BytesWritten()

        //responseSize should be 68

CookieDecode

Deprecated
String CookieDecode( String cookie,
                     String ipAddress,
                     [Integer salt] )
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.

CookieEncode

Deprecated
String CookieEncode( String cookie,
                     String ipAddress,
                     [Integer salt] )

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.

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.

Example

Here is an example:

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

The output of the example is:

http://joe.fred.com

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.

Example

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

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.

Example

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

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.

Example

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

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.

Example

Here is an example:

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

The output is:

&lt;Hello&gt;

EscapeJSON

String EscapeJSON( String value,
                   [Boolean trimStrings] )

Converts a specified String to JSON format.

Parameters

value

The string to convert.

trimStrings

TRUE if leading and trailing whitespace should be removed; FALSE otherwise. The default is TRUE.

Returns:

The converted string.

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.

Example

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;

EscapeXMLNoCC

String EscapeXMLNoCC( String value )

Converts a specified string to XML compatible format by escaping special and control characters, such as greater-than, less-than, ampersand and apostrophe. illegal control chars and del (0x7f) are replaced by substitute char 0xfffd

Parameters

value

The string to be escaped.

Returns:

The converted string.

Example

Here is an example: ^z means ctl-z hex 1A

Echo( Web.EscapeXMLNoCC("<&^z"))

The output is:

&lt;&amp;&#xfffd;

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.

Example

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.

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.

Example

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

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.

ToJSON

String ToJSON( Dynamic value,
               [Boolean trimStrings] )

Serializes the specified object to JSON format.

Parameters

value

The object to serialize to JSON.

trimStrings

TRUE if leading and trailing whitespace should be removed from String values; FALSE otherwise. The default is TRUE.

Returns:

The JSON representation of the object.

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.

Example

Here is an example:

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

The output of the example is:

http://joe.fred.com

UnescapeJSON

String UnescapeJSON( String value )

Converts a specified String from JSON format.

Parameters

value

The string to convert.

Returns:

The converted string.

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.

Example

Here is a short example of Web.Write() and Web.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.

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.

Example

See File.Write() for an example.

 Copyright © 2023 OpenText Corporation. All rights reserved.