Hello world/Web server: Difference between revisions

Content added Content deleted
(Added Pony)
Line 1,498: Line 1,498:
return -1; // -1 is a special case that retirns control to the backend
return -1; // -1 is a special case that retirns control to the backend
}
}
</lang>

=={{header|Pony}}==
Using only TCP listeners, not the in-built HTTP server package
<lang Pony>
use "net"

actor Main
new create(env: Env) =>
try TCPListener(env.root as AmbientAuth,
Listener,
"127.0.0.1", "8080")
else env.err.print("unable to use the network")
end

// Boilerplate code, create a TCP listener on a socket
class Listener is TCPListenNotify

new iso create() => None

fun ref listening(_: TCPListener ref) => None

fun ref not_listening(listen: TCPListener ref) => listen.close()

fun ref connected(listen: TCPListener ref): TCPConnectionNotify iso^ =>
Server


// HTTP server that handles the TCP connections
class val Server is TCPConnectionNotify

// Empty ctor
new iso create() => None

// Impl for when server accepts the client request
fun accepted(_: TCPConnection ref) => None

// Impl for when server receives client data
fun ref received(conn: TCPConnection ref, _: Array[U8] iso, _: USize)
: Bool
=>
// handle request
conn.write("HTTP/1.1 200 OK\r\n\r\n")
conn.write("<html><body><p>")
conn.write("Goodbye, World!")
conn.write("</p></body></html>")
conn.dispose()
false

// Impl for when client closes the connection
fun ref closed(conn: TCPConnection ref) => conn.dispose()

// Impl for when client fails to connect to all possible addresses for the server
fun ref connect_failed(_: TCPConnection ref) => None
</lang>
</lang>