/thirdparty/qxt/qxtweb-standalone/qxtweb/qxthttpserverconnector.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 125 lines · 56 code · 9 blank · 60 comment · 10 complexity · 1a0035c3c52f77ce0fcc1f25af4f8ab7 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) Qxt Foundation. Some rights reserved.
  4. **
  5. ** This file is part of the QxtWeb module of the Qxt library.
  6. **
  7. ** This library is free software; you can redistribute it and/or modify it
  8. ** under the terms of the Common Public License, version 1.0, as published
  9. ** by IBM, and/or under the terms of the GNU Lesser General Public License,
  10. ** version 2.1, as published by the Free Software Foundation.
  11. **
  12. ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
  13. ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
  14. ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
  15. ** FITNESS FOR A PARTICULAR PURPOSE.
  16. **
  17. ** You should have received a copy of the CPL and the LGPL along with this
  18. ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
  19. ** included with the source distribution for more information.
  20. ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
  21. **
  22. ** <http://libqxt.org> <foundation@libqxt.org>
  23. **
  24. ****************************************************************************/
  25. /*!
  26. \class QxtHttpServerConnector
  27. \inmodule QxtWeb
  28. \brief The QxtHttpServerConnector class provides a built-in HTTP server for QxtHttpSessionManager
  29. QxtHttpSessionManager does the work of managing sessions and state for the
  30. otherwise stateless HTTP protocol, but it relies on QxtAbstractHttpConnector
  31. subclasses to implement the protocol used to communicate with the web server.
  32. QxtHttpServerConnector implements a complete HTTP server internally and does
  33. not require an external web server to function. However, it provides very
  34. little control over the behavior of the web server and may not suitable for
  35. high traffic scenarios or virtual hosting configurations.
  36. \sa QxtHttpSessionManager
  37. */
  38. #include "qxthttpsessionmanager.h"
  39. #include "qxtwebevent.h"
  40. #include <QTcpServer>
  41. #include <QHash>
  42. #include <QTcpSocket>
  43. #include <QString>
  44. #ifndef QXT_DOXYGEN_RUN
  45. class QxtHttpServerConnectorPrivate : public QxtPrivate<QxtHttpServerConnector>
  46. {
  47. public:
  48. QTcpServer* server;
  49. };
  50. #endif
  51. /*!
  52. * Creates a QxtHttpServerConnector with the given \a parent.
  53. */
  54. QxtHttpServerConnector::QxtHttpServerConnector(QObject* parent) : QxtAbstractHttpConnector(parent)
  55. {
  56. QXT_INIT_PRIVATE(QxtHttpServerConnector);
  57. qxt_d().server = new QTcpServer(this);
  58. QObject::connect(qxt_d().server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
  59. }
  60. /*!
  61. * \reimp
  62. */
  63. bool QxtHttpServerConnector::listen(const QHostAddress& iface, quint16 port)
  64. {
  65. return qxt_d().server->listen(iface, port);
  66. }
  67. /*!
  68. * \internal
  69. */
  70. void QxtHttpServerConnector::acceptConnection()
  71. {
  72. QTcpSocket* socket = qxt_d().server->nextPendingConnection();
  73. addConnection(socket);
  74. }
  75. /*!
  76. * \reimp
  77. */
  78. bool QxtHttpServerConnector::canParseRequest(const QByteArray& buffer)
  79. {
  80. if (buffer.indexOf("\r\n\r\n") >= 0) return true; // 1.0+
  81. if (buffer.indexOf("\r\n") >= 0 && buffer.indexOf("HTTP/") == -1) return true; // 0.9
  82. return false;
  83. }
  84. /*!
  85. * \reimp
  86. */
  87. QHttpRequestHeader QxtHttpServerConnector::parseRequest(QByteArray& buffer)
  88. {
  89. int pos = buffer.indexOf("\r\n\r\n"), endpos = pos + 3;
  90. if (pos == -1)
  91. {
  92. pos = buffer.indexOf("\r\n"); // 0.9
  93. endpos = pos + 1;
  94. }
  95. QHttpRequestHeader header(QString::fromUtf8(buffer.left(endpos)));
  96. QByteArray firstLine = buffer.left(buffer.indexOf('\r'));
  97. if (firstLine.indexOf("HTTP/") == -1)
  98. {
  99. header.setRequest(header.method(), header.path(), 0, 9);
  100. }
  101. buffer.remove(0, endpos + 1);
  102. return header;
  103. }
  104. /*!
  105. * \reimp
  106. */
  107. void QxtHttpServerConnector::writeHeaders(QIODevice* device, const QHttpResponseHeader& header)
  108. {
  109. if (header.majorVersion() == 0) return; // 0.9 doesn't have headers
  110. device->write(header.toString().toUtf8());
  111. }