New version

This commit is contained in:
TheDoctor
2019-10-12 18:04:36 +01:00
parent b88bed3bd0
commit 6db271bd7e
64 changed files with 2972 additions and 2222 deletions

84
src/Cookies.cpp Normal file
View File

@ -0,0 +1,84 @@
// https://github.com/mfichman/http
#include "Cookies.hpp"
#include "Parse.hpp"
#include <cassert>
namespace std::net
{
ParseResult<std::string> ParseName(const char* str)
{
return ParseUntil(str, [](char ch)
{
return isspace(ch) || ch == '=';
});
}
ParseResult<std::string> ParseValue(const char* str)
{
return ParseUntil(str, [](char ch)
{
return ch == ';' || ch == '=';
});
}
ParseResult<std::string> ParseSeparator(const char* str)
{
if (*str)
{
assert(*str == ';' || *str == '=');
return ParseWhitespace(str + 1);
}
else
{
auto result = ParseResult<std::string>{};
result.ch = str;
return result;
}
}
Cookie ParseCookie(const char* str)
{
auto name = ParseName(str);
auto ch = ParseSeparator(name.ch).ch;
auto value = ParseValue(ch);
ch = ParseSeparator(value.ch).ch;
auto cookie = Cookie();
cookie.SetName(name.value);
cookie.SetValue(value.value);
while (*ch)
{
auto flag = ParseValue(ch);
if (flag.value == "Path")
{
ch = ParseSeparator(flag.ch).ch;
flag = ParseValue(ch);
cookie.SetPath(flag.value);
}
else if (flag.value == "HttpOnly")
cookie.SetHttpOnly(true);
else if (flag.value == "Secure")
cookie.SetSecure(true);
ch = ParseSeparator(flag.ch).ch;
}
return cookie;
}
Cookie::Cookie(const std::string& text)
{
*this = ParseCookie(text.c_str());
}
const Cookie Cookies::operator[](const std::string & name) const
{
auto i = m_cookie.find(name);
return (i == m_cookie.end()) ? Cookie() : i->second;
}
void Cookies::SetCookie(const Cookie& cookie)
{
m_cookie[cookie.GetName()] = cookie;
}
}