summaryrefslogtreecommitdiffhomepage
path: root/src/webbox.cpp
blob: ab6552985614a33e2765c789568b57599b6183b1 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <fcgiapp.h>

#include <QString>
#include <QStringList>
#include <QHash>
#include <QDir>
#include <QFileInfo>
#include <QXmlStreamReader>
#include <QDateTime>

#define PROGRAMVERSION "1.0"
#define BUFSIZE 1000000

// supported httpStatusCode:
// 400 Bad Request
// 403 Forbidden
// 404 Not Found
// 500 Internal Server Error
// message: additional message
QString httpError(int httpStatusCode, QString message) {
	QString description;

	switch(httpStatusCode) {
		case 400:
			description = "Bad Request";
			break;
		case 403:
			description = "Forbidden";
			break;
		case 404:
			description = "Not Found";
			break;
		case 500:
			description = "Internal Server Error";
			break;
		default:
			message = QString("Bad error code: %1, message: %2").arg(httpStatusCode).arg(message);
			httpStatusCode = 500;
			description = "Internal Server Error";
	}
	return QString("Status: %1 %2\r\nContent-Type: text/html\r\n\r\n<html><body><h1>%1 %2</h1><p>%3</p></body></html>\r\n").arg(httpStatusCode).arg(description).arg(message);
}

