Basic HTTP server implementation using the TCP stack provided by gawk. Example:
#
# hello.awk
# usage:
# - gawk -f hello.awk
# - open up localhost:3001 in your browser
#
@include "src/awkserver.awk"
function hello()
{
setResponseBody("Hello!")
}
BEGIN {
addRoute("GET", "/", "hello") # route requests on "/" to the function "hello()"
startAwkServer(3001) # start listening. this function never exits
}This just sends a plain text response "Hello!". To accomplish more, look at the API:
setResponseStatus(statusLine)sets the returned status stringsetResponseHeader(name, value)sets an outgoing response headersetResponseBody(body)sets the outgoing response bodysendFile(filename)sends a file as the responsenotFound()returns a 404badRequest()returns a 400redirect(location)sends a redirectaddRoute(method, endpoint, callback)whenendpointis called with the given method, thencallback()is invoked with no parameters. Only one callback function per method/endpoint pair.
All routing functions are in src/core.awk
getRequestHeader(name)returns a header (name is case-insensitive)getRequestParam(name)returns a param from the url querygetRequestBody()returns the request body, minus the last bytegetRequestEndpoint()returns the incoming request endpoint
All request functions are also in src/core.awk
getFile(filename)returns the contents of a file (src/core.awk)info(msg),error(msg),debug(msg)print formatted logs (src/log.awk)setStaticDirectory(dir)choose the directory to serve static files from. defaults to "static" (src/config.awk)setLogLevel(level)set the log level ("debug", "info", "error") (src/log.awk)- NEW!
parseJson(input, mapRef, keysRef)parses the contents of a JSON string (modules/json-parser.awk)
This goal of this project is to provide users with the ability to bring up a very basic HTTP server with minimal effort. It does not do templating, security, or encryption, and no matter what you do the last character of every incoming request body will be dropped (due to how awk splits records). You have been warned.
The function startAwkServer(port) begins an infinite loop serving all incoming requests on a single thread. Requests are handled in this fashion:
- Incoming request headers, query params, and endpoint are parsed.
- Request body is buffered in memory.
- Routing function lookup:
- The server checks the routing table for a user defined route function (see
addRoute(method, endpoint, callback)insrc/core.awk) - If no route exists, the server then tries to resolve the endpoint to a file in the static files directory (see
setStaticDirectory(dir)above) - If that fails, the route is resolved to a 404 error.
- The server checks the routing table for a user defined route function (see
- The routing function is called. It is assumed that the routing function will set the response headers / status / body
- The response is compiled and sent.
- Now we are ready to handle another request.
This request handling code can be found in src/http.awk
- GNU Awk 4.0.0 or later
- Patience