forked from PJayB/HTTP
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HTTPResponse.cpp
181 lines (148 loc) · 2.98 KB
/
HTTPResponse.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "HTTP.h"
#include <sstream>
#include <time.h>
namespace HTTP
{
#define HTTP_LINE_ENDING "\r\n";
ResponseHeaderBuilder::ResponseHeaderBuilder()
: Protocol(PROTOCOL_HTTP_1_1)
, Code(RESPONSE_OK)
, Method(METHOD_GET)
, AuthMode(AUTH_NONE)
{
}
ResponseHeaderBuilder& ResponseHeaderBuilder::AddKey(
StringRef A,
StringRef B)
{
if (A.size())
{
m_ExtraLines[A] = B;
}
return *this;
}
RESPONSE_HEADER_RESULT
ResponseHeaderBuilder::Build(
String& OutResponse) const
{
std::stringstream response;
response << ProtocolToString(Protocol) << " ";
//
// Some codes need special cases
//
if (Code == RESPONSE_MOVED ||
Code == RESPONSE_FOUND)
{
if (!RedirectURI.size())
{
return RESPONSE_HEADER_NEED_REDIRECT_URI;
}
response
<< "URI: "
<< RedirectURI
<< HTTP_LINE_ENDING;
}
else if (Code == RESPONSE_METHOD)
{
if (!RedirectURI.size())
{
return RESPONSE_HEADER_NEED_REDIRECT_URI;
}
response
<< "Method: "
<< MethodToString(Method)
<< " "
<< RedirectURI
<< HTTP_LINE_ENDING;
}
else
{
response << Code << " " << ResponseCodeToString(Code) << HTTP_LINE_ENDING;
}
//
// If we require authentication, say as much:
//
if (Code == RESPONSE_UNAUTHORISED)
{
if (AuthMode == AUTH_NONE)
{
return RESPONSE_HEADER_NEED_AUTH_MODE;
}
if (!AuthRealm.size())
{
return RESPONSE_HEADER_NEED_AUTH_REALM;
}
response
<< "WWW-Authenticate: "
<< AuthModeToString(AuthMode)
<< "Realm=\""
<< AuthRealm
<< "\""
<< HTTP_LINE_ENDING;
}
//
// Add the keys
//
for (auto pair : m_ExtraLines)
{
response << pair.first << ":";
if (pair.second.size())
response << " " << pair.second;
response << HTTP_LINE_ENDING;
}
// Terminating line break
response << HTTP_LINE_ENDING;
OutResponse = response.str();
return RESPONSE_HEADER_OK;
}
String IntToStr(SIZE_T T)
{
std::stringstream s;
s << T;
return s.str();
}
RESPONSE_HEADER_RESULT ResponseHeaderBuilder::AddBinaryHeaders(
SIZE_T ContentLength,
LPCSTR MimeType)
{
if (!MimeType || !*MimeType)
{
return RESPONSE_HEADER_NEED_CONTENT_MIME;
}
AddKey("Content-Type", MimeType);
AddKey("Content-Length", IntToStr(ContentLength));
AddKey("Connection", "close");
return RESPONSE_HEADER_OK;
}
RESPONSE_HEADER_RESULT ResponseHeaderBuilder::AddTextHeaders(
SIZE_T ContentLength,
LPCSTR MimeType,
LPCSTR Encoding)
{
if (!MimeType || !*MimeType ||
!Encoding || !*Encoding)
{
return RESPONSE_HEADER_NEED_CONTENT_MIME;
}
String mimeAndEncoding = MimeType;
mimeAndEncoding += "; ";
mimeAndEncoding += Encoding;
AddKey("Content-Type", mimeAndEncoding);
AddKey("Content-Length", IntToStr(ContentLength));
AddKey("Connection", "close");
return RESPONSE_HEADER_OK;
}
String TimeStampString()
{
struct tm timeinfo;
time_t rawtime;
time(&rawtime);
localtime_s(&timeinfo, &rawtime);
char time_str[256];
asctime_s(time_str, sizeof(time_str), &timeinfo);
// chop of \n
size_t len = strlen(time_str);
time_str[len-1] = 0;
return time_str;
}
}