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.
WebServer
HTTP and HTTP web server
Operations
GetActiveConnections # function
Gets the number of connections being served now
function : GetActiveConnections() ~ IntReturn
| Type | Description |
|---|---|
| Int | connections being served |
GetBoundPort # function
Gets the port this server is bound to. A server started on port 0 is bound to a port the system chose; once Serve has it listening this answers that port, so a program that starts the server in a thread can poll here instead of picking a port number.
function : GetBoundPort() ~ IntReturn
| Type | Description |
|---|---|
| Int | bound port, or 0 if no server has been started (or a server on port 0 is not yet listening) |
Example
# serve on a port the system chooses, then find out which
thread := System.Concurrency.Thread->New() {
New() { Parent(); }
method : public : Run(arg : System.Base) ~ Nil {
Web.HTTP.Server.WebServer->Serve(MyHandler->New()->GetClass(), 0, false);
}
};
thread->Execute(Nil);
while(Web.HTTP.Server.WebServer->GetBoundPort() = 0) {
System.Concurrency.Thread->Sleep(10);
};
port := Web.HTTP.Server.WebServer->GetBoundPort();Serve # function
NOT IMPLEMENTED: starting a HTTP server from a configuration file. The call reports this on standard error and returns without serving; it used to load the file and then silently serve nothing. Use Serve(callback, port) or Serve(callback, port, is_debug) for HTTP, or ServeSecure(filename) for HTTPS, which does read a configuration file.
function : Serve(filename:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| filename | String | configuration file (ignored) |
Serve # function
Starts a HTTP server that listens for requests
function : Serve(callback:Class, port:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpRequestHandler' |
| port | Int | server port |
Example
# start an HTTP server on port 8080 using MyHandler
Web.HTTP.Server.WebServer->Serve(MyHandler->New()->GetClass(), 8080, true);Serve # function
Starts a HTTP server that listens for requests
function : Serve(callback:Class, port:Int, is_debug:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpRequestHandler' |
| port | Int | server port |
| is_debug | Bool | true for debug output, false otherwise |
Serve # function
Starts a HTTP server that listens for requests on one local address instead of every interface, e.g. "127.0.0.1" to take only clients on this machine. The address is read as by TCPSocketServer->New(address, port): a literal ("127.0.0.1", "::1") or a host name, of which the first IPv4 address (else the first IPv6 one) is bound, so "localhost" binds 127.0.0.1; an empty address binds every interface. An address that cannot be bound is reported on standard error and the call returns.
function : Serve(callback:Class, address:String, port:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpRequestHandler' |
| address | String | local address to bind |
| port | Int | server port |
Example
# an HTTP server on port 8080 that only this machine can reach
Web.HTTP.Server.WebServer->Serve(MyHandler->New()->GetClass(), "127.0.0.1", 8080);Serve # function
Starts a HTTP server that listens for requests on one local address instead of every interface; see Serve(callback, address, port).
function : Serve(callback:Class, address:String, port:Int, is_debug:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpRequestHandler' |
| address | String | local address to bind |
| port | Int | server port |
| is_debug | Bool | true for debug output, false otherwise |
ServeSecure # function
Starts a HTTPS server that listens for requests
function : ServeSecure(filename:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| filename | String | configuration file (an example) |
Example
# start an HTTPS server from a JSON config file
Web.HTTP.Server.WebServer->ServeSecure("config/server_config.json");ServeSecure # function
Starts a HTTPS server that listens for requests
function : ServeSecure(callback:Class, port:Int, cert:String, cert_key:String, cert_key_passwd:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpsRequestHandler' |
| port | Int | server port |
| cert | String | SSL certificate |
| cert_key | String | SSL certificate key |
| cert_key_passwd | String | SSL certificate password |
ServeSecure # function
Starts a HTTPS server that listens for requests
function : ServeSecure(callback:Class, port:Int, cert:String, cert_key:String, cert_key_passwd:String, is_debug:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpsRequestHandler' |
| port | Int | server port |
| cert | String | SSL certificate |
| cert_key | String | SSL certificate key |
| cert_key_passwd | String | SSL certificate password |
| is_debug | Bool | true for debug output, false otherwise |
ServeSecure # function
Starts a HTTPS server that listens for requests on one local address instead of every interface, e.g. "127.0.0.1" to take only clients on this machine. The address is read as by TCPSocketServer->New(address, port): a literal ("127.0.0.1", "::1") or a host name, of which the first IPv4 address (else the first IPv6 one) is bound, so "localhost" binds 127.0.0.1; an empty address binds every interface. An address that cannot be bound is reported on standard error and the call returns.
function : ServeSecure(callback:Class, address:String, port:Int, cert:String, cert_key:String, cert_key_passwd:String) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpsRequestHandler' |
| address | String | local address to bind |
| port | Int | server port |
| cert | String | SSL certificate |
| cert_key | String | SSL certificate key |
| cert_key_passwd | String | SSL certificate password |
ServeSecure # function
Starts a HTTPS server that listens for requests on one local address instead of every interface; see ServeSecure(callback, address, port, cert, cert_key, cert_key_passwd).
function : ServeSecure(callback:Class, address:String, port:Int, cert:String, cert_key:String, cert_key_passwd:String, is_debug:Bool) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| callback | Class | class inherited from 'HttpsRequestHandler' |
| address | String | local address to bind |
| port | Int | server port |
| cert | String | SSL certificate |
| cert_key | String | SSL certificate key |
| cert_key_passwd | String | SSL certificate password |
| is_debug | Bool | true for debug output, false otherwise |
SetMaxConnections # function
Sets the most connections served at once; a connection beyond it is answered 503 and closed. Call before Serve. Zero, the default, allows 1024.
function : SetMaxConnections(max:Int) ~ NilParameters
| Name | Type | Description |
|---|---|---|
| max | Int | most simultaneous connections |
Stop # function
Stops the server
function : Stop() ~ NilExample
# stop a running HTTP or HTTPS server
Web.HTTP.Server.WebServer->Stop();