HttpServer.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /***************************************************************************
  2. * Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com> *
  3. * *
  4. * This file is part of Glowworm. *
  5. * *
  6. * Glowworm is free software: you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation, version 3 of the License. *
  9. * *
  10. * Glowworm is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with Glowworm. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #include "HttpServer.h"
  19. #include "HttpRequest.h"
  20. #include "HttpResponse.h"
  21. #include "FileServlet.h"
  22. #include "LogModel.h"
  23. #include <QCoreApplication>
  24. #include <QFile>
  25. #include <QStringList>
  26. #include <QTextCodec>
  27. #include <QTcpServer>
  28. #include <QTcpSocket>
  29. #include <QUrl>
  30. #include <QHostInfo>
  31. HttpServer::HttpServer(QObject* parent)
  32. : QObject(parent)
  33. , m_tcpServer(new QTcpServer(this))
  34. , m_port(8080)
  35. , m_fileServlet(new FileServlet(this))
  36. , m_logModel(new LogModel(this))
  37. {
  38. Q_CHECK_PTR(m_tcpServer);
  39. connect(m_tcpServer, SIGNAL(newConnection()), this, SLOT(handleIncomingConnection()));
  40. }
  41. HttpServer::~HttpServer()
  42. {
  43. delete m_tcpServer;
  44. }
  45. void HttpServer::handleIncomingConnection()
  46. {
  47. while (m_tcpServer->hasPendingConnections()) {
  48. QTcpSocket* tcpSocket = m_tcpServer->nextPendingConnection();
  49. Q_CHECK_PTR(tcpSocket);
  50. if (tcpSocket->isValid())
  51. {
  52. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(handleRead()));
  53. connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(handleDisconnect()));
  54. } else {
  55. delete(tcpSocket);
  56. }
  57. }
  58. }
  59. void HttpServer::handleRead()
  60. {
  61. QTcpSocket* tcpSocket = (QTcpSocket*) QObject::sender();
  62. Q_CHECK_PTR(tcpSocket);
  63. if (tcpSocket->canReadLine())
  64. {
  65. QStringList tokens = QString(tcpSocket->readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
  66. QString localhost(QHostInfo::localHostName());
  67. QUrl localUrl(QString("http://%1/").arg(localhost));
  68. QUrl queryUrl(localUrl.resolved(QUrl(tokens.at(1))));
  69. HttpRequest request;
  70. request.setMethod(tokens.at(0));
  71. QMap<QString, QString> map;
  72. QList<QPair<QString, QString> > qitms(queryUrl.queryItems());
  73. for (int i = 0; i < qitms.count(); ++i) {
  74. QPair<QString,QString> pair(qitms.at(i));
  75. map.insert(pair.first, pair.second);
  76. }
  77. request.setParameters(map);
  78. request.setUrl(queryUrl);
  79. HttpResponse response;
  80. response.setOutputDevice(tcpSocket);
  81. AbstractServlet* handler(m_fileServlet);
  82. logModel()->log(request.url().path());
  83. QString path(request.url().path());
  84. foreach (const QString & key, m_servlets.keys()) {
  85. if (path.startsWith(key)) {
  86. handler = m_servlets.value(key);
  87. break;
  88. }
  89. }
  90. handler->request(&request, &response);
  91. if (!response.headerSent()) {
  92. response.sendHeader();
  93. }
  94. tcpSocket->close();
  95. }
  96. }
  97. QString HttpServer::statusMessage(int statusCode, const QString& contentType)
  98. {
  99. QString description;
  100. switch(statusCode) {
  101. case 200:
  102. return QString("HTTP/1.0 200 OK\r\nContent-Type: %1\r\n\r\n").arg(contentType);
  103. case 403:
  104. description = QString("403 Forbidden");
  105. break;
  106. case 404:
  107. description = QString("404 File Not Found");
  108. break;
  109. case 500:
  110. description = QString("500 Internal Server Error");
  111. break;
  112. }
  113. return QString("HTTP/1.0 %1\r\nContent-Type: text/html\r\n\r\n<html><body><h1>%1</h1></body></html>\r\n").arg(description);
  114. }
  115. void HttpServer::handleDisconnect()
  116. {
  117. QTcpSocket* tcpSocket(qobject_cast<QTcpSocket*>(QObject::sender()));
  118. Q_ASSERT(tcpSocket != NULL);
  119. if (! tcpSocket->isValid())
  120. tcpSocket->deleteLater();
  121. }
  122. bool HttpServer::enabled() const
  123. {
  124. return m_tcpServer->isListening();
  125. }
  126. void HttpServer::setPort(int port)
  127. {
  128. m_port = port;
  129. }
  130. int HttpServer::port() const
  131. {
  132. return m_port;
  133. }
  134. LogModel* HttpServer::logModel() const
  135. {
  136. return m_logModel;
  137. }
  138. void HttpServer::setEnabled(bool enable)
  139. {
  140. if (enable == enabled()) {
  141. return;
  142. }
  143. if (enable) {
  144. m_tcpServer->listen(QHostAddress::Any, port());
  145. } else {
  146. m_tcpServer->close();
  147. }
  148. }
  149. QString HttpServer::wwwRoot() const
  150. {
  151. return m_wwwRoot;
  152. }
  153. void HttpServer::setWwwRoot(const QString & dir)
  154. {
  155. m_wwwRoot = dir;
  156. m_fileServlet->setWwwRoot(dir);
  157. }
  158. void HttpServer::addServlet(const QString & key, AbstractServlet* servlet)
  159. {
  160. m_servlets.insert(key, servlet);
  161. }