DeterministicESPAsyncWebServer v6.28.0
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
spa_router.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file spa_router.cpp
6 * @brief Single-page-app micro-routing decision (see spa_router.h).
7 */
8
10
11#if DETWS_ENABLE_SPA_ROUTER
12
13#include <string.h>
14
15bool detws_spa_has_extension(const char *path)
16{
17 if (!path)
18 return false;
19 // Look at the last path segment; an extension is a '.' after the last '/', not at the segment start.
20 const char *slash = strrchr(path, '/');
21 const char *seg = slash ? slash + 1 : path;
22 const char *dot = strrchr(seg, '.');
23 return dot && dot != seg && dot[1] != '\0';
24}
25
26DetwsSpaAction detws_spa_route(const char *path, const char *api_prefix)
27{
28 if (!path || path[0] == '\0' || (path[0] == '/' && path[1] == '\0'))
29 return DetwsSpaAction::DETWS_SPA_SERVE_SHELL; // "" or "/" -> the app shell
30
31 if (api_prefix && api_prefix[0])
32 {
33 size_t pl = strnlen(api_prefix, MAX_PATH_LEN + 1);
34 if (strncmp(path, api_prefix, pl) == 0)
35 return DetwsSpaAction::DETWS_SPA_PASSTHROUGH; // "/api/..." -> handlers
36 }
37
38 return detws_spa_has_extension(path) ? DetwsSpaAction::DETWS_SPA_SERVE_FILE : DetwsSpaAction::DETWS_SPA_SERVE_SHELL;
39}
40
41#endif // DETWS_ENABLE_SPA_ROUTER
#define MAX_PATH_LEN
Maximum URL path length (including leading /).
Single-page-app micro-routing decision (DETWS_ENABLE_SPA_ROUTER).