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.
0.6.0-stable
Dr Scofield 2008-05-19 11:38:35 +00:00
parent dfe8e2c502
commit d725d1208b
12 changed files with 351 additions and 24 deletions

View File

@ -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);

View File

@ -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
/// </summary>
/// <param name="request"></param>
/// <param name="response"></param>
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;

View File

@ -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)
{

View File

@ -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);

View File

@ -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
{

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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))

View File

@ -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);

View File

@ -27,6 +27,7 @@
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Xml;
@ -59,7 +60,8 @@ namespace OpenSim.Grid.AssetServer
m_assetProvider = assetProvider;
}
public override byte[] Handle(string path, Stream request)
public override byte[] Handle(string path, Stream request,
OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
string param = GetParam(path);
byte[] result = new byte[] {};
@ -121,7 +123,8 @@ namespace OpenSim.Grid.AssetServer
private OpenAsset_Main m_assetManager;
private IAssetProvider m_assetProvider;
public override byte[] Handle(string path, Stream request)
public override byte[] Handle(string path, Stream request,
OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
string param = GetParam(path);

View File

@ -27,6 +27,7 @@
using System;
using System.IO;
using System.Net;
using System.Reflection;
using libsecondlife;
using log4net;
@ -51,7 +52,8 @@ namespace OpenSim.Grid.MessagingServer
}
public override byte[] Handle(string path, Stream request)
public override byte[] Handle(string path, Stream request,
OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
string param = GetParam(path);
byte[] result = new byte[] {};
@ -84,7 +86,8 @@ namespace OpenSim.Grid.MessagingServer
{
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public override byte[] Handle(string path, Stream request)
public override byte[] Handle(string path, Stream request,
OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
string param = GetParam(path);

View File

@ -28,6 +28,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
@ -681,7 +682,8 @@ namespace OpenSim
/// </summary>
protected class SimStatusHandler : IStreamedRequestHandler
{
public byte[] Handle(string path, Stream request)
public byte[] Handle(string path, Stream request,
OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
return Encoding.UTF8.GetBytes("OK");
}