diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2010-01-26 07:02:42 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2010-01-26 07:03:59 -0800 |
commit | 4ab23e96ec0092617e2f4de29b65ae47fa24f228 (patch) | |
tree | 9f5308df92782bcb8b700b03fe1a94a3e5683353 /src | |
parent | cbdcd5be026a4a1155e1ab56b4ed0fb6b13ce358 (diff) | |
download | DotNetOpenAuth-4ab23e96ec0092617e2f4de29b65ae47fa24f228.zip DotNetOpenAuth-4ab23e96ec0092617e2f4de29b65ae47fa24f228.tar.gz DotNetOpenAuth-4ab23e96ec0092617e2f4de29b65ae47fa24f228.tar.bz2 |
Fixed Content-Encoding header bug.
Thanks to Andrew Csontos for finding this bug.
Diffstat (limited to 'src')
-rw-r--r-- | src/DotNetOpenAuth/Messaging/Channel.cs | 16 | ||||
-rw-r--r-- | src/DotNetOpenAuth/Messaging/MessagingUtilities.cs | 12 | ||||
-rw-r--r-- | src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs | 17 |
3 files changed, 29 insertions, 16 deletions
diff --git a/src/DotNetOpenAuth/Messaging/Channel.cs b/src/DotNetOpenAuth/Messaging/Channel.cs index 42b932e..831283a 100644 --- a/src/DotNetOpenAuth/Messaging/Channel.cs +++ b/src/DotNetOpenAuth/Messaging/Channel.cs @@ -16,6 +16,7 @@ namespace DotNetOpenAuth.Messaging { using System.Linq; using System.Net; using System.Net.Cache; + using System.Net.Mime; using System.Text; using System.Threading; using System.Web; @@ -39,6 +40,13 @@ namespace DotNetOpenAuth.Messaging { protected internal const string HttpFormUrlEncoded = "application/x-www-form-urlencoded"; /// <summary> + /// The content-type used on HTTP POST requests where the POST entity is a + /// URL-encoded series of key=value pairs. + /// This includes the <see cref="PostEntityEncoding"/> character encoding. + /// </summary> + protected internal static readonly ContentType HttpFormUrlEncodedContentType = new ContentType(HttpFormUrlEncoded) { CharSet = PostEntityEncoding.WebName }; + + /// <summary> /// The maximum allowable size for a 301 Redirect response before we send /// a 200 OK response with a scripted form POST with the parameters instead /// in order to ensure successfully sending a large payload to another server @@ -916,15 +924,9 @@ namespace DotNetOpenAuth.Messaging { Contract.Requires<ArgumentNullException>(httpRequest != null); Contract.Requires<ArgumentNullException>(fields != null); - httpRequest.ContentType = HttpFormUrlEncoded; - - // Setting the content-encoding to "utf-8" causes Google to reply - // with a 415 UnsupportedMediaType. But adding it doesn't buy us - // anything specific, so we disable it until we know how to get it right. - ////httpRequest.Headers[HttpRequestHeader.ContentEncoding] = PostEntityEncoding.WebName; - string requestBody = MessagingUtilities.CreateQueryString(fields); byte[] requestBytes = PostEntityEncoding.GetBytes(requestBody); + httpRequest.ContentType = HttpFormUrlEncodedContentType.ToString(); httpRequest.ContentLength = requestBytes.Length; Stream requestStream = this.WebRequestHandler.GetRequestStream(httpRequest); try { diff --git a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs index a6c1c65..04d91de 100644 --- a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs +++ b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs @@ -14,6 +14,7 @@ namespace DotNetOpenAuth.Messaging { using System.IO; using System.Linq; using System.Net; + using System.Net.Mime; using System.Security; using System.Security.Cryptography; using System.Text; @@ -158,20 +159,19 @@ namespace DotNetOpenAuth.Messaging { string initialPartLeadingBoundary = string.Format(CultureInfo.InvariantCulture, "--{0}\r\n", boundary); string partLeadingBoundary = string.Format(CultureInfo.InvariantCulture, "\r\n--{0}\r\n", boundary); string finalTrailingBoundary = string.Format(CultureInfo.InvariantCulture, "\r\n--{0}--\r\n", boundary); + var contentType = new ContentType("multipart/form-data") { + Boundary = boundary, + CharSet = Channel.PostEntityEncoding.WebName, + }; request.Method = "POST"; - request.ContentType = "multipart/form-data; boundary=" + boundary; + request.ContentType = contentType.ToString(); long contentLength = parts.Sum(p => partLeadingBoundary.Length + p.Length) + finalTrailingBoundary.Length; if (parts.Any()) { contentLength -= 2; // the initial part leading boundary has no leading \r\n } request.ContentLength = contentLength; - // Setting the content-encoding to "utf-8" causes Google to reply - // with a 415 UnsupportedMediaType. But adding it doesn't buy us - // anything specific, so we disable it until we know how to get it right. - ////request.Headers[HttpRequestHeader.ContentEncoding] = Channel.PostEntityEncoding.WebName; - var requestStream = requestHandler.GetRequestStream(request); try { StreamWriter writer = new StreamWriter(requestStream, Channel.PostEntityEncoding); diff --git a/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs b/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs index f6a930c..3d470bd 100644 --- a/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs +++ b/src/DotNetOpenAuth/Messaging/OutgoingWebResponse.cs @@ -10,6 +10,7 @@ namespace DotNetOpenAuth.Messaging { using System.Diagnostics.Contracts; using System.IO; using System.Net; + using System.Net.Mime; using System.Text; using System.Threading; using System.Web; @@ -87,7 +88,7 @@ namespace DotNetOpenAuth.Messaging { /// </summary> public string Body { get { return this.ResponseStream != null ? this.GetResponseReader().ReadToEnd() : null; } - set { this.SetResponse(value); } + set { this.SetResponse(value, null); } } /// <summary> @@ -205,13 +206,23 @@ namespace DotNetOpenAuth.Messaging { /// Sets the response to some string, encoded as UTF-8. /// </summary> /// <param name="body">The string to set the response to.</param> - internal void SetResponse(string body) { + /// <param name="contentType">Type of the content. May be null.</param> + internal void SetResponse(string body, ContentType contentType) { if (body == null) { this.ResponseStream = null; return; } - this.Headers[HttpResponseHeader.ContentEncoding] = bodyStringEncoder.HeaderName; + if (contentType == null) { + contentType = new ContentType("text/html"); + contentType.CharSet = bodyStringEncoder.WebName; + } else if (contentType.CharSet != bodyStringEncoder.WebName) { + // clone the original so we're not tampering with our inputs if it came as a parameter. + contentType = new ContentType(contentType.ToString()); + contentType.CharSet = bodyStringEncoder.WebName; + } + + this.Headers[HttpResponseHeader.ContentType] = contentType.ToString(); this.ResponseStream = new MemoryStream(); StreamWriter writer = new StreamWriter(this.ResponseStream, bodyStringEncoder); writer.Write(body); |