Add Etag support (#151)

* Add Etag support

* small tweak
This commit is contained in:
Brahim Hadriche authored and GitHub committed 2023-09-23 18:22:23 -04:00
1 parent dab6dc5bc4
commit c5680f7b06
10 files changed
+222 -118

No files matched your search

@@ -24,10 +24,6 @@ namespace Http
private state = Http.HttpRequestState.INIT
private id = 0
' copied members
private range_begin = 0
private range_end = 0
private range_begin_given = false
private range_end_given = false
private ready_for_response = false
function new()
@@ -197,7 +193,6 @@ namespace Http
end function
function ProcessHeaders(connection as HttpConnection)
m.ParseRange()
m.ParseConnection(connection)
end function
@@ -212,28 +207,6 @@ namespace Http
end if
end function
' TODO:P2 this function does not take into account multiple ranges.
function ParseRange()
range = m.headers.range
if range <> invalid
endpoints = lcase(range).tokenize("=")
if endpoints.count() = 2 and endpoints.GetHead() = "bytes"
range = endpoints.GetTail().Trim()
hyphen = range.instr("-")
if hyphen > 0
m.range_begin = strtoi(range.left(hyphen))
m.range_begin_given = true
end if
last = range.len() - 1
if hyphen < last
m.range_end = strtoi(range.right(last - hyphen))
m.range_end_given = true
end if
end if
LogInfo("range request begin" + Stri(m.range_begin) + " end" + Stri(m.range_end))
end if
end function
function ParseConnection(connection as HttpConnection)
connectionStr = m.headers.connection
if connectionStr <> invalid
@@ -1,6 +1,5 @@
import "pkg:/source/utils/TimeUtils.bs"
import "pkg:/source/utils/StringUtils.bs"
import "HttpResponseSource.bs"
namespace Http
function GetHttpResponseId() as integer
@@ -9,8 +8,6 @@ namespace Http
end function
class HttpResponse
private source = Http.HttpResponseSource.NONE
' members
private body_buffer = invalid
private header_buffer = invalid
@@ -42,7 +39,14 @@ namespace Http
routers = m.server.routers
for each router in routers
if router.OnRequest(m.request, m, connection)
return true
exit for
end if
end for
postRouters = m.server.postRouters
for each router in postRouters
if router.OnRequest(m.request, m, connection)
exit for
end if
end for
@@ -62,8 +66,6 @@ namespace Http
m.SetBodyDataString(buffer)
m.ContentType("text/html")
m.source = Http.HttpResponseSource.GENERATED
end function
function Redirect(where as string, code = 302 as integer)
@@ -78,8 +80,6 @@ namespace Http
buffer = FormatJson(data)
m.SetBodyDataString(buffer)
m.ContentType("application/json")
m.source = Http.HttpResponseSource.GENERATED
end function
function SetBodyDataBuffer(buffer as object)
@@ -94,6 +94,20 @@ namespace Http
m.ContentLength(m.body_buffer_length)
end function
function SetBodyDataFile(path as string)
m.body_buffer.ReadFile(path)
m.body_buffer_length = m.body_buffer.count()
m.ContentLength(m.body_buffer_length)
end function
function ClearBody(clearContentLength = true as boolean)
m.body_buffer.clear()
m.body_buffer_length = 0
if clearContentLength
m.ContentLength(0)
end if
end function
function ContentLength(length as integer)
m.headers["Content-Length"] = Stri(length).trim()
end function
@@ -104,13 +118,6 @@ namespace Http
function Send(sock as object, bufsize as integer) as integer
sent = -1
if m.source = Http.HttpResponseSource.FROMFILE
if m.start + m.sent >= m.buf_start + m.body_buffer.count()
m.buf_start = m.start + m.sent
m.body_buffer.ReadFile(m.path, m.buf_start, bufsize)
LogDebug(`Read ${m.body_buffer.count()} bytes from source file @${m.buf_start}`)
end if
end if
buf_pos = m.start + m.sent - m.buf_start
buf_remaining = m.body_buffer.count() - buf_pos
if buf_remaining = 0
@@ -1,9 +0,0 @@
namespace Http
enum HttpResponseSource
NONE = 0
GENERATED = 1
FROMFILE = 2
end enum
end namespace
@@ -18,6 +18,7 @@ namespace Http
private total_out = 0
private routers
private postRouters
public success as boolean
public settings as Http.HttpSettings
@@ -27,6 +28,7 @@ namespace Http
m.settings = settings
m.task = task
m.routers = []
m.postRouters = []
m.utils = new Utils()
m.success = m.Init()
end function
@@ -76,6 +78,10 @@ namespace Http
m.routers.Push(router)
end function
function UsePostRouter(router as HttpRouter)
m.postRouters.Push(router)
end function
function PreWait()
connections = m.connections
for each id in connections