Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Mar 24, 2024
1 parent 3d95d42 commit 4fc815b
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 91 deletions.
7 changes: 2 additions & 5 deletions src/hyperx/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ else:

type
Stream = object
# XXX: add body stream, if set stream data through it
id: StreamId
state: StreamState
msgs: QueueAsync[Frame]
Expand Down Expand Up @@ -249,7 +248,7 @@ func doTransitionSend(s: var Stream, frm: Frame) {.raises: [].} =
doAssert s.state != strmInvalid

# XXX continuations need a mechanism
# similar to streamBody i.e: if frm without end is
# similar to a stream i.e: if frm without end is
# found consume from streamContinuations
proc write(client: ClientContext, frm: Frame) {.async.} =
## Frames passed cannot be reused because they are references
Expand Down Expand Up @@ -380,8 +379,6 @@ proc read(client: ClientContext, frm: Frame) {.async.} =
payloadLen -= paddingLen
check isValidSize(frm, payloadLen), newConnError(errFrameSizeError)
if payloadLen > 0:
# XXX recv into Stream.bodyStream if typ is frmtData
# XXX recv in chunks and discard if stream does not exists
frm.grow payloadLen
check not client.sock.isClosed, newConnClosedError()
let payloadRln = await client.sock.recvInto(
Expand Down Expand Up @@ -863,8 +860,8 @@ proc request(
)
let body = new string
body[] = ""
body[].add data
if data.len > 0:
body[].add data
await strm.sendBody(body, finish = true)
body[].setLen 0
await strm.recvHeaders(body)
Expand Down
86 changes: 0 additions & 86 deletions src/hyperx/server.nim
Original file line number Diff line number Diff line change
@@ -1,89 +1,3 @@
## HTTP/2 server
## WIP

type
Header = object
s: string
name, value: Slice[int]

proc toHeader(n, v: string): Header {.compiletime.} =
Header(
s: n & ": " & v & "\r\L",
name: 0 .. n.len-1,
value: n.len .. n.len+v.len-1)

type
Response* = object
s: string
hline: Slice[int]
headers: seq[Slice[int]]
content: Slice[int]

proc add(resp: var Response, h: Header) =
resp.s.add(h.s)
resp.headers.add(h.name)
resp.headers.add(h.value)

proc add*(resp: var Response, hl: string) =
assert resp.headers.len == 0
resp.headers.add(resp.s.len .. resp.s.len+hl.len)
resp.s.add(hl)
resp.s.add("\r\L")

proc add*(resp: var Response, n, v: string) =
resp.headers.add(resp.s.len .. resp.s.len+n.len)
resp.s.add(n)
resp.s.add(": ")
resp.headers.add(resp.s.len .. resp.s.len+v.len)
resp.s.add(v)
resp.s.add("\r\L")

proc terminate(resp: var Response)
resp.s.add("\r\L")

proc write(resp: Response) =
resp.terminate()
write(resp.s)

proc isUpgrade(req: Request) =
# s3.2 HTTP/1.1 request MUST include exactly one HTTP2-Settings
req.has("Connection", "Upgrade") and
req.count("HTTP2-Settings") == 1

type
ReqCode = enum
rcExit
rcUpgrade

proc processRequests2(
req: var Request,
resp: var Response) =
## Process HTTP/2 Request
discard

proc processRequests11(
req: var Request,
resp: var Response): ReqCode =
## Process HTTP/1 Request
result = rcExit
if req.headers.get("Upgrade") == "h2":
# TLS is negotiated in another way (s3.3)
return
if req.isUpgrade:
resp.add("HTTP/1.1 101 Switching Protocols")
resp.add(("Connection", "Upgrade").toHeader)
resp.add(("Upgrade", "h2c").toHeader)
resp.write()
# response to original req must be http2
return rcUpgrade

# dispatch url, etc

proc processRequests(req: var Request) =
var rc = rcExit
if req.isHttp1:
rc = req.processRequests11()
if rc == rcExit:
return
assert req.isHttp2 or rc == rcUpgrade
req.processRequests2()

0 comments on commit 4fc815b

Please sign in to comment.