mirror of
https://github.com/opensim/opensim.git
synced 2026-08-04 16:22:50 +08:00
jsonSimStats: fix null ref when region not found; limite http content lenght; cosmetics
This commit is contained in:
@@ -112,12 +112,12 @@ namespace OSHttpServer
|
||||
LocalIPEndPoint = remoteEndPoint;
|
||||
m_log = m_logWriter;
|
||||
m_isClosing = false;
|
||||
m_currentRequest = new HttpRequest(this);
|
||||
m_parser = new HttpRequestParser(m_log);
|
||||
m_parser.RequestCompleted += OnRequestCompleted;
|
||||
m_parser.RequestLineReceived += OnRequestLine;
|
||||
m_parser.HeaderReceived += OnHeaderReceived;
|
||||
m_parser.BodyBytesReceived += OnBodyBytesReceived;
|
||||
m_currentRequest = new HttpRequest(this);
|
||||
IsSecured = secured;
|
||||
m_stream = stream;
|
||||
m_sock = sock;
|
||||
@@ -215,7 +215,6 @@ namespace OSHttpServer
|
||||
{
|
||||
LogWriter.Write(this, LogPrio.Debug, err.ToString());
|
||||
}
|
||||
//Task.Run(async () => await ReceiveLoop()).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -248,9 +247,9 @@ namespace OSHttpServer
|
||||
HttpRequest req = m_requests.Dequeue();
|
||||
req.Clear();
|
||||
}
|
||||
m_requests = null;
|
||||
}
|
||||
m_requests.Clear();
|
||||
m_requests = null;
|
||||
|
||||
m_parser.Clear();
|
||||
|
||||
FirstRequestLineReceived = false;
|
||||
@@ -420,91 +419,12 @@ namespace OSHttpServer
|
||||
//Disconnect(SocketError.NoRecovery);
|
||||
Disconnect(SocketError.Success); // try to flush
|
||||
}
|
||||
catch (IOException err)
|
||||
catch (HttpException err)
|
||||
{
|
||||
LogWriter.Write(this, LogPrio.Debug, "Failed to end receive: " + err.Message);
|
||||
if (err.InnerException is SocketException)
|
||||
Disconnect((SocketError)((SocketException)err.InnerException).ErrorCode);
|
||||
else
|
||||
Disconnect(SocketError.ConnectionReset);
|
||||
}
|
||||
catch (ObjectDisposedException err)
|
||||
{
|
||||
LogWriter.Write(this, LogPrio.Debug, "Failed to end receive : " + err.Message);
|
||||
Disconnect(SocketError.NotSocket);
|
||||
}
|
||||
catch (NullReferenceException err)
|
||||
{
|
||||
LogWriter.Write(this, LogPrio.Debug, "Failed to end receive : NullRef: " + err.Message);
|
||||
Disconnect(SocketError.NoRecovery);
|
||||
}
|
||||
catch (Exception err)
|
||||
{
|
||||
LogWriter.Write(this, LogPrio.Debug, "Failed to end receive: " + err.Message);
|
||||
Disconnect(SocketError.NoRecovery);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
private async Task ReceiveLoop()
|
||||
{
|
||||
m_ReceiveBytesLeft = 0;
|
||||
try
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
if (m_stream == null || !m_stream.CanRead)
|
||||
return;
|
||||
|
||||
int bytesRead = await m_stream.ReadAsync(m_ReceiveBuffer, m_ReceiveBytesLeft, m_ReceiveBuffer.Length - m_ReceiveBytesLeft).ConfigureAwait(false);
|
||||
|
||||
if (bytesRead == 0)
|
||||
{
|
||||
Disconnect(SocketError.Success);
|
||||
return;
|
||||
}
|
||||
|
||||
if(m_isClosing)
|
||||
continue;
|
||||
|
||||
m_ReceiveBytesLeft += bytesRead;
|
||||
|
||||
int offset = m_parser.Parse(m_ReceiveBuffer, 0, m_ReceiveBytesLeft);
|
||||
if (m_stream == null)
|
||||
return; // "Connection: Close" in effect.
|
||||
|
||||
while (offset != 0)
|
||||
{
|
||||
int nextBytesleft = m_ReceiveBytesLeft - offset;
|
||||
if(nextBytesleft <= 0)
|
||||
break;
|
||||
|
||||
int nextOffset = m_parser.Parse(m_ReceiveBuffer, offset, nextBytesleft);
|
||||
|
||||
if (m_stream == null)
|
||||
return; // "Connection: Close" in effect.
|
||||
|
||||
if (nextOffset == 0)
|
||||
break;
|
||||
|
||||
offset = nextOffset;
|
||||
}
|
||||
|
||||
// copy unused bytes to the beginning of the array
|
||||
if (offset > 0 && m_ReceiveBytesLeft > offset)
|
||||
Buffer.BlockCopy(m_ReceiveBuffer, offset, m_ReceiveBuffer, 0, m_ReceiveBytesLeft - offset);
|
||||
|
||||
m_ReceiveBytesLeft -= offset;
|
||||
if (StreamPassedOff)
|
||||
return; //?
|
||||
}
|
||||
}
|
||||
catch (BadRequestException err)
|
||||
{
|
||||
LogWriter.Write(this, LogPrio.Warning, "Bad request, responding with it. Error: " + err);
|
||||
LogWriter.Write(this, LogPrio.Warning, "Bad request, responding with it. Error: " + err.Message);
|
||||
try
|
||||
{
|
||||
Respond("HTTP/1.1", HttpStatusCode.BadRequest, err.Message);
|
||||
Respond("HTTP/1.1", err.HttpStatusCode, err.Message);
|
||||
}
|
||||
catch (Exception err2)
|
||||
{
|
||||
@@ -537,7 +457,6 @@ namespace OSHttpServer
|
||||
Disconnect(SocketError.NoRecovery);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
private void OnRequestCompleted(object source, EventArgs args)
|
||||
{
|
||||
@@ -546,7 +465,7 @@ namespace OSHttpServer
|
||||
FullRequestReceived = true;
|
||||
LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
|
||||
|
||||
if (m_maxRequests == 0)
|
||||
if (m_maxRequests <= 0 || RequestReceived == null)
|
||||
return;
|
||||
|
||||
if (--m_maxRequests == 0)
|
||||
@@ -573,22 +492,20 @@ namespace OSHttpServer
|
||||
|
||||
m_currentRequest.Body.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
bool donow = true;
|
||||
HttpRequest currentRequest = m_currentRequest;
|
||||
m_currentRequest = new HttpRequest(this);
|
||||
|
||||
lock (m_requestsLock)
|
||||
{
|
||||
if(m_waitingResponse)
|
||||
{
|
||||
m_requests.Enqueue(m_currentRequest);
|
||||
donow = false;
|
||||
m_requests.Enqueue(currentRequest);
|
||||
return;
|
||||
}
|
||||
else
|
||||
m_waitingResponse = true;
|
||||
}
|
||||
|
||||
if(donow)
|
||||
RequestReceived?.Invoke(this, new RequestEventArgs(m_currentRequest));
|
||||
|
||||
m_currentRequest = new HttpRequest(this);
|
||||
RequestReceived?.Invoke(this, new RequestEventArgs(currentRequest));
|
||||
}
|
||||
|
||||
public void StartSendResponse(HttpResponse response)
|
||||
|
||||
@@ -146,6 +146,8 @@ namespace OSHttpServer
|
||||
return;
|
||||
}
|
||||
|
||||
socket.NoDelay = true;
|
||||
|
||||
if (!OnAcceptingSocket(socket))
|
||||
{
|
||||
socket.Disconnect(true);
|
||||
@@ -154,9 +156,7 @@ namespace OSHttpServer
|
||||
|
||||
if(socket.Connected)
|
||||
{
|
||||
socket.NoDelay = true;
|
||||
|
||||
m_logWriter.Write(this, LogPrio.Debug, "Accepted connection from: " + socket.RemoteEndPoint);
|
||||
m_logWriter.Write(this, LogPrio.Debug, "Accepted connection from: " + socket.RemoteEndPoint);
|
||||
|
||||
if (m_certificate != null)
|
||||
m_contextFactory.CreateSecureContext(socket, m_certificate, m_sslProtocol, m_clientCertValCallback);
|
||||
@@ -201,9 +201,13 @@ namespace OSHttpServer
|
||||
/// <returns>true if connection can be accepted; otherwise false.</returns>
|
||||
protected bool OnAcceptingSocket(Socket socket)
|
||||
{
|
||||
ClientAcceptedEventArgs args = new ClientAcceptedEventArgs(socket);
|
||||
Accepted?.Invoke(this, args);
|
||||
return !args.Revoked;
|
||||
if(Accepted!=null)
|
||||
{
|
||||
ClientAcceptedEventArgs args = new ClientAcceptedEventArgs(socket);
|
||||
Accepted?.Invoke(this, args);
|
||||
return !args.Revoked;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace OSHttpServer
|
||||
private NameValueCollection m_queryString = null;
|
||||
private Uri m_uri = null;
|
||||
private string m_uriPath;
|
||||
public readonly IHttpClientContext m_context;
|
||||
public IHttpClientContext m_context;
|
||||
IPEndPoint m_remoteIPEndPoint = null;
|
||||
|
||||
public HttpRequest(IHttpClientContext pContext)
|
||||
@@ -340,6 +340,8 @@ namespace OSHttpServer
|
||||
case "content-length":
|
||||
if (!int.TryParse(value, out int t))
|
||||
throw new BadRequestException("Invalid content length.");
|
||||
if (t > 250 * 1024 * 1024)
|
||||
throw new OSHttpServer.Exceptions.HttpException(HttpStatusCode.RequestEntityTooLarge,"Request Entity Too Large");
|
||||
ContentLength = t;
|
||||
break; //todo: maybe throw an exception
|
||||
case "host":
|
||||
@@ -458,9 +460,11 @@ namespace OSHttpServer
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
if (m_body != null && m_body.CanRead)
|
||||
if (m_body != null)
|
||||
{
|
||||
m_body.Dispose();
|
||||
m_body = null;
|
||||
m_body = null;
|
||||
}
|
||||
m_contentLength = 0;
|
||||
m_method = string.Empty;
|
||||
m_uri = null;
|
||||
@@ -469,6 +473,7 @@ namespace OSHttpServer
|
||||
m_headers.Clear();
|
||||
m_connection = ConnectionType.KeepAlive;
|
||||
IsAjax = false;
|
||||
m_context = null;
|
||||
//_form.Clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,8 @@ namespace OSHttpServer.Parser
|
||||
public class HttpRequestParser : IHttpRequestParser
|
||||
{
|
||||
private ILogWriter m_log;
|
||||
private readonly BodyEventArgs m_bodyArgs = new BodyEventArgs();
|
||||
private readonly HeaderEventArgs m_headerArgs = new HeaderEventArgs();
|
||||
private readonly BodyEventArgs m_bodyEventArgs = new BodyEventArgs();
|
||||
private readonly RequestLineEventArgs m_requestLineArgs = new RequestLineEventArgs();
|
||||
private string m_curHeaderName = string.Empty;
|
||||
private string m_curHeaderValue = string.Empty;
|
||||
@@ -52,13 +52,18 @@ namespace OSHttpServer.Parser
|
||||
private int AddToBody(byte[] buffer, int offset, int count)
|
||||
{
|
||||
// got all bytes we need, or just a few of them?
|
||||
int bytesUsed = count > m_bodyBytesLeft ? m_bodyBytesLeft : count;
|
||||
m_bodyArgs.Buffer = buffer;
|
||||
m_bodyArgs.Offset = offset;
|
||||
m_bodyArgs.Count = bytesUsed;
|
||||
BodyBytesReceived?.Invoke(this, m_bodyArgs);
|
||||
int bytesCount = count > m_bodyBytesLeft ? m_bodyBytesLeft : count;
|
||||
|
||||
m_bodyBytesLeft -= bytesUsed;
|
||||
if(BodyBytesReceived != null)
|
||||
{
|
||||
m_bodyEventArgs.Buffer = buffer;
|
||||
m_bodyEventArgs.Offset = offset;
|
||||
m_bodyEventArgs.Count = bytesCount;
|
||||
BodyBytesReceived?.Invoke(this, m_bodyEventArgs);
|
||||
m_bodyEventArgs.Buffer = null;
|
||||
}
|
||||
|
||||
m_bodyBytesLeft -= bytesCount;
|
||||
if (m_bodyBytesLeft == 0)
|
||||
{
|
||||
// got a complete request.
|
||||
@@ -67,7 +72,7 @@ namespace OSHttpServer.Parser
|
||||
Clear();
|
||||
}
|
||||
|
||||
return offset + bytesUsed;
|
||||
return offset + bytesCount;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -139,10 +144,13 @@ namespace OSHttpServer.Parser
|
||||
throw new BadRequestException("Invalid HTTP version in Request line. Line: " + value);
|
||||
}
|
||||
|
||||
m_requestLineArgs.HttpMethod = method;
|
||||
m_requestLineArgs.HttpVersion = version;
|
||||
m_requestLineArgs.UriPath = path;
|
||||
RequestLineReceived(this, m_requestLineArgs);
|
||||
if(RequestLineReceived != null)
|
||||
{
|
||||
m_requestLineArgs.HttpMethod = method;
|
||||
m_requestLineArgs.HttpVersion = version;
|
||||
m_requestLineArgs.UriPath = path;
|
||||
RequestLineReceived?.Invoke(this, m_requestLineArgs);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -151,17 +159,23 @@ namespace OSHttpServer.Parser
|
||||
/// <param name="name">Name in lower case</param>
|
||||
/// <param name="value">Value, unmodified.</param>
|
||||
/// <exception cref="BadRequestException">If content length cannot be parsed.</exception>
|
||||
protected void OnHeader(string name, string value)
|
||||
protected void OnHeader()
|
||||
{
|
||||
m_headerArgs.Name = name;
|
||||
m_headerArgs.Value = value;
|
||||
if (string.Compare(name, "content-length", true) == 0)
|
||||
if (string.Compare(m_curHeaderName, "content-length", true) == 0)
|
||||
{
|
||||
if (!int.TryParse(value, out m_bodyBytesLeft))
|
||||
if (!int.TryParse(m_curHeaderValue, out m_bodyBytesLeft))
|
||||
throw new BadRequestException("Content length is not a number.");
|
||||
}
|
||||
|
||||
HeaderReceived?.Invoke(this, m_headerArgs);
|
||||
if (HeaderReceived != null)
|
||||
{
|
||||
m_headerArgs.Name = m_curHeaderName;
|
||||
m_headerArgs.Value = m_curHeaderValue;
|
||||
HeaderReceived?.Invoke(this, m_headerArgs);
|
||||
}
|
||||
|
||||
m_curHeaderName = string.Empty;
|
||||
m_curHeaderValue = string.Empty;
|
||||
}
|
||||
|
||||
private void OnRequestCompleted()
|
||||
@@ -372,12 +386,10 @@ namespace OSHttpServer.Parser
|
||||
{
|
||||
m_curHeaderValue += Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos);
|
||||
m_log.Write(this, LogPrio.Trace, "Header [" + m_curHeaderName + ": " + m_curHeaderValue + "]");
|
||||
OnHeader(m_curHeaderName, m_curHeaderValue);
|
||||
OnHeader();
|
||||
|
||||
startPos = -1;
|
||||
CurrentState = RequestParserState.HeaderName;
|
||||
m_curHeaderValue = string.Empty;
|
||||
m_curHeaderName = string.Empty;
|
||||
currentPos += newLineSize - 1;
|
||||
handledBytes = currentPos + 1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user