Skip to content

Module hb_http_server.erl

A router that attaches a HTTP server to the AO-Core resolver.

Description

Because AO-Core is built to speak in HTTP semantics, this module only has to marshal the HTTP request into a message, and then pass it to the AO-Core resolver.

hb_http:reply/4 is used to respond to the client, handling the process of converting a message back into an HTTP response.

The router uses an Opts message as its Cowboy initial state, such that changing it on start of the router server allows for the execution parameters of all downstream requests to be controlled.

Function Index

allowed_methods/2Return the list of allowed methods for the HTTP server.
cors_reply/2*Reply to CORS preflight requests.
get_opts/0Get the node message for the current process.
get_opts/1
handle_request/3*Handle all non-CORS preflight requests as AO-Core requests.
http3_conn_sup_loop/0*
init/2Entrypoint for all HTTP requests.
new_server/1*Trigger the creation of a new HTTP server node.
read_body/1*Helper to grab the full body of a HTTP request, even if it's chunked.
read_body/2*
set_default_opts/1Apply the default node message to the given opts map.
set_node_opts_test/0*Ensure that the start hook can be used to modify the node options.
set_opts/1Merges the provided Opts with uncommitted values from Request, preserves the http_server value, and updates node_history by prepending the Request.
set_opts/2
set_opts_test/0*Test the set_opts/2 function that merges request with options, manages node history, and updates server state.
set_proc_server_id/1Initialize the server ID for the current process.
start/0Starts the HTTP server.
start/1
start_http2/3*
start_http3/3*
start_node/0Test that we can start the server, send a message, and get a response.
start_node/1

Function Details

allowed_methods/2

allowed_methods(Req, State) -> any()

Return the list of allowed methods for the HTTP server.

cors_reply/2 *

cors_reply(Req, ServerID) -> any()

Reply to CORS preflight requests.

get_opts/0

get_opts() -> any()

Get the node message for the current process.

get_opts/1

get_opts(NodeMsg) -> any()

handle_request/3 *

handle_request(RawReq, Body, ServerID) -> any()

Handle all non-CORS preflight requests as AO-Core requests. Execution starts by parsing the HTTP request into HyerBEAM's message format, then passing the message directly to meta@1.0 which handles calling AO-Core in the appropriate way.

http3_conn_sup_loop/0 *

http3_conn_sup_loop() -> any()

init/2

init(Req, ServerID) -> any()

Entrypoint for all HTTP requests. Receives the Cowboy request option and the server ID, which can be used to lookup the node message.

new_server/1 *

new_server(RawNodeMsg) -> any()

Trigger the creation of a new HTTP server node. Accepts a NodeMsg message, which is used to configure the server. This function executed the start hook on the node, giving it the opportunity to modify the NodeMsg before it is used to configure the server. The start hook expects gives and expects the node message to be in the body key.

read_body/1 *

read_body(Req) -> any()

Helper to grab the full body of a HTTP request, even if it's chunked.

read_body/2 *

read_body(Req0, Acc) -> any()

set_default_opts/1

set_default_opts(Opts) -> any()

Apply the default node message to the given opts map.

set_node_opts_test/0 *

set_node_opts_test() -> any()

Ensure that the start hook can be used to modify the node options. We do this by creating a message with a device that has a start key. This key takes the message's body (the anticipated node options) and returns a modified version of that body, which will be used to configure the node. We then check that the node options were modified as we expected.

set_opts/1

set_opts(Opts) -> any()

Merges the provided Opts with uncommitted values from Request, preserves the http_server value, and updates node_history by prepending the Request. If a server reference exists, updates the Cowboy environment variable 'node_msg' with the resulting options map.

set_opts/2

set_opts(Request, Opts) -> any()

set_opts_test/0 *

set_opts_test() -> any()

Test the set_opts/2 function that merges request with options, manages node history, and updates server state.

set_proc_server_id/1

set_proc_server_id(ServerID) -> any()

Initialize the server ID for the current process.

start/0

start() -> any()

Starts the HTTP server. Optionally accepts an Opts message, which is used as the source for server configuration settings, as well as the Opts argument to use for all AO-Core resolution requests downstream.

start/1

start(Opts) -> any()

start_http2/3 *

start_http2(ServerID, ProtoOpts, NodeMsg) -> any()

start_http3/3 *

start_http3(ServerID, ProtoOpts, NodeMsg) -> any()

start_node/0

start_node() -> any()

Test that we can start the server, send a message, and get a response.

start_node/1

start_node(Opts) -> any()