int main(int argc, char* argv[]) {
	int result = FCGX_Init();
	if (result != 0) {
		return 1; // error on init
	}

	FCGX_Request request;

	if (FCGX_InitRequest(&request, 0, 0) != 0) {
		return 1; // error on init
	}

	int count = 0;

	while (FCGX_Accept_r(&request) == 0) {

		count++;

		QString queryString(FCGX_GetParam("QUERY_STRING", request.envp));

		// URL parameters
		QStringList paramList = queryString.split("&");
		QHash<QString, QString> paramHash;

		foreach(QString keyValue, paramList) {
			QStringList keyValueList = keyValue.split("=");
			if (keyValueList.size() == 2) {
				QString key = keyValueList[0];
				QString value = keyValueList[1];
				paramHash[key] = value;
			}
		}

		QString command = paramHash["command"];

		// process environment
		QString webboxPath(getenv("WEBBOX_PATH"));

		// FastCGI request environment
		QString pathInfo(FCGX_GetParam("PATH_INFO", request.envp));
		if (pathInfo == "") {
			pathInfo = "/";
		}
		if (pathInfo.contains("..")) {
			FCGX_PutS(httpError(403, QString("Bad path: %1").arg(pathInfo)).toUtf8().data(), request.out);
			continue;
		}

		QString path = webboxPath + pathInfo;

		if (command == "diag") {
			FCGX_PutS("Content-Type: text/html\r\n\r\n", request.out);

			FCGX_PutS("<html><head><title>Params</title></head><body>\r\n", request.out);

			FCGX_PutS(QString("Request no. %1<br/><br/>\r\n").arg(count).toUtf8().data(), request.out);

			char** tmp = request.envp;

			while (*tmp) {
				FCGX_PutS(QString("%1<br/>\r\n").arg(*tmp).toUtf8().data(), request.out);
				tmp++;
			}
				
			FCGX_PutS(QString("<br/>WEBBOX_PATH=%1<br/>\r\n").arg(getenv("WEBBOX_PATH")).toUtf8().data(), request.out);
			
			FCGX_PutS("</body></html>\r\n", request.out);
		} else
		if (command == "list") {
			FCGX_PutS("Content-Type: text/xml\r\n\r\n", request.out);

			FCGX_PutS("<list>\r\n", request.out);

			QDir dir(path);
			QFileInfoList dirEntryList = dir.entryInfoList(QDir::NoDot | QDir::AllEntries, QDir::DirsFirst | QDir::Name);
			foreach(QFileInfo i, dirEntryList) {
				if (pathInfo != "/" || i.fileName() != "..") { // skip on ".." in "/"
					FCGX_PutS(QString("<listentry type=\"%1\">%2</listentry>\r\n").arg(i.isDir() ? "dir" : "file").arg(i.fileName()).toUtf8().data(), request.out);
				}
			}
			FCGX_PutS("</list>\r\n", request.out);
		} else if (command == "title") {
			FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
			FCGX_PutS(getenv("WEBBOX_NAME"), request.out);
		} else if (command == "version") {
			FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
			FCGX_PutS(QString("webbox %1<br/>(C) 2017 <a href=\"https://www.reichwein.it/\">Reichwein.IT</a>\r\n").arg(PROGRAMVERSION).toUtf8().data(), request.out);
		} else if (command == "newdir") { // POST!
			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp));
			bool ok;
			int contentLength = contentLengthString.toInt(&ok);

			FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
			if (!ok) {
				FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out);
			} else {
				QByteArray content(contentLength, 0);
				
				int result = FCGX_GetStr(content.data(), content.size(), request.in);
				if (result != content.size()) {
					FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out);
				} else {
					QXmlStreamReader xml(content);

					while (!xml.atEnd()) {
						while (xml.readNextStartElement()) {
							if (xml.name() == "dirname") {
								QString dirname = xml.readElementText();
								QDir dir(path);
								if (dir.mkdir(dirname)) {
									FCGX_PutS("Successfully created directory", request.out);
								} else {
									FCGX_PutS("Error creating directory", request.out);
								}
							}
						}
					}
				}
			}
		} else if (command == "info") { // POST!
			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp));
			bool ok;
			int contentLength = contentLengthString.toInt(&ok);

			FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
			if (!ok) {
				FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out);
			} else {
				QByteArray content(contentLength, 0);
				
				int result = FCGX_GetStr(content.data(), content.size(), request.in);
				if (result != content.size()) {
					FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out);
				} else {
					QXmlStreamReader xml(content);

					while (!xml.atEnd()) {
						while (xml.readNextStartElement()) {
							if (xml.name() == "files") {
								while (xml.readNextStartElement()) {
									if (xml.name() == "file") {
										QString filename = xml.readElementText();
										QFileInfo fileInfo(path + "/" + filename);
										qint64 size = fileInfo.size();
										QString date = fileInfo.lastModified().toString();
										QString type = fileInfo.isDir() ? "directory" : "file";
										FCGX_PutS(QString("%1, %2 bytes, %3 (%4)<br>")
												.arg(filename)
												.arg(size)
												.arg(date)
												.arg(type).toUtf8().data(), request.out);
									}
								}
							}
						}
					}
				}
			}
		} else if (command == "download-zip") { // POST!
			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp));
			bool ok;
			int contentLength = contentLengthString.toInt(&ok);

			if (!ok) {
				FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
				FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out);
			} else {
				QByteArray content(contentLength, 0);
				
				int result = FCGX_GetStr(content.data(), content.size(), request.in);
				if (result != content.size()) {
					FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
					FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out);
				} else {
					QXmlStreamReader xml(content);

					QByteArray zipData;

					while (!xml.atEnd()) {
						while (xml.readNextStartElement()) {
							if (xml.name() == "files") {
								while (xml.readNextStartElement()) {
									if (xml.name() == "file") {
										QString filename = xml.readElementText();

										zipData.append(filename.toUtf8()); // TBD
									}
								}
							}
						}
					}

					FCGX_PutS(QString("Content-Disposition: attachment; filename=\"%1\"\r\n").arg("webbox-download.zip").toUtf8().data(), request.out);
					FCGX_PutS("Content-Type: application/octet-stream\r\n\r\n", request.out);
					
					FCGX_PutStr(zipData.data(), zipData.size(), request.out);
				}
			}
		} else if (command == "upload") { // POST!
			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp));
			bool ok;
			int contentLength = contentLengthString.toInt(&ok);
			FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out);
			if (!ok) {
				FCGX_PutS(QString("Bad content length").toUtf8().data(), request.out);
			} else {
				QByteArray content(contentLength, 0);
				
				int result = FCGX_GetStr(content.data(), content.size(), request.in);
				if (result != content.size()) {
					FCGX_PutS(QString("Read error (%1/%2)").arg(result).arg(content.size()).toUtf8().data(), request.out);
				} else {
					QString contentType(FCGX_GetParam("CONTENT_TYPE", request.envp));

					if (!contentType.contains("boundary=--")) {
						FCGX_PutS(QString("No boundary defined").toUtf8().data(), request.out);
					} else {
						QByteArray boundary = contentType.split("boundary=--")[1].toUtf8();
						int boundaryCount = content.count(boundary);
						if (boundaryCount != 2) {
							FCGX_PutS(QString("Bad boundary number found: %1").arg(boundaryCount).toUtf8().data(), request.out);
						} else {
							int start = content.indexOf(boundary) + boundary.size();
							int end = content.indexOf(boundary, start);

							content = content.mid(start, end - start);

							// Read filename
							start = content.indexOf("filename=\"");
							if (start == -1) {
								FCGX_PutS(QString("Error reading filename / start").toUtf8().data(), request.out);
							} else {
								start += QString("filename=\"").size();

								end = content.indexOf("\"", start);
								if (end == -1) {
									FCGX_PutS(QString("Error reading filename / end").toUtf8().data(), request.out);
								} else {
									QString filename = content.mid(start, end - start);

									if (filename.size() < 1) {
										FCGX_PutS(QString("Bad filename").toUtf8().data(), request.out);
									} else {
										// Remove header
										start = content.indexOf("\r\n\r\n");
										if (start == -1) {
											FCGX_PutS(QString("Error removing upload header").toUtf8().data(), request.out);
										} else {

											content = content.mid(start + QString("\r\n\r\n").size());

											QFile file(path + "/" + filename);
											if (!file.open(QIODevice::WriteOnly)) {
												FCGX_PutS(QString("Error opening file").toUtf8().data(), request.out);
											} else {
												qint64 written = file.write(content);
												if (written != content.size()) {
													FCGX_PutS(QString("Error writing file").toUtf8().data(), request.out);
												} else {
													FCGX_PutS("OK", request.out);
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		} else { // default: download
			QFile file(path);
			if (file.open(QIODevice::ReadOnly)) {
				QFileInfo fileInfo(path);
				FCGX_PutS(QString("Content-Disposition: attachment; filename=\"%1\"\r\n").arg(fileInfo.fileName()).toUtf8().data(), request.out);
				FCGX_PutS("Content-Type: application/octet-stream\r\n\r\n", request.out);
				
				while (!file.atEnd()) {
					QByteArray ba = file.read(BUFSIZE);
					FCGX_PutStr(ba.data(), ba.size(), request.out);
				}
			} else {
				FCGX_PutS(httpError(500, QString("Bad file: %1").arg(pathInfo)).toUtf8().data(), request.out);
			}
		}
	}

	return 0;
}