summaryrefslogtreecommitdiffhomepage
path: root/plugins/webbox/webbox.cpp
blob: 61668951992c465047ea25ad41b44a87f9bdf7d1 (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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#include "webbox.h"

#include <iostream>

using namespace std::string_literals;

std::string webbox_plugin::name()
{
 return "webbox";
}

webbox_plugin::webbox_plugin()
{
 //std::cout << "Plugin constructor" << std::endl;
}

webbox_plugin::~webbox_plugin()
{
 //std::cout << "Plugin destructor" << std::endl;
}

std::string webbox_plugin::generate_page(
  std::function<std::string(const std::string& key)>& GetServerParam,
  std::function<std::string(const std::string& key)>& GetRequestParam, // request including body (POST...)
  std::function<void(const std::string& key, const std::string& value)>& SetResponseHeader // to be added to result string
)
{
 return "Webbox";
}

#if 0
#include <fcgiapp.h>

#include <QString>
#include <QStringList>
#include <QHash>
#include <QDir>
#include <QFileInfo>
#include <QXmlStreamReader>
#include <QXmlStreamWriter>
#include <QDateTime>
#include <QProcess>
#include <QTemporaryFile>
#include <QUrlQuery>
#include <QPair>

#define BUFSIZE 1000000

// XML special characters:
// < : &lt;
// > : &gt;
// & : &amp;
// " : &quot;
// ' : &apos;
//
// here:replace &
QString escapeXML(QString s) {
	s.replace("&", "&amp;");
	return s;
}

// revert escapeXML();
QString unescapeXML(QString s) {
	s.replace("&amp;", "&");
	return s;
}

// 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);
}

struct CommandParameters {
	FCGX_Request request; // the request

	QUrlQuery urlQuery; // derived from request
	QHash<QString, QString> paramHash; // derived from urlQuery

	int count; // request count for this instance

	// Webbox parameters, constant and set globally in Web server config
	QString webboxPath;
	QString webboxName;
	bool webboxReadOnly;
};

class Command {
	public:
		// call interface
		void execute(CommandParameters& p) {
			// check if this webbox is writable and enforce this
			if (p.webboxReadOnly && m_isWriteCommand) {
				printHttpError(400, QString("Webbox is Read-Only"), p);
				return;
			}

			// check for correct method GET/POST
			QString requestMethod(FCGX_GetParam("REQUEST_METHOD", p.request.envp));
			if (requestMethod != m_requestMethod) {
				printHttpError(403, QString("Bad request method"), p);
				return;
			}

			// Set parameters from FastCGI request environment
			m_pathInfo = FCGX_GetParam("PATH_INFO", p.request.envp);
			if (m_pathInfo == "") {
				m_pathInfo = "/";
			}
			if (m_pathInfo.contains("..")) {
				printHttpError(403, QString("Bad path: %1"), p);
				return;
			}

			m_path = p.webboxPath + m_pathInfo;

			this->start(p);
		}

		QString getCommandName() {
			return m_commandName;
		}

	protected:
		// helper function for writing http error
		void printHttpError(int httpStatusCode, QString message, CommandParameters& p) {
			FCGX_PutS(httpError(httpStatusCode, message).toUtf8().data(), p.request.out);
		}
		
		// implemented in implementation classes
		virtual void start(CommandParameters& p) = 0;

		// Implementation class constants
		QString m_commandName;
		QString m_requestMethod;
		bool m_isWriteCommand; // if true, command must be prevented if p.webboxReadOnly

		// calculated during start of execute()
		QString m_pathInfo; // path inside webbox, derived from request
		QString m_path; // complete path
};

class GetCommand: public Command {
	public:
		GetCommand() {
			m_requestMethod = "GET";
		}
};

class PostCommand: public Command {
	public:
		PostCommand() {
			m_requestMethod = "POST";
		}

	protected:
		// prepare POST handler implementation: read m_contentLength and m_content
		// needs to be called at beginning of post implementations start()
		// returns true on success
		bool readContent(CommandParameters& p) {
			QString contentLengthString(FCGX_GetParam("CONTENT_LENGTH", p.request.envp));
			bool ok;
			m_contentLength = contentLengthString.toInt(&ok);

			if (!ok) {
				printHttpError(400, QString("Bad content length"), p);
				return false;
			} else {
				m_content.resize(m_contentLength);
				
				int result = FCGX_GetStr(m_content.data(), m_content.size(), p.request.in);
				if (result != m_content.size()) {
					printHttpError(400, QString("Read error (%1/%2)").arg(result).arg(m_content.size()), p);
					return false;
				}
			}
			return true;
		}

