47 lines
1.2 KiB
C++
Executable File
47 lines
1.2 KiB
C++
Executable File
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <cstdint>
|
|
|
|
namespace timbre {
|
|
|
|
inline String cookieValue(const String& cookieHeader, const char* name) {
|
|
if (cookieHeader.isEmpty() || name == nullptr) {
|
|
return String();
|
|
}
|
|
const String prefix = String(name) + "=";
|
|
std::size_t start = 0;
|
|
while (start < cookieHeader.length()) {
|
|
std::size_t end = cookieHeader.indexOf(';', start);
|
|
if (end == String::npos) {
|
|
end = cookieHeader.length();
|
|
}
|
|
String item = cookieHeader.substring(start, end);
|
|
item.trim();
|
|
if (item.startsWith(prefix)) {
|
|
String value = item.substring(prefix.length());
|
|
value.trim();
|
|
if (value.length() <= 128) {
|
|
return value;
|
|
}
|
|
return String();
|
|
}
|
|
start = end + 1;
|
|
}
|
|
return String();
|
|
}
|
|
|
|
inline String sessionCookieHeader(const String& token,
|
|
std::uint32_t maxAgeSeconds) {
|
|
return String("Set-Cookie: esp_sid=") + token +
|
|
"; Path=/; HttpOnly; SameSite=Strict; Max-Age=" +
|
|
String(maxAgeSeconds);
|
|
}
|
|
|
|
inline String clearSessionCookieHeader() {
|
|
return F("Set-Cookie: esp_sid=; Path=/; HttpOnly; SameSite=Strict; Max-Age=0");
|
|
}
|
|
|
|
} // namespace timbre
|