httpserver changes
This commit is contained in:
parent
7b433a3de3
commit
faac1bee85
1 changed files with 16 additions and 13 deletions
|
|
@ -234,12 +234,9 @@ proc next*(s: var TServer) =
|
||||||
var data = recv(s.client)
|
var data = recv(s.client)
|
||||||
#discard recvLine(s.client, data)
|
#discard recvLine(s.client, data)
|
||||||
|
|
||||||
var i = 0
|
var i = skipWhitespace(data)
|
||||||
if skipIgnoreCase(data, "GET") > 0: i = 3
|
if skipIgnoreCase(data, "GET") > 0: inc(i, 3)
|
||||||
elif skipIgnoreCase(data, "POST") > 0: i = 4
|
elif skipIgnoreCase(data, "POST") > 0: inc(i, 4)
|
||||||
elif data.len == 0:
|
|
||||||
# Google Chrome sends an empty line first? the web is ugly ...
|
|
||||||
nil
|
|
||||||
else:
|
else:
|
||||||
unimplemented(s.client)
|
unimplemented(s.client)
|
||||||
return
|
return
|
||||||
|
|
@ -268,7 +265,7 @@ proc run*(handleRequest: proc (client: TSocket, path, query: string): bool,
|
||||||
## encapsulates the server object and main loop
|
## encapsulates the server object and main loop
|
||||||
var s: TServer
|
var s: TServer
|
||||||
open(s, port)
|
open(s, port)
|
||||||
echo("httpserver running on port ", s.port)
|
#echo("httpserver running on port ", s.port)
|
||||||
while true:
|
while true:
|
||||||
next(s)
|
next(s)
|
||||||
if handleRequest(s.client, s.path, s.query): break
|
if handleRequest(s.client, s.path, s.query): break
|
||||||
|
|
@ -277,11 +274,17 @@ proc run*(handleRequest: proc (client: TSocket, path, query: string): bool,
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var counter = 0
|
var counter = 0
|
||||||
proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
|
|
||||||
|
var s: TServer
|
||||||
|
open(s, TPort(0))
|
||||||
|
echo("httpserver running on port ", s.port)
|
||||||
|
while true:
|
||||||
|
next(s)
|
||||||
|
|
||||||
inc(counter)
|
inc(counter)
|
||||||
client.send("Hello, Andreas, for the $#th time. $# ? $#" % [
|
s.client.send("Hello, Andreas, for the $#th time. $# ? $#" % [
|
||||||
$counter, path, query] & wwwNL)
|
$counter, s.path, s.query] & wwwNL)
|
||||||
return false # do not stop processing
|
|
||||||
|
|
||||||
run(handleRequest, TPort(0))
|
close(s.client)
|
||||||
|
close(s)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue