// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Net.Http; using System.ServiceModel.Channels; namespace System.Web.Http.SelfHost.Channels { /// /// Provides extension methods for getting an instance or /// an instance from a instance and /// provides extension methods for creating a instance from either an /// instance or an /// instance. /// internal static class HttpMessageExtensions { internal const string ToHttpRequestMessageMethodName = "ToHttpRequestMessage"; internal const string ToHttpResponseMessageMethodName = "ToHttpResponseMessage"; internal const string ToMessageMethodName = "ToMessage"; /// /// Returns a reference to the /// instance held by the given or null if the does not /// hold a reference to an /// instance. /// /// The given that holds a reference to an /// instance. /// /// /// A reference to the /// instance held by the given or null if the does not /// hold a reference to an /// instance. /// public static HttpRequestMessage ToHttpRequestMessage(this Message message) { if (message == null) { throw Error.ArgumentNull("message"); } HttpMessage httpMessage = message as HttpMessage; if (httpMessage != null && httpMessage.IsRequest) { return httpMessage.GetHttpRequestMessage(false); } return null; } /// /// Returns a reference to the /// instance held by the given or null if the does not /// hold a reference to an /// instance. /// /// The given that holds a reference to an /// instance. /// /// /// A reference to the /// instance held by the given or null if the does not /// hold a reference to an /// instance. /// public static HttpResponseMessage ToHttpResponseMessage(this Message message) { if (message == null) { throw Error.ArgumentNull("message"); } HttpMessage httpMessage = message as HttpMessage; if (httpMessage != null && !httpMessage.IsRequest) { return httpMessage.GetHttpResponseMessage(false); } return null; } /// /// Returns a reference to the /// instance held by the given or null if the does not /// hold a reference to an /// instance. /// /// The caller takes over the ownership of the associated and is responsible for its disposal. /// The given that holds a reference to an /// instance. /// /// /// A reference to the /// instance held by the given or null if the does not /// hold a reference to an /// instance. /// The caller is responsible for disposing any instance returned. /// public static HttpResponseMessage ExtractHttpResponseMessage(this Message message) { if (message == null) { throw Error.ArgumentNull("message"); } HttpMessage httpMessage = message as HttpMessage; if (httpMessage != null && !httpMessage.IsRequest) { return httpMessage.GetHttpResponseMessage(true); } return null; } /// /// Creates a new that holds a reference to the given /// instance. /// /// The /// instance to which the new should hold a reference. /// /// A that holds a reference to the given /// instance. /// public static Message ToMessage(this HttpRequestMessage request) { if (request == null) { throw Error.ArgumentNull("request"); } return new HttpMessage(request); } /// /// Creates a new that holds a reference to the given /// instance. /// /// The /// instance to which the new should hold a reference. /// /// A that holds a reference to the given /// instance. /// public static Message ToMessage(this HttpResponseMessage response) { if (response == null) { throw Error.ArgumentNull("response"); } return new HttpMessage(response); } } }