1 parent
cf6d89f44f
commit
190dcdffe3
37 files changed
+417
-291
No files matched your search
@@ -3,14 +3,12 @@ import "HttpRequest.bs"
|
||||
import "HttpResponse.bs"
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
import "pkg:/source/roku_modules/log/LogMixin.brs"
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
|
||||
namespace Http
|
||||
const BUFSIZE = 65536
|
||||
|
||||
class HttpConnection
|
||||
private log as log.Logger
|
||||
|
||||
private total_received = 0
|
||||
private total_sent = 0
|
||||
private id = "<uninit>"
|
||||
@@ -22,7 +20,6 @@ namespace Http
|
||||
private settings as HttpSettings
|
||||
|
||||
function new(server as HttpServer, settings as HttpSettings)
|
||||
m.log = new log.Logger("HttpConnection")
|
||||
m.server = server
|
||||
m.settings = settings
|
||||
m.Reset()
|
||||
@@ -42,14 +39,13 @@ namespace Http
|
||||
m.header = invalid
|
||||
m.response = invalid
|
||||
m.header_only = false
|
||||
m.http_code = 0
|
||||
m.close = true
|
||||
m.ws_upgrade = false
|
||||
m.SetState(Http.HttpConnectionState.INIT)
|
||||
end function
|
||||
|
||||
function Recycle()
|
||||
m.log.info("recycle")
|
||||
LogInfo("recycle")
|
||||
m.Restart()
|
||||
m.SetState(Http.HttpConnectionState.RECV_REQUEST) ' ready for another
|
||||
end function
|
||||
@@ -60,7 +56,7 @@ namespace Http
|
||||
m.SetState(Http.HttpConnectionState.RECV_REQUEST)
|
||||
m.client = m.socket.getReceivedFromAddress().getAddress()
|
||||
|
||||
m.log.info("accepted connection @" + m.client)
|
||||
LogInfo("accepted connection @" + m.client)
|
||||
|
||||
' try to read right away rather than going through another iteration
|
||||
' of the poll loop.
|
||||
@@ -69,7 +65,7 @@ namespace Http
|
||||
|
||||
function PollRequest(server as HttpServer)
|
||||
if m.state <> Http.HttpConnectionState.RECV_REQUEST
|
||||
m.log.error("illegal request state @" + m.client)
|
||||
LogError("illegal request state @" + m.client)
|
||||
end if
|
||||
|
||||
rcvStrByteLen = m.socket.GetCountRcvBuf()
|
||||
@@ -96,7 +92,7 @@ namespace Http
|
||||
else
|
||||
m.setState(Http.HttpConnectionState.DONE)
|
||||
m.close = true
|
||||
m.log.error("couldn't process request or response, closing @" + m.client)
|
||||
LogError("couldn't process request or response, closing @" + m.client)
|
||||
end if
|
||||
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)
|
||||
@@ -114,7 +110,7 @@ namespace Http
|
||||
|
||||
function PollHeader(server as HttpServer) as void
|
||||
if m.state <> Http.HttpConnectionState.SEND_HEADER
|
||||
m.log.error("illegal header state @" + m.client)
|
||||
LogError("illegal header state @" + m.client)
|
||||
end if
|
||||
|
||||
sent = m.response.SendHeader(m.socket, Http.BUFSIZE)
|
||||
@@ -136,7 +132,7 @@ namespace Http
|
||||
|
||||
function PollResponse(server as HttpServer)
|
||||
if m.state <> Http.HttpConnectionState.SEND_RESPONSE or m.response.header_only
|
||||
m.log.error("illegal response state @" + m.client)
|
||||
LogError("illegal response state @" + m.client)
|
||||
end if
|
||||
|
||||
sent = m.response.Send(m.socket, Http.BUFSIZE)
|
||||
@@ -166,9 +162,9 @@ namespace Http
|
||||
'info(m,op + " would have blocked @"+m.client)
|
||||
else
|
||||
if m.socket.eSuccess()
|
||||
m.log.info(op + " peer closed @" + m.client)
|
||||
LogInfo(op + " peer closed @" + m.client)
|
||||
else
|
||||
m.log.error(op + " transfer error @" + m.client, m.socket.status())
|
||||
LogError(op + " transfer error @" + m.client, m.socket.status())
|
||||
end if
|
||||
m.close = true
|
||||
m.SetState(Http.HttpConnectionState.DONE)
|
||||
@@ -178,17 +174,11 @@ namespace Http
|
||||
|
||||
function SetState(newState as Http.HttpConnectionState)
|
||||
if m.state <> newState
|
||||
' m.log.debug(m, "state change" + Stri(m.state) + " ->" + Stri(newState))
|
||||
LogDebug(m, "state change" + Stri(m.state) + " ->" + Stri(newState))
|
||||
m.state = newState
|
||||
end if
|
||||
end function
|
||||
|
||||
function LogInfo()
|
||||
if m.http_code <> 0 and not StringUtils.IsNullOrEmpty(m.method)
|
||||
m.log.info(m.last_active.totalSeconds(), " client:", m.client, " meth:", m.method, " uri:", m.uri, " code:", m.code, " sent:", m.total_sent, " referer:", m.referer, " user agent:", m.user_agent)
|
||||
end if
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -10,8 +10,6 @@ namespace Http
|
||||
end function
|
||||
|
||||
class HttpRequest
|
||||
private log as log.Logger
|
||||
|
||||
' members
|
||||
private method = invalid
|
||||
private uri = invalid
|
||||
@@ -33,7 +31,6 @@ namespace Http
|
||||
private ready_for_response = false
|
||||
|
||||
function new()
|
||||
m.log = new log.Logger("HttpRequest")
|
||||
m.id = Http.GetHttpRequestId()
|
||||
m.headers = CreateObject("roAssociativeArray")
|
||||
end function
|
||||
@@ -81,7 +78,7 @@ namespace Http
|
||||
|
||||
function ParseOperation() as boolean
|
||||
if m.buffer_index <> 0
|
||||
m.log.error(`invalid state: reading operation, but request is at index ${m.buffer_index}`)
|
||||
LogError(`invalid state: reading operation, but request is at index ${m.buffer_index}`)
|
||||
m.ready_for_response = true
|
||||
return false
|
||||
end if
|
||||
@@ -93,7 +90,7 @@ namespace Http
|
||||
operation = m.ReadHeaderLine()
|
||||
|
||||
if operation = invalid or operation = ""
|
||||
m.log.error("empty request")
|
||||
LogError("empty request")
|
||||
m.ready_for_response = true
|
||||
return false
|
||||
end if
|
||||
@@ -103,9 +100,9 @@ namespace Http
|
||||
m.method = Ucase(parts[0])
|
||||
m.uri = parts[1]
|
||||
m.protocol = Ucase(parts[2])
|
||||
m.log.info(`${m.method} '${m.uri}'`)
|
||||
LogInfo(`${m.method} '${m.uri}'`)
|
||||
else
|
||||
m.log.error("invalid request: " + operation)
|
||||
LogError("invalid request: " + operation)
|
||||
m.ready_for_response = true
|
||||
end if
|
||||
|
||||
@@ -148,7 +145,7 @@ namespace Http
|
||||
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")
|
||||
LogError("invalid request: request body found, but no Content-Length header")
|
||||
return false
|
||||
else
|
||||
return true
|
||||
@@ -160,7 +157,7 @@ namespace Http
|
||||
end if
|
||||
|
||||
if m.body.len() > contentLength
|
||||
m.log.error("invalid request: request body is larger than Content-Length")
|
||||
LogError("invalid request: request body is larger than Content-Length")
|
||||
m.ready_for_response = true
|
||||
return false
|
||||
end if
|
||||
@@ -233,7 +230,7 @@ namespace Http
|
||||
m.range_end_given = true
|
||||
end if
|
||||
end if
|
||||
m.log.info("range request begin" + Stri(m.range_begin) + " end" + Stri(m.range_end))
|
||||
LogInfo("range request begin" + Stri(m.range_begin) + " end" + Stri(m.range_end))
|
||||
end if
|
||||
end function
|
||||
|
||||
|
||||
@@ -9,8 +9,6 @@ namespace Http
|
||||
end function
|
||||
|
||||
class HttpResponse
|
||||
private log as log.Logger
|
||||
|
||||
private source = Http.HttpResponseSource.NONE
|
||||
|
||||
' members
|
||||
@@ -32,7 +30,6 @@ namespace Http
|
||||
private settings
|
||||
|
||||
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
|
||||
@@ -111,20 +108,20 @@ namespace Http
|
||||
if m.start + m.sent >= m.buf_start + m.body_buffer.count()
|
||||
m.buf_start = m.start + m.sent
|
||||
m.body_buffer.ReadFile(m.path, m.buf_start, bufsize)
|
||||
m.log.info(`Read ${m.body_buffer.count()} bytes from source file @${m.buf_start}`)
|
||||
LogDebug(`Read ${m.body_buffer.count()} bytes from source file @${m.buf_start}`)
|
||||
end if
|
||||
end if
|
||||
buf_pos = m.start + m.sent - m.buf_start
|
||||
buf_remaining = m.body_buffer.count() - buf_pos
|
||||
if buf_remaining = 0
|
||||
m.log.info("source buffer is empty")
|
||||
LogInfo("source buffer is empty")
|
||||
end if
|
||||
req_remaining = m.body_buffer_length - m.sent
|
||||
if buf_remaining > req_remaining
|
||||
buf_remaining = req_remaining
|
||||
end if
|
||||
sent = sock.send(m.body_buffer, buf_pos, buf_remaining)
|
||||
m.LogInfo(sent, m.buf_start + buf_pos, m.body_buffer_length)
|
||||
m.LogSentRange(sent, m.buf_start + buf_pos, m.body_buffer_length)
|
||||
if sent > 0
|
||||
m.sent = m.sent + sent
|
||||
end if
|
||||
@@ -141,7 +138,7 @@ namespace Http
|
||||
else
|
||||
sent = sock.sendStr(m.header.mid(m.header_sent))
|
||||
end if
|
||||
m.LogInfo(sent, m.header_sent, m.header_length)
|
||||
m.LogSentRange(sent, m.header_sent, m.header_length)
|
||||
if sent > 0
|
||||
m.header_sent = m.header_sent + sent
|
||||
end if
|
||||
@@ -176,8 +173,8 @@ namespace Http
|
||||
return m.header_sent = m.header_length
|
||||
end function
|
||||
|
||||
function LogInfo(recent as integer, from as integer, total as integer)
|
||||
m.log.info(`Sent ${recent} [${m.MakeRange(from, recent, total)}]`)
|
||||
function LogSentRange(recent as integer, from as integer, total as integer)
|
||||
LogDebug(`Sent ${recent} [${m.MakeRange(from, recent, total)}]`)
|
||||
end function
|
||||
|
||||
function KeepAlive(keepAlive as boolean)
|
||||
|
||||
@@ -7,8 +7,6 @@ import "HttpConnection.bs"
|
||||
namespace Http
|
||||
|
||||
class HttpServer
|
||||
private log as log.Logger
|
||||
|
||||
' members
|
||||
private connections = invalid
|
||||
private sockin = invalid
|
||||
@@ -26,7 +24,6 @@ namespace Http
|
||||
public task as object
|
||||
|
||||
function new(settings as HttpSettings, task as object)
|
||||
m.log = new log.Logger("HttpServer")
|
||||
m.settings = settings
|
||||
m.task = task
|
||||
m.routers = []
|
||||
@@ -44,13 +41,13 @@ namespace Http
|
||||
sockin = CreateObject("roStreamSocket")
|
||||
if sockin = invalid
|
||||
success = false
|
||||
m.log.error("socket()")
|
||||
LogError("socket()")
|
||||
end if
|
||||
|
||||
' reuse address
|
||||
if not sockin.setReuseAddr(true)
|
||||
success = false
|
||||
m.log.error("setsockopt(SO_REUSEADDR)", sockin.status())
|
||||
LogError("setsockopt(SO_REUSEADDR)", sockin.status())
|
||||
end if
|
||||
|
||||
' bind socket
|
||||
@@ -58,12 +55,12 @@ namespace Http
|
||||
addrin.setPort(m.settings.Port)
|
||||
if not sockin.setAddress(addrin)
|
||||
success = false
|
||||
m.log.error("bind(port" + Stri(m.settings.Port) + ")", sockin.status())
|
||||
LogError("bind(port" + Stri(m.settings.Port) + ")", sockin.status())
|
||||
end if
|
||||
' listen on socket
|
||||
if not sockin.listen(m.settings.MaxConnections)
|
||||
success = false
|
||||
m.log.error("listen()", sockin.status())
|
||||
LogError("listen()", sockin.status())
|
||||
end if
|
||||
' monitor socket
|
||||
sockin.setMessagePort(m.settings.MessagePort)
|
||||
@@ -71,7 +68,7 @@ namespace Http
|
||||
|
||||
m.sockin = sockin
|
||||
|
||||
m.log.info("listening on " + addrin.getAddress())
|
||||
LogInfo("listening on " + addrin.getAddress())
|
||||
return success
|
||||
end function
|
||||
|
||||
@@ -95,7 +92,7 @@ namespace Http
|
||||
conn.socket.notifyWritable(true)
|
||||
conn.socket.notifyReadable(false)
|
||||
else
|
||||
m.log.error("invalid state")
|
||||
LogError("invalid state")
|
||||
end if
|
||||
end for
|
||||
end function
|
||||
@@ -126,7 +123,7 @@ namespace Http
|
||||
else if cs = Http.HttpConnectionState.DONE
|
||||
' handle with other connections that might transition to done
|
||||
else
|
||||
m.log.error("invalid state")
|
||||
LogError("invalid state")
|
||||
end if
|
||||
cs = conn.state
|
||||
if cs = Http.HttpConnectionState.DONE
|
||||
@@ -134,7 +131,7 @@ namespace Http
|
||||
conn.socket.close()
|
||||
connections.delete(id)
|
||||
else if conn.ws_upgrade and m.websocket <> invalid
|
||||
m.log.info("upgrading connection to web socket")
|
||||
LogInfo("upgrading connection to web socket")
|
||||
m.websocket.AddConnection(conn.socket)
|
||||
connections.delete(id)
|
||||
else
|
||||
@@ -149,7 +146,7 @@ namespace Http
|
||||
sockin = m.sockin
|
||||
socket = sockin.accept()
|
||||
if not sockin.eOK()
|
||||
m.log.error("accept()", sockin.status())
|
||||
LogError("accept()", sockin.status())
|
||||
end if
|
||||
if socket = invalid
|
||||
return invalid
|
||||
@@ -171,7 +168,7 @@ namespace Http
|
||||
m.websocket.Close()
|
||||
end if
|
||||
' final stats
|
||||
m.log.info(`\n` + m.stats())
|
||||
LogInfo(`\n` + m.stats())
|
||||
end function
|
||||
|
||||
function Stats() as string
|
||||
|
||||
Reference in new issue
Block a user