summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth/OpenId/ChannelElements/Accept400ErrorWebRequestHandlerWrappers.cs
blob: 67fd0cd804f778c286ca1881ec7f944c2ed9a476 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
namespace DotNetOpenAuth.OpenId.ChannelElements {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using DotNetOpenAuth.Messaging;
	using System.IO;
	using System.Net;

	internal class Accept400ErrorDirectWebRequestHandlerWrapper : IDirectWebRequestHandler {
		private IDirectWebRequestHandler wrappedHandler;

		internal Accept400ErrorDirectWebRequestHandlerWrapper(IDirectWebRequestHandler wrappedHandler) {
			ErrorUtilities.VerifyArgumentNotNull(wrappedHandler, "wrappedHandler");
			this.wrappedHandler = wrappedHandler;
		}

		internal IDirectWebRequestHandler WrappedHandler {
			get { return this.wrappedHandler; }
		}

		#region IDirectWebRequestHandler Members

		public Stream GetRequestStream(HttpWebRequest request) {
			return this.wrappedHandler.GetRequestStream(request);
		}

		public DirectWebResponse GetResponse(HttpWebRequest request) {
			try {
				return this.wrappedHandler.GetResponse(request);
			} catch (ProtocolException ex) {
				WebException innerWeb = ex.InnerException as WebException;
				if (innerWeb != null && innerWeb.Status == WebExceptionStatus.ProtocolError) {
					HttpWebResponse httpResponse = innerWeb.Response as HttpWebResponse;
					if (httpResponse != null && httpResponse.StatusCode == HttpStatusCode.BadRequest) {
						// This is OK.  The OpenID spec says that server errors be returned as HTTP 400,
						// So we'll just swallow the exception and generate the message that's in the
						// error response.
					}
				}

				// This isn't a recognized acceptable case.
				throw;
			}
		}

		#endregion
	}

	internal class Accept400ErrorDirectSslWebRequestHandlerWrapper : Accept400ErrorDirectWebRequestHandlerWrapper, IDirectSslWebRequestHandler {
		private IDirectSslWebRequestHandler wrappedHandler;

		internal Accept400ErrorDirectSslWebRequestHandlerWrapper(IDirectSslWebRequestHandler wrappedHandler)
			: base(wrappedHandler) {
			this.wrappedHandler = wrappedHandler;
		}

		#region IDirectSslWebRequestHandler Members

		public Stream GetRequestStream(HttpWebRequest request, bool requireSsl) {
			return this.wrappedHandler.GetRequestStream(request, requireSsl);
		}

		public DirectWebResponse GetResponse(HttpWebRequest request, bool requireSsl) {
			try {
				return this.wrappedHandler.GetResponse(request, requireSsl);
			} catch (ProtocolException ex) {
				WebException innerWeb = ex.InnerException as WebException;
				if (innerWeb != null && innerWeb.Status == WebExceptionStatus.ProtocolError) {
					HttpWebResponse httpResponse = innerWeb.Response as HttpWebResponse;
					if (httpResponse != null && httpResponse.StatusCode == HttpStatusCode.BadRequest) {
						// This is OK.  The OpenID spec says that server errors be returned as HTTP 400,
						// So we'll just swallow the exception and generate the message that's in the
						// error response.
					}
				}

				// This isn't a recognized acceptable case.
				throw;
			}
		}

		#endregion
	}
}