tests/auto/qhttp/dummyserver.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 // Use if you need
       
    42 
       
    43 class DummyHttpServer : public QTcpServer
       
    44 {
       
    45     Q_OBJECT
       
    46 public:
       
    47     DummyHttpServer() : phase(Header)
       
    48         { listen(); }
       
    49 
       
    50 protected:
       
    51     enum {
       
    52         Header,
       
    53         Data1,
       
    54         Data2,
       
    55         Close
       
    56     } phase;
       
    57     void incomingConnection(int socketDescriptor)
       
    58     {
       
    59         QSslSocket *socket = new QSslSocket(this);
       
    60         socket->setSocketDescriptor(socketDescriptor, QAbstractSocket::ConnectedState);
       
    61         socket->ignoreSslErrors();
       
    62         socket->startServerEncryption();
       
    63         connect(socket, SIGNAL(readyRead()), SLOT(handleReadyRead()));
       
    64     }
       
    65 
       
    66 public slots:
       
    67     void handleReadyRead()
       
    68     {
       
    69         QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
       
    70         socket->readAll();
       
    71         if (phase != Header)
       
    72             return;
       
    73 
       
    74         phase = Data1;
       
    75         static const char header[] =
       
    76             "HTTP/1.0 200 OK\r\n"
       
    77             "Date: Fri, 07 Sep 2007 12:33:18 GMT\r\n"
       
    78             "Server: Apache\r\n"
       
    79             "Expires:\r\n"
       
    80             "Cache-Control:\r\n"
       
    81             "Pragma:\r\n"
       
    82             "Last-Modified: Thu, 06 Sep 2007 08:52:06 +0000\r\n"
       
    83             "Etag: a700f59a6ccb1ad39af68d998aa36fb1\r\n"
       
    84             "Vary: Accept-Encoding\r\n"
       
    85             "Content-Length: 6560\r\n"
       
    86             "Connection: close\r\n"
       
    87             "Content-Type: text/html; charset=utf-8\r\n"
       
    88             "\r\n";
       
    89 
       
    90 
       
    91         socket->write(header, sizeof header - 1);
       
    92         connect(socket, SIGNAL(bytesWritten(qint64)), SLOT(handleBytesWritten()), Qt::QueuedConnection);
       
    93     }
       
    94 
       
    95     void handleBytesWritten()
       
    96     {
       
    97         QTcpSocket *socket = static_cast<QTcpSocket *>(sender());
       
    98         if (socket->bytesToWrite() != 0)
       
    99             return;
       
   100 
       
   101         if (phase == Data1) {
       
   102             QByteArray data(4096, 'a');
       
   103             socket->write(data);
       
   104             phase = Data2;
       
   105         } else if (phase == Data2) {
       
   106             QByteArray data(2464, 'a');
       
   107             socket->write(data);
       
   108             phase = Close;
       
   109         } else {
       
   110             //socket->disconnectFromHost();
       
   111             //socket->deleteLater();
       
   112         }
       
   113     }
       
   114 };