[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 function
|
||||
|
||||
' TODO: this function does not take into account multiple ranges.
|
||||
function ParseRange()
|
||||
range = m.headers.range
|
||||
if range <> invalid
|
||||
|
||||
@@ -17,21 +17,19 @@ namespace Http
|
||||
private source = Http.HttpResponseSource.NONE
|
||||
|
||||
' members
|
||||
private buf = invalid
|
||||
private header = invalid
|
||||
private body_buffer = invalid
|
||||
private header_buffer = invalid
|
||||
private path = invalid
|
||||
private request = invalid
|
||||
private id = 0
|
||||
' copy-initializable members
|
||||
private start = 0
|
||||
private length = 0
|
||||
private body_buffer_length = 0
|
||||
private sent = 0
|
||||
private header_sent = 0
|
||||
private header_only = false
|
||||
private buf_start = 0
|
||||
private http_code = 0
|
||||
private mimetype = "text/html" ' for errors
|
||||
private allow_origin
|
||||
|
||||
private server
|
||||
private settings
|
||||
@@ -39,14 +37,11 @@ namespace Http
|
||||
function new(request as HttpRequest, server as HttpServer, settings as HttpSettings)
|
||||
m.log = new log.Logger("HttpResponse")
|
||||
m.id = Http.GetHttpResponseId()
|
||||
m.request = request
|
||||
m.server = server
|
||||
m.settings = settings
|
||||
m.Init(request)
|
||||
end function
|
||||
|
||||
function Init(request as HttpRequest)
|
||||
m.buf = CreateObject("roByteArray")
|
||||
m.request = request
|
||||
m.body_buffer = CreateObject("roByteArray")
|
||||
m.headers = {}
|
||||
end function
|
||||
|
||||
function Process() as boolean
|
||||
@@ -64,22 +59,22 @@ namespace Http
|
||||
errname = WebUtils.HttpTitle(errcode)
|
||||
m.http_code = errcode
|
||||
|
||||
buf = "<html><head><title>" + Stri(errcode).trim() + " " + errname + "</title></head><body>" + UnixNL()
|
||||
buf = buf + "<h1>" + errname + "</h1>" + UnixNL()
|
||||
buf = buf + reason + UnixNL()
|
||||
buf = buf + "<hr>" + UnixNL()
|
||||
buf = buf + m.GeneratedBy() + UnixNL()
|
||||
buf = buf + "</body></html>" + UnixNL()
|
||||
buffer = "<html><head><title>" + Stri(errcode).trim() + " " + errname + "</title></head><body>" + UnixNL()
|
||||
buffer = buffer + "<h1>" + errname + "</h1>" + UnixNL()
|
||||
buffer = buffer + reason + UnixNL()
|
||||
buffer = buffer + "<hr>" + UnixNL()
|
||||
buffer = buffer + m.GeneratedBy() + UnixNL()
|
||||
buffer = buffer + "</body></html>" + UnixNL()
|
||||
|
||||
m.buf.fromasciistring(buf)
|
||||
m.length = m.buf.count()
|
||||
m.SetBodyDataString(buffer)
|
||||
m.ContentType("text/html")
|
||||
|
||||
m.GenerateHeader(true)
|
||||
m.source = Http.HttpResponseSource.GENERATED
|
||||
end function
|
||||
|
||||
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>"
|
||||
m.Default(code, message)
|
||||
end function
|
||||
@@ -87,37 +82,48 @@ namespace Http
|
||||
function Json(data as object)
|
||||
m.http_code = 200
|
||||
|
||||
buf = FormatJson(data)
|
||||
|
||||
m.buf.fromasciistring(buf)
|
||||
m.length = m.buf.count()
|
||||
|
||||
m.mimetype = "application/json"
|
||||
buffer = FormatJson(data)
|
||||
m.SetBodyDataString(buffer)
|
||||
m.ContentType("application/json")
|
||||
|
||||
m.GenerateHeader(true)
|
||||
m.source = Http.HttpResponseSource.GENERATED
|
||||
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
|
||||
sent = -1
|
||||
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.ReadFile(m.path, m.buf_start, bufsize)
|
||||
m.log.info("Read" + Stri(m.buf.count()) + " bytes from source file @" + itostr(m.buf_start))
|
||||
m.body_buffer.ReadFile(m.path, m.buf_start, bufsize)
|
||||
m.log.info("Read" + Stri(m.body_buffer.count()) + " bytes from source file @" + itostr(m.buf_start))
|
||||
end if
|
||||
end if
|
||||
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
|
||||
m.log.info("source buf is empty")
|
||||
m.log.info("source buffer is empty")
|
||||
end if
|
||||
req_remaining = m.length - m.sent
|
||||
req_remaining = m.body_buffer_length - m.sent
|
||||
if buf_remaining > req_remaining
|
||||
buf_remaining = req_remaining
|
||||
end if
|
||||
sent = sock.send(m.buf, buf_pos, buf_remaining)
|
||||
m.LogInfo(sent, m.buf_start + buf_pos, m.length)
|
||||
sent = sock.send(m.body_buffer, buf_pos, buf_remaining)
|
||||
m.LogInfo(sent, m.buf_start + buf_pos, m.body_buffer_length)
|
||||
if sent > 0
|
||||
m.sent = m.sent + sent
|
||||
end if
|
||||
@@ -140,28 +146,22 @@ namespace Http
|
||||
function GenerateHeader(close = false as boolean)
|
||||
code = m.http_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 = m.header + "Date: " + m.Now() + WinNL()
|
||||
m.header = m.header + "Server: " + m.settings.PackageName + WinNL()
|
||||
if isstr(m.location)
|
||||
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()
|
||||
for each header in m.headers
|
||||
m.header = m.header + header + ": " + m.headers[header] + WinNL()
|
||||
end for
|
||||
|
||||
m.header = m.header + WinNL()
|
||||
m.header_length = m.header.len()
|
||||
end function
|
||||
|
||||
function Done() as boolean
|
||||
return m.sent = m.length
|
||||
return m.sent = m.body_buffer_length
|
||||
end function
|
||||
|
||||
function DoneHeader() as boolean
|
||||
@@ -172,8 +172,14 @@ namespace Http
|
||||
m.log.info("Sent" + Stri(recent) + " [" + m.MakeRange(from, recent, total) + "]")
|
||||
end function
|
||||
|
||||
function KeepAlive(close as boolean) as string
|
||||
return close ? "Connection: close" : "Keep-Alive: timeout=" + Stri(m.settings.Idletime).trim()
|
||||
function KeepAlive(close as boolean)
|
||||
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
|
||||
|
||||
function Now() as string
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Http
|
||||
m.All("*", function(context as object) as boolean
|
||||
router = context.router
|
||||
response = context.response
|
||||
response.allow_origin = router.origin
|
||||
response.headers["Access-Control-Allow-Origin"] = router.origin
|
||||
return false
|
||||
end function)
|
||||
end function
|
||||
|
||||
@@ -41,13 +41,14 @@ namespace Http
|
||||
response.Default(403, "Forbidden")
|
||||
return false
|
||||
end if
|
||||
response.mimetype = "text/html"
|
||||
response.ContentType("text/html")
|
||||
router.GenerateDirectoryListing(path, router, request, response)
|
||||
return true
|
||||
else if stat.type = "file"
|
||||
response.mimetype = WebUtils.MimeType(decoded_path)
|
||||
mimetype = WebUtils.MimeType(decoded_path)
|
||||
response.ContentType(mimetype)
|
||||
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)
|
||||
return true
|
||||
else
|
||||
@@ -90,15 +91,19 @@ namespace Http
|
||||
end if
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
response.length = response.fileLength
|
||||
response.body_buffer_length = response.fileLength
|
||||
response.ContentLength(response.body_buffer_length)
|
||||
response.http_code = 200
|
||||
end if
|
||||
|
||||
response.headers["Accept-Ranges"] = "bytes"
|
||||
response.GenerateHeader()
|
||||
return true
|
||||
end function
|
||||
@@ -166,9 +171,7 @@ namespace Http
|
||||
listing = listing + "</body>" + UnixNL()
|
||||
listing = listing + "</html>" + UnixNL()
|
||||
|
||||
response.buf.FromAsciiString(listing)
|
||||
response.length = response.buf.count()
|
||||
|
||||
response.SetBodyDataString(listing)
|
||||
response.http_code = 200
|
||||
response.GenerateHeader()
|
||||
response.source = Http.HttpResponseSource.GENERATED
|
||||
|
||||
Reference in new issue
Block a user