5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2010 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
30 using System.Collections.Specialized;
31 using System.Globalization;
34 using System.Security.Principal;
35 using System.ServiceModel;
37 using System.Threading;
40 namespace System.ServiceModel.Channels.Http
44 abstract class HttpContextInfo
46 public abstract HttpRequestInfo Request { get; }
47 public abstract HttpResponseInfo Response { get; }
49 public abstract string User { get; }
50 public abstract string Password { get; }
51 public abstract void ReturnUnauthorized ();
65 protected virtual void OnContextClosed ()
70 class HttpStandaloneContextInfo : HttpContextInfo
72 public HttpStandaloneContextInfo (HttpListenerContext ctx)
75 request = new HttpStandaloneRequestInfo (ctx.Request);
76 response = new HttpStandaloneResponseInfo (ctx.Response);
79 HttpListenerContext ctx;
80 HttpStandaloneRequestInfo request;
81 HttpStandaloneResponseInfo response;
83 public HttpListenerContext Source {
87 public override HttpRequestInfo Request {
88 get { return request; }
91 public override HttpResponseInfo Response {
92 get { return response; }
95 public override string User {
96 get { return ctx.User != null ? ((HttpListenerBasicIdentity) ctx.User.Identity).Name : null; }
99 public override string Password {
100 get { return ctx.User != null ? ((HttpListenerBasicIdentity) ctx.User.Identity).Password : null; }
103 public override void ReturnUnauthorized ()
105 ctx.Response.StatusCode = 401;
109 class AspNetHttpContextInfo : HttpContextInfo
111 public AspNetHttpContextInfo (SvcHttpHandler handler, HttpContext ctx)
114 this.handler = handler;
115 this.request = new AspNetHttpRequestInfo (ctx.Request);
116 this.response = new AspNetHttpResponseInfo (ctx.Response);
120 SvcHttpHandler handler;
121 AspNetHttpRequestInfo request;
122 AspNetHttpResponseInfo response;
124 public HttpContext Source {
128 public override HttpRequestInfo Request {
129 get { return request; }
132 public override HttpResponseInfo Response {
133 get { return response; }
136 public override string User {
137 get { return ctx.User != null ? ((GenericIdentity) ctx.User.Identity).Name : null; }
140 // FIXME: how to acquire this?
141 public override string Password {
145 public override void ReturnUnauthorized ()
147 ctx.Response.StatusCode = 401;
150 protected override void OnContextClosed ()
152 handler.EndHttpRequest (ctx);
158 abstract class HttpRequestInfo
160 public abstract NameValueCollection QueryString { get; }
161 public abstract NameValueCollection Headers { get; }
162 public abstract Uri Url { get; }
163 public abstract string ContentType { get; }
164 public abstract string HttpMethod { get; }
165 public abstract Stream InputStream { get; }
168 class HttpStandaloneRequestInfo : HttpRequestInfo
170 public HttpStandaloneRequestInfo (HttpListenerRequest request)
175 HttpListenerRequest req;
177 public override NameValueCollection QueryString {
178 get { return req.QueryString; }
180 public override NameValueCollection Headers {
181 get { return req.Headers; }
183 public override Uri Url {
184 get { return req.Url; }
186 public override string ContentType {
187 get { return req.ContentType; }
189 public override string HttpMethod {
190 get { return req.HttpMethod; }
192 public override Stream InputStream {
193 get { return req.InputStream; }
197 class AspNetHttpRequestInfo : HttpRequestInfo
199 public AspNetHttpRequestInfo (HttpRequest request)
206 public override NameValueCollection QueryString {
207 get { return req.QueryString; }
209 public override NameValueCollection Headers {
210 get { return req.Headers; }
212 public override Uri Url {
213 get { return req.Url; }
215 public override string ContentType {
216 get { return req.ContentType; }
218 public override string HttpMethod {
219 get { return req.HttpMethod; }
221 public override Stream InputStream {
222 get { return req.InputStream; }
228 abstract class HttpResponseInfo
230 public abstract string ContentType { get; set; }
231 public abstract NameValueCollection Headers { get; }
232 public abstract Stream OutputStream { get; }
233 public abstract int StatusCode { get; set; }
234 public abstract string StatusDescription { get; set; }
235 public abstract void Abort ();
236 public abstract void Close ();
237 public abstract void SetLength (long value);
239 public virtual bool SuppressContent { get; set; }
242 class HttpStandaloneResponseInfo : HttpResponseInfo
244 public HttpStandaloneResponseInfo (HttpListenerResponse response)
249 HttpListenerResponse res;
251 public override string ContentType {
252 get { return res.ContentType; }
253 set { res.ContentType = value; }
255 public override NameValueCollection Headers {
256 get { return res.Headers; }
258 public override int StatusCode {
259 get { return res.StatusCode; }
260 set { res.StatusCode = value; }
262 public override string StatusDescription {
263 get { return res.StatusDescription; }
264 set { res.StatusDescription = value; }
266 public override Stream OutputStream {
267 get { return res.OutputStream; }
270 public override void Abort ()
275 public override void Close ()
280 public override void SetLength (long value)
282 res.ContentLength64 = value;
286 class AspNetHttpResponseInfo : HttpResponseInfo
288 public AspNetHttpResponseInfo (HttpResponse response)
295 public override bool SuppressContent {
296 get { return res.SuppressContent; }
297 set { res.SuppressContent = value; }
299 public override string ContentType {
300 get { return res.ContentType; }
301 set { res.ContentType = value; }
303 public override NameValueCollection Headers {
304 get { return res.Headers; }
306 public override int StatusCode {
307 get { return res.StatusCode; }
308 set { res.StatusCode = value; }
311 public override string StatusDescription {
312 get { return res.StatusDescription; }
313 set { res.StatusDescription = value; }
315 public override Stream OutputStream {
316 get { return res.OutputStream; }
319 public override void Abort ()
325 public override void Close ()
331 public override void SetLength (long value)
333 res.AddHeader ("Content-Length", value.ToString (CultureInfo.InvariantCulture));