HTTP server framework. WebServer registers route handlers for GET/POST/PUT/DELETE, parses query params and multipart form data, and supports OAuth token exchange. Compile with -lib net_server.
Request
Web server request
Operations
- New
- GetContent
- GetContentBytes
- GetCookie
- GetCookies
- GetHeader
- GetLocalPort
- GetMethod
- GetParam
- GetParams
- GetPath
- GetQuery
- GetRemoteAddress
- GetRequestLine
- HasContent
- HasCookie
- HasParam
- ParseAttribs
- ParseCookies
- ParseParams
- ParseQuery
- SetConnectionInfo
GetContent #
Gets the request body content
method : public : GetContent() ~ StringReturn
| Type | Description |
|---|---|
| String | request content |
Example
# inside ProcessPost:
if(request->HasContent()) {
body := request->GetContent();
body->PrintLine(); # raw POST body string
};GetContentBytes #
Gets the request body content as bytes
method : public : GetContentBytes() ~ Byte[]Return
| Type | Description |
|---|---|
| Byte | request content as bytes |
GetCookie #
Gets a cookie value
method : public : GetCookie(name:String) ~ CookieParameters
| Name | Type | Description |
|---|---|---|
| name | String | cookie name |
Return
| Type | Description |
|---|---|
| Cookie | cookie value |
GetCookies #
Gets cookies
method : public : GetCookies() ~ Vector<Cookie>Return
| Type | Description |
|---|---|
| Vector<Cookie> | cookies |
GetHeader #
Gets a request header. Header names are case-insensitive (RFC 9110 sec. 5.1) and the server stores them lowercased, so "If-None-Match" finds "if-none-match"; a name stored exactly as given is found as well.
method : public : GetHeader(name:String) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| name | String | header name |
Return
| Type | Description |
|---|---|
| String | request header, or Nil if the request has none |
GetLocalPort #
Gets the port this server is bound to. This is the port passed to Serve, not a socket-level lookup of the accepted connection.
method : public : GetLocalPort() ~ IntReturn
| Type | Description |
|---|---|
| Int | local port, or 0 if the server did not supply one |
GetMethod #
Gets the HTTP verb. The verb is parsed by the server to choose a handler method; this exposes it to handlers that need it directly.
method : public : GetMethod() ~ StringReturn
| Type | Description |
|---|---|
| String | HTTP verb, or Nil if the server did not supply one |
GetParam #
Gets a parameter value
method : public : GetParam(name:String) ~ StringParameters
| Name | Type | Description |
|---|---|---|
| name | String | parameter name |
Return
| Type | Description |
|---|---|
| String | parameter value |
Example
# inside ProcessGet or ProcessPost:
name := request->GetParam("username");
if(name <> Nil) {
"Hello, {$name}!"->PrintLine();
};GetParams #
Gets parameters
method : public : GetParams() ~ Map<String,String>Return
| Type | Description |
|---|---|
| Map<String,String> | parameters |
GetPath #
Gets the request path
method : public : GetPath() ~ StringReturn
| Type | Description |
|---|---|
| String | request path |
Example
# inside ProcessGet:
path := request->GetPath();
path->PrintLine(); # e.g. /api/usersGetQuery #
Gets the request query
method : public : GetQuery() ~ StringReturn
| Type | Description |
|---|---|
| String | request query |
GetRemoteAddress #
Gets the address of the connected peer.
method : public : GetRemoteAddress() ~ StringReturn
| Type | Description |
|---|---|
| String | peer address, or Nil if the server did not supply one |
GetRequestLine #
Gets the request line
method : public : GetRequestLine() ~ StringReturn
| Type | Description |
|---|---|
| String | request line |
HasContent #
Checks for request body content
method : public : HasContent() ~ BoolReturn
| Type | Description |
|---|---|
| Bool | true if content exists, false otherwise |
HasCookie #
Checks for a cookie
method : public : HasCookie(name:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| name | String | cookie name |
Return
| Type | Description |
|---|---|
| Bool | true if cookie exists, false otherwise |
HasParam #
Checks for a parameter
method : public : HasParam(name:String) ~ BoolParameters
| Name | Type | Description |
|---|---|---|
| name | String | parameter name |
Return
| Type | Description |
|---|---|
| Bool | true if parameter exists, false otherwise |
New # constructor
Constructor
New(request_line:String, request_headers:Map<String,String>)Parameters
| Name | Type | Description |
|---|---|---|
| request_line | String | HTTP request line |
| request_headers | Map<String,String> | HTTP request headers |
New # constructor
Constructor
New(request_line:String, request_headers:Map<String,String>, content:Byte[])Parameters
| Name | Type | Description |
|---|---|---|
| request_line | String | HTTP request line |
| request_headers | Map<String,String> | HTTP request headers |
| content | Byte | request body content |
ParseAttribs # function
Parses an attribute string
function : ParseAttribs(attrib_str:String) ~ Map<String,String>Parameters
| Name | Type | Description |
|---|---|---|
| attrib_str | String | attribute string |
Return
| Type | Description |
|---|---|
| Map<String,String> | map of names/values |
ParseQuery # function
Parses a query string or an application/x-www-form-urlencoded body into names and values. Pairs are separated by '&' and split at the FIRST '=', so a value may itself hold '='. Names and values are decoded with Web.HTTP.Url->Decode ('+' is a space, %XX runs are UTF-8). A pair with nothing after its '=' has an empty value, and a bare name with no '=' (a flag) also has an empty value. Empty segments ('a=1&&b=2', a trailing '&') and pairs with an empty name are skipped. When a name repeats, the last value wins.
function : ParseQuery(query:String) ~ Map<String,String>Parameters
| Name | Type | Description |
|---|---|---|
| query | String | encoded query string, without the leading '?' |
Return
| Type | Description |
|---|---|
| Map<String,String> | names and values; empty if query is Nil |
Example
params := Web.HTTP.Server.Request->ParseQuery("q=a%3Db+c&flag&empty=");
params->Find("q")->PrintLine(); # a=b c
params->Has("flag")->PrintLine(); # trueSetConnectionInfo #
Sets connection metadata. Called by the server immediately after the request is constructed; handlers should treat these as read-only.
method : public : SetConnectionInfo(request_verb:String, remote_address:String, local_port:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| request_verb | String | HTTP verb, e.g. GET or POST |
| remote_address | String | address of the connected peer |
| local_port | Int | port this server is bound to |