Seregon/zftpd

Zero-copy FTP/HTTP Daemon compatible with all POSIX systems

C/11.0 KB/No license
include/http_api.h
zftpd / include / http_api.h
1/*
2MIT License
3 
4Copyright (c) 2026 Seregon
5 
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12 
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15 
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24 
25/**
26 * @file http_api.h
27 * @brief REST API handlers
28 */
29 
30#ifndef HTTP_API_H
31#define HTTP_API_H
32 
33#include "ftp_types.h"
34#include "http_parser.h"
35#include "http_response.h"
36 
37http_response_t *http_api_handle(const http_request_t *request);
38 
39/**
40 * @brief Attach the FTP server context to the HTTP API layer.
41 *
42 * Required for the POST /api/network/reset endpoint (Fix #4).
43 * Must be called after ftp_server_init() and before http_server_create().
44 *
45 * @param ctx Initialized FTP server context, or NULL to detach.
46 */
47void http_api_set_server_ctx(ftp_server_context_t *ctx);
48 
49/**
50 * @brief Set the root directory for HTTP API path confinement
51 *
52 * All HTTP file operations (list, download, upload, stats) will be
53 * restricted to paths within this root. Must be called before
54 * http_server_create().
55 *
56 * FTP path confinement: ftp_path_resolve() -> ftp_path_is_within_root()
57 * HTTP path confinement: validate_path() -> http_is_within_root() [NEW]
58 */
59void http_api_set_root(const char *root);
60 
61/**
62 * @brief Get the configured HTTP root directory
63 * @return Pointer to static root buffer (empty string if unset)
64 */
65const char *http_api_get_root(void);
66 
67/**
68 * @brief Recursively compute the total size of all regular files under a directory.
69 *
70 * @param path Absolute path to the directory.
71 * @param depth Initial recursion depth (pass 0).
72 * @return Total bytes of all regular files, or 0 on error / empty dir.
73 */
74uint64_t http_dir_size_recursive(const char *path, int depth);
75 
76#endif /* HTTP_API_H */
77