# # 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("\n") %tmp->$write(" KVIrc simple http server\n") %tmp->$write(" \n") %tmp->$write("

Hello World!

\n") %tmp->$write(" \n") %tmp->$write("\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