File: httpd.kvs

package info (click to toggle)
kvirc2 2.1.3-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 16,308 kB
  • ctags: 9,533
  • sloc: cpp: 86,257; sh: 10,532; makefile: 631; perl: 419; ansic: 315; sed: 16
file content (99 lines) | stat: -rw-r--r-- 3,387 bytes parent folder | download | duplicates (2)
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
# This script shows how to implement a really simple http daemon
# It listens on the port 80 and sends a simple fixed html document
# to all the incoming requests.
# You can try it by /PARSING this file , or by copying its contens
# and placing it in an alias.
# If you pass "stop" as the first parameter the service will be stopped
# otherwise will be started.
# This script should be really split in two parts:
# The classes declaration should be executed only once per session
# (you could put it in the OnStartup event) and the daemon-start/stop
# part should go into the *real* alias.
# Anyway, it will work also in this way.
# syntax: httpd [stop]

# Check if we want to stop the daemon
if("$1" == "stop")
{
	# Yes, find it and KILL it :)
	%o = $root->$findChild("httpdaemon");
	if("%o" == "0")echo HTTPD: no daemon running
	else {  destroy %o ; echo HTTPD: stopped the service; }
	halt;
}

# OK, we want to start the service.
# Well, check if we're already running
if("$root->$findChild(\"httpdaemon\")" != "0")
{
	# Yes, already started!
	echo HTTPD: another HTTPD is already running
	halt
}

# If the class is already defined remove it.
# This is useful for testing and changing this script.
# Once we're sure that the class definition is
# working correctly we might put it into
# the OnStartup event, so it will be called just once

if($classDefined(httpd))clearobjects httpd

# Daemon class: listens on port 80 for incoming requests
class(httpd,socket)
{
	function(constructor)
	{
		# Autolisten
		if(!$this->$listen(default,80))
		{
			echo HTTPD: failed to listen on port 80: $this->$lastError(): dying
			# If we return 0, KVIrc will automatically destroy the object.
			# We have failed, so we die:
			setreturn 0
		}
	}
	event(OnIncomingConnection)
	{
		# A connection arrived. Set up a socket to handle it
		%sock = $new(httpdchildsock,$this,httpdchild);
		# To be sure, we should check %sock for valid object id's (not "0")
		# but it is really uncommon that it will fail so...
		# Accept the incoming connection
		if(!%sock->$accept($this))echo HTTPD: failed to accept an incoming connection: %sock->$lastError()
		# $this object remains listening , the request is now handled by %sock
	}
}

# Same as above for the class definition

if($classDefined(httpdchildsock))clearobjects httpdchildsock

# Child socket, handles the requests

class(httpdchildsock,socket)
{
	event(OnDataReceived)
	{
		# Here we have received a request...
		# In fact we should parse the data received and check for valid HTTP requests,
		# but we would go out of the scope of this example.
		# So ignore anything that we might receive, just send out the simple data
		if($this->$write("<html><body><h1>Hello world</h1></body></html>$lf") < 0) echo HTTPD: failed to write the http document.
		# and then die.
		destroy $this
	}
}

# OK, the classes are defined
# As said before, all the above code should really go into a separate
# alias or event handler, so it would be called only once per session
# The real part of the httpd script is here:
# Simply create the daemon object

%void = $new(httpd,$root,"httpdaemon")
echo HTTPD: service started
echo HTTPD: listening on port 80 (open netscape and write 127.0.0.1 in the location field)

# OK, now just open netscape and write 127.0.0.1 in the location field. :)
# If you get connection errors, make sure that your loopback interface is up.