Merge branch 'devel' of https://github.com/Araq/Nimrod into devel
This commit is contained in:
commit
cb976c9c9a
4 changed files with 64 additions and 46 deletions
|
|
@ -37,6 +37,7 @@ type
|
||||||
|
|
||||||
AsyncHttpServer* = ref object
|
AsyncHttpServer* = ref object
|
||||||
socket: PAsyncSocket
|
socket: PAsyncSocket
|
||||||
|
reuseAddr: bool
|
||||||
|
|
||||||
HttpCode* = enum
|
HttpCode* = enum
|
||||||
Http200 = "200 OK",
|
Http200 = "200 OK",
|
||||||
|
|
@ -64,9 +65,10 @@ proc `==`*(protocol: tuple[orig: string, major, minor: int],
|
||||||
of HttpVer10: 0
|
of HttpVer10: 0
|
||||||
result = protocol.major == major and protocol.minor == minor
|
result = protocol.major == major and protocol.minor == minor
|
||||||
|
|
||||||
proc newAsyncHttpServer*(): PAsyncHttpServer =
|
proc newAsyncHttpServer*(reuseAddr = true): PAsyncHttpServer =
|
||||||
## Creates a new ``AsyncHttpServer`` instance.
|
## Creates a new ``AsyncHttpServer`` instance.
|
||||||
new result
|
new result
|
||||||
|
result.reuseAddr = reuseAddr
|
||||||
|
|
||||||
proc addHeaders(msg: var string, headers: PStringTable) =
|
proc addHeaders(msg: var string, headers: PStringTable) =
|
||||||
for k, v in headers:
|
for k, v in headers:
|
||||||
|
|
@ -210,6 +212,8 @@ proc serve*(server: PAsyncHttpServer, port: Port,
|
||||||
##
|
##
|
||||||
## When a request is made by a client the specified callback will be called.
|
## When a request is made by a client the specified callback will be called.
|
||||||
server.socket = newAsyncSocket()
|
server.socket = newAsyncSocket()
|
||||||
|
if server.reuseAddr:
|
||||||
|
server.socket.setSockOpt(OptReuseAddr, true)
|
||||||
server.socket.bindAddr(port, address)
|
server.socket.bindAddr(port, address)
|
||||||
server.socket.listen()
|
server.socket.listen()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,8 @@ import rawsockets
|
||||||
import net
|
import net
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
export SOBool
|
||||||
|
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
import openssl
|
import openssl
|
||||||
|
|
||||||
|
|
@ -425,6 +427,17 @@ when defined(ssl):
|
||||||
socket.bioOut = bioNew(bio_s_mem())
|
socket.bioOut = bioNew(bio_s_mem())
|
||||||
sslSetBio(socket.sslHandle, socket.bioIn, socket.bioOut)
|
sslSetBio(socket.sslHandle, socket.bioIn, socket.bioOut)
|
||||||
|
|
||||||
|
proc getSockOpt*(socket: AsyncSocket, opt: SOBool, level = SOL_SOCKET): bool {.
|
||||||
|
tags: [ReadIOEffect].} =
|
||||||
|
## Retrieves option ``opt`` as a boolean value.
|
||||||
|
var res = getSockOptInt(socket.fd, cint(level), toCInt(opt))
|
||||||
|
result = res != 0
|
||||||
|
|
||||||
|
proc setSockOpt*(socket: AsyncSocket, opt: SOBool, value: bool,
|
||||||
|
level = SOL_SOCKET) {.tags: [WriteIOEffect].} =
|
||||||
|
## Sets option ``opt`` to a boolean value specified by ``value``.
|
||||||
|
var valuei = cint(if value: 1 else: 0)
|
||||||
|
setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei)
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
type
|
type
|
||||||
|
|
|
||||||
|
|
@ -456,7 +456,7 @@ type
|
||||||
socket: AsyncSocket
|
socket: AsyncSocket
|
||||||
connected: bool
|
connected: bool
|
||||||
currentURL: TURL ## Where we are currently connected.
|
currentURL: TURL ## Where we are currently connected.
|
||||||
headers: StringTableRef
|
headers*: StringTableRef
|
||||||
maxRedirects: int
|
maxRedirects: int
|
||||||
userAgent: string
|
userAgent: string
|
||||||
when defined(ssl):
|
when defined(ssl):
|
||||||
|
|
|
||||||
|
|
@ -428,7 +428,8 @@ proc close*(socket: Socket) =
|
||||||
elif res != 1:
|
elif res != 1:
|
||||||
socketError(socket)
|
socketError(socket)
|
||||||
|
|
||||||
proc toCInt(opt: SOBool): cint =
|
proc toCInt*(opt: SOBool): cint =
|
||||||
|
## Converts a ``SOBool`` into its Socket Option cint representation.
|
||||||
case opt
|
case opt
|
||||||
of OptAcceptConn: SO_ACCEPTCONN
|
of OptAcceptConn: SO_ACCEPTCONN
|
||||||
of OptBroadcast: SO_BROADCAST
|
of OptBroadcast: SO_BROADCAST
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue