185 lines
6.5 KiB
Plaintext
185 lines
6.5 KiB
Plaintext
import "HttpRequest.bs"
|
|
import "HttpResponse.bs"
|
|
import "HttpSettings.bs"
|
|
import "pkg:/source/utils/Logging.bs"
|
|
import "pkg:/source/utils/StringUtils.bs"
|
|
import "pkg:/source/utils/Types.bs"
|
|
|
|
namespace Http
|
|
const BUFSIZE = 65536
|
|
|
|
class HttpConnection
|
|
private total_received = 0
|
|
private total_sent = 0
|
|
private id = "<uninit>"
|
|
private close = true
|
|
private ws_upgrade = false
|
|
private state = Http.HttpConnectionState.INIT
|
|
|
|
private server as HttpServer
|
|
private settings as HttpSettings
|
|
|
|
function new(server as HttpServer, settings as HttpSettings)
|
|
m.server = server
|
|
m.settings = settings
|
|
m.Reset()
|
|
end function
|
|
|
|
function Reset()
|
|
' re-initializes all instance members
|
|
m.socket = invalid
|
|
m.addr = invalid
|
|
m.Restart()
|
|
end function
|
|
|
|
function Restart()
|
|
' re-initializes members for next request-response
|
|
m.last_active = CreateObject("roTimespan")
|
|
m.request = new HttpRequest()
|
|
m.header = invalid
|
|
m.response = invalid
|
|
m.header_only = false
|
|
m.close = true
|
|
m.ws_upgrade = false
|
|
m.SetState(Http.HttpConnectionState.INIT)
|
|
end function
|
|
|
|
function Recycle()
|
|
LogDebug("recycle")
|
|
m.Restart()
|
|
m.SetState(Http.HttpConnectionState.RECV_REQUEST) ' ready for another
|
|
end function
|
|
|
|
function Accepted(server as HttpServer, socket as object)
|
|
m.socket = socket
|
|
m.id = socket.getID().ToStr()
|
|
m.SetState(Http.HttpConnectionState.RECV_REQUEST)
|
|
m.client = m.socket.getReceivedFromAddress().getAddress()
|
|
|
|
LogInfo("accepted connection @" + m.client)
|
|
|
|
' try to read right away rather than going through another iteration
|
|
' of the poll loop.
|
|
m.PollRequest(server)
|
|
end function
|
|
|
|
function PollRequest(server as HttpServer)
|
|
if m.state <> Http.HttpConnectionState.RECV_REQUEST
|
|
LogError("illegal request state @" + m.client)
|
|
end if
|
|
|
|
rcvStrByteLen = m.socket.GetCountRcvBuf()
|
|
rcvStr = m.socket.receiveStr(Http.BUFSIZE)
|
|
received = rcvStr.len()
|
|
rcvStrByteLen -= m.socket.GetCountRcvBuf()
|
|
|
|
maybeUnicode = rcvStrByteLen <> received
|
|
|
|
if m.CheckResult(received, "receive request")
|
|
m.total_received = m.total_received + received
|
|
server.total_in = server.total_in + received
|
|
end if
|
|
|
|
' append to connection request
|
|
m.request.Add(rcvStr, maybeUnicode)
|
|
|
|
if m.request.ProcessNext(m)
|
|
if m.request.state = Http.HttpRequestState.DONE
|
|
server.num_requests = validint(server.num_requests) + 1
|
|
m.response = new HttpResponse(m.request, m.server, m.settings)
|
|
m.response.Process(m)
|
|
m.SetState(Http.HttpConnectionState.SEND_HEADER)
|
|
else
|
|
m.setState(Http.HttpConnectionState.DONE)
|
|
m.close = true
|
|
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)
|
|
m.response.Default(413, "Your request was dropped because it was too long.")
|
|
m.SetState(Http.HttpConnectionState.SEND_HEADER)
|
|
end if
|
|
|
|
' if we've moved on to the next state, try to send right away, instead of
|
|
' going through another iteration of the poll loop.
|
|
'
|
|
if m.state = Http.HttpConnectionState.SEND_HEADER
|
|
m.PollHeader(server)
|
|
end if
|
|
end function
|
|
|
|
function PollHeader(server as HttpServer) as void
|
|
if m.state <> Http.HttpConnectionState.SEND_HEADER
|
|
LogError("illegal header state @" + m.client)
|
|
end if
|
|
|
|
sent = m.response.SendHeader(m.socket)
|
|
|
|
if m.CheckResult(sent, "send header")
|
|
m.total_sent = m.total_sent + sent
|
|
server.total_out = server.total_out + sent
|
|
end if
|
|
|
|
if m.response.DoneHeader()
|
|
if m.response.header_only
|
|
m.SetState(Http.HttpConnectionState.DONE)
|
|
else
|
|
m.SetState(Http.HttpConnectionState.SEND_RESPONSE)
|
|
m.PollResponse(server)
|
|
end if
|
|
end if
|
|
end function
|
|
|
|
function PollResponse(server as HttpServer)
|
|
if m.state <> Http.HttpConnectionState.SEND_RESPONSE or m.response.header_only
|
|
LogError("illegal response state @" + m.client)
|
|
end if
|
|
|
|
sent = m.response.Send(m.socket)
|
|
if m.CheckResult(sent, "send response")
|
|
m.total_sent = m.total_sent + sent
|
|
server.total_out = server.total_out + sent
|
|
end if
|
|
|
|
if m.response.Done()
|
|
m.SetState(Http.HttpConnectionState.DONE)
|
|
end if
|
|
end function
|
|
|
|
function CheckTimeout(timeoutInMs as integer)
|
|
if m.last_active.TotalMilliseconds() > timeoutInMs
|
|
m.close = true
|
|
m.SetState(Http.HttpConnectionState.DONE)
|
|
end if
|
|
end function
|
|
|
|
function CheckResult(byteCount as integer, op as string) as boolean
|
|
gotSome = false
|
|
if byteCount > 0
|
|
m.last_active.mark()
|
|
gotSome = true
|
|
else if m.socket.eOK() and not m.socket.eSuccess()
|
|
'info(m,op + " would have blocked @"+m.client)
|
|
else
|
|
if m.socket.eSuccess()
|
|
LogInfo(op + " peer closed @" + m.client)
|
|
else
|
|
LogError(op + " transfer error @" + m.client, m.socket.status())
|
|
end if
|
|
m.close = true
|
|
m.SetState(Http.HttpConnectionState.DONE)
|
|
end if
|
|
return gotSome
|
|
end function
|
|
|
|
function SetState(newState as Http.HttpConnectionState)
|
|
if m.state <> newState
|
|
' LogDebug("state change" + Stri(m.state) + " ->" + Stri(newState))
|
|
m.state = newState
|
|
end if
|
|
end function
|
|
|
|
end class
|
|
|
|
end namespace
|