Skip to content

Commit

Permalink
Middle response headers. Thanks to Azor Ahai.
Browse files Browse the repository at this point in the history
New property: EnableMiddleHeaders for HttpRequest.
New property: MiddleHeaders for HttpResponse.
  • Loading branch information
grandsilence committed Aug 21, 2018
1 parent f00abc7 commit c6b6782
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
8 changes: 7 additions & 1 deletion Leaf.xNet/~Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ public event EventHandler<DownloadProgressChangedEventArgs> DownloadProgressChan
/// </summary>
public bool AllowEmptyHeaderValues { get; set; }


/// <summary>
/// Включить отслеживание заголовков в промежуточных запросах (редиректы) и сохранять их в <see cref="HttpResponse.MiddleHeaders"/>.
/// </summary>
public bool EnableMiddleHeaders { get; set; }

#region Поведение

/// <summary>
Expand Down Expand Up @@ -1750,7 +1756,7 @@ private void ReceiveResponseHeaders(HttpMethod method)
_canReportBytesReceived = false;

_bytesReceived = 0;
_totalBytesReceived = Response.LoadResponse(method);
_totalBytesReceived = Response.LoadResponse(method, EnableMiddleHeaders);

_canReportBytesReceived = true;
}
Expand Down
13 changes: 12 additions & 1 deletion Leaf.xNet/~Http/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ public override void Write(byte[] buffer, int offset, int count)
private readonly Dictionary<string, string> _headers =
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

// Lazy redirect headers
public Dictionary<string, string> MiddleHeaders => _middleHeaders ??
(_middleHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

private Dictionary<string, string> _middleHeaders;


private string _loadedMessageBody;
//private MemoryStream _loadedMessageBody;
Expand Down Expand Up @@ -775,7 +781,7 @@ public Dictionary<string, string>.Enumerator EnumerateHeaders()


// Загружает ответ и возвращает размер ответа в байтах.
internal long LoadResponse(HttpMethod method)
internal long LoadResponse(HttpMethod method, bool includeRedirectHeaders)
{
Method = method;
Address = _request.Address;
Expand All @@ -785,6 +791,11 @@ internal long LoadResponse(HttpMethod method)
KeepAliveTimeout = null;
MaximumKeepAliveRequests = null;

if (includeRedirectHeaders && _headers.Count > 0)
{
foreach (var key in _headers.Keys)
MiddleHeaders[key] = _headers[key];
}
_headers.Clear();

if (!_request.DontTrackCookies)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ dotnet add package Leaf.xNet
```
paket add Leaf.xNet
```

# Features
## Middle response headers (when redirected)
```csharp
var req = new HttpRequest();
// REQUIRED!
req.EnableMiddleHeaders = true;

// This requrest has a lot of redirects
var resp = req.Get("https://account.sonyentertainmentnetwork.com/");
var md = resp.MiddleHeaders;
```

0 comments on commit c6b6782

Please sign in to comment.