diff options
author | Andreas Rumpf <andreas@andreas-desktop> | 2010-01-05 00:34:15 +0100 |
---|---|---|
committer | Andreas Rumpf <andreas@andreas-desktop> | 2010-01-05 00:34:15 +0100 |
commit | a8303679960a13aa9ddd1f2fa44c2e4f9a12f071 (patch) | |
tree | 69cc1b3d8c501eb3d7fddaffe3d0e4e06bd5c82e /lib/devel | |
parent | a58a2f3823c33104992dc0e4129fa53e66a18f44 (diff) | |
download | Nim-a8303679960a13aa9ddd1f2fa44c2e4f9a12f071.tar.gz |
further development of httpserver
Diffstat (limited to 'lib/devel')
-rwxr-xr-x[-rw-r--r--] | lib/devel/httpserver.nim | 92 |
1 files changed, 74 insertions, 18 deletions
diff --git a/lib/devel/httpserver.nim b/lib/devel/httpserver.nim index ca1e315f0..285dadf0a 100644..100755 --- a/lib/devel/httpserver.nim +++ b/lib/devel/httpserver.nim @@ -134,14 +134,6 @@ proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) = # --------------- Server Setup ----------------------------------------------- -proc startup(): tuple[socket: TSocket, port: TPort] = - var s = socket(AF_INET) - if s == InvalidSocket: OSError() - bindAddr(s) - listen(s) - result.socket = s - result.port = getSockName(s) - proc acceptRequest(client: TSocket) = var cgi = false var query = "" @@ -181,16 +173,80 @@ proc acceptRequest(client: TSocket) = else: executeCgi(client, path, query, meth) -proc main = - var (server, port) = startup() - echo("httpserver running on port ", int16(port)) +type + TServer* = object + socket: TSocket + port: TPort + client*: TSocket ## the socket to write the file data to + path*, query*: string ## path and query the client requested + +proc open*(s: var TServer, port = TPort(0)) = + ## creates a new server at port `port`. If ``port == 0`` a free port is + ## aquired that can be accessed later by the ``port`` proc. + s.socket = socket(AF_INET) + if s.socket == InvalidSocket: OSError() + bindAddr(s.socket) + listen(s.socket) + s.port = getSockName(s.socket) + s.client = InvalidSocket + s.path = "" + s.query = "" + +proc port*(s: var TServer): TPort = + ## get the port number the server has aquired. + result = s.port - while true: - var client = accept(server) - if client == InvalidSocket: OSError() - acceptRequest(client) - close(client) - close(server) +proc next*(s: var TServer) = + ## proceed to the first/next request. + s.client = accept(s.socket) + headers(s.client, "") + var buf = "" + discard recvLine(s.client, buf) + var data = buf.split() + if cmpIgnoreCase(data[0], "GET") == 0: + var q = find(data[1], '?') + if q >= 0: + s.query = data[1].copy(q+1) + s.path = data[1].copy(0, q-1) + else: + s.query = "" + s.path = data[1] + else: + unimplemented(s.client) + +proc close*(s: TServer) = + close(s.socket) + +proc run*(handleRequest: proc (client: TSocket, path, query: string): bool, + port = TPort(0)) = + ## encapsulates the server object and main loop + var s: TServer + open(s, port) + echo("httpserver running on port ", s.port) + while true: + next(s) + if handleRequest(s.client, s.path, s.query): break + close(s.client) + close(s.socket) + +when false: + proc main = + var (server, port) = startup() + echo("httpserver running on port ", int16(port)) + + while true: + var client = accept(server) + if client == InvalidSocket: OSError() + acceptRequest(client) + close(client) + close(server) when isMainModule: - main() + var counter = 0 + proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} = + inc(counter) + client.send("Hallo, Andreas for the $#th time." % $counter & wwwNL) + return false # do not stop processing + + run(handleRequest) + |