PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/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/*!
 27\class QxtHttpServerConnector
 28
 29\inmodule QxtWeb
 30
 31\brief The QxtHttpServerConnector class provides a built-in HTTP server for QxtHttpSessionManager
 32
 33QxtHttpSessionManager does the work of managing sessions and state for the
 34otherwise stateless HTTP protocol, but it relies on QxtAbstractHttpConnector
 35subclasses to implement the protocol used to communicate with the web server.
 36
 37QxtHttpServerConnector implements a complete HTTP server internally and does
 38not require an external web server to function. However, it provides very
 39little control over the behavior of the web server and may not suitable for
 40high traffic scenarios or virtual hosting configurations.
 41
 42\sa QxtHttpSessionManager
 43*/
 44#include "qxthttpsessionmanager.h"
 45#include "qxtwebevent.h"
 46#include <QTcpServer>
 47#include <QHash>
 48#include <QTcpSocket>
 49#include <QString>
 50
 51#ifndef QXT_DOXYGEN_RUN
 52class QxtHttpServerConnectorPrivate : public QxtPrivate<QxtHttpServerConnector>
 53{
 54public:
 55    QTcpServer* server;
 56};
 57#endif
 58
 59/*!
 60 * Creates a QxtHttpServerConnector with the given \a parent.
 61 */
 62QxtHttpServerConnector::QxtHttpServerConnector(QObject* parent) : QxtAbstractHttpConnector(parent)
 63{
 64    QXT_INIT_PRIVATE(QxtHttpServerConnector);
 65    qxt_d().server = new QTcpServer(this);
 66    QObject::connect(qxt_d().server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
 67}
 68
 69/*!
 70 * \reimp
 71 */
 72bool QxtHttpServerConnector::listen(const QHostAddress& iface, quint16 port)
 73{
 74    return qxt_d().server->listen(iface, port);
 75}
 76
 77/*!
 78 * \internal
 79 */
 80void QxtHttpServerConnector::acceptConnection()
 81{
 82    QTcpSocket* socket = qxt_d().server->nextPendingConnection();
 83    addConnection(socket);
 84}
 85
 86/*!
 87 * \reimp
 88 */
 89bool QxtHttpServerConnector::canParseRequest(const QByteArray& buffer)
 90{
 91    if (buffer.indexOf("\r\n\r\n") >= 0) return true; // 1.0+
 92    if (buffer.indexOf("\r\n") >= 0 && buffer.indexOf("HTTP/") == -1) return true; // 0.9
 93    return false;
 94}
 95
 96/*!
 97 * \reimp
 98 */
 99QHttpRequestHeader QxtHttpServerConnector::parseRequest(QByteArray& buffer)
100{
101    int pos = buffer.indexOf("\r\n\r\n"), endpos = pos + 3;
102    if (pos == -1)
103    {
104        pos = buffer.indexOf("\r\n"); // 0.9
105        endpos = pos + 1;
106    }
107
108    QHttpRequestHeader header(QString::fromUtf8(buffer.left(endpos)));
109    QByteArray firstLine = buffer.left(buffer.indexOf('\r'));
110    if (firstLine.indexOf("HTTP/") == -1)
111    {
112        header.setRequest(header.method(), header.path(), 0, 9);
113    }
114    buffer.remove(0, endpos + 1);
115    return header;
116}
117
118/*!
119 * \reimp
120 */
121void QxtHttpServerConnector::writeHeaders(QIODevice* device, const QHttpResponseHeader& header)
122{
123    if (header.majorVersion() == 0) return; // 0.9 doesn't have headers
124    device->write(header.toString().toUtf8());
125}