Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes the multiple user-agent headers #5315

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import javax.net.ssl.SSLContext;

import jakarta.ws.rs.ext.RuntimeDelegate;
import org.eclipse.jetty.client.HttpClientTransport;
import org.eclipse.jetty.client.HttpRequest;
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
Expand Down Expand Up @@ -254,7 +255,6 @@ public CookieStore getCookieStore() {

@Override
public ClientResponse apply(final ClientRequest jerseyRequest) throws ProcessingException {
applyUserAgentHeader(jerseyRequest.getHeaders());
final Request jettyRequest = translateRequest(jerseyRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest);
final ContentProvider entity = getBytesProvider(jerseyRequest);
Expand Down Expand Up @@ -338,34 +338,17 @@ private Request translateRequest(final ClientRequest clientRequest) {
return request;
}

/**
* Re-write User-agent header set by Jetty; Jersey already sets this in its request (incl. Jetty version)
* it shall be propagated to Jetty client before HttpRequest instance is created.
* HttpRequest takes User Agent header from client then.
*
* @param headers - map of Jersey headers
*/
private void applyUserAgentHeader(final MultivaluedMap<String, Object> headers) {
if (headers.containsKey(HttpHeaders.USER_AGENT)) {
final Map<String, String> stringHeaders =
HeaderUtils.asStringHeadersSingleValue(headers, configuration);
client.setUserAgentField(
new HttpField(HttpHeader.USER_AGENT,
HttpHeader.USER_AGENT.name(),
stringHeaders.get(HttpHeaders.USER_AGENT))
);
}
}

private Map<String, String> writeOutBoundHeaders(final MultivaluedMap<String, Object> headers, final Request request) {
final Map<String, String> stringHeaders = HeaderUtils.asStringHeadersSingleValue(headers, configuration);

if (request instanceof HttpRequest) {
final HttpRequest httpRequest = (HttpRequest) request;
for (final Map.Entry<String, String> e : stringHeaders.entrySet()) {
httpRequest.addHeader(new HttpField(e.getKey(), e.getValue()));
}
}
// remove User-agent header set by Jetty; Jersey already sets this in its request (incl. Jetty version)
request.headers(httpFields -> httpFields.remove(HttpHeader.USER_AGENT));
if (request instanceof HttpRequest) {
final HttpRequest httpRequest = (HttpRequest) request;
for (final Map.Entry<String, String> e : stringHeaders.entrySet()) {
httpRequest.addHeader(new HttpField(e.getKey(), e.getValue()));
}
}
return stringHeaders;
}

Expand Down Expand Up @@ -422,7 +405,6 @@ private void processContent(final ClientRequest clientRequest, final ContentProv

@Override
public Future<?> apply(final ClientRequest jerseyRequest, final AsyncConnectorCallback callback) {
applyUserAgentHeader(jerseyRequest.getHeaders());
final Request jettyRequest = translateRequest(jerseyRequest);
final Map<String, String> clientHeadersSnapshot = writeOutBoundHeaders(jerseyRequest.getHeaders(), jettyRequest);
final ContentProvider entity = getStreamProvider(jerseyRequest);
Expand Down
1 change: 1 addition & 0 deletions tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
</activation>
<modules>
<module>release-test</module>
<module>version-agnostic</module>
</modules>
</profile>
</profiles>
Expand Down
129 changes: 129 additions & 0 deletions tests/version-agnostic/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.eclipse.ee4j</groupId>
<artifactId>project</artifactId>
<version>1.0.8</version>
<relativePath>../../../pom.xml</relativePath>
</parent>

<groupId>org.glassfish.jersey.tests</groupId>
<artifactId>version-agnostic</artifactId>
<packaging>jar</packaging>
<name>jersey-agnostic-test</name>

<description>Jersey post-release validation tests</description>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<hk2.version>3.0.3</hk2.version>
<jersey.version>3.0.99-SNAPSHOT</jersey.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<enableAssertions>false</enableAssertions>
</configuration>
</plugin>
<plugin>
<groupId>org.glassfish.copyright</groupId>
<artifactId>glassfish-copyright-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludeFile>etc/config/copyright-exclude</excludeFile>
<!--svn|mercurial|git - defaults to svn-->
<scm>git</scm>
<!-- turn on/off debugging -->
<debug>false</debug>
<!-- skip files not under SCM-->
<scmOnly>true</scmOnly>
<!-- turn off warnings -->
<warn>false</warn>
<!-- for use with repair -->
<update>false</update>
<!-- check that year is correct -->
<ignoreYear>false</ignoreYear>
<templateFile>etc/config/copyright.txt</templateFile>
<bsdTemplateFile>etc/config/edl-copyright.txt</bsdTemplateFile>
</configuration>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-bundle</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-jetty-connector</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-api</artifactId>
<version>${hk2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-utils</artifactId>
<version>${hk2.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-locator</artifactId>
<version>${hk2.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.test.agnostic;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.jetty.connector.JettyConnectorProvider;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.logging.Level;

public class JettyHeaderTest extends JerseyTest {
@Path("/")
public static class JettyHeaderTestResource {
@Context
HttpHeaders httpHeaders;

@GET
public String get() {
return httpHeaders.getHeaderString(HttpHeaders.USER_AGENT);
}
}

@Override
protected Application configure() {
return new ResourceConfig(JettyHeaderTestResource.class);
//.register(LoggingFeature.builder().level(Level.INFO).verbosity(LoggingFeature.Verbosity.PAYLOAD_ANY).build());
}

@Override
protected void configureClient(ClientConfig config) {
config.connectorProvider(new JettyConnectorProvider());
}

@Test
public void helloWorldTest() {
try (Response r = target().request().get()) {
Assertions.assertEquals(200, r.getStatus());

int index = -1;
String userAgent = r.readEntity(String.class);
index = userAgent.indexOf("Jersey");
Assertions.assertTrue(index != -1);

index = userAgent.indexOf("Jersey", index + 1);
Assertions.assertEquals(-1, index);
}
}
}