HTTP Server API
See also: docs.rs for HTTP Server part of process_lib
.
Note: Most processes will not use this API directly. Instead, they will use the process_lib
library, which papers over this API and provides a set of types and functions which are much easier to natively use. This is mostly useful for re-implementing this module in a different client or performing niche actions unsupported by the library.
The HTTP server is used by sending and receiving requests and responses.
From a process, you may send an HttpServerAction
to the http-server:distro:sys
process.
This struct must be serialized to JSON and placed in the body
of a requests to http-server:distro:sys
.
For actions that take additional data, such as Bind
and WebSocketPush
, it is placed in the lazy_load_blob
of that request.
After handling such a request, the HTTP server will always give a response of the shape Result<(), HttpServerError>
, also serialized to JSON. This can be ignored, or awaited and handled.
Certain actions will cause the HTTP server to send requests to the process in the future.
If a process uses Bind
or SecureBind
, that process will need to field future requests from the HTTP server. The server will handle incoming HTTP protocol messages to that path by sending an HttpServerRequest
to the process which performed the binding, and will expect a response that it can then send to the client.
Note: Paths bound using the HTTP server are always prefixed by the ProcessId of the process that bound them.
Note 2: If a process creates a static binding by setting cache
to true
, the HTTP server will serve whatever bytes were in the accompanying lazy_load_blob
to all GET requests on that path.
If a process uses WebSocketBind
or WebSocketSecureBind
, future WebSocket connections to that path will be sent to the process, which is expected to issue a response that can then be sent to the client.
Bindings can be removed using Unbind
and WebSocketUnbind
actions.
Note that the HTTP server module will persist bindings until the node itself is restarted (and no later), so unbinding paths is usually not necessary unless cleaning up an old static resource.
The incoming request, whether the binding is for HTTP or WebSocket, will look like this:
Processes that use the HTTP server should expect to field this request type, serialized to JSON. The process must issue a response with this structure in the body, serialized to JSON:
This response is only required for HTTP requests.
WebSocketOpen
, WebSocketPush
, and WebSocketClose
requests do not require a response.
If a process is meant to send data over an open WebSocket connection, it must issue a HttpServerAction::WebSocketPush
request with the appropriate channel_id
.
Find discussion of the HttpServerAction::WebSocketExt*
requests in the extensions document.