#!/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