-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.cpp
73 lines (62 loc) · 2.06 KB
/
connection.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
#include <QtCore/QUrlQuery>
#include <QtCore/QDateTime>
#include <QtCore/QStringBuilder>
#include <QtNetwork/QTcpSocket>
#include "connection.h"
namespace com { namespace cutehacks { namespace auctor {
static const char* RESPONSE_HEADER =
"HTTP/1.1 200 OK\n"
"content-type: text/html; charset=utf-8\n"
"Cache-Control: no-cache\n"
"Expires: Fri, 01 Jan 1990 00:00:00 GMT\n"
"Content-Length: %1\n"
"Server: Auctor/1.0\n"
"Date: %2\n"
"\n";
static const char* RESPONSE_BODY =
"<!DOCTYPE html>\n"
"<html lang=\"en\">\n"
"<head>\n"
"</head>\n"
"<body>\n"
" <p>You may now close this tab.</p>\n"
"</body>\n"
"</html>\n";
Connection::Connection(QObject *parent, qintptr socketDescriptor) :
QObject(parent),
m_socketDescriptor(socketDescriptor)
{}
void Connection::run()
{
QTcpSocket *socket = new QTcpSocket();
socket->setSocketDescriptor(m_socketDescriptor);
if (socket->waitForReadyRead()) {
QList<QByteArray> requestLine = socket->readLine().split(QChar::Space);
QString method = requestLine.at(0);
QString uri = requestLine.at(1);
int start = uri.indexOf("?") + 1;
if (start > 0) {
QVariantMap params;
QUrlQuery query(uri.mid(start));
QList<QPair<QString, QString> > items = query.queryItems();
QList<QPair<QString, QString> >::const_iterator it = items.constBegin();
while (it != items.constEnd()) {
params.insert((*it).first, (*it).second);
it++;
}
emit receivedParams(params);
}
// Write a response
const QDateTime t = QDateTime::currentDateTime();
QString response = QString(RESPONSE_HEADER)
.arg(qstrlen(RESPONSE_BODY))
.arg(t.toString("ddd, dd MMM yyyy HH:mm:ss GMT"));
response = response % QString(RESPONSE_BODY);
socket->write(response.toUtf8());
socket->flush();
socket->waitForBytesWritten();
}
socket->close();
delete socket;
}
} } }