public bool ReadHeader(out string headerName, out string headerValue) { int index = 0; while (index < _span.Length && ValidHeaderNameChar(_span[index])) { index++; } if (index > 0) { CheckResponseMsgFormat(index < _span.Length); CheckResponseMsgFormat(_span[index] == ':'); HeaderBufferSpan headerNameSpan = _span.Substring(0, index); if (!HttpKnownHeaderNames.TryGetHeaderName(_span.Buffer, _span.Length, out headerName)) { headerName = headerNameSpan.ToString(); } CheckResponseMsgFormat(headerName.Length > 0); index++; headerValue = _span.Substring(index).Trim().ToString(); return(true); } headerName = null; headerValue = null; return(false); }
public bool ReadStatusLine(HttpResponseMessage response) { if (!_span.StartsWithHttpPrefix()) { return(false); } int index = HttpPrefix.Length; int majorVersion = _span.ReadInt(ref index); CheckResponseMsgFormat(majorVersion != 0); CheckResponseMsgFormat(index < _span.Length); int minorVersion; if (_span[index] == '.') { index++; CheckResponseMsgFormat(index < _span.Length && _span[index] >= '0' && _span[index] <= '9'); minorVersion = _span.ReadInt(ref index); } else { minorVersion = 0; } CheckResponseMsgFormat(_span.SkipSpace(ref index)); // Parse status code. int statusCode = _span.ReadInt(ref index); CheckResponseMsgFormat(statusCode >= 100 && statusCode < 1000); bool foundSpace = _span.SkipSpace(ref index); CheckResponseMsgFormat(index <= _span.Length); CheckResponseMsgFormat(foundSpace || index == _span.Length); // Set the response HttpVersion. response.Version = (majorVersion == 1 && minorVersion == 1) ? HttpVersionInternal.Version11 : (majorVersion == 1 && minorVersion == 0) ? HttpVersionInternal.Version10 : (majorVersion == 2 && minorVersion == 0) ? HttpVersionInternal.Version20 : HttpVersionInternal.Unknown; response.StatusCode = (HttpStatusCode)statusCode; // Try to use a known reason phrase instead of allocating a new string. HeaderBufferSpan reasonPhraseSpan = _span.Substring(index); string knownReasonPhrase = HttpStatusDescription.Get(response.StatusCode); response.ReasonPhrase = reasonPhraseSpan.EqualsOrdinal(knownReasonPhrase) ? knownReasonPhrase : reasonPhraseSpan.ToString(); return(true); }
public bool ReadHeader(out string headerName, out string headerValue) { int index = 0; while (index < _span.Length && ValidHeaderNameChar(_span[index])) { index++; } if (index > 0) { // For compatability, skip past any whitespace before the colon, even though // the RFC suggests there shouldn't be any. int headerNameLength = index; while (index < _span.Length && IsWhiteSpaceLatin1(_span[index])) { index++; } CheckResponseMsgFormat(index < _span.Length); CheckResponseMsgFormat(_span[index] == ':'); HeaderBufferSpan headerNameSpan = _span.Substring(0, headerNameLength); if (!HttpKnownHeaderNames.TryGetHeaderName(_span.Buffer, _span.Length, out headerName)) { headerName = headerNameSpan.ToString(); } CheckResponseMsgFormat(headerName.Length > 0); index++; headerValue = _span.Substring(index).Trim().ToString(); return(true); } headerName = null; headerValue = null; return(false); }