aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework
diff options
context:
space:
mode:
authorlbsa712007-07-04 14:12:32 +0000
committerlbsa712007-07-04 14:12:32 +0000
commit6a2588454a1ac4bb484ad0b9ee648e9ac156f8db (patch)
treefd4e0d33f65e36365d15d991f95d323e6d15e8b1 /OpenSim/Framework
parent* Added StreamHandler support (diff)
downloadopensim-SC_OLD-6a2588454a1ac4bb484ad0b9ee648e9ac156f8db.zip
opensim-SC_OLD-6a2588454a1ac4bb484ad0b9ee648e9ac156f8db.tar.gz
opensim-SC_OLD-6a2588454a1ac4bb484ad0b9ee648e9ac156f8db.tar.bz2
opensim-SC_OLD-6a2588454a1ac4bb484ad0b9ee648e9ac156f8db.tar.xz
* Removed AssetHttpServer, using BaseHttpServer instead
* Removed legacy REST handling * Created two custom IStreamHandlers for asset up/download * Removed quite a lot of double and triple encodings, trying to work towards binary only and direct write into storage. * Introduced BaseStreamHandler with GetParam() and some other goodies
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/Servers/BaseHttpServer.cs163
-rw-r--r--OpenSim/Framework/Servers/BaseStreamHandler.cs40
-rw-r--r--OpenSim/Framework/Servers/IStreamHandler.cs3
-rw-r--r--OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj3
-rw-r--r--OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build1
-rw-r--r--OpenSim/Framework/Servers/RestStreamHandler.cs30
6 files changed, 143 insertions, 97 deletions
diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs
index 9831108..aed538b 100644
--- a/OpenSim/Framework/Servers/BaseHttpServer.cs
+++ b/OpenSim/Framework/Servers/BaseHttpServer.cs
@@ -42,48 +42,56 @@ namespace OpenSim.Framework.Servers
42 { 42 {
43 protected Thread m_workerThread; 43 protected Thread m_workerThread;
44 protected HttpListener m_httpListener; 44 protected HttpListener m_httpListener;
45 protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>(); 45 //protected Dictionary<string, RestMethodEntry> m_restHandlers = new Dictionary<string, RestMethodEntry>();
46 protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); 46 protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>();
47 protected Dictionary<string, IStreamHandler> m_streamHandlers = new Dictionary<string, IStreamHandler>(); 47 protected Dictionary<string, IStreamHandler> m_streamHandlers = new Dictionary<string, IStreamHandler>();
48 protected int m_port; 48 protected int m_port;
49 protected bool firstcaps = true; 49 protected bool m_firstcaps = true;
50 50
51 public BaseHttpServer(int port) 51 public BaseHttpServer(int port)
52 { 52 {
53 m_port = port; 53 m_port = port;
54 } 54 }
55 55
56 public void AddStreamHandler( string path, IStreamHandler handler) 56 public void AddStreamHandler( IStreamHandler handler)
57 { 57 {
58 string handlerKey = handler.HttpMethod + ":" + path; 58 string httpMethod = handler.HttpMethod;
59 string path = handler.Path;
60
61 string handlerKey = GetHandlerKey(httpMethod, path);
59 m_streamHandlers.Add(handlerKey, handler); 62 m_streamHandlers.Add(handlerKey, handler);
60 } 63 }
61 64
62 public bool AddRestHandler(string method, string path, RestMethod handler) 65 private static string GetHandlerKey(string httpMethod, string path)
63 { 66 {
64 //Console.WriteLine("adding new REST handler for path " + path); 67 return httpMethod + ":" + path;
65 string methodKey = String.Format("{0}: {1}", method, path);
66
67 if (!this.m_restHandlers.ContainsKey(methodKey))
68 {
69 this.m_restHandlers.Add(methodKey, new RestMethodEntry(path, handler));
70 return true;
71 }
72
73 //must already have a handler for that path so return false
74 return false;
75 } 68 }
76 69
77 public bool RemoveRestHandler(string method, string path) 70 //public bool AddRestHandler(string method, string path, RestMethod handler)
78 { 71 //{
79 string methodKey = String.Format("{0}: {1}", method, path); 72 // //Console.WriteLine("adding new REST handler for path " + path);
80 if (this.m_restHandlers.ContainsKey(methodKey)) 73 // string methodKey = String.Format("{0}: {1}", method, path);
81 { 74
82 this.m_restHandlers.Remove(methodKey); 75 // if (!this.m_restHandlers.ContainsKey(methodKey))
83 return true; 76 // {
84 } 77 // this.m_restHandlers.Add(methodKey, new RestMethodEntry(path, handler));
85 return false; 78 // return true;
86 } 79 // }
80
81 // //must already have a handler for that path so return false
82 // return false;
83 //}
84
85 //public bool RemoveRestHandler(string method, string path)
86 //{
87 // string methodKey = String.Format("{0}: {1}", method, path);
88 // if (this.m_restHandlers.ContainsKey(methodKey))
89 // {
90 // this.m_restHandlers.Remove(methodKey);
91 // return true;
92 // }
93 // return false;
94 //}
87 95
88 public bool AddXmlRPCHandler(string method, XmlRpcMethod handler) 96 public bool AddXmlRPCHandler(string method, XmlRpcMethod handler)
89 { 97 {
@@ -119,40 +127,40 @@ namespace OpenSim.Framework.Servers
119 return XmlRpcResponseSerializer.Singleton.Serialize(response); 127 return XmlRpcResponseSerializer.Singleton.Serialize(response);
120 } 128 }
121 129
122 protected virtual string ParseREST(string request, string path, string method) 130 //protected virtual string ParseREST(string request, string path, string method)
123 { 131 //{
124 string response; 132 // string response;
125 133
126 string requestKey = String.Format("{0}: {1}", method, path); 134 // string requestKey = String.Format("{0}: {1}", method, path);
127 135
128 string bestMatch = String.Empty; 136 // string bestMatch = String.Empty;
129 foreach (string currentKey in m_restHandlers.Keys) 137 // foreach (string currentKey in m_restHandlers.Keys)
130 { 138 // {
131 if (requestKey.StartsWith(currentKey)) 139 // if (requestKey.StartsWith(currentKey))
132 { 140 // {
133 if (currentKey.Length > bestMatch.Length) 141 // if (currentKey.Length > bestMatch.Length)
134 { 142 // {
135 bestMatch = currentKey; 143 // bestMatch = currentKey;
136 } 144 // }
137 } 145 // }
138 } 146 // }
139 147
140 RestMethodEntry restMethodEntry; 148 // RestMethodEntry restMethodEntry;
141 if (m_restHandlers.TryGetValue(bestMatch, out restMethodEntry)) 149 // if (m_restHandlers.TryGetValue(bestMatch, out restMethodEntry))
142 { 150 // {
143 RestMethod restMethod = restMethodEntry.RestMethod; 151 // RestMethod restMethod = restMethodEntry.RestMethod;
144 152
145 string param = path.Substring(restMethodEntry.Path.Length); 153 // string param = path.Substring(restMethodEntry.Path.Length);
146 response = restMethod(request, path, param); 154 // response = restMethod(request, path, param);
147 155
148 } 156 // }
149 else 157 // else
150 { 158 // {
151 response = String.Empty; 159 // response = String.Empty;
152 } 160 // }
153 161
154 return response; 162 // return response;
155 } 163 //}
156 164
157 165
158 protected virtual string ParseXMLRPC(string requestBody) 166 protected virtual string ParseXMLRPC(string requestBody)
@@ -179,13 +187,13 @@ namespace OpenSim.Framework.Servers
179 response.SendChunked = false; 187 response.SendChunked = false;
180 188
181 string path = request.RawUrl; 189 string path = request.RawUrl;
182 string handlerKey = request.HttpMethod + ":" + path; 190 string handlerKey = GetHandlerKey( request.HttpMethod, path );
183 191
184 IStreamHandler streamHandler; 192 IStreamHandler streamHandler;
185 193
186 if (TryGetStreamHandler( handlerKey, out streamHandler)) 194 if (TryGetStreamHandler( handlerKey, out streamHandler))
187 { 195 {
188 byte[] buffer = streamHandler.Handle(path, request.InputStream ); 196 byte[] buffer = streamHandler.Handle(path, request.InputStream);
189 request.InputStream.Close(); 197 request.InputStream.Close();
190 198
191 response.ContentType = streamHandler.ContentType; 199 response.ContentType = streamHandler.ContentType;
@@ -253,25 +261,25 @@ namespace OpenSim.Framework.Servers
253 response.AddHeader("Content-type", "text/xml"); 261 response.AddHeader("Content-type", "text/xml");
254 break; 262 break;
255 263
256 case "application/xml": 264 //case "application/xml":
257 case "application/octet-stream": 265 //case "application/octet-stream":
258 // probably LLSD we hope, otherwise it should be ignored by the parser 266 // // probably LLSD we hope, otherwise it should be ignored by the parser
259 // responseString = ParseLLSDXML(requestBody); 267 // // responseString = ParseLLSDXML(requestBody);
260 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); 268 // responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
261 response.AddHeader("Content-type", "application/xml"); 269 // response.AddHeader("Content-type", "application/xml");
262 break; 270 // break;
263 271
264 case "application/x-www-form-urlencoded": 272 //case "application/x-www-form-urlencoded":
265 // a form data POST so send to the REST parser 273 // // a form data POST so send to the REST parser
266 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); 274 // responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
267 response.AddHeader("Content-type", "text/html"); 275 // response.AddHeader("Content-type", "text/html");
268 break; 276 // break;
269 277
270 case null: 278 //case null:
271 // must be REST or invalid crap, so pass to the REST parser 279 // // must be REST or invalid crap, so pass to the REST parser
272 responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod); 280 // responseString = ParseREST(requestBody, request.RawUrl, request.HttpMethod);
273 response.AddHeader("Content-type", "text/html"); 281 // response.AddHeader("Content-type", "text/html");
274 break; 282 // break;
275 283
276 } 284 }
277 285
@@ -318,5 +326,10 @@ namespace OpenSim.Framework.Servers
318 } 326 }
319 } 327 }
320 328
329
330 public void RemoveStreamHandler(string httpMethod, string path)
331 {
332 m_streamHandlers.Remove(GetHandlerKey(httpMethod, path));
333 }
321 } 334 }
322} 335}
diff --git a/OpenSim/Framework/Servers/BaseStreamHandler.cs b/OpenSim/Framework/Servers/BaseStreamHandler.cs
new file mode 100644
index 0000000..95e9707
--- /dev/null
+++ b/OpenSim/Framework/Servers/BaseStreamHandler.cs
@@ -0,0 +1,40 @@
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5
6namespace OpenSim.Framework.Servers
7{
8 public abstract class BaseStreamHandler : IStreamHandler
9 {
10 public string ContentType
11 {
12 get { return "application/xml"; }
13 }
14
15 private string m_httpMethod;
16 public string HttpMethod
17 {
18 get { return m_httpMethod; }
19 }
20
21 private string m_path;
22 public string Path
23 {
24 get { return m_path; }
25 }
26
27 protected string GetParam( string path )
28 {
29 return path.Substring( m_path.Length );
30 }
31
32 public abstract byte[] Handle(string path, Stream request);
33
34 protected BaseStreamHandler(string path, string httpMethod )
35 {
36 m_httpMethod = httpMethod;
37 m_path = path;
38 }
39 }
40}
diff --git a/OpenSim/Framework/Servers/IStreamHandler.cs b/OpenSim/Framework/Servers/IStreamHandler.cs
index bc76e9c..6cab40d 100644
--- a/OpenSim/Framework/Servers/IStreamHandler.cs
+++ b/OpenSim/Framework/Servers/IStreamHandler.cs
@@ -15,5 +15,8 @@ namespace OpenSim.Framework.Servers
15 15
16 // Return required http method 16 // Return required http method
17 string HttpMethod { get;} 17 string HttpMethod { get;}
18
19 // Return path
20 string Path { get; }
18 } 21 }
19} 22}
diff --git a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj
index 555bd5d..4eb9844 100644
--- a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj
+++ b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.csproj
@@ -93,6 +93,9 @@
93 <Compile Include="BaseHttpServer.cs"> 93 <Compile Include="BaseHttpServer.cs">
94 <SubType>Code</SubType> 94 <SubType>Code</SubType>
95 </Compile> 95 </Compile>
96 <Compile Include="BaseStreamHandler.cs">
97 <SubType>Code</SubType>
98 </Compile>
96 <Compile Include="CheckSumServer.cs"> 99 <Compile Include="CheckSumServer.cs">
97 <SubType>Code</SubType> 100 <SubType>Code</SubType>
98 </Compile> 101 </Compile>
diff --git a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build
index a3d140f..5e96ef1 100644
--- a/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build
+++ b/OpenSim/Framework/Servers/OpenSim.Framework.Servers.dll.build
@@ -12,6 +12,7 @@
12 </resources> 12 </resources>
13 <sources failonempty="true"> 13 <sources failonempty="true">
14 <include name="BaseHttpServer.cs" /> 14 <include name="BaseHttpServer.cs" />
15 <include name="BaseStreamHandler.cs" />
15 <include name="CheckSumServer.cs" /> 16 <include name="CheckSumServer.cs" />
16 <include name="ILlsdMethodHandler.cs" /> 17 <include name="ILlsdMethodHandler.cs" />
17 <include name="IStreamHandler.cs" /> 18 <include name="IStreamHandler.cs" />
diff --git a/OpenSim/Framework/Servers/RestStreamHandler.cs b/OpenSim/Framework/Servers/RestStreamHandler.cs
index 64d6ea3..7ca369d 100644
--- a/OpenSim/Framework/Servers/RestStreamHandler.cs
+++ b/OpenSim/Framework/Servers/RestStreamHandler.cs
@@ -5,41 +5,27 @@ using System.IO;
5 5
6namespace OpenSim.Framework.Servers 6namespace OpenSim.Framework.Servers
7{ 7{
8 public class RestStreamHandler : IStreamHandler 8 public class RestStreamHandler : BaseStreamHandler
9 { 9 {
10 RestMethod m_restMethod; 10 RestMethod m_restMethod;
11 11
12 private string m_contentType; 12 override public byte[] Handle(string path, Stream request )
13 public string ContentType
14 {
15 get { return m_contentType; }
16 }
17
18 private string m_httpMethod;
19 public string HttpMethod
20 {
21 get { return m_httpMethod; }
22 }
23
24
25 public byte[] Handle(string path, Stream request )
26 { 13 {
27 Encoding encoding = Encoding.UTF8; 14 Encoding encoding = Encoding.UTF8;
28 StreamReader reader = new StreamReader(request, encoding); 15 StreamReader streamReader = new StreamReader(request, encoding);
29 16
30 string requestBody = reader.ReadToEnd(); 17 string requestBody = streamReader.ReadToEnd();
31 reader.Close(); 18 streamReader.Close();
32 19
33 string responseString = m_restMethod(requestBody, path, m_httpMethod); 20 string param = GetParam(path);
21 string responseString = m_restMethod(requestBody, path, param );
34 22
35 return Encoding.UTF8.GetBytes(responseString); 23 return Encoding.UTF8.GetBytes(responseString);
36 } 24 }
37 25
38 public RestStreamHandler(RestMethod restMethod, string httpMethod, string contentType) 26 public RestStreamHandler(string httpMethod, string path, RestMethod restMethod) : base( path, httpMethod )
39 { 27 {
40 m_restMethod = restMethod; 28 m_restMethod = restMethod;
41 m_httpMethod = httpMethod;
42 m_contentType = contentType;
43 } 29 }
44 } 30 }
45} 31}