99
1010namespace System . Net . Http . QPack
1111{
12- internal sealed class QPackEncoder
12+ internal static class QPackEncoder
1313 {
14- private IEnumerator < KeyValuePair < string , string > > ? _enumerator ;
15-
1614 // https://tools.ietf.org/html/draft-ietf-quic-qpack-11#section-4.5.2
1715 // 0 1 2 3 4 5 6 7
1816 // +---+---+---+---+---+---+---+---+
@@ -403,93 +401,5 @@ private static bool EncodeHeaderBlockPrefix(Span<byte> destination, out int byte
403401
404402 return true ;
405403 }
406-
407- public bool BeginEncode ( IEnumerable < KeyValuePair < string , string > > headers , Span < byte > buffer , out int length )
408- {
409- _enumerator = headers . GetEnumerator ( ) ;
410-
411- bool hasValue = _enumerator . MoveNext ( ) ;
412- Debug . Assert ( hasValue == true ) ;
413-
414- buffer [ 0 ] = 0 ;
415- buffer [ 1 ] = 0 ;
416-
417- bool doneEncode = Encode ( buffer . Slice ( 2 ) , out length ) ;
418-
419- // Add two for the first two bytes.
420- length += 2 ;
421- return doneEncode ;
422- }
423-
424- public bool BeginEncode ( int statusCode , IEnumerable < KeyValuePair < string , string > > headers , Span < byte > buffer , out int length )
425- {
426- _enumerator = headers . GetEnumerator ( ) ;
427-
428- bool hasValue = _enumerator . MoveNext ( ) ;
429- Debug . Assert ( hasValue == true ) ;
430-
431- // https://quicwg.org/base-drafts/draft-ietf-quic-qpack.html#header-prefix
432- buffer [ 0 ] = 0 ;
433- buffer [ 1 ] = 0 ;
434-
435- int statusCodeLength = EncodeStatusCode ( statusCode , buffer . Slice ( 2 ) ) ;
436- bool done = Encode ( buffer . Slice ( statusCodeLength + 2 ) , throwIfNoneEncoded : false , out int headersLength ) ;
437- length = statusCodeLength + headersLength + 2 ;
438-
439- return done ;
440- }
441-
442- public bool Encode ( Span < byte > buffer , out int length )
443- {
444- return Encode ( buffer , throwIfNoneEncoded : true , out length ) ;
445- }
446-
447- private bool Encode ( Span < byte > buffer , bool throwIfNoneEncoded , out int length )
448- {
449- length = 0 ;
450-
451- do
452- {
453- if ( ! EncodeLiteralHeaderFieldWithoutNameReference ( _enumerator ! . Current . Key , _enumerator . Current . Value , buffer . Slice ( length ) , out int headerLength ) )
454- {
455- if ( length == 0 && throwIfNoneEncoded )
456- {
457- throw new QPackEncodingException ( "TODO sync with corefx" /* CoreStrings.HPackErrorNotEnoughBuffer */ ) ;
458- }
459- return false ;
460- }
461-
462- length += headerLength ;
463- } while ( _enumerator . MoveNext ( ) ) ;
464-
465- return true ;
466- }
467-
468- private int EncodeStatusCode ( int statusCode , Span < byte > buffer )
469- {
470- switch ( statusCode )
471- {
472- case 200 :
473- case 204 :
474- case 206 :
475- case 304 :
476- case 400 :
477- case 404 :
478- case 500 :
479- EncodeStaticIndexedHeaderField ( H3StaticTable . StatusIndex [ statusCode ] , buffer , out var bytesWritten ) ;
480- return bytesWritten ;
481- default :
482- // https://tools.ietf.org/html/draft-ietf-quic-qpack-21#section-4.5.4
483- // Index is 63 - :status
484- buffer [ 0 ] = 0b01011111 ;
485- buffer [ 1 ] = 0b00110000 ;
486-
487- ReadOnlySpan < byte > statusBytes = StatusCodes . ToStatusBytes ( statusCode ) ;
488- buffer [ 2 ] = ( byte ) statusBytes . Length ;
489- statusBytes . CopyTo ( buffer . Slice ( 3 ) ) ;
490-
491- return 3 + statusBytes . Length ;
492- }
493- }
494404 }
495405}
0 commit comments