From a8303679960a13aa9ddd1f2fa44c2e4f9a12f071 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Tue, 5 Jan 2010 00:34:15 +0100 Subject: further development of httpserver --- examples/allany.nim | 17 +++++++ lib/devel/httpserver.nim | 92 ++++++++++++++++++++++++++++------- lib/newwrap/cairo/cairo.nim | 0 lib/newwrap/cairo/cairoft.nim | 0 lib/newwrap/cairo/cairowin32.nim | 0 lib/newwrap/cairo/cairoxlib.nim | 0 lib/newwrap/gtk/atk.nim | 0 lib/newwrap/gtk/gdk2.nim | 0 lib/newwrap/gtk/gdk2pixbuf.nim | 0 lib/newwrap/gtk/gdkglext.nim | 0 lib/newwrap/gtk/glib2.nim | 0 lib/newwrap/gtk/gtk2.nim | 0 lib/newwrap/gtk/gtkglext.nim | 0 lib/newwrap/gtk/gtkhtml.nim | 0 lib/newwrap/gtk/libglade2.nim | 0 lib/newwrap/gtk/pango.nim | 0 lib/newwrap/gtk/pangoutils.nim | 0 lib/newwrap/libcurl.nim | 0 lib/newwrap/lua/lauxlib.nim | 0 lib/newwrap/lua/lua.nim | 0 lib/newwrap/lua/lualib.nim | 0 lib/newwrap/mysql.nim | 0 lib/newwrap/opengl/gl.nim | 0 lib/newwrap/opengl/glext.nim | 0 lib/newwrap/opengl/glu.nim | 0 lib/newwrap/opengl/glut.nim | 0 lib/newwrap/opengl/glx.nim | 0 lib/newwrap/opengl/wingl.nim | 0 lib/newwrap/pcre/pcre.nim | 0 lib/newwrap/postgres.nim | 0 lib/newwrap/sdl/sdl.nim | 0 lib/newwrap/sdl/sdl_gfx.nim | 0 lib/newwrap/sdl/sdl_image.nim | 0 lib/newwrap/sdl/sdl_mixer.nim | 0 lib/newwrap/sdl/sdl_mixer_nosmpeg.nim | 0 lib/newwrap/sdl/sdl_net.nim | 0 lib/newwrap/sdl/sdl_ttf.nim | 0 lib/newwrap/sdl/smpeg.nim | 0 lib/newwrap/sqlite3.nim | 0 lib/newwrap/tcl.nim | 0 lib/pure/sockets.nim | 2 + llvm/llvm.nim | 0 llvm/llvm_orig.nim | 0 rod/llvmtype.nim | 0 rod/scanner.nim | 10 ++-- tests/tfinally.nim | 2 +- tests/tmatrix.nim | 8 +-- 47 files changed, 102 insertions(+), 29 deletions(-) create mode 100755 examples/allany.nim mode change 100644 => 100755 lib/devel/httpserver.nim mode change 100644 => 100755 lib/newwrap/cairo/cairo.nim mode change 100644 => 100755 lib/newwrap/cairo/cairoft.nim mode change 100644 => 100755 lib/newwrap/cairo/cairowin32.nim mode change 100644 => 100755 lib/newwrap/cairo/cairoxlib.nim mode change 100644 => 100755 lib/newwrap/gtk/atk.nim mode change 100644 => 100755 lib/newwrap/gtk/gdk2.nim mode change 100644 => 100755 lib/newwrap/gtk/gdk2pixbuf.nim mode change 100644 => 100755 lib/newwrap/gtk/gdkglext.nim mode change 100644 => 100755 lib/newwrap/gtk/glib2.nim mode change 100644 => 100755 lib/newwrap/gtk/gtk2.nim mode change 100644 => 100755 lib/newwrap/gtk/gtkglext.nim mode change 100644 => 100755 lib/newwrap/gtk/gtkhtml.nim mode change 100644 => 100755 lib/newwrap/gtk/libglade2.nim mode change 100644 => 100755 lib/newwrap/gtk/pango.nim mode change 100644 => 100755 lib/newwrap/gtk/pangoutils.nim mode change 100644 => 100755 lib/newwrap/libcurl.nim mode change 100644 => 100755 lib/newwrap/lua/lauxlib.nim mode change 100644 => 100755 lib/newwrap/lua/lua.nim mode change 100644 => 100755 lib/newwrap/lua/lualib.nim mode change 100644 => 100755 lib/newwrap/mysql.nim mode change 100644 => 100755 lib/newwrap/opengl/gl.nim mode change 100644 => 100755 lib/newwrap/opengl/glext.nim mode change 100644 => 100755 lib/newwrap/opengl/glu.nim mode change 100644 => 100755 lib/newwrap/opengl/glut.nim mode change 100644 => 100755 lib/newwrap/opengl/glx.nim mode change 100644 => 100755 lib/newwrap/opengl/wingl.nim mode change 100644 => 100755 lib/newwrap/pcre/pcre.nim mode change 100644 => 100755 lib/newwrap/postgres.nim mode change 100644 => 100755 lib/newwrap/sdl/sdl.nim mode change 100644 => 100755 lib/newwrap/sdl/sdl_gfx.nim mode change 100644 => 100755 lib/newwrap/sdl/sdl_image.nim mode change 100644 => 100755 lib/newwrap/sdl/sdl_mixer.nim mode change 100644 => 100755 lib/newwrap/sdl/sdl_mixer_nosmpeg.nim mode change 100644 => 100755 lib/newwrap/sdl/sdl_net.nim mode change 100644 => 100755 lib/newwrap/sdl/sdl_ttf.nim mode change 100644 => 100755 lib/newwrap/sdl/smpeg.nim mode change 100644 => 100755 lib/newwrap/sqlite3.nim mode change 100644 => 100755 lib/newwrap/tcl.nim mode change 100644 => 100755 lib/pure/sockets.nim mode change 100644 => 100755 llvm/llvm.nim mode change 100644 => 100755 llvm/llvm_orig.nim mode change 100644 => 100755 rod/llvmtype.nim mode change 100644 => 100755 tests/tmatrix.nim diff --git a/examples/allany.nim b/examples/allany.nim new file mode 100755 index 000000000..4747ce0d6 --- /dev/null +++ b/examples/allany.nim @@ -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" + + diff --git a/lib/devel/httpserver.nim b/lib/devel/httpserver.nim old mode 100644 new mode 100755 index ca1e315f0..285dadf0a --- 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) + diff --git a/lib/newwrap/cairo/cairo.nim b/lib/newwrap/cairo/cairo.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/cairo/cairoft.nim b/lib/newwrap/cairo/cairoft.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/cairo/cairowin32.nim b/lib/newwrap/cairo/cairowin32.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/cairo/cairoxlib.nim b/lib/newwrap/cairo/cairoxlib.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/atk.nim b/lib/newwrap/gtk/atk.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/gdk2.nim b/lib/newwrap/gtk/gdk2.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/gdk2pixbuf.nim b/lib/newwrap/gtk/gdk2pixbuf.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/gdkglext.nim b/lib/newwrap/gtk/gdkglext.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/glib2.nim b/lib/newwrap/gtk/glib2.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/gtk2.nim b/lib/newwrap/gtk/gtk2.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/gtkglext.nim b/lib/newwrap/gtk/gtkglext.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/gtkhtml.nim b/lib/newwrap/gtk/gtkhtml.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/libglade2.nim b/lib/newwrap/gtk/libglade2.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/pango.nim b/lib/newwrap/gtk/pango.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/gtk/pangoutils.nim b/lib/newwrap/gtk/pangoutils.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/libcurl.nim b/lib/newwrap/libcurl.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/lua/lauxlib.nim b/lib/newwrap/lua/lauxlib.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/lua/lua.nim b/lib/newwrap/lua/lua.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/lua/lualib.nim b/lib/newwrap/lua/lualib.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/mysql.nim b/lib/newwrap/mysql.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/opengl/gl.nim b/lib/newwrap/opengl/gl.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/opengl/glext.nim b/lib/newwrap/opengl/glext.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/opengl/glu.nim b/lib/newwrap/opengl/glu.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/opengl/glut.nim b/lib/newwrap/opengl/glut.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/opengl/glx.nim b/lib/newwrap/opengl/glx.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/opengl/wingl.nim b/lib/newwrap/opengl/wingl.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/pcre/pcre.nim b/lib/newwrap/pcre/pcre.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/postgres.nim b/lib/newwrap/postgres.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/sdl.nim b/lib/newwrap/sdl/sdl.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/sdl_gfx.nim b/lib/newwrap/sdl/sdl_gfx.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/sdl_image.nim b/lib/newwrap/sdl/sdl_image.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/sdl_mixer.nim b/lib/newwrap/sdl/sdl_mixer.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/sdl_mixer_nosmpeg.nim b/lib/newwrap/sdl/sdl_mixer_nosmpeg.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/sdl_net.nim b/lib/newwrap/sdl/sdl_net.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/sdl_ttf.nim b/lib/newwrap/sdl/sdl_ttf.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sdl/smpeg.nim b/lib/newwrap/sdl/smpeg.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/sqlite3.nim b/lib/newwrap/sqlite3.nim old mode 100644 new mode 100755 diff --git a/lib/newwrap/tcl.nim b/lib/newwrap/tcl.nim old mode 100644 new mode 100755 diff --git a/lib/pure/sockets.nim b/lib/pure/sockets.nim old mode 100644 new mode 100755 index 0f9221b37..be2e4434a --- a/lib/pure/sockets.nim +++ b/lib/pure/sockets.nim @@ -66,6 +66,8 @@ const proc `==`*(a, b: TSocket): bool {.borrow.} proc `==`*(a, b: TPort): bool {.borrow.} +proc `$`* (p: TPort): string = result = $ze(int16(p)) + proc ToInt(domain: TDomain): cint = case domain of AF_UNIX: result = posix.AF_UNIX diff --git a/llvm/llvm.nim b/llvm/llvm.nim old mode 100644 new mode 100755 diff --git a/llvm/llvm_orig.nim b/llvm/llvm_orig.nim old mode 100644 new mode 100755 diff --git a/rod/llvmtype.nim b/rod/llvmtype.nim old mode 100644 new mode 100755 diff --git a/rod/scanner.nim b/rod/scanner.nim index 2036a4d4d..9382c7621 100755 --- a/rod/scanner.nim +++ b/rod/scanner.nim @@ -408,7 +408,6 @@ proc handleDecChars(L: var TLexer, xi: var int) = inc(L.bufpos) proc getEscapedChar(L: var TLexer, tok: var TToken) = - var xi: int inc(L.bufpos) # skip '\' case L.buf[L.bufpos] of 'n', 'N': @@ -447,14 +446,14 @@ proc getEscapedChar(L: var TLexer, tok: var TToken) = Inc(L.bufpos) of 'x', 'X': inc(L.bufpos) - xi = 0 + var xi = 0 handleHexChar(L, xi) handleHexChar(L, xi) add(tok.literal, Chr(xi)) of '0'..'9': if matchTwoChars(L, '0', {'0'..'9'}): lexMessage(L, warnOctalEscape) - xi = 0 + var xi = 0 handleDecChars(L, xi) if (xi <= 255): add(tok.literal, Chr(xi)) else: lexMessage(L, errInvalidCharacterConstant) @@ -505,8 +504,7 @@ proc getString(L: var TLexer, tok: var TToken, rawMode: bool) = else: add(tok.literal, buf[pos]) Inc(pos) - L.bufpos = pos + - 3 # skip the three """ + L.bufpos = pos + 3 # skip the three """ else: # ordinary string literal if rawMode: tok.tokType = tkRStrLit @@ -787,4 +785,4 @@ proc rawGetTok(L: var TLexer, tok: var TToken) = lexMessage(L, errInvalidToken, c & " (\\" & $(ord(c)) & ')') Inc(L.bufpos) -dummyIdent = getIdent("") \ No newline at end of file +dummyIdent = getIdent("") diff --git a/tests/tfinally.nim b/tests/tfinally.nim index 102444e82..df6397125 100755 --- a/tests/tfinally.nim +++ b/tests/tfinally.nim @@ -6,5 +6,5 @@ proc main: int = finally: echo "came here" -echo main() +discard main() #OUT came here diff --git a/tests/tmatrix.nim b/tests/tmatrix.nim old mode 100644 new mode 100755 index a64ac0395..a162d0f10 --- a/tests/tmatrix.nim +++ b/tests/tmatrix.nim @@ -21,18 +21,18 @@ proc `[,]`*(m: TMatrix, x, y: int): float {.inline.} = proc `[,]=`*(m: var TMatrix, x, y: int, val: float) {.inline.} = 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) for x in a..b: for y in c..d: 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 y in c..d: 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.height == d-c+1 for x in a..b: @@ -57,4 +57,4 @@ for i in 0..w-1: m[i, i] = 1.0 for i in 0..w-1: - stdout.write(m[i,i]) #OUT 1.01.01.0 + stdout.write(m[i,i]) #OUT 111 -- cgit 1.4.1-2-gfad0