[Refactor] web server header cleanups (#16)
* Rename variables * Content type headers * Headers cleanup
This commit is contained in:
1 parent
27b4ad90d3
commit
1734425b9c
4 files changed
+72
-62
No files matched your search
@@ -182,6 +182,7 @@ namespace Http
|
|||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
' TODO: this function does not take into account multiple ranges.
|
||||||
function ParseRange()
|
function ParseRange()
|
||||||
range = m.headers.range
|
range = m.headers.range
|
||||||
if range <> invalid
|
if range <> invalid
|
||||||
|
|||||||
@@ -17,21 +17,19 @@ namespace Http
|
|||||||
private source = Http.HttpResponseSource.NONE
|
private source = Http.HttpResponseSource.NONE
|
||||||
|
|
||||||
' members
|
' members
|
||||||
private buf = invalid
|
private body_buffer = invalid
|
||||||
private header = invalid
|
private header_buffer = invalid
|
||||||
private path = invalid
|
private path = invalid
|
||||||
private request = invalid
|
private request = invalid
|
||||||
private id = 0
|
private id = 0
|
||||||
' copy-initializable members
|
' copy-initializable members
|
||||||
private start = 0
|
private start = 0
|
||||||
private length = 0
|
private body_buffer_length = 0
|
||||||
private sent = 0
|
private sent = 0
|
||||||
private header_sent = 0
|
private header_sent = 0
|
||||||
private header_only = false
|
private header_only = false
|
||||||
private buf_start = 0
|
private buf_start = 0
|
||||||
private http_code = 0
|
private http_code = 0
|
||||||
private mimetype = "text/html" ' for errors
|
|
||||||
private allow_origin
|
|
||||||
|
|
||||||
private server
|
private server
|
||||||
private settings
|
private settings
|
||||||
@@ -39,14 +37,11 @@ namespace Http
|
|||||||
function new(request as HttpRequest, server as HttpServer, settings as HttpSettings)
|
function new(request as HttpRequest, server as HttpServer, settings as HttpSettings)
|
||||||
m.log = new log.Logger("HttpResponse")
|
m.log = new log.Logger("HttpResponse")
|
||||||
m.id = Http.GetHttpResponseId()
|
m.id = Http.GetHttpResponseId()
|
||||||
|
m.request = request
|
||||||
m.server = server
|
m.server = server
|
||||||
m.settings = settings
|
m.settings = settings
|
||||||
m.Init(request)
|
m.body_buffer = CreateObject("roByteArray")
|
||||||
end function
|
m.headers = {}
|
||||||
|
|
||||||
function Init(request as HttpRequest)
|
|
||||||
m.buf = CreateObject("roByteArray")
|
|
||||||
m.request = request
|
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Process() as boolean
|
function Process() as boolean
|
||||||
@@ -64,22 +59,22 @@ namespace Http
|
|||||||
errname = WebUtils.HttpTitle(errcode)
|
errname = WebUtils.HttpTitle(errcode)
|
||||||
m.http_code = errcode
|
m.http_code = errcode
|
||||||
|
|
||||||
buf = "<html><head><title>" + Stri(errcode).trim() + " " + errname + "</title></head><body>" + UnixNL()
|
buffer = "<html><head><title>" + Stri(errcode).trim() + " " + errname + "</title></head><body>" + UnixNL()
|
||||||
buf = buf + "<h1>" + errname + "</h1>" + UnixNL()
|
buffer = buffer + "<h1>" + errname + "</h1>" + UnixNL()
|
||||||
buf = buf + reason + UnixNL()
|
buffer = buffer + reason + UnixNL()
|
||||||
buf = buf + "<hr>" + UnixNL()
|
buffer = buffer + "<hr>" + UnixNL()
|
||||||
buf = buf + m.GeneratedBy() + UnixNL()
|
buffer = buffer + m.GeneratedBy() + UnixNL()
|
||||||
buf = buf + "</body></html>" + UnixNL()
|
buffer = buffer + "</body></html>" + UnixNL()
|
||||||
|
|
||||||
m.buf.fromasciistring(buf)
|
m.SetBodyDataString(buffer)
|
||||||
m.length = m.buf.count()
|
m.ContentType("text/html")
|
||||||
|
|
||||||
m.GenerateHeader(true)
|
m.GenerateHeader(true)
|
||||||
m.source = Http.HttpResponseSource.GENERATED
|
m.source = Http.HttpResponseSource.GENERATED
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Redirect(where as string, code = 301 as integer)
|
function Redirect(where as string, code = 301 as integer)
|
||||||
m.location = where
|
m.headers["Location"] = where
|
||||||
message = "Moved to: <a href=" + Quote() + where + Quote() + ">" + where + "</a>"
|
message = "Moved to: <a href=" + Quote() + where + Quote() + ">" + where + "</a>"
|
||||||
m.Default(code, message)
|
m.Default(code, message)
|
||||||
end function
|
end function
|
||||||
@@ -87,37 +82,48 @@ namespace Http
|
|||||||
function Json(data as object)
|
function Json(data as object)
|
||||||
m.http_code = 200
|
m.http_code = 200
|
||||||
|
|
||||||
buf = FormatJson(data)
|
buffer = FormatJson(data)
|
||||||
|
m.SetBodyDataString(buffer)
|
||||||
m.buf.fromasciistring(buf)
|
m.ContentType("application/json")
|
||||||
m.length = m.buf.count()
|
|
||||||
|
|
||||||
m.mimetype = "application/json"
|
|
||||||
|
|
||||||
m.GenerateHeader(true)
|
m.GenerateHeader(true)
|
||||||
m.source = Http.HttpResponseSource.GENERATED
|
m.source = Http.HttpResponseSource.GENERATED
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function SetBodyDataString(data as string)
|
||||||
|
m.body_buffer.fromasciistring(data)
|
||||||
|
m.body_buffer_length = m.body_buffer.count()
|
||||||
|
m.ContentLength(m.body_buffer_length)
|
||||||
|
end function
|
||||||
|
|
||||||
|
function ContentLength(length as integer)
|
||||||
|
m.headers["Content-Length"] = Stri(length).trim()
|
||||||
|
end function
|
||||||
|
|
||||||
|
function ContentType(mimeType as string)
|
||||||
|
m.headers["Content-Type"] = mimeType
|
||||||
|
end function
|
||||||
|
|
||||||
function Send(sock as object, bufsize as integer) as integer
|
function Send(sock as object, bufsize as integer) as integer
|
||||||
sent = -1
|
sent = -1
|
||||||
if m.source = Http.HttpResponseSource.FROMFILE
|
if m.source = Http.HttpResponseSource.FROMFILE
|
||||||
if m.start + m.sent >= m.buf_start + m.buf.count()
|
if m.start + m.sent >= m.buf_start + m.body_buffer.count()
|
||||||
m.buf_start = m.start + m.sent
|
m.buf_start = m.start + m.sent
|
||||||
m.buf.ReadFile(m.path, m.buf_start, bufsize)
|
m.body_buffer.ReadFile(m.path, m.buf_start, bufsize)
|
||||||
m.log.info("Read" + Stri(m.buf.count()) + " bytes from source file @" + itostr(m.buf_start))
|
m.log.info("Read" + Stri(m.body_buffer.count()) + " bytes from source file @" + itostr(m.buf_start))
|
||||||
end if
|
end if
|
||||||
end if
|
end if
|
||||||
buf_pos = m.start + m.sent - m.buf_start
|
buf_pos = m.start + m.sent - m.buf_start
|
||||||
buf_remaining = m.buf.count() - buf_pos
|
buf_remaining = m.body_buffer.count() - buf_pos
|
||||||
if buf_remaining = 0
|
if buf_remaining = 0
|
||||||
m.log.info("source buf is empty")
|
m.log.info("source buffer is empty")
|
||||||
end if
|
end if
|
||||||
req_remaining = m.length - m.sent
|
req_remaining = m.body_buffer_length - m.sent
|
||||||
if buf_remaining > req_remaining
|
if buf_remaining > req_remaining
|
||||||
buf_remaining = req_remaining
|
buf_remaining = req_remaining
|
||||||
end if
|
end if
|
||||||
sent = sock.send(m.buf, buf_pos, buf_remaining)
|
sent = sock.send(m.body_buffer, buf_pos, buf_remaining)
|
||||||
m.LogInfo(sent, m.buf_start + buf_pos, m.length)
|
m.LogInfo(sent, m.buf_start + buf_pos, m.body_buffer_length)
|
||||||
if sent > 0
|
if sent > 0
|
||||||
m.sent = m.sent + sent
|
m.sent = m.sent + sent
|
||||||
end if
|
end if
|
||||||
@@ -140,28 +146,22 @@ namespace Http
|
|||||||
function GenerateHeader(close = false as boolean)
|
function GenerateHeader(close = false as boolean)
|
||||||
code = m.http_code
|
code = m.http_code
|
||||||
title = WebUtils.HttpTitle(code)
|
title = WebUtils.HttpTitle(code)
|
||||||
|
|
||||||
|
m.headers["Date"] = m.Now()
|
||||||
|
m.headers["Server"] = m.settings.PackageName
|
||||||
|
m.KeepAlive(close)
|
||||||
|
|
||||||
m.header = "HTTP/1.1" + Stri(code) + " " + title + WinNL()
|
m.header = "HTTP/1.1" + Stri(code) + " " + title + WinNL()
|
||||||
m.header = m.header + "Date: " + m.Now() + WinNL()
|
for each header in m.headers
|
||||||
m.header = m.header + "Server: " + m.settings.PackageName + WinNL()
|
m.header = m.header + header + ": " + m.headers[header] + WinNL()
|
||||||
if isstr(m.location)
|
end for
|
||||||
m.header = m.header + "Location: " + m.location + WinNL()
|
|
||||||
end if
|
|
||||||
if m.allow_origin <> invalid
|
|
||||||
m.header = m.header + "Access-Control-Allow-Origin: " + m.allow_origin + WinNL()
|
|
||||||
end if
|
|
||||||
m.header = m.header + m.KeepAlive(close) + WinNL()
|
|
||||||
m.header = m.header + "Content-Length:" + Stri(m.length) + WinNL()
|
|
||||||
if code = 206
|
|
||||||
m.header = m.header + "Content-Range: bytes" + m.MakeRange(m.start, m.length, m.filelength) + WinNL()
|
|
||||||
end if
|
|
||||||
m.header = m.header + "Content-Type: " + m.mimetype + WinNL()
|
|
||||||
m.header = m.header + "Accept-Ranges: bytes" + WinNL()
|
|
||||||
m.header = m.header + WinNL()
|
m.header = m.header + WinNL()
|
||||||
m.header_length = m.header.len()
|
m.header_length = m.header.len()
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Done() as boolean
|
function Done() as boolean
|
||||||
return m.sent = m.length
|
return m.sent = m.body_buffer_length
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function DoneHeader() as boolean
|
function DoneHeader() as boolean
|
||||||
@@ -172,8 +172,14 @@ namespace Http
|
|||||||
m.log.info("Sent" + Stri(recent) + " [" + m.MakeRange(from, recent, total) + "]")
|
m.log.info("Sent" + Stri(recent) + " [" + m.MakeRange(from, recent, total) + "]")
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function KeepAlive(close as boolean) as string
|
function KeepAlive(close as boolean)
|
||||||
return close ? "Connection: close" : "Keep-Alive: timeout=" + Stri(m.settings.Idletime).trim()
|
if close
|
||||||
|
m.headers["Connection"] = "close"
|
||||||
|
m.headers.Delete("Keep-Alive")
|
||||||
|
else
|
||||||
|
m.headers["Keep-Alive"] = "timeout=" + Stri(m.settings.Idletime).trim()
|
||||||
|
m.headers.Delete("Connection")
|
||||||
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Now() as string
|
function Now() as string
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ namespace Http
|
|||||||
m.All("*", function(context as object) as boolean
|
m.All("*", function(context as object) as boolean
|
||||||
router = context.router
|
router = context.router
|
||||||
response = context.response
|
response = context.response
|
||||||
response.allow_origin = router.origin
|
response.headers["Access-Control-Allow-Origin"] = router.origin
|
||||||
return false
|
return false
|
||||||
end function)
|
end function)
|
||||||
end function
|
end function
|
||||||
|
|||||||
@@ -41,13 +41,14 @@ namespace Http
|
|||||||
response.Default(403, "Forbidden")
|
response.Default(403, "Forbidden")
|
||||||
return false
|
return false
|
||||||
end if
|
end if
|
||||||
response.mimetype = "text/html"
|
response.ContentType("text/html")
|
||||||
router.GenerateDirectoryListing(path, router, request, response)
|
router.GenerateDirectoryListing(path, router, request, response)
|
||||||
return true
|
return true
|
||||||
else if stat.type = "file"
|
else if stat.type = "file"
|
||||||
response.mimetype = WebUtils.MimeType(decoded_path)
|
mimetype = WebUtils.MimeType(decoded_path)
|
||||||
|
response.ContentType(mimetype)
|
||||||
response.fileLength = stat.size
|
response.fileLength = stat.size
|
||||||
router.log.info("uri=" + request.uri + ", target=" + path + ", content-type=" + response.mimetype + ", content-length=" + Stri(response.fileLength).trim())
|
router.log.info("uri=" + request.uri + ", target=" + path + ", content-type=" + mimetype + ", content-length=" + Stri(response.fileLength).trim())
|
||||||
router.GenerateFile(path, router, request, response)
|
router.GenerateFile(path, router, request, response)
|
||||||
return true
|
return true
|
||||||
else
|
else
|
||||||
@@ -90,15 +91,19 @@ namespace Http
|
|||||||
end if
|
end if
|
||||||
|
|
||||||
response.start = start
|
response.start = start
|
||||||
response.length = finish - start + 1
|
response.body_buffer_length = finish - start + 1
|
||||||
|
response.ContentLength(response.body_buffer_length)
|
||||||
|
response.headers["Content-Range"] = "bytes " + response.MakeRange(response.start, response.body_buffer_length, response.filelength)
|
||||||
response.http_code = 206
|
response.http_code = 206
|
||||||
|
|
||||||
router.log.info("sending range " + response.MakeRange(start, response.length, response.fileLength))
|
router.log.info("sending range " + response.MakeRange(start, response.body_buffer_length, response.fileLength))
|
||||||
else ' no range stuff
|
else ' no range stuff
|
||||||
response.length = response.fileLength
|
response.body_buffer_length = response.fileLength
|
||||||
|
response.ContentLength(response.body_buffer_length)
|
||||||
response.http_code = 200
|
response.http_code = 200
|
||||||
end if
|
end if
|
||||||
|
|
||||||
|
response.headers["Accept-Ranges"] = "bytes"
|
||||||
response.GenerateHeader()
|
response.GenerateHeader()
|
||||||
return true
|
return true
|
||||||
end function
|
end function
|
||||||
@@ -166,9 +171,7 @@ namespace Http
|
|||||||
listing = listing + "</body>" + UnixNL()
|
listing = listing + "</body>" + UnixNL()
|
||||||
listing = listing + "</html>" + UnixNL()
|
listing = listing + "</html>" + UnixNL()
|
||||||
|
|
||||||
response.buf.FromAsciiString(listing)
|
response.SetBodyDataString(listing)
|
||||||
response.length = response.buf.count()
|
|
||||||
|
|
||||||
response.http_code = 200
|
response.http_code = 200
|
||||||
response.GenerateHeader()
|
response.GenerateHeader()
|
||||||
response.source = Http.HttpResponseSource.GENERATED
|
response.source = Http.HttpResponseSource.GENERATED
|
||||||
|
|||||||
Reference in new issue
Block a user