Skip to content

Commit

Permalink
update server date every seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed Aug 4, 2023
1 parent e70a8e6 commit dee2e46
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/scorper/http/streamserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
##
## Copyright (c) 2020 Bung

import chronos
import chronos except Moment
import mofuparser
import npeg/codegen
import urlencodedparser, multipartparser, acceptparser, rangeparser, oids, httpform, httpdate, httpcore, router,
Expand All @@ -19,7 +19,7 @@ include constant
import std / [os, streams, options, strformat, json, sequtils, parseutils, strutils, macros, macrocache]
import ./ rxnim / rxnim
import segfaults
from std/times import Time, parseTime, utc, `<`, now, `$`
from std/times import Time, parseTime, utc, `<`, now, `$`, initDuration
import zippy
from httprequest import Request
import ../ scorpermacros
Expand Down Expand Up @@ -106,10 +106,12 @@ proc formatCommon*(r: ImpRequest, status: HttpCode, size: int): string =
let remoteUser = os.getEnv("REMOTE_USER", "-")
result = fmt"""{r.hostname} - {remoteUser} {$now()} "{r.meth} {r.url.path} HTTP/{r.protocol.major}.{r.protocol.minor}" {status} {size}"""

var serverDate {.threadvar.}: string

proc genericHeaders(headers = newHttpHeaders()): lent HttpHeaders {.tags: [TimeEffect].} =
## genericHeaders contains Date,X-Frame-Options
# Date: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.18
headers.Date httpDate()
headers.Date serverDate
headers.XFrameOptions "SAMEORIGIN"
when HttpServer.len > 0:
headers.Server HttpServer
Expand Down Expand Up @@ -841,6 +843,15 @@ template processClientImpl(isSecurity: bool): proc (server: StreamServer, transp
else:
req.reader = req.transp.newAsyncStreamReader
req.writer = req.transp.newAsyncStreamWriter
var pipeContinuation: proc (udata: pointer) {.gcsafe, raises: [].}
const Du = chronos.microseconds(1000)
pipeContinuation= proc (udata: pointer) {.gcsafe, raises: [].} =
serverDate = httpDate()
discard setTimer(Moment.fromNow(Du),
pipeContinuation, nil)
serverDate = httpDate()
discard setTimer(Moment.fromNow(Du),
pipeContinuation, nil)
while not transp.atEof():
let retry = await processRequest(req.server, req, isSecurity)
handlePostProcessMiddlewares(req)
Expand Down
2 changes: 1 addition & 1 deletion tests/tacceptparser.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ./scorper/http/acceptparser
import scorper/http/acceptparser
import sequtils
block basic:
let parser = accpetParser()
Expand Down
10 changes: 5 additions & 5 deletions tests/tauth.nim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

import ./scorper/http/streamserver
import ./scorper/http/httprequest
import ./scorper/http/streamclient
import ./scorper/http/router
import ./scorper/http/httpcore, chronos
import scorper/http/streamserver
import scorper/http/httprequest
import scorper/http/streamclient
import scorper/http/router
import scorper/http/httpcore, chronos
import tables
import strformat
import base64
Expand Down

0 comments on commit dee2e46

Please sign in to comment.