		int m_contentLength;
		QByteArray m_content;
};

class DiagCommand: public GetCommand {
	public:
		DiagCommand() {
			m_commandName = "diag";
			m_isWriteCommand = false;
		}

	protected:
		virtual void start(CommandParameters& p) {
			QString serverName(FCGX_GetParam("SERVER_NAME", p.request.envp));
			// provide diag only on "localhost"
			if (serverName != "localhost") {
				printHttpError(403, QString("Command not available"), p);
				return;
			}

			FCGX_PutS("Content-Type: text/html\r\n\r\n", p.request.out);

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

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

			char** tmp = p.request.envp;

			while (*tmp) {
				FCGX_PutS(QString("%1<br/>\r\n").arg(*tmp).toUtf8().data(), p.request.out);
				tmp++;
			}
				
			FCGX_PutS(QString("<br/>WEBBOX_PATH=%1<br/>\r\n").arg(p.webboxPath).toUtf8().data(), p.request.out);

			FCGX_PutS(QString("<br/>URL Query=%1<br/>\r\n").arg(p.urlQuery.toString()).toUtf8().data(), p.request.out);

			
			FCGX_PutS("</body></html>\r\n", p.request.out);
		}
};

class ListCommand: public GetCommand {
	public:
		ListCommand() {
			m_commandName = "list";
			m_isWriteCommand = false;
		}

	protected:
		virtual void start(CommandParameters& p) {
			FCGX_PutS("Content-Type: text/xml\r\n\r\n", p.request.out);

			QDir dir(m_path);
			QFileInfoList dirEntryList = dir.entryInfoList(QDir::NoDot | QDir::AllEntries, QDir::DirsFirst | QDir::Name | QDir::IgnoreCase);

			QByteArray xmlData;
			QXmlStreamWriter xmlWriter(&xmlData);
			xmlWriter.writeStartDocument();
			xmlWriter.writeStartElement("list");
			foreach(QFileInfo i, dirEntryList) {
				if (m_pathInfo != "/" || i.fileName() != "..") { // skip on ".." in "/"
					xmlWriter.writeStartElement("listentry");
					xmlWriter.writeAttribute("type", i.isDir() ? "dir" : "file");
					xmlWriter.writeCharacters(i.fileName());
					xmlWriter.writeEndElement();
				}
			}
			xmlWriter.writeEndElement(); // list
			xmlWriter.writeEndDocument();
			FCGX_PutS(xmlData.data(), p.request.out);
		}
};

// Retrieve from Server:
//   Title
//   ReadOnly flag
class ServerInfoCommand: public GetCommand {
	public:
		ServerInfoCommand() {
			m_commandName = "server-info";
			m_isWriteCommand = false;
		}

	protected:
		virtual void start(CommandParameters& p) {
			FCGX_PutS("Content-Type: text/xml\r\n\r\n", p.request.out);

			QByteArray xmlData;
			QXmlStreamWriter xmlWriter(&xmlData);
			xmlWriter.writeStartDocument();
			xmlWriter.writeStartElement("serverinfo");

			xmlWriter.writeTextElement("title", p.webboxName);
			
			xmlWriter.writeTextElement("readonly", p.webboxReadOnly ? "1" : "0");

			xmlWriter.writeEndElement(); // serverinfo
			xmlWriter.writeEndDocument();
			FCGX_PutS(xmlData.data(), p.request.out);
		}
};

class VersionCommand: public GetCommand {
	public:
		VersionCommand() {
			m_commandName = "version";
			m_isWriteCommand = false;
		}

	protected:
		virtual void start(CommandParameters& p) {
			FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out);
			FCGX_PutS(QString("webbox %1<br/>(C) 2018 <a href=\"https://www.reichwein.it/\">Reichwein.IT</a>\r\n").arg(PROGRAMVERSION).toUtf8().data(), p.request.out);
		}
};

