aboutsummaryrefslogtreecommitdiffstats
path: root/lib/JsonScgiPeer.cpp
blob: 093e3808505d840bb52b5e57e2f7872a4b552d99 (plain) (blame)
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
/*
 * Copyright (C) 2012 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 *
 * See COPYING for license information.
 *
 */

#include "JsonScgiPeer.h"
#include "JsonScgiServer.h"
#include "QtJson.h"
#include <QtCore/QTimer>
#include <QtCore/QStringList>
#include <cstring>

using namespace QtJson;

JsonScgiPeer::JsonScgiPeer(QTcpSocket *connection, JsonScgiServer *parent) :
	QObject(parent),
	m_connection(connection),
	m_server(parent),
	m_jsonOffset(0),
	m_jsonLength(0),
	m_https(false)
{
	if (!connection->isOpen())
		QTimer::singleShot(0, this, SLOT(disconnected()));
	if (connection->bytesAvailable() > 0)
		QTimer::singleShot(0, this, SLOT(readyRead()));
	connect(connection, SIGNAL(disconnected()), this, SLOT(disconnected()));
	connect(connection, SIGNAL(readyRead()), this, SLOT(readyRead()));
}

JsonScgiPeer::~JsonScgiPeer()
{
	m_connection->deleteLater();
}

void JsonScgiPeer::sendResponse(const QVariantMap &response)
{
	m_outputBuffer = "Status: 200 OK\r\nContent-Type: application/json\r\n"
			 "Set-Cookie: JSONSCGIQTSESSION=" + m_sessionCookie.toAscii() + "; path=/; expires=" +
			 QLocale("C").toString(m_server->cookieExpiration(m_sessionCookie), "ddd, dd MMM yyyy hh:mm:ss").toAscii() +
			 " GMT; HttpOnly" + (m_https ? "; secure" : "") + "\r\n\r\n" + Json::serialize(response) + "\n";
	QTimer::singleShot(0, this, SLOT(readyWrite()));
}

void JsonScgiPeer::die()
{
	m_connection->close();
}

void JsonScgiPeer::notFound()
{
	m_outputBuffer = "Status: 404 Not Found\r\nContent-Type: text/plain\r\n\r\nThese aren't the droids you're looking for.\n";
	QTimer::singleShot(0, this, SLOT(readyWrite()));
}

// Implements http://python.ca/scgi/protocol.txt
void JsonScgiPeer::parseScgiHeader()
{
	int i = m_inputBuffer.indexOf(':');
	if (i == -1)
		return;
	bool ok;
	m_jsonOffset = m_inputBuffer.left(i).toUInt(&ok) + 2 + i;
	if (!ok)
		return;

	if (static_cast<unsigned int>(m_inputBuffer.size()) < m_jsonOffset)
		return;

	++i;

	// Sure, hate me. But sometimes C strings are just simpler,
	// especially when you're dealing with a null-terminated spec.
	// Besides, Qt ensures that data() will be null terminated so
	// that we don't risk any overruns.
	const char *key, *value;
	const char *data = m_inputBuffer.constData();
	while (static_cast<unsigned int>(i) < m_jsonOffset) {
		if (data[i] == ',')
			break;
		key = data + i;
		i += strlen(key) + 1;
		if (static_cast<unsigned int>(i) >= m_jsonOffset)
			break;
		value = data + i;
		i += strlen(value) + 1;

		if (!strcmp(key, "CONTENT_LENGTH"))
			m_jsonLength = QString(value).toUInt();
		else if (!strcmp(key, "REQUEST_URI"))
			m_url.setUrl(value);
		else if (!strcmp(key, "HTTP_COOKIE")) {
			QString cookieList(value);
			QStringList cookies = cookieList.split(';', QString::SkipEmptyParts);
			foreach (const QString &cookie, cookies) {
				QStringList cookiePair = cookie.split('=', QString::SkipEmptyParts);
				if (cookiePair.size() != 2)
					continue;
				if (cookiePair.at(0).trimmed() == "JSONSCGIQTSESSION") {
					m_sessionCookie = cookiePair.at(1);
					break;
				}
			}
		} else if (!strcmp(key, "HTTPS"))
			m_https = true;
	}

	if (!m_jsonLength)
		m_server->newWebRequest(m_url, m_sessionCookie, QVariantMap(), *this);
	else if (static_cast<unsigned int>(m_inputBuffer.size()) >= m_jsonOffset + m_jsonLength)
		parseJsonBody();
}

void JsonScgiPeer::parseJsonBody()
{
	if (static_cast<unsigned int>(m_inputBuffer.size()) < m_jsonOffset + m_jsonLength)
		return;

	bool ok;
	const QVariantMap result = Json::parse(m_inputBuffer.data() + m_jsonOffset, ok).toMap();
	if (!ok) {
		notFound();
		return;
	}
	m_server->newWebRequest(m_url, m_sessionCookie, result, *this);
}

void JsonScgiPeer::disconnected()
{
	deleteLater();
}

void JsonScgiPeer::readyRead()
{
	if (m_inputBuffer.size() + m_connection->bytesAvailable() > 100 * 1024 * 1024 /* 100 megs */) {
		die();
		return;
	}
	m_inputBuffer.append(m_connection->readAll());
	if (m_connection->bytesAvailable())
		QTimer::singleShot(0, this, SLOT(readyRead()));

	if (m_jsonOffset && m_jsonLength && static_cast<unsigned int>(m_inputBuffer.size()) >= m_jsonLength + m_jsonOffset)
		parseJsonBody();
	else
		parseScgiHeader();
}

void JsonScgiPeer::readyWrite()
{
	int writen = m_connection->write(m_outputBuffer);
	if (writen == -1) {
		die();
		return;
	}
	m_outputBuffer.remove(0, writen);
	if (m_outputBuffer.size())
		QTimer::singleShot(0, this, SLOT(readyWrite()));
	else
		m_connection->disconnectFromHost();
}