|
7 | 7 | using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
|
8 | 8 | using Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTransport;
|
9 | 9 | using Microsoft.AspNetCore.Testing;
|
| 10 | +using Microsoft.AspNetCore.WebUtilities; |
10 | 11 | using Microsoft.Extensions.Logging;
|
11 | 12 | using Microsoft.Extensions.Primitives;
|
12 | 13 | using Moq;
|
13 |
| -using Xunit; |
14 | 14 | using BadHttpRequestException = Microsoft.AspNetCore.Http.BadHttpRequestException;
|
15 | 15 |
|
16 | 16 | namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests
|
@@ -323,6 +323,191 @@ private async Task TestBadRequest(string request, string expectedResponseStatusC
|
323 | 323 | Assert.Contains(expectedExceptionMessage, exceptionString);
|
324 | 324 | }
|
325 | 325 |
|
| 326 | + [Theory] |
| 327 | + [InlineData("\r")] |
| 328 | + [InlineData("\n")] |
| 329 | + [InlineData("\r\n")] |
| 330 | + [InlineData("\n\r")] |
| 331 | + [InlineData("\r\n\r\n")] |
| 332 | + [InlineData("\r\r\r\r\r")] |
| 333 | + public async Task ExtraLinesBetweenRequestsIgnored(string extraLines) |
| 334 | + { |
| 335 | + BadHttpRequestException loggedException = null; |
| 336 | + |
| 337 | + TestSink.MessageLogged += context => |
| 338 | + { |
| 339 | + if (context.EventId.Name == "ConnectionBadRequest" && context.Exception is BadHttpRequestException ex) |
| 340 | + { |
| 341 | + loggedException = ex; |
| 342 | + } |
| 343 | + }; |
| 344 | + |
| 345 | + // Set up a listener to catch the BadRequest event |
| 346 | + var diagListener = new DiagnosticListener("NotBadRequestTestsDiagListener"); |
| 347 | + var badRequestEventListener = new BadRequestEventListener(diagListener, (pair) => { }); |
| 348 | + |
| 349 | + await using (var server = new TestServer(context => context.Request.Body.DrainAsync(default), new TestServiceContext(LoggerFactory) { DiagnosticSource = diagListener })) |
| 350 | + { |
| 351 | + using (var connection = server.CreateConnection()) |
| 352 | + { |
| 353 | + await connection.SendAll( |
| 354 | + "POST / HTTP/1.1", |
| 355 | + "Host:", |
| 356 | + "Content-Length: 5", |
| 357 | + "", |
| 358 | + "funny", |
| 359 | + extraLines); |
| 360 | + |
| 361 | + await connection.Receive( |
| 362 | + "HTTP/1.1 200 OK", |
| 363 | + "Content-Length: 0", |
| 364 | + $"Date: {server.Context.DateHeaderValue}", |
| 365 | + "", |
| 366 | + ""); |
| 367 | + |
| 368 | + await connection.SendAll( |
| 369 | + "POST / HTTP/1.1", |
| 370 | + "Host:", |
| 371 | + "Content-Length: 5", |
| 372 | + "", |
| 373 | + "funny"); |
| 374 | + |
| 375 | + await connection.Receive( |
| 376 | + "HTTP/1.1 200 OK", |
| 377 | + "Content-Length: 0", |
| 378 | + $"Date: {server.Context.DateHeaderValue}", |
| 379 | + "", |
| 380 | + ""); |
| 381 | + |
| 382 | + connection.ShutdownSend(); |
| 383 | + |
| 384 | + await connection.ReceiveEnd(); |
| 385 | + } |
| 386 | + } |
| 387 | + |
| 388 | + Assert.Null(loggedException); |
| 389 | + // Verify DiagnosticSource event for bad request |
| 390 | + Assert.False(badRequestEventListener.EventFired); |
| 391 | + } |
| 392 | + |
| 393 | + [Fact] |
| 394 | + public async Task ExtraLinesIgnoredBetweenAdjacentRequests() |
| 395 | + { |
| 396 | + BadHttpRequestException loggedException = null; |
| 397 | + |
| 398 | + TestSink.MessageLogged += context => |
| 399 | + { |
| 400 | + if (context.EventId.Name == "ConnectionBadRequest" && context.Exception is BadHttpRequestException ex) |
| 401 | + { |
| 402 | + loggedException = ex; |
| 403 | + } |
| 404 | + }; |
| 405 | + |
| 406 | + // Set up a listener to catch the BadRequest event |
| 407 | + var diagListener = new DiagnosticListener("NotBadRequestTestsDiagListener"); |
| 408 | + var badRequestEventListener = new BadRequestEventListener(diagListener, (pair) => { }); |
| 409 | + |
| 410 | + await using (var server = new TestServer(context => context.Request.Body.DrainAsync(default), new TestServiceContext(LoggerFactory) { DiagnosticSource = diagListener })) |
| 411 | + { |
| 412 | + using (var connection = server.CreateConnection()) |
| 413 | + { |
| 414 | + await connection.SendAll( |
| 415 | + "POST / HTTP/1.1", |
| 416 | + "Host:", |
| 417 | + "Content-Length: 5", |
| 418 | + "", |
| 419 | + "funny", |
| 420 | + "", |
| 421 | + "", |
| 422 | + "", |
| 423 | + "POST /"); // Split the request line |
| 424 | + |
| 425 | + await connection.Receive( |
| 426 | + "HTTP/1.1 200 OK", |
| 427 | + "Content-Length: 0", |
| 428 | + $"Date: {server.Context.DateHeaderValue}", |
| 429 | + "", |
| 430 | + ""); |
| 431 | + |
| 432 | + await connection.SendAll( |
| 433 | + " HTTP/1.1", |
| 434 | + "Host:", |
| 435 | + "Content-Length: 5", |
| 436 | + "", |
| 437 | + "funny"); |
| 438 | + |
| 439 | + await connection.Receive( |
| 440 | + "HTTP/1.1 200 OK", |
| 441 | + "Content-Length: 0", |
| 442 | + $"Date: {server.Context.DateHeaderValue}", |
| 443 | + "", |
| 444 | + ""); |
| 445 | + |
| 446 | + connection.ShutdownSend(); |
| 447 | + |
| 448 | + await connection.ReceiveEnd(); |
| 449 | + } |
| 450 | + } |
| 451 | + |
| 452 | + Assert.Null(loggedException); |
| 453 | + // Verify DiagnosticSource event for bad request |
| 454 | + Assert.False(badRequestEventListener.EventFired); |
| 455 | + } |
| 456 | + |
| 457 | + [Theory] |
| 458 | + [InlineData("\r")] |
| 459 | + [InlineData("\n")] |
| 460 | + [InlineData("\r\n")] |
| 461 | + [InlineData("\n\r")] |
| 462 | + [InlineData("\n\n")] |
| 463 | + [InlineData("\r\n\r\n")] |
| 464 | + [InlineData("\r\r\r\r\r")] |
| 465 | + public async Task ExtraLinesAtEndOfConnectionIgnored(string extraLines) |
| 466 | + { |
| 467 | + BadHttpRequestException loggedException = null; |
| 468 | + |
| 469 | + TestSink.MessageLogged += context => |
| 470 | + { |
| 471 | + if (context.EventId.Name == "ConnectionBadRequest" && context.Exception is BadHttpRequestException ex) |
| 472 | + { |
| 473 | + loggedException = ex; |
| 474 | + } |
| 475 | + }; |
| 476 | + |
| 477 | + // Set up a listener to catch the BadRequest event |
| 478 | + var diagListener = new DiagnosticListener("NotBadRequestTestsDiagListener"); |
| 479 | + var badRequestEventListener = new BadRequestEventListener(diagListener, (pair) => { }); |
| 480 | + |
| 481 | + await using (var server = new TestServer(context => context.Request.Body.DrainAsync(default), new TestServiceContext(LoggerFactory) { DiagnosticSource = diagListener })) |
| 482 | + { |
| 483 | + using (var connection = server.CreateConnection()) |
| 484 | + { |
| 485 | + await connection.SendAll( |
| 486 | + "POST / HTTP/1.1", |
| 487 | + "Host:", |
| 488 | + "Content-Length: 5", |
| 489 | + "", |
| 490 | + "funny", |
| 491 | + extraLines); |
| 492 | + |
| 493 | + await connection.Receive( |
| 494 | + "HTTP/1.1 200 OK", |
| 495 | + "Content-Length: 0", |
| 496 | + $"Date: {server.Context.DateHeaderValue}", |
| 497 | + "", |
| 498 | + ""); |
| 499 | + |
| 500 | + connection.ShutdownSend(); |
| 501 | + |
| 502 | + await connection.ReceiveEnd(); |
| 503 | + } |
| 504 | + } |
| 505 | + |
| 506 | + Assert.Null(loggedException); |
| 507 | + // Verify DiagnosticSource event for bad request |
| 508 | + Assert.False(badRequestEventListener.EventFired); |
| 509 | + } |
| 510 | + |
326 | 511 | private async Task ReceiveBadRequestResponse(InMemoryConnection connection, string expectedResponseStatusCode, string expectedDateHeaderValue, string expectedAllowHeader = null)
|
327 | 512 | {
|
328 | 513 | var lines = new[]
|
|
0 commit comments