forked from nikhilm/qhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqhttpresponse.cpp
More file actions
196 lines (167 loc) · 5.76 KB
/
qhttpresponse.cpp
File metadata and controls
196 lines (167 loc) · 5.76 KB
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
189
190
191
192
193
194
195
196
/*
* Copyright 2011-2014 Nikhil Marathe <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "qhttpresponse.h"
#include <QDateTime>
#include <QLocale>
#include "qhttpserver.h"
#include "qhttpconnection.h"
QHttpResponse::QHttpResponse(QHttpConnection *connection)
// TODO: parent child relation
: QObject(0),
m_connection(connection),
m_headerWritten(false),
m_sentConnectionHeader(false),
m_sentContentLengthHeader(false),
m_sentTransferEncodingHeader(false),
m_sentDate(false),
m_keepAlive(true),
m_last(false),
m_useChunkedEncoding(false),
m_finished(false)
{
connect(m_connection, SIGNAL(allBytesWritten()), this, SIGNAL(allBytesWritten()));
}
QHttpResponse::~QHttpResponse()
{
}
void QHttpResponse::setHeader(const QString &field, const QString &value)
{
if (!m_finished)
m_headers[field] = value;
else
qWarning() << "QHttpResponse::setHeader() Cannot set headers after response has finished.";
}
void QHttpResponse::writeHeader(const char *field, const QString &value)
{
if (!m_finished) {
m_connection->write(field);
m_connection->write(": ");
m_connection->write(value.toUtf8());
m_connection->write("\r\n");
} else
qWarning()
<< "QHttpResponse::writeHeader() Cannot write headers after response has finished.";
}
void QHttpResponse::writeHeaders()
{
if (m_finished)
return;
foreach(const QString & name, m_headers.keys()) {
QString value = m_headers[name];
if (name.compare("connection", Qt::CaseInsensitive) == 0) {
m_sentConnectionHeader = true;
if (value.compare("close", Qt::CaseInsensitive) == 0)
m_last = true;
else
m_keepAlive = true;
} else if (name.compare("transfer-encoding", Qt::CaseInsensitive) == 0) {
m_sentTransferEncodingHeader = true;
if (value.compare("chunked", Qt::CaseInsensitive) == 0)
m_useChunkedEncoding = true;
} else if (name.compare("content-length", Qt::CaseInsensitive) == 0)
m_sentContentLengthHeader = true;
else if (name.compare("date", Qt::CaseInsensitive) == 0)
m_sentDate = true;
/// @todo Expect case (??)
writeHeader(name.toLatin1(), value.toLatin1());
}
if (!m_sentConnectionHeader) {
if (m_keepAlive && (m_sentContentLengthHeader || m_useChunkedEncoding)) {
writeHeader("Connection", "keep-alive");
} else {
m_last = true;
writeHeader("Connection", "close");
}
}
if (!m_sentContentLengthHeader && !m_sentTransferEncodingHeader) {
if (m_useChunkedEncoding)
writeHeader("Transfer-Encoding", "chunked");
else
m_last = true;
}
// Sun, 06 Nov 1994 08:49:37 GMT - RFC 822. Use QLocale::c() so english is used for month and
// day.
if (!m_sentDate)
writeHeader("Date",
QLocale::c().toString(QDateTime::currentDateTimeUtc(),
"ddd, dd MMM yyyy hh:mm:ss") + " GMT");
}
void QHttpResponse::writeHead(int status)
{
if (m_finished) {
qWarning()
<< "QHttpResponse::writeHead() Cannot write headers after response has finished.";
return;
}
if (m_headerWritten) {
qWarning() << "QHttpResponse::writeHead() Already called once for this response.";
return;
}
m_connection->write(
QString("HTTP/1.1 %1 %2\r\n").arg(status).arg(STATUS_CODES[status]).toLatin1());
writeHeaders();
m_connection->write("\r\n");
m_headerWritten = true;
}
void QHttpResponse::writeHead(StatusCode statusCode)
{
writeHead(static_cast<int>(statusCode));
}
void QHttpResponse::write(const QByteArray &data)
{
if (m_finished) {
qWarning() << "QHttpResponse::write() Cannot write body after response has finished.";
return;
}
if (!m_headerWritten) {
qWarning() << "QHttpResponse::write() You must call writeHead() before writing body data.";
return;
}
m_connection->write(data);
}
void QHttpResponse::flush()
{
m_connection->flush();
}
void QHttpResponse::waitForBytesWritten()
{
m_connection->waitForBytesWritten();
}
void QHttpResponse::end(const QByteArray &data)
{
if (m_finished) {
qWarning() << "QHttpResponse::end() Cannot write end after response has finished.";
return;
}
if (data.size() > 0)
write(data);
m_finished = true;
Q_EMIT done();
/// @todo End connection and delete ourselves. Is this a still valid note?
deleteLater();
}
void QHttpResponse::connectionClosed()
{
m_finished = true;
Q_EMIT done();
deleteLater();
}