Skip to main content

web Network Access

web is Alia's standard library network module, providing URL parsing, an HTTP client/server, MIME type lookup, and more.

Importing the Module​

import 'web' as web ~ import as a variable
import 'web' ~ import into scope

URL Utilities​

urlParse​

Parses a URL string into a structured object.

ParameterTypeRequiredDescription
First positional argumentStringYesThe URL string to parse

Returned object properties:

PropertyTypeDescription
protocolStringProtocol, e.g. 'https'
hostStringHostname + port, e.g. 'example.com:8080'
hostnameStringHostname, e.g. 'example.com'
portNumber?Port number, null if absent
pathnameStringPath, e.g. '/path'
searchString?Query string, null if absent
hashString?Anchor, null if absent
hrefStringFull URL
const parsed = web.urlParse 'https://example.com:8080/path?key=value#anchor'
parsed.protocol ~ 'https'
parsed.hostname ~ 'example.com'
parsed.port ~ 8080
parsed.pathname ~ '/path'
parsed.search ~ '?key=value'
parsed.hash ~ '#anchor'

urlBuild​

Builds a URL string from a structured object. urlBuild is the inverse of urlParse.

ParameterTypeRequiredDescription
First positional argumentObjectYesObject containing the URL components
const url = call web.urlBuild *[
protocol: 'https',
hostname: 'example.com',
port: 8080,
pathname: '/api',
search: '?q=alia'
]
~ 'https://example.com:8080/api?q=alia'

urlEncode / urlDecode​

