cleaning up OSHttpResponse: note that read access to extra header

fields is GONE (HttpServer does not support that), you can read the
"normal" HTTP headers available via properties, and you can add
headers. also, it is now possible to set a timeout for KeepAlive (for
those clients that pay attention to it).

this also fixes the broken REST inventory/assets/appearance services,
they should be working again.

testcase for OSHttpResponse will follow.
0.6.0-stable
Dr Scofield 2008-10-06 21:59:43 +00:00
parent 348893ccac
commit ad04626737
4 changed files with 108 additions and 207 deletions

View File

@ -1213,7 +1213,7 @@ namespace OpenSim.ApplicationPlugins.Rest.Inventory
response.ContentLength64 = buffer.Length;
if (response.Headers.Get("Content-Encoding") == null)
if (response.ContentEncoding == null)
response.ContentEncoding = encoding;
response.SendChunked = chunked;
@ -1256,7 +1256,7 @@ namespace OpenSim.ApplicationPlugins.Rest.Inventory
handled = true;
DumpHeaders();
// DumpHeaders();
// if (request.InputStream != null)
// {
@ -1273,8 +1273,9 @@ namespace OpenSim.ApplicationPlugins.Rest.Inventory
// Closing the outputstream should complete the transmission process
Rest.Log.DebugFormat("{0} Closing output stream", MsgId);
response.OutputStream.Close();
Rest.Log.DebugFormat("{0} Sending response", MsgId);
// response.OutputStream.Close();
response.Send();
}
@ -1292,44 +1293,35 @@ namespace OpenSim.ApplicationPlugins.Rest.Inventory
internal void AddHeader(string hdr, string data)
{
if (Rest.DEBUG)
{
Rest.Log.DebugFormat("{0} Adding header: <{1}: {2}>",
MsgId, hdr, data);
if (response.Headers.Get(hdr) != null)
{
Rest.Log.DebugFormat("{0} Multipe {1} headers will be generated>",
MsgId, hdr);
}
}
response.Headers.Add(hdr, data);
if (Rest.DEBUG) Rest.Log.DebugFormat("{0} Adding header: <{1}: {2}>", MsgId, hdr, data);
response.AddHeader(hdr, data);
}
internal void RemoveHeader(string hdr)
{
if (Rest.DEBUG)
{
Rest.Log.DebugFormat("{0} Removing header: <{1}>", MsgId, hdr);
if (response.Headers.Get(hdr) == null)
{
Rest.Log.DebugFormat("{0} No such header existed",
MsgId, hdr);
}
}
response.Headers.Remove(hdr);
}
// internal void RemoveHeader(string hdr)
// {
// if (Rest.DEBUG)
// {
// Rest.Log.DebugFormat("{0} Removing header: <{1}>", MsgId, hdr);
// if (response.Headers.Get(hdr) == null)
// {
// Rest.Log.DebugFormat("{0} No such header existed",
// MsgId, hdr);
// }
// }
// response.Headers.Remove(hdr);
// }
internal void DumpHeaders()
{
if (Rest.DEBUG)
{
for (int i=0;i<response.Headers.Count;i++)
{
Rest.Log.DebugFormat("{0} Header[{1}] : {2}", MsgId, i,
response.Headers.Get(i));
}
}
}
// internal void DumpHeaders()
// {
// if (Rest.DEBUG)
// {
// for (int i=0;i<response.Headers.Count;i++)
// {
// Rest.Log.DebugFormat("{0} Header[{1}] : {2}", MsgId, i,
// response.Headers.Get(i));
// }
// }
// }
// Setup the XML writer for output

View File

