summaryrefslogtreecommitdiffstats
path: root/doc/scriptexamples/simplehttp/simplehttp.kvs
diff options
context:
space:
mode:
Diffstat (limited to 'doc/scriptexamples/simplehttp/simplehttp.kvs')
-rw-r--r--doc/scriptexamples/simplehttp/simplehttp.kvs105
1 files changed, 105 insertions, 0 deletions
diff --git a/doc/scriptexamples/simplehttp/simplehttp.kvs b/doc/scriptexamples/simplehttp/simplehttp.kvs
new file mode 100644
index 00000000..8c49014a
--- /dev/null
+++ b/doc/scriptexamples/simplehttp/simplehttp.kvs
@@ -0,0 +1,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