1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#!/usr/bin/perl -w
## This program is based on an example program for Qt. It
## may be used, distributed and modified without limitation.
##
## Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
# When a new client connects, the server constructs a Qt::Socket and all
# communication with the client is done over this Socket object. Qt::Socket
# works asynchronously - this means that all the communication is done
# through the two slots readClient() and discardClient().
package HttpDaemon;
use Qt;
use Qt::isa qw(Qt::ServerSocket);
use Qt::signals
newConnect => [],
endConnect => [],
wroteToClient => [];
use Qt::slots
readClient => [],
discardClient => [];
use Qt::attributes qw(
sockets
);
sub NEW
{
shift->SUPER::NEW(8080, 1, $_[0]);
if( !this->ok() )
{
die "Failed to bind to port 8080\n";
}
sockets = {};
}
sub newConnection
{
my $s = Qt::Socket( this );
this->connect( $s, SIGNAL 'readyRead()', this, SLOT 'readClient()' );
this->connect( $s, SIGNAL 'delayedCloseFinished()', this, SLOT 'discardClient()' );
$s->setSocket( shift );
sockets->{ $s } = $s;
emit newConnect();
}
sub readClient
{
# This slot is called when the client sent data to the server. The
# server looks if it was a get request and sends a very simple HTML
# document back.
my $s = sender();
if ( $s->canReadLine() )
{
my @tokens = split( /\s\s*/, $s->readLine() );
if ( $tokens[0] eq "GET" )
{
my $string = "HTTP/1.0 200 Ok\n\rContent-Type: text/html; charset=\"utf-8\"\n\r".
"\n\r<h1>Nothing to see here</h1>\n";
$s->writeBlock($string, length($string));
$s->close();
emit wroteToClient();
}
}
}
sub discardClient
{
my $s = sender();
sockets->{$s} = 0;
emit endConnect();
}
1;
# HttpInfo provides a simple graphical user interface to the server and shows
# the actions of the server.
package HttpInfo;
use Qt;
use Qt::isa qw(Qt::VBox);
use Qt::slots
newConnect => [],
endConnect => [],
wroteToClient => [];
use Qt::attributes qw(
httpd
infoText
);
use HttpDaemon;
sub NEW
{
shift->SUPER::NEW(@_);
httpd = HttpDaemon( this );
my $port = httpd->port();
my $itext = "This is a small httpd example.\n".
"You can connect with your\n".
"web browser to port $port\n";
my $lb = Label( $itext, this );
$lb->setAlignment( &AlignHCenter );
infoText = TextView( this );
my $quit = PushButton( "quit" , this );
this->connect( httpd, SIGNAL 'newConnect()', SLOT 'newConnect()' );
this->connect( httpd, SIGNAL 'endConnect()', SLOT 'endConnect()' );
this->connect( httpd, SIGNAL 'wroteToClient()', SLOT 'wroteToClient()' );
this->connect( $quit, SIGNAL 'pressed()', Qt::app(), SLOT 'quit()' );
}
sub newConnect
{
infoText->append( "New connection" );
}
sub endConnect
{
infoText->append( "Connection closed\n\n" );
}
sub wroteToClient
{
infoText->append( "Wrote to client" );
}
1;
package main;
use Qt;
use HttpInfo;
my $app = Qt::Application(\@ARGV);
my $info = HttpInfo;
$app->setMainWidget($info);
$info->show;
exit $app->exec;
|