@ -277,7 +277,7 @@ namespace OpenSim.Framework.Servers
}
IRequestHandler requestHandler;
response.KeepAlive = false;
response.KeepAlive = true;
response.SendChunked = false;
string path = request.RawUrl;
@ -634,7 +634,7 @@ namespace OpenSim.Framework.Servers
requestStream.Close();
//m_log.DebugFormat("[OGP]: {0}:{1}", request.RawUrl, requestBody);
response.KeepAlive = false;
response.KeepAlive = true;
LLSD llsdRequest = null;
LLSD llsdResponse = null;
@ -705,7 +705,7 @@ namespace OpenSim.Framework.Servers
response.SendChunked = false;
response.ContentLength64 = buffer.Length;
response.ContentEncoding = Encoding.UTF8;
response.KeepAlive = false;
response.KeepAlive = true;
try
{
@ -928,7 +928,7 @@ namespace OpenSim.Framework.Servers
{
m_log.Warn("[HTTP-AGENT]: Error - " + e.Message);
response.SendChunked = false;
response.KeepAlive = false;
response.KeepAlive = true;
response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError;
//response.OutputStream.Close();
try
@ -1128,6 +1128,7 @@ namespace OpenSim.Framework.Servers
int responsecode = (int)responsedata["int_response_code"];
string responseString = (string)responsedata["str_response_string"];
string contentType = (string)responsedata["content_type"];
if (responsedata.ContainsKey("error_status_text"))
{
response.StatusDescription = (string)responsedata["error_status_text"];

View File

@ -42,9 +42,8 @@ namespace OpenSim.Framework.Servers
{
private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected HttpServer.IHttpRequest _request = null;
protected HttpServer.IHttpClientContext _context = null;
protected IHttpRequest _request = null;
protected IHttpClientContext _context = null;
public string[] AcceptTypes
{
@ -137,8 +136,15 @@ namespace OpenSim.Framework.Servers
}
private string _userAgent;
internal IHttpRequest IHttpRequest
{
get { return _request; }
}
internal IHttpClientContext IHttpClientContext
{
get { return _context; }
}
/// <summary>
/// Internal whiteboard for handlers to store temporary stuff
@ -151,11 +157,9 @@ namespace OpenSim.Framework.Servers
private Dictionary<string, object> _whiteboard = new Dictionary<string, object>();
public OSHttpRequest()
{
}
public OSHttpRequest() {}
public OSHttpRequest(HttpServer.IHttpClientContext context, HttpServer.IHttpRequest req)
public OSHttpRequest(IHttpClientContext context, IHttpRequest req)
{
_request = req;
_context = context;

View File

@ -38,19 +38,8 @@ namespace OpenSim.Framework.Servers
/// OSHttpResponse is the OpenSim representation of an HTTP
/// response.
/// </summary>
/// <remarks>
/// OSHttpResponse is currently dual "homed" in that it support
/// both the .NET HttpListenerResponse and the HttpServer
/// HttpResponse (similar to OSHttpRequest); this duality is only
/// temporary and the .NET usage will disappear once the switch to
/// HttpServer is completed.
/// </remarks>
public class OSHttpResponse
{
// property code below is a bit messy, will all resolve to
// harmony once we've completed the switch
/// <summary>
/// Content type property.
/// </summary>
@ -62,22 +51,12 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.ContentType;
else
return _httpListenerResponse.ContentType;
return _httpResponse.ContentType;
}
set
{
if (HttpServer)
{
_httpResponse.ContentType = value;
}
else
{
_httpListenerResponse.ContentType = value;
_contentTypeSet = true;
}
_httpResponse.ContentType = value;
}
}
@ -102,17 +81,12 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.ContentLength;
else
return _httpListenerResponse.ContentLength64;
return _httpResponse.ContentLength;
}
set
{
if (HttpServer)
_httpResponse.ContentLength = value;
else
_httpListenerResponse.ContentLength64 = value;
_httpResponse.ContentLength = value;
}
}
@ -132,59 +106,55 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.Encoding;
else
return _httpListenerResponse.ContentEncoding;
return _httpResponse.Encoding;
}
set
{
if (HttpServer)
_httpResponse.Encoding = value;
else
_httpListenerResponse.ContentEncoding = value;
_httpResponse.Encoding = value;
}
}
/// <summary>
/// Headers of the response.
/// </summary>
public WebHeaderCollection Headers
{
get
{
if (HttpServer)
return null;
else
return _httpListenerResponse.Headers;
}
}
/// <summary>
/// Get or set the keep alive property.
/// </summary>
public bool KeepAlive
{
get
get
{
if (HttpServer)
return _httpResponse.Connection == ConnectionType.KeepAlive;
else
return _httpListenerResponse.KeepAlive;
return _httpResponse.Connection == ConnectionType.KeepAlive;
}
set
{
if (HttpServer)
if (value)
_httpResponse.Connection = ConnectionType.KeepAlive;
else
_httpResponse.Connection = ConnectionType.Close;
}
}
/// <summary>
/// Get or set the keep alive timeout property (default is
/// 20). Setting this to 0 also disables KeepAlive. Setting
/// this to something else but 0 also enable KeepAlive.
/// </summary>
public int KeepAliveTimeout
{
get
{
return _httpResponse.KeepAlive;
}
set
{
if (value == 0)
{
if (value == true)
_httpResponse.Connection = ConnectionType.KeepAlive;
else
_httpResponse.Connection = ConnectionType.Close;
_httpResponse.Connection = ConnectionType.Close;
_httpResponse.KeepAlive = 0;
}
else
_httpListenerResponse.KeepAlive = value;
{
_httpResponse.Connection = ConnectionType.KeepAlive;
_httpResponse.KeepAlive = value;
}
}
}
@ -198,10 +168,7 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.Body;
else
return _httpListenerResponse.OutputStream;
return _httpResponse.Body;
}
}
@ -209,18 +176,12 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.ProtocolVersion;
else
return _httpListenerResponse.ProtocolVersion.ToString();
return _httpResponse.ProtocolVersion;
}
set
{
if (HttpServer)
_httpResponse.ProtocolVersion = value;
else
_httpListenerResponse.ProtocolVersion = new Version(value); ;
_httpResponse.ProtocolVersion = value;
}
}
@ -231,9 +192,7 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.Body;
throw new Exception("[OSHttpResponse] mixed .NET and HttpServer access");
return _httpResponse.Body;
}
}
@ -245,10 +204,7 @@ namespace OpenSim.Framework.Servers
// get { return _redirectLocation; }
set
{
if (HttpServer)
_httpResponse.Redirect(value);
else
_httpListenerResponse.RedirectLocation = value;
_httpResponse.Redirect(value);
}
}
@ -260,18 +216,12 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.Chunked;
else
return _httpListenerResponse.SendChunked;
}
set
{
if (HttpServer)
_httpResponse.Chunked = value;
else
_httpListenerResponse.SendChunked = value;
_httpResponse.Chunked = value;
}
}
@ -282,18 +232,12 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return (int)_httpResponse.Status;
else
return _httpListenerResponse.StatusCode;
return (int)_httpResponse.Status;
}
set
{
if (HttpServer)
_httpResponse.Status = (HttpStatusCode)value;
else
_httpListenerResponse.StatusCode = value;
_httpResponse.Status = (HttpStatusCode)value;
}
}
@ -305,64 +249,35 @@ namespace OpenSim.Framework.Servers
{
get
{
if (HttpServer)
return _httpResponse.Reason;
else
return _httpListenerResponse.StatusDescription;
return _httpResponse.Reason;
}
set
{
if (HttpServer)
_httpResponse.Reason = value;
else
_httpListenerResponse.StatusDescription = value;
_httpResponse.Reason = value;
}
}
internal bool HttpServer
{
get { return null != _httpResponse; }
}
private HttpResponse _httpResponse;
private HttpListenerResponse _httpListenerResponse;
protected IHttpResponse _httpResponse;
internal HttpResponse HttpResponse
{
get { return _httpResponse; }
}
public OSHttpResponse() {}
public OSHttpResponse()
{
}
/// <summary>
/// Instantiate an OSHttpResponse object based on an
/// underlying .NET HttpListenerResponse.
/// </summary>
/// <remarks>
/// Almost deprecated; will go west to make once HttpServer
/// base takes over.
/// </remarks>
public OSHttpResponse(HttpListenerResponse resp)
{
_httpListenerResponse = resp;
}
public OSHttpResponse(HttpServer.HttpResponse resp)
public OSHttpResponse(IHttpResponse resp)
{
_httpResponse = resp;
}
/// <summary>
/// Instantiate an OSHttpResponse object from an OSHttpRequest
/// object.
/// </summary
/// <param name="req">Incoming OSHttpRequest to which we are
/// replying</param>
// public OSHttpResponse(OSHttpRequest req)
// {
// _httpResponse = new HttpResponse(req.HttpClientContext, req.HttpRequest);
// }
public OSHttpResponse(OSHttpRequest req)
{
_httpResponse = new HttpResponse(req.IHttpClientContext, req.IHttpRequest);
}
/// <summary>
/// Add a header field and content to the response.
@ -373,10 +288,7 @@ namespace OpenSim.Framework.Servers
/// value</param>
public void AddHeader(string key, string value)
{
if (HttpServer)
_httpResponse.AddHeader(key, value);
else
_httpListenerResponse.Headers.Add(key, value);
_httpResponse.AddHeader(key, value);
}
/// <summary>
@ -384,16 +296,8 @@ namespace OpenSim.Framework.Servers
/// </summary>
public void Send()
{
if (HttpServer)
{
_httpResponse.Body.Flush();
_httpResponse.Send();
}
else
{
OutputStream.Close();
}
_httpResponse.Body.Flush();
_httpResponse.Send();
}
}
}