| | 1 | | /* |
| | 2 | | * Copyright 2018 Stanislav Muhametsin. All rights Reserved. |
| | 3 | | * |
| | 4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | 5 | | * you may not use this file except in compliance with the License. |
| | 6 | | * You may obtain a copy of the License at |
| | 7 | | * |
| | 8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | 9 | | * |
| | 10 | | * Unless required by applicable law or agreed to in writing, software |
| | 11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | 12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| | 13 | | * implied. |
| | 14 | | * |
| | 15 | | * See the License for the specific language governing permissions and |
| | 16 | | * limitations under the License. |
| | 17 | | */ |
| | 18 | | using CBAM.Abstractions.Implementation; |
| | 19 | | using CBAM.Abstractions.Implementation.NetworkStream; |
| | 20 | | using CBAM.HTTP; |
| | 21 | | using CBAM.HTTP.Implementation; |
| | 22 | | using IOUtils.Network.Configuration; |
| | 23 | | using ResourcePooling.Async.Abstractions; |
| | 24 | | using System; |
| | 25 | | using System.Collections.Generic; |
| | 26 | | using System.IO; |
| | 27 | | using System.Net; |
| | 28 | | using System.Text; |
| | 29 | | using System.Threading; |
| | 30 | | using System.Threading.Tasks; |
| | 31 | | using UtilPack; |
| | 32 | |
|
| | 33 | | namespace CBAM.HTTP.Implementation |
| | 34 | | { |
| | 35 | | /// <summary> |
| | 36 | | /// This class provides static <see cref="Factory"/> property to create connection pools that provide instances of <s |
| | 37 | | /// This class also can be used dynamically by some other code, and then use <see cref="AsyncResourceFactoryProvider. |
| | 38 | | /// The configuration type for this factory class is <see cref="HTTPNetworkCreationInfo"/>. |
| | 39 | | /// For simpler configuration support, see <see cref="HTTPSimpleConfigurationPoolProvider{TRequestMetaData}"/> |
| | 40 | | /// </summary> |
| | 41 | | /// <typeparam name="TRequestMetaData">The type of metadata associated with each request, used in identifying the req |
| | 42 | | /// <seealso cref="HTTPSimpleConfigurationPoolProvider{TRequestMetaData}"/> |
| | 43 | | public sealed class HTTPNetworkConnectionPoolProvider<TRequestMetaData> : AbstractAsyncResourceFactoryProvider<HTTPCo |
| | 44 | | { |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Gets the <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create pools that provide instances |
| | 48 | | /// </summary> |
| | 49 | | /// <value>The <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create <see cref="HTTPConnection{T |
| | 50 | | /// <remarks> |
| | 51 | | /// By invoking <see cref="AsyncResourceFactory{TResource, TParams}.BindCreationParameters"/>, one gets the bound |
| | 52 | | /// Instead of directly using <see cref="AsyncResourceFactory{TResource}.CreateAcquireResourceContext"/>, typical |
| | 53 | | /// </remarks> |
| 5 | 54 | | public static AsyncResourceFactory<HTTPConnection<TRequestMetaData>, HTTPNetworkCreationInfo> Factory { get; } = n |
| 3 | 55 | | .NewFactoryParametrizer<HTTPNetworkCreationInfo, HTTPNetworkCreationInfoData, HTTPConnectionConfiguration, H |
| 3 | 56 | | .BindPublicConnectionType<HTTPConnection<TRequestMetaData>>() |
| 3 | 57 | | .CreateStatelessDelegatingConnectionFactory( |
| 3 | 58 | | Encoding.ASCII.CreateDefaultEncodingInfo(), |
| 3 | 59 | | ( parameters, encodingInfo, stringPool, stringPoolIsDedicated ) => |
| 3 | 60 | | { |
| 5 | 61 | | return TaskUtils.TaskFromBoolean( ( parameters.CreationData?.Connection?.ConnectionSSLMode ?? Connecti |
| 3 | 62 | | }, |
| 3 | 63 | | null, |
| 3 | 64 | | null, |
| 3 | 65 | | null, |
| 3 | 66 | | null, |
| 3 | 67 | | null, |
| 3 | 68 | | ( parameters, encodingInfo, stringPool, stringPoolIsDedicated, stream, socketOrNull, token ) => |
| 3 | 69 | | { |
| 5 | 70 | | return new ValueTask<HTTPConnectionFunctionalityImpl<TRequestMetaData>>( |
| 5 | 71 | | new HTTPConnectionFunctionalityImpl<TRequestMetaData>( |
| 5 | 72 | | HTTPConnectionVendorImpl<TRequestMetaData>.Instance, |
| 5 | 73 | | new ClientProtocolIOState( |
| 5 | 74 | | stream, |
| 5 | 75 | | stringPool, |
| 5 | 76 | | encodingInfo, |
| 5 | 77 | | new WriteState(), |
| 5 | 78 | | new ReadState() |
| 5 | 79 | | ) ) |
| 5 | 80 | | ); |
| 3 | 81 | | }, |
| 5 | 82 | | functionality => new ValueTask<HTTPConnectionImpl<TRequestMetaData>>( new HTTPConnectionImpl<TRequestMeta |
| 5 | 83 | | ( functionality, connection ) => new StatelessConnectionAcquireInfo<HTTPConnectionImpl<TRequestMetaData>, |
| 3 | 84 | | ( functionality, connection, token, error ) => functionality?.Stream |
| 3 | 85 | | ) ); |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Creates a new instance of <see cref="HTTPNetworkConnectionPoolProvider{TRequestMetaData}"/>. |
| | 89 | | /// </summary> |
| | 90 | | /// <remarks> |
| | 91 | | /// This constructor is not intended to be used directly, but a generic scenarios like MSBuild task dynamically lo |
| | 92 | | /// </remarks> |
| | 93 | | public HTTPNetworkConnectionPoolProvider() |
| 0 | 94 | | : base( typeof( HTTPNetworkCreationInfoData ) ) |
| | 95 | | { |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <inheritdoc /> |
| | 99 | | protected override HTTPNetworkCreationInfo TransformFactoryParameters( Object creationParameters ) |
| | 100 | | { |
| 0 | 101 | | ArgumentValidator.ValidateNotNull( nameof( creationParameters ), creationParameters ); |
| | 102 | |
|
| | 103 | | HTTPNetworkCreationInfo retVal; |
| 0 | 104 | | switch ( creationParameters ) |
| | 105 | | { |
| | 106 | | case HTTPNetworkCreationInfoData creationData: |
| 0 | 107 | | retVal = new HTTPNetworkCreationInfo( creationData ); |
| 0 | 108 | | break; |
| | 109 | | case HTTPNetworkCreationInfo creationInfo: |
| 0 | 110 | | retVal = creationInfo; |
| 0 | 111 | | break; |
| | 112 | | case SimpleHTTPConfiguration simpleConfig: |
| 0 | 113 | | retVal = simpleConfig.CreateNetworkCreationInfo(); |
| 0 | 114 | | break; |
| | 115 | | default: |
| 0 | 116 | | throw new ArgumentException( $"The {nameof( creationParameters )} must be instance of {typeof( HTTPNetwor |
| | 117 | | } |
| 0 | 118 | | return retVal; |
| | 119 | | } |
| | 120 | |
|
| | 121 | | /// <summary> |
| | 122 | | /// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}. |
| | 123 | | /// </summary> |
| | 124 | | /// <returns>The value of <see cref="Factory"/> static property.</returns> |
| | 125 | | protected override AsyncResourceFactory<HTTPConnection<TRequestMetaData>, HTTPNetworkCreationInfo> CreateFactory() |
| 0 | 126 | | => Factory; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// This class provides static <see cref="Factory"/> property to create connection pools that provide instances of <s |
| | 131 | | /// This class also can be used dynamically by some other code, and then use <see cref="AsyncResourceFactoryProvider. |
| | 132 | | /// The configuration type for this factory class is <see cref="SimpleHTTPConfiguration"/>. |
| | 133 | | /// For more advanced and customizable configuration support, see <see cref="HTTPNetworkConnectionPoolProvider{TReque |
| | 134 | | /// </summary> |
| | 135 | | /// <typeparam name="TRequestMetaData">The type of metadata associated with each request, used in identifying the req |
| | 136 | | /// <seealso cref="HTTPNetworkConnectionPoolProvider{TRequestMetaData}"/> |
| | 137 | | public sealed class HTTPSimpleConfigurationPoolProvider<TRequestMetaData> : AbstractAsyncResourceFactoryProvider<HTTP |
| | 138 | | { |
| | 139 | | /// <summary> |
| | 140 | | /// Gets the <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create pools that provide instances |
| | 141 | | /// </summary> |
| | 142 | | /// <value>The <see cref="AsyncResourceFactory{TResource, TParams}"/> which can create <see cref="HTTPConnection{T |
| | 143 | | /// <remarks> |
| | 144 | | /// By invoking <see cref="AsyncResourceFactory{TResource, TParams}.BindCreationParameters"/>, one gets the bound |
| | 145 | | /// Instead of directly using <see cref="AsyncResourceFactory{TResource}.CreateAcquireResourceContext"/>, typical |
| | 146 | | /// </remarks> |
| | 147 | | public static AsyncResourceFactory<HTTPConnection<TRequestMetaData>, SimpleHTTPConfiguration> Factory { get; } = n |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Creates a new instance of <see cref="HTTPSimpleConfigurationPoolProvider{TRequestMetaData}"/>. |
| | 151 | | /// </summary> |
| | 152 | | /// <remarks> |
| | 153 | | /// This constructor is not intended to be used directly, but a generic scenarios like MSBuild task dynamically lo |
| | 154 | | /// </remarks> |
| | 155 | | public HTTPSimpleConfigurationPoolProvider() |
| | 156 | | : base( typeof( SimpleHTTPConfiguration ) ) |
| | 157 | | { |
| | 158 | | } |
| | 159 | |
|
| | 160 | | /// <inheritdoc /> |
| | 161 | | protected override SimpleHTTPConfiguration TransformFactoryParameters( Object creationParameters ) |
| | 162 | | { |
| | 163 | | ArgumentValidator.ValidateNotNull( nameof( creationParameters ), creationParameters ); |
| | 164 | |
|
| | 165 | | SimpleHTTPConfiguration retVal; |
| | 166 | | switch ( creationParameters ) |
| | 167 | | { |
| | 168 | | case SimpleHTTPConfiguration simpleConfig: |
| | 169 | | retVal = simpleConfig; |
| | 170 | | break; |
| | 171 | | default: |
| | 172 | | throw new ArgumentException( $"The {nameof( creationParameters )} must be instance of { typeof( SimpleHTT |
| | 173 | | } |
| | 174 | | return retVal; |
| | 175 | | } |
| | 176 | |
|
| | 177 | | /// <summary> |
| | 178 | | /// This method implements <see cref="AbstractAsyncResourceFactoryProvider{TFactoryResource, TCreationParameters}. |
| | 179 | | /// </summary> |
| | 180 | | /// <returns>The value of <see cref="Factory"/> static property.</returns> |
| | 181 | | protected override AsyncResourceFactory<HTTPConnection<TRequestMetaData>, SimpleHTTPConfiguration> CreateFactory() |
| | 182 | | => Factory; |
| | 183 | | } |
| | 184 | |
|
| | 185 | |
|
| | 186 | | } |
| | 187 | |
|
| | 188 | | public static partial class E_CBAM |
| | 189 | | { |
| | 190 | | /// <summary> |
| | 191 | | /// This is ease-of-life method to asynchronously create <see cref="HTTPConnection{TRequestMetaData}"/> pool, send on |
| | 192 | | /// </summary> |
| | 193 | | /// <param name="config">This <see cref="SimpleHTTPConfiguration"/>.</param> |
| | 194 | | /// <param name="request">The request to send.</param> |
| | 195 | | /// <param name="defaultEncoding">The default encoding to use when stringifying the response contents. If not specifi |
| | 196 | | /// <returns>Asynchronously returns the <see cref="HTTPTextualResponseInfo"/> of the first response that server sends |
| | 197 | | /// <exception cref="NullReferenceException">If this <see cref="SimpleHTTPConfiguration"/> is <c>null</c>.</exception |
| | 198 | | /// <exception cref="ArgumentNullException">If <paramref name="request"/> is <c>null</c>.</exception> |
| | 199 | | public static Task<HTTPTextualResponseInfo> CreatePoolAndReceiveTextualResponseAsync( this SimpleHTTPConfiguration co |
| | 200 | | { |
| | 201 | | ArgumentValidator.ValidateNotNullReference( config ); |
| | 202 | | ArgumentValidator.ValidateNotNull( nameof( request ), request ); |
| | 203 | | return HTTPSimpleConfigurationPoolProvider<Int64> |
| | 204 | | .Factory |
| | 205 | | .BindCreationParameters( config ) |
| | 206 | | .CreateOneTimeUseResourcePool() |
| | 207 | | .UseResourceAsync( async conn => { return await conn.ReceiveOneTextualResponseAsync( request, defaultEncoding: |
| | 208 | | } |
| | 209 | | } |