summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId.Test/Hosting/AspNetHost.cs
blob: f171f5a326d3414328c40bac6206140825acc57e (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
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Hosting;
using System.IO;
using System.Diagnostics;
using System.Net;
using DotNetOpenId.Provider;
using System.Threading;

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

		public AspNetHost() {
			httpHost = HttpHost.CreateHost(this);
			if (!UntrustedWebRequest.WhitelistHosts.Contains("localhost"))
				UntrustedWebRequest.WhitelistHosts.Add("localhost");
			DotNetOpenId.Provider.SigningMessageEncoder.Signing += (s, e) => {
				if (MessageInterceptor != null) MessageInterceptor.OnSigningMessage(e.Message);
			};
		}

		public Uri BaseUri { get { return httpHost.BaseUri; } }
		public EncodingInterceptor MessageInterceptor { get; set; }

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

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

		public string ProcessRequest(string url, string body) {
			return 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) {
				Console.WriteLine(ex);
				throw;
			}
		}

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