in lib/src/http_parser.dart [354:758]
void _doParse() {
assert(!_parserCalled);
_parserCalled = true;
if (_state == _State.CLOSED) {
throw HttpException("Data on closed connection");
}
if (_state == _State.FAILURE) {
throw HttpException("Data on failed connection");
}
while (_buffer != null &&
_index < _buffer.length &&
_state != _State.FAILURE &&
_state != _State.UPGRADED) {
// Depending on _incoming, we either break on _bodyPaused or _paused.
if ((_incoming != null && _bodyPaused) ||
(_incoming == null && _paused)) {
_parserCalled = false;
return;
}
int byte = _buffer[_index++];
switch (_state) {
case _State.START:
if (byte == Const.HTTP[0]) {
// Start parsing method or HTTP version.
_httpVersionIndex = 1;
_state = _State.METHOD_OR_RESPONSE_HTTP_VERSION;
} else {
// Start parsing method.
if (!CharCode.isTokenChar(byte)) {
throw HttpException("Invalid request method");
}
_method.add(byte);
if (!_requestParser) {
throw HttpException("Invalid response line");
}
_state = _State.REQUEST_LINE_METHOD;
}
break;
case _State.METHOD_OR_RESPONSE_HTTP_VERSION:
if (_httpVersionIndex < Const.HTTP.length &&
byte == Const.HTTP[_httpVersionIndex]) {
// Continue parsing HTTP version.
_httpVersionIndex++;
} else if (_httpVersionIndex == Const.HTTP.length &&
byte == CharCode.SLASH) {
// HTTP/ parsed. As method is a token this cannot be a
// method anymore.
_httpVersionIndex++;
if (_requestParser) {
throw HttpException("Invalid request line");
}
_state = _State.RESPONSE_HTTP_VERSION;
} else {
// Did not parse HTTP version. Expect method instead.
for (int i = 0; i < _httpVersionIndex; i++) {
_method.add(Const.HTTP[i]);
}
if (byte == CharCode.SP) {
_state = _State.REQUEST_LINE_URI;
} else {
_method.add(byte);
_httpVersion = _HttpVersion.UNDETERMINED;
if (!_requestParser) {
throw HttpException("Invalid response line");
}
_state = _State.REQUEST_LINE_METHOD;
}
}
break;
case _State.RESPONSE_HTTP_VERSION:
if (_httpVersionIndex < Const.HTTP1DOT.length) {
// Continue parsing HTTP version.
_expect(byte, Const.HTTP1DOT[_httpVersionIndex]);
_httpVersionIndex++;
} else if (_httpVersionIndex == Const.HTTP1DOT.length &&
byte == CharCode.ONE) {
// HTTP/1.1 parsed.
_httpVersion = _HttpVersion.HTTP11;
_persistentConnection = true;
_httpVersionIndex++;
} else if (_httpVersionIndex == Const.HTTP1DOT.length &&
byte == CharCode.ZERO) {
// HTTP/1.0 parsed.
_httpVersion = _HttpVersion.HTTP10;
_persistentConnection = false;
_httpVersionIndex++;
} else if (_httpVersionIndex == Const.HTTP1DOT.length + 1) {
_expect(byte, CharCode.SP);
// HTTP version parsed.
_state = _State.RESPONSE_LINE_STATUS_CODE;
} else {
throw HttpException("Invalid response line");
}
break;
case _State.REQUEST_LINE_METHOD:
if (byte == CharCode.SP) {
_state = _State.REQUEST_LINE_URI;
} else {
if (Const.SEPARATOR_MAP[byte] ||
byte == CharCode.CR ||
byte == CharCode.LF) {
throw HttpException("Invalid request method");
}
_method.add(byte);
}
break;
case _State.REQUEST_LINE_URI:
if (byte == CharCode.SP) {
if (_uri_or_reason_phrase.isEmpty) {
throw HttpException("Invalid request URI");
}
_state = _State.REQUEST_LINE_HTTP_VERSION;
_httpVersionIndex = 0;
} else {
if (byte == CharCode.CR || byte == CharCode.LF) {
throw HttpException("Invalid request URI");
}
_uri_or_reason_phrase.add(byte);
}
break;
case _State.REQUEST_LINE_HTTP_VERSION:
if (_httpVersionIndex < Const.HTTP1DOT.length) {
_expect(byte, Const.HTTP11[_httpVersionIndex]);
_httpVersionIndex++;
} else if (_httpVersionIndex == Const.HTTP1DOT.length) {
if (byte == CharCode.ONE) {
// HTTP/1.1 parsed.
_httpVersion = _HttpVersion.HTTP11;
_persistentConnection = true;
_httpVersionIndex++;
} else if (byte == CharCode.ZERO) {
// HTTP/1.0 parsed.
_httpVersion = _HttpVersion.HTTP10;
_persistentConnection = false;
_httpVersionIndex++;
} else {
throw HttpException("Invalid response line");
}
} else {
if (byte == CharCode.CR) {
_state = _State.REQUEST_LINE_ENDING;
} else {
_expect(byte, CharCode.LF);
_messageType = _MessageType.REQUEST;
_state = _State.HEADER_START;
}
}
break;
case _State.REQUEST_LINE_ENDING:
_expect(byte, CharCode.LF);
_messageType = _MessageType.REQUEST;
_state = _State.HEADER_START;
break;
case _State.RESPONSE_LINE_STATUS_CODE:
if (byte == CharCode.SP) {
_state = _State.RESPONSE_LINE_REASON_PHRASE;
} else if (byte == CharCode.CR) {
// Some HTTP servers does not follow the spec. and send
// \r\n right after the status code.
_state = _State.RESPONSE_LINE_ENDING;
} else {
_statusCodeLength++;
if ((byte < 0x30 && 0x39 < byte) || _statusCodeLength > 3) {
throw HttpException("Invalid response status code");
} else {
_statusCode = _statusCode * 10 + byte - 0x30;
}
}
break;
case _State.RESPONSE_LINE_REASON_PHRASE:
if (byte == CharCode.CR) {
_state = _State.RESPONSE_LINE_ENDING;
} else {
if (byte == CharCode.CR || byte == CharCode.LF) {
throw HttpException("Invalid response reason phrase");
}
_uri_or_reason_phrase.add(byte);
}
break;
case _State.RESPONSE_LINE_ENDING:
_expect(byte, CharCode.LF);
_messageType = _MessageType.RESPONSE;
if (_statusCode < 100 || _statusCode > 599) {
throw HttpException("Invalid response status code");
} else {
// Check whether this response will never have a body.
if (_statusCode <= 199 ||
_statusCode == 204 ||
_statusCode == 304) {
_noMessageBody = true;
}
}
_state = _State.HEADER_START;
break;
case _State.HEADER_START:
_headers = HttpHeadersImpl(version);
if (byte == CharCode.CR) {
_state = _State.HEADER_ENDING;
} else if (byte == CharCode.LF) {
_state = _State.HEADER_ENDING;
_index--; // Make the new state see the LF again.
} else {
// Start of new header field.
_headerField.add(_toLowerCaseByte(byte));
_state = _State.HEADER_FIELD;
}
break;
case _State.HEADER_FIELD:
if (byte == CharCode.COLON) {
_state = _State.HEADER_VALUE_START;
} else {
if (!CharCode.isTokenChar(byte)) {
throw HttpException("Invalid header field name");
}
_headerField.add(_toLowerCaseByte(byte));
}
break;
case _State.HEADER_VALUE_START:
if (byte == CharCode.CR) {
_state = _State.HEADER_VALUE_FOLDING_OR_ENDING;
} else if (byte == CharCode.LF) {
_state = _State.HEADER_VALUE_FOLD_OR_END;
} else if (byte != CharCode.SP && byte != CharCode.HT) {
// Start of new header value.
_headerValue.add(byte);
_state = _State.HEADER_VALUE;
}
break;
case _State.HEADER_VALUE:
if (byte == CharCode.CR) {
_state = _State.HEADER_VALUE_FOLDING_OR_ENDING;
} else if (byte == CharCode.LF) {
_state = _State.HEADER_VALUE_FOLD_OR_END;
} else {
_headerValue.add(byte);
}
break;
case _State.HEADER_VALUE_FOLDING_OR_ENDING:
_expect(byte, CharCode.LF);
_state = _State.HEADER_VALUE_FOLD_OR_END;
break;
case _State.HEADER_VALUE_FOLD_OR_END:
if (byte == CharCode.SP || byte == CharCode.HT) {
_state = _State.HEADER_VALUE_START;
} else {
String headerField = String.fromCharCodes(_headerField);
String headerValue = String.fromCharCodes(_headerValue);
if (headerField == "transfer-encoding" &&
_caseInsensitiveCompare("chunked".codeUnits, _headerValue)) {
_chunked = true;
}
if (headerField == "connection") {
List<String> tokens = _tokenizeFieldValue(headerValue);
final bool isResponse = _messageType == _MessageType.RESPONSE;
final bool isUpgradeCode =
(_statusCode == HttpStatus.UPGRADE_REQUIRED) ||
(_statusCode == HttpStatus.SWITCHING_PROTOCOLS);
for (int i = 0; i < tokens.length; i++) {
final bool isUpgrade = _caseInsensitiveCompare(
"upgrade".codeUnits, tokens[i].codeUnits);
if ((isUpgrade && !isResponse) ||
(isUpgrade && isResponse && isUpgradeCode)) {
_connectionUpgrade = true;
}
_headers.add(headerField, tokens[i]);
}
} else {
_headers.add(headerField, headerValue);
}
_headerField.clear();
_headerValue.clear();
if (byte == CharCode.CR) {
_state = _State.HEADER_ENDING;
} else if (byte == CharCode.LF) {
_state = _State.HEADER_ENDING;
_index--; // Make the new state see the LF again.
} else {
// Start of new header field.
_headerField.add(_toLowerCaseByte(byte));
_state = _State.HEADER_FIELD;
}
}
break;
case _State.HEADER_ENDING:
_expect(byte, CharCode.LF);
if (_headersEnd()) {
return;
} else {
break;
}
return;
case _State.CHUNK_SIZE_STARTING_CR:
_expect(byte, CharCode.CR);
_state = _State.CHUNK_SIZE_STARTING_LF;
break;
case _State.CHUNK_SIZE_STARTING_LF:
_expect(byte, CharCode.LF);
_state = _State.CHUNK_SIZE;
break;
case _State.CHUNK_SIZE:
if (byte == CharCode.CR) {
_state = _State.CHUNK_SIZE_ENDING;
} else if (byte == CharCode.SEMI_COLON) {
_state = _State.CHUNK_SIZE_EXTENSION;
} else {
int value = _expectHexDigit(byte);
_remainingContent = _remainingContent * 16 + value;
}
break;
case _State.CHUNK_SIZE_EXTENSION:
if (byte == CharCode.CR) {
_state = _State.CHUNK_SIZE_ENDING;
}
break;
case _State.CHUNK_SIZE_ENDING:
_expect(byte, CharCode.LF);
if (_remainingContent > 0) {
_state = _State.BODY;
} else {
_state = _State.CHUNKED_BODY_DONE_CR;
}
break;
case _State.CHUNKED_BODY_DONE_CR:
_expect(byte, CharCode.CR);
_state = _State.CHUNKED_BODY_DONE_LF;
break;
case _State.CHUNKED_BODY_DONE_LF:
_expect(byte, CharCode.LF);
_reset();
_closeIncoming();
break;
case _State.BODY:
// The body is not handled one byte at a time but in blocks.
_index--;
int dataAvailable = _buffer.length - _index;
if (_remainingContent >= 0 && dataAvailable > _remainingContent) {
dataAvailable = _remainingContent;
}
// Always present the data as a view. This way we can handle all
// cases like this, and the user will not experience different data
// typed (which could lead to polymorphic user code).
Uint8List data = Uint8List.view(
_buffer.buffer, _buffer.offsetInBytes + _index, dataAvailable);
_bodyController.add(data);
if (_remainingContent != -1) {
_remainingContent -= data.length;
}
_index += data.length;
if (_remainingContent == 0) {
if (!_chunked) {
_reset();
_closeIncoming();
} else {
_state = _State.CHUNK_SIZE_STARTING_CR;
}
}
break;
case _State.FAILURE:
// Should be unreachable.
assert(false);
break;
default:
// Should be unreachable.
assert(false);
break;
}
}
_parserCalled = false;
if (_buffer != null && _index == _buffer.length) {
// If all data is parsed release the buffer and resume receiving
// data.
_releaseBuffer();
if (_state != _State.UPGRADED && _state != _State.FAILURE) {
_socketSubscription.resume();
}
}
}