import "HttpRequestState.bs" import "pkg:/components/Web/WebServer/Http/HttpConnection.bs" import "pkg:/components/Web/WebServer/Http/HttpUtils.bs" import "pkg:/source/utils/Logging.bs" import "pkg:/source/utils/Types.bs" import "pkg:/source/utils/UrlUtils.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 ' members private method = invalid private uri = invalid private route = invalid private query = invalid private protocol = invalid private encoder = 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 ready_for_response = false function new() m.id = Http.GetHttpRequestId() m.headers = CreateObject("roAssociativeArray") end function function FromParsedData(method as string, uri as string, headers as object, body as string) m.method = method m.uri = uri m.ProcessOperation() m.headers = headers m.body = body m.state = Http.HttpRequestState.DONE end function function Add(incoming as string, maybeUnicode as boolean) if IsString(m.buffer) m.buffer = m.buffer + incoming m.maybe_unicode = m.maybe_unicode or maybeUnicode else m.buffer = incoming m.maybe_unicode = maybeUnicode 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 LogError(`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 = "" LogError("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]) LogInfo(`${m.method} '${m.uri}'`) else LogError("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 IsString(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 ' TODO:P2 send a "411 Length Required" response LogError("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 IsString(m.body) return false end if if m.body.len() > contentLength LogError("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 if not m.ready_for_response and m.maybe_unicode = true ' If we have a body smaller than Content-Length, there are two possibliities: ' 1. The body is not yet complete ' 2. The body is complete, but the body has Unicode characters, causing the string length ' to be smaller than the byte length. ' ' When that happens, unfortunately, we have to convert the string to a byte array just ' to get the byte length. ' ' The alternative is to receive data as bytes in the first place (as we should) but this makes ' 1. parsing method, url, and headers slightly worse ' 2. requires us to convert most things to strings anyway ' 3. there are bugs https://community.roku.com/t5/Roku-Developer-Program/Help-with-roStreamSocket-data/td-p/249286 ' ' Currently there's no indication that this has terrible performance (since it's rare that we upload unicode ' text, at least for Playlet) and payloads are usually so small that this encoding thing will either not happen, ' or will happen only once. The small payload size also means that the performance impact is minimal. ' ' Finally, we use the flag `maybe_unicode` so we're less wasteful doing this encoding. if m.encoder = invalid m.encoder = CreateObject("roByteArray") end if m.encoder.FromAsciiString(m.body) if m.encoder.Count() = contentLength m.ready_for_response = true end if end if return m.ready_for_response end if end function function ProcessHeaders(connection as HttpConnection) 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 = UrlUtils.ParseQueryComponents(m.uri) else m.query = {} 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, `\r\n`) 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 '`\r\n`.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