Skip to content

Commit

Permalink
Fix contentType handling code. (#162)
Browse files Browse the repository at this point in the history
Add more respond() aliases.
Add responded() to check if response is already sent.
  • Loading branch information
cheatfate authored Mar 2, 2021
1 parent 0370742 commit c31e453
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
4 changes: 3 additions & 1 deletion chronos/apps/http/httpcommon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ func getContentEncoding*(ch: openarray[string]): HttpResult[
func getContentType*(ch: openarray[string]): HttpResult[string] {.
raises: [Defect].} =
## Check and prepare value of ``Content-Type`` header.
if len(ch) > 1:
if len(ch) == 0:
err("No Content-Type values found")
elif len(ch) > 1:
err("Multiple Content-Type values found")
else:
let mparts = ch[0].split(";")
Expand Down
34 changes: 27 additions & 7 deletions chronos/apps/http/httpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ type
headersTable: HttpTable
body: seq[byte]
flags: set[HttpResponseFlags]
state: HttpResponseState
state*: HttpResponseState
connection*: HttpConnectionRef
chunkedWriter: AsyncStreamWriter

Expand All @@ -111,6 +111,8 @@ type

HttpConnectionRef* = ref HttpConnection

ByteChar* = string | seq[byte]

proc init(htype: typedesc[HttpProcessError], error: HTTPServerError,
exc: ref CatchableError, remote: TransportAddress,
code: HttpCode): HttpProcessError {.raises: [Defect].} =
Expand Down Expand Up @@ -925,7 +927,7 @@ proc sendBody*(resp: HttpResponseRef, pbytes: pointer, nbytes: int) {.async.} =
resp.state = HttpResponseState.Failed
raiseHttpCriticalError("Unable to send response")

proc sendBody*[T: string|seq[byte]](resp: HttpResponseRef, data: T) {.async.} =
proc sendBody*(resp: HttpResponseRef, data: ByteChar) {.async.} =
## Send HTTP response at once by using data ``data``.
checkPending(resp)
let responseHeaders = resp.prepareLengthHeaders(len(data))
Expand Down Expand Up @@ -1000,8 +1002,7 @@ proc sendChunk*(resp: HttpResponseRef, pbytes: pointer, nbytes: int) {.async.} =
resp.state = HttpResponseState.Failed
raiseHttpCriticalError("Unable to send response")

proc sendChunk*[T: string|seq[byte]](resp: HttpResponseRef,
data: T) {.async.} =
proc sendChunk*(resp: HttpResponseRef, data: ByteChar) {.async.} =
## Send single chunk of data ``data``.
if HttpResponseFlags.Chunked notin resp.flags:
raiseHttpCriticalError("Response was not prepared")
Expand Down Expand Up @@ -1035,17 +1036,36 @@ proc finish*(resp: HttpResponseRef) {.async.} =
resp.state = HttpResponseState.Failed
raiseHttpCriticalError("Unable to send response")

proc respond*(req: HttpRequestRef, code: HttpCode, content: string,
proc respond*(req: HttpRequestRef, code: HttpCode, content: ByteChar,
headers: HttpTable): Future[HttpResponseRef] {.async.} =
## Responds to the request with the specified ``HttpCode``, headers and
## content.
## Responds to the request with the specified ``HttpCode``, HTTP ``headers``
## and ``content``.
let response = req.getResponse()
response.status = code
for k, v in headers.stringItems():
response.addHeader(k, v)
await response.sendBody(content)
return response

proc respond*(req: HttpRequestRef, code: HttpCode,
content: ByteChar): Future[HttpResponseRef] =
## Responds to the request with specified ``HttpCode`` and ``content``.
respond(req, code, content, HttpTable.init())

proc respond*(req: HttpRequestRef, code: HttpCode): Future[HttpResponseRef] =
## Reponds to the request with specified ``HttpCode`` only.
respond(req, code, "", HttpTable.init())

proc responded*(req: HttpRequestRef): bool =
## Returns ``true`` if request ``req`` has been responded or responding.
if isSome(req.response):
if req.response.get().state == HttpResponseState.Empty:
false
else:
true
else:
false

proc remoteAddress*(conn: HttpConnectionRef): TransportAddress =
## Returns address of the remote host that established connection ``conn``.
conn.transp.remoteAddress()
Expand Down

0 comments on commit c31e453

Please sign in to comment.