BSB Developer Docs
Loading...
Searching...
No Matches
web_server_i.h
1#pragma once
2#include <furi.h>
3#include <mongoose.h>
4#include <mongoose_glue.h>
5#include <network/network.h>
6#include <storage/storage.h>
7#include <m-list.h>
8
9#define WEB_ROOT APP_ASSETS_PATH("www/")
10
11#define HEADER_CORS_ORIGIN "Access-Control-Allow-Origin: *\r\n"
12#define HEADER_CORS_METHODS "Access-Control-Allow-Methods: GET, POST, PUT, DELETE\r\n"
13#define HEADER_CORS_HEADERS "Access-Control-Allow-Headers: *\r\n"
14#define HEADER_CORS HEADER_CORS_ORIGIN HEADER_CORS_HEADERS
15#define HEADER_CONTENT_TYPE_JSON "Content-Type: application/json\r\n"
16#define HEADER_CONTENT_TYPE_IMAGE "Content-Type: image/bmp\r\n"
17
18#define DEFAULT_JSON_HEADERS HEADER_CORS HEADER_CONTENT_TYPE_JSON
19#define DEFAULT_IMAGE_HEADERS HEADER_CORS HEADER_CONTENT_TYPE_IMAGE
20
21#define RESPONSE_BODY_OK "{\"result\":\"OK\"}\n"
22
23#define _MG_OPTIONS_RESULT(conn, code) \
24 mg_http_reply(conn, code, HEADER_CORS HEADER_CORS_METHODS, "")
25
26#define _MG_JSON_RESULT(conn, code, body, ...) \
27 mg_http_reply(conn, code, DEFAULT_JSON_HEADERS, body, ##__VA_ARGS__)
28
29#define MG_REPLY_IMAGE(conn, image, size) \
30 mg_http_reply(conn, 200, DEFAULT_IMAGE_HEADERS, "%M", mg_print_base64, size, image)
31
32#define MG_REPLY_OPTIONS(conn) _MG_OPTIONS_RESULT(conn, 200)
33#define MG_REPLY_OK(conn) _MG_JSON_RESULT(conn, 200, RESPONSE_BODY_OK)
34#define MG_REPLY_OK_BODY(conn, json_body, ...) _MG_JSON_RESULT(conn, 200, json_body, ##__VA_ARGS__)
35
36// Force closing the connection - to prevent clients from reusing it
37// (e.g. after a streaming upload) and racing with the server-side FIN
38#define MG_REPLY_OK_CLOSE(conn) \
39 mg_http_reply(conn, 200, DEFAULT_JSON_HEADERS "Connection: close\r\n", RESPONSE_BODY_OK)
40
41#define MG_REPLY_ERROR_CLOSE(conn, code, ...) \
42 mg_http_reply( \
43 conn, \
44 code, \
45 DEFAULT_JSON_HEADERS "Connection: close\r\n", \
46 "{\"error\":\"%s\"}\n", \
47 M_IF_EMPTY(__VA_ARGS__)("failed", __VA_ARGS__))
48
49#define MG_REPLY_ERROR(conn, code, ...) \
50 _MG_JSON_RESULT( \
51 conn, \
52 code, \
53 "{\"error\":\"%M\"}\n", \
54 M_IF_EMPTY(__VA_ARGS__)(MG_ESC("failed"), MG_ESC(__VA_ARGS__)))
55
56#define MG_REPLY_BAD_REQUEST(conn) MG_REPLY_ERROR(conn, 400, "Bad Request")
57#define MG_REPLY_NOT_FOUND(conn) MG_REPLY_ERROR(conn, 404, "Not Found")
58#define MG_REPLY_FORBIDDEN(conn) MG_REPLY_ERROR(conn, 403, "Forbidden")
59#define MG_REPLY_INVALID_VERSION(conn) MG_REPLY_ERROR(conn, 405, "Incompatible API version")
60
61#define MG_REPLY_TIMEOUT(conn, ...) \
62 MG_REPLY_ERROR_CLOSE(conn, 408, M_IF_EMPTY(__VA_ARGS__)("Request Timeout", __VA_ARGS__))
63#define MG_REPLY_CONFLICT(conn, ...) \
64 MG_REPLY_ERROR(conn, 409, M_IF_EMPTY(__VA_ARGS__)("Conflict", __VA_ARGS__))
65
66#define MG_REPLY_PAYLOAD_TOO_LARGE(conn, ...) \
67 MG_REPLY_ERROR(conn, 413, M_IF_EMPTY(__VA_ARGS__)("Payload Too Large", __VA_ARGS__))
68
69#define MG_REPLY_SERVICE_UNAVAILABLE(conn, ...) \
70 MG_REPLY_ERROR(conn, 503, M_IF_EMPTY(__VA_ARGS__)("Service Unavailable", __VA_ARGS__))
71
72#define MG_REPLY_OVERLOADED(conn) MG_REPLY_ERROR(conn, 508, "Resource Limit Reached")
73
74#define MG_REPLY_METHOD_NOT_ALLOWED(conn, headers_cstr) \
75 mg_http_reply(conn, 405, headers_cstr, "{\"error\":\"Method Not Allowed\"}\n")
76#define MG_REPLY_CORS_OPTIONS(conn, headers_cstr) mg_http_reply(conn, 200, headers_cstr, "")
77
78#define MG_CLOSE_AFTER_HEADERS(conn, msg) \
79 mg_iobuf_del(&conn->recv, 0, msg->head.len); \
80 conn->pfn = NULL; \
81 conn->is_draining = 1;
82
83#define IS_HTTP_ENDPOINT(path) furi_string_empty(path)
84
85#define IS_WEBSOCKET_UPGRADE(msg) mg_http_get_header(msg, "Sec-WebSocket-Key") != NULL
86
87// HTTP method bitmask, can be combined to support multiple methods for the handler
88typedef enum {
89 HttpMethodUnknown = 0, // Special value, should not be used
90
91 HttpMethodGet = (1 << 0),
92 HttpMethodHead = (1 << 1),
93 HttpMethodPost = (1 << 2),
94 HttpMethodPut = (1 << 3),
95 HttpMethodDelete = (1 << 4),
96 HttpMethodConnect = (1 << 5),
97 HttpMethodOptions = (1 << 6),
98 HttpMethodTrace = (1 << 7),
99 HttpMethodPatch = (1 << 8),
100
101 HttpMethodWebSocket = (1 << 9), // WebSocket upgrade request
102
103 HttpMethodAny = 0xFFFFFFFF,
104} HttpMethod;
105
106typedef struct {
107 char* uri;
108 HttpMethod method;
109 enum {
110 HttpHandlerCustom,
111 HttpHandlerFile,
112 HttpHandlerDir,
113 } type;
114
115 union {
116 struct {
117 char* path;
118 char* mime_types_custom;
119 char* extra_headers;
120 };
121 struct {
122 void* (*ctx_alloc)(void);
123 void (*ctx_free)(void*);
124 bool (*on_request)(
125 FuriString* path,
126 HttpMethod method,
127 struct mg_connection* conn,
128 struct mg_http_message* msg,
129 void* ctx);
130 bool (*on_headers)(
131 FuriString* path,
132 HttpMethod method,
133 struct mg_connection* conn,
134 struct mg_http_message* msg,
135 void* ctx);
136 };
137 };
139
140typedef struct {
141 const HttpHandler* handler;
142 void* context;
144LIST_DEF(HttpHandlersList, HttpHandlerInstance, M_POD_OPLIST);
145
146typedef union {
147 struct {
148 union {
149 struct {
150 void (*on_open)(struct mg_connection* conn);
151 void (*on_message)(struct mg_connection* conn, struct mg_ws_message* ws_msg);
152 } ws;
153 struct {
154 void (*on_data)(struct mg_connection* conn, struct mg_iobuf* data);
155 void (*on_poll)(struct mg_connection* conn);
156 } raw;
157 };
158 void (*on_close)(struct mg_connection* conn);
159 void (*on_wakeup)(struct mg_connection* conn, void* data, size_t len);
160 void* context;
161 bool is_js;
162 };
163 uint8_t data[MG_DATA_SIZE];
165static_assert(sizeof(ConnectionContext) == MG_DATA_SIZE);
166
167bool http_handle_request(
168 FuriString* path,
169 HttpMethod method,
170 HttpHandlersList_t handlers,
171 struct mg_connection* conn,
172 struct mg_http_message* msg);
173
174bool http_handle_headers(
175 FuriString* path,
176 HttpMethod method,
177 HttpHandlersList_t handlers,
178 struct mg_connection* conn,
179 struct mg_http_message* msg);
180
181void http_handler_add(HttpHandlersList_t list, const HttpHandler* handler);
182
183void http_handler_remove(HttpHandlersList_t list, const HttpHandler* handler);
184
185void http_handler_remove_all(HttpHandlersList_t list);
186
187void http_reply_405_method_not_allowed(
188 struct mg_connection* conn,
189 HttpMethod allowed_methods,
190 bool close);
191void http_reply_cors_preflight(struct mg_connection* conn, HttpMethod allowed_methods);
192
193void http_upload_start(
194 struct mg_connection* conn,
195 struct mg_http_message* msg,
196 const char* file_path,
197 bool append);
198
199struct mg_mgr* web_srv_get_mgr(void);
Network TCP/IP stack API.
APIs for working with storages, directories and files.
Definition web_server_i.h:106
Definition web_server_i.h:140
Definition web_server_i.h:146