Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b0e8b57

Browse files
committed
fix the test for HTTP protocol upgrade
If the HTTP client wants to request protocol upgrade, it must send the `Upgrade` header. It should also set the `Connection` header. This test didn't do that, so conforming implementations could reject the `request.upgrade()` call. The fix is simple: add the 2 headers on the client side (i.e., in the test). To be able to set `Connection` and `Upgrade` headers on the `java.net.HttpURLConnection`, restricted headers must be allowed by setting a special system property. See also https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism and the Servlet specification 4.0, section 2.3.3.5.
1 parent 278bf76 commit b0e8b57

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

‎servlet/protocol-handler/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,19 @@
1212
<packaging>war</packaging>
1313

1414
<name>Java EE 7 Sample: servlet - protocol-handler</name>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-surefire-plugin</artifactId>
21+
<configuration>
22+
<systemPropertyVariables combine.children="append">
23+
<!-- to request protocol upgrade, the client must send the Connection and Upgrade headers -->
24+
<sun.net.http.allowRestrictedHeaders>true</sun.net.http.allowRestrictedHeaders>
25+
</systemPropertyVariables>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
1530
</project>

‎servlet/protocol-handler/src/main/java/org/javaee7/servlet/protocolhandler/UpgradeServlet.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
package org.javaee7.servlet.protocolhandler;
4141

42+
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
4243
import static javax.servlet.http.HttpServletResponse.SC_SWITCHING_PROTOCOLS;
4344

4445
import java.io.IOException;
@@ -66,11 +67,16 @@ public class UpgradeServlet extends HttpServlet {
6667
* @throws IOException if an I/O error occurs
6768
*/
6869
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69-
response.setStatus(SC_SWITCHING_PROTOCOLS);
70-
response.setHeader("Connection", "Upgrade");
71-
response.setHeader("Upgrade", "echo");
72-
request.upgrade(MyProtocolHandler.class);
73-
74-
System.out.println("Request upgraded to MyProtocolHandler");
70+
String requestedUpgrade = request.getHeader("Upgrade");
71+
if ("echo".equals(requestedUpgrade)) {
72+
response.setStatus(SC_SWITCHING_PROTOCOLS);
73+
response.setHeader("Connection", "Upgrade");
74+
response.setHeader("Upgrade", "echo");
75+
request.upgrade(MyProtocolHandler.class);
76+
77+
System.out.println("Request upgraded to MyProtocolHandler");
78+
} else {
79+
response.sendError(SC_BAD_REQUEST, "unknown upgrade " + requestedUpgrade);
80+
}
7581
}
7682
}

‎servlet/protocol-handler/src/test/java/org/javaee7/servlet/protocolhandler/ProtocolHandlerTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.javaee7.servlet.protocolhandler;
22

3-
import static org.junit.Assert.assertTrue;
3+
import static org.junit.Assert.assertEquals;
44

55
import java.io.IOException;
66
import java.io.InputStream;
@@ -44,6 +44,8 @@ public void testUpgradeProtocol() throws IOException, URISyntaxException {
4444
// typically hang when reading.
4545

4646
URLConnection connection = new URL(base, "UpgradeServlet").openConnection();
47+
connection.setRequestProperty("Connection", "Upgrade");
48+
connection.setRequestProperty("Upgrade", "echo");
4749
connection.setConnectTimeout(2000);
4850
connection.setReadTimeout(2000);
4951

@@ -71,7 +73,7 @@ public void testUpgradeProtocol() throws IOException, URISyntaxException {
7173
}
7274
}
7375

74-
assertTrue("In protocol handler".equals(response.toString()));
76+
assertEquals("In protocol handler", response.toString());
7577
}
7678

7779
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /