//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOAuth.Messaging { using System; using System.Collections.Specialized; using System.Diagnostics; using System.IO; using System.Net; using System.Web; /// /// A property store of details of an incoming HTTP request. /// /// /// This serves a very similar purpose to , except that /// ASP.NET does not let us fully initialize that class, so we have to write one /// of our one. /// internal class HttpRequestInfo { /// /// The key/value pairs found in the entity of a POST request. /// private NameValueCollection form; /// /// The key/value pairs found in the querystring of the incoming request. /// private NameValueCollection queryString; /// /// Initializes a new instance of the class. /// internal HttpRequestInfo() { } /// /// Initializes a new instance of the class. /// /// The ASP.NET structure to copy from. internal HttpRequestInfo(HttpRequest request) { this.HttpMethod = request.HttpMethod; this.Url = request.Url; this.Headers = GetHeaderCollection(request.Headers); this.InputStream = request.InputStream; // These values would normally be calculated, but we'll reuse them from // HttpRequest since they're already calculated, and there's a chance () // that ASP.NET does a better job of being comprehensive about gathering // these as well. this.form = request.Form; this.queryString = request.QueryString; } /// /// Gets or sets the verb in the request (i.e. GET, POST, etc.) /// internal string HttpMethod { get; set; } /// /// Gets or sets the entire URL of the request. /// internal Uri Url { get; set; } /// /// Gets the query part of the URL (The ? and everything after it). /// internal string Query { get { return this.Url != null ? this.Url.Query : null; } } /// /// Gets or sets the collection of headers that came in with the request. /// internal WebHeaderCollection Headers { get; set; } /// /// Gets or sets the entity, or body of the request, if any. /// internal Stream InputStream { get; set; } /// /// Gets the key/value pairs found in the entity of a POST request. /// internal NameValueCollection Form { get { if (this.form == null) { if (this.HttpMethod == "POST" && this.Headers[HttpRequestHeader.ContentType] == "application/x-www-form-urlencoded") { StreamReader reader = new StreamReader(this.InputStream); long originalPosition = this.InputStream.Position; this.form = HttpUtility.ParseQueryString(reader.ReadToEnd()); if (this.InputStream.CanSeek) { this.InputStream.Seek(originalPosition, SeekOrigin.Begin); } } else { this.form = new NameValueCollection(); } } return this.form; } } /// /// Gets the key/value pairs found in the querystring of the incoming request. /// internal NameValueCollection QueryString { get { if (this.queryString == null) { this.queryString = HttpUtility.ParseQueryString(this.Query); } return this.queryString; } } /// /// Converts a NameValueCollection to a WebHeaderCollection. /// /// The collection a HTTP headers. /// A new collection of the given headers. private static WebHeaderCollection GetHeaderCollection(NameValueCollection pairs) { Debug.Assert(pairs != null, "pairs == null"); WebHeaderCollection headers = new WebHeaderCollection(); foreach (string key in pairs) { headers.Add(key, pairs[key]); } return headers; } } }