Files
playlet-piped/src/components/WebServer/HttpRequest.bs
T

246 lines
7.7 KiB
Plaintext

import "pkg:/components/WebServer/HttpRequestState.bs"
namespace Http
function GetHttpRequestId() as integer
m.http_request_id = validint(m.http_request_id) + 1
return m.http_request_id
end function
class HttpRequest
private log as log.Logger
' members
private method = invalid
private uri = invalid
private route = invalid
private query = invalid
private protocol = invalid
private buffer = invalid
private buffer_index = 0
private headers = invalid
private body = invalid
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()
m.log = new log.Logger("HttpRequest")
m.id = Http.GetHttpRequestId()
m.headers = CreateObject("roAssociativeArray")
end function
function Add(incoming as string)
if isstr(m.buffer)
m.buffer = m.buffer + incoming
else
m.buffer = incoming
end if
end function
function ProcessNext(connection as HttpConnection) as boolean
if m.state = Http.HttpRequestState.DONE
return m.ready_for_response
end if
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
function ParseOperation() as boolean
if m.buffer_index <> 0
m.log.error(`invalid state: reading operation, but request is at index ${m.buffer_index}`)
m.ready_for_response = true
return false
end if
if m.buffer.len() = 0
return false
end if
operation = m.ReadHeaderLine()
if operation = invalid or operation = ""
m.log.error("empty request")
m.ready_for_response = true
return false
end if
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
return true
end if
else
contentLength = m.headers["Content-Length"].toInt()
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 function
function ProcessHeaders(connection as HttpConnection)
m.ParseRange()
m.ParseConnection(connection)
end function
function ProcessOperation()
m.route = m.uri
split = m.route.Instr("?")
if split <> -1
m.route = m.route.Left(split)
m.query = WebUtils.UrlQueryComponents(m.uri)
else
m.query = {}
end if
end function
' TODO: 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
m.log.info("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
connection.close = (lcase(connectionStr.trim()) = "close")
end if
end function
function ReadHeaderLine() as dynamic
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
function Json() as dynamic
if m.body = invalid
return invalid
end if
return ParseJson(m.body)
end function
end class
end namespace