diff options
Diffstat (limited to 'OpenSim/Framework/Servers/BaseHttpServer.cs')
-rw-r--r-- | OpenSim/Framework/Servers/BaseHttpServer.cs | 69 |
1 files changed, 50 insertions, 19 deletions
diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs index a5e256b..91b5718 100644 --- a/OpenSim/Framework/Servers/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/BaseHttpServer.cs | |||
@@ -120,6 +120,10 @@ namespace OpenSim.Framework.Servers | |||
120 | return false; | 120 | return false; |
121 | } | 121 | } |
122 | 122 | ||
123 | // Note that the agent string is provided simply to differentiate | ||
124 | // the handlers - it is NOT required to be an actual agent header | ||
125 | // value. | ||
126 | |||
123 | public bool AddAgentHandler(string agent, IHttpAgentHandler handler) | 127 | public bool AddAgentHandler(string agent, IHttpAgentHandler handler) |
124 | { | 128 | { |
125 | if (!m_agentHandlers.ContainsKey(agent)) | 129 | if (!m_agentHandlers.ContainsKey(agent)) |
@@ -149,7 +153,7 @@ namespace OpenSim.Framework.Servers | |||
149 | { | 153 | { |
150 | HttpListenerContext context = (HttpListenerContext) stateinfo; | 154 | HttpListenerContext context = (HttpListenerContext) stateinfo; |
151 | 155 | ||
152 | OSHttpRequest request = new OSHttpRequest(context.Request); | 156 | OSHttpRequest request = new OSHttpRequest(context.Request); |
153 | OSHttpResponse response = new OSHttpResponse(context.Response); | 157 | OSHttpResponse response = new OSHttpResponse(context.Response); |
154 | 158 | ||
155 | if (request.UserAgent != null) | 159 | if (request.UserAgent != null) |
@@ -157,11 +161,11 @@ namespace OpenSim.Framework.Servers | |||
157 | 161 | ||
158 | IHttpAgentHandler agentHandler; | 162 | IHttpAgentHandler agentHandler; |
159 | 163 | ||
160 | if (TryGetAgentHandler(request.UserAgent, out agentHandler)) | 164 | if (TryGetAgentHandler(request, response, out agentHandler)) |
161 | { | 165 | { |
162 | m_log.DebugFormat("[HTTP-AGENT] Handler located for {0}", request.UserAgent); | 166 | // m_log.DebugFormat("[HTTP-AGENT] Handler located for {0}", request.UserAgent); |
163 | HandleAgentRequest(agentHandler, request, response); | 167 | if(HandleAgentRequest(agentHandler, request, response)) |
164 | return; | 168 | return; |
165 | } | 169 | } |
166 | } | 170 | } |
167 | 171 | ||
@@ -301,14 +305,14 @@ namespace OpenSim.Framework.Servers | |||
301 | } | 305 | } |
302 | } | 306 | } |
303 | 307 | ||
304 | private bool TryGetAgentHandler(string agent, out IHttpAgentHandler agentHandler) | 308 | private bool TryGetAgentHandler(OSHttpRequest request, OSHttpResponse response, out IHttpAgentHandler agentHandler) |
305 | { | 309 | { |
306 | agentHandler = null; | 310 | agentHandler = null; |
307 | try | 311 | try |
308 | { | 312 | { |
309 | foreach(IHttpAgentHandler handler in m_agentHandlers.Values) | 313 | foreach(IHttpAgentHandler handler in m_agentHandlers.Values) |
310 | { | 314 | { |
311 | if(handler.Match(agent)) | 315 | if(handler.Match(request, response)) |
312 | { | 316 | { |
313 | agentHandler = handler; | 317 | agentHandler = handler; |
314 | return true; | 318 | return true; |
@@ -472,7 +476,7 @@ namespace OpenSim.Framework.Servers | |||
472 | /// <param name="request"></param> | 476 | /// <param name="request"></param> |
473 | /// <param name="response"></param> | 477 | /// <param name="response"></param> |
474 | 478 | ||
475 | private void HandleAgentRequest(IHttpAgentHandler handler, OSHttpRequest request, OSHttpResponse response) | 479 | private bool HandleAgentRequest(IHttpAgentHandler handler, OSHttpRequest request, OSHttpResponse response) |
476 | { | 480 | { |
477 | 481 | ||
478 | // In the case of REST, then handler is responsible for ALL aspects of | 482 | // In the case of REST, then handler is responsible for ALL aspects of |
@@ -480,17 +484,27 @@ namespace OpenSim.Framework.Servers | |||
480 | 484 | ||
481 | try | 485 | try |
482 | { | 486 | { |
483 | handler.Handle(request, response); | 487 | return handler.Handle(request, response); |
484 | } | 488 | } |
485 | catch (Exception e) | 489 | catch (Exception e) |
486 | { | 490 | { |
487 | m_log.Warn("[HTTP-AGENT]: Error - " + e.Message); | 491 | // If the handler did in fact close the stream, then this will blow |
488 | response.SendChunked = false; | 492 | // chunks, so that that doesn;t disturb anybody we throw away any |
489 | response.KeepAlive = false; | 493 | // and all exceptions raised. We've done our best to release the |
490 | response.StatusCode = 500; | 494 | // client. |
491 | response.OutputStream.Close(); | 495 | try |
496 | { | ||
497 | m_log.Warn("[HTTP-AGENT]: Error - " + e.Message); | ||
498 | response.SendChunked = false; | ||
499 | response.KeepAlive = false; | ||
500 | response.StatusCode = (int)OSHttpStatusCode.ServerErrorInternalError; | ||
501 | response.OutputStream.Close(); | ||
502 | } | ||
503 | catch(Exception){} | ||
492 | } | 504 | } |
493 | 505 | ||
506 | return true; | ||
507 | |||
494 | } | 508 | } |
495 | 509 | ||
496 | public void HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response) | 510 | public void HandleHTTPRequest(OSHttpRequest request, OSHttpResponse response) |
@@ -498,7 +512,7 @@ namespace OpenSim.Framework.Servers | |||
498 | switch (request.HttpMethod) | 512 | switch (request.HttpMethod) |
499 | { | 513 | { |
500 | case "OPTIONS": | 514 | case "OPTIONS": |
501 | response.StatusCode = 200; | 515 | response.StatusCode = (int)OSHttpStatusCode.SuccessOk; |
502 | return; | 516 | return; |
503 | 517 | ||
504 | default: | 518 | default: |
@@ -599,9 +613,9 @@ namespace OpenSim.Framework.Servers | |||
599 | // We're forgoing the usual error status codes here because the client | 613 | // We're forgoing the usual error status codes here because the client |
600 | // ignores anything but 200 and 301 | 614 | // ignores anything but 200 and 301 |
601 | 615 | ||
602 | response.StatusCode = 200; | 616 | response.StatusCode = (int)OSHttpStatusCode.SuccessOk; |
603 | 617 | ||
604 | if (responsecode == 301) | 618 | if (responsecode == (int)OSHttpStatusCode.RedirectMovedPermanently) |
605 | { | 619 | { |
606 | response.RedirectLocation = (string)responsedata["str_redirect_location"]; | 620 | response.RedirectLocation = (string)responsedata["str_redirect_location"]; |
607 | response.StatusCode = responsecode; | 621 | response.StatusCode = responsecode; |
@@ -632,7 +646,7 @@ namespace OpenSim.Framework.Servers | |||
632 | public void SendHTML404(OSHttpResponse response, string host) | 646 | public void SendHTML404(OSHttpResponse response, string host) |
633 | { | 647 | { |
634 | // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s | 648 | // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s |
635 | response.StatusCode = 200; | 649 | response.StatusCode = (int)OSHttpStatusCode.SuccessOk; |
636 | response.AddHeader("Content-type", "text/html"); | 650 | response.AddHeader("Content-type", "text/html"); |
637 | 651 | ||
638 | string responseString = GetHTTP404(host); | 652 | string responseString = GetHTTP404(host); |
@@ -659,7 +673,7 @@ namespace OpenSim.Framework.Servers | |||
659 | public void SendHTML500(OSHttpResponse response) | 673 | public void SendHTML500(OSHttpResponse response) |
660 | { | 674 | { |
661 | // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s | 675 | // I know this statuscode is dumb, but the client doesn't respond to 404s and 500s |
662 | response.StatusCode = 200; | 676 | response.StatusCode = (int)OSHttpStatusCode.SuccessOk; |
663 | response.AddHeader("Content-type", "text/html"); | 677 | response.AddHeader("Content-type", "text/html"); |
664 | 678 | ||
665 | string responseString = GetHTTP500(); | 679 | string responseString = GetHTTP500(); |
@@ -738,6 +752,23 @@ namespace OpenSim.Framework.Servers | |||
738 | m_HTTPHandlers.Remove(GetHandlerKey(httpMethod, path)); | 752 | m_HTTPHandlers.Remove(GetHandlerKey(httpMethod, path)); |
739 | } | 753 | } |
740 | 754 | ||
755 | // Remove the agent IF it is registered. Intercept the possible | ||
756 | // exception. | ||
757 | |||
758 | public bool RemoveAgentHandler(string agent, IHttpAgentHandler handler) | ||
759 | { | ||
760 | try | ||
761 | { | ||
762 | if(handler == m_agentHandlers[agent]) | ||
763 | { | ||
764 | m_agentHandlers.Remove(agent); | ||
765 | return true; | ||
766 | } | ||
767 | } | ||
768 | catch(KeyNotFoundException) {} | ||
769 | return false; | ||
770 | } | ||
771 | |||
741 | public string GetHTTP404(string host) | 772 | public string GetHTTP404(string host) |
742 | { | 773 | { |
743 | string file = Path.Combine(Util.configDir(), "http_404.html"); | 774 | string file = Path.Combine(Util.configDir(), "http_404.html"); |