Web-server refactor (#14)
* rename field * Parse body support * Changelog * Parse query params * Error handling for larger than content-length body
This commit is contained in:
1 parent
3f5cb84696
commit
85209cb22e
9 files changed
+187
-75
No files matched your search
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
### Added
|
### Added
|
||||||
- Version check in settings page
|
- Version check in settings page
|
||||||
|
- Support for web request body parsing
|
||||||
|
|
||||||
## [0.4.0] - 2022-11-24
|
## [0.4.0] - 2022-11-24
|
||||||
### Added
|
### Added
|
||||||
|
|||||||
@@ -79,9 +79,9 @@ namespace Http
|
|||||||
' append to connection request
|
' append to connection request
|
||||||
m.request.Add(rcvStr)
|
m.request.Add(rcvStr)
|
||||||
|
|
||||||
if m.request.IsComplete()
|
if m.request.ProcessNext(m)
|
||||||
server.num_requests = validint(server.num_requests) + 1
|
if m.request.state = Http.HttpRequestState.DONE
|
||||||
if m.request.Process(m)
|
server.num_requests = validint(server.num_requests) + 1
|
||||||
m.response = new HttpResponse(m.request, m.server, m.settings)
|
m.response = new HttpResponse(m.request, m.server, m.settings)
|
||||||
m.response.Process()
|
m.response.Process()
|
||||||
m.SetState(Http.HttpConnectionState.SEND_HEADER)
|
m.SetState(Http.HttpConnectionState.SEND_HEADER)
|
||||||
@@ -90,7 +90,7 @@ namespace Http
|
|||||||
m.close = true
|
m.close = true
|
||||||
m.log.error("couldn't process request or response, closing @" + m.client)
|
m.log.error("couldn't process request or response, closing @" + m.client)
|
||||||
end if
|
end if
|
||||||
else if m.request.buf.len() > m.settings.MaxRequestLength ' die if it's too long
|
else if m.request.buffer.len() > m.settings.MaxRequestLength ' die if it's too long
|
||||||
m.response = new HttpResponse(m.request, m.server, m.settings)
|
m.response = new HttpResponse(m.request, m.server, m.settings)
|
||||||
m.response.Default(413, "Your request was dropped because it was too long.")
|
m.response.Default(413, "Your request was dropped because it was too long.")
|
||||||
m.SetState(Http.HttpConnectionState.SEND_HEADER)
|
m.SetState(Http.HttpConnectionState.SEND_HEADER)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import "pkg:/components/WebServer/HttpRequestState.bs"
|
||||||
|
|
||||||
namespace Http
|
namespace Http
|
||||||
|
|
||||||
function GetHttpRequestId() as integer
|
function GetHttpRequestId() as integer
|
||||||
@@ -8,82 +10,172 @@ namespace Http
|
|||||||
class HttpRequest
|
class HttpRequest
|
||||||
private log as log.Logger
|
private log as log.Logger
|
||||||
|
|
||||||
Unix2NewLine = UnixNL() + UnixNL()
|
|
||||||
Windows2NewLine = WinNL() + WinNL()
|
|
||||||
|
|
||||||
' members
|
' members
|
||||||
private method = invalid
|
private method = invalid
|
||||||
private uri = invalid
|
private uri = invalid
|
||||||
|
private query = invalid
|
||||||
private protocol = invalid
|
private protocol = invalid
|
||||||
private buf = invalid
|
private buffer = invalid
|
||||||
private fields = invalid
|
private buffer_index = 0
|
||||||
|
private headers = invalid
|
||||||
|
private body = invalid
|
||||||
|
private state = Http.HttpRequestState.INIT
|
||||||
private id = 0
|
private id = 0
|
||||||
' copied members
|
' copied members
|
||||||
private range_begin = 0
|
private range_begin = 0
|
||||||
private range_end = 0
|
private range_end = 0
|
||||||
private range_begin_given = false
|
private range_begin_given = false
|
||||||
private range_end_given = false
|
private range_end_given = false
|
||||||
private ok = true
|
private ready_for_response = false
|
||||||
|
|
||||||
function new()
|
function new()
|
||||||
m.log = new log.Logger("HttpRequest")
|
m.log = new log.Logger("HttpRequest")
|
||||||
m.id = Http.GetHttpRequestId()
|
m.id = Http.GetHttpRequestId()
|
||||||
m.fields = CreateObject("roAssociativeArray")
|
m.headers = CreateObject("roAssociativeArray")
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Add(incoming as string)
|
function Add(incoming as string)
|
||||||
if isstr(m.buf)
|
if isstr(m.buffer)
|
||||||
m.buf = m.buf + incoming
|
m.buffer = m.buffer + incoming
|
||||||
else
|
else
|
||||||
m.buf = incoming
|
m.buffer = incoming
|
||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function IsComplete() as boolean
|
function ProcessNext(connection as HttpConnection) as boolean
|
||||||
' TODO: this function is correct only if request does not have a body
|
if m.state = Http.HttpRequestState.DONE
|
||||||
' Need read and process line by line
|
return m.ready_for_response
|
||||||
' Once there's an empty line, we've read all headers
|
end if
|
||||||
' Check for Content-Length header, and read that amount of data
|
|
||||||
' If header is missing, error out
|
|
||||||
buf = m.buf
|
|
||||||
complete = isstr(buf) and (right(buf, 2) = m.Unix2NewLine) or (right(buf, 4) = m.Windows2NewLine)
|
|
||||||
'if complete then info(m,"header:"+UnixNL()+buf)
|
|
||||||
return complete
|
|
||||||
|
|
||||||
|
if m.state = Http.HttpRequestState.INIT
|
||||||
|
m.state = Http.HttpRequestState.READING_OPERATION
|
||||||
|
end if
|
||||||
|
|
||||||
|
if m.state = Http.HttpRequestState.READING_OPERATION
|
||||||
|
if m.ParseOperation()
|
||||||
|
m.ProcessOperation()
|
||||||
|
m.state = Http.HttpRequestState.READING_HEADERS
|
||||||
|
end if
|
||||||
|
end if
|
||||||
|
|
||||||
|
if m.state = Http.HttpRequestState.READING_HEADERS
|
||||||
|
if m.ParseHeaders()
|
||||||
|
m.ProcessHeaders(connection)
|
||||||
|
m.state = Http.HttpRequestState.READING_BODY
|
||||||
|
end if
|
||||||
|
end if
|
||||||
|
|
||||||
|
if m.state = Http.HttpRequestState.READING_BODY
|
||||||
|
if m.ParseBody()
|
||||||
|
m.state = Http.HttpRequestState.DONE
|
||||||
|
end if
|
||||||
|
end if
|
||||||
|
|
||||||
|
return m.ready_for_response
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Parse(connection as HttpConnection) as boolean
|
function ParseOperation() as boolean
|
||||||
lines = m.buf.tokenize(WinNL())
|
if m.buffer_index <> 0
|
||||||
operation = lines.RemoveHead()
|
m.log.error(`invalid state: reading operation, but request is at index ${m.buffer_index}`)
|
||||||
if operation <> invalid
|
m.ready_for_response = true
|
||||||
parts = operation.tokenize(" ")
|
return false
|
||||||
if parts.count() = 3
|
end if
|
||||||
m.method = Ucase(parts.RemoveHead())
|
|
||||||
m.uri = parts.RemoveHead()
|
if m.buffer.len() = 0
|
||||||
m.protocol = Ucase(parts.RemoveHead())
|
return false
|
||||||
m.log.info(m.method + " '" + m.uri + "'")
|
end if
|
||||||
for each line in lines
|
|
||||||
av = line.tokenize(": ")
|
operation = m.ReadHeaderLine()
|
||||||
if av.count() = 2
|
|
||||||
m.fields[av.GetHead()] = av.GetTail()
|
if operation = invalid or operation = ""
|
||||||
end if
|
m.log.error("empty request")
|
||||||
end for
|
m.ready_for_response = true
|
||||||
' interpret some fields if present
|
return false
|
||||||
m.ParseRange()
|
end if
|
||||||
m.ParseConnection(connection)
|
|
||||||
|
parts = operation.tokenize(" ")
|
||||||
|
if parts.count() = 3
|
||||||
|
m.method = Ucase(parts[0])
|
||||||
|
m.uri = parts[1]
|
||||||
|
m.protocol = Ucase(parts[2])
|
||||||
|
m.log.info(m.method + " '" + m.uri + "'")
|
||||||
|
else
|
||||||
|
m.log.error("invalid request: " + operation)
|
||||||
|
m.ready_for_response = true
|
||||||
|
end if
|
||||||
|
|
||||||
|
return not m.ready_for_response
|
||||||
|
end function
|
||||||
|
|
||||||
|
function ParseHeaders() as boolean
|
||||||
|
line = m.ReadHeaderLine()
|
||||||
|
while line <> invalid and line <> ""
|
||||||
|
splitIndex = line.InStr(": ")
|
||||||
|
if splitIndex > 0
|
||||||
|
key = line.Left(splitIndex)
|
||||||
|
value = line.Mid(splitIndex + 2)
|
||||||
|
m.headers[key] = value
|
||||||
|
end if
|
||||||
|
line = m.ReadHeaderLine()
|
||||||
|
end while
|
||||||
|
|
||||||
|
' Unfinished line, we wait for the rest
|
||||||
|
if line = invalid
|
||||||
|
return false
|
||||||
|
end if
|
||||||
|
|
||||||
|
' Empty line, we're done with headers
|
||||||
|
if line = ""
|
||||||
|
return true
|
||||||
|
end if
|
||||||
|
|
||||||
|
return not m.ready_for_response
|
||||||
|
end function
|
||||||
|
|
||||||
|
function ParseBody() as boolean
|
||||||
|
data = m.ReadBufferToEnd()
|
||||||
|
if isstr(m.body)
|
||||||
|
m.body = m.body + data
|
||||||
|
else
|
||||||
|
m.body = data
|
||||||
|
end if
|
||||||
|
|
||||||
|
if m.headers["Content-Length"] = invalid
|
||||||
|
m.ready_for_response = true
|
||||||
|
if m.body.len() > 0
|
||||||
|
m.log.error("invalid request: request body found, but no Content-Length header")
|
||||||
|
return false
|
||||||
else
|
else
|
||||||
m.log.error("invalid request: " + operation)
|
return true
|
||||||
m.ok = false
|
|
||||||
end if
|
end if
|
||||||
else
|
else
|
||||||
m.log.error("empty request")
|
contentLength = m.headers["Content-Length"].toInt()
|
||||||
m.ok = false
|
if not isstr(m.body)
|
||||||
|
return false
|
||||||
|
end if
|
||||||
|
|
||||||
|
if m.body.len() > contentLength
|
||||||
|
m.log.error("invalid request: request body is larger than Content-Length")
|
||||||
|
m.ready_for_response = true
|
||||||
|
return false
|
||||||
|
end if
|
||||||
|
|
||||||
|
m.ready_for_response = m.body.len() = contentLength
|
||||||
|
return m.ready_for_response
|
||||||
end if
|
end if
|
||||||
return m.ok
|
end function
|
||||||
|
|
||||||
|
function ProcessHeaders(connection as HttpConnection)
|
||||||
|
m.ParseRange()
|
||||||
|
m.ParseConnection(connection)
|
||||||
|
end function
|
||||||
|
|
||||||
|
function ProcessOperation()
|
||||||
|
m.query = WebUtils.UrlQueryComponents(m.uri)
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function ParseRange()
|
function ParseRange()
|
||||||
range = m.fields.range
|
range = m.headers.range
|
||||||
if range <> invalid
|
if range <> invalid
|
||||||
endpoints = lcase(range).tokenize("=")
|
endpoints = lcase(range).tokenize("=")
|
||||||
if endpoints.count() = 2 and endpoints.GetHead() = "bytes"
|
if endpoints.count() = 2 and endpoints.GetHead() = "bytes"
|
||||||
@@ -104,14 +196,32 @@ namespace Http
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function ParseConnection(connection as HttpConnection)
|
function ParseConnection(connection as HttpConnection)
|
||||||
connectionStr = m.fields.connection
|
connectionStr = m.headers.connection
|
||||||
if connectionStr <> invalid
|
if connectionStr <> invalid
|
||||||
connection.close = (lcase(connectionStr.trim()) = "close")
|
connection.close = (lcase(connectionStr.trim()) = "close")
|
||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Process(connection as HttpConnection) as boolean
|
function ReadHeaderLine() as dynamic
|
||||||
return m.Parse(connection)
|
index = m.buffer.Instr(m.buffer_index, WinNL())
|
||||||
|
if index = -1
|
||||||
|
return invalid
|
||||||
|
end if
|
||||||
|
|
||||||
|
length = index - m.buffer_index
|
||||||
|
line = m.buffer.Mid(m.buffer_index, length)
|
||||||
|
m.buffer_index += length + 2 'WinNL().len() = 2
|
||||||
|
return line
|
||||||
|
end function
|
||||||
|
|
||||||
|
function ReadBufferToEnd() as dynamic
|
||||||
|
if m.buffer_index = m.buffer.len()
|
||||||
|
return ""
|
||||||
|
end if
|
||||||
|
|
||||||
|
data = m.buffer.Mid(m.buffer_index)
|
||||||
|
m.buffer_index += data.len()
|
||||||
|
return data
|
||||||
end function
|
end function
|
||||||
end class
|
end class
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
namespace Http
|
||||||
|
|
||||||
|
enum HttpRequestState
|
||||||
|
INIT = 0
|
||||||
|
READING_OPERATION = 1
|
||||||
|
READING_HEADERS = 2
|
||||||
|
READING_BODY = 3
|
||||||
|
DONE = 4
|
||||||
|
end enum
|
||||||
|
|
||||||
|
end namespace
|
||||||
@@ -173,12 +173,7 @@ namespace Http
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function KeepAlive(close as boolean) as string
|
function KeepAlive(close as boolean) as string
|
||||||
if close
|
return close ? "Connection: close" : "Keep-Alive: timeout=" + Stri(m.settings.Idletime).trim()
|
||||||
base = "Connection: close"
|
|
||||||
else
|
|
||||||
base = "Keep-Alive: timeout=" + Stri(m.settings.Idletime).trim()
|
|
||||||
end if
|
|
||||||
return base
|
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function Now() as string
|
function Now() as string
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ namespace Http
|
|||||||
MessagePort = invalid
|
MessagePort = invalid
|
||||||
MaxConnections = 10
|
MaxConnections = 10
|
||||||
Timeout = 60 * 1000
|
Timeout = 60 * 1000
|
||||||
MaxRequestLength = 4000
|
MaxRequestLength = 2097152
|
||||||
Idletime = 60
|
Idletime = 60
|
||||||
WwwRoot = "pkg:/"
|
WwwRoot = "pkg:/"
|
||||||
IndexName = "index.html"
|
IndexName = "index.html"
|
||||||
|
|||||||
@@ -25,16 +25,15 @@ namespace Http
|
|||||||
route = request.uri
|
route = request.uri
|
||||||
|
|
||||||
if route.StartsWith("/api/command/play")
|
if route.StartsWith("/api/command/play")
|
||||||
components = WebUtils.UrlQueryComponents(route)
|
if request.query["v"] = invalid
|
||||||
if components["v"] = invalid
|
|
||||||
response.Default(400, "Missing arg 'v'")
|
response.Default(400, "Missing arg 'v'")
|
||||||
return true
|
return true
|
||||||
end if
|
end if
|
||||||
|
|
||||||
server.task.top.command = `play:${components["v"]}`
|
server.task.top.command = `play:${request.query["v"]}`
|
||||||
if components["redirect"] = "true"
|
if request.query["redirect"] = "true"
|
||||||
' 302 to prevent redirection to current host permanently
|
' 302 to prevent redirection to current host permanently
|
||||||
response.Redirect(`${InvidiousSettings.GetSelectedInstance()}/watch?v=${components["v"]}`, 302)
|
response.Redirect(`${InvidiousSettings.GetSelectedInstance()}/watch?v=${request.query["v"]}`, 302)
|
||||||
else
|
else
|
||||||
response.Default(204, "OK")
|
response.Default(204, "OK")
|
||||||
end if
|
end if
|
||||||
|
|||||||
@@ -15,11 +15,9 @@ namespace Http
|
|||||||
return false
|
return false
|
||||||
end if
|
end if
|
||||||
|
|
||||||
components = WebUtils.UrlQueryComponents(route)
|
|
||||||
|
|
||||||
instances = []
|
instances = []
|
||||||
if components.DoesExist("instances")
|
if request.query.DoesExist("instances")
|
||||||
instances = components["instances"]
|
instances = request.query["instances"]
|
||||||
if GetInterface(instances, "ifArray") = invalid
|
if GetInterface(instances, "ifArray") = invalid
|
||||||
instances = [instances]
|
instances = [instances]
|
||||||
end if
|
end if
|
||||||
@@ -46,11 +44,10 @@ namespace Http
|
|||||||
return false
|
return false
|
||||||
end if
|
end if
|
||||||
|
|
||||||
components = WebUtils.UrlQueryComponents(route)
|
if request.query["token"] <> invalid and request.query["referer"] <> invalid
|
||||||
if components["token"] <> invalid and components["referer"] <> invalid
|
|
||||||
' Token is encoded twice for some reason
|
' Token is encoded twice for some reason
|
||||||
token = WebUtils.UrlUnescape(components["token"]).DecodeUriComponent()
|
token = WebUtils.UrlUnescape(request.query["token"]).DecodeUriComponent()
|
||||||
referer = WebUtils.UrlUnescape(components["referer"])
|
referer = WebUtils.UrlUnescape(request.query["referer"])
|
||||||
InvidiousSettings.SetAuthToken(token, referer)
|
InvidiousSettings.SetAuthToken(token, referer)
|
||||||
server.task.top.login = true
|
server.task.top.login = true
|
||||||
end if
|
end if
|
||||||
|
|||||||
@@ -20,9 +20,8 @@ namespace Http
|
|||||||
|
|
||||||
keys = ["app", "device", "invidious"]
|
keys = ["app", "device", "invidious"]
|
||||||
|
|
||||||
components = WebUtils.UrlQueryComponents(route)
|
if request.query.DoesExist("key")
|
||||||
if components.DoesExist("key")
|
keys = request.query["key"]
|
||||||
keys = components["key"]
|
|
||||||
if GetInterface(keys, "ifArray") = invalid
|
if GetInterface(keys, "ifArray") = invalid
|
||||||
keys = [keys]
|
keys = [keys]
|
||||||
end if
|
end if
|
||||||
|
|||||||
Reference in new issue
Block a user