|
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 examples 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 |
|
42 |
|
43 #include <QTcpServer> |
|
44 #include <QTcpSocket> |
|
45 |
|
46 #include "BrowserWindow.h" |
|
47 #include "httpserver.h" |
|
48 |
|
49 #define HTTP_PORT 80 |
|
50 |
|
51 httpServer::httpServer( QObject *parent ) |
|
52 : QTcpServer( parent ) |
|
53 { |
|
54 httpSocket = NULL; |
|
55 |
|
56 tcpServer = new QTcpServer( /* this */ ); |
|
57 not_done = true; |
|
58 bool result = tcpServer->listen(QHostAddress::Any, HTTP_PORT ); |
|
59 if ( !result ) |
|
60 { |
|
61 delete tcpServer; |
|
62 close(); |
|
63 not_done = false; |
|
64 return; |
|
65 } |
|
66 connect(tcpServer, SIGNAL(newConnection()), this, SLOT(handleNewConnection())); |
|
67 |
|
68 } |
|
69 |
|
70 httpServer::~httpServer() |
|
71 { |
|
72 if ( httpSocket ) |
|
73 { |
|
74 httpSocket->disconnect(SIGNAL(readyRead())); |
|
75 httpSocket->disconnect(SIGNAL(error(QAbstractSocket::SocketError))); |
|
76 httpSocket->close(); |
|
77 httpSocket->abort(); |
|
78 httpSocket->deleteLater(); |
|
79 httpSocket = NULL; |
|
80 } |
|
81 } |
|
82 |
|
83 |
|
84 bool httpServer::isDone() |
|
85 { |
|
86 return not_done; |
|
87 } |
|
88 void httpServer::handleNewConnection() |
|
89 { |
|
90 QTcpSocket *clientConnection = tcpServer->nextPendingConnection(); |
|
91 clientConnection->setTextModeEnabled(true); |
|
92 |
|
93 if ( !httpSocket ) |
|
94 { |
|
95 httpSocket = clientConnection; |
|
96 connect( httpSocket, |
|
97 SIGNAL(readyRead()), |
|
98 this, |
|
99 SLOT(readConnection())); |
|
100 connect(httpSocket, SIGNAL(error(QAbstractSocket::SocketError)), |
|
101 this, SLOT(displayConnectionError(QAbstractSocket::SocketError))); |
|
102 } |
|
103 else |
|
104 { |
|
105 // We already are busy with open connection?! |
|
106 // qDebug() << "Server busy.\n"; |
|
107 clientConnection->disconnectFromHost(); |
|
108 } |
|
109 } |
|
110 |
|
111 |
|
112 void httpServer::displayConnectionError(QAbstractSocket::SocketError /* socketError */ ) |
|
113 { |
|
114 httpSocket->disconnect(SIGNAL(readyRead())); |
|
115 httpSocket->disconnect(SIGNAL(error(QAbstractSocket::SocketError))); |
|
116 httpSocket->close(); |
|
117 httpSocket->abort(); |
|
118 httpSocket->deleteLater(); |
|
119 httpSocket = NULL; |
|
120 } |
|
121 |
|
122 |
|
123 void httpServer::readConnection() |
|
124 { |
|
125 while (httpSocket->canReadLine()) |
|
126 { |
|
127 char buf[256]; |
|
128 qint64 lineLength = httpSocket->readLine(buf, sizeof(buf)); |
|
129 if (lineLength != -1) |
|
130 { |
|
131 QString tmpCommand(buf); |
|
132 tmpCommand = tmpCommand.simplified(); |
|
133 |
|
134 // qDebug() << "Received: '" << buf << "'\n"; |
|
135 |
|
136 if ( tmpCommand.left(4) == QString("GET ") ) |
|
137 { |
|
138 qDebug() << "QTESTWEB: Received GET:" << buf << "\n"; |
|
139 // Craft and send a response |
|
140 QString answer("HTTP/1.x 200 OK\nDate: Tue, 06 Jul 2004 17:59:22 GMT\n"); |
|
141 answer.append("Expires: Thu, 01 Jan 1970 00:00:00 GMT\n"); |
|
142 answer.append("Cache-Control: private, must-revalidate\n"); |
|
143 answer.append("Via: 1.0 cthulhu\n"); |
|
144 answer.append("Server: Apache/1.3.29 (Unix) PHP/4.3.4\n"); |
|
145 answer.append("X-Powered-By: PHP/4.3.4\n"); |
|
146 answer.append("Set-Cookie: fiwikiSession=a8015bd32eee4e5bd46fd83aea40f159; path=/\n"); |
|
147 answer.append("Vary: Accept-Encoding,Cookie\n"); |
|
148 answer.append("Content-Language: fi\n"); |
|
149 answer.append("Content-Type: text/html; charset=utf-8\n"); |
|
150 answer.append("X-Cache: MISS from wikipedia.org\n"); |
|
151 answer.append("Transfer-Encoding: chunked\n"); |
|
152 answer.append("Connection: keep-alive\n"); |
|
153 httpSocket->write(qPrintable(answer)); |
|
154 QString content("<HTML><HEAD><TITLE>Example Page</TITLE></HEAD>\n"); |
|
155 content.append("<body><p>Empty page sent by test server.</p></BODY></HTML>"); |
|
156 httpSocket->write(qPrintable(content)); |
|
157 httpSocket->flush(); |
|
158 |
|
159 QTest::qWait(200); |
|
160 |
|
161 not_done = false; |
|
162 } |
|
163 /* |
|
164 else |
|
165 { |
|
166 qDebug() << "Received unknown:" << buf << "\n"; |
|
167 } |
|
168 */ |
|
169 } |
|
170 } |
|
171 } |
|
172 |