diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Servers/BaseHttpServer.cs | 34 | ||||
-rw-r--r-- | OpenSim/Framework/Servers/BaseRequestHandler.cs | 35 | ||||
-rw-r--r-- | OpenSim/Framework/Servers/BaseStreamHandler.cs | 30 | ||||
-rw-r--r-- | OpenSim/Framework/Servers/IStreamHandler.cs | 18 | ||||
-rw-r--r-- | OpenSim/Framework/Servers/RestDeserialiseHandler.cs | 30 |
5 files changed, 92 insertions, 55 deletions
diff --git a/OpenSim/Framework/Servers/BaseHttpServer.cs b/OpenSim/Framework/Servers/BaseHttpServer.cs index 52d2a2c..a4a7b2b 100644 --- a/OpenSim/Framework/Servers/BaseHttpServer.cs +++ b/OpenSim/Framework/Servers/BaseHttpServer.cs | |||
@@ -43,7 +43,7 @@ namespace OpenSim.Framework.Servers | |||
43 | protected Thread m_workerThread; | 43 | protected Thread m_workerThread; |
44 | protected HttpListener m_httpListener; | 44 | protected HttpListener m_httpListener; |
45 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); | 45 | protected Dictionary<string, XmlRpcMethod> m_rpcHandlers = new Dictionary<string, XmlRpcMethod>(); |
46 | protected Dictionary<string, IStreamHandler> m_streamHandlers = new Dictionary<string, IStreamHandler>(); | 46 | protected Dictionary<string, IRequestHandler> m_streamHandlers = new Dictionary<string, IRequestHandler>(); |
47 | protected int m_port; | 47 | protected int m_port; |
48 | protected bool m_firstcaps = true; | 48 | protected bool m_firstcaps = true; |
49 | 49 | ||
@@ -57,7 +57,7 @@ namespace OpenSim.Framework.Servers | |||
57 | m_port = port; | 57 | m_port = port; |
58 | } | 58 | } |
59 | 59 | ||
60 | public void AddStreamHandler(IStreamHandler handler) | 60 | public void AddStreamHandler(IRequestHandler handler) |
61 | { | 61 | { |
62 | string httpMethod = handler.HttpMethod; | 62 | string httpMethod = handler.HttpMethod; |
63 | string path = handler.Path; | 63 | string path = handler.Path; |
@@ -97,14 +97,32 @@ namespace OpenSim.Framework.Servers | |||
97 | string path = request.RawUrl; | 97 | string path = request.RawUrl; |
98 | string handlerKey = GetHandlerKey(request.HttpMethod, path); | 98 | string handlerKey = GetHandlerKey(request.HttpMethod, path); |
99 | 99 | ||
100 | IStreamHandler streamHandler; | 100 | IRequestHandler requestHandler; |
101 | 101 | ||
102 | if (TryGetStreamHandler(handlerKey, out streamHandler)) | 102 | if (TryGetStreamHandler(handlerKey, out requestHandler)) |
103 | { | 103 | { |
104 | byte[] buffer = streamHandler.Handle(path, request.InputStream); | 104 | // Okay, so this is bad, but should be considered temporary until everything is IStreamHandler. |
105 | request.InputStream.Close(); | 105 | byte[] buffer; |
106 | if (requestHandler is IStreamedRequestHandler) | ||
107 | { | ||
108 | IStreamedRequestHandler streamedRequestHandler = requestHandler as IStreamedRequestHandler; | ||
109 | buffer = streamedRequestHandler.Handle(path, request.InputStream); | ||
110 | |||
111 | } | ||
112 | else | ||
113 | { | ||
114 | IStreamHandler streamHandler = (IStreamHandler)requestHandler; | ||
106 | 115 | ||
107 | response.ContentType = streamHandler.ContentType; | 116 | using (MemoryStream memoryStream = new MemoryStream()) |
117 | { | ||
118 | streamHandler.Handle(path, request.InputStream, memoryStream); | ||
119 | memoryStream.Flush(); | ||
120 | buffer = memoryStream.ToArray(); | ||
121 | } | ||
122 | } | ||
123 | |||
124 | request.InputStream.Close(); | ||
125 | response.ContentType = requestHandler.ContentType; | ||
108 | response.ContentLength64 = buffer.LongLength; | 126 | response.ContentLength64 = buffer.LongLength; |
109 | response.OutputStream.Write(buffer, 0, buffer.Length); | 127 | response.OutputStream.Write(buffer, 0, buffer.Length); |
110 | response.OutputStream.Close(); | 128 | response.OutputStream.Close(); |
@@ -115,7 +133,7 @@ namespace OpenSim.Framework.Servers | |||
115 | } | 133 | } |
116 | } | 134 | } |
117 | 135 | ||
118 | private bool TryGetStreamHandler(string handlerKey, out IStreamHandler streamHandler) | 136 | private bool TryGetStreamHandler(string handlerKey, out IRequestHandler streamHandler) |
119 | { | 137 | { |
120 | string bestMatch = null; | 138 | string bestMatch = null; |
121 | 139 | ||
diff --git a/OpenSim/Framework/Servers/BaseRequestHandler.cs b/OpenSim/Framework/Servers/BaseRequestHandler.cs new file mode 100644 index 0000000..b357763 --- /dev/null +++ b/OpenSim/Framework/Servers/BaseRequestHandler.cs | |||
@@ -0,0 +1,35 @@ | |||
1 | namespace OpenSim.Framework.Servers | ||
2 | { | ||
3 | public class BaseRequestHandler | ||
4 | { | ||
5 | public virtual string ContentType | ||
6 | { | ||
7 | get { return "application/xml"; } | ||
8 | } | ||
9 | |||
10 | private readonly string m_httpMethod; | ||
11 | |||
12 | public virtual string HttpMethod | ||
13 | { | ||
14 | get { return m_httpMethod; } | ||
15 | } | ||
16 | |||
17 | private readonly string m_path; | ||
18 | |||
19 | protected BaseRequestHandler(string httpMethod, string path) | ||
20 | { | ||
21 | m_httpMethod = httpMethod; | ||
22 | m_path = path; | ||
23 | } | ||
24 | |||
25 | public virtual string Path | ||
26 | { | ||
27 | get { return m_path; } | ||
28 | } | ||
29 | |||
30 | protected string GetParam(string path) | ||
31 | { | ||
32 | return path.Substring(m_path.Length); | ||
33 | } | ||
34 | } | ||
35 | } \ No newline at end of file | ||
diff --git a/OpenSim/Framework/Servers/BaseStreamHandler.cs b/OpenSim/Framework/Servers/BaseStreamHandler.cs index cd99183..a17c6ec 100644 --- a/OpenSim/Framework/Servers/BaseStreamHandler.cs +++ b/OpenSim/Framework/Servers/BaseStreamHandler.cs | |||
@@ -30,38 +30,12 @@ using System.IO; | |||
30 | 30 | ||
31 | namespace OpenSim.Framework.Servers | 31 | namespace OpenSim.Framework.Servers |
32 | { | 32 | { |
33 | public abstract class BaseStreamHandler : IStreamHandler | 33 | public abstract class BaseStreamHandler : BaseRequestHandler, IStreamedRequestHandler |
34 | { | 34 | { |
35 | public virtual string ContentType | ||
36 | { | ||
37 | get { return "application/xml"; } | ||
38 | } | ||
39 | |||
40 | private string m_httpMethod; | ||
41 | |||
42 | public virtual string HttpMethod | ||
43 | { | ||
44 | get { return m_httpMethod; } | ||
45 | } | ||
46 | |||
47 | private string m_path; | ||
48 | |||
49 | public virtual string Path | ||
50 | { | ||
51 | get { return m_path; } | ||
52 | } | ||
53 | |||
54 | protected string GetParam(string path) | ||
55 | { | ||
56 | return path.Substring(m_path.Length); | ||
57 | } | ||
58 | |||
59 | public abstract byte[] Handle(string path, Stream request); | 35 | public abstract byte[] Handle(string path, Stream request); |
60 | 36 | ||
61 | protected BaseStreamHandler(string httpMethod, string path) | 37 | protected BaseStreamHandler(string httpMethod, string path) : base(httpMethod, path) |
62 | { | 38 | { |
63 | m_httpMethod = httpMethod; | ||
64 | m_path = path; | ||
65 | } | 39 | } |
66 | } | 40 | } |
67 | } \ No newline at end of file | 41 | } \ No newline at end of file |
diff --git a/OpenSim/Framework/Servers/IStreamHandler.cs b/OpenSim/Framework/Servers/IStreamHandler.cs index d674172..6dc7189 100644 --- a/OpenSim/Framework/Servers/IStreamHandler.cs +++ b/OpenSim/Framework/Servers/IStreamHandler.cs | |||
@@ -30,11 +30,8 @@ using System.IO; | |||
30 | 30 | ||
31 | namespace OpenSim.Framework.Servers | 31 | namespace OpenSim.Framework.Servers |
32 | { | 32 | { |
33 | public interface IStreamHandler | 33 | public interface IRequestHandler |
34 | { | 34 | { |
35 | // Handle request stream, return byte array | ||
36 | byte[] Handle(string path, Stream request); | ||
37 | |||
38 | // Return response content type | 35 | // Return response content type |
39 | string ContentType { get; } | 36 | string ContentType { get; } |
40 | 37 | ||
@@ -44,4 +41,17 @@ namespace OpenSim.Framework.Servers | |||
44 | // Return path | 41 | // Return path |
45 | string Path { get; } | 42 | string Path { get; } |
46 | } | 43 | } |
44 | |||
45 | public interface IStreamedRequestHandler : IRequestHandler | ||
46 | { | ||
47 | // Handle request stream, return byte array | ||
48 | byte[] Handle(string path, Stream request); | ||
49 | } | ||
50 | |||
51 | public interface IStreamHandler : IRequestHandler | ||
52 | { | ||
53 | // Handle request stream, return byte array | ||
54 | void Handle(string path, Stream request, Stream response); | ||
55 | } | ||
56 | |||
47 | } \ No newline at end of file | 57 | } \ No newline at end of file |
diff --git a/OpenSim/Framework/Servers/RestDeserialiseHandler.cs b/OpenSim/Framework/Servers/RestDeserialiseHandler.cs index cb500ca..28084e2 100644 --- a/OpenSim/Framework/Servers/RestDeserialiseHandler.cs +++ b/OpenSim/Framework/Servers/RestDeserialiseHandler.cs | |||
@@ -7,35 +7,35 @@ using System.Xml.Serialization; | |||
7 | 7 | ||
8 | namespace OpenSim.Framework.Servers | 8 | namespace OpenSim.Framework.Servers |
9 | { | 9 | { |
10 | public delegate string RestDeserialiseMethod<TRequest>(TRequest request); | 10 | public delegate TResponse RestDeserialiseMethod<TRequest, TResponse>(TRequest request); |
11 | 11 | ||
12 | public class RestDeserialisehandler<TRequest> : BaseStreamHandler | 12 | public class RestDeserialisehandler<TRequest, TResponse> : BaseRequestHandler, IStreamHandler |
13 | where TRequest : new() | 13 | where TRequest : new() |
14 | { | 14 | { |
15 | private RestDeserialiseMethod<TRequest> m_method; | 15 | private RestDeserialiseMethod<TRequest, TResponse> m_method; |
16 | 16 | ||
17 | public RestDeserialisehandler(string httpMethod, string path, RestDeserialiseMethod<TRequest> method) | 17 | public RestDeserialisehandler(string httpMethod, string path, RestDeserialiseMethod<TRequest, TResponse> method) |
18 | : base(httpMethod, path) | 18 | : base(httpMethod, path) |
19 | { | 19 | { |
20 | m_method = method; | 20 | m_method = method; |
21 | } | 21 | } |
22 | 22 | ||
23 | public override byte[] Handle(string path, Stream request) | 23 | public void Handle(string path, Stream request, Stream responseStream ) |
24 | { | 24 | { |
25 | Type type = typeof(TRequest); | 25 | TRequest deserial; |
26 | 26 | using (XmlTextReader xmlReader = new XmlTextReader(request)) | |
27 | TRequest deserial= default(TRequest); | ||
28 | using (XmlTextReader xreader = new XmlTextReader(request)) | ||
29 | { | 27 | { |
30 | XmlSerializer serializer = new XmlSerializer(type); | 28 | XmlSerializer deserializer = new XmlSerializer(typeof(TRequest)); |
31 | deserial = (TRequest)serializer.Deserialize(xreader); | 29 | deserial = (TRequest)deserializer.Deserialize(xmlReader); |
32 | } | 30 | } |
33 | 31 | ||
34 | string response = m_method(deserial); | 32 | TResponse response = m_method(deserial); |
35 | |||
36 | Encoding encoding = new UTF8Encoding(false); | ||
37 | return encoding.GetBytes(response); | ||
38 | 33 | ||
34 | using (XmlWriter xmlWriter = XmlTextWriter.Create( responseStream )) | ||
35 | { | ||
36 | XmlSerializer serializer = new XmlSerializer(typeof(TResponse)); | ||
37 | serializer.Serialize(xmlWriter, response ); | ||
38 | } | ||
39 | } | 39 | } |
40 | } | 40 | } |
41 | } | 41 | } |