resolved system.nim conflicts
This commit is contained in:
commit
9a22ae0217
37 changed files with 28956 additions and 28481 deletions
|
|
@ -7,19 +7,19 @@
|
|||
# distribution, for details about the copyright.
|
||||
#
|
||||
|
||||
## This module implements a simple HTTP-Server.
|
||||
## This module implements a simple HTTP-Server.
|
||||
##
|
||||
## Example:
|
||||
## 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)
|
||||
## client.send("Hello for the $#th time." % $counter & wwwNL)
|
||||
## return false # do not stop processing
|
||||
##
|
||||
##
|
||||
## run(handleRequest, TPort(80))
|
||||
##
|
||||
|
||||
|
|
@ -47,7 +47,7 @@ proc cannotExec(client: TSocket) =
|
|||
sendTextContentType(client)
|
||||
send(client, "<P>Error prohibited CGI execution." & wwwNL)
|
||||
|
||||
proc headers(client: TSocket, filename: string) =
|
||||
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)
|
||||
|
|
@ -60,16 +60,16 @@ proc notFound(client: TSocket) =
|
|||
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, "is unavailable or nonexistent.</p>" & 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" &
|
||||
send(client, "<html><head><title>Method Not Implemented" &
|
||||
"</title></head>" &
|
||||
"<body><p>HTTP request method not supported." &
|
||||
"<body><p>HTTP request method not supported.</p>" &
|
||||
"</body></HTML>" & wwwNL)
|
||||
|
||||
# ----------------- file serving ---------------------------------------------
|
||||
|
|
@ -78,7 +78,7 @@ proc discardHeaders(client: TSocket) = skip(client)
|
|||
|
||||
proc serveFile(client: TSocket, filename: string) =
|
||||
discardHeaders(client)
|
||||
|
||||
|
||||
var f: TFile
|
||||
if open(f, filename):
|
||||
headers(client, filename)
|
||||
|
|
@ -109,7 +109,7 @@ proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) =
|
|||
case meth
|
||||
of reqGet:
|
||||
discardHeaders(client)
|
||||
|
||||
|
||||
env["REQUEST_METHOD"] = "GET"
|
||||
env["QUERY_STRING"] = query
|
||||
of reqPost:
|
||||
|
|
@ -122,16 +122,16 @@ proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) =
|
|||
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:
|
||||
|
|
@ -139,7 +139,7 @@ proc executeCgi(client: TSocket, path, query: string, meth: TRequestMethod) =
|
|||
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()
|
||||
|
|
@ -165,11 +165,11 @@ proc acceptRequest(client: TSocket) =
|
|||
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)
|
||||
|
|
@ -193,15 +193,15 @@ type
|
|||
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)) =
|
||||
|
||||
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.
|
||||
## acquired 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:
|
||||
|
|
@ -209,12 +209,12 @@ proc open*(s: var TServer, port = TPort(80)) =
|
|||
s.client = InvalidSocket
|
||||
s.path = ""
|
||||
s.query = ""
|
||||
|
||||
proc port*(s: var TServer): TPort =
|
||||
## get the port number the server has aquired.
|
||||
|
||||
proc port*(s: var TServer): TPort =
|
||||
## get the port number the server has acquired.
|
||||
result = s.port
|
||||
|
||||
proc next*(s: var TServer) =
|
||||
|
||||
proc next*(s: var TServer) =
|
||||
## proceed to the first/next request.
|
||||
s.client = accept(s.socket)
|
||||
headers(s.client, "")
|
||||
|
|
@ -236,13 +236,13 @@ 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,
|
||||
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:
|
||||
while true:
|
||||
next(s)
|
||||
if handleRequest(s.client, s.path, s.query): break
|
||||
close(s.client)
|
||||
|
|
@ -252,8 +252,8 @@ 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)
|
||||
client.send("Hello, Andreas, for the $#th time." % $counter & wwwNL)
|
||||
return false # do not stop processing
|
||||
|
||||
|
||||
run(handleRequest, TPort(80))
|
||||
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ proc execCmd*(command: string): int
|
|||
proc executeCommand*(command: string): int {.deprecated.} =
|
||||
## **Deprecated since version 0.8.2**: Use `execCmd` instead.
|
||||
result = execCmd(command)
|
||||
|
||||
|
||||
|
||||
proc startProcess*(command: string,
|
||||
workingDir: string = "",
|
||||
|
|
@ -115,7 +115,7 @@ when defined(macosx) or defined(bsd):
|
|||
a: var int, b: pointer, c: int): cint {.
|
||||
importc: "sysctl", header: "<sys/sysctl.h>".}
|
||||
|
||||
proc countProcessors*(): int =
|
||||
proc countProcessors*(): int =
|
||||
## returns the numer of the processors/cores the machine has.
|
||||
## Returns 0 if it cannot be determined.
|
||||
when defined(windows):
|
||||
|
|
@ -186,7 +186,7 @@ proc execProcesses*(cmds: openArray[string],
|
|||
inc(i)
|
||||
if i > high(cmds): break
|
||||
for i in 0..m-1:
|
||||
result = max(waitForExit(q[i]), result)
|
||||
result = max(waitForExit(q[i]), result)
|
||||
else:
|
||||
for i in 0..high(cmds):
|
||||
var p = startProcessAux(cmds[i], options=options)
|
||||
|
|
@ -243,7 +243,7 @@ when defined(Windows):
|
|||
if a == 0 and br != 0: OSError()
|
||||
s.atTheEnd = br < bufLen
|
||||
result = br
|
||||
|
||||
|
||||
proc hsWriteData(s: PFileHandleStream, buffer: pointer, bufLen: int) =
|
||||
var bytesWritten: int32
|
||||
var a = winlean.writeFile(s.handle, buffer, bufLen, bytesWritten, nil)
|
||||
|
|
@ -256,10 +256,10 @@ when defined(Windows):
|
|||
result.atEnd = hsAtEnd
|
||||
result.readData = hsReadData
|
||||
result.writeData = hsWriteData
|
||||
|
||||
|
||||
proc buildCommandLine(a: string, args: openarray[string]): cstring =
|
||||
var res = quoteIfContainsWhite(a)
|
||||
for i in 0..high(args):
|
||||
for i in 0..high(args):
|
||||
res.add(' ')
|
||||
res.add(quoteIfContainsWhite(args[i]))
|
||||
result = cast[cstring](alloc0(res.len+1))
|
||||
|
|
@ -324,7 +324,7 @@ when defined(Windows):
|
|||
result.inputHandle = si.hStdInput
|
||||
result.outputHandle = si.hStdOutput
|
||||
result.errorHandle = si.hStdError
|
||||
|
||||
|
||||
var cmdl: cstring
|
||||
if false: # poUseShell in options:
|
||||
cmdl = buildCommandLine(getEnv("COMSPEC"), @["/c", command] & args)
|
||||
|
|
@ -337,13 +337,13 @@ when defined(Windows):
|
|||
if poEchoCmd in options: echo($cmdl)
|
||||
success = winlean.CreateProcess(nil,
|
||||
cmdl, nil, nil, 1, NORMAL_PRIORITY_CLASS, e, wd, SI, ProcInfo)
|
||||
|
||||
|
||||
if poParentStreams notin options:
|
||||
FileClose(si.hStdInput)
|
||||
FileClose(si.hStdOutput)
|
||||
if poStdErrToStdOut notin options:
|
||||
FileClose(si.hStdError)
|
||||
|
||||
|
||||
if e != nil: dealloc(e)
|
||||
dealloc(cmdl)
|
||||
if success == 0: OSError()
|
||||
|
|
@ -382,7 +382,7 @@ when defined(Windows):
|
|||
proc errorStream(p: PProcess): PStream =
|
||||
result = newFileHandleStream(p.errorHandle)
|
||||
|
||||
proc execCmd(command: string): int =
|
||||
proc execCmd(command: string): int =
|
||||
var
|
||||
SI: TStartupInfo
|
||||
ProcInfo: TProcessInformation
|
||||
|
|
@ -535,7 +535,7 @@ else:
|
|||
|
||||
proc csystem(cmd: cstring): cint {.nodecl, importc: "system".}
|
||||
|
||||
proc execCmd(command: string): int =
|
||||
proc execCmd(command: string): int =
|
||||
result = csystem(command)
|
||||
|
||||
when isMainModule:
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -9,12 +9,12 @@
|
|||
|
||||
## This module implements Nimrod's support for the ``variant`` datatype.
|
||||
## `TVariant` shows how the flexibility of dynamic typing is achieved
|
||||
## within a static type system.
|
||||
## within a static type system.
|
||||
|
||||
type
|
||||
TVarType* = enum
|
||||
vtNone,
|
||||
vtBool,
|
||||
vtBool,
|
||||
vtChar,
|
||||
vtEnum,
|
||||
vtInt,
|
||||
|
|
@ -31,7 +31,7 @@ type
|
|||
of vtString: vstring: string
|
||||
of vtSet, vtSeq: q: seq[TVariant]
|
||||
of vtDict: d: seq[tuple[key, val: TVariant]]
|
||||
|
||||
|
||||
iterator objectFields*[T](x: T, skipInherited: bool): tuple[
|
||||
key: string, val: TVariant] {.magic: "ObjectFields"}
|
||||
|
||||
|
|
@ -74,10 +74,10 @@ proc `?`* [T: object](x: T): TVariant {.magic: "ToVariant".}
|
|||
|
||||
proc `><`*[T](v: TVariant, typ: T): T {.magic: "FromVariant".}
|
||||
|
||||
?[?5, ?67, ?"hallo"]
|
||||
?[?5, ?67, ?"hello"]
|
||||
myVar?int
|
||||
|
||||
|
||||
|
||||
proc `==`* (x, y: TVariant): bool =
|
||||
if x.vtype == y.vtype:
|
||||
case x.vtype
|
||||
|
|
@ -139,7 +139,7 @@ proc `[]=`* (a, b, c: TVariant) =
|
|||
a.d[b.vint].val = c
|
||||
variantError()
|
||||
else: variantError()
|
||||
|
||||
|
||||
proc `[]`* (a: TVariant, b: int): TVariant {.inline} = return a[?b]
|
||||
proc `[]`* (a: TVariant, b: string): TVariant {.inline} = return a[?b]
|
||||
proc `[]=`* (a: TVariant, b: int, c: TVariant) {.inline} = a[?b] = c
|
||||
|
|
@ -154,9 +154,9 @@ proc `+`* (x, y: TVariant): TVariant =
|
|||
else:
|
||||
case y.vtype
|
||||
of vtBool, vtChar, vtEnum, vtInt:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
vint: int64
|
||||
of vtFloat: vfloat: float64
|
||||
of vtString: vstring: string
|
||||
|
|
@ -171,11 +171,11 @@ proc `mod`* (x, y: TVariant): TVariant
|
|||
proc `&`* (x, y: TVariant): TVariant
|
||||
proc `$`* (x: TVariant): string =
|
||||
# uses JS notation
|
||||
|
||||
|
||||
proc parseVariant*(s: string): TVariant
|
||||
proc `<`* (x, y: TVariant): bool
|
||||
proc `<=`* (x, y: TVariant): bool
|
||||
|
||||
proc hash*(x: TVariant): int =
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue