Skip to content

Commit 91eee19

Browse files
zichanggcommit-bot@chromium.org
authored andcommitted
[http] loosen status code constraints and rephrase some http exceptions
1. Remove limitation of http response status code check. It used to be [100, 599]. Extends range to [0, 999] to allow users to use customized status code. 2. Rephrase some http exceptions to be more informative. 3. Possibly a bug in isHead setter, where it is only allowed to set to true. Bug: #38898 Change-Id: I77d8d66eb8333dec1f53742821860b802f74f680 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134763 Commit-Queue: Zichang Guo <zichangguo@google.com> Reviewed-by: Ben Konyi <bkonyi@google.com>
1 parent 86b1b9d commit 91eee19

2 files changed

Lines changed: 56 additions & 68 deletions

File tree

sdk/lib/_http/http_parser.dart

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,10 @@ class _HttpParser extends Stream<_HttpIncoming> {
406406
assert(!_parserCalled);
407407
_parserCalled = true;
408408
if (_state == _State.CLOSED) {
409-
throw new HttpException("Data on closed connection");
409+
throw HttpException("Data on closed connection");
410410
}
411411
if (_state == _State.FAILURE) {
412-
throw new HttpException("Data on failed connection");
412+
throw HttpException("Data on failed connection");
413413
}
414414
while (_buffer != null &&
415415
_index < _buffer.length &&
@@ -431,11 +431,11 @@ class _HttpParser extends Stream<_HttpIncoming> {
431431
} else {
432432
// Start parsing method.
433433
if (!_isTokenChar(byte)) {
434-
throw new HttpException("Invalid request method");
434+
throw HttpException("Invalid request method");
435435
}
436436
_method.add(byte);
437437
if (!_requestParser) {
438-
throw new HttpException("Invalid response line");
438+
throw HttpException("Invalid response line");
439439
}
440440
_state = _State.REQUEST_LINE_METHOD;
441441
}
@@ -452,7 +452,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
452452
// method anymore.
453453
_httpVersionIndex++;
454454
if (_requestParser) {
455-
throw new HttpException("Invalid request line");
455+
throw HttpException("Invalid request line");
456456
}
457457
_state = _State.RESPONSE_HTTP_VERSION;
458458
} else {
@@ -466,7 +466,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
466466
_method.add(byte);
467467
_httpVersion = _HttpVersion.UNDETERMINED;
468468
if (!_requestParser) {
469-
throw new HttpException("Invalid response line");
469+
throw HttpException("Invalid response line");
470470
}
471471
_state = _State.REQUEST_LINE_METHOD;
472472
}
@@ -495,7 +495,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
495495
// HTTP version parsed.
496496
_state = _State.RESPONSE_LINE_STATUS_CODE;
497497
} else {
498-
throw new HttpException("Invalid response line");
498+
throw HttpException(
499+
"Invalid response line, failed to parse HTTP version");
499500
}
500501
break;
501502

@@ -506,7 +507,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
506507
if (_Const.SEPARATOR_MAP[byte] ||
507508
byte == _CharCode.CR ||
508509
byte == _CharCode.LF) {
509-
throw new HttpException("Invalid request method");
510+
throw HttpException("Invalid request method");
510511
}
511512
_method.add(byte);
512513
}
@@ -515,13 +516,13 @@ class _HttpParser extends Stream<_HttpIncoming> {
515516
case _State.REQUEST_LINE_URI:
516517
if (byte == _CharCode.SP) {
517518
if (_uriOrReasonPhrase.length == 0) {
518-
throw new HttpException("Invalid request URI");
519+
throw HttpException("Invalid request, empty URI");
519520
}
520521
_state = _State.REQUEST_LINE_HTTP_VERSION;
521522
_httpVersionIndex = 0;
522523
} else {
523524
if (byte == _CharCode.CR || byte == _CharCode.LF) {
524-
throw new HttpException("Invalid request URI");
525+
throw HttpException("Invalid request, unexpected $byte in URI");
525526
}
526527
_uriOrReasonPhrase.add(byte);
527528
}
@@ -543,7 +544,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
543544
_persistentConnection = false;
544545
_httpVersionIndex++;
545546
} else {
546-
throw new HttpException("Invalid response line");
547+
throw HttpException("Invalid response, invalid HTTP version");
547548
}
548549
} else {
549550
if (byte == _CharCode.CR) {
@@ -571,8 +572,11 @@ class _HttpParser extends Stream<_HttpIncoming> {
571572
_state = _State.RESPONSE_LINE_ENDING;
572573
} else {
573574
_statusCodeLength++;
574-
if (byte < 0x30 || byte > 0x39 || _statusCodeLength > 3) {
575-
throw new HttpException("Invalid response status code");
575+
if (byte < 0x30 || byte > 0x39) {
576+
throw HttpException("Invalid response status code with $byte");
577+
} else if (_statusCodeLength > 3) {
578+
throw HttpException(
579+
"Invalid response, status code is over 3 digits");
576580
} else {
577581
_statusCode = _statusCode * 10 + byte - 0x30;
578582
}
@@ -584,7 +588,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
584588
_state = _State.RESPONSE_LINE_ENDING;
585589
} else {
586590
if (byte == _CharCode.CR || byte == _CharCode.LF) {
587-
throw new HttpException("Invalid response reason phrase");
591+
throw HttpException(
592+
"Invalid response, unexpected $byte in reason phrase");
588593
}
589594
_uriOrReasonPhrase.add(byte);
590595
}
@@ -593,15 +598,9 @@ class _HttpParser extends Stream<_HttpIncoming> {
593598
case _State.RESPONSE_LINE_ENDING:
594599
_expect(byte, _CharCode.LF);
595600
_messageType == _MessageType.RESPONSE;
596-
if (_statusCode < 100 || _statusCode > 599) {
597-
throw new HttpException("Invalid response status code");
598-
} else {
599-
// Check whether this response will never have a body.
600-
if (_statusCode <= 199 ||
601-
_statusCode == 204 ||
602-
_statusCode == 304) {
603-
_noMessageBody = true;
604-
}
601+
// Check whether this response will never have a body.
602+
if (_statusCode <= 199 || _statusCode == 204 || _statusCode == 304) {
603+
_noMessageBody = true;
605604
}
606605
_state = _State.HEADER_START;
607606
break;
@@ -625,7 +624,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
625624
_state = _State.HEADER_VALUE_START;
626625
} else {
627626
if (!_isTokenChar(byte)) {
628-
throw new HttpException("Invalid header field name");
627+
throw HttpException("Invalid header field name, with $byte");
629628
}
630629
_headerField.add(_toLowerCaseByte(byte));
631630
}
@@ -706,10 +705,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
706705
_expect(byte, _CharCode.LF);
707706
if (_headersEnd()) {
708707
return;
709-
} else {
710-
break;
711708
}
712-
return;
709+
break;
713710

714711
case _State.CHUNK_SIZE_STARTING_CR:
715712
_expect(byte, _CharCode.CR);
@@ -884,9 +881,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
884881
bool get upgrade => _connectionUpgrade && _state == _State.UPGRADED;
885882
bool get persistentConnection => _persistentConnection;
886883

887-
void set isHead(bool value) {
888-
if (value) _noMessageBody = true;
889-
}
884+
void set isHead(bool value) => _noMessageBody = value ?? false;
890885

891886
_HttpDetachedIncoming detachIncoming() {
892887
// Simulate detached by marking as upgraded.
@@ -978,8 +973,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
978973

979974
void _expect(int val1, int val2) {
980975
if (val1 != val2) {
981-
throw new HttpException(
982-
"Failed to parse HTTP, $val1 does not match $val2");
976+
throw HttpException("Failed to parse HTTP, $val1 does not match $val2");
983977
}
984978
}
985979

@@ -991,8 +985,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
991985
} else if (0x61 <= byte && byte <= 0x66) {
992986
return byte - 0x61 + 10; // a - f
993987
} else {
994-
throw new HttpException(
995-
"Failed to parse HTTP, $byte should be a Hex digit");
988+
throw HttpException(
989+
"Failed to parse HTTP, $byte is expected to be a Hex digit");
996990
}
997991
}
998992

sdk_nnbd/lib/_http/http_parser.dart

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,10 @@ class _HttpParser extends Stream<_HttpIncoming> {
400400
assert(!_parserCalled);
401401
_parserCalled = true;
402402
if (_state == _State.CLOSED) {
403-
throw new HttpException("Data on closed connection");
403+
throw HttpException("Data on closed connection");
404404
}
405405
if (_state == _State.FAILURE) {
406-
throw new HttpException("Data on failed connection");
406+
throw HttpException("Data on failed connection");
407407
}
408408
while (_buffer != null &&
409409
_index < _buffer!.length &&
@@ -427,11 +427,11 @@ class _HttpParser extends Stream<_HttpIncoming> {
427427
} else {
428428
// Start parsing method.
429429
if (!_isTokenChar(byte)) {
430-
throw new HttpException("Invalid request method");
430+
throw HttpException("Invalid request method");
431431
}
432432
_method.add(byte);
433433
if (!_requestParser) {
434-
throw new HttpException("Invalid response line");
434+
throw HttpException("Invalid response line");
435435
}
436436
_state = _State.REQUEST_LINE_METHOD;
437437
}
@@ -449,7 +449,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
449449
// method anymore.
450450
_httpVersionIndex = httpVersionIndex + 1;
451451
if (_requestParser) {
452-
throw new HttpException("Invalid request line");
452+
throw HttpException("Invalid request line");
453453
}
454454
_state = _State.RESPONSE_HTTP_VERSION;
455455
} else {
@@ -463,7 +463,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
463463
_method.add(byte);
464464
_httpVersion = _HttpVersion.UNDETERMINED;
465465
if (!_requestParser) {
466-
throw new HttpException("Invalid response line");
466+
throw HttpException("Invalid response line");
467467
}
468468
_state = _State.REQUEST_LINE_METHOD;
469469
}
@@ -493,7 +493,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
493493
// HTTP version parsed.
494494
_state = _State.RESPONSE_LINE_STATUS_CODE;
495495
} else {
496-
throw new HttpException("Invalid response line");
496+
throw HttpException(
497+
"Invalid response line, failed to parse HTTP version");
497498
}
498499
break;
499500

@@ -504,7 +505,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
504505
if (_Const.SEPARATOR_MAP[byte] ||
505506
byte == _CharCode.CR ||
506507
byte == _CharCode.LF) {
507-
throw new HttpException("Invalid request method");
508+
throw HttpException("Invalid request method");
508509
}
509510
_method.add(byte);
510511
}
@@ -513,13 +514,13 @@ class _HttpParser extends Stream<_HttpIncoming> {
513514
case _State.REQUEST_LINE_URI:
514515
if (byte == _CharCode.SP) {
515516
if (_uriOrReasonPhrase.length == 0) {
516-
throw new HttpException("Invalid request URI");
517+
throw HttpException("Invalid request, empty URI");
517518
}
518519
_state = _State.REQUEST_LINE_HTTP_VERSION;
519520
_httpVersionIndex = 0;
520521
} else {
521522
if (byte == _CharCode.CR || byte == _CharCode.LF) {
522-
throw new HttpException("Invalid request URI");
523+
throw HttpException("Invalid request, unexpected $byte in URI");
523524
}
524525
_uriOrReasonPhrase.add(byte);
525526
}
@@ -542,7 +543,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
542543
_persistentConnection = false;
543544
_httpVersionIndex = httpVersionIndex + 1;
544545
} else {
545-
throw new HttpException("Invalid response line");
546+
throw HttpException("Invalid response, invalid HTTP version");
546547
}
547548
} else {
548549
if (byte == _CharCode.CR) {
@@ -570,8 +571,11 @@ class _HttpParser extends Stream<_HttpIncoming> {
570571
_state = _State.RESPONSE_LINE_ENDING;
571572
} else {
572573
_statusCodeLength++;
573-
if (byte < 0x30 || byte > 0x39 || _statusCodeLength > 3) {
574-
throw new HttpException("Invalid response status code");
574+
if (byte < 0x30 || byte > 0x39) {
575+
throw HttpException("Invalid response status code with $byte");
576+
} else if (_statusCodeLength > 3) {
577+
throw HttpException(
578+
"Invalid response, status code is over 3 digits");
575579
} else {
576580
_statusCode = _statusCode * 10 + byte - 0x30;
577581
}
@@ -583,7 +587,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
583587
_state = _State.RESPONSE_LINE_ENDING;
584588
} else {
585589
if (byte == _CharCode.CR || byte == _CharCode.LF) {
586-
throw new HttpException("Invalid response reason phrase");
590+
throw HttpException(
591+
"Invalid response, unexpected $byte in reason phrase");
587592
}
588593
_uriOrReasonPhrase.add(byte);
589594
}
@@ -592,15 +597,9 @@ class _HttpParser extends Stream<_HttpIncoming> {
592597
case _State.RESPONSE_LINE_ENDING:
593598
_expect(byte, _CharCode.LF);
594599
_messageType == _MessageType.RESPONSE;
595-
if (_statusCode < 100 || _statusCode > 599) {
596-
throw new HttpException("Invalid response status code");
597-
} else {
598-
// Check whether this response will never have a body.
599-
if (_statusCode <= 199 ||
600-
_statusCode == 204 ||
601-
_statusCode == 304) {
602-
_noMessageBody = true;
603-
}
600+
// Check whether this response will never have a body.
601+
if (_statusCode <= 199 || _statusCode == 204 || _statusCode == 304) {
602+
_noMessageBody = true;
604603
}
605604
_state = _State.HEADER_START;
606605
break;
@@ -624,7 +623,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
624623
_state = _State.HEADER_VALUE_START;
625624
} else {
626625
if (!_isTokenChar(byte)) {
627-
throw new HttpException("Invalid header field name");
626+
throw HttpException("Invalid header field name, with $byte");
628627
}
629628
_headerField.add(_toLowerCaseByte(byte));
630629
}
@@ -706,10 +705,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
706705
_expect(byte, _CharCode.LF);
707706
if (_headersEnd()) {
708707
return;
709-
} else {
710-
break;
711708
}
712-
return;
709+
break;
713710

714711
case _State.CHUNK_SIZE_STARTING_CR:
715712
_expect(byte, _CharCode.CR);
@@ -886,9 +883,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
886883
bool get upgrade => _connectionUpgrade && _state == _State.UPGRADED;
887884
bool get persistentConnection => _persistentConnection;
888885

889-
void set isHead(bool value) {
890-
if (value) _noMessageBody = true;
891-
}
886+
void set isHead(bool value) => _noMessageBody = value ?? false;
892887

893888
_HttpDetachedIncoming detachIncoming() {
894889
// Simulate detached by marking as upgraded.
@@ -982,8 +977,7 @@ class _HttpParser extends Stream<_HttpIncoming> {
982977

983978
void _expect(int val1, int val2) {
984979
if (val1 != val2) {
985-
throw new HttpException(
986-
"Failed to parse HTTP, $val1 does not match $val2");
980+
throw HttpException("Failed to parse HTTP, $val1 does not match $val2");
987981
}
988982
}
989983

@@ -995,8 +989,8 @@ class _HttpParser extends Stream<_HttpIncoming> {
995989
} else if (0x61 <= byte && byte <= 0x66) {
996990
return byte - 0x61 + 10; // a - f
997991
} else {
998-
throw new HttpException(
999-
"Failed to parse HTTP, $byte should be a Hex digit");
992+
throw HttpException(
993+
"Failed to parse HTTP, $byte is expected to be a Hex digit");
1000994
}
1001995
}
1002996

0 commit comments

Comments
 (0)