diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/Servers/HttpServer/OSHttpHandler.cs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpHandler.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpHandler.cs new file mode 100644 index 0000000..f1788a0 --- /dev/null +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpHandler.cs | |||
@@ -0,0 +1,183 @@ | |||
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 OpenSim 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.Generic; | ||
30 | using System.IO; | ||
31 | using System.Text.RegularExpressions; | ||
32 | |||
33 | namespace OpenSim.Framework.Servers.HttpServer | ||
34 | { | ||
35 | /// <sumary> | ||
36 | /// Any OSHttpHandler must return one of the following results: | ||
37 | /// <list type = "table"> | ||
38 | /// <listheader> | ||
39 | /// <term>result code</term> | ||
40 | /// <description>meaning</description> | ||
41 | /// </listheader> | ||
42 | /// <item> | ||
43 | /// <term>Pass</term> | ||
44 | /// <description>handler did not process the request</request> | ||
45 | /// </item> | ||
46 | /// <item> | ||
47 | /// <term>Done</term> | ||
48 | /// <description>handler did process the request, OSHttpServer | ||
49 | /// can clean up and close the request</request> | ||
50 | /// </item> | ||
51 | /// </list> | ||
52 | /// </summary> | ||
53 | public enum OSHttpHandlerResult | ||
54 | { | ||
55 | Unprocessed, | ||
56 | Pass, | ||
57 | Done, | ||
58 | } | ||
59 | |||
60 | /// <summary> | ||
61 | /// An OSHttpHandler that matches on the "content-type" header can | ||
62 | /// supply an OSHttpContentTypeChecker delegate which will be | ||
63 | /// invoked by the request matcher in OSHttpRequestPump. | ||
64 | /// </summary> | ||
65 | /// <returns>true if the handler is interested in the content; | ||
66 | /// false otherwise</returns> | ||
67 | public delegate bool OSHttpContentTypeChecker(OSHttpRequest req); | ||
68 | |||
69 | public abstract class OSHttpHandler | ||
70 | { | ||
71 | /// <summary> | ||
72 | /// Regular expression used to match against method of | ||
73 | /// the incoming HTTP request. If you want to match any string | ||
74 | /// either use '.*' or null. To match on the empty string use | ||
75 | /// '^$'. | ||
76 | /// </summary> | ||
77 | public virtual Regex Method | ||
78 | { | ||
79 | get { return _method; } | ||
80 | } | ||
81 | protected Regex _method; | ||
82 | |||
83 | /// <summary> | ||
84 | /// Regular expression used to match against path of the | ||
85 | /// incoming HTTP request. If you want to match any string | ||
86 | /// either use '.*' or null. To match on the emtpy string use | ||
87 | /// '^$'. | ||
88 | /// </summary> | ||
89 | public virtual Regex Path | ||
90 | { | ||
91 | get { return _path; } | ||
92 | } | ||
93 | protected Regex _path; | ||
94 | |||
95 | /// <summary> | ||
96 | /// Dictionary of (query name, regular expression) tuples, | ||
97 | /// allowing us to match on URI query fields. | ||
98 | /// </summary> | ||
99 | public virtual Dictionary<string, Regex> Query | ||
100 | { | ||
101 | get { return _query; } | ||
102 | } | ||
103 | protected Dictionary<string, Regex> _query; | ||
104 | |||
105 | /// <summary> | ||
106 | /// Dictionary of (header name, regular expression) tuples, | ||
107 | /// allowing us to match on HTTP header fields. | ||
108 | /// </summary> | ||
109 | public virtual Dictionary<string, Regex> Headers | ||
110 | { | ||
111 | get { return _headers; } | ||
112 | } | ||
113 | protected Dictionary<string, Regex> _headers; | ||
114 | |||
115 | /// <summary> | ||
116 | /// Dictionary of (header name, regular expression) tuples, | ||
117 | /// allowing us to match on HTTP header fields. | ||
118 | /// </summary> | ||
119 | /// <remarks> | ||
120 | /// This feature is currently not implemented as it requires | ||
121 | /// (trivial) changes to HttpServer.HttpListener that have not | ||
122 | /// been implemented. | ||
123 | /// </remarks> | ||
124 | public virtual Regex IPEndPointWhitelist | ||
125 | { | ||
126 | get { return _ipEndPointRegex; } | ||
127 | } | ||
128 | protected Regex _ipEndPointRegex; | ||
129 | |||
130 | |||
131 | /// <summary> | ||
132 | /// Base class constructor. | ||
133 | /// </summary> | ||
134 | /// <param name="path">null or path regex</param> | ||
135 | /// <param name="headers">null or dictionary of header | ||
136 | /// regexs</param> | ||
137 | /// <param name="contentType">null or content type | ||
138 | /// regex</param> | ||
139 | /// <param name="whitelist">null or IP address regex</param> | ||
140 | public OSHttpHandler(Regex method, Regex path, Dictionary<string, Regex> query, | ||
141 | Dictionary<string, Regex> headers, Regex contentType, Regex whitelist) | ||
142 | { | ||
143 | _method = method; | ||
144 | _path = path; | ||
145 | _query = query; | ||
146 | _ipEndPointRegex = whitelist; | ||
147 | |||
148 | if (null == _headers && null != contentType) | ||
149 | { | ||
150 | _headers = new Dictionary<string, Regex>(); | ||
151 | _headers.Add("content-type", contentType); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | |||
156 | /// <summary> | ||
157 | /// Process an incoming OSHttpRequest that matched our | ||
158 | /// requirements. | ||
159 | /// </summary> | ||
160 | /// <returns> | ||
161 | /// OSHttpHandlerResult.Pass if we are after all not | ||
162 | /// interested in the request; OSHttpHandlerResult.Done if we | ||
163 | /// did process the request. | ||
164 | /// </returns> | ||
165 | public abstract OSHttpHandlerResult Process(OSHttpRequest request); | ||
166 | |||
167 | public override string ToString() | ||
168 | { | ||
169 | StringWriter sw = new StringWriter(); | ||
170 | sw.WriteLine("{0}", base.ToString()); | ||
171 | sw.WriteLine(" method regex {0}", null == Method ? "null" : Method.ToString()); | ||
172 | sw.WriteLine(" path regex {0}", null == Path ? "null": Path.ToString()); | ||
173 | foreach (string tag in Headers.Keys) | ||
174 | { | ||
175 | sw.WriteLine(" header {0} : {1}", tag, Headers[tag].ToString()); | ||
176 | } | ||
177 | sw.WriteLine(" IP whitelist {0}", null == IPEndPointWhitelist ? "null" : IPEndPointWhitelist.ToString()); | ||
178 | sw.WriteLine(); | ||
179 | sw.Close(); | ||
180 | return sw.ToString(); | ||
181 | } | ||
182 | } | ||
183 | } | ||