summaryrefslogtreecommitdiffstats
path: root/doc/scriptexamples/simplehttp/simplehttp.kvs
blob: 8c49014aea744f6021f00c488429f52636e2ef4b (plain)
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
#
# This is a really simple and senseless HTTP server
# It listens on port 8080 and serves a "Hello world" page
# to the incoming browser connections.
#


# Define the "startup" alias
alias(simplehttp_start)
{
	# If the server is already running , well... give up
	if($classDefined(simpleHttpListeningSocket) && (%SimpleHttpListeningSocket))
	{
		echo "The Simple HTTP server is already running"
		halt
	}

	# Define the server class (if the class is already defined it will be overridden)
	class(simpleHttpListeningSocket,socket)
	{
		# The constructor fails if we can't listen on port 8080!
		constructor()
		{
			if(!$$->$listen(8080))
			{
				return 0
			}
			return 1
		}

		# Handle the incoming connections
		incomingConnectionEvent()
		{
			# Create a slave socket
			%tmp = $new(socket)
			# Accept the connection
			$$->$accept(%tmp)
			# And handle it
			echo "Incoming connection:"
			echo "Remote end: %tmp->$remoteIp : %tmp->$remotePort"
			echo "Local end: %tmp->$localIp : %tmp->$localPort"
			# Write a "Hello world" page to the client
			%tmp->$write("HTTP/1.0 200 OK\r\n");
			%tmp->$write("Content-type: text/html\r\n\r\n");
			%tmp->$write("<html>\n")
			%tmp->$write(" <head><title>KVIrc simple http server</title></head>\n")
			%tmp->$write(" <body bgcolor=\"#000000\" text=\"#FF0000\">\n")
			%tmp->$write("  <center><h1><b>Hello World!</b></h1></center>\n")
			%tmp->$write(" </body>\n")
			%tmp->$write("</html>\n\r\n\r\n")
			# Close (this ensures data delivery)
			%tmp->$close()
			# Kill the slave
			delete %tmp;
			# Some browsers may tell you that the connection is "broken"
			# (konqueror , for example) since they can't send
			# the GET request... (we don't read it!)
			# Well...this is left as exercise to the scripter:
			# Write the data to the socket only in response to the GET message :)
			# You CAN do it.
		}
	}

	# Create the server socket
	%SimpleHttpListeningSocket = $new(simpleHttpListeningSocket)

	if(!%SimpleHttpListeningSocket)echo "Ops.. can't start the simple http server :((("
	else {
		echo "Ok.. up and running :)"
		echo "Point your browser to http://127.0.0.1:8080"
	}

}

# Stop alias : this is easy

alias(simplehttp_stop)
{
	if(!%SimpleHttpListeningSocket)
	{
		echo "The simple HTTP server is not running"
		halt
	}
	%SimpleHttpListeningSocket->$close()
	delete %SimpleHttpListeningSocket
	%SimpleHttpListeningSocket = ""
}

# Uninstall alias: another easy one

alias(simplehttp_uninstall)
{
	# Stop the service eventually
	if(%SimpleHttpListeningSocket)simplehttp_stop
	# Kill the class (again eventually)
	killclass -q simpleHttpListeningSocket
	# And kill the aliases (including "myself")
	alias(simplehttp_start){}
	alias(simplehttp_stop){}
	alias(simplehttp_uninstall){}
}

# and let's start it the first time

simplehttp_start