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]
15
# Check if we want to stop the daemon
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; }
25
# Ok...we want to start the service....
26
# Well..check if we're already running
27
if("$root->$findChild(\"httpdaemon\")" != "0")
29
# Yes...already started !
30
echo HTTPD : Another HTTPD is already running
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
40
if($classDefined(httpd))clearobjects httpd
42
# Daemon class : listens on port 80 for incoming requests
48
if(!$this->$listen(default,80))
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:
56
event(OnIncomingConnection)
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
68
# Same as above for the class definition
70
if($classDefined(httpdchildsock))clearobjects httpdchildsock
72
# Child socket , handles the requests
74
class(httpdchildsock,socket)
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.
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
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)
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