aboutsummaryrefslogtreecommitdiff
path: root/handler.c
Commit message (Collapse)AuthorAgeFilesLines
* Implement HTTP chunk encodingXavier Del Campo Romero2025-10-081-5/+5
| | | | | | A new function pointer, namely chunk, has been added to struct http_response so that library users can generate their message bodies dynamically.
* Allow custom backlog connectionsXavier Del Campo Romero2025-10-061-1/+1
| | | | | | | | | libweb calls listen(2) when setting up the HTTP server, and its backlog argument was hardcoded to 10. While probably not an issue for some applications, it can be too limiting for some others. Therefore, it is desirable to allow library users to set up their own limits. Otherwise, 10 is still chosen as a sane default.
* Fix design issues with async responses, add async exampleXavier Del Campo Romero2025-10-061-7/+26
| | | | | | | | | | | | | | | | | | struct http_response did not provide users any void * that could be used to maintain a state between calls to an asynchronous HTTP response. On the other hand, the user pointer could not be used for this purpose, since it is shared among all HTTP clients for a given struct handler instance. Moreover, the length callback was still not supporting this feature, which in fact might be required by some users. Implementing this was particularly challenging, as this broke the current assumption that all bytes on a call to http_read were being processed. Now, since a client request can only be partially processed because of the length callback, http_read must take this into account so that the remaining bytes are still available for future calls, before reading again from the file descriptor.
* Implement async HTTP responsesXavier Del Campo Romero2025-09-241-1/+14
| | | | | | | | | | | | Sometimes, library users cannot return a HTTP response as soon as the request is received, or the operations that are required to generate it can take a long time. In order to solve this, libweb adds a new member to struct http_response, namely step, which must be assigned to a function whenever a HTTP response should be generated in a non-blocking manner. Leaving the function pointer as null will fall back to the default behaviour.
* handler.c: Do not printf when exitingXavier Del Campo Romero2024-08-251-3/+0
| | | | | libweb is meant to be silent during normal operation, thus only printing to stderr on errors.
* Move signal handling to processesXavier Del Campo Romero2024-08-221-0/+5
| | | | | | | | | | | | | | So far, libweb installed a signal handler so as to handle SIGTERM, SIGPIPE and SIGINT signals so that processes would not have to care about such details. However, it is not advisable for libraries to install signal handlers, as signals are handled on a per-process basis. The previous approach would be incompatible if several instances of the library were allocated by the same process. Unfortunately, this has the undesired side effect of adding the boilerplate code into the process.
* Limit maximum multipart/form-data pairs and filesXavier Del Campo Romero2024-02-191-1/+2
| | | | | | A malicious user could inject an infinite number of empty files or key/value pairs into a request in order to exhaust the device's resources.
* Merge pull request 'Fix double-free on failed `server_client_close`' (#2) ↵xavi2023-11-201-5/+2
|\ | | | | | | | | | | from midokura-xavi/libweb:fix-double-free into master Reviewed-on: https://gitea.privatedns.org/xavi/libweb/pulls/2
| * Fix double-free on failed server_client_closeXavier Del Campo2023-11-201-5/+2
| | | | | | | | | | Even if server_client_close fails, it is needed for client_free to remove the dangling reference from h->clients.
* | Split handler_loop from handler_listenXavier Del Campo2023-11-201-2/+8
|/ | | | | | | | | | | | | | Some applications might set up a struct handler object to listen on any port i.e., 0, but still need a way to determine which port number was eventually selected by the implementation. Therefore, handler_listen has been reduced to the server initialization bit, whereas the main loop has been split into its own function, namely handler_loop. Because of these changes, it no longer made sense for libweb to write the selected port to standard output, as this is something now applications can do on their own.
* Send HTTP headers to payload callbackXavier Del Campo Romero2023-11-181-1/+2
| | | | | | | | | | | | Even if libweb already parses some common headers, such as Content-Length, some users might find it interesting to inspect which headers were received from a request. Since HTTP/1.1 does not define a limit on the number of maximum headers a client can send, for security reasons a maximum value must be provided by the user. Any extra headers shall be then discarded by libweb. An example application showing this new feature is also provided.
* Rename project from slweb to libwebv0.1.0-rc3Xavier Del Campo Romero2023-10-111-4/+4
| | | | | | | | | | | | It was found out there was another project of the same name around (https://git.sr.ht/~strahinja/slweb/), also related to website generation. In order to avoid confusion, a new name has been chosen for this project. Surprisingly, libweb was not in use by any distributions (according to https://repology.org and AUR index), and it should reflect well the intention behind this project i.e., being a library to build web-related stuff.
* handler: Define port as unsigned shortXavier Del Campo Romero2023-09-071-1/+1
| | | | | Port numbers are unsigned by definition. Fortunately, this was a minor issues since server_init was doing an implicit cast to unsigned short.
* Move header files to subdirectoryXavier Del Campo Romero2023-07-211-4/+4
| | | | | | | | | | | Since slweb is meant as a library, it is advisable to keep public header files under their own directory in order to avoid name clashing i.e., #include "something.h" Now becomes: #include "slweb/something.h"
* wildcard_cmp: Allow case-insensitive searchesXavier Del Campo Romero2023-07-201-1/+1
| | | | The new search feature will require them.
* Split wildcard_cmp into its own componentXavier Del Campo Romero2023-07-201-42/+1
| | | | Future commits will make use of this function outside handler.c.
* Replace select(2) with poll(2)Xavier Del Campo Romero2023-07-201-2/+2
| | | | | | select(2) has a number of well-known issues (e.g.: FD_SETSIZE limiting the maximum amount of file descriptors to watch) that are mostly solved by poll(2) and thus can be used as a drop-in replacement.
* Define _POSIX_C_SOURCEXavier Del Campo Romero2023-07-201-0/+2
| | | | | This allows using the default compiler defined by make(1) (i.e., c99(1)), thus improving POSIX compatibility.
* Send response on quota exceededXavier Del Campo Romero2023-07-201-2/+3
| | | | | | | | | | | | | | | | | So far, slcl would just close the connection with a client when the Content-Length of an incoming request exceeded the user quota, without any meaningful information given back to the user. Now, slcl responds with a HTML file with meaningful information about the error. Limitations: - While this commits has been successfully tested on ungoogled-chromium, LibreWolf (and I assume Firefox and any other derivates too) does not seem to receive the response from the server. - However, this issue only occurred during local testing, but not on remote instances.
* Implement user quotaXavier Del Campo Romero2023-07-201-18/+30
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | This feature allows admins to set a specific quota for each user, in MiB. This feature is particularly useful for shared instances, where unlimited user storage might be unfeasible or even dangerous for the server. Also, a nice HTML5 <progress> element has been added to the site that shows how much of the quota has been consumed. If no quota is set, slcl falls back to the default behaviour i.e., assume unlimited storage. Limitations: - While HTTP does specify a Content-Length, which determines the length of the whole request, it does not specify how many files are involved or their individual sizes. - Because of this, if multiple files are uploaded simultaneously, the whole request would be dropped if user quota is exceeded, even if not all files exceeded it. - Also, Content-Length adds the length of some HTTP boilerplate (e.g.: boundaries), but slcl must rely on this before accepting the whole request. In other words, this means some requests might be rejected by slcl because of the extra bytes caused by such boilerplate. - When the quota is exceeded, slcl must close the connection so that the rest of the transfer is cancelled. Unfortunately, this means no HTML can be sent back to the customer to inform about the situation.
* Perform some minor optimizationsXavier Del Campo Romero2023-07-201-3/+2
|
* Fix memory leak on failed realloc(3)Xavier Del Campo Romero2023-07-201-10/+16
| | | | | | | | | | According to C99 §7.20.3.4: If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged. Therefore, a temporary pointer must be used to ensure the original object can still be deallocated should realloc(3) return a null pointer.
* Initial commitXavier Del Campo Romero2023-07-201-0/+336