httpserver supports a fixed port number

This commit is contained in:
Andreas Rumpf 2010-02-06 12:24:54 +01:00
commit fd522aa406
9 changed files with 278 additions and 201 deletions

259
lib/pure/httpserver.nim Normal file
View file

@ -0,0 +1,259 @@
#
#
# Nimrod's Runtime Library
# (c) Copyright 2010 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
## This module implements a simple HTTP-Server.
##
## Example:
##
## .. code-block:: nimrod
## import strutils, sockets, httpserver
##
## var counter = 0
## proc handleRequest(client: TSocket, path, query: string): bool {.procvar.} =
## inc(counter)
## client.send("Hallo for the $#th time." % $counter & wwwNL)
## return false # do not stop processing
##
## run(handleRequest, TPort(80))
##
import strutils, os, osproc, strtabs, streams, sockets
const
wwwNL* = "\r\L"
ServerSig = "Server: httpserver.nim/1.0.0" & wwwNL
# --------------- output messages --------------------------------------------
proc sendTextContentType(client: TSocket) =
send(client, "Content-type: text/html" & wwwNL)
send(client, wwwNL)
proc badRequest(client: TSocket) =
# Inform the client that a request it has made has a problem.
send(client, "HTTP/1.0 400 BAD REQUEST" & wwwNL)
sendTextContentType(client)
send(client, "<p>Your browser sent a bad request, " &
"such as a POST without a Content-Length." & wwwNL)
proc cannotExec(client: TSocket) =
send(client, "HTTP/1.0 500 Internal Server Error" & wwwNL)
sendTextContentType(client)
send(client, "<P>Error prohibited CGI execution." & wwwNL)
proc headers(client: TSocket, filename: string) =
# XXX could use filename to determine file type
send(client, "HTTP/1.0 200 OK" & wwwNL)
send(client, ServerSig)
sendTextContentType(client)
proc notFound(client: TSocket) =
send(client, "HTTP/1.0 404 NOT FOUND" & wwwNL)
send(client, ServerSig)
sendTextContentType(client)
send(client, "<html><title>Not Found</title>" & wwwNL)
send(client, "<body><p>The server could not fulfill" & wwwNL)
send(client, "your request because the resource specified" & wwwNL)
send(client, "is unavailable or nonexistent." & wwwNL)
send(client, "</body></html>" & wwwNL)
proc unimplemented(client: TSocket) =
send(client, "HTTP/1.0 501 Method Not Implemented" & wwwNL)
send(client, ServerSig)
sendTextContentType(client)
send(client, "<html><head><title>Method Not Implemented" &
"</title></head>" &
"<body><p>HTTP request method not supported." &
"</body></HTML>" & wwwNL)
# ----------------- file serving ---------------------------------------------
proc discardHeaders(client: TSocket) = skip(client)
proc serveFile(client: TSocket, filename: string) =
discardHeaders(client)
var f: TFile
if open(f, filename):
headers(client, filename)
const bufSize = 8000 # != 8K might be good for memory manager
var buf = alloc(bufsize)
while True:
var bytesread = readBuffer(f, buf, bufsize)
if bytesread > 0:
var byteswritten = send(client, buf, bytesread)
if bytesread != bytesWritten:
dealloc(buf)
close(f)
OSError()
if bytesread != bufSize: break
dealloc(buf)
close(f)
else:
notFound(client)
# ------------------ CGI execution -------------------------------------------
type
TRequestMethod = enum reqGet, reqPost
proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) =
var env = newStringTable(modeCaseInsensitive)
var contentLength = -1
case meth
of reqGet:
discardHeaders(client)
env["REQUEST_METHOD"] = "GET"
env["QUERY_STRING"] = query
of reqPost:
var buf = ""
var dataAvail = false
while dataAvail:
dataAvail = recvLine(client, buf)
var L = toLower(buf)
if L.startsWith("content-length:"):
var i = len("content-length:")
while L[i] in Whitespace: inc(i)
contentLength = parseInt(copy(L, i))
if contentLength < 0:
badRequest(client)
return
env["REQUEST_METHOD"] = "POST"
env["CONTENT_LENGTH"] = $contentLength
send(client, "HTTP/1.0 200 OK" & wwwNL)
var process = startProcess(command=path, env=env)
if meth == reqPost:
# get from client and post to CGI program:
var buf = alloc(contentLength)
if recv(client, buf, contentLength) != contentLength: OSError()
var inp = process.inputStream
inp.writeData(inp, buf, contentLength)
var outp = process.outputStream
while running(process) or not outp.atEnd(outp):
var line = outp.readLine()
send(client, line)
send(client, wwwNL)
# --------------- Server Setup -----------------------------------------------
proc acceptRequest(client: TSocket) =
var cgi = false
var query = ""
var buf = ""
discard recvLine(client, buf)
var data = buf.split()
var meth = reqGet
if cmpIgnoreCase(data[0], "GET") == 0:
var q = find(data[1], '?')
if q >= 0:
cgi = true
query = data[1].copy(q+1)
elif cmpIgnoreCase(data[0], "POST") == 0:
cgi = true
meth = reqPost
else:
unimplemented(client)
var path = data[1]
if path[path.len-1] == '/' or existsDir(path):
path = path / "index.html"
if not ExistsFile(path):
discardHeaders(client)
notFound(client)
else:
when defined(Windows):
var ext = splitFile(path).ext.toLower
if ext == ".exe" or ext == ".cgi":
# XXX: extract interpreter information here?
cgi = true
else:
if {fpUserExec, fpGroupExec, fpOthersExec} * path.getFilePermissions != {}:
cgi = true
if not cgi:
serveFile(client, path)
else:
executeCgi(client, path, query, meth)
type
TServer* = object ## contains the current server state
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(80)) =
## 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, port)
listen(s.socket)
if port == TPort(0):
s.port = getSockName(s.socket)
else:
s.port = port
s.client = InvalidSocket
s.path = ""
s.query = ""
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) =
## closes the server (and the socket the server uses).
close(s.socket)
proc run*(handleRequest: proc (client: TSocket, path, query: string): bool,
port = TPort(80)) =
## 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)
when isMainModule:
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, TPort(80))

