Written from a line-by-line source review; every example output is from a real run.
Introduction
The web plugin provides web text-processing helpers: HTML and URL encoding, Base64 encoding, parsing of raw HTTP requests, handling of the Content-Type header and MIME types, and assembling complete HTTP responses. Its typical use is building a simple web server or web service in DominScript: you split the incoming request apart with the request parsers and produce the response with the response builders. The plugin is stateless — every call is standalone.
Text and blob
Most functions work with text (string), but the raw HTTP data — the incoming request and the outgoing response — is a blob, because it can contain arbitrary bytes (even 0 bytes). So the HTTP request parsers take a blob (the raw request), and the response builders return a blob (the finished, byte-exact response you can send directly).
Loading the plugin
plugin "../plugins/print/PrintPlugin"; plugin "../plugins/web_util/WebUtilPlugin";
A typical flow — serving a request
// $req is the incoming raw request (a blob) string $path[256]; blob $resp[1024]; $path = web.HttpRequestPath($req); // ... decide based on $path ... $resp = web.HttpTextResponse("200 OK", "text/plain", "Hello"); // send $resp back to the client
What to know about every function (the basics)
The plugin is stateless and standalone: no open/close, no handle. The functions are thread-safe.
The question-like (Is*) functions and ContentTypeMatches return an integer: 1 = true, 0 = false.
The parsers that return text (HttpHeaderValue, CookieValue, UrlFormValue, ContentTypeParam, etc.) return an empty string on no match — they do not fail.
An error (stopping the script) is caused only by a wrong argument count or type; Base64 decoding also raises an error for invalid input.
HTML escaping replaces the & < > " ' characters; UrlEncode encodes per RFC 3986 (a space becomes %20, not +).
The HTTP request parsers split apart the raw request (a blob); if the requested part is absent they return an empty string.
How to read the signatures
request is a raw HTTP request blob, contentType is the text of a Content-Type header. The type after the -> arrow is the return type. For example, web.HttpHeaderValue(request, name) -> string takes a request blob and a header name and returns the header's value as text.
HTML processing
Safely escaping and unescaping HTML text.
web.HtmlEscape
web.HtmlEscape(text) -> string
Escapes the characters that are special in HTML (& < > " ') into their entities.
| Parameter | Type | Description |
|---|---|---|
| text | string | The text to escape. |
The escaped text (for example < becomes <).
printf("%s\n", web.HtmlEscape("a<b>&\"c\""));a<b>&"c"
Before inserting user text into HTML: this protects against HTML injection (XSS), because the control characters are not interpreted as code.
web.HtmlUnescape
web.HtmlUnescape(text) -> string
Unescapes HTML entities back into the original characters.
| Parameter | Type | Description |
|---|---|---|
| text | string | The text to unescape, containing entities. |
The unescaped text (for example < becomes <).
printf("%s\n", web.HtmlUnescape("a<b>&c"));a<b>&c
To recover the original form of text extracted from HTML — for example when processing a value read from an HTML attribute or text node.
URL encoding
Converting text to and from a URL-safe (percent-encoded) form.
web.UrlEncode
web.UrlEncode(text) -> string
Encodes the text into a form safely embeddable in a URL (percent-encoding per RFC 3986).
| Parameter | Type | Description |
|---|---|---|
| text | string | The text to encode. |
The encoded text (unsafe bytes as %XX, a space as %20).
printf("%s\n", web.UrlEncode("a b&c=d"));a%20b%26c%3Dd
To assemble URL parameters or path segments from user data, so special characters (&, =, space) do not break the URL's structure.
web.UrlDecode
web.UrlDecode(text) -> string
Decodes URL-encoded (percent-encoded) text back to its original form.
| Parameter | Type | Description |
|---|---|---|
| text | string | The text to decode, containing %XX sequences. |
The decoded text.
printf("%s\n", web.UrlDecode("a%20b%26c%3Dd"));a b&c=d
To restore the original form of a value extracted from a URL or form data (the counterpart of UrlEncode). For some fields UrlFormValue does this automatically.
Base64
Converting data to and from Base64 text. Separate variants for text and blob.
web.Base64EncodeText
web.Base64EncodeText(text) -> string
Encodes a text into Base64 text.
| Parameter | Type | Description |
|---|---|---|
| text | string | The text to encode. |
The Base64-encoded text.
printf("%s\n", web.Base64EncodeText("hello"));aGVsbG8=
To put text into a form safely transportable over a text channel (for example in a header or a JSON field).
web.Base64EncodeBlob
web.Base64EncodeBlob(data) -> string
Encodes a blob's raw bytes into Base64 text.
| Parameter | Type | Description |
|---|---|---|
| data | blob | The binary data to encode. |
The Base64-encoded text.
blob $B[8];
$B = blob.FromText("hi");
printf("%s\n", web.Base64EncodeBlob($B));aGk=
To put binary data (an image, a file, a hash) into text form — for example to embed in a data: URI or a text protocol.
web.Base64DecodeToText
web.Base64DecodeToText(text) -> string
Decodes a Base64 text and returns the result as text.
| Parameter | Type | Description |
|---|---|---|
| text | string | The Base64 text to decode. |
The decoded text. Invalid Base64 causes a runtime error. (A 0 byte becomes the ? character.)
printf("%s\n", web.Base64DecodeToText("aGVsbG8="));hello
To decode textual Base64 content. If the decoded data is binary (may contain 0 bytes), Base64DecodeToBlob is correct, since it does not truncate.
web.Base64DecodeToBlob
web.Base64DecodeToBlob(text) -> blob
Decodes a Base64 text into a raw blob.
| Parameter | Type | Description |
|---|---|---|
| text | string | The Base64 text to decode. |
The decoded raw bytes as a blob. Invalid Base64 causes a runtime error.
blob $B[16];
$B = web.Base64DecodeToBlob("aGk=");
printf("%d\n", $B.Length);2
To decode binary Base64 content (an image, a file), where the 0 byte must be preserved. The counterpart of Base64EncodeBlob.
HTTP request parsing
Extracting the parts of a raw HTTP request (a blob): the request line, the method, the path, the headers, a cookie, and the body.
web.HttpRequestLine
web.HttpRequestLine(request) -> string
Extracts the first line of the request (the request line).
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
The request line (for example “GET /path HTTP/1.1”).
blob $R[256];
$R = blob.FromText("GET /p?q=1 HTTP/1.1\r\nHost: x\r\n\r\n");
printf("%s\n", web.HttpRequestLine($R));GET /p?q=1 HTTP/1.1
For a quick overview or logging of the request. For detailed processing, use the more targeted HttpRequestMethod/Path/Query functions.
web.HttpRequestMethod
web.HttpRequestMethod(request) -> string
Extracts the request's HTTP method.
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
The method (for example “GET”, “POST”).
blob $R[256];
$R = blob.FromText("GET /p HTTP/1.1\r\n\r\n");
printf("%s\n", web.HttpRequestMethod($R));GET
To branch by the request's kind: for example read on GET, write on POST.
web.HttpRequestTarget
web.HttpRequestTarget(request) -> string
Extracts the request's full target (path and query together).
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
The full target (for example “/path?q=1&x=2”).
blob $R[256];
$R = blob.FromText("GET /path?q=1&x=2 HTTP/1.1\r\n\r\n");
printf("%s\n", web.HttpRequestTarget($R));/path?q=1&x=2
When you need the path and query together. Separately, HttpRequestPath and HttpRequestQuery give them.
web.HttpRequestPath
web.HttpRequestPath(request) -> string
Extracts the request's path (without the query part).
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
The path (the part before the ?, for example “/path/page”).
blob $R[256];
$R = blob.FromText("GET /path/page?q=1 HTTP/1.1\r\n\r\n");
printf("%s\n", web.HttpRequestPath($R));/path/page
For routing: based on the path you decide which handler serves the request.
web.HttpRequestQuery
web.HttpRequestQuery(request) -> string
Extracts the request's query part (the part after the ?).
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
The query part (for example “q=1&x=2”), or an empty string if there is none.
blob $R[256];
$R = blob.FromText("GET /p?q=1&x=2 HTTP/1.1\r\n\r\n");
printf("%s\n", web.HttpRequestQuery($R));q=1&x=2
To process the query parameters. You read an individual parameter's value out of it with UrlFormValue.
web.HttpRequestBodyText
web.HttpRequestBodyText(request) -> string
Extracts the request's body as text (the part after the headers).
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
The request body as text.
blob $R[256];
$R = blob.FromText("POST /p
HTTP/1.1\r\n\r\nname=Ada&age=36");
printf("%s\n", web.HttpRequestBodyText($R));name=Ada&age=36
To process the content of POST/PUT requests (form data, JSON). An individual field of an urlencoded form is read by UrlFormValue.
web.HttpHeaderValue
web.HttpHeaderValue(request, name) -> string
Extracts the value of a given header from the request.
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
| name | string | The header's name (case-insensitive). |
The header's value, or an empty string if there is no such header.
blob $R[256];
$R = blob.FromText("GET / HTTP/1.1\r\nHost:
example.com\r\n\r\n");
printf("%s\n", web.HttpHeaderValue($R, "Host"));example.com
To read any header (Host, Authorization, Content-Length, etc.). A non-existent header gives an empty string, so its presence can be checked with an empty test.
web.HttpCookieValue
web.HttpCookieValue(request, name) -> string
Extracts the value of a given cookie directly from the request.
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
| name | string | The cookie's name. |
The cookie's value, or an empty string if there is no such cookie.
blob $R[256];
$R = blob.FromText("GET / HTTP/1.1\r\nCookie: sid=abc;
theme=dark\r\n\r\n");
printf("%s\n", web.HttpCookieValue($R, "theme"));dark
To read a session id or a setting from the request in one step. (CookieValue works directly from the text of the Cookie header.)
web.HttpRequestUsesChunked
web.HttpRequestUsesChunked(request) -> int
Tells whether the request uses chunked transfer encoding.
| Parameter | Type | Description |
|---|---|---|
| request | blob | The raw HTTP request. |
1 if the request is Transfer-Encoding: chunked; otherwise 0.
blob $R[256];
$R = blob.FromText("GET / HTTP/1.1\r\nHost: x\r\n\r\n");
printf("%d\n", web.HttpRequestUsesChunked($R));0
To read the request body correctly: with chunked, the body arrives in pieces and must be processed differently from a Content-Length-based one.
Cookie, form, Content-Type and MIME
Reading cookie and urlencoded-form fields, parsing the Content-Type header, handling MIME types, and building a header line.
web.CookieValue
web.CookieValue(cookieHeader, name) -> string
Extracts a cookie value from the text of a Cookie header.
| Parameter | Type | Description |
|---|---|---|
| cookieHeader | string | The Cookie header value (for example “sid=abc; theme=dark”). |
| name | string | The name of the cookie sought. |
The cookie's value, or an empty string if there is none.
printf("%s\n", web.CookieValue("sid=abc; theme=dark", "theme"));dark
When you have already extracted the Cookie header text (for example with HttpHeaderValue) and want to read a specific cookie from it. Directly from the request, HttpCookieValue works.
web.UrlFormValue
web.UrlFormValue(text, key) -> string
Extracts a field value from an urlencoded form or query string (resolving the percent-encoding).
| Parameter | Type | Description |
|---|---|---|
| text | string | The urlencoded text (for example “name=Ada&age=36”). |
| key | string | The name of the field sought. |
The field's (decoded) value, or an empty string if there is no such key.
printf("%s\n", web.UrlFormValue("name=Ada&age=36", "age"));36
To read an individual field from a POST form body or the query part (HttpRequestQuery). It automatically decodes percent-encoded values.
web.HttpHeaderLine
web.HttpHeaderLine(name, value) -> string
Assembles a proper HTTP header line (with name, value, and trailing CRLF).
| Parameter | Type | Description |
|---|---|---|
| name | string | The header's name. |
| value | string | The header's value. |
The finished header line, in the form “Name: value\r\n”.
printf("%s", web.HttpHeaderLine("X-Test", "yes"));X-Test: yes
To assemble your own headers for the response builders' extraHeaders argument. You can pass several lines concatenated one after another.
web.ContentTypeMediaType
web.ContentTypeMediaType(contentType) -> string
Extracts the media-type part of a Content-Type header (without the parameters).
| Parameter | Type | Description |
|---|---|---|
| contentType | string | The full Content-Type value (for example “text/html; charset=utf-8”). |
The media type (for example “text/html”).
printf("%s\n", web.ContentTypeMediaType("text/html;
charset=utf-8"));text/html
When you only need the type (text/html, application/json), without the charset and other parameters — for example to decide how to process the content.
web.ContentTypeParam
web.ContentTypeParam(contentType, paramName) -> string
Extracts the value of a parameter of the Content-Type header (for example the charset).
| Parameter | Type | Description |
|---|---|---|
| contentType | string | The full Content-Type value. |
| paramName | string | The name of the parameter sought (for example “charset”). |
The parameter's value, or an empty string if there is none.
printf("%s\n", web.ContentTypeParam("text/html; charset=utf-8",
"charset"));utf-8
To read the character encoding (charset) or the multipart boundary, when it is needed to interpret the content correctly.
web.ContentTypeMatches
web.ContentTypeMatches(contentType, mediaType) -> int
Tells whether a Content-Type belongs to the given media type (ignoring the parameters).
| Parameter | Type | Description |
|---|---|---|
| contentType | string | The full Content-Type value. |
| mediaType | string | The media type to check (for example “text/html”). |
1 if it matches; otherwise 0.
printf("%d\n", web.ContentTypeMatches("text/html; charset=utf-8",
"text/html"));1
To branch by content type: it correctly compares a header given with a charset against the desired type.
web.MimeTypeFromPath
web.MimeTypeFromPath(path) -> string
Gives the MIME type matching a file based on its extension.
| Parameter | Type | Description |
|---|---|---|
| path | string | The file's path or name (the extension matters). |
The MIME type (for example “text/css; charset=utf-8” or “image/png”).
printf("%s\n", web.MimeTypeFromPath("style.css"));
printf("%s\n", web.MimeTypeFromPath("img.png"));text/css; charset=utf-8 image/png
When serving static files, to set the correct Content-Type header based on the file name — so the browser interprets the content correctly.
web.IsTextContentType
web.IsTextContentType(contentType) -> int
Tells whether the Content-Type denotes text-like content.
| Parameter | Type | Description |
|---|---|---|
| contentType | string | The Content-Type value to check. |
1 if it is text-like (for example text/*, application/json); otherwise 0.
printf("%d\n", web.IsTextContentType("text/plain"));1
To decide whether to treat the content as text or as binary data (for example when logging or choosing an encoding).
web.IsFormUrlEncoded
web.IsFormUrlEncoded(contentType) -> int
Tells whether the Content-Type denotes an urlencoded form.
| Parameter | Type | Description |
|---|---|---|
| contentType | string | The Content-Type value to check. |
1 if it is application/x-www-form-urlencoded; otherwise 0.
printf("%d\n",
web.IsFormUrlEncoded("application/x-www-form-urlencoded"));1
Before processing a POST request: if it is an urlencoded form, you can split the body into fields with UrlFormValue.
web.IsTransferEncodingChunked
web.IsTransferEncodingChunked(transferEncoding) -> int
Tells whether a Transfer-Encoding header value denotes chunked.
| Parameter | Type | Description |
|---|---|---|
| transferEncoding | string | The Transfer-Encoding header value. |
1 if it is chunked; otherwise 0.
printf("%d\n", web.IsTransferEncodingChunked("chunked"));1
When you have already extracted the Transfer-Encoding header value and want to decide whether the transfer is chunked. Directly from the request, HttpRequestUsesChunked works.
Building HTTP responses
Assembling a finished, byte-exact HTTP response (status line, headers, body), returned as a blob. Separate variants for text and binary body, and with extra headers.
web.HttpTextResponse
web.HttpTextResponse(statusLine, contentType, body) -> blob
Assembles a complete HTTP response with a text body.
| Parameter | Type | Description |
|---|---|---|
| statusLine | string | The status (for example “200 OK”). |
| contentType | string | The content type (for example “text/plain”). |
| body | string | The response body. |
The finished HTTP response as a blob (status line + headers + body).
blob $R[256];
$R = web.HttpTextResponse("200 OK", "text/plain", "Hello");
printf("%d\n", $R.Length);60
The most common response: returning text or HTML to the client. The plugin automatically sets the correct length and headers. For extra headers, use HttpTextResponseWithHeaders.
web.HttpTextResponseWithHeaders
web.HttpTextResponseWithHeaders(statusLine, contentType, extraHeaders, body) -> blob
Like HttpTextResponse, but you can also provide additional headers.
| Parameter | Type | Description |
|---|---|---|
| statusLine | string | The status (for example “200 OK”). |
| contentType | string | The content type. |
| extraHeaders | string | Additional headers (one “Name: value\r\n” per line, for example built with HttpHeaderLine). |
| body | string | The response body. |
The finished HTTP response as a blob.
blob $R[256];
$R = web.HttpTextResponseWithHeaders("200 OK", "text/plain", "X-A:
1\r\n", "Hi");
printf("%d\n", $R.Length);65
When you also need to send your own headers (a cookie setting, cache control, CORS) alongside the text response. Build the extraHeaders lines with HttpHeaderLine.
web.HttpBlobResponse
web.HttpBlobResponse(statusLine, contentType, body) -> blob
Assembles a complete HTTP response with a binary (blob) body.
| Parameter | Type | Description |
|---|---|---|
| statusLine | string | The status (for example “200 OK”). |
| contentType | string | The content type (for example “image/png”). |
| body | blob | The binary response body. |
The finished HTTP response as a blob.
blob $B[8];
blob $R[256];
$B = blob.FromText("BIN");
$R = web.HttpBlobResponse("200 OK", "application/octet-stream",
$B);
printf("%d\n", $R.Length);72
To return binary content (an image, a downloaded file, a PDF), where the body is raw bytes and the 0 byte must be preserved.
web.HttpBlobResponseWithHeaders
web.HttpBlobResponseWithHeaders(statusLine, contentType, extraHeaders, body) -> blob
Like HttpBlobResponse, but you can also provide additional headers.
| Parameter | Type | Description |
|---|---|---|
| statusLine | string | The status (for example “200 OK”). |
| contentType | string | The content type. |
| extraHeaders | string | Additional headers (one “Name: value\r\n” per line). |
| body | blob | The binary response body. |
The finished HTTP response as a blob.
blob $B[8];
blob $R[256];
$B = blob.FromText("BIN");
$R = web.HttpBlobResponseWithHeaders("200 OK",
"application/octet-stream", "X-A: 1\r\n", $B);
printf("%d\n", $R.Length);80
For a binary response with your own headers — for example with a Content-Disposition header set, for a file download.
Practical notes
What the web plugin is good for
Building a simple web server or web service: splitting the incoming request apart and assembling the response.
Handling user data safely: HTML escaping against XSS, URL encoding for parameters.
Transporting data as text: Base64 encoding for binary data so it can be sent over a text channel.
Content-type handling: choosing the correct processing mode and response header based on the Content-Type and the file extension.
Request in, response out
The typical flow: you split the raw request (a blob) apart with the HttpRequest* and HttpHeaderValue/HttpCookieValue functions, decide based on the path/method, then assemble the response with HttpTextResponse / HttpBlobResponse (or the …WithHeaders variants). The response is a finished, byte-exact blob you can send directly to the client.
Safety
ALWAYS run user text inserted into HTML through HtmlEscape (XSS protection).
Encode a value inserted into a URL with UrlEncode, so the special characters do not break the URL.
Read form and query fields with UrlFormValue, decoded — do not process the raw text by hand.
Text vs blob
HTTP data (a request, a response) is a blob, because it can contain arbitrary bytes. The text helpers (escaping, encoding) work with strings. For a binary body, use the …Blob… variants, because a string would be truncated at the first 0 byte. Base64DecodeToText turns a 0 byte into the ? character — if that is undesirable, Base64DecodeToBlob is correct.
Error handling
A wrong argument count or type, and invalid Base64 input, are reported by the runtime as a runtime error, and the script stops. The parsers that return text signal no match with a neutral empty string (they do not fail), and the question-like (Is*) functions with 0/1 — so the usual cases need no extra guard checks, just inspect the return value.
The process process-management plugin
Running external programs, background processes, and capturing output — complete function reference