From d725d1208bfbeae02f181cc6731f5a98dc7fce6d Mon Sep 17 00:00:00 2001
From: Dr Scofield
Date: Mon, 19 May 2008 11:38:35 +0000
Subject: adding OSHttpRequest and OSHttpResponse which wrap
HttpListenerRequest and HttpListenerResponse respectively. enhancing
IStreamHandler and IStreamedHandler interfaces so that
OSHttp{Request,Response} get passed in, allowing RestHandlers to set response
status code, redirections, etc.
---
.../Capabilities/LLSDStreamHandler.cs | 4 +-
OpenSim/Framework/Servers/BaseHttpServer.cs | 24 +--
OpenSim/Framework/Servers/BaseStreamHandler.cs | 4 +-
OpenSim/Framework/Servers/BinaryStreamHandler.cs | 3 +-
OpenSim/Framework/Servers/IStreamHandler.cs | 5 +-
OpenSim/Framework/Servers/OSHttpRequest.cs | 145 ++++++++++++++++++
OpenSim/Framework/Servers/OSHttpResponse.cs | 165 +++++++++++++++++++++
.../Framework/Servers/RestDeserialiseHandler.cs | 4 +-
OpenSim/Framework/Servers/RestStreamHandler.cs | 3 +-
9 files changed, 338 insertions(+), 19 deletions(-)
create mode 100644 OpenSim/Framework/Servers/OSHttpRequest.cs
create mode 100644 OpenSim/Framework/Servers/OSHttpResponse.cs
(limited to 'OpenSim/Framework')
diff --git a/OpenSim/Framework/Communications/Capabilities/LLSDStreamHandler.cs b/OpenSim/Framework/Communications/Capabilities/LLSDStreamHandler.cs
index 8683cea..c5cb268 100644
--- a/OpenSim/Framework/Communications/Capabilities/LLSDStreamHandler.cs
+++ b/OpenSim/Framework/Communications/Capabilities/LLSDStreamHandler.cs
@@ -27,6 +27,7 @@
using System.Collections;
using System.IO;
+using System.Net;
using System.Text;
using OpenSim.Framework.Servers;
@@ -43,7 +44,8 @@ namespace OpenSim.Framework.Communications.Capabilities
m_method = method;
}
- public override byte[] Handle(string path, Stream request)
+ public override byte[] Handle(string path, Stream request,
+ OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
//Encoding encoding = Encoding.UTF8;
//StreamReader streamReader = new StreamReader(request, false);
diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs
index 1eb1da9..eeb63e1 100644
--- a/OpenSim/Framework/Servers/BaseHttpServer.cs
+++ b/OpenSim/Framework/Servers/BaseHttpServer.cs
@@ -136,8 +136,8 @@ namespace OpenSim.Framework.Servers
{
HttpListenerContext context = (HttpListenerContext) stateinfo;
- HttpListenerRequest request = context.Request;
- HttpListenerResponse response = context.Response;
+ OSHttpRequest request = new OSHttpRequest(context.Request);
+ OSHttpResponse response = new OSHttpResponse(context.Response);
response.KeepAlive = false;
response.SendChunked = false;
@@ -157,7 +157,7 @@ namespace OpenSim.Framework.Servers
{
IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler;
- buffer = streamedRequestHandler.Handle(path, request.InputStream);
+ buffer = streamedRequestHandler.Handle(path, request.InputStream, request, response);
}
else
{
@@ -165,14 +165,14 @@ namespace OpenSim.Framework.Servers
using (MemoryStream memoryStream = new MemoryStream())
{
- streamHandler.Handle(path, request.InputStream, memoryStream);
+ streamHandler.Handle(path, request.InputStream, memoryStream, request, response);
memoryStream.Flush();
buffer = memoryStream.ToArray();
}
}
request.InputStream.Close();
- response.ContentType = requestHandler.ContentType;
+ if (!response.IsContentTypeSet) response.ContentType = requestHandler.ContentType;
response.ContentLength64 = buffer.LongLength;
try
@@ -280,7 +280,7 @@ namespace OpenSim.Framework.Servers
///
///
///
- private void HandleXmlRpcRequests(HttpListenerRequest request, HttpListenerResponse response)
+ private void HandleXmlRpcRequests(OSHttpRequest request, OSHttpResponse response)
{
Stream requestStream = request.InputStream;
@@ -358,7 +358,7 @@ namespace OpenSim.Framework.Servers
}
}
- private void HandleLLSDRequests(HttpListenerRequest request, HttpListenerResponse response)
+ private void HandleLLSDRequests(OSHttpRequest request, OSHttpResponse response)
{
Stream requestStream = request.InputStream;
@@ -416,7 +416,7 @@ namespace OpenSim.Framework.Servers
}
}
- public void HandleHTTPRequest(HttpListenerRequest request, HttpListenerResponse response)
+ public void HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response)
{
switch (request.HttpMethod)
{
@@ -430,7 +430,7 @@ namespace OpenSim.Framework.Servers
}
}
- private void HandleContentVerbs(HttpListenerRequest request, HttpListenerResponse response)
+ private void HandleContentVerbs(OSHttpRequest request, OSHttpResponse response)
{
// This is a test. There's a workable alternative.. as this way sucks.
// We'd like to put this into a text file parhaps that's easily editable.
@@ -505,7 +505,7 @@ namespace OpenSim.Framework.Servers
}
}
- private static void DoHTTPGruntWork(Hashtable responsedata, HttpListenerResponse response)
+ private static void DoHTTPGruntWork(Hashtable responsedata, OSHttpResponse response)
{
int responsecode = (int)responsedata["int_response_code"];
string responseString = (string)responsedata["str_response_string"];
@@ -552,7 +552,7 @@ namespace OpenSim.Framework.Servers
}
}
- public void SendHTML404(HttpListenerResponse response, string host)
+ public void SendHTML404(OSHttpResponse response, string host)
{
// I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
response.StatusCode = 200;
@@ -579,7 +579,7 @@ namespace OpenSim.Framework.Servers
}
}
- public void SendHTML500(HttpListenerResponse response)
+ public void SendHTML500(OSHttpResponse response)
{
// I know this statuscode is dumb, but the client doesn't respond to 404s and 500s
response.StatusCode = 200;
diff --git a/OpenSim/Framework/Servers/BaseStreamHandler.cs b/OpenSim/Framework/Servers/BaseStreamHandler.cs
index 91ebc2c..a81a1b7 100644
--- a/OpenSim/Framework/Servers/BaseStreamHandler.cs
+++ b/OpenSim/Framework/Servers/BaseStreamHandler.cs
@@ -26,12 +26,14 @@
*/
using System.IO;
+using System.Net;
namespace OpenSim.Framework.Servers
{
public abstract class BaseStreamHandler : BaseRequestHandler, IStreamedRequestHandler
{
- public abstract byte[] Handle(string path, Stream request);
+ public abstract byte[] Handle(string path, Stream request,
+ OSHttpRequest httpRequest, OSHttpResponse httpResponse);
protected BaseStreamHandler(string httpMethod, string path) : base(httpMethod, path)
{
diff --git a/OpenSim/Framework/Servers/BinaryStreamHandler.cs b/OpenSim/Framework/Servers/BinaryStreamHandler.cs
index dcd1cba..26a6f10 100644
--- a/OpenSim/Framework/Servers/BinaryStreamHandler.cs
+++ b/OpenSim/Framework/Servers/BinaryStreamHandler.cs
@@ -27,6 +27,7 @@
using System.IO;
using System.Text;
+using System.Net;
namespace OpenSim.Framework.Servers
{
@@ -36,7 +37,7 @@ namespace OpenSim.Framework.Servers
{
private BinaryMethod m_method;
- public override byte[] Handle(string path, Stream request)
+ public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
byte[] data = ReadFully(request);
string param = GetParam(path);
diff --git a/OpenSim/Framework/Servers/IStreamHandler.cs b/OpenSim/Framework/Servers/IStreamHandler.cs
index de6fdf5..b3ce34d 100644
--- a/OpenSim/Framework/Servers/IStreamHandler.cs
+++ b/OpenSim/Framework/Servers/IStreamHandler.cs
@@ -27,6 +27,7 @@
using System.Collections;
using System.IO;
+using System.Net;
namespace OpenSim.Framework.Servers
{
@@ -45,13 +46,13 @@ namespace OpenSim.Framework.Servers
public interface IStreamedRequestHandler : IRequestHandler
{
// Handle request stream, return byte array
- byte[] Handle(string path, Stream request);
+ byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse);
}
public interface IStreamHandler : IRequestHandler
{
// Handle request stream, return byte array
- void Handle(string path, Stream request, Stream response);
+ void Handle(string path, Stream request, Stream response, OSHttpRequest httpReqbuest, OSHttpResponse httpResponse);
}
public interface IGenericHTTPHandler : IRequestHandler
{
diff --git a/OpenSim/Framework/Servers/OSHttpRequest.cs b/OpenSim/Framework/Servers/OSHttpRequest.cs
new file mode 100644
index 0000000..212e224
--- /dev/null
+++ b/OpenSim/Framework/Servers/OSHttpRequest.cs
@@ -0,0 +1,145 @@
+/*
+ * Copyright (c) Contributors, http://opensimulator.org/
+ * See CONTRIBUTORS.TXT for a full list of copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the OpenSim Project nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+using System;
+using System.Collections.Specialized;
+using System.Net;
+using System.IO;
+using System.Text;
+
+namespace OpenSim.Framework.Servers
+{
+ public class OSHttpRequest
+ {
+ private string[] _acceptTypes;
+ private Encoding _contentEncoding;
+ private long _contentLength64;
+ private string _contentType;
+ private CookieCollection _cookies;
+ private NameValueCollection _headers;
+ private string _httpMethod;
+ private Stream _inputStream;
+ private bool _isSecureConnection;
+ private bool _keepAlive;
+ private string _rawUrl;
+ private Uri _url;
+ private NameValueCollection _queryString;
+ private string _userAgent;
+
+ public string[] AcceptTypes
+ {
+ get { return _acceptTypes; }
+ }
+
+ public Encoding ContentEncoding
+ {
+ get { return _contentEncoding; }
+ }
+
+ public long ContentLength
+ {
+ get { return _contentLength64; }
+ }
+
+ public string ContentType
+ {
+ get { return _contentType; }
+ }
+
+ public CookieCollection Cookies
+ {
+ get { return _cookies; }
+ }
+
+ public NameValueCollection Headers
+ {
+ get { return _headers; }
+ }
+
+ public string HttpMethod
+ {
+ get { return _httpMethod; }
+ }
+
+ public Stream InputStream
+ {
+ get { return _inputStream; }
+ }
+
+ public bool IsSecureConnection
+ {
+ get { return _isSecureConnection; }
+ }
+
+ public bool KeepAlive
+ {
+ get { return _keepAlive; }
+ }
+
+ public string RawUrl
+ {
+ get { return _rawUrl; }
+ }
+
+ public Uri Url
+ {
+ get { return _url; }
+ }
+
+ public string UserAgent
+ {
+ get { return _userAgent; }
+ }
+
+ public NameValueCollection QueryString
+ {
+ get { return _queryString; }
+ }
+
+ public OSHttpRequest()
+ {
+ }
+
+ public OSHttpRequest(HttpListenerRequest req)
+ {
+ _acceptTypes = req.AcceptTypes;
+ _contentEncoding = req.ContentEncoding;
+ _contentLength64 = req.ContentLength64;
+ _contentType = req.ContentType;
+ _cookies = req.Cookies;
+ _headers = req.Headers;
+ _httpMethod = req.HttpMethod;
+ _inputStream = req.InputStream;
+ _isSecureConnection = req.IsSecureConnection;
+ _keepAlive = req.KeepAlive;
+ _rawUrl = req.RawUrl;
+ _url = req.Url;
+ _queryString = req.QueryString;
+ _userAgent = req.UserAgent;
+ }
+ }
+}
diff --git a/OpenSim/Framework/Servers/OSHttpResponse.cs b/OpenSim/Framework/Servers/OSHttpResponse.cs
new file mode 100644
index 0000000..28d513a
--- /dev/null
+++ b/OpenSim/Framework/Servers/OSHttpResponse.cs
@@ -0,0 +1,165 @@
+/*
+ * Copyright (c) Contributors, http://opensimulator.org/
+ * See CONTRIBUTORS.TXT for a full list of copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the OpenSim Project nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+using System.Collections;
+using System.IO;
+using System.Net;
+using System.Text;
+
+namespace OpenSim.Framework.Servers
+{
+ public class OSHttpResponse
+ {
+ private string _contentType;
+ private bool _contentTypeSet;
+ public string ContentType
+ {
+ get { return _contentType; }
+ set
+ {
+ _contentType = value;
+ _contentTypeSet = true;
+ }
+ }
+ public bool IsContentTypeSet
+ {
+ get { return _contentTypeSet; }
+ }
+
+ private long _contentLength64;
+ public long ContentLength64
+ {
+ get { return _contentLength64; }
+ set
+ {
+ _contentLength64 = value;
+ if (null != _resp) _resp.ContentLength64 = value;
+ }
+ }
+
+ private Encoding _contentEncoding;
+ public Encoding ContentEncoding
+ {
+ get { return _contentEncoding; }
+ set
+ {
+ _contentEncoding = value;
+ if (null != _resp) _resp.ContentEncoding = value;
+ }
+ }
+
+ public WebHeaderCollection Headers;
+ public CookieCollection Cookies;
+
+ private bool _keepAlive;
+ public bool KeepAlive
+ {
+ get { return _keepAlive; }
+ set
+ {
+ _keepAlive = value;
+ if (null != _resp) _resp.KeepAlive = value;
+ }
+ }
+
+ public Stream OutputStream;
+
+ private string _redirectLocation;
+ public string RedirectLocation
+ {
+ get { return _redirectLocation; }
+ set
+ {
+ _redirectLocation = value;
+ if (null != _resp) _resp.RedirectLocation = value;
+ }
+ }
+
+ private bool _sendChunked;
+ public bool SendChunked
+ {
+ get { return _sendChunked; }
+ set
+ {
+ _sendChunked = value;
+ if (null != _resp) _resp.SendChunked = value;
+ }
+ }
+
+ private int _statusCode;
+ public int StatusCode
+ {
+ get { return _statusCode; }
+ set
+ {
+ _statusCode = value;
+ if (null != _resp) _resp.StatusCode = value;
+ }
+ }
+
+ private string _statusDescription;
+ public string StatusDescription
+ {
+ get { return _statusDescription; }
+ set
+ {
+ _statusDescription = value;
+ if (null != _resp) _resp.StatusDescription = value;
+ }
+ }
+
+ private HttpListenerResponse _resp;
+
+ public OSHttpResponse()
+ {
+ }
+
+ public OSHttpResponse(HttpListenerResponse resp)
+ {
+ ContentEncoding = resp.ContentEncoding;
+ ContentLength64 = resp.ContentLength64;
+ _contentType = resp.ContentType;
+ Headers = resp.Headers;
+ Cookies = resp.Cookies;
+ KeepAlive = resp.KeepAlive;
+ OutputStream = resp.OutputStream;
+ RedirectLocation = resp.RedirectLocation;
+ SendChunked = resp.SendChunked;
+ StatusCode = resp.StatusCode;
+ StatusDescription = resp.StatusDescription;
+
+ _contentTypeSet = false;
+
+ _resp = resp;
+ }
+
+ public void AddHeader(string key, string value)
+ {
+ Headers.Add(key, value);
+ }
+ }
+}
diff --git a/OpenSim/Framework/Servers/RestDeserialiseHandler.cs b/OpenSim/Framework/Servers/RestDeserialiseHandler.cs
index 39f440f..fcc8839 100644
--- a/OpenSim/Framework/Servers/RestDeserialiseHandler.cs
+++ b/OpenSim/Framework/Servers/RestDeserialiseHandler.cs
@@ -26,6 +26,7 @@
*/
using System.IO;
+using System.Net;
using System.Xml;
using System.Xml.Serialization;
@@ -44,7 +45,8 @@ namespace OpenSim.Framework.Servers
m_method = method;
}
- public void Handle(string path, Stream request, Stream responseStream)
+ public void Handle(string path, Stream request, Stream responseStream,
+ OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
TRequest deserial;
using (XmlTextReader xmlReader = new XmlTextReader(request))
diff --git a/OpenSim/Framework/Servers/RestStreamHandler.cs b/OpenSim/Framework/Servers/RestStreamHandler.cs
index 76dbd73..301b0a9 100644
--- a/OpenSim/Framework/Servers/RestStreamHandler.cs
+++ b/OpenSim/Framework/Servers/RestStreamHandler.cs
@@ -27,6 +27,7 @@
using System.IO;
using System.Text;
+using System.Net;
namespace OpenSim.Framework.Servers
{
@@ -39,7 +40,7 @@ namespace OpenSim.Framework.Servers
get { return m_restMethod; }
}
- public override byte[] Handle(string path, Stream request)
+ public override byte[] Handle(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
Encoding encoding = Encoding.UTF8;
StreamReader streamReader = new StreamReader(request, encoding);
--
cgit v1.1