HTTPD library is based on HTTP 1.1 protocol implementation, built-in high-performance HTTP protocol parser and URLdecode parsing library.

The HTTPD library works fine by default, but some parameters need to be tweaked in extreme scenarios.

An introduction to common built-in HTTPD methods

1. httpd:timeout(number)

Set the maximum waiting time for each connection to idle, beyond which HTTPD will actively disconnect. (Default :30 seconds)

2. httpd:max_path_size(number)

Set the maximum length of Path, beyond which HTTPD will return 414. (Default: 1024)

3. httpd:max_header_size(number)

Set the maximum Header length, beyond which HTTPD will return 431. (Default: 65535)

4. httpd:max_body_size(number)

Set the maximum length of the Body, beyond which 413 will be returned. (Default: 1024 * 1024)

5. httpd:before(function)

The before method determines the behavior of API and USE route callbacks before they are triggered, allowing all routes through by default.

The before method is commonly used to set and modify user authentication routing behavior (such as header authentication), which provides an opportunity for developers to design middleware based on the before function.

When the developer sets up function (that is, an empty function), the HTTP library is used to determine the behavior.

6. httpd:group(type, prefix, handles)

The group method provides a way to register routes in batches. It provides a simple and convenient registration method for a group of the same route.

The first parameter type is the type of routes to be registered in batches. After initializing the HTTPD object, USE app.USE or app.API to pass the value;

The second argument prefix is the string header; For example, / API, /admin;

The third argument is an array of routing handler functions or processing classes. The type is {route = ‘/login’, class = class}.

Note: This method only supports batch registration of API and USE routes. Different types of routes cannot be registered at the same time.

7. httpd:static(folder, ttl)

The Listen method is used to tell an HTTPD object to listen on a specified port.

The first parameter, IP, is not currently used by HTTPD (but must be set). By default, it listens for ‘0.0.0.0’ and the specified port number for all network cards.

Backlog is the maximum user connection waiting queue. Appropriate Settings can reduce the number of connections reset (the default is 128).

8. httpd:run()

After all HTTPD library parameters and routes are set, call the run method to enable listening mode.

HTTPD request log

Log format: [year/month/day hour: minute: second] – [IP] – [X-real-ip] – [path] – [method] – [HTTP code] – [Request Handle Timeline]

Middleware for HTTPD

The HTTPD library provides before methods that make it possible for developers to customize ‘middleware’ behavior. For details, see HTTP library.

http content

Each HTTP request passes in a content that is all the parameters of the client request when it calls the route before registered with the user.

Args: supports standard GET or POST parameters, a[1]=1&a[2]=2 will be resolved to array type; Supports multipart/form-data parameter transmission.

Header: the original header key-value table. The framework layer does not use the header for content parsing.

Body: Currently body supports these types: multipart/form-data, Application/X-www-form-urlencoded, Application /json, Application/XML;

Json/XML: When body is json, the JSON property of content is true; When body is of type XML, the CONTENT XML attribute is true.

File: this property is present when the client passes data using multipart/form-data; This property is an array type;

Continue to learn

In the next chapter we will learn how to build an HTTPD template engine using the Template library