-
-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#553 drop GetHeaderValue, move some code around
- Loading branch information
Showing
6 changed files
with
135 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using Flurl.Http.Content; | ||
using Flurl.Util; | ||
|
||
namespace Flurl.Http | ||
{ | ||
/// <summary> | ||
/// Extension methods off HttpRequestMessage and HttpResponseMessage. | ||
/// </summary> | ||
public static class HttpMessageExtensions | ||
{ | ||
/// <summary> | ||
/// Set a header on this HttpRequestMessage (default), or its Content property if it's a known content-level header. | ||
/// No validation. Overwrites any existing value(s) for the header. | ||
/// </summary> | ||
/// <param name="request">The HttpRequestMessage.</param> | ||
/// <param name="name">The header name.</param> | ||
/// <param name="value">The header value.</param> | ||
/// <param name="createContentIfNecessary">If it's a content-level header and there is no content, this determines whether to create an empty HttpContent or just ignore the header.</param> | ||
public static void SetHeader(this HttpRequestMessage request, string name, object value, bool createContentIfNecessary = true) { | ||
new HttpMessage(request).SetHeader(name, value, createContentIfNecessary); | ||
} | ||
|
||
/// <summary> | ||
/// Set a header on this HttpResponseMessage (default), or its Content property if it's a known content-level header. | ||
/// No validation. Overwrites any existing value(s) for the header. | ||
/// </summary> | ||
/// <param name="response">The HttpResponseMessage.</param> | ||
/// <param name="name">The header name.</param> | ||
/// <param name="value">The header value.</param> | ||
/// <param name="createContentIfNecessary">If it's a content-level header and there is no content, this determines whether to create an empty HttpContent or just ignore the header.</param> | ||
public static void SetHeader(this HttpResponseMessage response, string name, object value, bool createContentIfNecessary = true) { | ||
new HttpMessage(response).SetHeader(name, value, createContentIfNecessary); | ||
} | ||
|
||
/// <summary> | ||
/// Associate a FlurlCall object with this request | ||
/// </summary> | ||
internal static void SetHttpCall(this HttpRequestMessage request, FlurlCall call) { | ||
if (request?.Properties != null) | ||
request.Properties["FlurlHttpCall"] = call; | ||
} | ||
|
||
/// <summary> | ||
/// Get the FlurlCall associated with this request, if any. | ||
/// </summary> | ||
internal static FlurlCall GetHttpCall(this HttpRequestMessage request) { | ||
if (request?.Properties != null && request.Properties.TryGetValue("FlurlHttpCall", out var obj) && obj is FlurlCall call) | ||
return call; | ||
return null; | ||
} | ||
|
||
private static void SetHeader(this HttpMessage msg, string name, object value, bool createContentIfNecessary) { | ||
switch (name.ToLower()) { | ||
// https://docs.microsoft.com/en-us/dotnet/api/system.net.http.headers.httpcontentheaders | ||
case "allow": | ||
case "content-disposition": | ||
case "content-encoding": | ||
case "content-language": | ||
case "content-length": | ||
case "content-location": | ||
case "content-md5": | ||
case "content-range": | ||
case "content-type": | ||
case "expires": | ||
case "last-modified": | ||
// it's a content-level header | ||
if (msg.Content == null && (!createContentIfNecessary || value == null)) | ||
break; | ||
|
||
if (msg.Content == null) { | ||
msg.Content = new CapturedStringContent(""); | ||
msg.Content.Headers.Clear(); | ||
} | ||
else { | ||
msg.Content.Headers.Remove(name); | ||
} | ||
|
||
if (value != null) | ||
msg.Content.Headers.TryAddWithoutValidation(name, new[] { value.ToInvariantString() }); | ||
break; | ||
default: | ||
// it's a request/response-level header | ||
if (!name.Equals("Set-Cookie", StringComparison.OrdinalIgnoreCase)) // multiple set-cookie headers are allowed | ||
msg.Headers.Remove(name); | ||
if (value != null) | ||
msg.Headers.TryAddWithoutValidation(name, new[] { value.ToInvariantString() }); | ||
break; | ||
} | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Wrapper class for treating HttpRequestMessage and HttpResponseMessage uniformly. (Unfortunately they don't have a common interface.) | ||
/// </summary> | ||
private class HttpMessage | ||
{ | ||
private readonly HttpRequestMessage _request; | ||
private readonly HttpResponseMessage _response; | ||
|
||
public HttpHeaders Headers => _request?.Headers as HttpHeaders ?? _response?.Headers; | ||
|
||
public HttpContent Content { | ||
get => _request?.Content ?? _response?.Content; | ||
set { | ||
if (_request != null) _request.Content = value; | ||
else _response.Content = value; | ||
} | ||
} | ||
|
||
public HttpMessage(HttpRequestMessage request) { | ||
_request = request; | ||
} | ||
|
||
public HttpMessage(HttpResponseMessage response) { | ||
_response = response; | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters