-
Notifications
You must be signed in to change notification settings - Fork 17
/
HTTPHeaders.h
119 lines (106 loc) · 2.95 KB
/
HTTPHeaders.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// --------------------------------------------------------------------------
//
// File
// Name: HTTPHeaders.h
// Purpose: Utility class to decode HTTP headers
// Created: 16/8/2015
//
// --------------------------------------------------------------------------
#ifndef HTTPHEADERS__H
#define HTTPHEADERS__H
#include <string>
#include <vector>
#include "autogen_HTTPException.h"
#include "IOStream.h"
class IOStreamGetLine;
// --------------------------------------------------------------------------
//
// Class
// Name: HTTPHeaders
// Purpose: Utility class to decode HTTP headers
// Created: 16/8/2015
//
// --------------------------------------------------------------------------
class HTTPHeaders
{
public:
HTTPHeaders()
: mKeepAlive(false),
mHostPort(80), // default if not specified
mContentLength(-1)
{ }
virtual ~HTTPHeaders() { }
// copying is fine
void ReadFromStream(IOStreamGetLine &rGetLine, int Timeout);
void ParseHeaderLine(const std::string& line);
void AddHeader(const std::string& name, const std::string& value);
void WriteTo(IOStream& rOutput, int Timeout) const;
typedef std::pair<std::string, std::string> Header;
bool GetHeader(const std::string& name, std::string* pValueOut) const
{
const std::string lc_name = ToLowerCase(name);
for (std::vector<Header>::const_iterator
i = mExtraHeaders.begin();
i != mExtraHeaders.end(); i++)
{
if (i->first == lc_name)
{
*pValueOut = i->second;
return true;
}
}
return false;
}
std::string GetHeaderValue(const std::string& name, bool required = true) const
{
std::string value;
if (GetHeader(name, &value))
{
return value;
}
if(required)
{
THROW_EXCEPTION_MESSAGE(CommonException, ConfigNoKey,
"Expected header was not present: " << name);
}
else
{
return "";
}
}
const std::vector<Header> GetExtraHeaders() const { return mExtraHeaders; }
void SetKeepAlive(bool KeepAlive) {mKeepAlive = KeepAlive;}
bool IsKeepAlive() const {return mKeepAlive;}
void SetContentType(const std::string& rContentType)
{
mContentType = rContentType;
}
const std::string& GetContentType() const { return mContentType; }
void SetContentLength(int64_t ContentLength) { mContentLength = ContentLength; }
int64_t GetContentLength() const { return mContentLength; }
const std::string &GetHostName() const {return mHostName;}
const int GetHostPort() const {return mHostPort;}
std::string GetHostNameWithPort() const;
void SetHostName(const std::string& rHostName)
{
AddHeader("host", rHostName);
}
private:
bool mKeepAlive;
std::string mContentType;
std::string mHostName;
int mHostPort;
int64_t mContentLength; // only used when reading response from stream
std::vector<Header> mExtraHeaders;
std::string ToLowerCase(const std::string& input) const
{
std::string output = input;
for (std::string::iterator c = output.begin();
c != output.end(); c++)
{
*c = tolower(*c);
}
return output;
}
};
#endif // HTTPHEADERS__H