View file

@ -1,7 +1,7 @@
#
#
# Nimrod's Runtime Library
# (c) Copyright 2009 Andreas Rumpf
# (c) Copyright 2010 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.

View file

@ -9,28 +9,171 @@
## Regular expression support for Nimrod. Consider using the pegs module
## instead.
## Currently this module is implemented by providing a wrapper around the
## `PRCE (Perl-Compatible Regular Expressions) <http://www.pcre.org>`_
## C library. This means that your application will depend on the PRCE
## library's licence when using this module, which should not be a problem
## though.
## PRCE's licence follows:
##
## .. include:: ../doc/regexprs.txt
##
{.compile: "tre/tre_all.c".}
from strutils import addf
type
TRegExDesc {.pure, final.} = object
re_nsub: int # Number of parenthesized subexpressions.
value: pointer # For internal use only.
TRegEx* = ref TRegExDesc ## a compiled regular expression
EInvalidRegEx* = object of EInvalidValue
## is raised if the pattern is no valid regular expression.
TRegMatch {.pure.} = object
so, eo: cint
import
pcre, strutils
const
MaxSubpatterns* = 10
## defines the maximum number of subpatterns that can be captured.
## More subpatterns cannot be captured!
type
TRegExOptions* = enum ## options for regular expressions
reIgnoreCase = 0, ## do caseless matching
reMultiLine = 1, ## ``^`` and ``$`` match newlines within data
reDotAll = 2, ## ``.`` matches anything including NL
reExtended = 3, ## ignore whitespace and ``#`` comments
PCRE_ANCHORED* = 0x00000010
PCRE_DOLLAR_ENDONLY* = 0x00000020
PCRE_EXTRA* = 0x00000040
PCRE_NOTBOL* = 0x00000080
PCRE_NOTEOL* = 0x00000100
PCRE_UNGREEDY* = 0x00000200
PCRE_NOTEMPTY* = 0x00000400
PCRE_UTF8* = 0x00000800
PCRE_NO_AUTO_CAPTURE* = 0x00001000
TRegExDesc {.pure, final.} = object
h: PPcre
TRegEx* = ref TRegExDesc ## a compiled regular expression
EInvalidRegEx* = object of EInvalidValue
## is raised if the pattern is no valid regular expression.
proc rawCompile(pattern: string, flags: cint): PPcre =
var
msg: CString
offset: int
com = pcreCompile(pattern, flags, addr(msg), addr(offset), nil)
if com == nil:
var e: ref EInvalidRegEx
new(e)
e.msg = $msg & "\n" & pattern & "\n" & repeatChar(offset) & "^\n"
raise e
return com
proc finalizeRegEx(x: TRegEx) = dealloc(x.h)
proc re*(s: string): TRegEx =
## Constructor of regular expressions. Note that Nimrod's
## extended raw string literals supports this syntax ``re"[abc]"`` as
## a short form for ``re(r"[abc]")``.
new(result, finalizeRegEx)
result.h = rawCompile(s,
var err = int(regncomp(addr(result^), s, s.len,
cint(REG_EXTENDED or REG_NEWLINE)))
if err != 0:
var e: ref EInvalidRegEx
new(e)
e.msg = ErrorMessages[err]
raise e
proc xre*(pattern: string): TRegEx =
## deletes whitespace from a pattern that is not escaped or in a character
## class. Then it constructs a regular expresion object via `re`.
## This is modelled after Perl's ``/x`` modifier.
var p = ""
var i = 0
while i < pattern.len:
case pattern[i]
of ' ', '\t':
inc i
of '\\':
add p, '\\'
add p, pattern[i+1]
inc i, 2
of '[':
while pattern[i] != ']' and pattern[i] != '\0':
add p, pattern[i]
inc i
else:
add p, pattern[i]
inc i
result = re(p)
proc matchOrFind(s: string, pattern: PPcre, matches: var openarray[string],
start: cint): cint =
var
rawMatches: array [0..maxSubpatterns * 3 - 1, cint]
res = int(pcreExec(pattern, nil, s, len(s), start, 0,
cast[ptr cint](addr(rawMatches)), maxSubpatterns * 3))
dealloc(pattern)
if res < 0: return res
for i in 0..res-1:
var
a = rawMatches[i * 2]
b = rawMatches[i * 2 + 1]
if a >= 0'i32: matches[i] = copy(s, a, int(b)-1)
else: matches[i] = ""
return res
proc matchOrFind(s: string, pattern: PPcre, start: cint): cint =
var
rawMatches: array [0..maxSubpatterns * 3 - 1, cint]
res = pcreExec(pattern, nil, s, len(s), start, 0,
cast[ptr cint](addr(rawMatches)), maxSubpatterns * 3)
dealloc(pattern)
return res
proc match(s, pattern: string, matches: var openarray[string],
start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_ANCHORED),
matches, start) >= 0'i32
proc matchLen(s, pattern: string, matches: var openarray[string],
start: int = 0): int =
return matchOrFind(s, rawCompile(pattern, PCRE_ANCHORED), matches, start)
proc find(s, pattern: string, matches: var openarray[string],
start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_MULTILINE),
matches, start) >= 0'i32
proc match(s, pattern: string, start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_ANCHORED), start) >= 0'i32
proc find(s, pattern: string, start: int = 0): bool =
return matchOrFind(s, rawCompile(pattern, PCRE_MULTILINE), start) >= 0'i32
template `=~` *(s, pattern: expr): expr =
## This calls ``match`` with an implicit declared ``matches`` array that
## can be used in the scope of the ``=~`` call:
##
## .. code-block:: nimrod
##
## if line =~ r"\s*(\w+)\s*\=\s*(\w+)":
## # matches a key=value pair:
## echo("Key: ", matches[1])
## echo("Value: ", matches[2])
## elif line =~ r"\s*(\#.*)":
## # matches a comment
## # note that the implicit ``matches`` array is different from the
## # ``matches`` array of the first branch
## echo("comment: ", matches[1])
## else:
## echo("syntax error")
##
when not definedInScope(matches):
var matches: array[0..maxSubPatterns-1, string]
match(s, pattern, matches)
proc regnexec(preg: ptr TRegExDesc, s: cstring, len, nmatch: int,
pmatch: ptr array [0..maxSubpatterns-1, TRegMatch],
eflags: cint): cint {.importc.}
@ -75,44 +218,6 @@ const
"Invalid use of repetition operators"
]
proc finalizeRegEx(x: TRegEx) = regfree(addr(x^))
proc re*(s: string): TRegEx =
## Constructor of regular expressions. Note that Nimrod's
## extended raw string literals supports this syntax ``re"[abc]"`` as
## a short form for ``re(r"[abc]")``.
new(result, finalizeRegEx)
var err = int(regncomp(addr(result^), s, s.len,
cint(REG_EXTENDED or REG_NEWLINE)))
if err != 0:
var e: ref EInvalidRegEx
new(e)
e.msg = ErrorMessages[err]
raise e
proc xre*(pattern: string): TRegEx =
## deletes whitespace from a pattern that is not escaped or in a character
## class. Then it constructs a regular expresion object via `re`.
## This is modelled after Perl's ``/x`` modifier.
var p = ""
var i = 0
while i < pattern.len:
case pattern[i]
of ' ', '\t':
inc i
of '\\':
add p, '\\'
add p, pattern[i+1]
inc i, 2
of '[':
while pattern[i] != ']' and pattern[i] != '\0':
add p, pattern[i]
inc i
else:
add p, pattern[i]
inc i
result = re(p)
proc rawmatch(s: string, pattern: TRegEx, matches: var openarray[string],
start: int): tuple[first, last: int] =
var