Skip to content

Commit b128e9c

Browse files
committed
Do not stuck Netty on header-setting exception
Signed-off-by: jansupol <[email protected]>
1 parent 47d1794 commit b128e9c

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-5
lines changed

connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyConnector.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2024 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -17,7 +17,9 @@
1717
package org.glassfish.jersey.netty.connector;
1818

1919
import java.io.IOException;
20+
import java.io.InterruptedIOException;
2021
import java.io.OutputStream;
22+
import java.io.OutputStreamWriter;
2123
import java.net.InetSocketAddress;
2224
import java.net.SocketAddress;
2325
import java.net.URI;
@@ -462,10 +464,15 @@ public void operationComplete(io.netty.util.concurrent.Future<? super Void> futu
462464
jerseyRequest.setStreamProvider(new OutboundMessageContext.StreamProvider() {
463465
@Override
464466
public OutputStream getOutputStream(int contentLength) throws IOException {
465-
replaceHeaders(jerseyRequest, nettyRequest.headers()); // WriterInterceptor changes
466-
setHostHeader(jerseyRequest, nettyRequest);
467-
headersSet.countDown();
468-
467+
try {
468+
replaceHeaders(jerseyRequest, nettyRequest.headers()); // WriterInterceptor changes
469+
setHostHeader(jerseyRequest, nettyRequest);
470+
} catch (Exception e) {
471+
responseDone.completeExceptionally(e);
472+
throw new IOException(e);
473+
} finally {
474+
headersSet.countDown();
475+
}
469476
return entityWriter.getOutputStream();
470477
}
471478
});
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.netty.connector;
18+
19+
import org.glassfish.jersey.client.ClientConfig;
20+
import org.glassfish.jersey.server.ResourceConfig;
21+
import org.glassfish.jersey.test.JerseyTest;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.Test;
24+
25+
import javax.ws.rs.GET;
26+
import javax.ws.rs.Path;
27+
import javax.ws.rs.ProcessingException;
28+
import javax.ws.rs.client.ClientBuilder;
29+
import javax.ws.rs.client.Entity;
30+
import javax.ws.rs.core.Application;
31+
import javax.ws.rs.core.MediaType;
32+
import javax.ws.rs.core.MultivaluedHashMap;
33+
import javax.ws.rs.core.MultivaluedMap;
34+
import javax.ws.rs.core.Response;
35+
import java.util.Collections;
36+
import java.util.concurrent.ExecutionException;
37+
import java.util.logging.Logger;
38+
39+
public class EmptyHeaderTest extends JerseyTest {
40+
41+
private static final Logger LOGGER = Logger.getLogger(EmptyHeaderTest.class.getName());
42+
43+
public static void main(String[] args) throws ExecutionException, InterruptedException {
44+
new EmptyHeaderTest().testEmptyHeaders();
45+
}
46+
47+
@Override
48+
protected Application configure() {
49+
return new ResourceConfig().register(new EmptyHeaderTestResource());
50+
}
51+
52+
@Test
53+
public void testEmptyHeaders() throws ExecutionException, InterruptedException {
54+
MultivaluedMap<String, Object> jersey2Headers = new MultivaluedHashMap();
55+
jersey2Headers.put("", Collections.singletonList("sss"));
56+
57+
Entity mData = Entity.entity("{\"dd\":\"ddd\"}", MediaType.APPLICATION_JSON_TYPE);
58+
59+
ClientConfig config = new ClientConfig();
60+
config.connectorProvider(new NettyConnectorProvider());
61+
try {
62+
Response r = ClientBuilder.newBuilder()
63+
.withConfig(config)
64+
.build()
65+
.target(target().getUri())
66+
.request()
67+
.headers(jersey2Headers)
68+
.post(mData);
69+
Assertions.fail("Processing Exception not thrown for empty header name");
70+
} catch (ProcessingException processingException) {
71+
System.out.println(processingException.getMessage());
72+
}
73+
}
74+
75+
@Path("")
76+
private static class EmptyHeaderTestResource {
77+
@GET
78+
public Response ok() {
79+
return Response.ok().build();
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)