| 2 |
raymond |
1 |
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
2 |
// Copyright 2016-2026 Hristo Gochkov, Mathieu Carbou, Emil Muratov, Will Miles
|
|
|
3 |
|
|
|
4 |
#include <ESPAsyncWebServer.h>
|
|
|
5 |
|
|
|
6 |
const AsyncWebHeader AsyncWebHeader::parse(const char *data) {
|
|
|
7 |
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers
|
|
|
8 |
// In HTTP/1.X, a header is a case-insensitive name followed by a colon, then optional whitespace which will be ignored, and finally by its value
|
|
|
9 |
if (!data) {
|
|
|
10 |
return AsyncWebHeader(); // nullptr
|
|
|
11 |
}
|
|
|
12 |
if (data[0] == '\0') {
|
|
|
13 |
return AsyncWebHeader(); // empty string
|
|
|
14 |
}
|
|
|
15 |
if (strchr(data, '\n') || strchr(data, '\r')) {
|
|
|
16 |
return AsyncWebHeader(); // Invalid header format
|
|
|
17 |
}
|
|
|
18 |
const char *colon = strchr(data, ':');
|
|
|
19 |
if (!colon) {
|
|
|
20 |
return AsyncWebHeader(); // separator not found
|
|
|
21 |
}
|
|
|
22 |
if (colon == data) {
|
|
|
23 |
return AsyncWebHeader(); // Header name cannot be empty
|
|
|
24 |
}
|
|
|
25 |
const char *startOfValue = colon + 1; // Skip the colon
|
|
|
26 |
// skip one optional whitespace after the colon
|
|
|
27 |
if (*startOfValue == ' ') {
|
|
|
28 |
startOfValue++;
|
|
|
29 |
}
|
|
|
30 |
String name;
|
|
|
31 |
name.reserve(colon - data);
|
|
|
32 |
name.concat(data, colon - data);
|
|
|
33 |
return AsyncWebHeader(name, String(startOfValue));
|
|
|
34 |
}
|