ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
origin.cpp
Go to the documentation of this file.
1#include "origin.h"
2
3#include <cstring>
4
6
8
9bool is_origin_allowed(const char *origin, size_t origin_len, const char *host, size_t host_len,
10 const char *const *allowed) {
11 if (origin_len == 0)
12 return true;
13
14 if (host_len != 0) {
15 // Same-origin: covers the device's IP, mDNS name or DNS name without knowing any at compile time.
16 // The scheme has no ':', so the first one starts "://".
17 const char *colon = static_cast<const char *>(memchr(origin, ':', origin_len));
18 if (colon != nullptr) {
19 const size_t authority = colon - origin + 3;
20 if (authority <= origin_len && colon[1] == '/' && colon[2] == '/' &&
21 StringRef(origin + authority, origin_len - authority) == StringRef(host, host_len))
22 return true;
23 }
24 }
25
26 if (allowed == nullptr)
27 return false;
28 const StringRef origin_ref(origin, origin_len);
29 for (; *allowed != nullptr; allowed++) {
30 if (origin_ref == *allowed)
31 return true;
32 }
33 return false;
34}
35
36} // namespace esphome::web_server_base
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
static bool const char *const * allowed
const AsyncWebHeader * origin
char host[MAX_ORIGIN_LENGTH+1]
bool is_origin_allowed(const char *origin, size_t origin_len, const char *host, size_t host_len, const char *const *allowed)
Decide whether a browser request from the given Origin may reach a handler.
Definition origin.cpp:9