class NewDirCommand: public PostCommand {
	public:
		NewDirCommand() {
			m_commandName = "newdir";
			m_isWriteCommand = true;
		}

	protected:
		virtual void start(CommandParameters& p) {
			if (!readContent(p))
				return;

			FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out);
			QXmlStreamReader xml(m_content);

			while (!xml.atEnd()) {
				while (xml.readNextStartElement()) {
					if (xml.name() == "dirname") {
						QString dirname = xml.readElementText();
						QDir dir(m_path);
						if (dir.mkdir(dirname)) {
							FCGX_PutS("Successfully created directory", p.request.out);
						} else {
							FCGX_PutS("Error creating directory", p.request.out);
						}
					}
				}
			}
		}
};

class InfoCommand: public PostCommand {
	public:
		InfoCommand() {
			m_commandName = "info";
			m_isWriteCommand = false;
		}

	protected:
		virtual void start(CommandParameters& p) {
			if (!readContent(p))
				return;

			FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out);
			QXmlStreamReader xml(m_content);

			while (!xml.atEnd()) {
				while (xml.readNextStartElement()) {
					if (xml.name() == "files") {
						while (xml.readNextStartElement()) {
							if (xml.name() == "file") {
								QString filename = xml.readElementText();
								QFileInfo fileInfo(m_path + "/" + filename);
								qint64 size = fileInfo.size();
								QString date = fileInfo.lastModified().toString();
								if (fileInfo.isDir()) {
									FCGX_PutS(QString("%1, %2 (folder)<br>")
											.arg(filename)
											.arg(date).toUtf8().data(), p.request.out);
								} else {
									FCGX_PutS(QString("%1, %2 bytes, %3 (file)<br>")
											.arg(filename)
											.arg(size)
											.arg(date).toUtf8().data(), p.request.out);
								}
							}
						}
					}
				}
			}
		}
};

class DownloadZipCommand: public PostCommand {
	public:
		DownloadZipCommand() {
			m_commandName = "download-zip";
		}

	protected:
		virtual void start(CommandParameters& p) {
			if (!readContent(p))
				return;

			QXmlStreamReader xml(m_content);

			QByteArray zipData;
			QStringList argumentList;
			QTemporaryFile tempfile(QDir::tempPath() + "/webboxXXXXXX.zip");
			tempfile.open();
			QFileInfo fileInfo(tempfile);
			QString tempfilePath = fileInfo.absolutePath();
			QString tempfileName = fileInfo.fileName();
			tempfile.close();
			tempfile.remove();

			argumentList << "-r"; // recursive packing
			argumentList << tempfilePath + "/" + tempfileName; // zip filename

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

								argumentList.append(filename); // add parts
							}
						}
					}
				}
			}

			QProcess process;
			process.setWorkingDirectory(m_path);
			process.setProgram("/usr/bin/zip");
			process.setArguments(argumentList);
			process.start();
			process.waitForFinished();

			QString debugText = process.readAll();
			process.setReadChannel(QProcess::StandardError);
			debugText += process.readAll();

			if (process.state() != QProcess::NotRunning ||
			    process.exitCode() != 0 ||
			    process.exitStatus() != QProcess::NormalExit)
			{
				printHttpError(500, QString("Error running process: %1 %2 %3 %4 %5 %6 %7").
					  arg(process.state()).
					  arg(process.exitCode()).
					  arg(process.exitStatus()).
					  arg(tempfilePath).
					  arg(tempfileName).
					  arg(argumentList[0]).
					  arg(debugText), p);
			} else {

				QFile tempfile(tempfilePath + "/" + tempfileName);
				if (!tempfile.open(QIODevice::ReadOnly)) {
					printHttpError(500, QString("Error reading file"), p);
				} else {
					zipData = tempfile.readAll();

					FCGX_PutS(QString("Content-Disposition: attachment; filename=\"%1\"\r\n").arg("webbox-download.zip").toUtf8().data(), p.request.out);
					FCGX_PutS("Content-Type: application/octet-stream\r\n\r\n", p.request.out);
					
					FCGX_PutStr(zipData.data(), zipData.size(), p.request.out);
				}

				tempfile.close();
				tempfile.remove();
			}
		}
};

