| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- //
- // HttpContextInfo.cs
- //
- // Author:
- // Atsushi Enomoto <[email protected]>
- //
- // Copyright (C) 2010 Novell, Inc. http://www.novell.com
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections.Generic;
- using System.Collections.Specialized;
- using System.Globalization;
- using System.IO;
- using System.Net;
- using System.Security.Principal;
- using System.ServiceModel;
- using System.Text;
- using System.Threading;
- using System.Web;
- namespace System.ServiceModel.Channels.Http
- {
- // Context
-
- abstract class HttpContextInfo
- {
- public abstract HttpRequestInfo Request { get; }
- public abstract HttpResponseInfo Response { get; }
- public abstract string User { get; }
- public abstract string Password { get; }
- public abstract void ReturnUnauthorized ();
- public void Abort ()
- {
- Response.Abort ();
- OnContextClosed ();
- }
- public void Close ()
- {
- Response.Close ();
- OnContextClosed ();
- }
-
- protected virtual void OnContextClosed ()
- {
- }
- }
- class HttpStandaloneContextInfo : HttpContextInfo
- {
- public HttpStandaloneContextInfo (HttpListenerContext ctx)
- {
- this.ctx = ctx;
- request = new HttpStandaloneRequestInfo (ctx.Request);
- response = new HttpStandaloneResponseInfo (ctx.Response);
- }
-
- HttpListenerContext ctx;
- HttpStandaloneRequestInfo request;
- HttpStandaloneResponseInfo response;
- public HttpListenerContext Source {
- get { return ctx; }
- }
- public override HttpRequestInfo Request {
- get { return request; }
- }
- public override HttpResponseInfo Response {
- get { return response; }
- }
- public override string User {
- get { return ctx.User != null ? ((HttpListenerBasicIdentity) ctx.User.Identity).Name : null; }
- }
- public override string Password {
- get { return ctx.User != null ? ((HttpListenerBasicIdentity) ctx.User.Identity).Password : null; }
- }
- public override void ReturnUnauthorized ()
- {
- ctx.Response.StatusCode = 401;
- }
- }
- class AspNetHttpContextInfo : HttpContextInfo
- {
- public AspNetHttpContextInfo (SvcHttpHandler handler, HttpContext ctx)
- {
- this.ctx = ctx;
- this.handler = handler;
- this.request = new AspNetHttpRequestInfo (ctx.Request);
- this.response = new AspNetHttpResponseInfo (ctx.Response);
- }
-
- HttpContext ctx;
- SvcHttpHandler handler;
- AspNetHttpRequestInfo request;
- AspNetHttpResponseInfo response;
- public HttpContext Source {
- get { return ctx; }
- }
-
- public override HttpRequestInfo Request {
- get { return request; }
- }
- public override HttpResponseInfo Response {
- get { return response; }
- }
- public override string User {
- get { return ctx.User != null ? ((GenericIdentity) ctx.User.Identity).Name : null; }
- }
- // FIXME: how to acquire this?
- public override string Password {
- get { return null; }
- }
- public override void ReturnUnauthorized ()
- {
- ctx.Response.StatusCode = 401;
- }
- protected override void OnContextClosed ()
- {
- handler.EndHttpRequest (ctx);
- }
- }
- // Request
- abstract class HttpRequestInfo
- {
- public abstract long ContentLength64 { get; }
- public abstract NameValueCollection QueryString { get; }
- public abstract NameValueCollection Headers { get; }
- public abstract Uri Url { get; }
- public abstract string ContentType { get; }
- public abstract string HttpMethod { get; }
- public abstract Stream InputStream { get; }
- public abstract string ClientIPAddress { get; }
- public abstract int ClientPort { get; }
- }
- class HttpStandaloneRequestInfo : HttpRequestInfo
- {
- public HttpStandaloneRequestInfo (HttpListenerRequest request)
- {
- this.req = request;
- }
-
- HttpListenerRequest req;
- public override long ContentLength64 {
- get { return req.ContentLength64; }
- }
- public override NameValueCollection QueryString {
- get { return req.QueryString; }
- }
- public override NameValueCollection Headers {
- get { return req.Headers; }
- }
- public override Uri Url {
- get { return req.Url; }
- }
- public override string ContentType {
- get { return req.ContentType; }
- }
- public override string HttpMethod {
- get { return req.HttpMethod; }
- }
- public override Stream InputStream {
- get { return req.InputStream; }
- }
- public override string ClientIPAddress {
- get { return req.RemoteEndPoint.Address.ToString (); }
- }
- public override int ClientPort {
- get { return req.RemoteEndPoint.Port; }
- }
- }
- class AspNetHttpRequestInfo : HttpRequestInfo
- {
- public AspNetHttpRequestInfo (HttpRequest request)
- {
- this.req = request;
- }
-
- HttpRequest req;
- public override long ContentLength64 {
- get { return req.ContentLength; }
- }
- public override NameValueCollection QueryString {
- get { return req.QueryString; }
- }
- public override NameValueCollection Headers {
- get { return req.Headers; }
- }
- public override Uri Url {
- get { return req.Url; }
- }
- public override string ContentType {
- get { return req.ContentType; }
- }
- public override string HttpMethod {
- get { return req.HttpMethod; }
- }
- public override Stream InputStream {
- get { return req.InputStream; }
- }
- public override string ClientIPAddress {
- get { return req.UserHostAddress; }
- }
- public override int ClientPort {
- get { return -1; } // cannot retrieve
- }
- }
-
- // Response
-
- abstract class HttpResponseInfo
- {
- public abstract string ContentType { get; set; }
- public abstract NameValueCollection Headers { get; }
- public abstract Stream OutputStream { get; }
- public abstract int StatusCode { get; set; }
- public abstract string StatusDescription { get; set; }
- public abstract void Abort ();
- public abstract void Close ();
- public abstract void SetLength (long value);
-
- public virtual bool SuppressContent { get; set; }
- }
- class HttpStandaloneResponseInfo : HttpResponseInfo
- {
- public HttpStandaloneResponseInfo (HttpListenerResponse response)
- {
- this.res = response;
- }
-
- HttpListenerResponse res;
- public override string ContentType {
- get { return res.ContentType; }
- set { res.ContentType = value; }
- }
- public override NameValueCollection Headers {
- get { return res.Headers; }
- }
- public override int StatusCode {
- get { return res.StatusCode; }
- set { res.StatusCode = value; }
- }
- public override string StatusDescription {
- get { return res.StatusDescription; }
- set { res.StatusDescription = value; }
- }
- public override Stream OutputStream {
- get { return res.OutputStream; }
- }
-
- public override void Abort ()
- {
- res.Abort ();
- }
-
- public override void Close ()
- {
- res.Close ();
- }
-
- public override void SetLength (long value)
- {
- res.ContentLength64 = value;
- }
- }
- class AspNetHttpResponseInfo : HttpResponseInfo
- {
- public AspNetHttpResponseInfo (HttpResponse response)
- {
- this.res = response;
- }
-
- HttpResponse res;
-
- public override bool SuppressContent {
- get { return res.SuppressContent; }
- set { res.SuppressContent = value; }
- }
- public override string ContentType {
- get { return res.ContentType; }
- set { res.ContentType = value; }
- }
- public override NameValueCollection Headers {
- get { return res.Headers; }
- }
- public override int StatusCode {
- get { return res.StatusCode; }
- set { res.StatusCode = value; }
- }
-
- public override string StatusDescription {
- get { return res.StatusDescription; }
- set { res.StatusDescription = value; }
- }
- public override Stream OutputStream {
- get { return res.OutputStream; }
- }
-
- public override void Abort ()
- {
- res.End ();
- }
-
- public override void Close ()
- {
- // We must not close the response here, as everything is taking place in the
- // HttpApplication's pipeline context and the output is sent to the client
- // _after_ we leave this method. Closing the response here will stop any
- // output from reaching the client.
- }
-
- public override void SetLength (long value)
- {
- res.AddHeader ("Content-Length", value.ToString (CultureInfo.InvariantCulture));
- }
- }
- }
|