mirror of
https://github.com/nim-lang/Nim.git
synced 2026-07-31 04:29:02 +00:00
further development of httpserver
This commit is contained in:
92
lib/devel/httpserver.nim
Normal file → Executable file
92
lib/devel/httpserver.nim
Normal file → Executable file
@@ -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 = ""
|
||||
|
||||
while true:
|
||||
var client = accept(server)
|
||||
if client == InvalidSocket: OSError()
|
||||
acceptRequest(client)
|
||||
close(client)
|
||||
close(server)
|
||||
proc port*(s: var TServer): TPort =
|
||||
## get the port number the server has aquired.
|
||||
result = s.port
|
||||
|
||||
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)
|
||||
|
||||
|
||||
0
lib/newwrap/cairo/cairo.nim
Normal file → Executable file
0
lib/newwrap/cairo/cairo.nim
Normal file → Executable file
0
lib/newwrap/cairo/cairoft.nim
Normal file → Executable file
0
lib/newwrap/cairo/cairoft.nim
Normal file → Executable file
0
lib/newwrap/cairo/cairowin32.nim
Normal file → Executable file
0
lib/newwrap/cairo/cairowin32.nim
Normal file → Executable file
0
lib/newwrap/cairo/cairoxlib.nim
Normal file → Executable file
0
lib/newwrap/cairo/cairoxlib.nim
Normal file → Executable file
0
lib/newwrap/gtk/atk.nim
Normal file → Executable file
0
lib/newwrap/gtk/atk.nim
Normal file → Executable file
0
lib/newwrap/gtk/gdk2.nim
Normal file → Executable file
0
lib/newwrap/gtk/gdk2.nim
Normal file → Executable file
0
lib/newwrap/gtk/gdk2pixbuf.nim
Normal file → Executable file
0
lib/newwrap/gtk/gdk2pixbuf.nim
Normal file → Executable file
0
lib/newwrap/gtk/gdkglext.nim
Normal file → Executable file
0
lib/newwrap/gtk/gdkglext.nim
Normal file → Executable file
0
lib/newwrap/gtk/glib2.nim
Normal file → Executable file
0
lib/newwrap/gtk/glib2.nim
Normal file → Executable file
0
lib/newwrap/gtk/gtk2.nim
Normal file → Executable file
0
lib/newwrap/gtk/gtk2.nim
Normal file → Executable file
0
lib/newwrap/gtk/gtkglext.nim
Normal file → Executable file
0
lib/newwrap/gtk/gtkglext.nim
Normal file → Executable file
0
lib/newwrap/gtk/gtkhtml.nim
Normal file → Executable file
0
lib/newwrap/gtk/gtkhtml.nim
Normal file → Executable file
0
lib/newwrap/gtk/libglade2.nim
Normal file → Executable file
0
lib/newwrap/gtk/libglade2.nim
Normal file → Executable file
0
lib/newwrap/gtk/pango.nim
Normal file → Executable file
0
lib/newwrap/gtk/pango.nim
Normal file → Executable file
0
lib/newwrap/gtk/pangoutils.nim
Normal file → Executable file
0
lib/newwrap/gtk/pangoutils.nim
Normal file → Executable file
0
lib/newwrap/libcurl.nim
Normal file → Executable file
0
lib/newwrap/libcurl.nim
Normal file → Executable file
0
lib/newwrap/lua/lauxlib.nim
Normal file → Executable file
0
lib/newwrap/lua/lauxlib.nim
Normal file → Executable file
0
lib/newwrap/lua/lua.nim
Normal file → Executable file
0
lib/newwrap/lua/lua.nim
Normal file → Executable file
0
lib/newwrap/lua/lualib.nim
Normal file → Executable file
0
lib/newwrap/lua/lualib.nim
Normal file → Executable file
0
lib/newwrap/mysql.nim
Normal file → Executable file
0
lib/newwrap/mysql.nim
Normal file → Executable file
0
lib/newwrap/opengl/gl.nim
Normal file → Executable file
0
lib/newwrap/opengl/gl.nim
Normal file → Executable file
0
lib/newwrap/opengl/glext.nim
Normal file → Executable file
0
lib/newwrap/opengl/glext.nim
Normal file → Executable file
0
lib/newwrap/opengl/glu.nim
Normal file → Executable file
0
lib/newwrap/opengl/glu.nim
Normal file → Executable file
0
lib/newwrap/opengl/glut.nim
Normal file → Executable file
0
lib/newwrap/opengl/glut.nim
Normal file → Executable file
0
lib/newwrap/opengl/glx.nim
Normal file → Executable file
0
lib/newwrap/opengl/glx.nim
Normal file → Executable file
0
lib/newwrap/opengl/wingl.nim
Normal file → Executable file
0
lib/newwrap/opengl/wingl.nim
Normal file → Executable file
0
lib/newwrap/pcre/pcre.nim
Normal file → Executable file
0
lib/newwrap/pcre/pcre.nim
Normal file → Executable file
0
lib/newwrap/postgres.nim
Normal file → Executable file
0
lib/newwrap/postgres.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_gfx.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_gfx.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_image.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_image.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_mixer.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_mixer.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_mixer_nosmpeg.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_mixer_nosmpeg.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_net.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_net.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_ttf.nim
Normal file → Executable file
0
lib/newwrap/sdl/sdl_ttf.nim
Normal file → Executable file
0
lib/newwrap/sdl/smpeg.nim
Normal file → Executable file
0
lib/newwrap/sdl/smpeg.nim
Normal file → Executable file
0
lib/newwrap/sqlite3.nim
Normal file → Executable file
0
lib/newwrap/sqlite3.nim
Normal file → Executable file
0
lib/newwrap/tcl.nim
Normal file → Executable file
0
lib/newwrap/tcl.nim
Normal file → Executable file
2
lib/pure/sockets.nim
Normal file → Executable file
2
lib/pure/sockets.nim
Normal file → Executable file
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user