3131
3232package io .grpc .transport .netty ;
3333
34+ import static io .grpc .transport .HttpUtil .CONTENT_TYPE_KEY ;
35+ import static io .grpc .transport .HttpUtil .USER_AGENT_KEY ;
3436import static io .netty .util .CharsetUtil .UTF_8 ;
3537
3638import com .google .common .base .Preconditions ;
4042import io .grpc .SharedResourceHolder .Resource ;
4143import io .grpc .transport .HttpUtil ;
4244import io .grpc .transport .TransportFrameUtil ;
43- import io .netty .buffer .ByteBuf ;
44- import io .netty .buffer .ByteBufAllocator ;
45+
4546import io .netty .channel .EventLoopGroup ;
4647import io .netty .channel .nio .NioEventLoopGroup ;
4748import io .netty .handler .codec .http2 .DefaultHttp2Headers ;
5051import io .netty .util .concurrent .Future ;
5152import io .netty .util .concurrent .GenericFutureListener ;
5253
53- import java .nio .ByteBuffer ;
5454import java .util .Map ;
5555import java .util .concurrent .ExecutorService ;
5656import java .util .concurrent .Executors ;
@@ -66,28 +66,20 @@ class Utils {
6666 public static final ByteString HTTP_METHOD = new ByteString (HttpUtil .HTTP_METHOD .getBytes (UTF_8 ));
6767 public static final ByteString HTTPS = new ByteString ("https" .getBytes (UTF_8 ));
6868 public static final ByteString HTTP = new ByteString ("http" .getBytes (UTF_8 ));
69- public static final ByteString CONTENT_TYPE_HEADER = new ByteString (HttpUtil . CONTENT_TYPE .name ()
69+ public static final ByteString CONTENT_TYPE_HEADER = new ByteString (CONTENT_TYPE_KEY .name ()
7070 .getBytes (UTF_8 ));
7171 public static final ByteString CONTENT_TYPE_GRPC = new ByteString (
7272 HttpUtil .CONTENT_TYPE_GRPC .getBytes (UTF_8 ));
73- public static final ByteString TE_HEADER = new ByteString (HttpUtil . TE . name () .getBytes (UTF_8 ));
73+ public static final ByteString TE_HEADER = new ByteString ("te" .getBytes (UTF_8 ));
7474 public static final ByteString TE_TRAILERS = new ByteString (HttpUtil .TE_TRAILERS .getBytes (UTF_8 ));
75+ public static final ByteString USER_AGENT = new ByteString (USER_AGENT_KEY .name ().getBytes (UTF_8 ));
7576
7677 public static final Resource <EventLoopGroup > DEFAULT_BOSS_EVENT_LOOP_GROUP =
7778 new DefaultEventLoopGroupResource (1 , "grpc-default-boss-ELG" );
7879
7980 public static final Resource <EventLoopGroup > DEFAULT_WORKER_EVENT_LOOP_GROUP =
8081 new DefaultEventLoopGroupResource (0 , "grpc-default-worker-ELG" );
8182
82- /**
83- * Copies the content of the given {@link ByteBuffer} to a new {@link ByteBuf} instance.
84- */
85- static ByteBuf toByteBuf (ByteBufAllocator alloc , ByteBuffer source ) {
86- ByteBuf buf = alloc .buffer (source .remaining ());
87- buf .writeBytes (source );
88- return buf ;
89- }
90-
9183 public static Metadata .Headers convertHeaders (Http2Headers http2Headers ) {
9284 Metadata .Headers headers = new Metadata .Headers (convertHeadersToArray (http2Headers ));
9385 if (http2Headers .authority () != null ) {
@@ -137,6 +129,10 @@ public static Http2Headers convertClientHeaders(Metadata.Headers headers,
137129 http2Headers .path (new ByteString (headers .getPath ().getBytes (UTF_8 )));
138130 }
139131
132+ // Set the User-Agent header.
133+ String userAgent = HttpUtil .getGrpcUserAgent ("netty" , headers .get (USER_AGENT_KEY ));
134+ http2Headers .set (USER_AGENT , new ByteString (userAgent .getBytes (UTF_8 )));
135+
140136 return http2Headers ;
141137 }
142138
@@ -165,9 +161,11 @@ private static Http2Headers convertMetadata(Metadata headers) {
165161 Http2Headers http2Headers = new DefaultHttp2Headers ();
166162 byte [][] serializedHeaders = TransportFrameUtil .toHttp2Headers (headers );
167163 for (int i = 0 ; i < serializedHeaders .length ; i += 2 ) {
168- http2Headers .add (new ByteString (serializedHeaders [i ], false ),
169- new ByteString (serializedHeaders [i + 1 ], false ));
164+ ByteString name = new ByteString (serializedHeaders [i ], false );
165+ ByteString value = new ByteString (serializedHeaders [i + 1 ], false );
166+ http2Headers .add (name , value );
170167 }
168+
171169 return http2Headers ;
172170 }
173171
0 commit comments