further development of httpserver

This commit is contained in:
Andreas Rumpf 2010-01-05 00:34:15 +01:00
commit a830367996
47 changed files with 102 additions and 29 deletions

17
examples/allany.nim Executable file
View file

@ -0,0 +1,17 @@
# All and any
template all(container, cond: expr): expr =
block:
var result = true
for item in items(container):
if not cond(item):
result = false
break
result
if all("mystring", {'a'..'z'}.contains):
echo "works"
else:
echo "does not work"

92
lib/devel/httpserver.nim Normal file → Executable file
View file

@ -134,14 +134,6 @@ proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) =
# --------------- Server Setup ----------------------------------------------- # --------------- 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) = proc acceptRequest(client: TSocket) =
var cgi = false var cgi = false
var query = "" var query = ""
@ -181,16 +173,80 @@ proc acceptRequest(client: TSocket) =
else: else:
executeCgi(client, path, query, meth) executeCgi(client, path, query, meth)
proc main = type
var (server, port) = startup() TServer* = object
echo("httpserver running on port ", int16(port)) 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 = ""
while true: proc port*(s: var TServer): TPort =
var client = accept(server) ## get the port number the server has aquired.
if client == InvalidSocket: OSError() result = s.port
acceptRequest(client)
close(client) proc next*(s: var TServer) =
close(server) ## 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: 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)

0
lib/newwrap/cairo/cairo.nim Normal file → Executable file
View file

0
lib/newwrap/cairo/cairoft.nim Normal file → Executable file
View file

0
lib/newwrap/cairo/cairowin32.nim Normal file → Executable file
View file

0
lib/newwrap/cairo/cairoxlib.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/atk.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/gdk2.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/gdk2pixbuf.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/gdkglext.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/glib2.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/gtk2.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/gtkglext.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/gtkhtml.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/libglade2.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/pango.nim Normal file → Executable file
View file

0
lib/newwrap/gtk/pangoutils.nim Normal file → Executable file
View file

0
lib/newwrap/libcurl.nim Normal file → Executable file
View file

0
lib/newwrap/lua/lauxlib.nim Normal file → Executable file
View file

0
lib/newwrap/lua/lua.nim Normal file → Executable file
View file

0
lib/newwrap/lua/lualib.nim Normal file → Executable file
View file

0
lib/newwrap/mysql.nim Normal file → Executable file
View file

0
lib/newwrap/opengl/gl.nim Normal file → Executable file
View file

0
lib/newwrap/opengl/glext.nim Normal file → Executable file
View file

0
lib/newwrap/opengl/glu.nim Normal file → Executable file
View file

0
lib/newwrap/opengl/glut.nim Normal file → Executable file
View file

0
lib/newwrap/opengl/glx.nim Normal file → Executable file
View file

0
lib/newwrap/opengl/wingl.nim Normal file → Executable file
View file

0
lib/newwrap/pcre/pcre.nim Normal file → Executable file
View file

0
lib/newwrap/postgres.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/sdl.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/sdl_gfx.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/sdl_image.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/sdl_mixer.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/sdl_mixer_nosmpeg.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/sdl_net.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/sdl_ttf.nim Normal file → Executable file
View file

0
lib/newwrap/sdl/smpeg.nim Normal file → Executable file
View file

0
lib/newwrap/sqlite3.nim Normal file → Executable file
View file

0
lib/newwrap/tcl.nim Normal file → Executable file
View file

2
lib/pure/sockets.nim Normal file → Executable file
View file

@ -66,6 +66,8 @@ const
proc `==`*(a, b: TSocket): bool {.borrow.} proc `==`*(a, b: TSocket): bool {.borrow.}
proc `==`*(a, b: TPort): bool {.borrow.} proc `==`*(a, b: TPort): bool {.borrow.}
proc `$`* (p: TPort): string = result = $ze(int16(p))
proc ToInt(domain: TDomain): cint = proc ToInt(domain: TDomain): cint =
case domain case domain
of AF_UNIX: result = posix.AF_UNIX of AF_UNIX: result = posix.AF_UNIX

0
llvm/llvm.nim Normal file → Executable file
View file

0
llvm/llvm_orig.nim Normal file → Executable file
View file

0
rod/llvmtype.nim Normal file → Executable file
View file

View file

