Skip to content

Commit f485171

Browse files
author
Julia Boes
committed
8269692: sun.net.httpserver.ServerImpl::createContext should throw IAE
Reviewed-by: dfuchs
1 parent 16aa8cb commit f485171

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

src/jdk.httpserver/share/classes/sun/net/httpserver/ContextList.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,20 +26,23 @@
2626
package sun.net.httpserver;
2727

2828
import java.util.*;
29-
import com.sun.net.httpserver.*;
30-
import com.sun.net.httpserver.spi.*;
3129

3230
class ContextList {
3331

34-
final static int MAX_CONTEXTS = 50;
35-
36-
LinkedList<HttpContextImpl> list = new LinkedList<HttpContextImpl>();
32+
private final LinkedList<HttpContextImpl> list = new LinkedList<>();
3733

3834
public synchronized void add (HttpContextImpl ctx) {
3935
assert ctx.getPath() != null;
36+
if (contains(ctx)) {
37+
throw new IllegalArgumentException ("cannot add context to list");
38+
}
4039
list.add (ctx);
4140
}
4241

42+
boolean contains(HttpContextImpl ctx) {
43+
return findContext(ctx.getProtocol(), ctx.getPath(), true) != null;
44+
}
45+
4346
public synchronized int size () {
4447
return list.size();
4548
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8269692
27+
* @summary HttpContext::createContext should throw IllegalArgumentException
28+
* if context already exists
29+
* @run testng/othervm HttpContextTest
30+
*/
31+
32+
import java.io.IOException;
33+
import com.sun.net.httpserver.HttpContext;
34+
import com.sun.net.httpserver.HttpExchange;
35+
import com.sun.net.httpserver.HttpHandler;
36+
import com.sun.net.httpserver.HttpServer;
37+
import org.testng.annotations.Test;
38+
import static org.testng.Assert.assertEquals;
39+
import static org.testng.Assert.assertThrows;
40+
41+
public class HttpContextTest {
42+
43+
static final Class<IllegalArgumentException> IAE = IllegalArgumentException.class;
44+
45+
@Test
46+
public static void test() throws IOException {
47+
final var server = HttpServer.create(null, 0);
48+
final var path = "/foo/";
49+
50+
assertThrows(IAE, () -> server.removeContext(path));
51+
HttpContext context = server.createContext(path);
52+
assertEquals(context.getPath(), path);
53+
assertThrows(IAE, () -> server.createContext(path));
54+
assertThrows(IAE, () -> server.createContext(path, new Handler()));
55+
56+
context.setHandler(new Handler());
57+
assertThrows(IAE, () -> server.createContext(path));
58+
assertThrows(IAE, () -> server.createContext(path, new Handler()));
59+
server.removeContext(context);
60+
assertThrows(IAE, () -> server.removeContext(path));
61+
62+
context = server.createContext(path, new Handler());
63+
assertEquals(context.getPath(), path);
64+
assertThrows(IAE, () -> server.createContext(path));
65+
assertThrows(IAE, () -> server.createContext(path, new Handler()));
66+
server.removeContext(path);
67+
assertThrows(IAE, () -> server.removeContext(path));
68+
}
69+
70+
/**
71+
* Confirms that it is possible to create a subcontext, a context whose path
72+
* shares the prefix of an existing context.
73+
*/
74+
@Test
75+
public static void testSubcontext() throws IOException {
76+
final var server = HttpServer.create(null, 0);
77+
server.createContext("/foo/bar/");
78+
server.createContext("/foo/");
79+
80+
server.createContext("/foo");
81+
server.createContext("/foo/bar");
82+
}
83+
84+
/**
85+
* A no-op handler
86+
*/
87+
static class Handler implements HttpHandler {
88+
@Override
89+
public void handle(HttpExchange exchange) throws IOException { }
90+
}
91+
}

0 commit comments

Comments
 (0)