Encodes/decodes an entire URL (preserving URL reserved characters such as ://, ?, #).

web.urlEncode 'https://example.com/路径'
~ 'https://example.com/%E8%B7%AF%E5%BE%84'

web.urlDecode 'https://example.com/%E8%B7%AF%E5%BE%84'
~ 'https://example.com/路径'

urlEncodeComponent / urlDecodeComponent​

Encodes/decodes a URI component (encodes all special characters).

web.urlEncodeComponent 'hello world'
~ 'hello%20world'

web.urlDecodeComponent 'hello%20world'
~ 'hello world'

MIME Types​

mimeGet​

Gets the MIME type for a file extension.

ParameterTypeRequiredDescription
First positional argumentStringYesFile extension (with or without the . prefix)
web.mimeGet 'json' ~ 'application/json'
web.mimeGet '.html' ~ 'text/html'
web.mimeGet 'png' ~ 'image/png'
web.mimeGet 'xyz' ~ 'application/octet-stream' ~ default for unknown types

Built-in MIME mapping table:

ExtensionMIME Type
html, htmtext/html
csstext/css
js, mjsapplication/javascript
jsonapplication/json
xmlapplication/xml
txttext/plain
csvtext/csv
pngimage/png
jpg, jpegimage/jpeg
gifimage/gif
svgimage/svg+xml
icoimage/x-icon
webpimage/webp
mp3audio/mpeg
wavaudio/wav
oggaudio/ogg
mp4video/mp4
webmvideo/webm
pdfapplication/pdf
zipapplication/zip
gzapplication/gzip
tarapplication/x-tar
wasmapplication/wasm
ttffont/ttf
wofffont/woff
woff2font/woff2
yaml, ymlapplication/x-yaml
mdtext/markdown

mimeGetExtension​

Gets the preferred file extension for a MIME type.

ParameterTypeRequiredDescription
First positional argumentStringYesMIME type string
web.mimeGetExtension 'text/html' ~ 'html'
web.mimeGetExtension 'application/json' ~ 'json'
web.mimeGetExtension 'application/x-unknown' ~ null

HTTP Client​

httpRequest​

Initiates an asynchronous HTTP request.

ParameterTypeRequiredDescription
First positional argumentObjectYesRequest options object

Request options object properties:

PropertyTypeRequiredDefaultDescription
urlStringYes-Request URL
methodStringNo'GET'HTTP method
headersObjectNo{}Request headers
bodyStringNonullRequest body
timeoutNumberNo30000Timeout (milliseconds)

Returns a Stack (asynchronous request object, to be consumed with await):

After the request is initiated, an awaitable Stack conforming to Alia's asynchronous model is returned. The request runs on a background thread; await waits for the result, and the response object can be accessed via task.response.

PropertyTypeDescription
statusNumberHTTP status code
statusTextStringStatus text
headersObjectResponse headers
bodyStringResponse body
okBooleantrue when the status code is 200-299
const opts = *[
method: 'GET',
url: 'https://api.example.com/data'
]
const resp = await web.httpRequest opts
if (resp.ok as Boolean) {
print resp.body as String
}

await accepts an expression returning a Stack, so the response can be obtained in a single step; alternatively, hold the task handle first and then await it, in order to access isRunning/isDone/detach:

const task = web.httpRequest opts
await task
if (task.response.ok as Boolean) {
print task.response.body as String
}

Online Detection​

It is recommended to determine online status using the https://alia.0832.ink/externalAccess static probe: this file is deployed on the documentation site and its content is true.

import 'web' as web
const opts = *[url: 'https://alia.0832.ink/externalAccess', timeout: 10000]
const task = web.httpRequest opts
await task
assert (Ok task.response.body) error 'externalAccess should return body'
assert (call task.response.body.trim) == 'true' error 'externalAccess should be true'
assert task.response.status == 200 error 'externalAccess should return 200'

When offline, the request fails or falls back to HTML, which is not true.

serverCreate​

Creates an HTTP server instance.

const server = call web.serverCreate

Methods of the returned Server object:

server.addRoute​

Registers a route handler.

ParameterTypeRequiredDescription
First positional argumentStringYesHTTP method (GET, POST, etc.), or * to match all
Second positional argumentStringYesPath pattern, supporting :param dynamic parameters
Third positional argumentAliasYesRequest handler (req, res) -> { ... }
server.addRoute 'GET' '/hello' (alias -> <Object req> and <Object res> {
res.send 'Hello, World!'
})

server.addRoute 'GET' '/users/:id' (alias -> <Object req> and <Object res> {
call res.json *[userId: (req.params as Object)['id']]
})

server.addRoute 'POST' '/api/data' (alias -> <Object req> and <Object res> {
~ req.body contains the request body
call res.json *[received: true]
})

Request object (req) properties:

PropertyTypeDescription
methodStringHTTP method
urlStringOriginal URL
pathStringPath portion
queryObjectQuery parameters
headersObjectRequest headers
bodyString?Request body
paramsObject?Path parameters

Response object (res) methods:

MethodDescription
res.setStatus codeSets the status code, defaults to 200
res.setHeader name valueSets a response header
res.send bodyTextSends a text response
res.json dataSends a JSON response (sets Content-Type automatically)
res.endEnds the response

server.listen​

Starts the server listening on the specified port.

ParameterTypeRequiredDescription
First positional argumentNumberYesPort number to listen on
callbackAliasNoCallback invoked after a successful startup
server.listen 3000 (alias -> {
io.print 'Server running on http://localhost:3000'
})

server.close​

Closes the server (initiating and non-blocking: the call merely initiates the shutdown, and port resources are released asynchronously).

server.close

Complete Example​

import 'web' as web
import 'io' as io

~ URL parsing
const parsed = web.urlParse 'https://example.com/search?q=alia'
print 'Host: ' .. parsed.hostname
print 'Path: ' .. parsed.pathname

~ HTTP client (asynchronous)
const opts = *[
url: 'https://api.github.com'
]
const task = web.httpRequest opts
await task
print 'Status code: ' .. (task.response.status as Number)

~ HTTP server
const server = call web.serverCreate

server.addRoute 'GET' '/api/hello' (alias -> <Object req> and <Object res> {
call res.json *[greeting: 'Hello from Alia!']
})

server.listen 3000
io.print 'Server started, visit http://localhost:3000/api/hello'