Fixed OSError + recvLine deprecation warnings.
This commit is contained in:
parent
3ff572ffcd
commit
a9f2c3ffaf
4 changed files with 143 additions and 144 deletions
|
|
@ -47,6 +47,7 @@ proc badRequest(client: TSocket) =
|
||||||
send(client, "<p>Your browser sent a bad request, " &
|
send(client, "<p>Your browser sent a bad request, " &
|
||||||
"such as a POST without a Content-Length.</p>" & wwwNL)
|
"such as a POST without a Content-Length.</p>" & wwwNL)
|
||||||
|
|
||||||
|
when false:
|
||||||
proc cannotExec(client: TSocket) =
|
proc cannotExec(client: TSocket) =
|
||||||
send(client, "HTTP/1.1 500 Internal Server Error" & wwwNL)
|
send(client, "HTTP/1.1 500 Internal Server Error" & wwwNL)
|
||||||
sendTextContentType(client)
|
sendTextContentType(client)
|
||||||
|
|
@ -79,11 +80,11 @@ proc unimplemented(client: TSocket) =
|
||||||
|
|
||||||
# ----------------- file serving ---------------------------------------------
|
# ----------------- file serving ---------------------------------------------
|
||||||
|
|
||||||
|
when false:
|
||||||
proc discardHeaders(client: TSocket) = skip(client)
|
proc discardHeaders(client: TSocket) = skip(client)
|
||||||
|
|
||||||
proc serveFile*(client: TSocket, filename: string) =
|
proc serveFile*(client: TSocket, filename: string) =
|
||||||
## serves a file to the client.
|
## serves a file to the client.
|
||||||
when false: discardHeaders(client)
|
|
||||||
var f: TFile
|
var f: TFile
|
||||||
if open(f, filename):
|
if open(f, filename):
|
||||||
headers(client, filename)
|
headers(client, filename)
|
||||||
|
|
@ -96,7 +97,7 @@ proc serveFile*(client: TSocket, filename: string) =
|
||||||
if bytesread != bytesWritten:
|
if bytesread != bytesWritten:
|
||||||
dealloc(buf)
|
dealloc(buf)
|
||||||
close(f)
|
close(f)
|
||||||
OSError()
|
OSError(OSLastError())
|
||||||
if bytesread != bufSize: break
|
if bytesread != bufSize: break
|
||||||
dealloc(buf)
|
dealloc(buf)
|
||||||
close(f)
|
close(f)
|
||||||
|
|
@ -104,7 +105,8 @@ proc serveFile*(client: TSocket, filename: string) =
|
||||||
notFound(client)
|
notFound(client)
|
||||||
|
|
||||||
# ------------------ CGI execution -------------------------------------------
|
# ------------------ CGI execution -------------------------------------------
|
||||||
|
when false:
|
||||||
|
# TODO: Fix this, or get rid of it.
|
||||||
type
|
type
|
||||||
TRequestMethod = enum reqGet, reqPost
|
TRequestMethod = enum reqGet, reqPost
|
||||||
|
|
||||||
|
|
@ -226,7 +228,7 @@ proc open*(s: var TServer, port = TPort(80)) =
|
||||||
## creates a new server at port `port`. If ``port == 0`` a free port is
|
## creates a new server at port `port`. If ``port == 0`` a free port is
|
||||||
## acquired that can be accessed later by the ``port`` proc.
|
## acquired that can be accessed later by the ``port`` proc.
|
||||||
s.socket = socket(AF_INET)
|
s.socket = socket(AF_INET)
|
||||||
if s.socket == InvalidSocket: OSError()
|
if s.socket == InvalidSocket: OSError(OSLastError())
|
||||||
bindAddr(s.socket, port)
|
bindAddr(s.socket, port)
|
||||||
listen(s.socket)
|
listen(s.socket)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,11 +50,11 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
result.size = 0
|
result.size = 0
|
||||||
|
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
template fail(msg: expr) =
|
template fail(errCode: TOSErrorCode, msg: expr) =
|
||||||
rollback()
|
rollback()
|
||||||
if result.fHandle != 0: discard CloseHandle(result.fHandle)
|
if result.fHandle != 0: discard CloseHandle(result.fHandle)
|
||||||
if result.mapHandle != 0: discard CloseHandle(result.mapHandle)
|
if result.mapHandle != 0: discard CloseHandle(result.mapHandle)
|
||||||
OSError()
|
OSError(errCode)
|
||||||
# return false
|
# return false
|
||||||
#raise newException(EIO, msg)
|
#raise newException(EIO, msg)
|
||||||
|
|
||||||
|
|
@ -74,7 +74,7 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
result.fHandle = callCreateFile(CreateFileA, filename)
|
result.fHandle = callCreateFile(CreateFileA, filename)
|
||||||
|
|
||||||
if result.fHandle == INVALID_HANDLE_VALUE:
|
if result.fHandle == INVALID_HANDLE_VALUE:
|
||||||
fail "error opening file"
|
fail(OSLastError(), "error opening file")
|
||||||
|
|
||||||
if newFileSize != -1:
|
if newFileSize != -1:
|
||||||
var
|
var
|
||||||
|
|
@ -83,9 +83,10 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
|
|
||||||
var status = SetFilePointer(result.fHandle, sizeLow, addr(sizeHigh),
|
var status = SetFilePointer(result.fHandle, sizeLow, addr(sizeHigh),
|
||||||
FILE_BEGIN)
|
FILE_BEGIN)
|
||||||
if (status == INVALID_SET_FILE_POINTER and GetLastError() != NO_ERROR) or
|
let lastErr = OSLastError()
|
||||||
|
if (status == INVALID_SET_FILE_POINTER and lastErr.int32 != NO_ERROR) or
|
||||||
(SetEndOfFile(result.fHandle) == 0):
|
(SetEndOfFile(result.fHandle) == 0):
|
||||||
fail "error setting file size"
|
fail(lastErr, "error setting file size")
|
||||||
|
|
||||||
# since the strings are always 'nil', we simply always call
|
# since the strings are always 'nil', we simply always call
|
||||||
# CreateFileMappingW which should be slightly faster anyway:
|
# CreateFileMappingW which should be slightly faster anyway:
|
||||||
|
|
@ -95,7 +96,7 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
0, 0, nil)
|
0, 0, nil)
|
||||||
|
|
||||||
if result.mapHandle == 0:
|
if result.mapHandle == 0:
|
||||||
fail "error creating mapping"
|
fail(OSLastError(), "error creating mapping")
|
||||||
|
|
||||||
result.mem = MapViewOfFileEx(
|
result.mem = MapViewOfFileEx(
|
||||||
result.mapHandle,
|
result.mapHandle,
|
||||||
|
|
@ -106,22 +107,22 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
nil)
|
nil)
|
||||||
|
|
||||||
if result.mem == nil:
|
if result.mem == nil:
|
||||||
fail "error mapping view"
|
fail(OSLastError(), "error mapping view")
|
||||||
|
|
||||||
var hi, low: int32
|
var hi, low: int32
|
||||||
low = GetFileSize(result.fHandle, addr(hi))
|
low = GetFileSize(result.fHandle, addr(hi))
|
||||||
if low == INVALID_FILE_SIZE:
|
if low == INVALID_FILE_SIZE:
|
||||||
fail "error getting file size"
|
fail(OSLastError(), "error getting file size")
|
||||||
else:
|
else:
|
||||||
var fileSize = (int64(hi) shr 32) or low
|
var fileSize = (int64(hi) shr 32) or low
|
||||||
if mappedSize != -1: result.size = min(fileSize, mappedSize).int
|
if mappedSize != -1: result.size = min(fileSize, mappedSize).int
|
||||||
else: result.size = fileSize.int
|
else: result.size = fileSize.int
|
||||||
|
|
||||||
else:
|
else:
|
||||||
template fail(msg: expr) =
|
template fail(errCode: TOSErrorCode, msg: expr) =
|
||||||
rollback()
|
rollback()
|
||||||
if result.handle != 0: discard close(result.handle)
|
if result.handle != 0: discard close(result.handle)
|
||||||
OSError()
|
OSError(errCode)
|
||||||
|
|
||||||
var flags = if readonly: O_RDONLY else: O_RDWR
|
var flags = if readonly: O_RDONLY else: O_RDWR
|
||||||
|
|
||||||
|
|
@ -132,11 +133,11 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
if result.handle == -1:
|
if result.handle == -1:
|
||||||
# XXX: errno is supposed to be set here
|
# XXX: errno is supposed to be set here
|
||||||
# Is there an exception that wraps it?
|
# Is there an exception that wraps it?
|
||||||
fail "error opening file"
|
fail(OSLastError(), "error opening file")
|
||||||
|
|
||||||
if newFileSize != -1:
|
if newFileSize != -1:
|
||||||
if ftruncate(result.handle, newFileSize) == -1:
|
if ftruncate(result.handle, newFileSize) == -1:
|
||||||
fail "error setting file size"
|
fail(OSLastError(), "error setting file size")
|
||||||
|
|
||||||
if mappedSize != -1:
|
if mappedSize != -1:
|
||||||
result.size = mappedSize
|
result.size = mappedSize
|
||||||
|
|
@ -147,7 +148,7 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
# Why is mmap taking int anyway?
|
# Why is mmap taking int anyway?
|
||||||
result.size = int(stat.st_size)
|
result.size = int(stat.st_size)
|
||||||
else:
|
else:
|
||||||
fail "error getting file size"
|
fail(OSLastError(), "error getting file size")
|
||||||
|
|
||||||
result.mem = mmap(
|
result.mem = mmap(
|
||||||
nil,
|
nil,
|
||||||
|
|
@ -158,21 +159,24 @@ proc open*(filename: string, mode: TFileMode = fmRead,
|
||||||
offset)
|
offset)
|
||||||
|
|
||||||
if result.mem == cast[pointer](MAP_FAILED):
|
if result.mem == cast[pointer](MAP_FAILED):
|
||||||
fail "file mapping failed"
|
fail(OSLastError(), "file mapping failed")
|
||||||
|
|
||||||
proc close*(f: var TMemFile) =
|
proc close*(f: var TMemFile) =
|
||||||
## closes the memory mapped file `f`. All changes are written back to the
|
## closes the memory mapped file `f`. All changes are written back to the
|
||||||
## file system, if `f` was opened with write access.
|
## file system, if `f` was opened with write access.
|
||||||
|
|
||||||
var error = false
|
var error = false
|
||||||
|
var lastErr: TOSErrorCode
|
||||||
|
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
if f.fHandle != INVALID_HANDLE_VALUE:
|
if f.fHandle != INVALID_HANDLE_VALUE:
|
||||||
|
lastErr = OSLastError()
|
||||||
error = UnmapViewOfFile(f.mem) == 0
|
error = UnmapViewOfFile(f.mem) == 0
|
||||||
error = (CloseHandle(f.mapHandle) == 0) or error
|
error = (CloseHandle(f.mapHandle) == 0) or error
|
||||||
error = (CloseHandle(f.fHandle) == 0) or error
|
error = (CloseHandle(f.fHandle) == 0) or error
|
||||||
else:
|
else:
|
||||||
if f.handle != 0:
|
if f.handle != 0:
|
||||||
|
lastErr = OSLastError()
|
||||||
error = munmap(f.mem, f.size) != 0
|
error = munmap(f.mem, f.size) != 0
|
||||||
error = (close(f.handle) != 0) or error
|
error = (close(f.handle) != 0) or error
|
||||||
|
|
||||||
|
|
@ -185,5 +189,5 @@ proc close*(f: var TMemFile) =
|
||||||
else:
|
else:
|
||||||
f.handle = 0
|
f.handle = 0
|
||||||
|
|
||||||
if error: OSError()
|
if error: OSError(lastErr)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ proc open*(host = "localhost", port = 6379.TPort): TRedis =
|
||||||
## Opens a connection to the redis server.
|
## Opens a connection to the redis server.
|
||||||
result.socket = socket(buffered = false)
|
result.socket = socket(buffered = false)
|
||||||
if result.socket == InvalidSocket:
|
if result.socket == InvalidSocket:
|
||||||
OSError()
|
OSError(OSLastError())
|
||||||
result.socket.connect(host, port)
|
result.socket.connect(host, port)
|
||||||
|
|
||||||
proc raiseInvalidReply(expected, got: char) =
|
proc raiseInvalidReply(expected, got: char) =
|
||||||
|
|
@ -50,7 +50,7 @@ proc raiseNoOK(status: string) =
|
||||||
|
|
||||||
proc parseStatus(r: TRedis): TRedisStatus =
|
proc parseStatus(r: TRedis): TRedisStatus =
|
||||||
var line = ""
|
var line = ""
|
||||||
if r.socket.recvLine(line):
|
r.socket.readLine(line)
|
||||||
if line == "":
|
if line == "":
|
||||||
raise newException(ERedis, "Server closed connection prematurely")
|
raise newException(ERedis, "Server closed connection prematurely")
|
||||||
|
|
||||||
|
|
@ -60,12 +60,10 @@ proc parseStatus(r: TRedis): TRedisStatus =
|
||||||
raiseInvalidReply('+', line[0])
|
raiseInvalidReply('+', line[0])
|
||||||
|
|
||||||
return line.substr(1) # Strip '+'
|
return line.substr(1) # Strip '+'
|
||||||
else:
|
|
||||||
OSError()
|
|
||||||
|
|
||||||
proc parseInteger(r: TRedis): TRedisInteger =
|
proc parseInteger(r: TRedis): TRedisInteger =
|
||||||
var line = ""
|
var line = ""
|
||||||
if r.socket.recvLine(line):
|
r.socket.readLine(line)
|
||||||
if line == "":
|
if line == "":
|
||||||
raise newException(ERedis, "Server closed connection prematurely")
|
raise newException(ERedis, "Server closed connection prematurely")
|
||||||
|
|
||||||
|
|
@ -77,7 +75,6 @@ proc parseInteger(r: TRedis): TRedisInteger =
|
||||||
# Strip ':'
|
# Strip ':'
|
||||||
if parseBiggestInt(line, result, 1) == 0:
|
if parseBiggestInt(line, result, 1) == 0:
|
||||||
raise newException(EInvalidReply, "Unable to parse integer.")
|
raise newException(EInvalidReply, "Unable to parse integer.")
|
||||||
else: OSError()
|
|
||||||
|
|
||||||
proc recv(sock: TSocket, size: int): TaintedString =
|
proc recv(sock: TSocket, size: int): TaintedString =
|
||||||
result = newString(size).TaintedString
|
result = newString(size).TaintedString
|
||||||
|
|
@ -86,8 +83,7 @@ proc recv(sock: TSocket, size: int): TaintedString =
|
||||||
|
|
||||||
proc parseBulk(r: TRedis, allowMBNil = False): TRedisString =
|
proc parseBulk(r: TRedis, allowMBNil = False): TRedisString =
|
||||||
var line = ""
|
var line = ""
|
||||||
if not r.socket.recvLine(line.TaintedString):
|
r.socket.readLine(line.TaintedString)
|
||||||
raise newException(EInvalidReply, "recvLine failed")
|
|
||||||
|
|
||||||
# Error.
|
# Error.
|
||||||
if line[0] == '-':
|
if line[0] == '-':
|
||||||
|
|
@ -110,8 +106,7 @@ proc parseBulk(r: TRedis, allowMBNil = False): TRedisString =
|
||||||
|
|
||||||
proc parseMultiBulk(r: TRedis): TRedisList =
|
proc parseMultiBulk(r: TRedis): TRedisList =
|
||||||
var line = TaintedString""
|
var line = TaintedString""
|
||||||
if not r.socket.recvLine(line):
|
r.socket.readLine(line)
|
||||||
raise newException(EInvalidReply, "recvLine failed")
|
|
||||||
|
|
||||||
if line.string[0] != '*':
|
if line.string[0] != '*':
|
||||||
raiseInvalidReply('*', line.string[0])
|
raiseInvalidReply('*', line.string[0])
|
||||||
|
|
@ -848,10 +843,8 @@ proc shutdown*(r: TRedis) =
|
||||||
## Synchronously save the dataset to disk and then shut down the server
|
## Synchronously save the dataset to disk and then shut down the server
|
||||||
r.sendCommand("SHUTDOWN")
|
r.sendCommand("SHUTDOWN")
|
||||||
var s = "".TaintedString
|
var s = "".TaintedString
|
||||||
if r.socket.recvLine(s):
|
r.socket.readLine(s)
|
||||||
if s.string.len != 0: raise newException(ERedis, s.string)
|
if s.string.len != 0: raise newException(ERedis, s.string)
|
||||||
else:
|
|
||||||
OSError()
|
|
||||||
|
|
||||||
proc slaveof*(r: TRedis, host: string, port: string) =
|
proc slaveof*(r: TRedis, host: string, port: string) =
|
||||||
## Make the server a slave of another instance, or promote it as master
|
## Make the server a slave of another instance, or promote it as master
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,8 @@ s = socket()
|
||||||
|
|
||||||
s.connect("www.google.com", TPort(80))
|
s.connect("www.google.com", TPort(80))
|
||||||
|
|
||||||
var recvData: string = ""
|
var data: string = ""
|
||||||
echo(s.recvLine(recvData))
|
s.readLine(data)
|
||||||
echo(recvData)
|
echo(data)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue