Languages

[Genie] Simple server test

Your rating: None Average: 5 (1 vote)
[indent=4]
 
def process_request (input : InputStream, output : OutputStream) raises Error
    var data_in = new DataInputStream (input)
    line : string
    while ((line = data_in.read_line (null, null)) != null)
        print line
        if (line.strip () == "") do break
 
    content : string = "<html><h1>Hello from Genie server</h1></html>"
    var header = new StringBuilder ()
    header.append ("HTTP/1.0 200 OK\r\n")
    header.append ("Content-Type: text/html\r\n")
    header.append_printf ("Content-Length: %lu\r\n\r\n", content.size ())
    output.write (header.str, header.len, null)
    output.write (content, content.size (), null)
    output.flush (null)
 
init
    try
        var service = new SocketService ()
        service.add_inet_port (8080, null)
        service.start ()
        while true
            var conn = service.accept (null, null)
            process_request (conn.input_stream, conn.output_stream)
    except e : GLib.Error
        print (e.message)
    return