mirror of
https://github.com/opensim/opensim.git
synced 2026-07-31 22:05:36 +08:00
some more cosmetics on oshttpserver
This commit is contained in:
@@ -3,53 +3,23 @@
|
||||
namespace OSHttpServer.Parser
|
||||
{
|
||||
/// <summary>
|
||||
/// Arguments used when more body bytes have come.
|
||||
/// Arguments used when body bytes have come.
|
||||
/// </summary>
|
||||
public class BodyEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BodyEventArgs"/> class.
|
||||
/// </summary>
|
||||
/// <param name="buffer">buffer that contains the received bytes.</param>
|
||||
/// <param name="offset">offset in buffer where to start processing.</param>
|
||||
/// <param name="count">number of bytes from <paramref name="offset"/> that should be parsed.</param>
|
||||
public BodyEventArgs(byte[] buffer, int offset, int count)
|
||||
{
|
||||
Buffer = buffer;
|
||||
Offset = offset;
|
||||
Count = count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BodyEventArgs"/> class.
|
||||
/// </summary>
|
||||
public BodyEventArgs()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets buffer that contains the received bytes.
|
||||
/// </summary>
|
||||
public byte[] Buffer { get; set; }
|
||||
/*
|
||||
public byte[] Buffer;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets number of bytes used by the request.
|
||||
/// number of bytes from <see cref="Offset"/> that should be parsed.
|
||||
/// </summary>
|
||||
public int BytesUsed { get; set; }
|
||||
*/
|
||||
public int Count;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets number of bytes from <see cref="Offset"/> that should be parsed.
|
||||
/// offset in buffer where to start processing.
|
||||
/// </summary>
|
||||
public int Count { get; set; }
|
||||
/*
|
||||
/// <summary>
|
||||
/// Gets or sets whether the body is complete.
|
||||
/// </summary>
|
||||
public bool IsBodyComplete { get; set; }
|
||||
*/
|
||||
/// <summary>
|
||||
/// Gets or sets offset in buffer where to start processing.
|
||||
/// </summary>
|
||||
public int Offset { get; set; }
|
||||
public int Offset;
|
||||
}
|
||||
}
|
||||
@@ -9,24 +9,6 @@ namespace OSHttpServer.Parser
|
||||
public class HeaderEventArgs : EventArgs
|
||||
{
|
||||
public osUTF8Slice Name;
|
||||
public string Value;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HeaderEventArgs"/> class.
|
||||
/// </summary>
|
||||
public HeaderEventArgs()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HeaderEventArgs"/> class.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of header.</param>
|
||||
/// <param name="value">Header value.</param>
|
||||
public HeaderEventArgs(osUTF8Slice name, string value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
public osUTF8Slice Value;
|
||||
}
|
||||
}
|
||||
@@ -182,14 +182,14 @@ namespace OSHttpServer
|
||||
Respond("HTTP/1.1", HttpStatusCode.Continue, null);
|
||||
}
|
||||
}
|
||||
m_currentRequest.AddHeader(e.Name.ToString(), e.Value);
|
||||
m_currentRequest.AddHeader(e.Name.ToString(), e.Value.ToString());
|
||||
}
|
||||
|
||||
private void OnRequestLine(object sender, RequestLineEventArgs e)
|
||||
{
|
||||
m_currentRequest.Method = e.HttpMethod;
|
||||
m_currentRequest.HttpVersion = e.HttpVersion;
|
||||
m_currentRequest.UriPath = e.UriPath;
|
||||
m_currentRequest.Method = e.HttpMethod.ToString();
|
||||
m_currentRequest.HttpVersion = e.HttpVersion.ToString();
|
||||
m_currentRequest.UriPath = e.UriPath.ToString();
|
||||
m_currentRequest.AddHeader("remote_addr", LocalIPEndPoint.Address.ToString());
|
||||
m_currentRequest.AddHeader("remote_port", LocalIPEndPoint.Port.ToString());
|
||||
m_currentRequest.ArrivalTS = ContextTimeoutManager.GetTimeStamp();
|
||||
@@ -212,7 +212,7 @@ namespace OSHttpServer
|
||||
{
|
||||
m_stream.BeginRead(m_ReceiveBuffer, 0, m_ReceiveBuffer.Length, OnReceive, null);
|
||||
}
|
||||
catch (IOException err)
|
||||
catch (Exception err)
|
||||
{
|
||||
LogWriter.Write(this, LogPrio.Debug, err.ToString());
|
||||
}
|
||||
@@ -353,7 +353,7 @@ namespace OSHttpServer
|
||||
try
|
||||
{
|
||||
int bytesRead = 0;
|
||||
if (m_stream == null)
|
||||
if (m_stream is null)
|
||||
return;
|
||||
try
|
||||
{
|
||||
@@ -377,24 +377,24 @@ namespace OSHttpServer
|
||||
m_ReceiveBytesLeft += bytesRead;
|
||||
|
||||
int offset = m_parser.Parse(m_ReceiveBuffer, 0, m_ReceiveBytesLeft);
|
||||
if (m_stream == null)
|
||||
if (m_stream is null)
|
||||
return; // "Connection: Close" in effect.
|
||||
|
||||
while (offset != 0)
|
||||
if(offset > 0)
|
||||
{
|
||||
int nextBytesleft = m_ReceiveBytesLeft - offset;
|
||||
if (nextBytesleft <= 0)
|
||||
break;
|
||||
int nextBytesleft, nextOffset;
|
||||
while ((nextBytesleft = m_ReceiveBytesLeft - offset) > 0)
|
||||
{
|
||||
nextOffset = m_parser.Parse(m_ReceiveBuffer, offset, nextBytesleft);
|
||||
|
||||
int nextOffset = m_parser.Parse(m_ReceiveBuffer, offset, nextBytesleft);
|
||||
if (m_stream is null)
|
||||
return; // "Connection: Close" in effect.
|
||||
|
||||
if (m_stream == null)
|
||||
return; // "Connection: Close" in effect.
|
||||
if (nextOffset == 0)
|
||||
break;
|
||||
|
||||
if (nextOffset == 0)
|
||||
break;
|
||||
|
||||
offset = nextOffset;
|
||||
offset = nextOffset;
|
||||
}
|
||||
}
|
||||
|
||||
// copy unused bytes to the beginning of the array
|
||||
@@ -472,7 +472,7 @@ namespace OSHttpServer
|
||||
if (--m_maxRequests == 0)
|
||||
m_currentRequest.Connection = ConnectionType.Close;
|
||||
|
||||
if(m_currentRequest.Uri == null)
|
||||
if(m_currentRequest.Uri is null)
|
||||
{
|
||||
// should not happen
|
||||
try
|
||||
@@ -488,7 +488,7 @@ namespace OSHttpServer
|
||||
}
|
||||
|
||||
// load cookies if they exist
|
||||
if(m_currentRequest.Headers["cookie"] != null)
|
||||
if(m_currentRequest.Headers["cookie"] is not null)
|
||||
m_currentRequest.SetCookies(new RequestCookies(m_currentRequest.Headers["cookie"]));
|
||||
|
||||
m_currentRequest.Body.Seek(0, SeekOrigin.Begin);
|
||||
@@ -519,7 +519,7 @@ namespace OSHttpServer
|
||||
|
||||
public bool TrySendResponse(int bytesLimit)
|
||||
{
|
||||
if (m_currentResponse == null)
|
||||
if (m_currentResponse is null)
|
||||
return false;
|
||||
try
|
||||
{
|
||||
@@ -540,7 +540,7 @@ namespace OSHttpServer
|
||||
|
||||
public void ContinueSendResponse()
|
||||
{
|
||||
if(m_currentResponse == null)
|
||||
if(m_currentResponse is null)
|
||||
return;
|
||||
LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
|
||||
ContextTimeoutManager.EnqueueSend(this, m_currentResponse.Priority);
|
||||
@@ -566,16 +566,16 @@ namespace OSHttpServer
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Stream == null || !Stream.CanWrite)
|
||||
if (Stream is null || !Stream.CanWrite)
|
||||
return;
|
||||
|
||||
LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
|
||||
HttpRequest nextRequest = null;
|
||||
lock (m_requestsLock)
|
||||
{
|
||||
if (m_requests != null && m_requests.Count > 0)
|
||||
if (m_requests is not null && m_requests.Count > 0)
|
||||
nextRequest = m_requests.Dequeue();
|
||||
if (nextRequest != null && RequestReceived != null)
|
||||
if (nextRequest is not null && RequestReceived is not null)
|
||||
{
|
||||
m_waitingResponse = true;
|
||||
TriggerKeepalive = false;
|
||||
@@ -583,7 +583,7 @@ namespace OSHttpServer
|
||||
else
|
||||
TriggerKeepalive = true;
|
||||
}
|
||||
if (nextRequest != null)
|
||||
if (nextRequest is not null)
|
||||
RequestReceived?.Invoke(this, new RequestEventArgs(nextRequest));
|
||||
}
|
||||
ContextTimeoutManager.PulseWaitSend();
|
||||
@@ -659,7 +659,7 @@ namespace OSHttpServer
|
||||
|
||||
public bool Send(byte[] buffer, int offset, int size)
|
||||
{
|
||||
if (m_stream == null || m_sock == null || !m_sock.Connected)
|
||||
if (m_stream is null || m_sock is null || !m_sock.Connected)
|
||||
return false;
|
||||
|
||||
if (offset + size > buffer.Length)
|
||||
@@ -682,7 +682,7 @@ namespace OSHttpServer
|
||||
}
|
||||
|
||||
ContextTimeoutManager.ContextLeaveActiveSend();
|
||||
if (!ok && m_stream != null)
|
||||
if (!ok && m_stream is not null)
|
||||
Disconnect(SocketError.NoRecovery);
|
||||
return ok;
|
||||
}
|
||||
@@ -693,6 +693,7 @@ namespace OSHttpServer
|
||||
try
|
||||
{
|
||||
m_stream.EndWrite(res);
|
||||
|
||||
ContextTimeoutManager.ContextLeaveActiveSend();
|
||||
didleave = true;
|
||||
m_currentResponse.CheckSendNextAsyncContinue();
|
||||
@@ -700,7 +701,7 @@ namespace OSHttpServer
|
||||
catch (Exception e)
|
||||
{
|
||||
e.GetHashCode();
|
||||
if (m_stream != null)
|
||||
if (m_stream is not null)
|
||||
Disconnect(SocketError.NoRecovery);
|
||||
}
|
||||
if(!didleave)
|
||||
@@ -709,7 +710,7 @@ namespace OSHttpServer
|
||||
|
||||
public bool SendAsyncStart(byte[] buffer, int offset, int size)
|
||||
{
|
||||
if (m_stream == null || m_sock == null || !m_sock.Connected)
|
||||
if (m_stream is null || m_sock is null || !m_sock.Connected)
|
||||
return false;
|
||||
|
||||
if (offset + size > buffer.Length)
|
||||
@@ -728,7 +729,7 @@ namespace OSHttpServer
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if (!ok && m_stream != null)
|
||||
if (!ok && m_stream is not null)
|
||||
Disconnect(SocketError.NoRecovery);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using OSHttpServer.Exceptions;
|
||||
using OpenMetaverse;
|
||||
|
||||
@@ -102,7 +101,7 @@ namespace OSHttpServer.Parser
|
||||
/// <param name="value"></param>
|
||||
/// <exception cref="BadRequestException">If line is incorrect</exception>
|
||||
/// <remarks>Expects the following format: "Method SP Request-URI SP HTTP-Version CRLF"</remarks>
|
||||
protected void OnFirstLine(string value)
|
||||
protected void OnFirstLine(osUTF8Slice value)
|
||||
{
|
||||
//
|
||||
//todo: In the interest of robustness, servers SHOULD ignore any empty line(s) received where a Request-Line is expected.
|
||||
@@ -111,7 +110,7 @@ namespace OSHttpServer.Parser
|
||||
m_log.Write(this, LogPrio.Debug, $"Got request: {value}");
|
||||
|
||||
//Request-Line = Method SP Request-URI SP HTTP-Version CRLF
|
||||
int pos = value.IndexOf(' ');
|
||||
int pos = value.IndexOf((byte)' ');
|
||||
int oldPos = pos + 1;
|
||||
if (pos == -1 || oldPos >= value.Length)
|
||||
{
|
||||
@@ -119,18 +118,20 @@ namespace OSHttpServer.Parser
|
||||
throw new BadRequestException($"Invalid request line, missing Method. Line: {value}");
|
||||
}
|
||||
|
||||
string method = value[..pos].ToUpper();
|
||||
pos = value.IndexOf(' ', oldPos);
|
||||
osUTF8Slice method = value.SubUTF8(0, pos);
|
||||
value.SubUTF8Self(oldPos);
|
||||
pos = value.IndexOf((byte)' ');
|
||||
if (pos == -1)
|
||||
{
|
||||
m_log.Write(this, LogPrio.Warning, "Invalid request line, missing URI. Line: " + value);
|
||||
throw new BadRequestException("Invalid request line, missing URI. Line: " + value);
|
||||
m_log.Write(this, LogPrio.Warning, "Invalid request line, missing URI");
|
||||
throw new BadRequestException("Invalid request line, missing URI");
|
||||
}
|
||||
string path = value[oldPos..pos];
|
||||
if (path.Length > 4196)
|
||||
throw new BadRequestException("Too long URI.");
|
||||
if (path == "*")
|
||||
throw new BadRequestException("Not supported URI.");
|
||||
if(pos > 4196)
|
||||
throw new BadRequestException("URI too long");
|
||||
|
||||
osUTF8Slice path = value.SubUTF8(0, pos);
|
||||
if (path.ACSIILowerEquals("*"))
|
||||
throw new BadRequestException("URI not supported");
|
||||
|
||||
oldPos = pos + 1;
|
||||
if (oldPos >= value.Length)
|
||||
@@ -139,20 +140,21 @@ namespace OSHttpServer.Parser
|
||||
throw new BadRequestException($"Invalid request line, missing HTTP-Version. Line: {value}");
|
||||
}
|
||||
|
||||
string version = value[oldPos..];
|
||||
if (version.Length < 4 || string.Compare(version[..4], "HTTP", true) != 0)
|
||||
osUTF8Slice version = value.SubUTF8(oldPos);
|
||||
if (version.Length < 4 || !version.SubUTF8(0,4).ACSIILowerEquals("http"))
|
||||
{
|
||||
m_log.Write(this, LogPrio.Warning, $"Invalid HTTP version in request line. Line: {value}");
|
||||
throw new BadRequestException($"Invalid HTTP version in Request line. Line: {value}");
|
||||
}
|
||||
|
||||
if(RequestLineReceived != null)
|
||||
if(RequestLineReceived is not null)
|
||||
{
|
||||
method.ToASCIIUpperSelf();
|
||||
m_requestLineArgs.HttpMethod = method;
|
||||
m_requestLineArgs.HttpVersion = version;
|
||||
m_requestLineArgs.UriPath = path;
|
||||
RequestLineReceived?.Invoke(this, m_requestLineArgs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly byte[] OSUTF8contentlength = osUTF8.GetASCIIBytes("content-length");
|
||||
@@ -175,7 +177,7 @@ namespace OSHttpServer.Parser
|
||||
if (HeaderReceived != null)
|
||||
{
|
||||
m_headerArgs.Name = m_curHeaderName;
|
||||
m_headerArgs.Value = m_curHeaderValue.ToString();
|
||||
m_headerArgs.Value = m_curHeaderValue;
|
||||
HeaderReceived?.Invoke(this, m_headerArgs);
|
||||
}
|
||||
|
||||
@@ -256,7 +258,7 @@ namespace OSHttpServer.Parser
|
||||
else if(ch == '\r' || ch == '\n')
|
||||
{
|
||||
int size = GetLineBreakSize(buffer, currentPos);
|
||||
OnFirstLine(Encoding.UTF8.GetString(buffer, startPos, currentPos - startPos));
|
||||
OnFirstLine(new osUTF8Slice(buffer, startPos, currentPos - startPos));
|
||||
currentPos += size - 1;
|
||||
handledBytes = currentPos + 1;
|
||||
startPos = -1;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using OpenMetaverse;
|
||||
using System;
|
||||
|
||||
namespace OSHttpServer.Parser
|
||||
{
|
||||
@@ -8,41 +9,18 @@ namespace OSHttpServer.Parser
|
||||
public class RequestLineEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestLineEventArgs"/> class.
|
||||
/// http method.
|
||||
/// </summary>
|
||||
/// <param name="httpMethod">The HTTP method.</param>
|
||||
/// <param name="uriPath">The URI path.</param>
|
||||
/// <param name="httpVersion">The HTTP version.</param>
|
||||
public RequestLineEventArgs(string httpMethod, string uriPath, string httpVersion)
|
||||
{
|
||||
HttpMethod = httpMethod;
|
||||
UriPath = uriPath;
|
||||
HttpVersion = httpVersion;
|
||||
}
|
||||
public osUTF8Slice HttpMethod;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RequestLineEventArgs"/> class.
|
||||
/// version of the HTTP protocol that the client want to use.
|
||||
/// </summary>
|
||||
public RequestLineEventArgs()
|
||||
{
|
||||
}
|
||||
public osUTF8Slice HttpVersion;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets http method.
|
||||
/// requested URI path.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Should be one of the methods declared in <see cref="Method"/>.
|
||||
/// </remarks>
|
||||
public string HttpMethod { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the version of the HTTP protocol that the client want to use.
|
||||
/// </summary>
|
||||
public string HttpVersion { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets requested URI path.
|
||||
/// </summary>
|
||||
public string UriPath { get; set; }
|
||||
public osUTF8Slice UriPath;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user