Skip to content

Commit

Permalink
headers no longer a ref
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Sep 8, 2024
1 parent 02db1a0 commit bf1d845
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Http/1 can only compete by enabling [pipelining](https://en.wikipedia.org/wiki/H

### Serving http/1 and http/3 traffic

You may use a reverse proxy such a [Caddy](https://github.com/caddyserver/caddy) or a cloud offering such as AWS ALB for this. Look for a service that can receive http/1, 2, 3 traffic and forward it as http/2.
You may use a reverse proxy such as [Caddy](https://github.com/caddyserver/caddy) or a cloud offering such as AWS ALB for this. Look for a service that can receive http/1, 2, 3 traffic and forward it as http/2 to hyperx.

### Data streaming

Expand Down
8 changes: 2 additions & 6 deletions examples/dataStream.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ const frmSize = 16 * 1024
doAssert dataSize mod frmSize == 0
const chunks = dataSize div frmSize

func newStringRef(s = ""): ref string =
new result
result[] = s

when isMainModule:
var dataSentSize = 0
var dataRecvSize = 0
Expand All @@ -31,7 +27,7 @@ when isMainModule:
hmPost, path,
contentLen = dataSize
)
var data = newStringref()
var data = new string
for _ in 0 .. frmSize-1:
data[].add 'a'
for i in 0 .. chunks-1:
Expand All @@ -41,7 +37,7 @@ when isMainModule:
proc recv(
strm: ClientStream
) {.async.} =
var data = newStringRef()
var data = new string
await strm.recvHeaders(data)
doAssert ":status:" in data[]
while not strm.recvEnded:
Expand Down
13 changes: 2 additions & 11 deletions examples/localServer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@ const localPort* = Port 8783
const certFile = getEnv "HYPERX_TEST_CERTFILE"
const keyFile = getEnv "HYPERX_TEST_KEYFILE"

func newStringRef(s = ""): ref string =
new result
result[] = s

func newSeqRef[T](s: seq[T] = @[]): ref seq[T] =
new result
result[] = s

proc processStream(strm: ClientStream) {.async.} =
## Full-duplex echo stream
with strm:
let data = newStringRef()
let data = new string
await strm.recvHeaders(data)
await strm.sendHeaders(
newSeqRef(@[(":status", "200")]),
finish = false
@[(":status", "200")], finish = false
)
if strm.recvEnded:
data[] = "Hello world!"
Expand Down
8 changes: 2 additions & 6 deletions examples/streamClient.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import std/asyncdispatch
import ../src/hyperx/client
from ./localServer import localHost, localPort

func newStringRef(s = ""): ref string =
new result
result[] = s

when isMainModule:
var completed = newSeq[string]()

Expand All @@ -22,7 +18,7 @@ when isMainModule:
hmPost, path,
contentLen = contentLen
)
var data = newStringRef()
var data = new string
var i = 0
for chunk in chunks:
data[].setLen 0
Expand All @@ -45,7 +41,7 @@ when isMainModule:
let strm = client.newClientStream()
with strm:
# send and recv concurrently
var data = newStringRef()
var data = new string
let recvFut = strm.recv(data)
let sendFut = strm.send(path, chunks)
await recvFut
Expand Down
19 changes: 9 additions & 10 deletions src/hyperx/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,18 @@ proc sendHeaders*(
template stream: untyped = strm.stream
check stream.state in strmStateHeaderSendAllowed,
newErrorOrDefault(stream.error, newStrmError errStreamClosed)
var headers = new(seq[byte])
headers[] = newSeq[byte]()
client.hpackEncode(headers[], ":method", $httpMethod)
client.hpackEncode(headers[], ":scheme", "https")
client.hpackEncode(headers[], ":path", path)
client.hpackEncode(headers[], ":authority", client.hostname)
client.hpackEncode(headers[], "user-agent", userAgent)
var headers = newSeq[byte]()
client.hpackEncode(headers, ":method", $httpMethod)
client.hpackEncode(headers, ":scheme", "https")
client.hpackEncode(headers, ":path", path)
client.hpackEncode(headers, ":authority", client.hostname)
client.hpackEncode(headers, "user-agent", userAgent)
if httpMethod in {hmGet, hmHead}:
doAssert contentLen == 0
client.hpackEncode(headers[], "accept", accept)
client.hpackEncode(headers, "accept", accept)
if httpMethod in {hmPost, hmPut, hmPatch}:
client.hpackEncode(headers[], "content-type", contentType)
client.hpackEncode(headers[], "content-length", $contentLen)
client.hpackEncode(headers, "content-type", contentType)
client.hpackEncode(headers, "content-length", $contentLen)
let finish = contentLen == 0
result = strm.sendHeadersImpl(headers, finish)

Expand Down
13 changes: 6 additions & 7 deletions src/hyperx/clientserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ func recvTrailers*(strm: ClientStream): string =

proc sendHeadersImpl*(
strm: ClientStream,
headers: ref seq[byte], # XXX ref string
headers: seq[byte],
finish: bool
): Future[void] =
## Headers must be HPACK encoded;
Expand All @@ -1113,7 +1113,7 @@ proc sendHeadersImpl*(
(strm.stateSend in {csStateHeaders, csStateData} and finish)
strm.stateSend = csStateHeaders
var frm = newFrame()
frm.add headers[]
frm.add headers
frm.setTyp frmtHeaders
frm.setSid strm.stream.id.FrmSid
frm.setPayloadLen frm.payload.len.FrmPayloadLen
Expand All @@ -1125,17 +1125,16 @@ proc sendHeadersImpl*(

proc sendHeaders*(
strm: ClientStream,
headers: ref seq[(string, string)],
headers: seq[(string, string)],
finish: bool
): Future[void] =
template client: untyped = strm.client
template stream: untyped = strm.stream
check stream.state in strmStateHeaderSendAllowed,
newErrorOrDefault(stream.error, newStrmError errStreamClosed)
var henc = new(seq[byte])
henc[] = newSeq[byte]()
for (n, v) in headers[]:
client.hpackEncode(henc[], n, v)
var henc = newSeq[byte]()
for (n, v) in headers:
client.hpackEncode(henc, n, v)
result = strm.sendHeadersImpl(henc, finish)

proc sendBodyNaked(
Expand Down
9 changes: 4 additions & 5 deletions src/hyperx/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ proc sendHeaders*(
template stream: untyped = strm.stream
check stream.state in strmStateHeaderSendAllowed,
newErrorOrDefault(stream.error, newStrmError errStreamClosed)
var headers = new(seq[byte])
headers[] = newSeq[byte]()
client.hpackEncode(headers[], ":status", $status)
var headers = newSeq[byte]()
client.hpackEncode(headers, ":status", $status)
if contentType.len > 0:
client.hpackEncode(headers[], "content-type", contentType)
client.hpackEncode(headers, "content-type", contentType)
if contentLen > -1:
client.hpackEncode(headers[], "content-length", $contentLen)
client.hpackEncode(headers, "content-length", $contentLen)
let finish = contentLen <= 0
result = strm.sendHeadersImpl(headers, finish)
4 changes: 2 additions & 2 deletions tests/functional/tcancel.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const dataFrameLen = 1

proc send(strm: ClientStream) {.async.} =
await strm.sendHeaders(
newSeqRef(@[
@[
(":method", "POST"),
(":scheme", "https"),
(":path", "/file/"),
(":authority", "foo.bar"),
("user-agent", "HyperX/0.1"),
("content-type", "text/plain")
]),
],
finish = false
)
let data = newStringRef newString(dataFrameLen)
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/tconcurrent.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ proc spawnStream(
with strm:
let data = newStringref()
let recvFut = strm.recv(data)
let sendFut = strm.sendHeaders(headers.s, finish = true)
let sendFut = strm.sendHeaders(headers.s[], finish = true)
await recvFut
await sendFut
doAssert data[] == headers.raw[]
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/tconcurrentdata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newReqsCtx(): ReqsCtx =

proc send(strm: ClientStream, req: Req) {.async.} =
# XXX send multiple data frames
await strm.sendHeaders(req.headers.s, finish = false)
await strm.sendHeaders(req.headers.s[], finish = false)
await strm.sendBody(req.data.s, finish = true)

proc recv(strm: ClientStream, req: Req) {.async.} =
Expand Down
2 changes: 1 addition & 1 deletion tests/functional/tflowcontrol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newReqsCtx(): ReqsCtx =
)

proc send(strm: ClientStream, req: Req) {.async.} =
await strm.sendHeaders(req.headers.s, finish = false)
await strm.sendHeaders(req.headers.s[], finish = false)
await strm.sendBody(req.data.s, finish = true)

proc recv(strm: ClientStream, req: Req) {.async.} =
Expand Down
4 changes: 1 addition & 3 deletions tests/functional/tserial.nim
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ proc main() {.async.} =
let rawHeaders = headers.rawHeaders()
let strm = client.newClientStream()
with strm:
let headersRef = new(seq[Header])
headersRef[] = headers
await strm.sendHeaders(headersRef, finish = true)
await strm.sendHeaders(headers, finish = true)
var data = newStringref()
await strm.recvHeaders(data)
doAssert data[] == ":status: 200\r\n"
Expand Down
4 changes: 1 addition & 3 deletions tests/functional/tserialinsecure.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ proc main() {.async.} =
let rawHeaders = headers.rawHeaders()
let strm = client.newClientStream()
with strm:
let headersRef = new(seq[Header])
headersRef[] = headers
await strm.sendHeaders(headersRef, finish = true)
await strm.sendHeaders(headers, finish = true)
var data = newStringref()
await strm.recvHeaders(data)
doAssert data[] == ":status: 200\r\n"
Expand Down
5 changes: 2 additions & 3 deletions tests/functional/tserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ const keyFile = getEnv "HYPERX_TEST_KEYFILE"

proc processStream(strm: ClientStream) {.async.} =
with strm:
let data = newStringRef()
let data = new string
await strm.recvHeaders(data)
if "x-flow-control-check" in data[]:
# let recv buff for a bit
#debugEcho "sleeping"
await sleepAsync(10_000)
await strm.sendHeaders(
newSeqRef(@[(":status", "200")]),
finish = false
@[(":status", "200")], finish = false
)
await strm.sendBody(data, finish = strm.recvEnded)
while not strm.recvEnded:
Expand Down
4 changes: 0 additions & 4 deletions tests/functional/tutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ func newStringRef*(s = ""): ref string =
new result
result[] = s

func newSeqRef*[T](s: seq[T] = @[]): ref seq[T] =
new result
result[] = s

func rawHeaders*(headers: seq[Header]): string =
doAssert headers.len > 0
result = ""
Expand Down

0 comments on commit bf1d845

Please sign in to comment.