Skip to content

Commit

Permalink
Merge pull request HapticX#354 from HapticX/dev
Browse files Browse the repository at this point in the history
try downgrade
  • Loading branch information
Ethosa authored Oct 5, 2024
2 parents a7949f9 + aaf0440 commit 749d77d
Showing 1 changed file with 8 additions and 189 deletions.
197 changes: 8 additions & 189 deletions src/happyx/ssr/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -246,125 +246,11 @@ macro `.`*(obj: JsonNode, field: untyped): JsonNode =
newCall("[]", obj, newLit($field.toStrLit))


const defaultHeaders = "Content-Type: text/plain;charset=utf-8"


template answer*(
req: Request,
message: string | int | float | bool | char,
code: HttpCode = Http200
) =
## Answers to the request
##
## ⚠ `Low-level API` ⚠
##
## Arguments:
## `req: Request`: An instance of the Request type, representing the request that we are responding to.
## `message: string`: The message that we want to include in the response body.
## `code: HttpCode = Http200`: The HTTP status code that we want to send in the response.
## This argument is optional, with a default value of Http200 (OK).
##
## Use this example instead
##
## .. code-block::nim
## get "/":
## return "Hello, world!"
##
const useHeaders = declared(outHeaders) or declared(outCookies) or corsRegistered.value > 0
when useHeaders:
var h = newHttpHeaders([
("Content-Type", "text/plain; charset=utf-8")
])
when corsRegistered.value > 0:
when exportJvm or exportPython or defined(napibuild):
when enableHttpBeast or enableHttpx:
addCORSHeaders(req.ip, h)
else:
addCORSHeaders(req.hostname, h)
else:
h.addCORSHeaders()
when declared(outHeaders):
for key, val in outHeaders.pairs():
h[key] = val
# HTTPX
when enableHttpx:
when useHeaders:
var headersArr = ""
for key, value in h.pairs():
headersArr &= key & ':' & value & "\r\n"
when declared(outCookies):
for cookie in outCookies:
headersArr &= cookie & "\r\n"
if headersArr.len > 0:
headersArr.delete(headersArr.len-2..headersArr.len-1)

# check safety requests
when enableSafeRequests:
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
else:
# Use unsafeSend to improve speed
var data: string = "HTTP/1.1 "
when declared(statusCode):
when statusCode is int:
data &= $statusCode
else:
data &= $code
else:
data &= $code
when message is string:
data &= "\c\LContent-Length:" & $len(message)
data &= "\c\L" & (when useHeaders: headersArr else: defaultHeaders) & "\c\L\c\L" & message
else:
data &= "\c\LContent-Length:" & $len($message)
data &= "\c\L" & (when useHeaders: headersArr else: defaultHeaders) & "\c\L\c\L" & $message
req.unsafeSend(data)
# HTTP BEAST
elif enableHttpBeast:
when useHeaders:
var headersArr = ""
for key, value in h.pairs():
headersArr &= key & ':' & value & "\r\n"
when declared(outCookies):
for cookie in outCookies:
headersArr &= cookie & "\r\n"
if headersArr.len > 0:
headersArr.delete(headersArr.len-2..headersArr.len-1)
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
else:
req.send(code, $message, when useHeaders: headersArr else: defaultHeaders)
# ASYNC HTTP SERVER / MICRO ASYNC HTTP SERVER
else:
when useHeaders:
when declared(outCookies):
for cookie in outCookies:
let data = cookie.split(":", 1)
h.add("Set-Cookie", data[1].strip())
else:
let h = newHttpHeaders([("Content-Type", "text/plain;charset=utf-8")])
when declared(statusCode):
when statusCode is int:
await req.respond(statusCode.HttpCode, $message, h)
else:
await req.respond(code, $message, h)
else:
await req.respond(code, $message, h)


template answer*(
req: Request,
message: string | int | float | bool | char,
code: HttpCode = Http200,
headers: HttpHeaders,
headers: HttpHeaders = newHttpHeaders([("Content-Type", "text/plain;charset=utf-8")]),
contentLength: Option[int] = int.none
) =
## Answers to the request
Expand Down Expand Up @@ -395,87 +281,20 @@ template answer*(
when declared(outHeaders):
for key, val in outHeaders.pairs():
h[key] = val
# HTTPX
when enableHttpx:
var headersArr = ""
for key, value in h.pairs():
headersArr &= key & ':' & value & "\c\L"
when declared(outCookies):
for cookie in outCookies:
headersArr &= cookie & "\c\L"
if headersArr.len > 0:
headersArr.delete(headersArr.len-2..headersArr.len-1)
if contentLength.isSome:
# useful for file answers
when enableSafeRequests:
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, contentLength, headersArr)
else:
req.send(code, $message, contentLength, headersArr)
else:
req.send(code, $message, contentLength, headersArr)
else:
# Use unsafeSend to improve speed
var data: string = "HTTP/1.1 "
when declared(statusCode):
when statusCode is int:
data &= $statusCode
else:
data &= $code
else:
data &= $code
when message is string:
data &= "\c\LContent-Length:" & $contentLength.get()
data &= "\c\L" & headersArr & "\c\L\c\L" & message
else:
data &= "\c\LContent-Length:" & $contentLength.get()
data &= "\c\L" & headersArr & "\c\L\c\L" & $message
req.unsafeSend(data)
else:
when enableSafeRequests:
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, headersArr)
else:
req.send(code, $message, headersArr)
else:
req.send(code, $message, headersArr)
else:
# Use unsafeSend to improve speed
var data: string = "HTTP/1.1 "
when declared(statusCode):
when statusCode is int:
data &= $statusCode
else:
data &= $code
else:
data &= $code
when message is string:
data &= "\c\LContent-Length:" & $len(message)
data &= "\c\L" & headersArr & "\c\L\c\L" & message
else:
data &= "\c\LContent-Length:" & $len($message)
data &= "\c\L" & headersArr & "\c\L\c\L" & $message
req.unsafeSend(data)
# HTTP BEAST
elif enableHttpBeast:
var headersArr = ""
when enableHttpx or enableHttpBeast:
var headersArr: seq[string] = @[]
for key, value in h.pairs():
headersArr &= key & ':' & value & "\r\n"
headersArr.add(key & ':' & value)
when declared(outCookies):
for cookie in outCookies:
headersArr &= cookie & "\r\n"
if headersArr.len > 0:
headersArr.delete(headersArr.len-2..headersArr.len-1)
headersArr.add(cookie)
when declared(statusCode):
when statusCode is int:
req.send(statusCode.HttpCode, $message, headersArr)
req.send(statusCode.HttpCode, $message, headersArr.join("\r\n"))
else:
req.send(code, $message, headersArr)
req.send(code, $message, headersArr.join("\r\n"))
else:
req.send(code, $message, headersArr)
# ASYNC HTTP SERVER / MICRO ASYNC HTTP SERVER
req.send(code, $message, headersArr.join("\r\n"))
else:
when declared(outCookies):
for cookie in outCookies:
Expand Down

0 comments on commit 749d77d

Please sign in to comment.