/// <summary>
            /// Processes an <see cref="HttpWebRequest"/> and converts the
            /// <see cref="HttpWebResponse"/> to a <see cref="IncomingWebResponse"/> instance.
            /// </summary>
            /// <param name="request">The <see cref="HttpWebRequest"/> to handle.</param>
            /// <returns>An instance of <see cref="IncomingWebResponse"/> describing the response.</returns>
            /// <exception cref="ProtocolException">Thrown for any network error.</exception>
            /// <remarks>
            /// <para>Implementations should catch <see cref="WebException"/> and wrap it in a
            /// <see cref="ProtocolException"/> to abstract away the transport and provide
            /// a single exception type for hosts to catch. The <see cref="WebException.Response"/>
            /// value, if set, should be Closed before throwing.</para>
            /// </remarks>
            public IncomingWebResponse GetResponse(HttpWebRequest request)
            {
                // If the request has an entity, the action would have already been processed in GetRequestStream.
                if (request.Method == "GET")
                {
                    _action(request);
                }

                return(_wrappedHandler.GetResponse(request));
            }
        /// <summary>
        /// Gets the host-meta for a given identifier.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <param name="requestHandler">The request handler.</param>
        /// <param name="signingHost">The host name on the certificate that should be used to verify the signature in the XRDS.</param>
        /// <returns>
        /// The host-meta response, or <c>null</c> if no host-meta document could be obtained.
        /// </returns>
        private IncomingWebResponse GetHostMeta(UriIdentifier identifier, IDirectWebRequestHandler requestHandler, out string signingHost)
        {
            Contract.Requires <ArgumentNullException>(identifier != null);
            Contract.Requires <ArgumentNullException>(requestHandler != null);
            foreach (var hostMetaProxy in this.GetHostMetaLocations(identifier))
            {
                var hostMetaLocation = hostMetaProxy.GetProxy(identifier);
                var request          = (HttpWebRequest)WebRequest.Create(hostMetaLocation);
                request.CachePolicy = Yadis.IdentifierDiscoveryCachePolicy;
                var options = DirectWebRequestOptions.AcceptAllHttpResponses;
                if (identifier.IsDiscoverySecureEndToEnd)
                {
                    options |= DirectWebRequestOptions.RequireSsl;
                }
                var response = requestHandler.GetResponse(request, options);
                if (response.Status == HttpStatusCode.OK)
                {
                    Logger.Yadis.InfoFormat("Found host-meta for {0} at: {1}", identifier.Uri.Host, hostMetaLocation);
                    signingHost = hostMetaProxy.GetSigningHost(identifier);
                    return(response);
                }
                else
                {
                    Logger.Yadis.InfoFormat("Could not obtain host-meta for {0} from {1}", identifier.Uri.Host, hostMetaLocation);
                }
            }

            signingHost = null;
            return(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sends a multipart HTTP POST request (useful for posting files).
        /// </summary>
        /// <param name="request">The HTTP request.</param>
        /// <param name="requestHandler">The request handler.</param>
        /// <param name="parts">The parts to include in the POST entity.</param>
        /// <returns>The HTTP response.</returns>
        public static IncomingWebResponse PostMultipart(this HttpWebRequest request, IDirectWebRequestHandler requestHandler, IEnumerable <MultipartPostPart> parts)
        {
            Contract.Requires <ArgumentNullException>(request != null);
            Contract.Requires <ArgumentNullException>(requestHandler != null);
            Contract.Requires <ArgumentNullException>(parts != null);

            PostMultipartNoGetResponse(request, requestHandler, parts);
            return(requestHandler.GetResponse(request));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Sends a YADIS HTTP request as part of identifier discovery.
        /// </summary>
        /// <param name="requestHandler">The request handler to use to actually submit the request.</param>
        /// <param name="uri">The URI to GET.</param>
        /// <param name="requireSsl">Whether only HTTPS URLs should ever be retrieved.</param>
        /// <param name="acceptTypes">The value of the Accept HTTP header to include in the request.</param>
        /// <returns>The HTTP response retrieved from the request.</returns>
        internal static IncomingWebResponse Request(IDirectWebRequestHandler requestHandler, Uri uri, bool requireSsl, params string[] acceptTypes)
        {
            Contract.Requires <ArgumentNullException>(requestHandler != null);
            Contract.Requires <ArgumentNullException>(uri != null);
            Contract.Ensures(Contract.Result <IncomingWebResponse>() != null);
            Contract.Ensures(Contract.Result <IncomingWebResponse>().ResponseStream != null);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.CachePolicy = IdentifierDiscoveryCachePolicy;
            if (acceptTypes != null)
            {
                request.Accept = string.Join(",", acceptTypes);
            }

            DirectWebRequestOptions options = DirectWebRequestOptions.None;

            if (requireSsl)
            {
                options |= DirectWebRequestOptions.RequireSsl;
            }

            try {
                return(requestHandler.GetResponse(request, options));
            } catch (ProtocolException ex) {
                var webException = ex.InnerException as WebException;
                if (webException != null)
                {
                    var response = webException.Response as HttpWebResponse;
                    if (response != null && response.IsFromCache)
                    {
                        // We don't want to report error responses from the cache, since the server may have fixed
                        // whatever was causing the problem.  So try again with cache disabled.
                        Logger.Messaging.Error("An HTTP error response was obtained from the cache.  Retrying with cache disabled.", ex);
                        var nonCachingRequest = request.Clone();
                        nonCachingRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Reload);
                        return(requestHandler.GetResponse(nonCachingRequest, options));
                    }
                }

                throw;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Gets the XRDS HTTP response for a given identifier.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <param name="requestHandler">The request handler.</param>
        /// <param name="xrdsLocation">The location of the XRDS document to retrieve.</param>
        /// <returns>
        /// A HTTP response carrying an XRDS document.
        /// </returns>
        /// <exception cref="ProtocolException">Thrown if the XRDS document could not be obtained.</exception>
        private static IncomingWebResponse GetXrdsResponse(UriIdentifier identifier, IDirectWebRequestHandler requestHandler, Uri xrdsLocation)
        {
            Requires.NotNull(identifier, "identifier");
            Requires.NotNull(requestHandler, "requestHandler");
            Requires.NotNull(xrdsLocation, "xrdsLocation");

            var request = (HttpWebRequest)WebRequest.Create(xrdsLocation);

            request.CachePolicy = Yadis.IdentifierDiscoveryCachePolicy;
            request.Accept      = ContentTypes.Xrds;
            var options  = identifier.IsDiscoverySecureEndToEnd ? DirectWebRequestOptions.RequireSsl : DirectWebRequestOptions.None;
            var response = requestHandler.GetResponse(request, options).GetSnapshot(Yadis.MaximumResultToScan);

            if (!string.Equals(response.ContentType.MediaType, ContentTypes.Xrds, StringComparison.Ordinal))
            {
                Logger.Yadis.WarnFormat("Host-meta pointed to XRDS at {0}, but Content-Type at that URL was unexpected value '{1}'.", xrdsLocation, response.ContentType);
            }

            return(response);
        }
        /// <summary>
        /// Gets the XRDS HTTP response for a given identifier.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <param name="requestHandler">The request handler.</param>
        /// <param name="xrdsLocation">The location of the XRDS document to retrieve.</param>
        /// <returns>
        /// A HTTP response carrying an XRDS document.
        /// </returns>
        /// <exception cref="ProtocolException">Thrown if the XRDS document could not be obtained.</exception>
        private static IncomingWebResponse GetXrdsResponse(UriIdentifier identifier, IDirectWebRequestHandler requestHandler, Uri xrdsLocation)
        {
            Contract.Requires <ArgumentNullException>(identifier != null);
            Contract.Requires <ArgumentNullException>(requestHandler != null);
            Contract.Requires <ArgumentNullException>(xrdsLocation != null);
            Contract.Ensures(Contract.Result <IncomingWebResponse>() != null);

            var request = (HttpWebRequest)WebRequest.Create(xrdsLocation);

            request.CachePolicy = Yadis.IdentifierDiscoveryCachePolicy;
            request.Accept      = ContentTypes.Xrds;
            var options  = identifier.IsDiscoverySecureEndToEnd ? DirectWebRequestOptions.RequireSsl : DirectWebRequestOptions.None;
            var response = requestHandler.GetResponse(request, options);

            if (!string.Equals(response.ContentType.MediaType, ContentTypes.Xrds, StringComparison.Ordinal))
            {
                Logger.Yadis.WarnFormat("Host-meta pointed to XRDS at {0}, but Content-Type at that URL was unexpected value '{1}'.", xrdsLocation, response.ContentType);
            }

            return(response);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Sends a YADIS HTTP request as part of identifier discovery.
        /// </summary>
        /// <param name="requestHandler">The request handler to use to actually submit the request.</param>
        /// <param name="uri">The URI to GET.</param>
        /// <param name="requireSsl">Whether only HTTPS URLs should ever be retrieved.</param>
        /// <param name="acceptTypes">The value of the Accept HTTP header to include in the request.</param>
        /// <returns>The HTTP response retrieved from the request.</returns>
        internal static DirectWebResponse Request(IDirectWebRequestHandler requestHandler, Uri uri, bool requireSsl, params string[] acceptTypes)
        {
            ErrorUtilities.VerifyArgumentNotNull(requestHandler, "requestHandler");
            ErrorUtilities.VerifyArgumentNotNull(uri, "uri");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.CachePolicy = IdentifierDiscoveryCachePolicy;
            if (acceptTypes != null)
            {
                request.Accept = string.Join(",", acceptTypes);
            }

            DirectWebRequestOptions options = DirectWebRequestOptions.None;

            if (requireSsl)
            {
                options |= DirectWebRequestOptions.RequireSsl;
            }

            return(requestHandler.GetResponse(request, options));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets the host-meta for a given identifier.
        /// </summary>
        /// <param name="identifier">The identifier.</param>
        /// <param name="requestHandler">The request handler.</param>
        /// <param name="signingHost">The host name on the certificate that should be used to verify the signature in the XRDS.</param>
        /// <returns>
        /// The host-meta response, or <c>null</c> if no host-meta document could be obtained.
        /// </returns>
        private IncomingWebResponse GetHostMeta(UriIdentifier identifier, IDirectWebRequestHandler requestHandler, out string signingHost)
        {
            Requires.NotNull(identifier, "identifier");
            Requires.NotNull(requestHandler, "requestHandler");
            foreach (var hostMetaProxy in this.GetHostMetaLocations(identifier))
            {
                var hostMetaLocation = hostMetaProxy.GetProxy(identifier);
                var request          = (HttpWebRequest)WebRequest.Create(hostMetaLocation);
                request.CachePolicy = Yadis.IdentifierDiscoveryCachePolicy;
                var options = DirectWebRequestOptions.AcceptAllHttpResponses;
                if (identifier.IsDiscoverySecureEndToEnd)
                {
                    options |= DirectWebRequestOptions.RequireSsl;
                }
                var response = requestHandler.GetResponse(request, options).GetSnapshot(Yadis.MaximumResultToScan);
                try {
                    if (response.Status == HttpStatusCode.OK)
                    {
                        Logger.Yadis.InfoFormat("Found host-meta for {0} at: {1}", identifier.Uri.Host, hostMetaLocation);
                        signingHost = hostMetaProxy.GetSigningHost(identifier);
                        return(response);
                    }
                    else
                    {
                        Logger.Yadis.InfoFormat("Could not obtain host-meta for {0} from {1}", identifier.Uri.Host, hostMetaLocation);
                        response.Dispose();
                    }
                } catch {
                    response.Dispose();
                    throw;
                }
            }

            signingHost = null;
            return(null);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Sends a YADIS HTTP request as part of identifier discovery.
        /// </summary>
        /// <param name="requestHandler">The request handler to use to actually submit the request.</param>
        /// <param name="uri">The URI to GET.</param>
        /// <param name="requireSsl">Whether only HTTPS URLs should ever be retrieved.</param>
        /// <param name="acceptTypes">The value of the Accept HTTP header to include in the request.</param>
        /// <returns>The HTTP response retrieved from the request.</returns>
        internal static IncomingWebResponse Request(IDirectWebRequestHandler requestHandler, Uri uri, bool requireSsl, params string[] acceptTypes)
        {
            Contract.Requires <ArgumentNullException>(requestHandler != null);
            Contract.Requires <ArgumentNullException>(uri != null);
            Contract.Ensures(Contract.Result <IncomingWebResponse>() != null);
            Contract.Ensures(Contract.Result <IncomingWebResponse>().ResponseStream != null);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.CachePolicy = IdentifierDiscoveryCachePolicy;
            if (acceptTypes != null)
            {
                request.Accept = string.Join(",", acceptTypes);
            }

            DirectWebRequestOptions options = DirectWebRequestOptions.None;

            if (requireSsl)
            {
                options |= DirectWebRequestOptions.RequireSsl;
            }

            return(requestHandler.GetResponse(request, options));
        }
Ejemplo n.º 10
0
		/// <summary>
		/// Gets the host-meta for a given identifier.
		/// </summary>
		/// <param name="identifier">The identifier.</param>
		/// <param name="requestHandler">The request handler.</param>
		/// <param name="signingHost">The host name on the certificate that should be used to verify the signature in the XRDS.</param>
		/// <returns>
		/// The host-meta response, or <c>null</c> if no host-meta document could be obtained.
		/// </returns>
		private IncomingWebResponse GetHostMeta(UriIdentifier identifier, IDirectWebRequestHandler requestHandler, out string signingHost) {
			Contract.Requires<ArgumentNullException>(identifier != null);
			Contract.Requires<ArgumentNullException>(requestHandler != null);
			foreach (var hostMetaProxy in this.GetHostMetaLocations(identifier)) {
				var hostMetaLocation = hostMetaProxy.GetProxy(identifier);
				var request = (HttpWebRequest)WebRequest.Create(hostMetaLocation);
				request.CachePolicy = Yadis.IdentifierDiscoveryCachePolicy;
				var options = DirectWebRequestOptions.AcceptAllHttpResponses;
				if (identifier.IsDiscoverySecureEndToEnd) {
					options |= DirectWebRequestOptions.RequireSsl;
				}
				var response = requestHandler.GetResponse(request, options).GetSnapshot(Yadis.MaximumResultToScan);
				try {
					if (response.Status == HttpStatusCode.OK) {
						Logger.Yadis.InfoFormat("Found host-meta for {0} at: {1}", identifier.Uri.Host, hostMetaLocation);
						signingHost = hostMetaProxy.GetSigningHost(identifier);
						return response;
					} else {
						Logger.Yadis.InfoFormat("Could not obtain host-meta for {0} from {1}", identifier.Uri.Host, hostMetaLocation);
						response.Dispose();
					}
				} catch {
					response.Dispose();
					throw;
				}
			}

			signingHost = null;
			return null;
		}
Ejemplo n.º 11
0
		/// <summary>
		/// Gets the XRDS HTTP response for a given identifier.
		/// </summary>
		/// <param name="identifier">The identifier.</param>
		/// <param name="requestHandler">The request handler.</param>
		/// <param name="xrdsLocation">The location of the XRDS document to retrieve.</param>
		/// <returns>
		/// A HTTP response carrying an XRDS document.
		/// </returns>
		/// <exception cref="ProtocolException">Thrown if the XRDS document could not be obtained.</exception>
		private static IncomingWebResponse GetXrdsResponse(UriIdentifier identifier, IDirectWebRequestHandler requestHandler, Uri xrdsLocation) {
			Contract.Requires<ArgumentNullException>(identifier != null);
			Contract.Requires<ArgumentNullException>(requestHandler != null);
			Contract.Requires<ArgumentNullException>(xrdsLocation != null);
			Contract.Ensures(Contract.Result<IncomingWebResponse>() != null);

			var request = (HttpWebRequest)WebRequest.Create(xrdsLocation);
			request.CachePolicy = Yadis.IdentifierDiscoveryCachePolicy;
			request.Accept = ContentTypes.Xrds;
			var options = identifier.IsDiscoverySecureEndToEnd ? DirectWebRequestOptions.RequireSsl : DirectWebRequestOptions.None;
			var response = requestHandler.GetResponse(request, options).GetSnapshot(Yadis.MaximumResultToScan);
			if (!string.Equals(response.ContentType.MediaType, ContentTypes.Xrds, StringComparison.Ordinal)) {
				Logger.Yadis.WarnFormat("Host-meta pointed to XRDS at {0}, but Content-Type at that URL was unexpected value '{1}'.", xrdsLocation, response.ContentType);
			}

			return response;
		}
Ejemplo n.º 12
0
        /// <summary>
        /// Sends a YADIS HTTP request as part of identifier discovery.
        /// </summary>
        /// <param name="requestHandler">The request handler to use to actually submit the request.</param>
        /// <param name="uri">The URI to GET.</param>
        /// <param name="requireSsl">Whether only HTTPS URLs should ever be retrieved.</param>
        /// <param name="acceptTypes">The value of the Accept HTTP header to include in the request.</param>
        /// <returns>The HTTP response retrieved from the request.</returns>
        internal static IncomingWebResponse Request(IDirectWebRequestHandler requestHandler, Uri uri, bool requireSsl, params string[] acceptTypes)
        {
            ErrorUtilities.VerifyArgumentNotNull(requestHandler, "requestHandler");
            ErrorUtilities.VerifyArgumentNotNull(uri, "uri");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.CachePolicy = IdentifierDiscoveryCachePolicy;
            if (acceptTypes != null) {
                request.Accept = string.Join(",", acceptTypes);
            }

            DirectWebRequestOptions options = DirectWebRequestOptions.None;
            if (requireSsl) {
                options |= DirectWebRequestOptions.RequireSsl;
            }

            return requestHandler.GetResponse(request, options);
        }