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

View File

@ -277,7 +277,7 @@ namespace OpenSim.Framework.Servers
} }
IRequestHandler requestHandler; IRequestHandler requestHandler;
response.KeepAlive = false; response.KeepAlive = true;
response.SendChunked = false; response.SendChunked = false;
string path = request.RawUrl; string path = request.RawUrl;
@ -634,7 +634,7 @@ namespace OpenSim.Framework.Servers
requestStream.Close(); requestStream.Close();
//m_log.DebugFormat("[OGP]: {0}:{1}", request.RawUrl, requestBody); //m_log.DebugFormat("[OGP]: {0}:{1}", request.RawUrl, requestBody);
response.KeepAlive = false; response.KeepAlive = true;
LLSD llsdRequest = null; LLSD llsdRequest = null;
LLSD llsdResponse = null; LLSD llsdResponse = null;
@ -705,7 +705,7 @@ namespace OpenSim.Framework.Servers
response.SendChunked = false; response.SendChunked = false;
response.ContentLength64 = buffer.Length; response.ContentLength64 = buffer.Length;
response.ContentEncoding = Encoding.UTF8; response.ContentEncoding = Encoding.UTF8;
response.KeepAlive = false; response.KeepAlive = true;
try try
{ {
@ -928,7 +928,7 @@ namespace OpenSim.Framework.Servers
{ {
m_log.Warn("[HTTP-AGENT]: Error - " + e.Message); m_log.Warn("[HTTP-AGENT]: Error - " + e.Message);
response.SendChunked = false; response.SendChunked = false;
response.KeepAlive = false; response.KeepAlive = true;
response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError; response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError;
//response.OutputStream.Close(); //response.OutputStream.Close();
try try
@ -1128,6 +1128,7 @@ namespace OpenSim.Framework.Servers
int responsecode = (int)responsedata["int_response_code"]; int responsecode = (int)responsedata["int_response_code"];
string responseString = (string)responsedata["str_response_string"]; string responseString = (string)responsedata["str_response_string"];
string contentType = (string)responsedata["content_type"]; string contentType = (string)responsedata["content_type"];
if (responsedata.ContainsKey("error_status_text")) if (responsedata.ContainsKey("error_status_text"))
{ {
response.StatusDescription = (string)responsedata["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); private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
protected HttpServer.IHttpRequest _request = null; protected IHttpRequest _request = null;
protected HttpServer.IHttpClientContext _context = null; protected IHttpClientContext _context = null;
public string[] AcceptTypes public string[] AcceptTypes
{ {
@ -137,8 +136,15 @@ namespace OpenSim.Framework.Servers
} }
private string _userAgent; private string _userAgent;
internal IHttpRequest IHttpRequest
{
get { return _request; }
}
internal IHttpClientContext IHttpClientContext
{
get { return _context; }
}
/// <summary> /// <summary>
/// Internal whiteboard for handlers to store temporary stuff /// 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>(); 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; _request = req;
_context = context; _context = context;

View File

@ -38,19 +38,8 @@ namespace OpenSim.Framework.Servers
/// OSHttpResponse is the OpenSim representation of an HTTP /// OSHttpResponse is the OpenSim representation of an HTTP
/// response. /// response.
/// </summary> /// </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 public class OSHttpResponse
{ {
// property code below is a bit messy, will all resolve to
// harmony once we've completed the switch
/// <summary> /// <summary>
/// Content type property. /// Content type property.
/// </summary> /// </summary>
@ -62,22 +51,12 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return _httpResponse.ContentType;
return _httpResponse.ContentType;
else
return _httpListenerResponse.ContentType;
} }
set set
{ {
if (HttpServer) _httpResponse.ContentType = value;
{
_httpResponse.ContentType = value;
}
else
{
_httpListenerResponse.ContentType = value;
_contentTypeSet = true;
}
} }
} }
@ -102,17 +81,12 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return _httpResponse.ContentLength;
return _httpResponse.ContentLength;
else
return _httpListenerResponse.ContentLength64;
} }
set set
{ {
if (HttpServer) _httpResponse.ContentLength = value;
_httpResponse.ContentLength = value;
else
_httpListenerResponse.ContentLength64 = value;
} }
} }
@ -132,59 +106,55 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return _httpResponse.Encoding;
return _httpResponse.Encoding;
else
return _httpListenerResponse.ContentEncoding;
} }
set set
{ {
if (HttpServer) _httpResponse.Encoding = value;
_httpResponse.Encoding = value;
else
_httpListenerResponse.ContentEncoding = 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 public bool KeepAlive
{ {
get get
{ {
if (HttpServer) return _httpResponse.Connection == ConnectionType.KeepAlive;
return _httpResponse.Connection == ConnectionType.KeepAlive;
else
return _httpListenerResponse.KeepAlive;
} }
set 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.Close;
_httpResponse.Connection = ConnectionType.KeepAlive; _httpResponse.KeepAlive = 0;
else
_httpResponse.Connection = ConnectionType.Close;
} }
else else
_httpListenerResponse.KeepAlive = value; {
_httpResponse.Connection = ConnectionType.KeepAlive;
_httpResponse.KeepAlive = value;
}
} }
} }
@ -198,10 +168,7 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return _httpResponse.Body;
return _httpResponse.Body;
else
return _httpListenerResponse.OutputStream;
} }
} }
@ -209,18 +176,12 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return _httpResponse.ProtocolVersion;
return _httpResponse.ProtocolVersion;
else
return _httpListenerResponse.ProtocolVersion.ToString();
} }
set set
{ {
if (HttpServer) _httpResponse.ProtocolVersion = value;
_httpResponse.ProtocolVersion = value;
else
_httpListenerResponse.ProtocolVersion = new Version(value); ;
} }
} }
@ -231,9 +192,7 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return _httpResponse.Body;
return _httpResponse.Body;
throw new Exception("[OSHttpResponse] mixed .NET and HttpServer access");
} }
} }
@ -245,10 +204,7 @@ namespace OpenSim.Framework.Servers
// get { return _redirectLocation; } // get { return _redirectLocation; }
set set
{ {
if (HttpServer) _httpResponse.Redirect(value);
_httpResponse.Redirect(value);
else
_httpListenerResponse.RedirectLocation = value;
} }
} }
@ -260,18 +216,12 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer)
return _httpResponse.Chunked; return _httpResponse.Chunked;
else
return _httpListenerResponse.SendChunked;
} }
set set
{ {
if (HttpServer) _httpResponse.Chunked = value;
_httpResponse.Chunked = value;
else
_httpListenerResponse.SendChunked = value;
} }
} }
@ -282,18 +232,12 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return (int)_httpResponse.Status;
return (int)_httpResponse.Status;
else
return _httpListenerResponse.StatusCode;
} }
set set
{ {
if (HttpServer) _httpResponse.Status = (HttpStatusCode)value;
_httpResponse.Status = (HttpStatusCode)value;
else
_httpListenerResponse.StatusCode = value;
} }
} }
@ -305,64 +249,35 @@ namespace OpenSim.Framework.Servers
{ {
get get
{ {
if (HttpServer) return _httpResponse.Reason;
return _httpResponse.Reason;
else
return _httpListenerResponse.StatusDescription;
} }
set set
{ {
if (HttpServer) _httpResponse.Reason = value;
_httpResponse.Reason = value;
else
_httpListenerResponse.StatusDescription = value;
} }
} }
internal bool HttpServer protected IHttpResponse _httpResponse;
{
get { return null != _httpResponse; }
}
private HttpResponse _httpResponse;
private HttpListenerResponse _httpListenerResponse;
internal HttpResponse HttpResponse public OSHttpResponse() {}
{
get { return _httpResponse; }
}
public OSHttpResponse() public OSHttpResponse(IHttpResponse resp)
{
}
/// <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)
{ {
_httpResponse = resp; _httpResponse = resp;
} }
/// <summary> /// <summary>
/// Instantiate an OSHttpResponse object from an OSHttpRequest /// Instantiate an OSHttpResponse object from an OSHttpRequest
/// object. /// object.
/// </summary /// </summary
/// <param name="req">Incoming OSHttpRequest to which we are /// <param name="req">Incoming OSHttpRequest to which we are
/// replying</param> /// replying</param>
// public OSHttpResponse(OSHttpRequest req) public OSHttpResponse(OSHttpRequest req)
// { {
// _httpResponse = new HttpResponse(req.HttpClientContext, req.HttpRequest); _httpResponse = new HttpResponse(req.IHttpClientContext, req.IHttpRequest);
// } }
/// <summary> /// <summary>
/// Add a header field and content to the response. /// Add a header field and content to the response.
@ -373,10 +288,7 @@ namespace OpenSim.Framework.Servers
/// value</param> /// value</param>
public void AddHeader(string key, string value) public void AddHeader(string key, string value)
{ {
if (HttpServer) _httpResponse.AddHeader(key, value);
_httpResponse.AddHeader(key, value);
else
_httpListenerResponse.Headers.Add(key, value);
} }
/// <summary> /// <summary>
@ -384,16 +296,8 @@ namespace OpenSim.Framework.Servers
/// </summary> /// </summary>
public void Send() public void Send()
{ {
if (HttpServer) _httpResponse.Body.Flush();
{ _httpResponse.Send();
_httpResponse.Body.Flush();
_httpResponse.Send();
}
else
{
OutputStream.Close();
}
} }
} }
} }