class DeleteCommand: public PostCommand {
	public:
		DeleteCommand() {
			m_commandName = "delete";
			m_isWriteCommand = true;
		}

	protected:
		virtual void start(CommandParameters& p) {
			if (!readContent(p))
				return;

			QXmlStreamReader xml(m_content);
			
			QString response = "";

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

								QFileInfo fileInfo(m_path + "/" + filename);
								if (fileInfo.isDir()) {
									QDir dir(m_path);
									if (!dir.rmdir(filename)) {
										response += QString("Error on removing directory %1<br/>").arg(filename);
									}
								} else if (fileInfo.isFile()) {
									QFile file(m_path + "/" + filename);
									if (!file.remove()) {
										response += QString("Error on removing file %1<br/>").arg(filename);
									}
								} else {
									response += QString("Error: %1 is neither file nor directory.<br/>").arg(filename);
								}
							}
						}
					}
				}
			}

			if (response == "") {
				response = "OK";
			}
			
			FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out);
			FCGX_PutS(response.toUtf8().data(), p.request.out);
		}
};

class MoveCommand: public PostCommand {
	public:
		MoveCommand() {
			m_commandName = "move";
			m_isWriteCommand = true;
		}

	protected:
		virtual void start(CommandParameters& p) {
			if (!readContent(p))
				return;

			QXmlStreamReader xml(m_content);
			
			QString response = "";
			QString targetDir;

			while (!xml.atEnd()) {
				while (xml.readNextStartElement()) {
					if (xml.name() == "request") {
						while (xml.readNextStartElement()) {
							if (xml.name() == "target") {
								targetDir = xml.readElementText();
							} else if (xml.name() == "file") {
								QString filename = xml.readElementText();

								QFileInfo fileInfo(m_path + "/" + filename);
								if (fileInfo.isDir()) {
									QDir dir(m_path);
									if (!dir.rename(filename, targetDir + "/" + filename)) {
										response += QString("Error moving directory %1<br/>").arg(filename);
									}
								} else if (fileInfo.isFile()) {
									QFile file(m_path + "/" + filename);
									if (!file.rename(m_path + "/" + targetDir + "/" + filename)) {
										response += QString("Error on moving file %1<br/>").arg(filename);
									}
								} else {
									response += QString("Error: %1 is neither file nor directory.<br/>").arg(filename);
								}
							}
						}
					}
				}
			}

			if (response == "") {
				response = "OK";
			}
			
			FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out);
			FCGX_PutS(response.toUtf8().data(), p.request.out);
		}
};

class RenameCommand: public PostCommand {
	public:
		RenameCommand() {
			m_commandName = "rename";
			m_isWriteCommand = true;
		}

	protected:
		virtual void start(CommandParameters& p) {
			if (!readContent(p))
				return;

			QXmlStreamReader xml(m_content);
			
			QString oldname;
			QString newname;

			while (!xml.atEnd()) {
				while (xml.readNextStartElement()) {
					if (xml.name() == "request") {
						while (xml.readNextStartElement()) {
							if (xml.name() == "oldname") {
								oldname = xml.readElementText();
							} else
							if (xml.name() == "newname") {
								newname = xml.readElementText();
							}
						}
					}
				}
			}
			
			QDir dir(m_path);
			QString response;
			if (!dir.rename(oldname, newname)) {
				response = QString("Error renaming %1 to %2<br/>").arg(oldname).arg(newname);
			} else {
				response = "OK";
			}
			
			FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out);
			FCGX_PutS(response.toUtf8().data(), p.request.out);
		}
};

class UploadCommand: public PostCommand {
	public:
		UploadCommand() {
			m_commandName = "upload";
			m_isWriteCommand = true;
		}

	protected:
		virtual void start(CommandParameters& p) {
			if (!readContent(p))
				return;

			FCGX_PutS("Content-Type: text/plain\r\n\r\n", p.request.out);
			QString contentType(FCGX_GetParam("CONTENT_TYPE", p.request.envp));

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

						if (end == -1) { // no further boundary found: all handled.
							break;
						}

						QByteArray filecontent = m_content.mid(start, end - start);
						int nextBoundaryIndex = end;

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

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

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

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

										QFile file(m_path + "/" + filename);
										if (!file.open(QIODevice::WriteOnly)) {
											FCGX_PutS(QString("Error opening file").toUtf8().data(), p.request.out);
										} else {
											qint64 written = file.write(filecontent);
											if (written != filecontent.size()) {
												FCGX_PutS(QString("Error writing file").toUtf8().data(), p.request.out);
											}
										}
									}
								}
							}
						}
						m_content.remove(0, nextBoundaryIndex);
					}
				}
			}
		}
};

