Zero-copy FTP/HTTP Daemon compatible with all POSIX systems
| 1 | #include "http_api.h" |
| 2 | #include <dirent.h> |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | static int starts_with(const char *s, const char *prefix) { |
| 7 | if ((s == NULL) || (prefix == NULL)) { |
| 8 | return 0; |
| 9 | } |
| 10 | size_t n = strlen(prefix); |
| 11 | return (strncmp(s, prefix, n) == 0) ? 1 : 0; |
| 12 | } |
| 13 | |
| 14 | int main(void) { |
| 15 | http_request_t req; |
| 16 | memset(&req, 0, sizeof(req)); |
| 17 | req.method = HTTP_METHOD_GET; |
| 18 | |
| 19 | { |
| 20 | (void)snprintf(req.uri, sizeof(req.uri), "/api/list?path=%%2F"); |
| 21 | http_response_t *resp = http_api_handle(&req); |
| 22 | if (resp == NULL) { |
| 23 | return 2; |
| 24 | } |
| 25 | |
| 26 | if ((resp->used == 0U) || (!starts_with(resp->data, "HTTP/1.1 200"))) { |
| 27 | if (resp->stream_dir != NULL) { |
| 28 | closedir((DIR *)resp->stream_dir); |
| 29 | resp->stream_dir = NULL; |
| 30 | } |
| 31 | http_response_destroy(resp); |
| 32 | return 3; |
| 33 | } |
| 34 | |
| 35 | if (resp->stream_dir != NULL) { |
| 36 | closedir((DIR *)resp->stream_dir); |
| 37 | resp->stream_dir = NULL; |
| 38 | } |
| 39 | http_response_destroy(resp); |
| 40 | } |
| 41 | |
| 42 | { |
| 43 | (void)snprintf(req.uri, sizeof(req.uri), "/api/list?path=/"); |
| 44 | http_response_t *resp = http_api_handle(&req); |
| 45 | if (resp == NULL) { |
| 46 | return 4; |
| 47 | } |
| 48 | |
| 49 | if ((resp->used == 0U) || (!starts_with(resp->data, "HTTP/1.1 200"))) { |
| 50 | if (resp->stream_dir != NULL) { |
| 51 | closedir((DIR *)resp->stream_dir); |
| 52 | resp->stream_dir = NULL; |
| 53 | } |
| 54 | http_response_destroy(resp); |
| 55 | return 5; |
| 56 | } |
| 57 | |
| 58 | if (resp->stream_dir != NULL) { |
| 59 | closedir((DIR *)resp->stream_dir); |
| 60 | resp->stream_dir = NULL; |
| 61 | } |
| 62 | http_response_destroy(resp); |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 |