clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27  
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Net;
33 using System.Reflection;
34 using System.Text;
35 using System.Text.RegularExpressions;
36 using System.Xml;
37 using log4net;
38 using Nwc.XmlRpc;
39  
40 namespace OpenSim.Framework.Servers.HttpServer
41 {
42 public delegate XmlRpcResponse OSHttpHttpProcessor(XmlRpcRequest request);
43  
44 public class OSHttpHttpHandler: OSHttpHandler
45 {
46 private static readonly ILog _log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47  
48 // contains handler for processing HTTP Request
49 private GenericHTTPMethod _handler;
50  
51 /// <summary>
52 /// Instantiate an HTTP handler.
53 /// </summary>
54 /// <param name="handler">a GenericHTTPMethod</param>
55 /// <param name="method">null or HTTP method regex</param>
56 /// <param name="path">null or path regex</param>
57 /// <param name="query">null or dictionary with query regexs</param>
58 /// <param name="headers">null or dictionary with header
59 /// regexs</param>
60 /// <param name="whitelist">null or IP address whitelist</param>
61 public OSHttpHttpHandler(GenericHTTPMethod handler, Regex method, Regex path,
62 Dictionary<string, Regex> query,
63 Dictionary<string, Regex> headers, Regex whitelist)
64 : base(method, path, query, headers, new Regex(@"^text/html", RegexOptions.IgnoreCase | RegexOptions.Compiled),
65 whitelist)
66 {
67 _handler = handler;
68 }
69  
70 /// <summary>
71 /// Instantiate an HTTP handler.
72 /// </summary>
73 /// <param name="handler">a GenericHTTPMethod</param>
74 public OSHttpHttpHandler(GenericHTTPMethod handler)
75 : this(handler, new Regex(@"^GET$", RegexOptions.IgnoreCase | RegexOptions.Compiled), null, null, null, null)
76 {
77 }
78  
79 /// <summary>
80 /// Invoked by OSHttpRequestPump.
81 /// </summary>
82 public override OSHttpHandlerResult Process(OSHttpRequest request)
83 {
84 // call handler method
85 Hashtable responseData = _handler(request.Query);
86  
87 int responseCode = (int)responseData["int_response_code"];
88 string responseString = (string)responseData["str_response_string"];
89 string contentType = (string)responseData["content_type"];
90  
91 //Even though only one other part of the entire code uses HTTPHandlers, we shouldn't expect this
92 //and should check for NullReferenceExceptions
93  
94 if (string.IsNullOrEmpty(contentType))
95 {
96 contentType = "text/html";
97 }
98  
99 OSHttpResponse response = new OSHttpResponse(request);
100  
101 // We're forgoing the usual error status codes here because the client
102 // ignores anything but 200 and 301
103  
104 response.StatusCode = (int)OSHttpStatusCode.SuccessOk;
105  
106 if (responseCode == (int)OSHttpStatusCode.RedirectMovedPermanently)
107 {
108 response.RedirectLocation = (string)responseData["str_redirect_location"];
109 response.StatusCode = responseCode;
110 }
111  
112 response.AddHeader("Content-type", contentType);
113  
114 byte[] buffer;
115  
116 if (!contentType.Contains("image"))
117 {
118 buffer = Encoding.UTF8.GetBytes(responseString);
119 }
120 else
121 {
122 buffer = Convert.FromBase64String(responseString);
123 }
124  
125 response.SendChunked = false;
126 response.ContentLength64 = buffer.Length;
127 response.ContentEncoding = Encoding.UTF8;
128  
129 try
130 {
131 response.Body.Write(buffer, 0, buffer.Length);
132 }
133 catch (Exception ex)
134 {
135 _log.ErrorFormat("[OSHttpHttpHandler]: Error: {0}", ex.Message);
136 }
137 finally
138 {
139 response.Send();
140 }
141  
142 return OSHttpHandlerResult.Done;
143 }
144 }
145 }