1 | /****************************************************************************
|
---|
2 | ** $Id: httpd.cpp 2 2005-11-16 15:49:26Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 | #include <stdlib.h>
|
---|
11 | #include <qsocket.h>
|
---|
12 | #include <qregexp.h>
|
---|
13 | #include <qserversocket.h>
|
---|
14 | #include <qapplication.h>
|
---|
15 | #include <qmainwindow.h>
|
---|
16 | #include <qtextstream.h>
|
---|
17 | #include <qvbox.h>
|
---|
18 | #include <qlabel.h>
|
---|
19 | #include <qtextview.h>
|
---|
20 | #include <qpushbutton.h>
|
---|
21 |
|
---|
22 | // HttpDaemon is the the class that implements the simple HTTP server.
|
---|
23 | class HttpDaemon : public QServerSocket
|
---|
24 | {
|
---|
25 | Q_OBJECT
|
---|
26 | public:
|
---|
27 | HttpDaemon( QObject* parent=0 ) :
|
---|
28 | QServerSocket(8080,1,parent)
|
---|
29 | {
|
---|
30 | if ( !ok() ) {
|
---|
31 | qWarning("Failed to bind to port 8080");
|
---|
32 | exit( 1 );
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | void newConnection( int socket )
|
---|
37 | {
|
---|
38 | // When a new client connects, the server constructs a QSocket and all
|
---|
39 | // communication with the client is done over this QSocket. QSocket
|
---|
40 | // works asynchronouslyl, this means that all the communication is done
|
---|
41 | // in the two slots readClient() and discardClient().
|
---|
42 | QSocket* s = new QSocket( this );
|
---|
43 | connect( s, SIGNAL(readyRead()), this, SLOT(readClient()) );
|
---|
44 | connect( s, SIGNAL(delayedCloseFinished()), this, SLOT(discardClient()) );
|
---|
45 | s->setSocket( socket );
|
---|
46 | emit newConnect();
|
---|
47 | }
|
---|
48 |
|
---|
49 | signals:
|
---|
50 | void newConnect();
|
---|
51 | void endConnect();
|
---|
52 | void wroteToClient();
|
---|
53 |
|
---|
54 | private slots:
|
---|
55 | void readClient()
|
---|
56 | {
|
---|
57 | // This slot is called when the client sent data to the server. The
|
---|
58 | // server looks if it was a get request and sends a very simple HTML
|
---|
59 | // document back.
|
---|
60 | QSocket* socket = (QSocket*)sender();
|
---|
61 | if ( socket->canReadLine() ) {
|
---|
62 | QStringList tokens = QStringList::split( QRegExp("[ \r\n][ \r\n]*"), socket->readLine() );
|
---|
63 | if ( tokens[0] == "GET" ) {
|
---|
64 | QTextStream os( socket );
|
---|
65 | os.setEncoding( QTextStream::UnicodeUTF8 );
|
---|
66 | os << "HTTP/1.0 200 Ok\r\n"
|
---|
67 | "Content-Type: text/html; charset=\"utf-8\"\r\n"
|
---|
68 | "\r\n"
|
---|
69 | "<h1>Nothing to see here</h1>\n";
|
---|
70 | socket->close();
|
---|
71 | emit wroteToClient();
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 | void discardClient()
|
---|
76 | {
|
---|
77 | QSocket* socket = (QSocket*)sender();
|
---|
78 | delete socket;
|
---|
79 | emit endConnect();
|
---|
80 | }
|
---|
81 | };
|
---|
82 |
|
---|
83 |
|
---|
84 | // HttpInfo provides a simple graphical user interface to the server and shows
|
---|
85 | // the actions of the server.
|
---|
86 | class HttpInfo : public QVBox
|
---|
87 | {
|
---|
88 | Q_OBJECT
|
---|
89 | public:
|
---|
90 | HttpInfo()
|
---|
91 | {
|
---|
92 | HttpDaemon *httpd = new HttpDaemon( this );
|
---|
93 |
|
---|
94 | QString itext = QString(
|
---|
95 | "This is a small httpd example.\n"
|
---|
96 | "You can connect with your\n"
|
---|
97 | "web browser to port %1"
|
---|
98 | ).arg( httpd->port() );
|
---|
99 | QLabel *lb = new QLabel( itext, this );
|
---|
100 | lb->setAlignment( AlignHCenter );
|
---|
101 | infoText = new QTextView( this );
|
---|
102 | QPushButton *quit = new QPushButton( "quit" , this );
|
---|
103 |
|
---|
104 | connect( httpd, SIGNAL(newConnect()), SLOT(newConnect()) );
|
---|
105 | connect( httpd, SIGNAL(endConnect()), SLOT(endConnect()) );
|
---|
106 | connect( httpd, SIGNAL(wroteToClient()), SLOT(wroteToClient()) );
|
---|
107 | connect( quit, SIGNAL(pressed()), qApp, SLOT(quit()) );
|
---|
108 | }
|
---|
109 |
|
---|
110 | ~HttpInfo()
|
---|
111 | {
|
---|
112 | }
|
---|
113 |
|
---|
114 | private slots:
|
---|
115 | void newConnect()
|
---|
116 | {
|
---|
117 | infoText->append( "New connection" );
|
---|
118 | }
|
---|
119 | void endConnect()
|
---|
120 | {
|
---|
121 | infoText->append( "Connection closed\n\n" );
|
---|
122 | }
|
---|
123 | void wroteToClient()
|
---|
124 | {
|
---|
125 | infoText->append( "Wrote to client" );
|
---|
126 | }
|
---|
127 |
|
---|
128 | private:
|
---|
129 | QTextView *infoText;
|
---|
130 | };
|
---|
131 |
|
---|
132 |
|
---|
133 | int main( int argc, char** argv )
|
---|
134 | {
|
---|
135 | QApplication app( argc, argv );
|
---|
136 | HttpInfo info;
|
---|
137 | app.setMainWidget( &info );
|
---|
138 | info.show();
|
---|
139 | return app.exec();
|
---|
140 | }
|
---|
141 |
|
---|
142 | #include "httpd.moc"
|
---|