HttpContextInfo.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // HttpContextInfo.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2010 Novell, Inc. http://www.novell.com
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.Globalization;
  32. using System.IO;
  33. using System.Net;
  34. using System.Security.Principal;
  35. using System.ServiceModel;
  36. using System.Text;
  37. using System.Threading;
  38. using System.Web;
  39. namespace System.ServiceModel.Channels.Http
  40. {
  41. // Context
  42. abstract class HttpContextInfo
  43. {
  44. public abstract HttpRequestInfo Request { get; }
  45. public abstract HttpResponseInfo Response { get; }
  46. public abstract string User { get; }
  47. public abstract string Password { get; }
  48. public abstract void ReturnUnauthorized ();
  49. public void Abort ()
  50. {
  51. Response.Abort ();
  52. OnContextClosed ();
  53. }
  54. public void Close ()
  55. {
  56. Response.Close ();
  57. OnContextClosed ();
  58. }
  59. protected virtual void OnContextClosed ()
  60. {
  61. }
  62. }
  63. class HttpStandaloneContextInfo : HttpContextInfo
  64. {
  65. public HttpStandaloneContextInfo (HttpListenerContext ctx)
  66. {
  67. this.ctx = ctx;
  68. request = new HttpStandaloneRequestInfo (ctx.Request);
  69. response = new HttpStandaloneResponseInfo (ctx.Response);
  70. }
  71. HttpListenerContext ctx;
  72. HttpStandaloneRequestInfo request;
  73. HttpStandaloneResponseInfo response;
  74. public HttpListenerContext Source {
  75. get { return ctx; }
  76. }
  77. public override HttpRequestInfo Request {
  78. get { return request; }
  79. }
  80. public override HttpResponseInfo Response {
  81. get { return response; }
  82. }
  83. public override string User {
  84. get { return ctx.User != null ? ((HttpListenerBasicIdentity) ctx.User.Identity).Name : null; }
  85. }
  86. public override string Password {
  87. get { return ctx.User != null ? ((HttpListenerBasicIdentity) ctx.User.Identity).Password : null; }
  88. }
  89. public override void ReturnUnauthorized ()
  90. {
  91. ctx.Response.StatusCode = 401;
  92. }
  93. }
  94. class AspNetHttpContextInfo : HttpContextInfo
  95. {
  96. public AspNetHttpContextInfo (SvcHttpHandler handler, HttpContext ctx)
  97. {
  98. this.ctx = ctx;
  99. this.handler = handler;
  100. this.request = new AspNetHttpRequestInfo (ctx.Request);
  101. this.response = new AspNetHttpResponseInfo (ctx.Response);
  102. }
  103. HttpContext ctx;
  104. SvcHttpHandler handler;
  105. AspNetHttpRequestInfo request;
  106. AspNetHttpResponseInfo response;
  107. public HttpContext Source {
  108. get { return ctx; }
  109. }
  110. public override HttpRequestInfo Request {
  111. get { return request; }
  112. }
  113. public override HttpResponseInfo Response {
  114. get { return response; }
  115. }
  116. public override string User {
  117. get { return ctx.User != null ? ((GenericIdentity) ctx.User.Identity).Name : null; }
  118. }
  119. // FIXME: how to acquire this?
  120. public override string Password {
  121. get { return null; }
  122. }
  123. public override void ReturnUnauthorized ()
  124. {
  125. ctx.Response.StatusCode = 401;
  126. }
  127. protected override void OnContextClosed ()
  128. {
  129. handler.EndHttpRequest (ctx);
  130. }
  131. }
  132. // Request
  133. abstract class HttpRequestInfo
  134. {
  135. public abstract long ContentLength64 { get; }
  136. public abstract NameValueCollection QueryString { get; }
  137. public abstract NameValueCollection Headers { get; }
  138. public abstract Uri Url { get; }
  139. public abstract string ContentType { get; }
  140. public abstract string HttpMethod { get; }
  141. public abstract Stream InputStream { get; }
  142. public abstract string ClientIPAddress { get; }
  143. public abstract int ClientPort { get; }
  144. }
  145. class HttpStandaloneRequestInfo : HttpRequestInfo
  146. {
  147. public HttpStandaloneRequestInfo (HttpListenerRequest request)
  148. {
  149. this.req = request;
  150. }
  151. HttpListenerRequest req;
  152. public override long ContentLength64 {
  153. get { return req.ContentLength64; }
  154. }
  155. public override NameValueCollection QueryString {
  156. get { return req.QueryString; }
  157. }
  158. public override NameValueCollection Headers {
  159. get { return req.Headers; }
  160. }
  161. public override Uri Url {
  162. get { return req.Url; }
  163. }
  164. public override string ContentType {
  165. get { return req.ContentType; }
  166. }
  167. public override string HttpMethod {
  168. get { return req.HttpMethod; }
  169. }
  170. public override Stream InputStream {
  171. get { return req.InputStream; }
  172. }
  173. public override string ClientIPAddress {
  174. get { return req.RemoteEndPoint.Address.ToString (); }
  175. }
  176. public override int ClientPort {
  177. get { return req.RemoteEndPoint.Port; }
  178. }
  179. }
  180. class AspNetHttpRequestInfo : HttpRequestInfo
  181. {
  182. public AspNetHttpRequestInfo (HttpRequest request)
  183. {
  184. this.req = request;
  185. }
  186. HttpRequest req;
  187. public override long ContentLength64 {
  188. get { return req.ContentLength; }
  189. }
  190. public override NameValueCollection QueryString {
  191. get { return req.QueryString; }
  192. }
  193. public override NameValueCollection Headers {
  194. get { return req.Headers; }
  195. }
  196. public override Uri Url {
  197. get { return req.Url; }
  198. }
  199. public override string ContentType {
  200. get { return req.ContentType; }
  201. }
  202. public override string HttpMethod {
  203. get { return req.HttpMethod; }
  204. }
  205. public override Stream InputStream {
  206. get { return req.InputStream; }
  207. }
  208. public override string ClientIPAddress {
  209. get { return req.UserHostAddress; }
  210. }
  211. public override int ClientPort {
  212. get { return -1; } // cannot retrieve
  213. }
  214. }
  215. // Response
  216. abstract class HttpResponseInfo
  217. {
  218. public abstract string ContentType { get; set; }
  219. public abstract NameValueCollection Headers { get; }
  220. public abstract Stream OutputStream { get; }
  221. public abstract int StatusCode { get; set; }
  222. public abstract string StatusDescription { get; set; }
  223. public abstract void Abort ();
  224. public abstract void Close ();
  225. public abstract void SetLength (long value);
  226. public virtual bool SuppressContent { get; set; }
  227. }
  228. class HttpStandaloneResponseInfo : HttpResponseInfo
  229. {
  230. public HttpStandaloneResponseInfo (HttpListenerResponse response)
  231. {
  232. this.res = response;
  233. }
  234. HttpListenerResponse res;
  235. public override string ContentType {
  236. get { return res.ContentType; }
  237. set { res.ContentType = value; }
  238. }
  239. public override NameValueCollection Headers {
  240. get { return res.Headers; }
  241. }
  242. public override int StatusCode {
  243. get { return res.StatusCode; }
  244. set { res.StatusCode = value; }
  245. }
  246. public override string StatusDescription {
  247. get { return res.StatusDescription; }
  248. set { res.StatusDescription = value; }
  249. }
  250. public override Stream OutputStream {
  251. get { return res.OutputStream; }
  252. }
  253. public override void Abort ()
  254. {
  255. res.Abort ();
  256. }
  257. public override void Close ()
  258. {
  259. res.Close ();
  260. }
  261. public override void SetLength (long value)
  262. {
  263. res.ContentLength64 = value;
  264. }
  265. }
  266. class AspNetHttpResponseInfo : HttpResponseInfo
  267. {
  268. public AspNetHttpResponseInfo (HttpResponse response)
  269. {
  270. this.res = response;
  271. }
  272. HttpResponse res;
  273. public override bool SuppressContent {
  274. get { return res.SuppressContent; }
  275. set { res.SuppressContent = value; }
  276. }
  277. public override string ContentType {
  278. get { return res.ContentType; }
  279. set { res.ContentType = value; }
  280. }
  281. public override NameValueCollection Headers {
  282. get { return res.Headers; }
  283. }
  284. public override int StatusCode {
  285. get { return res.StatusCode; }
  286. set { res.StatusCode = value; }
  287. }
  288. public override string StatusDescription {
  289. get { return res.StatusDescription; }
  290. set { res.StatusDescription = value; }
  291. }
  292. public override Stream OutputStream {
  293. get { return res.OutputStream; }
  294. }
  295. public override void Abort ()
  296. {
  297. res.End ();
  298. }
  299. public override void Close ()
  300. {
  301. // We must not close the response here, as everything is taking place in the
  302. // HttpApplication's pipeline context and the output is sent to the client
  303. // _after_ we leave this method. Closing the response here will stop any
  304. // output from reaching the client.
  305. }
  306. public override void SetLength (long value)
  307. {
  308. res.AddHeader ("Content-Length", value.ToString (CultureInfo.InvariantCulture));
  309. }
  310. }
  311. }