forked from cloudbase/EHS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpresponse.cpp
188 lines (168 loc) · 6.21 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
182
183
184
185
186
187
188
/* $Id$
*
* EHS is a library for embedding HTTP(S) support into a C++ application
*
* Copyright (C) 2004 Zachary J. Hansen
*
* Code cleanup, new features and bugfixes: Copyright (C) 2010 Fritz Elfert
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation;
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* This can be found in the 'COPYING' file.
*
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef _MSC_VER
#pragma warning(disable : 4786)
#endif
#include "httpresponse.h"
#include "formvalue.h"
#include "httprequest.h"
#include "datum.h"
#include "ehsconnection.h"
#include <ctime>
#include <clocale>
#include <cstring>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <boost/assign.hpp>
using namespace std;
string HttpResponse::HttpTime ( time_t stamp )
{
char buf[30];
char *ol = setlocale(LC_TIME, "C"); // We want english (C)
strftime ( buf, 30, "%a, %d %b %Y %H:%M:%S GMT", gmtime( &stamp ));
setlocale(LC_TIME, ol); // restore locale
return string(buf);
}
////////////////////////////////////////////////////////////////////
// HTTP RESPONSE
//
////////////////////////////////////////////////////////////////////
HttpResponse::HttpResponse ( int inResponseId,
EHSConnection * ipoEHSConnection )
: GenericResponse(inResponseId, ipoEHSConnection)
, m_nResponseCode(HTTPRESPONSECODE_INVALID)
, m_oResponseHeaders(StringCaseMap())
, m_oCookieList (StringList())
{
// General Header Fields (HTTP 1.1 Section 4.5)
time_t t = time ( NULL );
SetDate(t);
SetLastModified(t);
m_oResponseHeaders [ "Cache-Control" ] = "no-cache";
m_oResponseHeaders [ "Content-Type" ] = "text/html";
m_oResponseHeaders [ "Content-Length" ] = "0";
}
///< HTTP response code to get text version of
const char *HttpResponse::GetPhrase(ResponseCode code)
{
static map<int, const char *> phrases = boost::assign::map_list_of
(HTTPRESPONSECODE_200_OK, "OK")
(HTTPRESPONSECODE_101_SWITCHING_PROTOCOLS, "Switching Protocols")
(HTTPRESPONSECODE_301_MOVEDPERMANENTLY, "Moved Permanently")
(HTTPRESPONSECODE_302_FOUND, "FOUND")
(HTTPRESPONSECODE_304_NOT_MODIFIED, "Not modified")
(HTTPRESPONSECODE_400_BADREQUEST, "Bad request")
(HTTPRESPONSECODE_401_UNAUTHORIZED, "Unauthorized")
(HTTPRESPONSECODE_403_FORBIDDEN, "Forbidden")
(HTTPRESPONSECODE_404_NOTFOUND, "Not Found")
(HTTPRESPONSECODE_413_TOOLARGE, "Request entity too large")
(HTTPRESPONSECODE_426_UPGRADE_REQUIRED, "Upgrade required")
(HTTPRESPONSECODE_500_INTERNALSERVERERROR, "Internal Server Error")
(HTTPRESPONSECODE_503_SERVICEUNAVAILABLE, "Service Unavailable");
map<int, const char *>::const_iterator i = phrases.find(code);
return (phrases.end() == i) ? "INVALID" : i->second;
}
std::string HttpResponse::GetStatusString()
{
ostringstream oss;
oss << m_nResponseCode << " " << GetPhrase(m_nResponseCode);
std::string ret(oss.str());
return ret;
}
HttpResponse *HttpResponse::Error(ResponseCode code, int inResponseId, EHSConnection * ipoEHSConnection)
{
HttpResponse *ret = new HttpResponse(inResponseId, ipoEHSConnection);
ret->SetResponseCode(code);
ostringstream oss;
oss << "<html><head><title>" << code << " " << GetPhrase(code)
<< "</title><body>" << code << " " << GetPhrase(code) << "</body></html>";
ret->SetBody(oss.str().c_str(), oss.str().length());
return ret;
}
HttpResponse *HttpResponse::Error (ResponseCode code, HttpRequest *request)
{
return Error(code, request->Id(), request->Connection());
}
void HttpResponse::SetDate ( time_t stamp )
{
m_oResponseHeaders [ "Date" ] = HttpTime( stamp );
}
void HttpResponse::SetLastModified ( time_t stamp )
{
m_oResponseHeaders [ "Last-Modified" ] = HttpTime( stamp );
}
// sets information regarding the body of the HTTP response
// sent back to the client
void HttpResponse::SetBody ( const char * ipsBody, ///< body to return to user
size_t inBodyLength ///< length of the body
)
{
GenericResponse::SetBody(ipsBody, inBodyLength);
ostringstream oss;
oss << m_sBody.length();
m_oResponseHeaders [ "Content-Length" ] = oss.str();
}
// this will send stuff if it's not valid..
void HttpResponse::SetCookie ( CookieParameters & iroCookieParameters )
{
// name and value must be set
if ( iroCookieParameters [ "name" ] != "" &&
iroCookieParameters [ "value" ] != "" ) {
ostringstream ssBuffer;
ssBuffer << iroCookieParameters["name"].GetCharString ( ) <<
"=" << iroCookieParameters["value"].GetCharString ( );
// Version should have capital V according to RFC 2109
if ( iroCookieParameters [ "Version" ] == "" ) {
iroCookieParameters [ "Version" ] = "1";
}
for ( CookieParameters::iterator i = iroCookieParameters.begin ( );
i != iroCookieParameters.end ( ); ++i ) {
if ( i->first != "name" && i->first != "value" ) {
ssBuffer << "; " << i->first << "=" << i->second.GetCharString ( );
}
}
m_oCookieList.push_back ( ssBuffer.str ( ) );
} else {
#ifdef EHS_DEBUG
cerr << "Cookie set with insufficient data -- requires name and value" << endl;
#endif
}
}
void GenericResponse::EnableIdleTimeout(bool enable)
{
if (m_poEHSConnection) {
m_poEHSConnection->EnableIdleTimeout(enable);
}
}
void GenericResponse::EnableKeepAlive(bool enable)
{
if (m_poEHSConnection) {
m_poEHSConnection->EnableKeepAlive(enable);
}
}