@Test public void createRequestHeaders_sanitizes() { Metadata metaData = new Metadata(); metaData.put(GrpcUtil.CONTENT_TYPE_KEY, "to-be-removed"); metaData.put(GrpcUtil.USER_AGENT_KEY, "to-be-removed"); metaData.put(GrpcUtil.TE_HEADER, "to-be-removed"); Metadata.Key<String> userKey = Metadata.Key.of("user-key", Metadata.ASCII_STRING_MARSHALLER); String userValue = "user-value"; metaData.put(userKey, userValue); String path = " String authority = "localhost"; String userAgent = "useragent"; List<Header> headers = Headers.createRequestHeaders( metaData, path, authority, userAgent, false); assertEquals(7 + 1, headers.size()); assertThat(headers).contains(Headers.CONTENT_TYPE_HEADER); assertThat(headers).contains(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent)); assertThat(headers).contains(new Header(GrpcUtil.TE_HEADER.name(), GrpcUtil.TE_TRAILERS)); assertThat(headers).contains(new Header(userKey.name(), userValue)); }
public static List<Header> createRequestHeaders( Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet) { Preconditions.checkNotNull(headers, "headers"); Preconditions.checkNotNull(defaultPath, "defaultPath"); Preconditions.checkNotNull(authority, "authority"); headers.discardAll(GrpcUtil.CONTENT_TYPE_KEY); headers.discardAll(GrpcUtil.TE_HEADER); headers.discardAll(GrpcUtil.USER_AGENT_KEY); List<Header> okhttpHeaders = new ArrayList<>(7 + InternalMetadata.headerCount(headers)); okhttpHeaders.add(SCHEME_HEADER); if (useGet) { okhttpHeaders.add(METHOD_GET_HEADER); } else { okhttpHeaders.add(METHOD_HEADER); } okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority)); String path = defaultPath; okhttpHeaders.add(new Header(Header.TARGET_PATH, path)); okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent)); okhttpHeaders.add(CONTENT_TYPE_HEADER); okhttpHeaders.add(TE_HEADER); byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers); for (int i = 0; i < serializedHeaders.length; i += 2) { ByteString key = ByteString.of(serializedHeaders[i]); String keyString = key.utf8(); if (isApplicationHeader(keyString)) { ByteString value = ByteString.of(serializedHeaders[i + 1]); okhttpHeaders.add(new Header(key, value)); } } return okhttpHeaders; }
Headers { public static List<Header> createRequestHeaders( Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet) { Preconditions.checkNotNull(headers, "headers"); Preconditions.checkNotNull(defaultPath, "defaultPath"); Preconditions.checkNotNull(authority, "authority"); headers.discardAll(GrpcUtil.CONTENT_TYPE_KEY); headers.discardAll(GrpcUtil.TE_HEADER); headers.discardAll(GrpcUtil.USER_AGENT_KEY); List<Header> okhttpHeaders = new ArrayList<>(7 + InternalMetadata.headerCount(headers)); okhttpHeaders.add(SCHEME_HEADER); if (useGet) { okhttpHeaders.add(METHOD_GET_HEADER); } else { okhttpHeaders.add(METHOD_HEADER); } okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority)); String path = defaultPath; okhttpHeaders.add(new Header(Header.TARGET_PATH, path)); okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent)); okhttpHeaders.add(CONTENT_TYPE_HEADER); okhttpHeaders.add(TE_HEADER); byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers); for (int i = 0; i < serializedHeaders.length; i += 2) { ByteString key = ByteString.of(serializedHeaders[i]); String keyString = key.utf8(); if (isApplicationHeader(keyString)) { ByteString value = ByteString.of(serializedHeaders[i + 1]); okhttpHeaders.add(new Header(key, value)); } } return okhttpHeaders; } }
Headers { public static List<Header> createRequestHeaders( Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet) { Preconditions.checkNotNull(headers, "headers"); Preconditions.checkNotNull(defaultPath, "defaultPath"); Preconditions.checkNotNull(authority, "authority"); headers.discardAll(GrpcUtil.CONTENT_TYPE_KEY); headers.discardAll(GrpcUtil.TE_HEADER); headers.discardAll(GrpcUtil.USER_AGENT_KEY); List<Header> okhttpHeaders = new ArrayList<>(7 + InternalMetadata.headerCount(headers)); okhttpHeaders.add(SCHEME_HEADER); if (useGet) { okhttpHeaders.add(METHOD_GET_HEADER); } else { okhttpHeaders.add(METHOD_HEADER); } okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority)); String path = defaultPath; okhttpHeaders.add(new Header(Header.TARGET_PATH, path)); okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent)); okhttpHeaders.add(CONTENT_TYPE_HEADER); okhttpHeaders.add(TE_HEADER); byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers); for (int i = 0; i < serializedHeaders.length; i += 2) { ByteString key = ByteString.of(serializedHeaders[i]); String keyString = key.utf8(); if (isApplicationHeader(keyString)) { ByteString value = ByteString.of(serializedHeaders[i + 1]); okhttpHeaders.add(new Header(key, value)); } } return okhttpHeaders; }  }
Headers { public static List<Header> createRequestHeaders( Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet) { Preconditions.checkNotNull(headers, "headers"); Preconditions.checkNotNull(defaultPath, "defaultPath"); Preconditions.checkNotNull(authority, "authority"); headers.discardAll(GrpcUtil.CONTENT_TYPE_KEY); headers.discardAll(GrpcUtil.TE_HEADER); headers.discardAll(GrpcUtil.USER_AGENT_KEY); List<Header> okhttpHeaders = new ArrayList<>(7 + InternalMetadata.headerCount(headers)); okhttpHeaders.add(SCHEME_HEADER); if (useGet) { okhttpHeaders.add(METHOD_GET_HEADER); } else { okhttpHeaders.add(METHOD_HEADER); } okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority)); String path = defaultPath; okhttpHeaders.add(new Header(Header.TARGET_PATH, path)); okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent)); okhttpHeaders.add(CONTENT_TYPE_HEADER); okhttpHeaders.add(TE_HEADER); byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers); for (int i = 0; i < serializedHeaders.length; i += 2) { ByteString key = ByteString.of(serializedHeaders[i]); String keyString = key.utf8(); if (isApplicationHeader(keyString)) { ByteString value = ByteString.of(serializedHeaders[i + 1]); okhttpHeaders.add(new Header(key, value)); } } return okhttpHeaders; }  static List<Header> createRequestHeaders(
      Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet); }
