@@ -4,6 +4,7 @@ import times
44import assets/ web as webAssets
55import asyncdispatch
66import httpclient
7+ import httpcore
78import threadpool
89import jester
910import locks
@@ -42,6 +43,18 @@ proc h(message: string): string =
4243proc s (message: string ): string =
4344 message.replace (" '" , " \\ '" ).replace (" \n " , " \\ n" )
4445
46+ proc shouldReturnNotModified * (headers: HttpHeaders , lastUpdate: float ): bool {.gcsafe .} =
47+ if lastUpdate <= 0.0 :
48+ return false
49+ let ifModifiedSince = seq [string ](headers.getOrDefault (" if-modified-since" )).join (" , " )
50+ if ifModifiedSince == " " :
51+ return false
52+ try :
53+ let ifModifiedTime = parse (ifModifiedSince, " ddd, dd MMM yyyy HH:mm:ss 'GMT'" , utc ())
54+ return int64 (lastUpdate) <= ifModifiedTime.toTime ().toUnix ()
55+ except CatchableError :
56+ return false
57+
4558const AUTH_HEADER = " authorization"
4659const AUTH_TYPE = " Bearer"
4760
@@ -125,20 +138,14 @@ router myrouter:
125138 log (%* {" event" : " http" , " get" : request.pathInfo})
126139 {.gcsafe .}: # We're reading immutable globals and png data via a lock. It's fine.
127140 let (sceneId, _, _, lastUpdate) = getLastPublicState ()
128- let ifModifiedSince = seq [string ](request.headers.getOrDefault (" if-modified-since" )).join (" , " )
129- if ifModifiedSince != " " and lastUpdate > 0.0 :
130- try :
131- let ifModifiedTime = parse (ifModifiedSince, " ddd, dd MMM yyyy HH:mm:ss 'GMT'" , utc ())
132- if int64 (lastUpdate) <= ifModifiedTime.toTime ().toUnix ():
133- resp Http304 , [(" X-Scene-Id" , $ sceneId), (" Access-Control-Expose-Headers" , " X-Scene-Id" )], " "
134- return
135- except :
136- discard # Invalid date, proceed
141+ if shouldReturnNotModified (request.headers, lastUpdate):
142+ resp Http304 , [(" X-Scene-Id" , $ sceneId), (" Access-Control-Expose-Headers" , " X-Scene-Id" )], " "
143+ return
137144 var headers = @ [
138- (" Content-Type" , " image/png" ),
139- (" Content-Disposition" , & " inline; filename=\" { sceneId} .png\" " ),
140- (" X-Scene-Id" , $ sceneId),
141- (" Access-Control-Expose-Headers" , " X-Scene-Id" )
145+ (" Content-Type" , " image/png" ),
146+ (" Content-Disposition" , & " inline; filename=\" { sceneId} .png\" " ),
147+ (" X-Scene-Id" , $ sceneId),
148+ (" Access-Control-Expose-Headers" , " X-Scene-Id" )
142149 ]
143150 if lastUpdate > 0.0 :
144151 let lastModified = format (fromUnix (int64 (lastUpdate)), " ddd, dd MMM yyyy HH:mm:ss 'GMT'" , utc ())
0 commit comments