| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2016 The Qt Company Ltd. |
| 4 |
** Contact: https://www.qt.io/licensing/ |
| 5 |
** |
| 6 |
** This file is part of the test suite of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 |
** included in the packaging of this file. Please review the following |
| 22 |
** information to ensure the GNU General Public License requirements will |
| 23 |
** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 |
** |
| 25 |
** $QT_END_LICENSE$ |
| 26 |
** |
| 27 |
****************************************************************************/ |
| 28 |
|
| 29 |
#include <testhttpserver.h> |
| 30 |
|
| 31 |
#include <QCoreApplication> |
| 32 |
#include <QCommandLineParser> |
| 33 |
#include <QCommandLineOption> |
| 34 |
#include <QDebug> |
| 35 |
#include <QDir> |
| 36 |
#include <QFileInfo> |
| 37 |
|
| 38 |
#include <iostream> |
| 39 |
#include <iterator> |
| 40 |
#include <algorithm> |
| 41 |
|
| 42 |
inline std::wostream &operator<<(std::wostream &str, const QString &s) |
| 43 |
{ |
| 44 |
#ifdef Q_OS_WIN |
| 45 |
str << reinterpret_cast<const wchar_t *>(s.utf16()); |
| 46 |
#else |
| 47 |
str << s.toStdWString(); |
| 48 |
#endif |
| 49 |
return str; |
| 50 |
} |
| 51 |
|
| 52 |
enum { defaultPort = 14457 }; |
| 53 |
|
| 54 |
int main(int argc, char *argv[]) |
| 55 |
{ |
| 56 |
QCoreApplication a(argc, argv); |
| 57 |
QCoreApplication::setApplicationName("HttpServer"); |
| 58 |
QCoreApplication::setApplicationVersion(QT_VERSION_STR); |
| 59 |
|
| 60 |
QCommandLineParser commandLineParser; |
| 61 |
commandLineParser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions); |
| 62 |
commandLineParser.setApplicationDescription(QStringLiteral("HTTP Test server")); |
| 63 |
commandLineParser.addHelpOption(); |
| 64 |
commandLineParser.addVersionOption(); |
| 65 |
|
| 66 |
QCommandLineOption portOption(QStringLiteral("p"), |
| 67 |
QStringLiteral("Port (default: ") + QString::number(defaultPort) + QLatin1Char(')'), |
| 68 |
QStringLiteral("port")); |
| 69 |
|
| 70 |
commandLineParser.addOption(portOption); |
| 71 |
|
| 72 |
commandLineParser.addPositionalArgument(QStringLiteral("[directory]"), |
| 73 |
QStringLiteral("Directory to serve.")); |
| 74 |
|
| 75 |
commandLineParser.process(a); |
| 76 |
|
| 77 |
const QStringList args = commandLineParser.positionalArguments(); |
| 78 |
if (args.size() != 1) |
| 79 |
commandLineParser.showHelp(1); |
| 80 |
|
| 81 |
const QString directory = QDir::cleanPath(args.front()); |
| 82 |
if (!QFileInfo(directory).isDir()) { |
| 83 |
std::wcerr << '"' << QDir::toNativeSeparators(directory) << "\" is not a directory.\n"; |
| 84 |
return -1; |
| 85 |
} |
| 86 |
|
| 87 |
unsigned short port = defaultPort; |
| 88 |
if (commandLineParser.isSet(portOption)) { |
| 89 |
const QString portV = commandLineParser.value(portOption); |
| 90 |
bool ok; |
| 91 |
port = portV.toUShort(&ok); |
| 92 |
if (!ok) { |
| 93 |
std::wcerr << portV << " is not a valid port number.\n"; |
| 94 |
return -1; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
std::wcout << "Serving \"" << QDir::toNativeSeparators(directory) |
| 99 |
<< "\":\n\n" << QDir(directory).entryList(QDir::Files).join(QLatin1Char('\n')) |
| 100 |
<< "\n\non http://localhost:" << port << '\n'; |
| 101 |
|
| 102 |
TestHTTPServer server; |
| 103 |
if (!server.listen(port)) { |
| 104 |
std::wcout << "Couldn't listen on port " << port << server.errorString().toLocal8Bit(); |
| 105 |
exit(-1); |
| 106 |
} |
| 107 |
server.serveDirectory(directory); |
| 108 |
|
| 109 |
return a.exec(); |
| 110 |
} |