public CurlResponseHeaderReader(IntPtr buffer, ulong size) { Debug.Assert(buffer != IntPtr.Zero); Debug.Assert(size <= int.MaxValue); _span = new HeaderBufferSpan(buffer, (int)size).Trim(); }
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 && _span[index] == '.'); index++; // Need minor version. CheckResponseMsgFormat(index < _span.Length && _span[index] >= '0' && _span[index] <= '9'); int minorVersion = _span.ReadInt(ref index); CheckResponseMsgFormat(_span.SkipSpace(ref index)); // Parse status code. int statusCode = _span.ReadInt(ref index); CheckResponseMsgFormat(statusCode >= 100 && statusCode < 600); 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) ? HttpVersion.Version11 : (majorVersion == 1 && minorVersion == 0) ? HttpVersion.Version10 : (majorVersion == 2 && minorVersion == 0) ? HttpVersion.Version20 : HttpVersion.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); }