nim-websock/ws/stream.nim
Arijit Das 6b76bd8261
WIP: Implement websocket TLS. (#7)
* Update http to use chronos http.

* Implement TLS in websocket.

* Add webscoket TLS test.

* Minor nit.

* Add TLS test file.

* Update http to use chronos http. (#6)

* Update http to use chronos http.

* Add stream.nim file.

* Address comments.

* Fix CI failure.

* Minor change.

* Address comments.

* Fix windows CI failing test.

* minor cleanup

* spacess

* more idiomatic connect

* use stew/base10

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>

* Implement TLS in websocket.

* Minor nit.

* merge master

* wip

* Update http to use chronos http. (#6)

* Update http to use chronos http.

* Add stream.nim file.

* Address comments.

* Fix CI failure.

* Minor change.

* Address comments.

* Fix windows CI failing test.

* minor cleanup

* spacess

* more idiomatic connect

* use stew/base10

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>

* Update http to use chronos http.

* Implement TLS in websocket.

* Minor nit.

* Update http to use chronos http. (#6)

* Update http to use chronos http.

* Add stream.nim file.

* Address comments.

* Fix CI failure.

* Minor change.

* Address comments.

* Fix windows CI failing test.

* minor cleanup

* spacess

* more idiomatic connect

* use stew/base10

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>

* Implement TLS in websocket.

* Minor nit.

* add testing keys

* wip

* fix test

* wip

* remove eth dep and add skipdirs

* fix package structure

* fix deps

* check nim version

* Fix CI failure.

* Don't call `ws.stream.closeWait()`

* always close both ends to complete the sequence

* misc

* don't fail on close

* Fix windows CI.

* fix linux x86 builds

* use consistent connect pattern

* move keys to better place

* return dumbResponse

* small cleanup

Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com>
2021-04-13 16:05:58 -06:00

54 lines
1.7 KiB
Nim

import pkg/[chronos,
chronos/apps/http/httpserver,
chronos/timer,
chronicles,
httputils]
import strutils
const
HeaderSep = @[byte('\c'), byte('\L'), byte('\c'), byte('\L')]
HttpHeadersTimeout = timer.seconds(120) # timeout for receiving headers (120 sec)
MaxHttpHeadersSize = 8192 # maximum size of HTTP headers in octets
proc readHeaders*(rstream: AsyncStreamReader): Future[seq[byte]] {.async.} =
var buffer = newSeq[byte](MaxHttpHeadersSize)
var error = false
try:
let hlenfut = rstream.readUntil(
addr buffer[0], MaxHttpHeadersSize,
sep = HeaderSep)
let ores = await withTimeout(hlenfut, HttpHeadersTimeout)
if not ores:
# Timeout
debug "Timeout expired while receiving headers",
address = rstream.tsource.remoteAddress()
error = true
else:
let hlen = hlenfut.read()
buffer.setLen(hlen)
except AsyncStreamLimitError:
# size of headers exceeds `MaxHttpHeadersSize`
debug "Maximum size of headers limit reached",
address = rstream.tsource.remoteAddress()
error = true
except AsyncStreamIncompleteError:
# remote peer disconnected
debug "Remote peer disconnected", address = rstream.tsource.remoteAddress()
error = true
except AsyncStreamError as exc:
debug "Problems with networking", address = rstream.tsource.remoteAddress(),
error = exc.msg
error = true
if error:
buffer.setLen(0)
return buffer
proc closeWait*(wsStream : AsyncStream) {.async.} =
await allFutures(
wsStream.writer.tsource.closeWait(),
wsStream.reader.tsource.closeWait())
# TODO: Implement stream read and write wrapper.