summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Test/Hosting/AspNetHost.cs
blob: 94bf4803ef2be5969eed00d3b0f4f7b125840570 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//-----------------------------------------------------------------------
// <copyright file="AspNetHost.cs" company="Andrew Arnott">
//     Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.Test.Hosting {
	using System;
	using System.IO;
	using System.Net;
	using System.Threading;
	using System.Web;
	using System.Web.Hosting;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.Test.OpenId;

	/// <summary>
	/// Hosts a 'portable' version of the OpenIdProvider for testing itself and the
	/// RelyingParty against it.
	/// </summary>
	internal class AspNetHost : MarshalByRefObject {
		private HttpHost httpHost;

		public AspNetHost() {
			this.httpHost = HttpHost.CreateHost(this);
			////if (!UntrustedWebRequestHandler.WhitelistHosts.Contains("localhost"))
			////    UntrustedWebRequestHandler.WhitelistHosts.Add("localhost");
		}

		public Uri BaseUri {
			get { return this.httpHost.BaseUri; }
		}

		public static AspNetHost CreateHost(string webDirectory) {
			AspNetHost host = (AspNetHost)ApplicationHost.
				CreateApplicationHost(typeof(AspNetHost), "/", webDirectory);
			return host;
		}

		public string ProcessRequest(string url) {
			return this.httpHost.ProcessRequest(url);
		}

		public string ProcessRequest(string url, string body) {
			return this.httpHost.ProcessRequest(url, body);
		}

		public void BeginProcessRequest(HttpListenerContext context) {
			ThreadPool.QueueUserWorkItem(state => { ProcessRequest(context); });
		}

		public void ProcessRequest(HttpListenerContext context) {
			try {
				using (TextWriter tw = new StreamWriter(context.Response.OutputStream)) {
					HttpRuntime.ProcessRequest(new TestingWorkerRequest(context, tw));
				}
			} catch (Exception ex) {
				Logger.Error("Exception in AspNetHost", ex);
				throw;
			}
		}

		public void CloseHttp() {
			this.httpHost.Dispose();
		}
	}
}