Headers { public static List<Header> createRequestHeaders( Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet) { Preconditions.checkNotNull(headers, "headers"); Preconditions.checkNotNull(defaultPath, "defaultPath"); Preconditions.checkNotNull(authority, "authority"); headers.discardAll(GrpcUtil.CONTENT_TYPE_KEY); headers.discardAll(GrpcUtil.TE_HEADER); headers.discardAll(GrpcUtil.USER_AGENT_KEY); List<Header> okhttpHeaders = new ArrayList<>(7 + InternalMetadata.headerCount(headers)); okhttpHeaders.add(SCHEME_HEADER); if (useGet) { okhttpHeaders.add(METHOD_GET_HEADER); } else { okhttpHeaders.add(METHOD_HEADER); } okhttpHeaders.add(new Header(Header.TARGET_AUTHORITY, authority)); String path = defaultPath; okhttpHeaders.add(new Header(Header.TARGET_PATH, path)); okhttpHeaders.add(new Header(GrpcUtil.USER_AGENT_KEY.name(), userAgent)); okhttpHeaders.add(CONTENT_TYPE_HEADER); okhttpHeaders.add(TE_HEADER); byte[][] serializedHeaders = TransportFrameUtil.toHttp2Headers(headers); for (int i = 0; i < serializedHeaders.length; i += 2) { ByteString key = ByteString.of(serializedHeaders[i]); String keyString = key.utf8(); if (isApplicationHeader(keyString)) { ByteString value = ByteString.of(serializedHeaders[i + 1]); okhttpHeaders.add(new Header(key, value)); } } return okhttpHeaders; }  static List<Header> createRequestHeaders(
      Metadata headers, String defaultPath, String authority, String userAgent, boolean useGet); static final Header SCHEME_HEADER; static final Header METHOD_HEADER; static final Header METHOD_GET_HEADER; static final Header CONTENT_TYPE_HEADER; static final Header TE_HEADER; }