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 | |
parent | a58a2f3823c33104992dc0e4129fa53e66a18f44 (diff) | |
download | Nim-a8303679960a13aa9ddd1f2fa44c2e4f9a12f071.tar.gz |
further development of httpserver
47 files changed, 102 insertions, 29 deletions
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 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) + diff --git a/lib/newwrap/cairo/cairo.nim b/lib/newwrap/cairo/cairo.nim index d391b4622..d391b4622 100644..100755 --- a/lib/newwrap/cairo/cairo.nim +++ b/lib/newwrap/cairo/cairo.nim diff --git a/lib/newwrap/cairo/cairoft.nim b/lib/newwrap/cairo/cairoft.nim index e80b9f82c..e80b9f82c 100644..100755 --- a/lib/newwrap/cairo/cairoft.nim +++ b/lib/newwrap/cairo/cairoft.nim diff --git a/lib/newwrap/cairo/cairowin32.nim b/lib/newwrap/cairo/cairowin32.nim index cee034902..cee034902 100644..100755 --- a/lib/newwrap/cairo/cairowin32.nim +++ b/lib/newwrap/cairo/cairowin32.nim diff --git a/lib/newwrap/cairo/cairoxlib.nim b/lib/newwrap/cairo/cairoxlib.nim index a889d7a9b..a889d7a9b 100644..100755 --- a/lib/newwrap/cairo/cairoxlib.nim +++ b/lib/newwrap/cairo/cairoxlib.nim diff --git a/lib/newwrap/gtk/atk.nim b/lib/newwrap/gtk/atk.nim index 9c0a1cb86..9c0a1cb86 100644..100755 --- a/lib/newwrap/gtk/atk.nim +++ b/lib/newwrap/gtk/atk.nim diff --git a/lib/newwrap/gtk/gdk2.nim b/lib/newwrap/gtk/gdk2.nim index c171068d3..c171068d3 100644..100755 --- a/lib/newwrap/gtk/gdk2.nim +++ b/lib/newwrap/gtk/gdk2.nim diff --git a/lib/newwrap/gtk/gdk2pixbuf.nim b/lib/newwrap/gtk/gdk2pixbuf.nim index f22ab345b..f22ab345b 100644..100755 --- a/lib/newwrap/gtk/gdk2pixbuf.nim +++ b/lib/newwrap/gtk/gdk2pixbuf.nim diff --git a/lib/newwrap/gtk/gdkglext.nim b/lib/newwrap/gtk/gdkglext.nim index 41ec0038f..41ec0038f 100644..100755 --- a/lib/newwrap/gtk/gdkglext.nim +++ b/lib/newwrap/gtk/gdkglext.nim diff --git a/lib/newwrap/gtk/glib2.nim b/lib/newwrap/gtk/glib2.nim index 0f1d45340..0f1d45340 100644..100755 --- a/lib/newwrap/gtk/glib2.nim +++ b/lib/newwrap/gtk/glib2.nim diff --git a/lib/newwrap/gtk/gtk2.nim b/lib/newwrap/gtk/gtk2.nim index 3c1eed666..3c1eed666 100644..100755 --- a/lib/newwrap/gtk/gtk2.nim +++ b/lib/newwrap/gtk/gtk2.nim diff --git a/lib/newwrap/gtk/gtkglext.nim b/lib/newwrap/gtk/gtkglext.nim index 779c78ee1..779c78ee1 100644..100755 --- a/lib/newwrap/gtk/gtkglext.nim +++ b/lib/newwrap/gtk/gtkglext.nim diff --git a/lib/newwrap/gtk/gtkhtml.nim b/lib/newwrap/gtk/gtkhtml.nim index a71a24836..a71a24836 100644..100755 --- a/lib/newwrap/gtk/gtkhtml.nim +++ b/lib/newwrap/gtk/gtkhtml.nim diff --git a/lib/newwrap/gtk/libglade2.nim b/lib/newwrap/gtk/libglade2.nim index 47e78e05e..47e78e05e 100644..100755 --- a/lib/newwrap/gtk/libglade2.nim +++ b/lib/newwrap/gtk/libglade2.nim diff --git a/lib/newwrap/gtk/pango.nim b/lib/newwrap/gtk/pango.nim index 1443aba1d..1443aba1d 100644..100755 --- a/lib/newwrap/gtk/pango.nim +++ b/lib/newwrap/gtk/pango.nim diff --git a/lib/newwrap/gtk/pangoutils.nim b/lib/newwrap/gtk/pangoutils.nim index 552362b9a..552362b9a 100644..100755 --- a/lib/newwrap/gtk/pangoutils.nim +++ b/lib/newwrap/gtk/pangoutils.nim diff --git a/lib/newwrap/libcurl.nim b/lib/newwrap/libcurl.nim index 6dc61f112..6dc61f112 100644..100755 --- a/lib/newwrap/libcurl.nim +++ b/lib/newwrap/libcurl.nim diff --git a/lib/newwrap/lua/lauxlib.nim b/lib/newwrap/lua/lauxlib.nim index 13a473687..13a473687 100644..100755 --- a/lib/newwrap/lua/lauxlib.nim +++ b/lib/newwrap/lua/lauxlib.nim diff --git a/lib/newwrap/lua/lua.nim b/lib/newwrap/lua/lua.nim index b702e381c..b702e381c 100644..100755 --- a/lib/newwrap/lua/lua.nim +++ b/lib/newwrap/lua/lua.nim diff --git a/lib/newwrap/lua/lualib.nim b/lib/newwrap/lua/lualib.nim index c2c6cd97b..c2c6cd97b 100644..100755 --- a/lib/newwrap/lua/lualib.nim +++ b/lib/newwrap/lua/lualib.nim diff --git a/lib/newwrap/mysql.nim b/lib/newwrap/mysql.nim index 5f6468faa..5f6468faa 100644..100755 --- a/lib/newwrap/mysql.nim +++ b/lib/newwrap/mysql.nim diff --git a/lib/newwrap/opengl/gl.nim b/lib/newwrap/opengl/gl.nim index 9387b5bc9..9387b5bc9 100644..100755 --- a/lib/newwrap/opengl/gl.nim +++ b/lib/newwrap/opengl/gl.nim diff --git a/lib/newwrap/opengl/glext.nim b/lib/newwrap/opengl/glext.nim index 32871df0e..32871df0e 100644..100755 --- a/lib/newwrap/opengl/glext.nim +++ b/lib/newwrap/opengl/glext.nim diff --git a/lib/newwrap/opengl/glu.nim b/lib/newwrap/opengl/glu.nim index 29edb0df2..29edb0df2 100644..100755 --- a/lib/newwrap/opengl/glu.nim +++ b/lib/newwrap/opengl/glu.nim diff --git a/lib/newwrap/opengl/glut.nim b/lib/newwrap/opengl/glut.nim index 43bee3382..43bee3382 100644..100755 --- a/lib/newwrap/opengl/glut.nim +++ b/lib/newwrap/opengl/glut.nim diff --git a/lib/newwrap/opengl/glx.nim b/lib/newwrap/opengl/glx.nim index 76c052d70..76c052d70 100644..100755 --- a/lib/newwrap/opengl/glx.nim +++ b/lib/newwrap/opengl/glx.nim diff --git a/lib/newwrap/opengl/wingl.nim b/lib/newwrap/opengl/wingl.nim index 7ed78f970..7ed78f970 100644..100755 --- a/lib/newwrap/opengl/wingl.nim +++ b/lib/newwrap/opengl/wingl.nim diff --git a/lib/newwrap/pcre/pcre.nim b/lib/newwrap/pcre/pcre.nim index c2220b4e5..c2220b4e5 100644..100755 --- a/lib/newwrap/pcre/pcre.nim +++ b/lib/newwrap/pcre/pcre.nim diff --git a/lib/newwrap/postgres.nim b/lib/newwrap/postgres.nim index cdb4a7283..cdb4a7283 100644..100755 --- a/lib/newwrap/postgres.nim +++ b/lib/newwrap/postgres.nim diff --git a/lib/newwrap/sdl/sdl.nim b/lib/newwrap/sdl/sdl.nim index 40f2fafff..40f2fafff 100644..100755 --- a/lib/newwrap/sdl/sdl.nim +++ b/lib/newwrap/sdl/sdl.nim diff --git a/lib/newwrap/sdl/sdl_gfx.nim b/lib/newwrap/sdl/sdl_gfx.nim index 39d4c3dc8..39d4c3dc8 100644..100755 --- a/lib/newwrap/sdl/sdl_gfx.nim +++ b/lib/newwrap/sdl/sdl_gfx.nim diff --git a/lib/newwrap/sdl/sdl_image.nim b/lib/newwrap/sdl/sdl_image.nim index 621749e32..621749e32 100644..100755 --- a/lib/newwrap/sdl/sdl_image.nim +++ b/lib/newwrap/sdl/sdl_image.nim diff --git a/lib/newwrap/sdl/sdl_mixer.nim b/lib/newwrap/sdl/sdl_mixer.nim index d4dd2e8df..d4dd2e8df 100644..100755 --- a/lib/newwrap/sdl/sdl_mixer.nim +++ b/lib/newwrap/sdl/sdl_mixer.nim diff --git a/lib/newwrap/sdl/sdl_mixer_nosmpeg.nim b/lib/newwrap/sdl/sdl_mixer_nosmpeg.nim index 571263014..571263014 100644..100755 --- a/lib/newwrap/sdl/sdl_mixer_nosmpeg.nim +++ b/lib/newwrap/sdl/sdl_mixer_nosmpeg.nim diff --git a/lib/newwrap/sdl/sdl_net.nim b/lib/newwrap/sdl/sdl_net.nim index 09318bc79..09318bc79 100644..100755 --- a/lib/newwrap/sdl/sdl_net.nim +++ b/lib/newwrap/sdl/sdl_net.nim diff --git a/lib/newwrap/sdl/sdl_ttf.nim b/lib/newwrap/sdl/sdl_ttf.nim index 410597317..410597317 100644..100755 --- a/lib/newwrap/sdl/sdl_ttf.nim +++ b/lib/newwrap/sdl/sdl_ttf.nim diff --git a/lib/newwrap/sdl/smpeg.nim b/lib/newwrap/sdl/smpeg.nim index 5c9562201..5c9562201 100644..100755 --- a/lib/newwrap/sdl/smpeg.nim +++ b/lib/newwrap/sdl/smpeg.nim diff --git a/lib/newwrap/sqlite3.nim b/lib/newwrap/sqlite3.nim index f2912f05f..f2912f05f 100644..100755 --- a/lib/newwrap/sqlite3.nim +++ b/lib/newwrap/sqlite3.nim diff --git a/lib/newwrap/tcl.nim b/lib/newwrap/tcl.nim index d62a54c2d..d62a54c2d 100644..100755 --- a/lib/newwrap/tcl.nim +++ b/lib/newwrap/tcl.nim diff --git a/lib/pure/sockets.nim b/lib/pure/sockets.nim index 0f9221b37..be2e4434a 100644..100755 --- 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 index d010457f8..d010457f8 100644..100755 --- a/llvm/llvm.nim +++ b/llvm/llvm.nim diff --git a/llvm/llvm_orig.nim b/llvm/llvm_orig.nim index 8e09f9c68..8e09f9c68 100644..100755 --- a/llvm/llvm_orig.nim +++ b/llvm/llvm_orig.nim diff --git a/rod/llvmtype.nim b/rod/llvmtype.nim index 7790855ac..7790855ac 100644..100755 --- a/rod/llvmtype.nim +++ b/rod/llvmtype.nim 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 index a64ac0395..a162d0f10 100644..100755 --- 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 |