Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | /*
* Copyright (c) 2020 Alexander Kozhinov <AlexanderKozhinov@yandex.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <logging/log.h>
LOG_MODULE_REGISTER(http_server_handlers, LOG_LEVEL_DBG);
#include "http_server_handlers.h"
#define TX_CHUNK_SIZE_BYTES CONFIG_NET_TX_STACK_SIZE
#define URL_MAIN "/$"
#define URL_INDEX_CSS "/index.css"
#define URL_INDEX_HTML "/index.html"
#define URL_FAVICON_ICO "/favicon.ico"
#define URL_WS_JS "/ws.js"
#define HTTP_TEXT_HTML "text/html"
#define HTTP_TEXT_CSS "text/css"
#define HTTP_TEXT_JS "text/javascript"
#define HTTP_EOFL "\r\n" /* http end of line */
#define HTTP_CONTEND_ENCODING "Content-Encoding: "
#define HTTP_ENCODING_GZ "gzip"
#define __code_decl /* static */
#define __data_decl static
__code_decl void this_register_handlers(struct mg_context *ctx);
__code_decl int this_send_buffer_chunked(struct mg_connection *conn,
const char *mime_type,
const char *buff,
const size_t buff_len);
__code_decl int this_redirect_2_index_html(struct mg_connection *conn,
void *cbdata);
__code_decl int this_index_html_handler(struct mg_connection *conn,
void *cbdata);
__code_decl int this_index_css_handler(struct mg_connection *conn,
void *cbdata);
__code_decl int this_ws_js_handler(struct mg_connection *conn,
void *cbdata);
__code_decl int this_favicon_ico_handler(struct mg_connection *conn,
void *cbdata);
__code_decl void this_set_return_value(int *ret_val);
void init_http_server_handlers(struct mg_context *ctx)
{
this_register_handlers(ctx);
}
__code_decl void this_register_handlers(struct mg_context *ctx)
{
mg_set_request_handler(ctx, URL_MAIN,
this_redirect_2_index_html, NULL);
mg_set_request_handler(ctx, URL_INDEX_HTML,
this_index_html_handler, NULL);
mg_set_request_handler(ctx, URL_INDEX_CSS,
this_index_css_handler, NULL);
mg_set_request_handler(ctx, URL_WS_JS,
this_ws_js_handler, NULL);
mg_set_request_handler(ctx, URL_FAVICON_ICO,
this_favicon_ico_handler, NULL);
}
__code_decl int this_send_buffer_chunked(struct mg_connection *conn,
const char *mime_type,
const char *buff,
const size_t buff_len)
{
int ret = 0;
ret = mg_send_http_ok(conn, mime_type, -1);
if (ret < 0) {
goto error_this_send_buffer_chunked;
}
long left_bytes = buff_len;
char *itr = (char *)buff; /* buffer iterator */
LOG_DBG("Transferring:");
LOG_DBG("itr: 0x%08X ret: %d left_bytes: %ld chunk_size: %zd B",
(unsigned int)itr, ret, left_bytes, TX_CHUNK_SIZE_BYTES);
while (left_bytes > TX_CHUNK_SIZE_BYTES) {
ret = mg_send_chunk(conn, itr, TX_CHUNK_SIZE_BYTES);
itr += TX_CHUNK_SIZE_BYTES;
left_bytes -= TX_CHUNK_SIZE_BYTES;
LOG_DBG("itr: 0x%08X ret: %d left_bytes: %ld",
(unsigned int)itr, ret, left_bytes);
if (ret < 0) {
goto error_this_send_buffer_chunked;
}
}
if (left_bytes > 0) {
ret = mg_send_chunk(conn, itr, left_bytes);
itr += left_bytes;
left_bytes = 0;
LOG_DBG("itr: 0x%08X ret: %d left_bytes: %ld",
(unsigned int)itr, ret, left_bytes);
if (ret < 0) {
goto error_this_send_buffer_chunked;
}
}
/* Must be sent at the end of the chuked sequence */
ret = mg_send_chunk(conn, "", 0);
error_this_send_buffer_chunked:
if (ret < 0) {
LOG_ERR("aborted! ret: %d", ret);
}
return ret;
}
__code_decl int this_redirect_2_index_html(struct mg_connection *conn,
void *cbdata)
{
int ret = 0;
ret = mg_send_http_redirect(conn, URL_INDEX_HTML, 303);
return 202;
}
__code_decl int this_index_html_handler(struct mg_connection *conn,
void *cbdata)
{
__data_decl const char index_html[] = {
#include "web_page/index.html.gz.inc"
};
int ret = 0;
ret = this_send_buffer_chunked(conn, HTTP_TEXT_HTML
HTTP_EOFL
HTTP_CONTEND_ENCODING
HTTP_ENCODING_GZ,
index_html, sizeof(index_html));
this_set_return_value(&ret);
return ret;
}
__code_decl int this_index_css_handler(struct mg_connection *conn, void *cbdata)
{
__data_decl const char index_css[] = {
#include "web_page/index.css.gz.inc"
};
int ret = 0;
ret = this_send_buffer_chunked(conn, HTTP_TEXT_CSS
HTTP_EOFL
HTTP_CONTEND_ENCODING
HTTP_ENCODING_GZ,
index_css, sizeof(index_css));
this_set_return_value(&ret);
return ret;
}
__code_decl int this_ws_js_handler(struct mg_connection *conn, void *cbdata)
{
__data_decl const char ws_js[] = {
#include "web_page/ws.js.gz.inc"
};
int ret = 0;
ret = this_send_buffer_chunked(conn, HTTP_TEXT_JS
HTTP_EOFL
HTTP_CONTEND_ENCODING
HTTP_ENCODING_GZ,
ws_js, sizeof(ws_js));
this_set_return_value(&ret);
return ret;
}
__code_decl int this_favicon_ico_handler(struct mg_connection *conn,
void *cbdata)
{
int ret = 404;
return ret; /* should fail */
}
__code_decl void this_set_return_value(int *ret_val)
{
if (*ret_val < 0) {
*ret_val = 404; /* 404 - HTTP FAIL or 0 - handler fail */
} else {
*ret_val = 200; /* 200 - HTTP OK*/
}
}
|