HttpContextInfo.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 NameValueCollection QueryString { get; }
  136. public abstract NameValueCollection Headers { get; }
  137. public abstract Uri Url { get; }
  138. public abstract string ContentType { get; }
  139. public abstract string HttpMethod { get; }
  140. public abstract Stream InputStream { get; }
  141. }
  142. class HttpStandaloneRequestInfo : HttpRequestInfo
  143. {
  144. public HttpStandaloneRequestInfo (HttpListenerRequest request)
  145. {
  146. this.req = request;
  147. }
  148. HttpListenerRequest req;
  149. public override NameValueCollection QueryString {
  150. get { return req.QueryString; }
  151. }
  152. public override NameValueCollection Headers {
  153. get { return req.Headers; }
  154. }
  155. public override Uri Url {
  156. get { return req.Url; }
  157. }
  158. public override string ContentType {
  159. get { return req.ContentType; }
  160. }
  161. public override string HttpMethod {
  162. get { return req.HttpMethod; }
  163. }
  164. public override Stream InputStream {
  165. get { return req.InputStream; }
  166. }
  167. }
  168. class AspNetHttpRequestInfo : HttpRequestInfo
  169. {
  170. public AspNetHttpRequestInfo (HttpRequest request)
  171. {
  172. this.req = request;
  173. }
  174. HttpRequest req;
  175. public override NameValueCollection QueryString {
  176. get { return req.QueryString; }
  177. }
  178. public override NameValueCollection Headers {
  179. get { return req.Headers; }
  180. }
  181. public override Uri Url {
  182. get { return req.Url; }
  183. }
  184. public override string ContentType {
  185. get { return req.ContentType; }
  186. }
  187. public override string HttpMethod {
  188. get { return req.HttpMethod; }
  189. }
  190. public override Stream InputStream {
  191. get { return req.InputStream; }
  192. }
  193. }
  194. // Response
  195. abstract class HttpResponseInfo
  196. {
  197. public abstract string ContentType { get; set; }
  198. public abstract NameValueCollection Headers { get; }
  199. public abstract Stream OutputStream { get; }
  200. public abstract int StatusCode { get; set; }
  201. public abstract string StatusDescription { get; set; }
  202. public abstract void Abort ();
  203. public abstract void Close ();
  204. public abstract void SetLength (long value);
  205. public virtual bool SuppressContent { get; set; }
  206. }
  207. class HttpStandaloneResponseInfo : HttpResponseInfo
  208. {
  209. public HttpStandaloneResponseInfo (HttpListenerResponse response)
  210. {
  211. this.res = response;
  212. }
  213. HttpListenerResponse res;
  214. public override string ContentType {
  215. get { return res.ContentType; }
  216. set { res.ContentType = value; }
  217. }
  218. public override NameValueCollection Headers {
  219. get { return res.Headers; }
  220. }
  221. public override int StatusCode {
  222. get { return res.StatusCode; }
  223. set { res.StatusCode = value; }
  224. }
  225. public override string StatusDescription {
  226. get { return res.StatusDescription; }
  227. set { res.StatusDescription = value; }
  228. }
  229. public override Stream OutputStream {
  230. get { return res.OutputStream; }
  231. }
  232. public override void Abort ()
  233. {
  234. res.Abort ();
  235. }
  236. public override void Close ()
  237. {
  238. res.Close ();
  239. }
  240. public override void SetLength (long value)
  241. {
  242. res.ContentLength64 = value;
  243. }
  244. }
  245. class AspNetHttpResponseInfo : HttpResponseInfo
  246. {
  247. public AspNetHttpResponseInfo (HttpResponse response)
  248. {
  249. this.res = response;
  250. }
  251. HttpResponse res;
  252. public override bool SuppressContent {
  253. get { return res.SuppressContent; }
  254. set { res.SuppressContent = value; }
  255. }
  256. public override string ContentType {
  257. get { return res.ContentType; }
  258. set { res.ContentType = value; }
  259. }
  260. public override NameValueCollection Headers {
  261. get { return res.Headers; }
  262. }
  263. public override int StatusCode {
  264. get { return res.StatusCode; }
  265. set { res.StatusCode = value; }
  266. }
  267. public override string StatusDescription {
  268. get { return res.StatusDescription; }
  269. set { res.StatusDescription = value; }
  270. }
  271. public override Stream OutputStream {
  272. get { return res.OutputStream; }
  273. }
  274. public override void Abort ()
  275. {
  276. res.Flush ();
  277. res.Close ();
  278. }
  279. public override void Close ()
  280. {
  281. res.Flush ();
  282. res.Close ();
  283. }
  284. public override void SetLength (long value)
  285. {
  286. res.AddHeader ("Content-Length", value.ToString (CultureInfo.InvariantCulture));
  287. }
  288. }
  289. }