From 3f584dbf120e812e0f5234e651a4f02cf69c27e6 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Mon, 20 Dec 2021 11:07:26 +0000 Subject: [PATCH] add auxiliar httpresponse.Redirect(string url, [HttpStatusCode redirStatusCode = HttpStatusCode.Redirect]) --- .../GetTexture/GetTextureRobustHandler.cs | 3 +-- .../Servers/HttpServer/BaseHttpServer.cs | 10 +++------- .../HttpServer/Interfaces/IOSHttpResponse.cs | 7 +++++++ .../Servers/HttpServer/OSHttpResponse.cs | 10 ++++++++++ .../HttpServer/OSHttpServer/HttpResponse.cs | 16 +++++++++++++++- .../HttpServer/OSHttpServer/IHttpResponse.cs | 7 +++++++ .../Servers/HttpServer/PollServiceHttpRequest.cs | 5 +++-- .../Linden/Caps/ServerReleaseNotesModule.cs | 3 +-- .../Handlers/Asset/AssetServerGetHandler.cs | 12 ++++-------- OpenSim/Tests/Common/Mock/TestOSHttpResponse.cs | 8 +++++++- 10 files changed, 58 insertions(+), 23 deletions(-) diff --git a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureRobustHandler.cs b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureRobustHandler.cs index 10add50c50..dbe03e94da 100644 --- a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureRobustHandler.cs +++ b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureRobustHandler.cs @@ -136,9 +136,8 @@ namespace OpenSim.Capabilities.Handlers if(!String.IsNullOrEmpty(m_RedirectURL)) { string textureUrl = m_RedirectURL + "?texture_id=" + textureID.ToString(); + httpResponse.Redirect(textureUrl, HttpStatusCode.Moved); m_log.Debug("[GETTEXTURE]: Redirecting texture request to " + textureUrl); - httpResponse.StatusCode = (int)HttpStatusCode.Moved; - httpResponse.AddHeader("Location:", textureUrl); return true; } diff --git a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs index a2397449be..7869feb2bf 100644 --- a/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/HttpServer/BaseHttpServer.cs @@ -2031,8 +2031,7 @@ namespace OpenSim.Framework.Servers.HttpServer if (responsecode == (int)HttpStatusCode.Moved) { - response.AddHeader("Location:", (string)responsedata["str_redirect_location"]); - response.StatusCode = responsecode; + response.Redirect((string)responsedata["str_redirect_location"], HttpStatusCode.Moved); } response.AddHeader("Content-Type", contentType); @@ -2430,15 +2429,12 @@ namespace OpenSim.Framework.Servers.HttpServer if (httpRequest.QueryString.Count == 0) { - httpResponse.StatusCode = (int)HttpStatusCode.Redirect; - httpResponse.AddHeader("Location", "http://opensimulator.org"); + httpResponse.Redirect("http://opensimulator.org"); return; } if (httpRequest.QueryFlags.Contains("about")) { - - httpResponse.StatusCode = (int)HttpStatusCode.Redirect; - httpResponse.AddHeader("Location", "http://opensimulator.org/wiki/0.9.2.1_Release"); + httpResponse.Redirect("http://opensimulator.org/wiki/0.9.2.1_Release"); return; } if (!httpRequest.QueryAsDictionary.TryGetValue("method", out string methods) || string.IsNullOrWhiteSpace(methods)) diff --git a/OpenSim/Framework/Servers/HttpServer/Interfaces/IOSHttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/Interfaces/IOSHttpResponse.cs index e3c7b34d72..7a128489d1 100644 --- a/OpenSim/Framework/Servers/HttpServer/Interfaces/IOSHttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/Interfaces/IOSHttpResponse.cs @@ -119,6 +119,13 @@ namespace OpenSim.Framework.Servers.HttpServer double RequestTS { get; } + /// + /// Set response as a http redirect + /// + /// redirection target url + /// the response Status, must be Found, Redirect, Moved,MovedPermanently,RedirectKeepVerb, RedirectMethod, TemporaryRedirect. Defaults to Redirect + void Redirect(string url, HttpStatusCode redirStatusCode = HttpStatusCode.Redirect); + /// /// Add a header field and content to the response. /// diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs index c9f3a6981d..0015c10dc1 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpResponse.cs @@ -321,6 +321,16 @@ namespace OpenSim.Framework.Servers.HttpServer _httpResponse = resp; } + /// + /// Set response as a http redirect + /// + /// redirection target url + /// the response Status, must be Found, Redirect, Moved,MovedPermanently,RedirectKeepVerb, RedirectMethod, TemporaryRedirect. Defaults to Redirect + public void Redirect(string url, HttpStatusCode redirStatusCode = HttpStatusCode.Redirect) + { + _httpResponse.Redirect(url, redirStatusCode); + } + /// /// Add a header field and content to the response. /// diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs index 3f027fa622..73f87998f6 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs @@ -191,10 +191,24 @@ namespace OSHttpServer get { return m_cookies; } } + /// + /// Set response as a http redirect + /// + /// redirection target url + /// the response Status, must be Found, Redirect, Moved,MovedPermanently,RedirectKeepVerb, RedirectMethod, TemporaryRedirect. Defaults to Redirect + public void Redirect(string url, HttpStatusCode redirStatusCode = HttpStatusCode.Redirect) + { + if (HeadersSent) + throw new InvalidOperationException("Headers have already been sent."); + + m_headers["Location"] = url; + Status = redirStatusCode; + } + /// /// Add another header to the document. /// - /// Name of the header, case sensitive, use lower cases. + /// Name of the header, case sensitive. /// Header values can span over multiple lines as long as each line starts with a white space. New line chars should be \r\n /// If headers already been sent. /// If value conditions have not been met. diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpResponse.cs index a091294e37..819d935a58 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpResponse.cs @@ -112,6 +112,13 @@ namespace OSHttpServer /// ResponseCookies Cookies { get; } + /// + /// Set response as a http redirect + /// + /// redirection target url + /// the response Status, must be Found, Redirect, Moved,MovedPermanently,RedirectKeepVerb, RedirectMethod, TemporaryRedirect. Defaults to Redirect + void Redirect(string url, HttpStatusCode redirStatusCode = HttpStatusCode.Redirect); + /// /// Add another header to the document. /// diff --git a/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs b/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs index bbfe535924..784e84fa05 100644 --- a/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs +++ b/OpenSim/Framework/Servers/HttpServer/PollServiceHttpRequest.cs @@ -116,16 +116,17 @@ namespace OpenSim.Framework.Servers.HttpServer return; } - response.StatusCode = responsecode; if (responsecode == (int)HttpStatusCode.Moved) { - response.AddHeader("Location", (string)responsedata["str_redirect_location"]); + response.Redirect((string)responsedata["str_redirect_location"], HttpStatusCode.Moved); response.KeepAlive = false; PollServiceArgs.RequestsHandled++; response.Send(); return; } + response.StatusCode = responsecode; + if (responsedata.ContainsKey("http_protocol_version")) response.ProtocolVersion = (string)responsedata["http_protocol_version"]; diff --git a/OpenSim/Region/ClientStack/Linden/Caps/ServerReleaseNotesModule.cs b/OpenSim/Region/ClientStack/Linden/Caps/ServerReleaseNotesModule.cs index 60f26dc62b..4a92ae98ec 100644 --- a/OpenSim/Region/ClientStack/Linden/Caps/ServerReleaseNotesModule.cs +++ b/OpenSim/Region/ClientStack/Linden/Caps/ServerReleaseNotesModule.cs @@ -122,8 +122,7 @@ namespace OpenSim.Region.ClientStack.LindenCaps private void ProcessServerReleaseNotes(IOSHttpResponse httpResponse) { - httpResponse.StatusCode = (int)HttpStatusCode.Moved; - httpResponse.AddHeader("Location", m_ServerReleaseNotesURL); + httpResponse.Redirect(m_ServerReleaseNotesURL); } } } diff --git a/OpenSim/Server/Handlers/Asset/AssetServerGetHandler.cs b/OpenSim/Server/Handlers/Asset/AssetServerGetHandler.cs index 8bfc69026a..783b3d4bab 100644 --- a/OpenSim/Server/Handlers/Asset/AssetServerGetHandler.cs +++ b/OpenSim/Server/Handlers/Asset/AssetServerGetHandler.cs @@ -102,13 +102,11 @@ namespace OpenSim.Server.Handlers.Asset if (metadata != null) { - XmlSerializer xs = - new XmlSerializer(typeof(AssetMetadata)); + XmlSerializer xs = new XmlSerializer(typeof(AssetMetadata)); result = ServerUtils.SerializeResult(xs, metadata); httpResponse.StatusCode = (int)HttpStatusCode.OK; - httpResponse.ContentType = - SLUtil.SLAssetTypeToContentType(metadata.Type); + httpResponse.ContentType = SLUtil.SLAssetTypeToContentType(metadata.Type); } else { @@ -138,8 +136,7 @@ namespace OpenSim.Server.Handlers.Asset result = ServerUtils.SerializeResult(xs, asset); httpResponse.StatusCode = (int)HttpStatusCode.OK; - httpResponse.ContentType = - SLUtil.SLAssetTypeToContentType(asset.Type); + httpResponse.ContentType = SLUtil.SLAssetTypeToContentType(asset.Type); } else { @@ -158,12 +155,11 @@ namespace OpenSim.Server.Handlers.Asset if (httpResponse.StatusCode == (int)HttpStatusCode.NotFound && !string.IsNullOrEmpty(m_RedirectURL) && !string.IsNullOrEmpty(id)) { - httpResponse.StatusCode = (int)HttpStatusCode.Redirect; string rurl = m_RedirectURL; if (!path.StartsWith("/")) rurl += "/"; rurl += path; - httpResponse.AddHeader("Location", rurl); + httpResponse.Redirect(rurl); m_log.DebugFormat("[ASSET GET HANDLER]: Asset not found, redirecting to {0} ({1})", rurl, httpResponse.StatusCode); } return result; diff --git a/OpenSim/Tests/Common/Mock/TestOSHttpResponse.cs b/OpenSim/Tests/Common/Mock/TestOSHttpResponse.cs index 04255df8eb..6afcb05d8a 100644 --- a/OpenSim/Tests/Common/Mock/TestOSHttpResponse.cs +++ b/OpenSim/Tests/Common/Mock/TestOSHttpResponse.cs @@ -28,8 +28,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Net; using System.Text; -using System.Web; using OpenSim.Framework.Servers.HttpServer; namespace OpenSim.Tests.Common @@ -119,6 +119,12 @@ namespace OpenSim.Tests.Common public double RequestTS { get; } + /// + /// Set response as a http redirect + /// + /// redirection target url + /// the response Status, must be Redirect, Moved,MovedPermanently,RedirectKeepVerb, RedirectMethod, TemporaryRedirect. Defaults to Redirect + public void Redirect(string url, HttpStatusCode redirStatusCode = HttpStatusCode.Redirect) { throw new NotImplementedException(); } /// /// Add a header field and content to the response. ///