Skip to content

Commit cfc0f44

Browse files
committed
Added extension method to stringify the http method
1 parent 753c114 commit cfc0f44

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

AngleSharp.Io/AngleSharp.Io.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<Reference Include="System.Xml" />
4444
</ItemGroup>
4545
<ItemGroup>
46+
<Compile Include="Extensions\GeneralExtensions.cs" />
4647
<Compile Include="Network\HttpClientRequester.cs" />
4748
<Compile Include="Network\Response.cs" />
4849
<Compile Include="Properties\AssemblyInfo.cs" />
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace AngleSharp.Io.Extensions
2+
{
3+
using AngleSharp.Network;
4+
using System;
5+
6+
/// <summary>
7+
/// Some general extension methods.
8+
/// </summary>
9+
static class GeneralExtensions
10+
{
11+
/// <summary>
12+
/// Returns the string representation for the specified HTTP method.
13+
/// </summary>
14+
/// <param name="method">The type of HTTP method to stringify.</param>
15+
/// <returns>The string representing the HTTP method.</returns>
16+
public static String Stringify(this HttpMethod method)
17+
{
18+
switch (method)
19+
{
20+
case HttpMethod.Get:
21+
return "GET";
22+
case HttpMethod.Delete:
23+
return "DELETE";
24+
case HttpMethod.Post:
25+
return "POST";
26+
case HttpMethod.Put:
27+
return "PUT";
28+
default:
29+
return method.ToString().ToUpperInvariant();
30+
}
31+
}
32+
}
33+
}

AngleSharp.Io/Network/HttpClientRequester.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
namespace AngleSharp.Io.Network
22
{
3+
using AngleSharp.Io.Extensions;
4+
using AngleSharp.Network;
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
68
using System.Net.Http;
79
using System.Threading;
810
using System.Threading.Tasks;
9-
using AngleSharp.Network;
1011
using HttpMethod = System.Net.Http.HttpMethod;
1112

1213
/// <summary>
@@ -29,14 +30,15 @@ public HttpClientRequester(HttpClient client)
2930
/// Checks if the given protocol is supported.
3031
/// </summary>
3132
/// <param name="protocol">
32-
/// The protocol to check for, e.g. http.
33+
/// The protocol to check for, e.g., http.
3334
/// </param>
3435
/// <returns>
3536
/// True if the protocol is supported, otherwise false.
3637
/// </returns>
3738
public Boolean SupportsProtocol(String protocol)
3839
{
39-
return protocol.Equals("http", StringComparison.OrdinalIgnoreCase) || protocol.Equals("https", StringComparison.OrdinalIgnoreCase);
40+
return protocol.Equals("http", StringComparison.OrdinalIgnoreCase) ||
41+
protocol.Equals("https", StringComparison.OrdinalIgnoreCase);
4042
}
4143

4244
/// <summary>
@@ -50,9 +52,10 @@ public Boolean SupportsProtocol(String protocol)
5052
public async Task<IResponse> RequestAsync(IRequest request, CancellationToken cancel)
5153
{
5254
// create the request message
53-
var method = new HttpMethod(request.Method.ToString().ToUpper());
55+
var method = new HttpMethod(request.Method.Stringify());
5456
var requestMessage = new HttpRequestMessage(method, request.Address);
5557
var contentHeaders = new List<KeyValuePair<String, String>>();
58+
5659
foreach (var header in request.Headers)
5760
{
5861
// Source:
@@ -65,12 +68,13 @@ public async Task<IResponse> RequestAsync(IRequest request, CancellationToken ca
6568
if (request.Content != null && method != HttpMethod.Get && method != HttpMethod.Head)
6669
{
6770
requestMessage.Content = new StreamContent(request.Content);
71+
6872
foreach (var header in contentHeaders)
6973
requestMessage.Content.Headers.TryAddWithoutValidation(header.Key, header.Value);
7074
}
7175

7276
// execute the request
73-
var responseMessage = await _client.SendAsync(requestMessage, cancel);
77+
var responseMessage = await _client.SendAsync(requestMessage, cancel).ConfigureAwait(false);
7478

7579
// convert the response
7680
var response = new Response
@@ -82,7 +86,8 @@ public async Task<IResponse> RequestAsync(IRequest request, CancellationToken ca
8286

8387
if (responseMessage.Content != null)
8488
{
85-
response.Content = await responseMessage.Content.ReadAsStreamAsync();
89+
response.Content = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);
90+
8691
foreach (var pair in responseMessage.Content.Headers)
8792
response.Headers[pair.Key] = String.Join(", ", pair.Value);
8893
}

0 commit comments

Comments
 (0)