@ -408,7 +408,6 @@ proc handleDecChars(L: var TLexer, xi: var int) =
inc(L.bufpos) inc(L.bufpos)
proc getEscapedChar(L: var TLexer, tok: var TToken) = proc getEscapedChar(L: var TLexer, tok: var TToken) =
var xi: int
inc(L.bufpos) # skip '\' inc(L.bufpos) # skip '\'
case L.buf[L.bufpos] case L.buf[L.bufpos]
of 'n', 'N': of 'n', 'N':
@ -447,14 +446,14 @@ proc getEscapedChar(L: var TLexer, tok: var TToken) =
Inc(L.bufpos) Inc(L.bufpos)
of 'x', 'X': of 'x', 'X':
inc(L.bufpos) inc(L.bufpos)
xi = 0 var xi = 0
handleHexChar(L, xi) handleHexChar(L, xi)
handleHexChar(L, xi) handleHexChar(L, xi)
add(tok.literal, Chr(xi)) add(tok.literal, Chr(xi))
of '0'..'9': of '0'..'9':
if matchTwoChars(L, '0', {'0'..'9'}): if matchTwoChars(L, '0', {'0'..'9'}):
lexMessage(L, warnOctalEscape) lexMessage(L, warnOctalEscape)
xi = 0 var xi = 0
handleDecChars(L, xi) handleDecChars(L, xi)
if (xi <= 255): add(tok.literal, Chr(xi)) if (xi <= 255): add(tok.literal, Chr(xi))
else: lexMessage(L, errInvalidCharacterConstant) else: lexMessage(L, errInvalidCharacterConstant)
@ -505,8 +504,7 @@ proc getString(L: var TLexer, tok: var TToken, rawMode: bool) =
else: else:
add(tok.literal, buf[pos]) add(tok.literal, buf[pos])
Inc(pos) Inc(pos)
L.bufpos = pos + L.bufpos = pos + 3 # skip the three """
3 # skip the three """
else: else:
# ordinary string literal # ordinary string literal
if rawMode: tok.tokType = tkRStrLit if rawMode: tok.tokType = tkRStrLit
@ -787,4 +785,4 @@ proc rawGetTok(L: var TLexer, tok: var TToken) =
lexMessage(L, errInvalidToken, c & " (\\" & $(ord(c)) & ')') lexMessage(L, errInvalidToken, c & " (\\" & $(ord(c)) & ')')
Inc(L.bufpos) Inc(L.bufpos)
dummyIdent = getIdent("") dummyIdent = getIdent("")

View file

@ -6,5 +6,5 @@ proc main: int =
finally: finally:
echo "came here" echo "came here"
echo main() discard main() #OUT came here

8
tests/tmatrix.nim Normal file → Executable file
View file

@ -21,18 +21,18 @@ proc `[,]`*(m: TMatrix, x, y: int): float {.inline.} =
proc `[,]=`*(m: var TMatrix, x, y: int, val: float) {.inline.} = proc `[,]=`*(m: var TMatrix, x, y: int, val: float) {.inline.} =
m.data[x|y] = val m.data[x|y] = val
proc `[$..$, $..$]`*(m: TMatrix, a, b, c, d: int): TMatrix = proc `[$ .. $, $ .. $]`*(m: TMatrix, a, b, c, d: int): TMatrix =
result = createMatrix(b-a+1, d-c+1) result = createMatrix(b-a+1, d-c+1)
for x in a..b: for x in a..b:
for y in c..d: for y in c..d:
result[x-a, y-c] = m[x, y] result[x-a, y-c] = m[x, y]
proc `[$..$, $..$]=`*(m: var TMatrix, a, b, c, d: int, val: float) = proc `[$ .. $, $ .. $]=`*(m: var TMatrix, a, b, c, d: int, val: float) =
for x in a..b: for x in a..b:
for y in c..d: for y in c..d:
m[x, y] = val m[x, y] = val
proc `[$..$, $..$]=`*(m: var TMatrix, a, b, c, d: int, val: TMatrix) = proc `[$ .. $, $ .. $]=`*(m: var TMatrix, a, b, c, d: int, val: TMatrix) =
assert val.width == b-a+1 assert val.width == b-a+1
assert val.height == d-c+1 assert val.height == d-c+1
for x in a..b: for x in a..b:
@ -57,4 +57,4 @@ for i in 0..w-1:
m[i, i] = 1.0 m[i, i] = 1.0
for i in 0..w-1: for i in 0..w-1:
stdout.write(m[i,i]) #OUT 1.01.01.0 stdout.write(m[i,i]) #OUT 111