diff options
Diffstat (limited to '')
-rw-r--r-- | Common/XmlRpcCS/SimpleHttpRequest.cs | 234 |
1 files changed, 0 insertions, 234 deletions
diff --git a/Common/XmlRpcCS/SimpleHttpRequest.cs b/Common/XmlRpcCS/SimpleHttpRequest.cs deleted file mode 100644 index b331ce0..0000000 --- a/Common/XmlRpcCS/SimpleHttpRequest.cs +++ /dev/null | |||
@@ -1,234 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.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 | namespace Nwc.XmlRpc | ||
29 | { | ||
30 | using System; | ||
31 | using System.IO; | ||
32 | using System.Net.Sockets; | ||
33 | using System.Collections; | ||
34 | using System.Text; | ||
35 | |||
36 | ///<summary>Very basic HTTP request handler.</summary> | ||
37 | ///<remarks>This class is designed to accept a TcpClient and treat it as an HTTP request. | ||
38 | /// It will do some basic header parsing and manage the input and output streams associated | ||
39 | /// with the request.</remarks> | ||
40 | public class SimpleHttpRequest | ||
41 | { | ||
42 | private String _httpMethod = null; | ||
43 | private String _protocol; | ||
44 | private String _filePathFile = null; | ||
45 | private String _filePathDir = null; | ||
46 | private String __filePath; | ||
47 | private TcpClient _client; | ||
48 | private StreamReader _input; | ||
49 | private StreamWriter _output; | ||
50 | private Hashtable _headers; | ||
51 | |||
52 | /// <summary>A constructor which accepts the TcpClient.</summary> | ||
53 | /// <remarks>It creates the associated input and output streams, determines the request type, | ||
54 | /// and parses the remaining HTTP header.</remarks> | ||
55 | /// <param name="client">The <c>TcpClient</c> associated with the HTTP connection.</param> | ||
56 | public SimpleHttpRequest(TcpClient client) | ||
57 | { | ||
58 | _client = client; | ||
59 | |||
60 | _output = new StreamWriter(client.GetStream() ); | ||
61 | _input = new StreamReader(client.GetStream() ); | ||
62 | |||
63 | GetRequestMethod(); | ||
64 | GetRequestHeaders(); | ||
65 | } | ||
66 | |||
67 | /// <summary>The output <c>StreamWriter</c> associated with the request.</summary> | ||
68 | public StreamWriter Output | ||
69 | { | ||
70 | get { return _output; } | ||
71 | } | ||
72 | |||
73 | /// <summary>The input <c>StreamReader</c> associated with the request.</summary> | ||
74 | public StreamReader Input | ||
75 | { | ||
76 | get { return _input; } | ||
77 | } | ||
78 | |||
79 | /// <summary>The <c>TcpClient</c> with the request.</summary> | ||
80 | public TcpClient Client | ||
81 | { | ||
82 | get { return _client; } | ||
83 | } | ||
84 | |||
85 | private String _filePath | ||
86 | { | ||
87 | get { return __filePath; } | ||
88 | set | ||
89 | { | ||
90 | __filePath = value; | ||
91 | _filePathDir = null; | ||
92 | _filePathFile = null; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | /// <summary>The type of HTTP request (i.e. PUT, GET, etc.).</summary> | ||
97 | public String HttpMethod | ||
98 | { | ||
99 | get { return _httpMethod; } | ||
100 | } | ||
101 | |||
102 | /// <summary>The level of the HTTP protocol.</summary> | ||
103 | public String Protocol | ||
104 | { | ||
105 | get { return _protocol; } | ||
106 | } | ||
107 | |||
108 | /// <summary>The "path" which is part of any HTTP request.</summary> | ||
109 | public String FilePath | ||
110 | { | ||
111 | get { return _filePath; } | ||
112 | } | ||
113 | |||
114 | /// <summary>The file portion of the "path" which is part of any HTTP request.</summary> | ||
115 | public String FilePathFile | ||
116 | { | ||
117 | get | ||
118 | { | ||
119 | if (_filePathFile != null) | ||
120 | return _filePathFile; | ||
121 | |||
122 | int i = FilePath.LastIndexOf("/"); | ||
123 | |||
124 | if (i == -1) | ||
125 | return ""; | ||
126 | |||
127 | i++; | ||
128 | _filePathFile = FilePath.Substring(i, FilePath.Length - i); | ||
129 | return _filePathFile; | ||
130 | } | ||
131 | } | ||
132 | |||
133 | /// <summary>The directory portion of the "path" which is part of any HTTP request.</summary> | ||
134 | public String FilePathDir | ||
135 | { | ||
136 | get | ||
137 | { | ||
138 | if (_filePathDir != null) | ||
139 | return _filePathDir; | ||
140 | |||
141 | int i = FilePath.LastIndexOf("/"); | ||
142 | |||
143 | if (i == -1) | ||
144 | return ""; | ||
145 | |||
146 | i++; | ||
147 | _filePathDir = FilePath.Substring(0, i); | ||
148 | return _filePathDir; | ||
149 | } | ||
150 | } | ||
151 | |||
152 | private void GetRequestMethod() | ||
153 | { | ||
154 | string req = _input.ReadLine(); | ||
155 | if (req == null) | ||
156 | throw new ApplicationException("Void request."); | ||
157 | |||
158 | if (0 == String.Compare("GET ", req.Substring(0, 4), true)) | ||
159 | _httpMethod = "GET"; | ||
160 | else if (0 == String.Compare("POST ", req.Substring(0, 5), true)) | ||
161 | _httpMethod = "POST"; | ||
162 | else | ||
163 | throw new InvalidOperationException("Unrecognized method in query: " + req); | ||
164 | |||
165 | req = req.TrimEnd(); | ||
166 | int idx = req.IndexOf(' ') + 1; | ||
167 | if (idx >= req.Length) | ||
168 | throw new ApplicationException("What do you want?"); | ||
169 | |||
170 | string page_protocol = req.Substring(idx); | ||
171 | int idx2 = page_protocol.IndexOf(' '); | ||
172 | if (idx2 == -1) | ||
173 | idx2 = page_protocol.Length; | ||
174 | |||
175 | _filePath = page_protocol.Substring(0, idx2).Trim(); | ||
176 | _protocol = page_protocol.Substring(idx2).Trim(); | ||
177 | } | ||
178 | |||
179 | private void GetRequestHeaders() | ||
180 | { | ||
181 | String line; | ||
182 | int idx; | ||
183 | |||
184 | _headers = new Hashtable(); | ||
185 | |||
186 | while ((line = _input.ReadLine()) != "") | ||
187 | { | ||
188 | if (line == null) | ||
189 | { | ||
190 | break; | ||
191 | } | ||
192 | |||
193 | idx = line.IndexOf(':'); | ||
194 | if (idx == -1 || idx == line.Length - 1) | ||
195 | { | ||
196 | Logger.WriteEntry("Malformed header line: " + line, LogLevel.Information); | ||
197 | continue; | ||
198 | } | ||
199 | |||
200 | String key = line.Substring(0, idx); | ||
201 | String value = line.Substring(idx + 1); | ||
202 | |||
203 | try | ||
204 | { | ||
205 | _headers.Add(key, value); | ||
206 | } | ||
207 | catch (Exception) | ||
208 | { | ||
209 | Logger.WriteEntry("Duplicate header key in line: " + line, LogLevel.Information); | ||
210 | } | ||
211 | } | ||
212 | } | ||
213 | |||
214 | /// <summary> | ||
215 | /// Format the object contents into a useful string representation. | ||
216 | /// </summary> | ||
217 | ///<returns><c>String</c> representation of the <c>SimpleHttpRequest</c> as the <i>HttpMethod FilePath Protocol</i>.</returns> | ||
218 | override public String ToString() | ||
219 | { | ||
220 | return HttpMethod + " " + FilePath + " " + Protocol; | ||
221 | } | ||
222 | |||
223 | /// <summary> | ||
224 | /// Close the <c>SimpleHttpRequest</c>. This flushes and closes all associated io streams. | ||
225 | /// </summary> | ||
226 | public void Close() | ||
227 | { | ||
228 | _output.Flush(); | ||
229 | _output.Close(); | ||
230 | _input.Close(); | ||
231 | _client.Close(); | ||
232 | } | ||
233 | } | ||
234 | } | ||