Webserv
Loading...
Searching...
No Matches
Headers.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <sstream>
5#include <string>
6
7namespace http {
8
17class Headers {
18public:
19 typedef std::map<std::string, std::string> HeaderMap;
20 typedef HeaderMap::iterator iterator;
21 typedef HeaderMap::const_iterator const_iterator;
22
23public:
27 Headers();
28
34 bool has(std::string const &key) const;
35
42 Headers &add(std::string const &key, std::string const &value);
43
50 bool get(const std::string &key, std::string &value) const;
51
57 std::string get(const std::string &key) const;
58
59
65 size_t getContentLength() const;
66
71 bool isContentChunked() const;
72
77 Headers &clear();
78
84 Headers &erase(std::string const &key);
85
91 std::string toString() const;
92
98 HeaderMap const &getMap() const;
99
100 const_iterator find(std::string const &key) const;
101 const_iterator begin() const;
102 const_iterator end() const;
103
104public:
113 static bool parse(std::string &, Headers &, bool strict = false);
114
116 static bool parse(std::istringstream &, Headers &, bool strict = false);
117
125 static size_t findHeaderEnd(const std::string &buffer, size_t &offset);
126
127private:
132 static void normalizeKey(std::string &key);
133
138 static std::string normalizeKey(std::string const &key);
139
146};
147
148} // namespace http
static size_t findHeaderEnd(const std::string &buffer, size_t &offset)
Finds the end of the HTTP header section in a buffer. Compatible with \r \r , , \r ,...
Definition Headers.cpp:118
static bool parse(std::string &, Headers &, bool strict=false)
Parses a raw string of headers into a provided Headers reference. The string should contain multiple ...
Definition Headers.cpp:77
bool get(const std::string &key, std::string &value) const
C++98-style getter. Retrieves a header value.
Definition Headers.cpp:27
HeaderMap::iterator iterator
Definition Headers.hpp:20
bool isContentChunked() const
Checks if the 'Transfer-Encoding' header is set to 'chunked'.
Definition Headers.cpp:82
std::string toString() const
Serializes all headers into a single string. Each header is on its own line, formatted as "Key: Value...
Definition Headers.cpp:100
const_iterator find(std::string const &key) const
Definition Headers.cpp:114
HeaderMap::const_iterator const_iterator
Definition Headers.hpp:21
static void normalizeKey(std::string &key)
Internal: Normalizes a key to lowercase in-place.
Definition Headers.cpp:10
Headers & add(std::string const &key, std::string const &value)
Adds or overwrites a header key-value pair.
Definition Headers.cpp:20
std::map< std::string, std::string > HeaderMap
Definition Headers.hpp:19
Headers & erase(std::string const &key)
Removes a single header by its key.
Definition Headers.cpp:95
Headers & clear()
Clears all headers from the map.
Definition Headers.cpp:90
HeaderMap map_
The internal storage for headers, mapping lowercase_key -> Original_Value.
Definition Headers.hpp:145
HeaderMap const & getMap() const
Returns a const reference to the internal header map. Useful for iterating over all headers (e....
Definition Headers.cpp:109
const_iterator end() const
Definition Headers.cpp:112
size_t getContentLength() const
A specialized accessor for the Content-Length header.
Definition Headers.cpp:41
bool has(std::string const &key) const
Checks if a header with the given key exists.
Definition Headers.cpp:110
Headers()
Constructs an empty Headers object.
Definition Headers.cpp:8
const_iterator begin() const
Definition Headers.cpp:111
Definition IArgument.hpp:5