class DownloadCommand: public GetCommand {
	public:
		DownloadCommand() {
			m_commandName = ""; // default command w/o explict "command=" query argument
			m_isWriteCommand = false;
		}

	protected:
		virtual void start(CommandParameters& p) {
			QFile file(m_path);
			if (file.open(QIODevice::ReadOnly)) {
				QFileInfo fileInfo(m_path);
				FCGX_PutS(QString("Content-Disposition: attachment; filename=\"%1\"\r\n").arg(fileInfo.fileName()).toUtf8().data(), p.request.out);
				FCGX_PutS("Content-Type: application/octet-stream\r\n\r\n", p.request.out);
				
				while (!file.atEnd()) {
					QByteArray ba = file.read(BUFSIZE);
					FCGX_PutStr(ba.data(), ba.size(), p.request.out);
				}
			} else {
				FCGX_PutS(httpError(500, QString("Bad file: %1").arg(m_pathInfo)).toUtf8().data(), p.request.out);
			}
		}
};

// Hash of commands for fast access
class Commands: public QHash<QString, Command*> {
	public:
		void registerCommand(Command& command) {
			(*this)[command.getCommandName()] = &command;
		}
};

void initlocale() {
	if (setenv("LC_ALL", "UTF-8", 1)) {
		exit(1);
	}
}

int main(int argc, char* argv[]) {
	initlocale();

	int result = FCGX_Init();
	if (result != 0) {
		return 1; // error on init
	}

	CommandParameters commandParameters;

	FCGX_Request& request = commandParameters.request;

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

	commandParameters.count = 0;

	// values constant to this instance:
	commandParameters.webboxPath = getenv("WEBBOX_PATH");
	commandParameters.webboxName = getenv("WEBBOX_NAME");
	char* WEBBOX_READONLY = getenv("WEBBOX_READONLY");
	commandParameters.webboxReadOnly = ((WEBBOX_READONLY != NULL) && !strcmp(WEBBOX_READONLY, "On"));

	Commands commands;

	DiagCommand diagCommand;
	commands.registerCommand(diagCommand);

	ListCommand listCommand;
	commands.registerCommand(listCommand);

	ServerInfoCommand serverInfoCommand;
	commands.registerCommand(serverInfoCommand);

	VersionCommand versionCommand;
	commands.registerCommand(versionCommand);

	NewDirCommand newDirCommand;
	commands.registerCommand(newDirCommand);

	InfoCommand infoCommand;
	commands.registerCommand(infoCommand);

	DownloadZipCommand downloadZipCommand;
	commands.registerCommand(downloadZipCommand);
	
	DeleteCommand deleteCommand;
	commands.registerCommand(deleteCommand);
	
	MoveCommand moveCommand;
	commands.registerCommand(moveCommand);
	
	RenameCommand renameCommand;
	commands.registerCommand(renameCommand);
	
	UploadCommand uploadCommand;
	commands.registerCommand(uploadCommand);
	
	DownloadCommand downloadCommand;
	commands.registerCommand(downloadCommand);
	
	while (FCGX_Accept_r(&request) == 0) {

		commandParameters.count++;

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

		// URL parameters
		commandParameters.urlQuery.setQuery(queryString);

		QList<QPair<QString, QString> > items = commandParameters.urlQuery.queryItems();
		commandParameters.paramHash.clear();

		for (int i = 0; i < items.size(); i++) {
			commandParameters.paramHash[items[i].first] = items[i].second;
		}

		QString commandName = commandParameters.paramHash["command"];
		if (commands.contains(commandName)) {
			Command* command = commands[commandName];

			command->execute(commandParameters);
		} else {
			FCGX_PutS(httpError(400, QString("Bad command: %1").arg(commandName)).toUtf8().data(), request.out);
		}
	}

	return 0;
}
#endif