Skip to content

Commit 1dead18

Browse files
kblokclaude
andcommitted
Fix: Correct content-length header for responses with multi-byte characters (#12702)
Move content-length computation outside the headers null check in CDP so it is always set when body data is present. Also use InvariantCulture for number formatting and align the BiDi implementation. Add tests for correct content-length with string and binary bodies containing emoji. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent c88daa0 commit 1dead18

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

lib/PuppeteerSharp.Tests/RequestInterceptionTests/RequestRespondTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,40 @@ await e.Request.RespondAsync(new ResponseData
148148
Assert.That(error.Message, Does.Match("Invalid header|Expected \"header\"|invalid argument"));
149149
}
150150

151+
[Test, PuppeteerTest("requestinterception.spec", "Request.respond", "should report correct content-length header with string")]
152+
public async Task ShouldReportCorrectContentLengthHeaderWithString()
153+
{
154+
await Page.SetRequestInterceptionAsync(true);
155+
Page.Request += async (_, e) =>
156+
{
157+
await e.Request.RespondAsync(new ResponseData
158+
{
159+
Status = HttpStatusCode.OK,
160+
Body = "Correct length \U0001F4CF?",
161+
});
162+
};
163+
var response = await Page.GoToAsync(TestConstants.EmptyPage);
164+
var headers = response.Headers;
165+
Assert.That(headers["content-length"], Is.EqualTo("20"));
166+
}
167+
168+
[Test, PuppeteerTest("requestinterception.spec", "Request.respond", "should report correct content-length header with binary body")]
169+
public async Task ShouldReportCorrectContentLengthHeaderWithBinaryBody()
170+
{
171+
await Page.SetRequestInterceptionAsync(true);
172+
Page.Request += async (_, e) =>
173+
{
174+
await e.Request.RespondAsync(new ResponseData
175+
{
176+
Status = HttpStatusCode.OK,
177+
BodyData = System.Text.Encoding.UTF8.GetBytes("Correct length \U0001F4CF?"),
178+
});
179+
};
180+
var response = await Page.GoToAsync(TestConstants.EmptyPage);
181+
var headers = response.Headers;
182+
Assert.That(headers["content-length"], Is.EqualTo("20"));
183+
}
184+
151185
[Test, PuppeteerTest("requestinterception.spec", "Request.respond", "should stringify intercepted request response headers")]
152186
public async Task ShouldStringifyInterceptedRequestResponseHeaders()
153187
{

lib/PuppeteerSharp/Bidi/BidiHttpRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ private async Task RespondInternalAsync(ResponseData response)
487487
}
488488

489489
// Add content-length if not present and we have body data
490-
if (!hasContentLength && response.BodyData != null)
490+
if (!hasContentLength && response.BodyData is { Length: > 0 })
491491
{
492492
responseHeaders.Add(new Header("content-length", response.BodyData.Length.ToString(CultureInfo.InvariantCulture)));
493493
}

lib/PuppeteerSharp/Cdp/CdpHttpRequest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,11 @@ private async Task RespondInternalAsync(ResponseData response)
355355
responseHeaders.Add(new Header { Name = keyValuePair.Key, Value = keyValuePair.Value.ToString() });
356356
}
357357
}
358+
}
358359

359-
if (!response.Headers.ContainsKey("content-length") && response.BodyData != null)
360-
{
361-
responseHeaders.Add(new Header { Name = "content-length", Value = response.BodyData.Length.ToString(CultureInfo.CurrentCulture) });
362-
}
360+
if (response.BodyData is { Length: > 0 } && (response.Headers == null || !response.Headers.ContainsKey("content-length")))
361+
{
362+
responseHeaders.Add(new Header { Name = "content-length", Value = response.BodyData.Length.ToString(CultureInfo.InvariantCulture) });
363363
}
364364

365365
if (response.ContentType != null)

0 commit comments

Comments
 (0)