Skip to content

Commit 07d814f

Browse files
committed
fix
1 parent 161cd24 commit 07d814f

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

lib/PuppeteerSharp/Bidi/BidiHttpRequest.cs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ private BidiHttpRequest(Request request, BidiFrame frame, BidiHttpRequest redire
5353
Requests.AddItem(request, this);
5454
}
5555

56+
/// <summary>
57+
/// Gets the merged headers including extra HTTP headers and user agent headers.
58+
/// </summary>
59+
public override Dictionary<string, string> Headers
60+
{
61+
get
62+
{
63+
// Callers should not be allowed to mutate internal structure.
64+
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
65+
foreach (var header in _request.Headers)
66+
{
67+
headers[header.Name.ToLowerInvariant()] = header.Value.Value;
68+
}
69+
70+
foreach (var kvp in ExtraHttpHeaders)
71+
{
72+
headers[kvp.Key.ToLowerInvariant()] = kvp.Value;
73+
}
74+
75+
foreach (var kvp in UserAgentHeaders)
76+
{
77+
headers[kvp.Key.ToLowerInvariant()] = kvp.Value;
78+
}
79+
80+
return headers;
81+
}
82+
}
83+
5684
// TODO: I don't like having this static field at all. This will cause memory leaks for sure.
5785
// We need to move this to a place where we can control its lifecycle.
5886
internal static AsyncDictionaryHelper<Request, BidiHttpRequest> Requests { get; } = new("Request {0} not found");
@@ -202,17 +230,7 @@ public override async Task AbortAsync(RequestAbortErrorCode errorCode = RequestA
202230

203231
internal static BidiHttpRequest From(Request bidiRequest, BidiFrame frame, BidiHttpRequest redirect = null)
204232
{
205-
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
206-
foreach (var header in bidiRequest.Headers)
207-
{
208-
headers[header.Name] = header.Value.Value;
209-
}
210-
211-
var request = new BidiHttpRequest(bidiRequest, frame, redirect)
212-
{
213-
Url = bidiRequest.Url,
214-
Headers = headers,
215-
};
233+
var request = new BidiHttpRequest(bidiRequest, frame, redirect) { Url = bidiRequest.Url, };
216234
request.Initialize();
217235
return request;
218236
}
@@ -279,24 +297,8 @@ private static string GetReasonPhrase(int statusCode)
279297

280298
private Dictionary<string, string> GetMergedHeaders(Dictionary<string, string> overrideHeaders)
281299
{
282-
// Start with the original request headers
283-
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
284-
foreach (var header in _request.Headers)
285-
{
286-
headers[header.Name.ToLowerInvariant()] = header.Value.Value;
287-
}
288-
289-
// Add extra HTTP headers from the page
290-
foreach (var kvp in ExtraHttpHeaders)
291-
{
292-
headers[kvp.Key.ToLowerInvariant()] = kvp.Value;
293-
}
294-
295-
// Add user agent headers from the page
296-
foreach (var kvp in UserAgentHeaders)
297-
{
298-
headers[kvp.Key.ToLowerInvariant()] = kvp.Value;
299-
}
300+
// Start with the merged headers (original + extra + user agent)
301+
var headers = new Dictionary<string, string>(Headers, StringComparer.OrdinalIgnoreCase);
300302

301303
// Apply any override headers from the payload
302304
if (overrideHeaders != null)
@@ -487,11 +489,11 @@ private void Initialize()
487489
_interception.Handlers.Add(async () =>
488490
{
489491
await ContinueAsync(
490-
new Payload()
491-
{
492-
Headers = Headers,
493-
},
494-
0).ConfigureAwait(false);
492+
new Payload
493+
{
494+
Headers = Headers,
495+
},
496+
0).ConfigureAwait(false);
495497
});
496498
}
497499
}

lib/PuppeteerSharp/Request.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class Request<TResponse>
4141
public string PostData { get; internal init; }
4242

4343
/// <inheritdoc/>
44-
public Dictionary<string, string> Headers { get; internal init; }
44+
public virtual Dictionary<string, string> Headers { get; protected set; }
4545

4646
/// <inheritdoc/>
4747
public string Url { get; internal init; }

0 commit comments

Comments
 (0)