From 713c222ec8456d7273a7341a16c2aa5729b27645 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Mon, 11 May 2020 15:12:21 +0100 Subject: [PATCH] remove some redundancy --- .../GetMesh/GetMeshServerConnector.cs | 2 +- .../GetTexture/GetTextureServerConnector.cs | 2 +- .../Servers/HttpServer/BaseHttpServer.cs | 46 ++++++------------- .../Servers/HttpServer/OSHttpResponse.cs | 3 +- .../HttpServer/OSHttpServer/HttpResponse.cs | 4 +- .../HttpServer/PollServiceHttpRequest.cs | 33 ++----------- .../HttpServer/PollServiceRequestManager.cs | 4 +- .../HttpServer/WebsocketServerHandler.cs | 6 +-- 8 files changed, 27 insertions(+), 73 deletions(-) diff --git a/OpenSim/Capabilities/Handlers/GetMesh/GetMeshServerConnector.cs b/OpenSim/Capabilities/Handlers/GetMesh/GetMeshServerConnector.cs index b494aa4962..5825c40027 100644 --- a/OpenSim/Capabilities/Handlers/GetMesh/GetMeshServerConnector.cs +++ b/OpenSim/Capabilities/Handlers/GetMesh/GetMeshServerConnector.cs @@ -68,7 +68,7 @@ namespace OpenSim.Capabilities.Handlers IRequestHandler reqHandler = new RestHTTPHandler( "GET", - "/CAPS/" + UUID.Random(), + "/" + UUID.Random(), httpMethod => gmeshHandler.ProcessGetMesh(httpMethod, UUID.Zero, null), "GetMesh", null); diff --git a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureServerConnector.cs b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureServerConnector.cs index 479cebb3a9..4726f1bc6b 100644 --- a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureServerConnector.cs +++ b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureServerConnector.cs @@ -66,7 +66,7 @@ namespace OpenSim.Capabilities.Handlers string rurl = serverConfig.GetString("GetTextureRedirectURL"); ; server.AddStreamHandler( - new GetTextureRobustHandler("/CAPS/GetTexture/", m_AssetService, "GetTexture", null, rurl)); + new GetTextureRobustHandler("/CAPS/GetTexture", m_AssetService, "GetTexture", null, rurl)); } } } diff --git a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs index 14cb6e17fd..1a676dd98a 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs @@ -101,7 +101,7 @@ namespace OpenSim.Framework.Servers.HttpServer protected Dictionary m_HTTPHandlers = new Dictionary(); // protected Dictionary m_agentHandlers = new Dictionary(); protected ConcurrentDictionary m_pollHandlers = new ConcurrentDictionary(); - protected Dictionary m_WebSocketHandlers = new Dictionary(); + protected ConcurrentDictionary m_WebSocketHandlers = new ConcurrentDictionary(); protected ConcurrentDictionary m_streamHandlers = new ConcurrentDictionary(); protected ConcurrentDictionary m_simpleStreamHandlers = new ConcurrentDictionary(); @@ -354,18 +354,12 @@ namespace OpenSim.Framework.Servers.HttpServer public void AddWebSocketHandler(string servicepath, WebSocketRequestDelegate handler) { - lock (m_WebSocketHandlers) - { - if (!m_WebSocketHandlers.ContainsKey(servicepath)) - m_WebSocketHandlers.Add(servicepath, handler); - } + m_WebSocketHandlers.TryAdd(servicepath, handler); } public void RemoveWebSocketHandler(string servicepath) { - lock (m_WebSocketHandlers) - if (m_WebSocketHandlers.ContainsKey(servicepath)) - m_WebSocketHandlers.Remove(servicepath); + m_WebSocketHandlers.TryRemove(servicepath, out WebSocketRequestDelegate dummy); } public List GetStreamHandlerKeys() @@ -548,24 +542,30 @@ namespace OpenSim.Framework.Servers.HttpServer return null; } - public void OnRequest(object source, RequestEventArgs args) { RequestNumber++; try { - IHttpClientContext context = (IHttpClientContext)source; IHttpRequest request = args.Request; + OSHttpRequest osRequest = new OSHttpRequest(request); + + if(m_WebSocketHandlers.TryGetValue(osRequest.RawUrl, out WebSocketRequestDelegate dWebSocketRequestDelegate)) + { + dWebSocketRequestDelegate?.Invoke(osRequest.Url.AbsolutePath, new WebSocketHttpServerHandler(osRequest, 8192)); + return; + } + if (TryGetPollServiceHTTPHandler(Util.TrimEndSlash(request.UriPath), out PollServiceEventArgs psEvArgs)) { psEvArgs.RequestsReceived++; - PollServiceHttpRequest psreq = new PollServiceHttpRequest(psEvArgs, context, request); - psEvArgs.Request?.Invoke(psreq.RequestID, new OSHttpRequest(request)); + PollServiceHttpRequest psreq = new PollServiceHttpRequest(psEvArgs, request); + psEvArgs.Request?.Invoke(psreq.RequestID, osRequest); m_pollServiceManager.Enqueue(psreq); } else { - OnHandleRequestIOThread(request); + HandleRequest(osRequest, new OSHttpResponse(osRequest)); } } catch (Exception e) @@ -574,24 +574,6 @@ namespace OpenSim.Framework.Servers.HttpServer } } - private void OnHandleRequestIOThread(IHttpRequest request) - { - OSHttpRequest req = new OSHttpRequest(request); - WebSocketRequestDelegate dWebSocketRequestDelegate = null; - lock (m_WebSocketHandlers) - { - if (m_WebSocketHandlers.ContainsKey(req.RawUrl)) - dWebSocketRequestDelegate = m_WebSocketHandlers[req.RawUrl]; - } - if (dWebSocketRequestDelegate != null) - { - dWebSocketRequestDelegate(req.Url.AbsolutePath, new WebSocketHttpServerHandler(req, request.Context, 8192)); - return; - } - - HandleRequest(req, new OSHttpResponse(req)); - } - /// /// This methods is the start of incoming HTTP request handling. /// diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs index 179257d545..126c64c162 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs @@ -309,8 +309,7 @@ namespace OpenSim.Framework.Servers.HttpServer /// replying public OSHttpResponse(OSHttpRequest req) { - _httpResponse = new HttpResponse(req.IHttpClientContext, req.IHttpRequest); - + _httpResponse = new HttpResponse(req.IHttpRequest); } public OSHttpResponse(HttpResponse resp) diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs index 8a247086ce..cb97993f2d 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs @@ -35,14 +35,14 @@ namespace OSHttpServer /// Client that send the . /// Contains information of what the client want to receive. /// cannot be empty. - public HttpResponse(IHttpClientContext context, IHttpRequest request) + public HttpResponse(IHttpRequest request) { m_httpVersion = request.HttpVersion; if (string.IsNullOrEmpty(m_httpVersion)) m_httpVersion = "HTTP/1.1"; Status = HttpStatusCode.OK; - m_context = context; + m_context = request.Context; m_Connetion = request.Connection; requestID = request.ID; RawBufferStart = -1; diff --git a/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs b/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs index e1c2bb2725..63881bfba2 100644 --- a/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs +++ b/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs @@ -42,42 +42,17 @@ namespace OpenSim.Framework.Servers.HttpServer private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); public readonly PollServiceEventArgs PollServiceArgs; - public readonly IHttpClientContext HttpContext; public readonly IHttpRequest Request; public readonly int RequestTime; public readonly UUID RequestID; - public int contextHash; -/* - private void GenContextHash() - { - - Random rnd = new Random(); - contextHash = 0; - if (Request.Headers["remote_addr"] != null) - contextHash = (Request.Headers["remote_addr"]).GetHashCode() << 16; - else - contextHash = rnd.Next() << 16; - if (Request.Headers["remote_port"] != null) - { - string[] strPorts = Request.Headers["remote_port"].Split(new char[] { ',' }); - contextHash += Int32.Parse(strPorts[0]); - } - else - contextHash += rnd.Next() & 0xffff; - - } -*/ public PollServiceHttpRequest( - PollServiceEventArgs pPollServiceArgs, IHttpClientContext pHttpContext, IHttpRequest pRequest) + PollServiceEventArgs pPollServiceArgs, IHttpRequest pRequest) { PollServiceArgs = pPollServiceArgs; - HttpContext = pHttpContext; Request = pRequest; RequestTime = System.Environment.TickCount; RequestID = UUID.Random(); -// GenContextHash(); - contextHash = HttpContext.contextID; } internal void DoHTTPGruntWork(Hashtable responsedata) @@ -85,8 +60,7 @@ namespace OpenSim.Framework.Servers.HttpServer if (Request.Body.CanRead) Request.Body.Dispose(); - OSHttpResponse response - = new OSHttpResponse(new HttpResponse(HttpContext, Request)); + OSHttpResponse response = new OSHttpResponse(new HttpResponse(Request)); if (responsedata == null) { @@ -248,8 +222,7 @@ namespace OpenSim.Framework.Servers.HttpServer internal void DoHTTPstop() { - OSHttpResponse response - = new OSHttpResponse(new HttpResponse(HttpContext, Request)); + OSHttpResponse response = new OSHttpResponse(new HttpResponse(Request)); if(Request.Body.CanRead) Request.Body.Dispose(); diff --git a/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs b/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs index 98372211b3..454a266a9e 100755 --- a/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs +++ b/OpenSim/Framework/Servers/HttpServer/PollServiceRequestManager.cs @@ -161,13 +161,13 @@ namespace OpenSim.Framework.Servers.HttpServer Watchdog.UpdateThread(); - if (!req.HttpContext.CanSend()) + if (!req.Request.Context.CanSend()) { req.PollServiceArgs.Drop(req.RequestID, req.PollServiceArgs.Id); continue; } - if (req.HttpContext.IsSending()) + if (req.Request.Context.IsSending()) { ReQueueEvent(req); continue; diff --git a/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs b/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs index b1e5de4de7..a6361fe30b 100644 --- a/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs +++ b/OpenSim/Framework/Servers/HttpServer/WebsocketServerHandler.cs @@ -132,13 +132,13 @@ namespace OpenSim.Framework.Servers.HttpServer /// private const string WebsocketHandshakeAcceptHashConstant = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; - public WebSocketHttpServerHandler(OSHttpRequest preq, IHttpClientContext pContext, int bufferlen) + public WebSocketHttpServerHandler(OSHttpRequest preq, int bufferlen) : base(preq.HttpMethod, preq.Url.OriginalString) { _request = preq; - _networkContext = pContext.GiveMeTheNetworkStreamIKnowWhatImDoing(); + _networkContext = preq.IHttpClientContext.GiveMeTheNetworkStreamIKnowWhatImDoing(); _networkContext.Stream.ReadTimeout = _defaultReadTimeout; - _clientContext = pContext; + _clientContext = preq.IHttpClientContext; _bufferLength = bufferlen; _buffer = new byte[_bufferLength]; }