~ubuntu-branches/ubuntu/feisty/kvirc/feisty

« back to all changes in this revision

Viewing changes to doc/script-examples/httpd.kvs

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2002-04-16 13:47:41 UTC
  • Revision ID: james.westby@ubuntu.com-20020416134741-02slrqk6ige4cchu
Tags: 1:2.1.2-11
* #138169: The problem in bug #138169 is due to a bug in libtool. See
  bug #98342 for details. KVirc still doesn't build correctly even with
  the latest libtool (1.4.2-4). When this gets properly fixed I'll update
  kvirc's build dependency on libtool. In the mean time, I've applied a
  patch from that bug report which fixes it for me.
  (Closes: #138169)
* Redid debian/rules somewhat; no longer try to build differently
  depending on how KDE is installed. If the preferred configuration breaks
  for some platform, I'd rather know about it.
* Don't link versus qt-mt anymore.
* GNU config automated update: config.sub (20010907 to 20020307),
  config.guess (20010904 to 20020320)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# this script shows how to implement a really simple http daemon
 
2
# It listens on the port 80 and sends a simple fixed html document
 
3
# to all the incoming requests.
 
4
# You can try it by /PARSING this file , or by copying its contens
 
5
# and placing it in an alias.
 
6
# If you pass "stop" as the first parameter the service will be stopped
 
7
# otherwise will be started.
 
8
# This script should be really splitted in two parts:
 
9
# The classes declaration should be executed only once per session
 
10
# (you could put it in the OnStartup event) and the daemon-start/stop
 
11
# part should go into the *real* alias.
 
12
# Anyway...it will work also in this way....
 
13
# syntax : httpd [stop]
 
14
 
 
15
# Check if we want to stop the daemon
 
16
if("$1" == "stop")
 
17
{
 
18
        # Yes...find it and KILL it :)
 
19
        %o = $root->$findChild("httpdaemon");
 
20
        if("%o" == "0")echo HTTPD : No daemon running
 
21
        else {  destroy %o ; echo HTTPD : Stopped the service; }
 
22
        halt;
 
23
}
 
24
 
 
25
# Ok...we want to start the service....
 
26
# Well..check if we're already running
 
27
if("$root->$findChild(\"httpdaemon\")" != "0")
 
28
{
 
29
        # Yes...already started !
 
30
        echo HTTPD : Another HTTPD is already running
 
31
        halt
 
32
}
 
33
 
 
34
# If the class is already defined remove it
 
35
# This is useful for testing & changing this script.
 
36
# Once we're sure that the class definition is
 
37
# working correctly we might put it into
 
38
# the OnStartup event, so it will be called just once
 
39
 
 
40
if($classDefined(httpd))clearobjects httpd
 
41
 
 
42
# Daemon class : listens on port 80 for incoming requests
 
43
class(httpd,socket)
 
44
{
 
45
        function(constructor)
 
46
        {
 
47
                # Autolisten
 
48
                if(!$this->$listen(default,80))
 
49
                {
 
50
                        echo HTTPD : Failed to listen on port 80 : $this->$lastError() : Dying
 
51
                        # If we return 0 , KVIrc will automatically destroy the object
 
52
                        # We have failed , so we die:
 
53
                        setreturn 0
 
54
                }
 
55
        }
 
56
        event(OnIncomingConnection)
 
57
        {
 
58
                # A connection arrived...setup a socket to handle it
 
59
                %sock = $new(httpdchildsock,$this,httpdchild);
 
60
                # To be sure , we should check %sock for valid object id's (not "0")
 
61
                # but it is really uncommon that it will fail so...
 
62
                # Accept the incoming connection
 
63
                if(!%sock->$accept($this))echo HTTPD : Failed to accept an incoming connection : %sock->$lastError()
 
64
                # $this object remains listening , the request is now handled by %sock
 
65
        }
 
66
}
 
67
 
 
68
# Same as above for the class definition
 
69
 
 
70
if($classDefined(httpdchildsock))clearobjects httpdchildsock
 
71
 
 
72
# Child socket , handles the requests
 
73
 
 
74
class(httpdchildsock,socket)
 
75
{
 
76
        event(OnDataReceived)
 
77
        {
 
78
                # Here we have received a request...
 
79
                # In fact we should parse the data received and check for valid HTTP requests..
 
80
                # but we would go out of the scope of this examle
 
81
                # so ignore anything that we might receive , just send out the simple data
 
82
                if($this->$write("<html><body><h1>Hello world</h1></body></html>$lf") < 0) echo HTTPD : Failed to write the http document.
 
83
                # and then die.
 
84
                destroy $this
 
85
        }
 
86
}
 
87
 
 
88
# Ok...the classes are defined
 
89
# As said before , all the above code should really go into a separate
 
90
# alias or event handler , so it would be called only once per session
 
91
# The real part of the httpd script is here:
 
92
# Simply create the daemon object
 
93
 
 
94
%void = $new(httpd,$root,"httpdaemon")
 
95
echo HTTPD : Service started
 
96
echo HTTPD : Listening on port 80 (open netscape and write 127.0.0.1 in the location field)
 
97
 
 
98
# Ok...now just open netscape and write 127.0.0.1 in the location field. :)
 
99
# If you get connection errors , make sure that your loopback interface is up.# A sample implementation of a time service daemon