Beispiel #1
0
 private static bool EncodeStatusHeader(int statusCode, DynamicHPackEncoder hpackEncoder, Span <byte> buffer, out int length)
 {
     if (H2StaticTable.TryGetStatusIndex(statusCode, out var index))
     {
         // Status codes which exist in the HTTP/2 StaticTable.
         return(HPackEncoder.EncodeIndexedHeaderField(index, buffer, out length));
     }
     else
     {
         const string name  = ":status";
         var          value = StatusCodes.ToStatusString(statusCode);
         return(hpackEncoder.EncodeHeader(buffer, H2StaticTable.Status200, HeaderEncodingHint.Index, name, value, valueEncoding: null, out length));
     }
 }
Beispiel #2
0
        private static bool EncodeStatusHeader(int statusCode, DynamicHPackEncoder hpackEncoder, Span <byte> buffer, out int length)
        {
            switch (statusCode)
            {
            case 200:
            case 204:
            case 206:
            case 304:
            case 400:
            case 404:
            case 500:
                // Status codes which exist in the HTTP/2 StaticTable.
                return(HPackEncoder.EncodeIndexedHeaderField(H2StaticTable.GetStatusIndex(statusCode), buffer, out length));

            default:
                const string name  = ":status";
                var          value = StatusCodes.ToStatusString(statusCode);
                return(hpackEncoder.EncodeHeader(buffer, H2StaticTable.Status200, HeaderEncodingHint.Index, name, value, out length));
            }
        }
Beispiel #3
0
    private static bool EncodeHeadersCore(DynamicHPackEncoder hpackEncoder, Http2HeadersEnumerator headersEnumerator, Span <byte> buffer, bool throwIfNoneEncoded, out int length)
    {
        var currentLength = 0;

        do
        {
            var staticTableId = headersEnumerator.HPackStaticTableId;
            var name          = headersEnumerator.Current.Key;
            var value         = headersEnumerator.Current.Value;
            var valueEncoding =
                ReferenceEquals(headersEnumerator.EncodingSelector, KestrelServerOptions.DefaultHeaderEncodingSelector)
                ? null : headersEnumerator.EncodingSelector(name);

            var hint = ResolveHeaderEncodingHint(staticTableId, name);

            if (!hpackEncoder.EncodeHeader(
                    buffer.Slice(currentLength),
                    staticTableId,
                    hint,
                    name,
                    value,
                    valueEncoding,
                    out var headerLength))
            {
                // If the header wasn't written, and no headers have been written, then the header is too large.
                // Throw an error to avoid an infinite loop of attempting to write large header.
                if (currentLength == 0 && throwIfNoneEncoded)
                {
                    throw new HPackEncodingException(SR.net_http_hpack_encode_failure);
                }

                length = currentLength;
                return(false);
            }

            currentLength += headerLength;
        }while (headersEnumerator.MoveNext());

        length = currentLength;
        return(true);
    }