HttpContextInfo.cs 8.1 KB

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