├── .gitignore
├── LICENSE
├── pom.xml
└── src
├── main
└── java
│ └── org
│ └── jboss
│ ├── com
│ └── sun
│ │ └── net
│ │ └── httpserver
│ │ ├── Authenticator.java
│ │ ├── BasicAuthenticator.java
│ │ ├── Filter.java
│ │ ├── Headers.java
│ │ ├── HttpContext.java
│ │ ├── HttpExchange.java
│ │ ├── HttpHandler.java
│ │ ├── HttpPrincipal.java
│ │ ├── HttpServer.java
│ │ ├── HttpsConfigurator.java
│ │ ├── HttpsExchange.java
│ │ ├── HttpsParameters.java
│ │ ├── HttpsServer.java
│ │ ├── package-info.java
│ │ └── spi
│ │ ├── HttpServerProvider.java
│ │ └── package-info.java
│ └── sun
│ └── net
│ └── httpserver
│ ├── AuthFilter.java
│ ├── ChunkedInputStream.java
│ ├── ChunkedOutputStream.java
│ ├── Code.java
│ ├── ContextList.java
│ ├── DefaultHttpServerProvider.java
│ ├── Event.java
│ ├── ExchangeImpl.java
│ ├── FixedLengthInputStream.java
│ ├── FixedLengthOutputStream.java
│ ├── HttpConnection.java
│ ├── HttpContextImpl.java
│ ├── HttpError.java
│ ├── HttpExchangeImpl.java
│ ├── HttpServerImpl.java
│ ├── HttpsExchangeImpl.java
│ ├── HttpsServerImpl.java
│ ├── LeftOverInputStream.java
│ ├── Request.java
│ ├── SSLStreams.java
│ ├── ServerConfig.java
│ ├── ServerImpl.java
│ ├── StreamClosedException.java
│ ├── TimeSource.java
│ ├── UndefLengthOutputStream.java
│ └── UnmodifiableHeaders.java
└── test
└── java
├── B6339483.java
├── B6341616.java
├── B6361557.java
├── B6373555.java
├── B6393710.java
├── B6401598.java
├── B6421581.java
├── B6424196.java
├── B6431193.java
├── B6433018.java
├── B6526158.java
├── B6526913.java
├── B6529200.java
├── B6744329.java
├── B6886436.java
├── DummyVerifier.java
├── FileServerHandler.java
├── FixedLengthInputStream.java
├── HeadTest.java
├── LogFilter.java
├── SelCacheTest.java
├── SimpleSSLContext.java
├── Test.java
├── Test1.java
├── Test10.java
├── Test11.java
├── Test12.java
├── Test13.java
├── Test14.java
├── Test2.java
├── Test3.java
├── Test4.java
├── Test5.java
├── Test6.java
├── Test6a.java
├── Test7.java
├── Test7a.java
├── Test8.java
├── Test8a.java
├── Test9.java
├── Test9a.java
├── TestLogging.java
├── org
└── jboss
│ └── com
│ └── sun
│ └── net
│ └── httpserver
│ └── BZ1312064.java
└── testkeys
/.gitignore:
--------------------------------------------------------------------------------
1 | target
2 | *.iml
3 | *.ipr
4 | *.iws
5 | .classpath
6 | *~
7 | .project
8 | .settings
9 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | org.jboss.com.sun.httpserver
6 | httpserver
7 | 1.0.9.Final-SNAPSHOT
8 |
9 |
10 | org.jboss
11 | jboss-parent
12 | 16
13 |
14 |
15 | Lightweight HTTP Server
16 |
17 |
18 |
19 | GPLv2 (with classpath exception)
20 | repo
21 |
22 |
23 |
24 |
25 |
26 | junit
27 | junit
28 | 4.8.1
29 |
30 |
31 |
32 |
33 |
34 |
35 | maven-compiler-plugin
36 |
37 |
38 | maven-jar-plugin
39 |
40 |
41 | maven-source-plugin
42 |
43 |
44 | maven-javadoc-plugin
45 |
46 |
47 |
48 |
49 |
50 | scm:git:https://github.com/jbossas/httpserver.git
51 | scm:git:git@github.com:jbossas/httpserver.git
52 | https://github.com/jbossas/httpserver
53 | HEAD
54 |
55 |
56 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/Authenticator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | /**
29 | * Authenticator represents an implementation of an HTTP authentication
30 | * mechanism. Sub-classes provide implementations of specific mechanisms
31 | * such as Digest or Basic auth. Instances are invoked to provide verification
32 | * of the authentication information provided in all incoming requests.
33 | * Note. This implies that any caching of credentials or other authentication
34 | * information must be done outside of this class.
35 | */
36 | public abstract class Authenticator {
37 |
38 | /**
39 | * Base class for return type from authenticate() method
40 | */
41 | public abstract static class Result {}
42 |
43 | /**
44 | * Indicates an authentication failure. The authentication
45 | * attempt has completed.
46 | */
47 | public static class Failure extends Result {
48 |
49 | private int responseCode;
50 |
51 | public Failure (int responseCode) {
52 | this.responseCode = responseCode;
53 | }
54 |
55 | /**
56 | * returns the response code to send to the client
57 | */
58 | public int getResponseCode() {
59 | return responseCode;
60 | }
61 | }
62 |
63 | /**
64 | * Indicates an authentication has succeeded and the
65 | * authenticated user principal can be acquired by calling
66 | * getPrincipal().
67 | */
68 | public static class Success extends Result {
69 | private HttpPrincipal principal;
70 |
71 | public Success (HttpPrincipal p) {
72 | principal = p;
73 | }
74 | /**
75 | * returns the authenticated user Principal
76 | */
77 | public HttpPrincipal getPrincipal() {
78 | return principal;
79 | }
80 | }
81 |
82 | /**
83 | * Indicates an authentication must be retried. The
84 | * response code to be sent back is as returned from
85 | * getResponseCode(). The Authenticator must also have
86 | * set any necessary response headers in the given HttpExchange
87 | * before returning this Retry object.
88 | */
89 | public static class Retry extends Result {
90 |
91 | private int responseCode;
92 |
93 | public Retry (int responseCode) {
94 | this.responseCode = responseCode;
95 | }
96 |
97 | /**
98 | * returns the response code to send to the client
99 | */
100 | public int getResponseCode() {
101 | return responseCode;
102 | }
103 | }
104 |
105 | /**
106 | * called to authenticate each incoming request. The implementation
107 | * must return a Failure, Success or Retry object as appropriate :-
108 | *
109 | * Failure means the authentication has completed, but has failed
110 | * due to invalid credentials.
111 | *
112 | * Sucess means that the authentication
113 | * has succeeded, and a Principal object representing the user
114 | * can be retrieved by calling Sucess.getPrincipal() .
115 | *
116 | * Retry means that another HTTP exchange is required. Any response
117 | * headers needing to be sent back to the client are set in the
118 | * given HttpExchange. The response code to be returned must be provided
119 | * in the Retry object. Retry may occur multiple times.
120 | */
121 | public abstract Result authenticate (HttpExchange exch);
122 | }
123 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/Filter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2010, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.util.List;
30 | import java.util.ListIterator;
31 |
32 | /**
33 | * A filter used to pre- and post-process incoming requests. Pre-processing occurs
34 | * before the application's exchange handler is invoked, and post-processing
35 | * occurs after the exchange handler returns. Filters
36 | * are organised in chains, and are associated with HttpContext instances.
37 | *
38 | * Each Filter in the chain, invokes the next filter within its own
39 | * doFilter() implementation. The final Filter in the chain invokes the applications
40 | * exchange handler.
41 | * @since 1.6
42 | */
43 | public abstract class Filter {
44 |
45 | protected Filter () {}
46 |
47 | /**
48 | * a chain of filters associated with a HttpServer.
49 | * Each filter in the chain is given one of these
50 | * so it can invoke the next filter in the chain
51 | */
52 | public static class Chain {
53 | /* the last element in the chain must invoke the users
54 | * handler
55 | */
56 | private ListIterator iter;
57 | private HttpHandler handler;
58 |
59 | public Chain (List filters, HttpHandler handler) {
60 | iter = filters.listIterator();
61 | this.handler = handler;
62 | }
63 |
64 | /**
65 | * calls the next filter in the chain, or else
66 | * the users exchange handler, if this is the
67 | * final filter in the chain. The Filter may decide
68 | * to terminate the chain, by not calling this method.
69 | * In this case, the filter must send the
70 | * response to the request, because the application's
71 | * exchange handler will not be invoked.
72 | * @param exchange the HttpExchange
73 | * @throws IOException let exceptions pass up the stack
74 | * @throws NullPointerException if exchange is null
75 | */
76 | public void doFilter (HttpExchange exchange) throws IOException {
77 | if (!iter.hasNext()) {
78 | handler.handle (exchange);
79 | } else {
80 | Filter f = iter.next();
81 | f.doFilter (exchange, this);
82 | }
83 | }
84 | }
85 |
86 | /**
87 | * Asks this filter to pre/post-process the given exchange. The filter
88 | * can :-
89 | * - examine or modify the request headers
90 | * - filter the request body or the response body, by creating suitable
91 | * filter streams and calling
92 | * {@link HttpExchange#setStreams(InputStream,OutputStream)}
93 | * - set attribute Objects in the exchange, which other filters or the
94 | * exchange handler can access.
95 | * - decide to either :-
96 | * - invoke the next filter in the chain, by calling
97 | * {@link Filter.Chain#doFilter(HttpExchange)}
98 | * - terminate the chain of invocation, by not calling
99 | * {@link Filter.Chain#doFilter(HttpExchange)}
100 | * - if option 1. above taken, then when doFilter() returns all subsequent
101 | * filters in the Chain have been called, and the response headers can be
102 | * examined or modified.
103 | * - if option 2. above taken, then this Filter must use the HttpExchange
104 | * to send back an appropriate response
105 | * @param exchange the HttpExchange
to be filtered.
106 | * @param chain the Chain which allows the next filter to be invoked.
107 | * @throws IOException may be thrown by any filter module, and if
108 | * caught, must be rethrown again.
109 | * @throws NullPointerException if either exchange or chain are null
110 | */
111 | public abstract void doFilter (HttpExchange exchange, Chain chain)
112 | throws IOException;
113 |
114 | /**
115 | * returns a short description of this Filter
116 | * @return a string describing the Filter
117 | */
118 | public abstract String description ();
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/HttpContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | import java.util.List;
29 | import java.util.Map;
30 |
31 | /**
32 | * HttpContext represents a mapping between the root URI path of an application
33 | * to a {@link HttpHandler} which is invoked to handle requests destined
34 | * for that path on the associated HttpServer or HttpsServer.
35 | *
36 | * HttpContext instances are created by the create methods in HttpServer
37 | * and HttpsServer
38 | *
39 | * A chain of {@link Filter} objects can be added to a HttpContext. All exchanges processed by the
40 | * context can be pre- and post-processed by each Filter in the chain.
41 | * @since 1.6
42 | */
43 | public abstract class HttpContext {
44 |
45 | protected HttpContext () {
46 | }
47 |
48 | /**
49 | * returns the handler for this context
50 | * @return the HttpHandler for this context
51 | */
52 | public abstract HttpHandler getHandler () ;
53 |
54 | /**
55 | * Sets the handler for this context, if not already set.
56 | * @param h the handler to set for this context
57 | * @throws IllegalArgumentException if this context's handler is already set.
58 | * @throws NullPointerException if handler is null
59 | */
60 | public abstract void setHandler (HttpHandler h) ;
61 |
62 | /**
63 | * returns the path this context was created with
64 | * @return this context's path
65 | */
66 | public abstract String getPath() ;
67 |
68 | /**
69 | * returns the server this context was created with
70 | * @return this context's server
71 | */
72 | public abstract HttpServer getServer () ;
73 |
74 | /**
75 | * returns a mutable Map, which can be used to pass
76 | * configuration and other data to Filter modules
77 | * and to the context's exchange handler.
78 | *
79 | * Every attribute stored in this Map will be visible to
80 | * every HttpExchange processed by this context
81 | */
82 | public abstract Map getAttributes() ;
83 |
84 | /**
85 | * returns this context's list of Filters. This is the
86 | * actual list used by the server when dispatching requests
87 | * so modifications to this list immediately affect the
88 | * the handling of exchanges.
89 | */
90 | public abstract List getFilters();
91 |
92 | /**
93 | * Sets the Authenticator for this HttpContext. Once an authenticator
94 | * is establised on a context, all client requests must be
95 | * authenticated, and the given object will be invoked to validate each
96 | * request. Each call to this method replaces any previous value set.
97 | * @param auth the authenticator to set. If null
then any
98 | * previously set authenticator is removed,
99 | * and client authentication will no longer be required.
100 | * @return the previous Authenticator, if any set, or null
101 | * otherwise.
102 | */
103 | public abstract Authenticator setAuthenticator (Authenticator auth);
104 |
105 | /**
106 | * Returns the currently set Authenticator for this context
107 | * if one exists.
108 | * @return this HttpContext's Authenticator, or null
109 | * if none is set.
110 | */
111 | public abstract Authenticator getAuthenticator ();
112 | }
113 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/HttpHandler.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 |
30 | /**
31 | * A handler which is invoked to process HTTP exchanges. Each
32 | * HTTP exchange is handled by one of these handlers.
33 | * @since 1.6
34 | */
35 | public interface HttpHandler {
36 | /**
37 | * Handle the given request and generate an appropriate response.
38 | * See {@link HttpExchange} for a description of the steps
39 | * involved in handling an exchange.
40 | * @param exchange the exchange containing the request from the
41 | * client and used to send the response
42 | * @throws NullPointerException if exchange is null
43 | */
44 | public abstract void handle (HttpExchange exchange) throws IOException;
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/HttpPrincipal.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | import java.security.Principal;
29 |
30 | /**
31 | * Represents a user authenticated by HTTP Basic or Digest
32 | * authentication.
33 | */
34 | public class HttpPrincipal implements Principal {
35 | private String username, realm;
36 |
37 | /**
38 | * creates a HttpPrincipal from the given username and realm
39 | * @param username The name of the user within the realm
40 | * @param realm The realm.
41 | * @throws NullPointerException if either username or realm are null
42 | */
43 | public HttpPrincipal (String username, String realm) {
44 | if (username == null || realm == null) {
45 | throw new NullPointerException();
46 | }
47 | this.username = username;
48 | this.realm = realm;
49 | }
50 |
51 | /**
52 | * Compares two HttpPrincipal. Returns true
53 | * if another is an instance of HttpPrincipal, and its
54 | * username and realm are equal to this object's username
55 | * and realm. Returns false
otherwise.
56 | */
57 | public boolean equals (Object another) {
58 | if (!(another instanceof HttpPrincipal)) {
59 | return false;
60 | }
61 | HttpPrincipal theother = (HttpPrincipal)another;
62 | return (username.equals(theother.username) &&
63 | realm.equals(theother.realm));
64 | }
65 |
66 | /**
67 | * returns the contents of this principal in the form
68 | * realm:username
69 | */
70 | public String getName() {
71 | return username;
72 | }
73 |
74 | /**
75 | * returns the username this object was created with.
76 | */
77 | public String getUsername() {
78 | return username;
79 | }
80 |
81 | /**
82 | * returns the realm this object was created with.
83 | */
84 | public String getRealm() {
85 | return realm;
86 | }
87 |
88 | /**
89 | * returns a hashcode for this HttpPrincipal. This is calculated
90 | * as (getUsername()+getRealm().hashCode()
91 | */
92 | public int hashCode() {
93 | return (username+realm).hashCode();
94 | }
95 |
96 | /**
97 | * returns the same string as getName()
98 | */
99 | public String toString() {
100 | return getName();
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/HttpsConfigurator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2010, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | import javax.net.ssl.SSLContext;
29 | import javax.net.ssl.SSLParameters;
30 |
31 |
32 | /**
33 | * This class is used to configure the https parameters for each incoming
34 | * https connection on a HttpsServer. Applications need to override
35 | * the {@link #configure(HttpsParameters)} method in order to change
36 | * the default configuration.
37 | *
38 | * The following example shows how this may be done:
39 | *
40 | *
41 | * SSLContext sslContext = SSLContext.getInstance (....);
42 | * HttpsServer server = HttpsServer.create();
43 | *
44 | * server.setHttpsConfigurator (new HttpsConfigurator(sslContext) {
45 | * public void configure (HttpsParameters params) {
46 | *
47 | * // get the remote address if needed
48 | * InetSocketAddress remote = params.getClientAddress();
49 | *
50 | * SSLContext c = getSSLContext();
51 | *
52 | * // get the default parameters
53 | * SSLParameters sslparams = c.getDefaultSSLParameters();
54 | * if (remote.equals (...) ) {
55 | * // modify the default set for client x
56 | * }
57 | *
58 | * params.setSSLParameters(sslparams);
59 | * }
60 | * });
61 | *
62 | * @since 1.6
63 | */
64 | public class HttpsConfigurator {
65 |
66 | private SSLContext context;
67 |
68 | /**
69 | * Creates an Https configuration, with the given SSLContext.
70 | * @param context the SSLContext to use for this configurator
71 | * @throws NullPointerException if no SSLContext supplied
72 | */
73 | public HttpsConfigurator (SSLContext context) {
74 | if (context == null) {
75 | throw new NullPointerException ("null SSLContext");
76 | }
77 | this.context = context;
78 | }
79 |
80 | /**
81 | * Returns the SSLContext for this HttpsConfigurator.
82 | * @return the SSLContext
83 | */
84 | public SSLContext getSSLContext() {
85 | return context;
86 | }
87 |
88 | //BEGIN_TIGER_EXCLUDE
89 | /**
90 | * Called by the HttpsServer to configure the parameters
91 | * for a https connection currently being established.
92 | * The implementation of configure() must call
93 | * {@link HttpsParameters#setSSLParameters(SSLParameters)}
94 | * in order to set the SSL parameters for the connection.
95 | *
96 | * The default implementation of this method uses the
97 | * SSLParameters returned from
98 | * getSSLContext().getDefaultSSLParameters()
99 | *
100 | * configure() may be overridden in order to modify this behavior.
101 | * See, the example above.
102 | * @param params the HttpsParameters to be configured.
103 | *
104 | * @since 1.6
105 | */
106 | public void configure (HttpsParameters params) {
107 | params.setSSLParameters (getSSLContext().getDefaultSSLParameters());
108 | }
109 | //END_TIGER_EXCLUDE
110 | }
111 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/HttpsExchange.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | import javax.net.ssl.SSLSession;
29 |
30 | /**
31 | * This class encapsulates a HTTPS request received and a
32 | * response to be generated in one exchange and defines
33 | * the extensions to HttpExchange that are specific to the HTTPS protocol.
34 | * @since 1.6
35 | */
36 |
37 | public abstract class HttpsExchange extends HttpExchange {
38 |
39 | protected HttpsExchange () {
40 | }
41 |
42 | /**
43 | * Get the SSLSession for this exchange.
44 | * @return the SSLSession
45 | */
46 | public abstract SSLSession getSSLSession ();
47 | }
48 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/HttpsServer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.com.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.net.BindException;
30 | import java.net.InetSocketAddress;
31 | import java.util.Map;
32 |
33 | import org.jboss.com.sun.net.httpserver.spi.HttpServerProvider;
34 |
35 | /**
36 | * This class is an extension of {@link HttpServer} which provides
37 | * support for HTTPS.
38 | * A HttpsServer must have an associated {@link HttpsConfigurator} object
39 | * which is used to establish the SSL configuration for the SSL connections.
40 | *
41 | * All other configuration is the same as for HttpServer.
42 | * @since 1.6
43 | */
44 |
45 | public abstract class HttpsServer extends HttpServer {
46 |
47 | /**
48 | */
49 | protected HttpsServer () {
50 | }
51 |
52 | /**
53 | * creates a HttpsServer instance which is initially not bound to any local address/port.
54 | * The HttpsServer is acquired from the currently installed {@link HttpServerProvider}
55 | * The server must be bound using {@link #bind(InetSocketAddress,int)} before it can be used.
56 | * The server must also have a HttpsConfigurator established with {@link #setHttpsConfigurator(HttpsConfigurator)}
57 | * @throws IOException
58 | */
59 | public static HttpsServer create () throws IOException {
60 | return create (null, 0);
61 | }
62 |
63 | /**
64 | * Create a HttpsServer
instance which will bind to the
65 | * specified {@link java.net.InetSocketAddress} (IP address and port number)
66 | *
67 | * A maximum backlog can also be specified. This is the maximum number of
68 | * queued incoming connections to allow on the listening socket.
69 | * Queued TCP connections exceeding this limit may be rejected by the TCP implementation.
70 | * The HttpsServer is acquired from the currently installed {@link HttpServerProvider}
71 | * The server must have a HttpsConfigurator established with {@link #setHttpsConfigurator(HttpsConfigurator)}
72 | *
73 | * @param addr the address to listen on, if null
then bind() must be called
74 | * to set the address
75 | * @param backlog the socket backlog. If this value is less than or equal to zero,
76 | * then a system default value is used.
77 | * @throws BindException if the server cannot bind to the requested address,
78 | * or if the server is already bound.
79 | * @throws IOException
80 | */
81 | public static HttpsServer create(InetSocketAddress addr, int backlog) throws IOException {
82 | return create(addr, backlog, null);
83 | }
84 |
85 | /**
86 | * Create a HttpsServer
instance which will bind to the
87 | * specified {@link java.net.InetSocketAddress} (IP address and port number)
88 | *
89 | * A maximum backlog can also be specified. This is the maximum number of
90 | * queued incoming connections to allow on the listening socket.
91 | * Queued TCP connections exceeding this limit may be rejected by the TCP implementation.
92 | * The HttpsServer is acquired from the currently installed {@link HttpServerProvider}
93 | * The server must have a HttpsConfigurator established with {@link #setHttpsConfigurator(HttpsConfigurator)}
94 | *
95 | * @param addr the address to listen on, if null
then bind() must be called
96 | * to set the address
97 | * @param backlog the socket backlog. If this value is less than or equal to zero,
98 | * then a system default value is used.
99 | * @param configuration, instance specific configuration for this server.
100 | * @throws BindException if the server cannot bind to the requested address,
101 | * or if the server is already bound.
102 | * @throws IOException
103 | */
104 | public static HttpsServer create(InetSocketAddress addr, int backlog, Map configuration) throws IOException {
105 | HttpServerProvider provider = HttpServerProvider.provider();
106 | return provider.createHttpsServer(addr, backlog, configuration);
107 | }
108 |
109 | /**
110 | * Sets this server's {@link HttpsConfigurator} object.
111 | * @param config the HttpsConfigurator to set
112 | * @throws NullPointerException if config is null.
113 | */
114 | public abstract void setHttpsConfigurator (HttpsConfigurator config) ;
115 |
116 | /**
117 | * Gets this server's {@link HttpsConfigurator} object, if it has been set.
118 | * @return the HttpsConfigurator for this server, or null
if not set.
119 | */
120 | public abstract HttpsConfigurator getHttpsConfigurator ();
121 | }
122 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/com/sun/net/httpserver/spi/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | /**
27 | Provides a pluggable service provider interface, which allows the HTTP server
28 | implementation to be replaced with other implementations.
29 | */
30 | package org.jboss.com.sun.net.httpserver.spi;
31 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/AuthFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.io.InputStream;
30 |
31 | import org.jboss.com.sun.net.httpserver.Authenticator;
32 | import org.jboss.com.sun.net.httpserver.Filter;
33 | import org.jboss.com.sun.net.httpserver.HttpExchange;
34 |
35 | public class AuthFilter extends Filter {
36 |
37 | private Authenticator authenticator;
38 |
39 | public AuthFilter (Authenticator authenticator) {
40 | this.authenticator = authenticator;
41 | }
42 |
43 | public String description () {
44 | return "Authentication filter";
45 | }
46 |
47 | public void setAuthenticator (Authenticator a) {
48 | authenticator = a;
49 | }
50 |
51 | public void consumeInput (HttpExchange t) throws IOException {
52 | InputStream i = t.getRequestBody();
53 | byte[] b = new byte [4096];
54 | while (i.read (b) != -1);
55 | i.close ();
56 | }
57 |
58 | /**
59 | * The filter's implementation, which is invoked by the server
60 | */
61 | public void doFilter (HttpExchange t, Filter.Chain chain) throws IOException
62 | {
63 | if (authenticator != null) {
64 | Authenticator.Result r = authenticator.authenticate (t);
65 | if (r instanceof Authenticator.Success) {
66 | Authenticator.Success s = (Authenticator.Success)r;
67 | ExchangeImpl e = ExchangeImpl.get (t);
68 | e.setPrincipal (s.getPrincipal());
69 | chain.doFilter (t);
70 | } else if (r instanceof Authenticator.Retry) {
71 | Authenticator.Retry ry = (Authenticator.Retry)r;
72 | consumeInput (t);
73 | t.sendResponseHeaders (ry.getResponseCode(), -1);
74 | } else if (r instanceof Authenticator.Failure) {
75 | Authenticator.Failure f = (Authenticator.Failure)r;
76 | consumeInput (t);
77 | t.sendResponseHeaders (f.getResponseCode(), -1);
78 | }
79 | } else {
80 | chain.doFilter (t);
81 | }
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/ChunkedOutputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2008, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.FilterOutputStream;
29 | import java.io.IOException;
30 | import java.io.OutputStream;
31 |
32 | /**
33 | * a class which allows the caller to write an arbitrary
34 | * number of bytes to an underlying stream.
35 | * normal close() does not close the underlying stream
36 | *
37 | * This class is buffered.
38 | *
39 | * Each chunk is written in one go as :-
40 | * abcd\r\nxxxxxxxxxxxxxx\r\n
41 | *
42 | * abcd is the chunk-size, and xxx is the chunk data
43 | * If the length is less than 4 chars (in size) then the buffer
44 | * is written with an offset.
45 | * Final chunk is:
46 | * 0\r\n\r\n
47 | */
48 |
49 | class ChunkedOutputStream extends FilterOutputStream
50 | {
51 | private boolean closed = false;
52 | /* max. amount of user data per chunk */
53 | final static int CHUNK_SIZE = 4096;
54 | /* allow 4 bytes for chunk-size plus 4 for CRLFs */
55 | final static int OFFSET = 6; /* initial <=4 bytes for len + CRLF */
56 | private int pos = OFFSET;
57 | private int count = 0;
58 | private byte[] buf = new byte [CHUNK_SIZE+OFFSET+2];
59 | ExchangeImpl t;
60 |
61 | ChunkedOutputStream (ExchangeImpl t, OutputStream src) {
62 | super (src);
63 | this.t = t;
64 | }
65 |
66 | public void write (int b) throws IOException {
67 | if (closed) {
68 | throw new StreamClosedException ();
69 | }
70 | buf [pos++] = (byte)b;
71 | count ++;
72 | if (count == CHUNK_SIZE) {
73 | writeChunk();
74 | }
75 | //DISABLED assert count < CHUNK_SIZE;
76 | }
77 |
78 | public void write (byte[]b, int off, int len) throws IOException {
79 | if (closed) {
80 | throw new StreamClosedException ();
81 | }
82 | int remain = CHUNK_SIZE - count;
83 | if (len > remain) {
84 | System.arraycopy (b,off,buf,pos,remain);
85 | count = CHUNK_SIZE;
86 | writeChunk();
87 | len -= remain;
88 | off += remain;
89 | while (len >= CHUNK_SIZE) {
90 | System.arraycopy (b,off,buf,OFFSET,CHUNK_SIZE);
91 | len -= CHUNK_SIZE;
92 | off += CHUNK_SIZE;
93 | count = CHUNK_SIZE;
94 | writeChunk();
95 | }
96 | }
97 | if (len > 0) {
98 | System.arraycopy (b,off,buf,pos,len);
99 | count += len;
100 | pos += len;
101 | }
102 | if (count == CHUNK_SIZE) {
103 | writeChunk();
104 | }
105 | }
106 |
107 | /**
108 | * write out a chunk , and reset the pointers
109 | * chunk does not have to be CHUNK_SIZE bytes
110 | * count must == number of user bytes (<= CHUNK_SIZE)
111 | */
112 | private void writeChunk () throws IOException {
113 | char[] c = Integer.toHexString (count).toCharArray();
114 | int clen = c.length;
115 | int startByte = 4 - clen;
116 | int i;
117 | for (i=0; i 0) {
158 | writeChunk();
159 | }
160 | out.flush();
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/Code.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | class Code {
29 |
30 | public static final int HTTP_CONTINUE = 100;
31 | public static final int HTTP_OK = 200;
32 | public static final int HTTP_CREATED = 201;
33 | public static final int HTTP_ACCEPTED = 202;
34 | public static final int HTTP_NOT_AUTHORITATIVE = 203;
35 | public static final int HTTP_NO_CONTENT = 204;
36 | public static final int HTTP_RESET = 205;
37 | public static final int HTTP_PARTIAL = 206;
38 | public static final int HTTP_MULT_CHOICE = 300;
39 | public static final int HTTP_MOVED_PERM = 301;
40 | public static final int HTTP_MOVED_TEMP = 302;
41 | public static final int HTTP_SEE_OTHER = 303;
42 | public static final int HTTP_NOT_MODIFIED = 304;
43 | public static final int HTTP_USE_PROXY = 305;
44 | public static final int HTTP_BAD_REQUEST = 400;
45 | public static final int HTTP_UNAUTHORIZED = 401;
46 | public static final int HTTP_PAYMENT_REQUIRED = 402;
47 | public static final int HTTP_FORBIDDEN = 403;
48 | public static final int HTTP_NOT_FOUND = 404;
49 | public static final int HTTP_BAD_METHOD = 405;
50 | public static final int HTTP_NOT_ACCEPTABLE = 406;
51 | public static final int HTTP_PROXY_AUTH = 407;
52 | public static final int HTTP_CLIENT_TIMEOUT = 408;
53 | public static final int HTTP_CONFLICT = 409;
54 | public static final int HTTP_GONE = 410;
55 | public static final int HTTP_LENGTH_REQUIRED = 411;
56 | public static final int HTTP_PRECON_FAILED = 412;
57 | public static final int HTTP_ENTITY_TOO_LARGE = 413;
58 | public static final int HTTP_REQ_TOO_LONG = 414;
59 | public static final int HTTP_UNSUPPORTED_TYPE = 415;
60 | public static final int HTTP_INTERNAL_ERROR = 500;
61 | public static final int HTTP_NOT_IMPLEMENTED = 501;
62 | public static final int HTTP_BAD_GATEWAY = 502;
63 | public static final int HTTP_UNAVAILABLE = 503;
64 | public static final int HTTP_GATEWAY_TIMEOUT = 504;
65 | public static final int HTTP_VERSION = 505;
66 |
67 | static String msg (int code) {
68 |
69 | switch (code) {
70 | case HTTP_OK: return " OK";
71 | case HTTP_CONTINUE: return " Continue";
72 | case HTTP_CREATED: return " Created";
73 | case HTTP_ACCEPTED: return " Accepted";
74 | case HTTP_NOT_AUTHORITATIVE: return " Non-Authoritative Information";
75 | case HTTP_NO_CONTENT: return " No Content";
76 | case HTTP_RESET: return " Reset Content";
77 | case HTTP_PARTIAL: return " Partial Content";
78 | case HTTP_MULT_CHOICE: return " Multiple Choices";
79 | case HTTP_MOVED_PERM: return " Moved Permanently";
80 | case HTTP_MOVED_TEMP: return " Temporary Redirect";
81 | case HTTP_SEE_OTHER: return " See Other";
82 | case HTTP_NOT_MODIFIED: return " Not Modified";
83 | case HTTP_USE_PROXY: return " Use Proxy";
84 | case HTTP_BAD_REQUEST: return " Bad Request";
85 | case HTTP_UNAUTHORIZED: return " Unauthorized" ;
86 | case HTTP_PAYMENT_REQUIRED: return " Payment Required";
87 | case HTTP_FORBIDDEN: return " Forbidden";
88 | case HTTP_NOT_FOUND: return " Not Found";
89 | case HTTP_BAD_METHOD: return " Method Not Allowed";
90 | case HTTP_NOT_ACCEPTABLE: return " Not Acceptable";
91 | case HTTP_PROXY_AUTH: return " Proxy Authentication Required";
92 | case HTTP_CLIENT_TIMEOUT: return " Request Time-Out";
93 | case HTTP_CONFLICT: return " Conflict";
94 | case HTTP_GONE: return " Gone";
95 | case HTTP_LENGTH_REQUIRED: return " Length Required";
96 | case HTTP_PRECON_FAILED: return " Precondition Failed";
97 | case HTTP_ENTITY_TOO_LARGE: return " Request Entity Too Large";
98 | case HTTP_REQ_TOO_LONG: return " Request-URI Too Large";
99 | case HTTP_UNSUPPORTED_TYPE: return " Unsupported Media Type";
100 | case HTTP_INTERNAL_ERROR: return " Internal Server Error";
101 | case HTTP_NOT_IMPLEMENTED: return " Not Implemented";
102 | case HTTP_BAD_GATEWAY: return " Bad Gateway";
103 | case HTTP_UNAVAILABLE: return " Service Unavailable";
104 | case HTTP_GATEWAY_TIMEOUT: return " Gateway Timeout";
105 | case HTTP_VERSION: return " HTTP Version Not Supported";
106 | default: return "";
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/ContextList.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.util.LinkedList;
29 |
30 | class ContextList {
31 |
32 | final static int MAX_CONTEXTS = 50;
33 |
34 | LinkedList list = new LinkedList();
35 |
36 | public synchronized void add (HttpContextImpl ctx) {
37 | //DISABLED assert ctx.getPath() != null;
38 | list.add (ctx);
39 | }
40 |
41 | public synchronized int size () {
42 | return list.size();
43 | }
44 |
45 | /* initially contexts are located only by protocol:path.
46 | * Context with longest prefix matches (currently case-sensitive)
47 | */
48 | synchronized HttpContextImpl findContext (String protocol, String path) {
49 | return findContext (protocol, path, false);
50 | }
51 |
52 | synchronized HttpContextImpl findContext (String protocol, String path, boolean exact) {
53 | protocol = protocol.toLowerCase();
54 | String longest = "";
55 | HttpContextImpl lc = null;
56 | for (HttpContextImpl ctx: list) {
57 | if (!ctx.getProtocol().equals(protocol)) {
58 | continue;
59 | }
60 | String cpath = ctx.getPath();
61 | if (exact && !cpath.equals (path)) {
62 | continue;
63 | } else if (!exact && !path.startsWith(cpath)) {
64 | continue;
65 | }
66 | if (cpath.length() > longest.length()) {
67 | longest = cpath;
68 | lc = ctx;
69 | }
70 | }
71 | return lc;
72 | }
73 |
74 | public synchronized void remove (String protocol, String path)
75 | throws IllegalArgumentException
76 | {
77 | HttpContextImpl ctx = findContext (protocol, path, true);
78 | if (ctx == null) {
79 | throw new IllegalArgumentException ("cannot remove element from list");
80 | }
81 | list.remove (ctx);
82 | }
83 |
84 | public synchronized void remove (HttpContextImpl context)
85 | throws IllegalArgumentException
86 | {
87 | for (HttpContextImpl ctx: list) {
88 | if (ctx.equals (context)) {
89 | list.remove (ctx);
90 | return;
91 | }
92 | }
93 | throw new IllegalArgumentException ("no such context in list");
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/DefaultHttpServerProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.net.InetSocketAddress;
30 | import java.util.Map;
31 |
32 | import org.jboss.com.sun.net.httpserver.HttpServer;
33 | import org.jboss.com.sun.net.httpserver.HttpsServer;
34 | import org.jboss.com.sun.net.httpserver.spi.HttpServerProvider;
35 |
36 | public class DefaultHttpServerProvider extends HttpServerProvider {
37 | public HttpServer createHttpServer (InetSocketAddress addr, int backlog, Map configuration) throws IOException {
38 | return new HttpServerImpl (addr, backlog, configuration);
39 | }
40 |
41 | public HttpsServer createHttpsServer (InetSocketAddress addr, int backlog, Map configuration) throws IOException {
42 | return new HttpsServerImpl (addr, backlog, configuration);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/Event.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2010, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | class Event {
29 |
30 | ExchangeImpl exchange;
31 |
32 | protected Event (ExchangeImpl t) {
33 | this.exchange = t;
34 | }
35 | }
36 |
37 | class WriteFinishedEvent extends Event {
38 | WriteFinishedEvent (ExchangeImpl t) {
39 | super (t);
40 | //DISABLED assert !t.writefinished;
41 | t.writefinished = true;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/FixedLengthInputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2010, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.io.InputStream;
30 |
31 | /**
32 | * a class which allows the caller to read up to a defined
33 | * number of bytes off an underlying stream
34 | * close() does not close the underlying stream
35 | */
36 |
37 | class FixedLengthInputStream extends LeftOverInputStream {
38 | private long remaining;
39 |
40 | FixedLengthInputStream (ExchangeImpl t, InputStream src, long len) {
41 | super (t, src);
42 | this.remaining = len;
43 | }
44 |
45 | protected int readImpl (byte[]b, int off, int len) throws IOException {
46 |
47 | eof = (remaining == 0L);
48 | if (eof) {
49 | return -1;
50 | }
51 | if (len > remaining) {
52 | len = (int)remaining;
53 | }
54 | int n = in.read(b, off, len);
55 | if (n > -1) {
56 | remaining -= n;
57 | if (remaining == 0) {
58 | t.getServerImpl().requestCompleted (t.getConnection());
59 | }
60 | }
61 | return n;
62 | }
63 |
64 | public int available () throws IOException {
65 | if (eof) {
66 | return 0;
67 | }
68 | int n = in.available();
69 | return n < remaining? n: (int)remaining;
70 | }
71 |
72 | public boolean markSupported () {return false;}
73 |
74 | public void mark (int l) {
75 | }
76 |
77 | public void reset () throws IOException {
78 | throw new IOException ("mark/reset not supported");
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/FixedLengthOutputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.FilterOutputStream;
29 | import java.io.IOException;
30 | import java.io.OutputStream;
31 |
32 | /**
33 | * a class which allows the caller to write up to a defined
34 | * number of bytes to an underlying stream. The caller *must*
35 | * write the pre-defined number or else an exception will be thrown
36 | * and the whole request aborted.
37 | * normal close() does not close the underlying stream
38 | */
39 |
40 | class FixedLengthOutputStream extends FilterOutputStream
41 | {
42 | private long remaining;
43 | private boolean eof = false;
44 | private boolean closed = false;
45 | ExchangeImpl t;
46 |
47 | FixedLengthOutputStream (ExchangeImpl t, OutputStream src, long len) {
48 | super (src);
49 | this.t = t;
50 | this.remaining = len;
51 | }
52 |
53 | public void write (int b) throws IOException {
54 | if (closed) {
55 | throw new IOException ("stream closed");
56 | }
57 | eof = (remaining == 0);
58 | if (eof) {
59 | throw new StreamClosedException();
60 | }
61 | out.write(b);
62 | remaining --;
63 | }
64 |
65 | public void write (byte[]b, int off, int len) throws IOException {
66 | if (closed) {
67 | throw new IOException ("stream closed");
68 | }
69 | eof = (remaining == 0);
70 | if (eof) {
71 | throw new StreamClosedException();
72 | }
73 | if (len > remaining) {
74 | // stream is still open, caller can retry
75 | throw new IOException ("too many bytes to write to stream");
76 | }
77 | out.write(b, off, len);
78 | remaining -= len;
79 | }
80 |
81 | public void close () throws IOException {
82 | if (closed) {
83 | return;
84 | }
85 | closed = true;
86 | if (remaining > 0) {
87 | t.close();
88 | throw new IOException ("insufficient bytes written to stream");
89 | }
90 | flush();
91 | eof = true;
92 | LeftOverInputStream is = t.getOriginalInputStream();
93 | if (!is.isClosed()) {
94 | try {
95 | is.close();
96 | } catch (IOException e) {}
97 | }
98 | WriteFinishedEvent e = new WriteFinishedEvent (t);
99 | t.getHttpContext().getServerImpl().addEvent (e);
100 | }
101 |
102 | // flush is a pass-through
103 | }
104 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/HttpContextImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.util.HashMap;
29 | import java.util.LinkedList;
30 | import java.util.List;
31 | import java.util.Map;
32 | import java.util.logging.Logger;
33 |
34 | import org.jboss.com.sun.net.httpserver.Authenticator;
35 | import org.jboss.com.sun.net.httpserver.Filter;
36 | import org.jboss.com.sun.net.httpserver.HttpContext;
37 | import org.jboss.com.sun.net.httpserver.HttpHandler;
38 | import org.jboss.com.sun.net.httpserver.HttpServer;
39 |
40 | /**
41 | * HttpContext represents a mapping between a protocol (http or https) together with a root URI path
42 | * to a {@link HttpHandler} which is invoked to handle requests destined
43 | * for the protocol/path on the associated HttpServer.
44 | *
45 | * HttpContext instances are created by {@link HttpServer#createContext(String,String,HttpHandler,Object)}
46 | *
47 | */
48 | class HttpContextImpl extends HttpContext {
49 |
50 | private String path;
51 | private String protocol;
52 | private HttpHandler handler;
53 | private Map attributes = new HashMap();
54 | private ServerImpl server;
55 | /* system filters, not visible to applications */
56 | private LinkedList sfilters = new LinkedList();
57 | /* user filters, set by applications */
58 | private LinkedList ufilters = new LinkedList();
59 | private Authenticator authenticator;
60 | private AuthFilter authfilter;
61 |
62 | /**
63 | * constructor is package private.
64 | */
65 | HttpContextImpl (String protocol, String path, HttpHandler cb, ServerImpl server) {
66 | if (path == null || protocol == null || path.length() < 1 || path.charAt(0) != '/') {
67 | throw new IllegalArgumentException ("Illegal value for path or protocol");
68 | }
69 | this.protocol = protocol.toLowerCase();
70 | this.path = path;
71 | if (!this.protocol.equals ("http") && !this.protocol.equals ("https")) {
72 | throw new IllegalArgumentException ("Illegal value for protocol");
73 | }
74 | this.handler = cb;
75 | this.server = server;
76 | authfilter = new AuthFilter(null);
77 | sfilters.add (authfilter);
78 | }
79 |
80 | /**
81 | * returns the handler for this context
82 | * @return the HttpHandler for this context
83 | */
84 | public HttpHandler getHandler () {
85 | return handler;
86 | }
87 |
88 | public void setHandler (HttpHandler h) {
89 | if (h == null) {
90 | throw new NullPointerException ("Null handler parameter");
91 | }
92 | if (handler != null) {
93 | throw new IllegalArgumentException ("handler already set");
94 | }
95 | handler = h;
96 | }
97 |
98 | /**
99 | * returns the path this context was created with
100 | * @return this context's path
101 | */
102 | public String getPath() {
103 | return path;
104 | }
105 |
106 | /**
107 | * returns the server this context was created with
108 | * @return this context's server
109 | */
110 | public HttpServer getServer () {
111 | return server.getWrapper();
112 | }
113 |
114 | ServerImpl getServerImpl () {
115 | return server;
116 | }
117 |
118 | /**
119 | * returns the protocol this context was created with
120 | * @return this context's path
121 | */
122 | public String getProtocol() {
123 | return protocol;
124 | }
125 |
126 | /**
127 | * returns a mutable Map, which can be used to pass
128 | * configuration and other data to Filter modules
129 | * and to the context's exchange handler.
130 | *
131 | * Every attribute stored in this Map will be visible to
132 | * every HttpExchange processed by this context
133 | */
134 | public Map getAttributes() {
135 | return attributes;
136 | }
137 |
138 | public List getFilters () {
139 | return ufilters;
140 | }
141 |
142 | List getSystemFilters () {
143 | return sfilters;
144 | }
145 |
146 | public Authenticator setAuthenticator (Authenticator auth) {
147 | Authenticator old = authenticator;
148 | authenticator = auth;
149 | authfilter.setAuthenticator (auth);
150 | return old;
151 | }
152 |
153 | public Authenticator getAuthenticator () {
154 | return authenticator;
155 | }
156 | Logger getLogger () {
157 | return server.getLogger();
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/HttpError.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2008, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | /**
29 | * A Http error
30 | */
31 | class HttpError extends RuntimeException {
32 | private static final long serialVersionUID = 8769596371344178179L;
33 |
34 | public HttpError (String msg) {
35 | super (msg);
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/HttpExchangeImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.io.InputStream;
30 | import java.io.OutputStream;
31 | import java.net.InetSocketAddress;
32 | import java.net.URI;
33 |
34 | import org.jboss.com.sun.net.httpserver.Headers;
35 | import org.jboss.com.sun.net.httpserver.HttpExchange;
36 | import org.jboss.com.sun.net.httpserver.HttpPrincipal;
37 |
38 | class HttpExchangeImpl extends HttpExchange {
39 |
40 | ExchangeImpl impl;
41 |
42 | HttpExchangeImpl (ExchangeImpl impl) {
43 | this.impl = impl;
44 | }
45 |
46 | public Headers getRequestHeaders () {
47 | return impl.getRequestHeaders();
48 | }
49 |
50 | public Headers getResponseHeaders () {
51 | return impl.getResponseHeaders();
52 | }
53 |
54 | public URI getRequestURI () {
55 | return impl.getRequestURI();
56 | }
57 |
58 | public String getRequestMethod (){
59 | return impl.getRequestMethod();
60 | }
61 |
62 | public HttpContextImpl getHttpContext (){
63 | return impl.getHttpContext();
64 | }
65 |
66 | public void close () {
67 | impl.close();
68 | }
69 |
70 | public InputStream getRequestBody () {
71 | return impl.getRequestBody();
72 | }
73 |
74 | public int getResponseCode () {
75 | return impl.getResponseCode();
76 | }
77 |
78 | public OutputStream getResponseBody () {
79 | return impl.getResponseBody();
80 | }
81 |
82 |
83 | public void sendResponseHeaders (int rCode, long contentLen)
84 | throws IOException
85 | {
86 | impl.sendResponseHeaders (rCode, contentLen);
87 | }
88 |
89 | public InetSocketAddress getRemoteAddress (){
90 | return impl.getRemoteAddress();
91 | }
92 |
93 | public InetSocketAddress getLocalAddress (){
94 | return impl.getLocalAddress();
95 | }
96 |
97 | public String getProtocol (){
98 | return impl.getProtocol();
99 | }
100 |
101 | public Object getAttribute (String name) {
102 | return impl.getAttribute (name);
103 | }
104 |
105 | public Object getAttribute(String name, HttpExchange.AttributeScope scope) {
106 | return impl.getAttribute(name, scope);
107 | }
108 |
109 | public void setAttribute (String name, Object value) {
110 | impl.setAttribute (name, value);
111 | }
112 |
113 | public void setAttribute(String name, Object value, HttpExchange.AttributeScope scope) {
114 | impl.setAttribute(name, value, scope);
115 | }
116 |
117 |
118 | public void setStreams (InputStream i, OutputStream o) {
119 | impl.setStreams (i, o);
120 | }
121 |
122 | public HttpPrincipal getPrincipal () {
123 | return impl.getPrincipal();
124 | }
125 |
126 | ExchangeImpl getExchangeImpl () {
127 | return impl;
128 | }
129 | }
130 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/HttpServerImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.net.InetSocketAddress;
30 | import java.util.Map;
31 | import java.util.concurrent.Executor;
32 |
33 | import org.jboss.com.sun.net.httpserver.HttpContext;
34 | import org.jboss.com.sun.net.httpserver.HttpHandler;
35 | import org.jboss.com.sun.net.httpserver.HttpServer;
36 |
37 | public class HttpServerImpl extends HttpServer {
38 |
39 | ServerImpl server;
40 |
41 | HttpServerImpl () throws IOException {
42 | this (new InetSocketAddress(80), 0);
43 | }
44 |
45 | HttpServerImpl (
46 | InetSocketAddress addr, int backlog
47 | ) throws IOException {
48 | this(addr, backlog, null);
49 | }
50 |
51 | HttpServerImpl (
52 | InetSocketAddress addr, int backlog, Map configuration
53 | ) throws IOException {
54 | server = new ServerImpl (this, "http", addr, backlog, configuration);
55 | }
56 |
57 |
58 | public void bind (InetSocketAddress addr, int backlog) throws IOException {
59 | server.bind (addr, backlog);
60 | }
61 |
62 | public void start () {
63 | server.start();
64 | }
65 |
66 | public void setExecutor (Executor executor) {
67 | server.setExecutor(executor);
68 | }
69 |
70 | public Executor getExecutor () {
71 | return server.getExecutor();
72 | }
73 |
74 | public void stop (int delay) {
75 | server.stop (delay);
76 | }
77 |
78 | public HttpContextImpl createContext (String path, HttpHandler handler) {
79 | return server.createContext (path, handler);
80 | }
81 |
82 | public HttpContextImpl createContext (String path) {
83 | return server.createContext (path);
84 | }
85 |
86 | public void removeContext (String path) throws IllegalArgumentException {
87 | server.removeContext (path);
88 | }
89 |
90 | public void removeContext (HttpContext context) throws IllegalArgumentException {
91 | server.removeContext (context);
92 | }
93 |
94 | public InetSocketAddress getAddress() {
95 | return server.getAddress();
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/HttpsExchangeImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import javax.net.ssl.SSLSession;
29 | import java.io.IOException;
30 | import java.io.InputStream;
31 | import java.io.OutputStream;
32 | import java.net.InetSocketAddress;
33 | import java.net.URI;
34 |
35 | import org.jboss.com.sun.net.httpserver.Headers;
36 | import org.jboss.com.sun.net.httpserver.HttpExchange;
37 | import org.jboss.com.sun.net.httpserver.HttpPrincipal;
38 | import org.jboss.com.sun.net.httpserver.HttpsExchange;
39 |
40 | class HttpsExchangeImpl extends HttpsExchange {
41 |
42 | ExchangeImpl impl;
43 |
44 | HttpsExchangeImpl (ExchangeImpl impl) throws IOException {
45 | this.impl = impl;
46 | }
47 |
48 | public Headers getRequestHeaders () {
49 | return impl.getRequestHeaders();
50 | }
51 |
52 | public Headers getResponseHeaders () {
53 | return impl.getResponseHeaders();
54 | }
55 |
56 | public URI getRequestURI () {
57 | return impl.getRequestURI();
58 | }
59 |
60 | public String getRequestMethod (){
61 | return impl.getRequestMethod();
62 | }
63 |
64 | public HttpContextImpl getHttpContext (){
65 | return impl.getHttpContext();
66 | }
67 |
68 | public void close () {
69 | impl.close();
70 | }
71 |
72 | public InputStream getRequestBody () {
73 | return impl.getRequestBody();
74 | }
75 |
76 | public int getResponseCode () {
77 | return impl.getResponseCode();
78 | }
79 |
80 | public OutputStream getResponseBody () {
81 | return impl.getResponseBody();
82 | }
83 |
84 |
85 | public void sendResponseHeaders (int rCode, long contentLen)
86 | throws IOException
87 | {
88 | impl.sendResponseHeaders (rCode, contentLen);
89 | }
90 |
91 | public InetSocketAddress getRemoteAddress (){
92 | return impl.getRemoteAddress();
93 | }
94 |
95 | public InetSocketAddress getLocalAddress (){
96 | return impl.getLocalAddress();
97 | }
98 |
99 | public String getProtocol (){
100 | return impl.getProtocol();
101 | }
102 |
103 | public SSLSession getSSLSession () {
104 | return impl.getSSLSession();
105 | }
106 |
107 | public Object getAttribute (String name) {
108 | return impl.getAttribute(name);
109 | }
110 |
111 | public Object getAttribute(String name, HttpExchange.AttributeScope scope) {
112 | return impl.getAttribute(name, scope);
113 | }
114 |
115 | public void setAttribute (String name, Object value) {
116 | impl.setAttribute (name, value);
117 | }
118 |
119 | public void setAttribute(String name, Object value, HttpExchange.AttributeScope scope) {
120 | impl.setAttribute(name, value);
121 | }
122 |
123 | public void setStreams (InputStream i, OutputStream o) {
124 | impl.setStreams (i, o);
125 | }
126 |
127 | public HttpPrincipal getPrincipal () {
128 | return impl.getPrincipal();
129 | }
130 |
131 | ExchangeImpl getExchangeImpl () {
132 | return impl;
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/HttpsServerImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 | import java.net.InetSocketAddress;
30 | import java.util.Map;
31 | import java.util.concurrent.Executor;
32 |
33 | import org.jboss.com.sun.net.httpserver.HttpContext;
34 | import org.jboss.com.sun.net.httpserver.HttpHandler;
35 | import org.jboss.com.sun.net.httpserver.HttpsConfigurator;
36 | import org.jboss.com.sun.net.httpserver.HttpsServer;
37 |
38 | public class HttpsServerImpl extends HttpsServer {
39 |
40 | ServerImpl server;
41 |
42 | HttpsServerImpl () throws IOException {
43 | this (new InetSocketAddress(443), 0);
44 | }
45 |
46 | HttpsServerImpl (
47 | InetSocketAddress addr, int backlog
48 | ) throws IOException {
49 | this(addr, backlog, null);
50 | }
51 |
52 | HttpsServerImpl(InetSocketAddress addr, int backlog, Map configuration) throws IOException {
53 | server = new ServerImpl(this, "https", addr, backlog, configuration);
54 | }
55 |
56 | public void setHttpsConfigurator (HttpsConfigurator config) {
57 | server.setHttpsConfigurator (config);
58 | }
59 |
60 | public HttpsConfigurator getHttpsConfigurator () {
61 | return server.getHttpsConfigurator();
62 | }
63 |
64 | public void bind (InetSocketAddress addr, int backlog) throws IOException {
65 | server.bind (addr, backlog);
66 | }
67 |
68 | public void start () {
69 | server.start();
70 | }
71 |
72 | public void setExecutor (Executor executor) {
73 | server.setExecutor(executor);
74 | }
75 |
76 | public Executor getExecutor () {
77 | return server.getExecutor();
78 | }
79 |
80 | public void stop (int delay) {
81 | server.stop (delay);
82 | }
83 |
84 | public HttpContextImpl createContext (String path, HttpHandler handler) {
85 | return server.createContext (path, handler);
86 | }
87 |
88 | public HttpContextImpl createContext (String path) {
89 | return server.createContext (path);
90 | }
91 |
92 | public void removeContext (String path) throws IllegalArgumentException {
93 | server.removeContext (path);
94 | }
95 |
96 | public void removeContext (HttpContext context) throws IllegalArgumentException {
97 | server.removeContext (context);
98 | }
99 |
100 | public InetSocketAddress getAddress() {
101 | return server.getAddress();
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/LeftOverInputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2007, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.FilterInputStream;
29 | import java.io.IOException;
30 | import java.io.InputStream;
31 |
32 | /**
33 | * a (filter) input stream which can tell us if bytes are "left over"
34 | * on the underlying stream which can be read (without blocking)
35 | * on another instance of this class.
36 | *
37 | * The class can also report if all bytes "expected" to be read
38 | * were read, by the time close() was called. In that case,
39 | * bytes may be drained to consume them (by calling drain() ).
40 | *
41 | * isEOF() returns true, when all expected bytes have been read
42 | */
43 | abstract class LeftOverInputStream extends FilterInputStream {
44 | ExchangeImpl t;
45 | ServerImpl server;
46 | protected boolean closed = false;
47 | protected boolean eof = false;
48 | byte[] one = new byte [1];
49 |
50 | public LeftOverInputStream (ExchangeImpl t, InputStream src) {
51 | super (src);
52 | this.t = t;
53 | this.server = t.getServerImpl();
54 | }
55 | /**
56 | * if bytes are left over buffered on *the UNDERLYING* stream
57 | */
58 | public boolean isDataBuffered () throws IOException {
59 | //DISABLED assert eof;
60 | return super.available() > 0;
61 | }
62 |
63 | public void close () throws IOException {
64 | if (closed) {
65 | return;
66 | }
67 | closed = true;
68 | if (!eof) {
69 | eof = drain (t.getServerConfig().getDrainAmount());
70 | }
71 | }
72 |
73 | public boolean isClosed () {
74 | return closed;
75 | }
76 |
77 | public boolean isEOF () {
78 | return eof;
79 | }
80 |
81 | protected abstract int readImpl (byte[]b, int off, int len) throws IOException;
82 |
83 | public synchronized int read () throws IOException {
84 | if (closed) {
85 | throw new IOException ("Stream is closed");
86 | }
87 | int c = readImpl (one, 0, 1);
88 | if (c == -1 || c == 0) {
89 | return c;
90 | } else {
91 | return one[0] & 0xFF;
92 | }
93 | }
94 |
95 | public synchronized int read (byte[]b, int off, int len) throws IOException {
96 | if (closed) {
97 | throw new IOException ("Stream is closed");
98 | }
99 | return readImpl (b, off, len);
100 | }
101 |
102 | /**
103 | * read and discard up to l bytes or "eof" occurs,
104 | * (whichever is first). Then return true if the stream
105 | * is at eof (ie. all bytes were read) or false if not
106 | * (still bytes to be read)
107 | */
108 | public boolean drain (long l) throws IOException {
109 | int bufSize = 2048;
110 | byte[] db = new byte [bufSize];
111 | while (l > 0) {
112 | long len = readImpl (db, 0, bufSize);
113 | if (len == -1) {
114 | eof = true;
115 | return true;
116 | } else {
117 | l = l - len;
118 | }
119 | }
120 | return false;
121 | }
122 | }
123 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/StreamClosedException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2008, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.IOException;
29 |
30 | class StreamClosedException extends IOException {
31 | private static final long serialVersionUID = -4485921499356327937L;
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/TimeSource.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | interface TimeSource {
29 | public long getTime();
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/UndefLengthOutputStream.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2007, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.io.FilterOutputStream;
29 | import java.io.IOException;
30 | import java.io.OutputStream;
31 |
32 | /**
33 | * a class which allows the caller to write an indefinite
34 | * number of bytes to an underlying stream , but without using
35 | * chunked encoding. Used for http/1.0 clients only
36 | * The underlying connection needs to be closed afterwards.
37 | */
38 |
39 | class UndefLengthOutputStream extends FilterOutputStream
40 | {
41 | private boolean closed = false;
42 | ExchangeImpl t;
43 |
44 | UndefLengthOutputStream (ExchangeImpl t, OutputStream src) {
45 | super (src);
46 | this.t = t;
47 | }
48 |
49 | public void write (int b) throws IOException {
50 | if (closed) {
51 | throw new IOException ("stream closed");
52 | }
53 | out.write(b);
54 | }
55 |
56 | public void write (byte[]b, int off, int len) throws IOException {
57 | if (closed) {
58 | throw new IOException ("stream closed");
59 | }
60 | out.write(b, off, len);
61 | }
62 |
63 | public void close () throws IOException {
64 | if (closed) {
65 | return;
66 | }
67 | closed = true;
68 | flush();
69 | LeftOverInputStream is = t.getOriginalInputStream();
70 | if (!is.isClosed()) {
71 | try {
72 | is.close();
73 | } catch (IOException e) {}
74 | }
75 | WriteFinishedEvent e = new WriteFinishedEvent (t);
76 | t.getHttpContext().getServerImpl().addEvent (e);
77 | }
78 |
79 | // flush is a pass-through
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/org/jboss/sun/net/httpserver/UnmodifiableHeaders.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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. Oracle designates this
8 | * particular file as subject to the "Classpath" exception as provided
9 | * by Oracle in the LICENSE file that accompanied this code.
10 | *
11 | * This code is distributed in the hope that it will be useful, but WITHOUT
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 | * version 2 for more details (a copy is included in the LICENSE file that
15 | * accompanied this code).
16 | *
17 | * You should have received a copy of the GNU General Public License version
18 | * 2 along with this work; if not, write to the Free Software Foundation,
19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 | *
21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 | * or visit www.oracle.com if you need additional information or have any
23 | * questions.
24 | */
25 |
26 | package org.jboss.sun.net.httpserver;
27 |
28 | import java.util.Collection;
29 | import java.util.Collections;
30 | import java.util.List;
31 | import java.util.Map;
32 | import java.util.Set;
33 |
34 | import org.jboss.com.sun.net.httpserver.Headers;
35 |
36 | class UnmodifiableHeaders extends Headers {
37 |
38 | Headers map;
39 |
40 | UnmodifiableHeaders(Headers map) {
41 | this.map = map;
42 | }
43 |
44 | public int size() {return map.size();}
45 |
46 | public boolean isEmpty() {return map.isEmpty();}
47 |
48 | public boolean containsKey(Object key) {
49 | return map.containsKey (key);
50 | }
51 |
52 | public boolean containsValue(Object value) {
53 | return map.containsValue(value);
54 | }
55 |
56 | public List get(Object key) {
57 | return map.get(key);
58 | }
59 |
60 | public String getFirst (String key) {
61 | return map.getFirst(key);
62 | }
63 |
64 |
65 | public List put(String key, List value) {
66 | return map.put (key, value);
67 | }
68 |
69 | public void add (String key, String value) {
70 | throw new UnsupportedOperationException ("unsupported operation");
71 | }
72 |
73 | public void set (String key, String value) {
74 | throw new UnsupportedOperationException ("unsupported operation");
75 | }
76 |
77 | public List remove(Object key) {
78 | throw new UnsupportedOperationException ("unsupported operation");
79 | }
80 |
81 | public void putAll(Map extends String,? extends List> t) {
82 | throw new UnsupportedOperationException ("unsupported operation");
83 | }
84 |
85 | public void clear() {
86 | throw new UnsupportedOperationException ("unsupported operation");
87 | }
88 |
89 | public Set keySet() {
90 | return Collections.unmodifiableSet (map.keySet());
91 | }
92 |
93 | public Collection> values() {
94 | return Collections.unmodifiableCollection(map.values());
95 | }
96 |
97 | /* TODO check that contents of set are not modifable : security */
98 |
99 | public Set>> entrySet() {
100 | return Collections.unmodifiableSet (map.entrySet());
101 | }
102 |
103 | public boolean equals(Object o) {return map.equals(o);}
104 |
105 | public int hashCode() {return map.hashCode();}
106 | }
107 |
--------------------------------------------------------------------------------
/src/test/java/B6339483.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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 6339483
27 | * @summary NullPointerException when creating a HttpContext with no handler
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.net.HttpURLConnection;
33 | import java.net.InetSocketAddress;
34 | import java.net.URL;
35 | import java.util.concurrent.ExecutorService;
36 | import java.util.concurrent.Executors;
37 |
38 | import org.jboss.com.sun.net.httpserver.HttpContext;
39 | import org.jboss.com.sun.net.httpserver.HttpServer;
40 |
41 | public class B6339483 {
42 |
43 | public static void main (String[] args) throws Exception {
44 | InetSocketAddress addr = new InetSocketAddress (0);
45 | HttpServer server = HttpServer.create (addr, 0);
46 | HttpContext ctx = server.createContext ("/test");
47 | ExecutorService executor = Executors.newCachedThreadPool();
48 | server.setExecutor (executor);
49 | server.start ();
50 |
51 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
52 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
53 | try {
54 | InputStream is = urlc.getInputStream();
55 | int c = 0;
56 | while (is.read()!= -1) {
57 | c ++;
58 | }
59 | } catch (IOException e) {
60 | server.stop(2);
61 | executor.shutdown();
62 | System.out.println ("OK");
63 | }
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/test/java/B6341616.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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 6341616
27 | * @summary Server doesnt send response if there is a RuntimeException in validate of BasicAuthFilter
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.net.HttpURLConnection;
33 | import java.net.InetSocketAddress;
34 | import java.net.PasswordAuthentication;
35 | import java.net.URL;
36 | import java.util.concurrent.ExecutorService;
37 | import java.util.concurrent.Executors;
38 |
39 | import org.jboss.com.sun.net.httpserver.BasicAuthenticator;
40 | import org.jboss.com.sun.net.httpserver.Headers;
41 | import org.jboss.com.sun.net.httpserver.HttpContext;
42 | import org.jboss.com.sun.net.httpserver.HttpExchange;
43 | import org.jboss.com.sun.net.httpserver.HttpHandler;
44 | import org.jboss.com.sun.net.httpserver.HttpServer;
45 |
46 | public class B6341616 {
47 |
48 | public static void main (String[] args) throws Exception {
49 | Handler handler = new Handler();
50 | InetSocketAddress addr = new InetSocketAddress (0);
51 | HttpServer server = HttpServer.create (addr, 0);
52 | HttpContext ctx = server.createContext ("/test", handler);
53 | BasicAuthenticator filter = new BasicAuthenticator ("foobar@test.realm") {
54 | public boolean checkCredentials (String username, String pw) {
55 | throw new RuntimeException ("");
56 | }
57 | };
58 |
59 | ctx.setAuthenticator (filter);
60 | ExecutorService executor = Executors.newCachedThreadPool();
61 | server.setExecutor (executor);
62 | server.start ();
63 | java.net.Authenticator.setDefault (new MyAuthenticator());
64 |
65 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
66 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
67 | try {
68 | InputStream is = urlc.getInputStream();
69 | int c = 0;
70 | while (is.read()!= -1) {
71 | c ++;
72 | }
73 | } catch (IOException e) {
74 | server.stop(2);
75 | executor.shutdown();
76 | System.out.println ("OK");
77 | }
78 | }
79 |
80 | public static boolean error = false;
81 |
82 | static class MyAuthenticator extends java.net.Authenticator {
83 | public PasswordAuthentication getPasswordAuthentication () {
84 | PasswordAuthentication pw;
85 | if (!getRequestingPrompt().equals ("foobar@test.realm")) {
86 | B6341616.error = true;
87 | }
88 | pw = new PasswordAuthentication ("fred", "xyz".toCharArray());
89 | return pw;
90 | }
91 | }
92 |
93 | static class Handler implements HttpHandler {
94 | int invocation = 1;
95 | public void handle (HttpExchange t)
96 | throws IOException
97 | {
98 | InputStream is = t.getRequestBody();
99 | Headers map = t.getRequestHeaders();
100 | Headers rmap = t.getResponseHeaders();
101 | while (is.read () != -1) ;
102 | is.close();
103 | t.sendResponseHeaders (200, -1);
104 | t.close();
105 | }
106 | }
107 | }
108 |
--------------------------------------------------------------------------------
/src/test/java/B6393710.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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 6393710
27 | * @summary Non authenticated call followed by authenticated call never returns
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.io.OutputStream;
33 | import java.net.InetSocketAddress;
34 | import java.net.Socket;
35 | import java.net.SocketTimeoutException;
36 |
37 | import org.jboss.com.sun.net.httpserver.BasicAuthenticator;
38 | import org.jboss.com.sun.net.httpserver.Headers;
39 | import org.jboss.com.sun.net.httpserver.HttpContext;
40 | import org.jboss.com.sun.net.httpserver.HttpExchange;
41 | import org.jboss.com.sun.net.httpserver.HttpHandler;
42 | import org.jboss.com.sun.net.httpserver.HttpServer;
43 |
44 | /*
45 | * Test checks for following bug(s) when a POST containing a request body
46 | * needs to be authenticated
47 | *
48 | * 1) we were not reading the request body
49 | *
50 | * 2) we were not re-enabling the interestops for the socket channel
51 | */
52 |
53 | public class B6393710 {
54 |
55 | static String CRLF = "\r\n";
56 |
57 | /* Two post requests containing data. The second one
58 | * has the expected authorization credentials
59 | */
60 | static String cmd =
61 | "POST /test/foo HTTP/1.1"+CRLF+
62 | "Content-Length: 22"+CRLF+
63 | "Pragma: no-cache"+CRLF+
64 | "Cache-Control: no-cache"+CRLF+ CRLF+
65 | " "+
66 | "POST /test/foo HTTP/1.1"+CRLF+
67 | "Content-Length: 22"+CRLF+
68 | "Pragma: no-cache"+CRLF+
69 | "Authorization: Basic ZnJlZDpmcmVkcGFzc3dvcmQ="+CRLF+
70 | "Cache-Control: no-cache"+CRLF+ CRLF+
71 | " ";
72 |
73 | public static void main (String[] args) throws Exception {
74 | Handler handler = new Handler();
75 | InetSocketAddress addr = new InetSocketAddress (0);
76 | HttpServer server = HttpServer.create (addr, 0);
77 | HttpContext ctx = server.createContext ("/test", handler);
78 | ctx.setAuthenticator (new BasicAuthenticator ("test") {
79 | public boolean checkCredentials (String user, String pass) {
80 | return user.equals ("fred") && pass.equals("fredpassword");
81 | }
82 | });
83 |
84 | server.start ();
85 |
86 | Socket s = new Socket ("localhost", server.getAddress().getPort());
87 | s.setSoTimeout (5000);
88 |
89 | OutputStream os = s.getOutputStream();
90 | os.write (cmd.getBytes());
91 | InputStream is = s.getInputStream ();
92 | try {
93 | ok = readAndCheck (is, "401 Unauthorized") &&
94 | readAndCheck (is, "200 OK");
95 | } catch (SocketTimeoutException e) {
96 | System.out.println ("Did not received expected data");
97 | ok = false;
98 | } finally {
99 | s.close();
100 | server.stop(2);
101 | }
102 |
103 | if (requests != 1) {
104 | throw new RuntimeException ("server handler did not receive the request");
105 | }
106 | if (!ok) {
107 | throw new RuntimeException ("did not get 200 OK");
108 | }
109 | System.out.println ("OK");
110 | }
111 |
112 | /* check for expected string and return true if found in stream */
113 |
114 | static boolean readAndCheck (InputStream is, String expected) throws IOException {
115 | int c;
116 | int count = 0;
117 | int expLen = expected.length();
118 | expected = expected.toLowerCase();
119 |
120 | while ((c=is.read()) != -1) {
121 | c = Character.toLowerCase (c);
122 | if (c == expected.charAt (count)) {
123 | count ++;
124 | if (count == expLen) {
125 | return true;
126 | }
127 | } else {
128 | count = 0;
129 | }
130 | }
131 | return false;
132 | }
133 |
134 | public static boolean ok = false;
135 | static int requests = 0;
136 |
137 | static class Handler implements HttpHandler {
138 | int invocation = 1;
139 | public void handle (HttpExchange t)
140 | throws IOException
141 | {
142 | int count = 0;
143 | InputStream is = t.getRequestBody();
144 | Headers map = t.getRequestHeaders();
145 | Headers rmap = t.getResponseHeaders();
146 | while (is.read () != -1) {
147 | count ++;
148 | }
149 | if (count != 22) {
150 | System.out.println ("Handler expected 22. got " + count);
151 | ok = false;
152 | }
153 | is.close();
154 | t.sendResponseHeaders (200, -1);
155 | t.close();
156 | requests ++;
157 | }
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/src/test/java/B6401598.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 2010, 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 6401598
27 | * @summary new HttpServer cannot serve binary stream data
28 | */
29 |
30 | import java.io.DataInputStream;
31 | import java.io.DataOutputStream;
32 | import java.io.IOException;
33 | import java.io.InputStream;
34 | import java.io.OutputStream;
35 | import java.net.HttpURLConnection;
36 | import java.net.InetSocketAddress;
37 | import java.net.URL;
38 | import java.util.concurrent.ExecutorService;
39 | import java.util.concurrent.Executors;
40 |
41 | import org.jboss.com.sun.net.httpserver.HttpExchange;
42 | import org.jboss.com.sun.net.httpserver.HttpHandler;
43 | import org.jboss.com.sun.net.httpserver.HttpServer;
44 |
45 | public class B6401598 {
46 |
47 | static class MyHandler implements HttpHandler {
48 |
49 | public MyHandler() {
50 |
51 | }
52 |
53 | public void handle(HttpExchange arg0) throws IOException {
54 | try {
55 | InputStream is = arg0.getRequestBody();
56 | OutputStream os = arg0.getResponseBody();
57 |
58 | DataInputStream dis = new DataInputStream(is);
59 |
60 | short input = dis.readShort();
61 | while (dis.read() != -1) ;
62 | dis.close();
63 |
64 | DataOutputStream dos = new DataOutputStream(os);
65 |
66 | arg0.sendResponseHeaders(200, 0);
67 |
68 | dos.writeShort(input);
69 |
70 | dos.flush();
71 | dos.close();
72 | } catch (IOException e) {
73 | e.printStackTrace();
74 | error = true;
75 | }
76 | }
77 |
78 | }
79 |
80 | static int port;
81 | static boolean error = false;
82 | static ExecutorService exec;
83 | static HttpServer server;
84 |
85 | public static void main(String[] args) {
86 | try {
87 | server = HttpServer.create(new InetSocketAddress(0), 400);
88 | server.createContext("/server/", new MyHandler());
89 | exec = Executors.newFixedThreadPool(3);
90 | server.setExecutor(exec);
91 | port = server.getAddress().getPort();
92 | server.start();
93 |
94 | short counter;
95 |
96 | for (counter = 0; counter < 1000; counter++) {
97 | HttpURLConnection connection = getHttpURLConnection(new URL("http://127.0.0.1:"+port+"/server/"), 10000);
98 |
99 | OutputStream os = connection.getOutputStream();
100 |
101 | DataOutputStream dos = new DataOutputStream(os);
102 |
103 | dos.writeShort(counter);
104 |
105 | dos.flush();
106 | dos.close();
107 |
108 | counter++;
109 |
110 | InputStream is = connection.getInputStream();
111 |
112 | DataInputStream dis = new DataInputStream(is);
113 |
114 | short ret = dis.readShort();
115 |
116 | dis.close();
117 | }
118 | System.out.println ("Stopping");
119 | server.stop (1);
120 | exec.shutdown();
121 | } catch (IOException e) {
122 | // TODO Auto-generated catch block
123 | e.printStackTrace();
124 | server.stop (1);
125 | exec.shutdown();
126 | }
127 | }
128 |
129 |
130 |
131 | static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException {
132 |
133 | HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
134 |
135 | httpURLConnection.setConnectTimeout(40000);
136 | httpURLConnection.setReadTimeout(timeout);
137 | httpURLConnection.setDoOutput(true);
138 | httpURLConnection.setDoInput(true);
139 | httpURLConnection.setUseCaches(false);
140 | httpURLConnection.setAllowUserInteraction(false);
141 | httpURLConnection.setRequestMethod("POST");
142 |
143 | // HttpURLConnection httpURLConnection = new MyHttpURLConnection(url);
144 |
145 | return httpURLConnection;
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/src/test/java/B6421581.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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 6421581
27 | * @summary NPE while closing HttpExchange.getResonseBody()
28 | */
29 |
30 | import java.io.InputStream;
31 | import java.io.OutputStream;
32 | import java.net.InetSocketAddress;
33 | import java.net.URL;
34 | import java.util.concurrent.ExecutorService;
35 | import java.util.concurrent.Executors;
36 | import java.util.concurrent.TimeUnit;
37 |
38 | import org.jboss.com.sun.net.httpserver.HttpContext;
39 | import org.jboss.com.sun.net.httpserver.HttpExchange;
40 | import org.jboss.com.sun.net.httpserver.HttpHandler;
41 | import org.jboss.com.sun.net.httpserver.HttpServer;
42 |
43 | public class B6421581 {
44 |
45 | static boolean error = false;
46 | static int iter = 0;
47 |
48 | public static void main(String[] args) throws Exception {
49 | once();
50 | }
51 |
52 | public static void once() throws Exception {
53 | InetSocketAddress inetAddress = new InetSocketAddress(
54 | "localhost", 0);
55 | HttpServer server = HttpServer.create(inetAddress, 5);
56 | int port = server.getAddress().getPort();
57 | ExecutorService e = (Executors.newFixedThreadPool(5));
58 | server.setExecutor(e);
59 | HttpContext context = server.createContext("/hello");
60 | server.start();
61 | context.setHandler(new HttpHandler() {
62 | public void handle(HttpExchange msg) {
63 | iter ++;
64 | System.out.println("Got request");
65 | switch (iter) {
66 | case 1:
67 | /* close output stream without opening inpustream */
68 | /* chunked encoding */
69 | try {
70 | msg.sendResponseHeaders(200, 0);
71 | OutputStream out = msg.getResponseBody();
72 | out.write("hello".getBytes());
73 | out.close();
74 | } catch(Exception e) {
75 | error = true;
76 | } finally {
77 | msg.close();
78 | }
79 | break;
80 | case 2:
81 | /* close output stream without opening inpustream */
82 | /* fixed encoding */
83 | try {
84 | msg.sendResponseHeaders(200, 5);
85 | OutputStream out = msg.getResponseBody();
86 | out.write("hello".getBytes());
87 | out.close();
88 | } catch(Exception e) {
89 | error = true;
90 | } finally {
91 | msg.close();
92 | }
93 | break;
94 | case 3:
95 | /* close exchange without opening any stream */
96 | try {
97 | msg.sendResponseHeaders(200, -1);
98 | msg.close();
99 | } catch(Exception e) {
100 | error = true;
101 | }
102 | break;
103 | }
104 | }
105 | });
106 |
107 | URL url = new URL("http://localhost:"+port+"/hello/url.text");
108 | doURL(url);
109 | doURL(url);
110 | doURL(url);
111 | e.shutdown();
112 | e.awaitTermination(4, TimeUnit.SECONDS);
113 | server.stop(0);
114 | if (error) {
115 | throw new RuntimeException ("test failed");
116 | }
117 | }
118 |
119 | static void doURL (URL url) throws Exception {
120 | InputStream is = url.openStream();
121 | while (is.read() != -1) ;
122 | is.close();
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/src/test/java/B6424196.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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 6424196
27 | * @summary PIT build 85 mustang: two jhttp tests fail
28 | */
29 |
30 | import java.io.IOException;
31 |
32 | import org.jboss.com.sun.net.httpserver.HttpServer;
33 |
34 | public class B6424196 {
35 |
36 | public static void main(String[] args) throws IOException {
37 | HttpServer server = HttpServer.create();
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/test/java/B6431193.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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 6431193
27 | * @summary The new HTTP server exits immediately
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.io.OutputStream;
33 | import java.net.InetSocketAddress;
34 | import java.net.URL;
35 |
36 | import org.jboss.com.sun.net.httpserver.HttpExchange;
37 | import org.jboss.com.sun.net.httpserver.HttpHandler;
38 | import org.jboss.com.sun.net.httpserver.HttpServer;
39 |
40 | public class B6431193 {
41 |
42 | static boolean error = false;
43 |
44 | public static void read (InputStream i) throws IOException {
45 | while (i.read() != -1);
46 | i.close();
47 | }
48 |
49 | /**
50 | * @param args
51 | */
52 | public static void main(String[] args) {
53 | class MyHandler implements HttpHandler {
54 | public void handle(HttpExchange t) throws IOException {
55 | InputStream is = t.getRequestBody();
56 | read(is);
57 | // .. read the request body
58 | String response = "This is the response";
59 | t.sendResponseHeaders(200, response.length());
60 | OutputStream os = t.getResponseBody();
61 | os.write(response.getBytes());
62 | os.close();
63 | error = Thread.currentThread().isDaemon();
64 | }
65 | }
66 |
67 |
68 | HttpServer server;
69 | try {
70 | server = HttpServer.create(new InetSocketAddress(0), 10);
71 |
72 | server.createContext("/apps", new MyHandler());
73 | server.setExecutor(null);
74 | // creates a default executor
75 | server.start();
76 | int port = server.getAddress().getPort();
77 | String s = "http://localhost:"+port+"/apps/foo";
78 | URL url = new URL (s);
79 | InputStream is = url.openStream();
80 | read (is);
81 | server.stop (1);
82 | if (error) {
83 | throw new RuntimeException ("error in test");
84 | }
85 |
86 | }
87 | catch (IOException e) {
88 | // TODO Auto-generated catch block
89 | e.printStackTrace();
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/test/java/B6433018.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2006, 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 6433018
27 | * @summary HTTP server sometimes sends bad request for browsers javascript
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.io.OutputStream;
33 | import java.net.InetSocketAddress;
34 | import java.net.Socket;
35 |
36 | import org.jboss.com.sun.net.httpserver.Headers;
37 | import org.jboss.com.sun.net.httpserver.HttpContext;
38 | import org.jboss.com.sun.net.httpserver.HttpExchange;
39 | import org.jboss.com.sun.net.httpserver.HttpHandler;
40 | import org.jboss.com.sun.net.httpserver.HttpServer;
41 |
42 | public class B6433018 {
43 |
44 | static String CRLF = "\r\n";
45 |
46 | /* invalid HTTP POST with extra CRLF at end */
47 | /* This checks that the server is able to handle it
48 | * and recognise the second request */
49 |
50 | static String cmd =
51 | "POST /test/item HTTP/1.1"+CRLF+
52 | "Keep-Alive: 300"+CRLF+
53 | "Proxy-Connection: keep-alive"+CRLF+
54 | "Content-Type: text/xml"+CRLF+
55 | "Content-Length: 22"+CRLF+
56 | "Pragma: no-cache"+CRLF+
57 | "Cache-Control: no-cache"+CRLF+ CRLF+
58 | " "+CRLF+
59 | "GET /test/items HTTP/1.1"+CRLF+
60 | "Host: araku:9999"+CRLF+
61 | "Accept-Language: en-us,en;q=0.5"+CRLF+
62 | "Accept-Encoding: gzip,deflate"+CRLF+
63 | "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"+CRLF+
64 | "Keep-Alive: 300"+CRLF+
65 | "Proxy-Connection: keep-alive"+CRLF+
66 | "Pragma: no-cache"+CRLF+
67 | "Cache-Control: no-cache"+CRLF+CRLF;
68 |
69 | public static void main (String[] args) throws Exception {
70 | Handler handler = new Handler();
71 | InetSocketAddress addr = new InetSocketAddress (0);
72 | HttpServer server = HttpServer.create (addr, 0);
73 | HttpContext ctx = server.createContext ("/test", handler);
74 |
75 | server.start ();
76 |
77 | Socket s = new Socket ("localhost", server.getAddress().getPort());
78 |
79 | try {
80 | OutputStream os = s.getOutputStream();
81 | os.write (cmd.getBytes());
82 | Thread.sleep (3000);
83 | s.close();
84 | } catch (IOException e) { }
85 | server.stop(2);
86 | if (requests != 2) {
87 | throw new RuntimeException ("did not receive the 2 requests");
88 | }
89 | System.out.println ("OK");
90 | }
91 |
92 | public static boolean error = false;
93 | static int requests = 0;
94 |
95 | static class Handler implements HttpHandler {
96 | int invocation = 1;
97 | public void handle (HttpExchange t)
98 | throws IOException
99 | {
100 | InputStream is = t.getRequestBody();
101 | Headers map = t.getRequestHeaders();
102 | Headers rmap = t.getResponseHeaders();
103 | while (is.read () != -1) ;
104 | is.close();
105 | t.sendResponseHeaders (200, -1);
106 | t.close();
107 | requests ++;
108 | }
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/src/test/java/B6526158.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2007, 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 6526158
27 | * @summary HttpExchange.getRequestBody().close() throws Exception
28 | */
29 |
30 | import java.io.BufferedOutputStream;
31 | import java.io.IOException;
32 | import java.io.InputStream;
33 | import java.io.OutputStream;
34 | import java.net.HttpURLConnection;
35 | import java.net.InetSocketAddress;
36 | import java.net.URL;
37 | import java.util.concurrent.ExecutorService;
38 | import java.util.concurrent.Executors;
39 |
40 | import org.jboss.com.sun.net.httpserver.HttpContext;
41 | import org.jboss.com.sun.net.httpserver.HttpExchange;
42 | import org.jboss.com.sun.net.httpserver.HttpHandler;
43 | import org.jboss.com.sun.net.httpserver.HttpServer;
44 |
45 | public class B6526158 {
46 |
47 | /* keep under 64 k */
48 | final static int SIZE = 60 * 1024;
49 |
50 | public static void main (String[] args) throws Exception {
51 | Handler handler = new Handler();
52 | InetSocketAddress addr = new InetSocketAddress (0);
53 | HttpServer server = HttpServer.create (addr, 0);
54 | HttpContext ctx = server.createContext ("/test", handler);
55 |
56 | ExecutorService executor = Executors.newCachedThreadPool();
57 | server.setExecutor (executor);
58 | server.start ();
59 |
60 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
61 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
62 | urlc.setDoOutput (true);
63 | try {
64 | OutputStream os = new BufferedOutputStream (urlc.getOutputStream());
65 | for (int i=0; i< SIZE; i++) {
66 | os.write (i);
67 | }
68 | os.close();
69 | InputStream is = urlc.getInputStream();
70 | int c = 0;
71 | while (is.read()!= -1) {
72 | c ++;
73 | }
74 | is.close();
75 | } finally {
76 | server.stop(2);
77 | executor.shutdown();
78 | }
79 | if (error) {
80 | throw new RuntimeException ("Test failed");
81 | }
82 | }
83 |
84 | public static boolean error = false;
85 |
86 | static class Handler implements HttpHandler {
87 | int invocation = 1;
88 | public void handle (HttpExchange t)
89 | throws IOException
90 | {
91 | InputStream is = t.getRequestBody();
92 | try {
93 | is.close();
94 | } catch (IOException e) {
95 | e.printStackTrace();
96 | error = true;
97 | }
98 | t.sendResponseHeaders (200, -1);
99 | t.close();
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/test/java/B6526913.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2007, 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 6526913
27 | * @run main/othervm -Dhttp.keepAlive=false B6526913
28 | * @summary HttpExchange.getResponseBody().close() throws Exception
29 | */
30 |
31 | import java.io.IOException;
32 | import java.io.InputStream;
33 | import java.io.OutputStream;
34 | import java.net.HttpURLConnection;
35 | import java.net.InetSocketAddress;
36 | import java.net.URL;
37 | import java.util.concurrent.ExecutorService;
38 | import java.util.concurrent.Executors;
39 |
40 | import org.jboss.com.sun.net.httpserver.HttpContext;
41 | import org.jboss.com.sun.net.httpserver.HttpExchange;
42 | import org.jboss.com.sun.net.httpserver.HttpHandler;
43 | import org.jboss.com.sun.net.httpserver.HttpServer;
44 |
45 | public class B6526913 {
46 |
47 | public static void main (String[] args) throws Exception {
48 | Handler handler = new Handler();
49 | InetSocketAddress addr = new InetSocketAddress (0);
50 | HttpServer server = HttpServer.create (addr, 0);
51 | HttpContext ctx = server.createContext ("/test", handler);
52 |
53 | ExecutorService executor = Executors.newCachedThreadPool();
54 | server.setExecutor (executor);
55 | server.start ();
56 |
57 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
58 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
59 | try {
60 | InputStream is = urlc.getInputStream();
61 | int c ,count = 0;
62 | byte [] buf = new byte [32 * 1024];
63 | while (count < 32 * 1024) {
64 | count += is.read (buf);
65 | }
66 | is.close();
67 | } finally {
68 | server.stop(2);
69 | executor.shutdown();
70 | }
71 | if (error) {
72 | throw new RuntimeException ("Test failed");
73 | }
74 | }
75 |
76 | public static boolean error = false;
77 |
78 | static class Handler implements HttpHandler {
79 | int invocation = 1;
80 | public void handle (HttpExchange t)
81 | throws IOException
82 | {
83 | InputStream is = t.getRequestBody();
84 | try {
85 | while (is.read() != -1) ;
86 | is.close();
87 | } catch (IOException e) {
88 | e.printStackTrace();
89 | error = true;
90 | }
91 | /* send a chunked response, but wait a while before
92 | * sending the final empty chunk
93 | */
94 | t.sendResponseHeaders (200, 0);
95 | OutputStream os = t.getResponseBody();
96 | byte[] bb = new byte [32 * 1024];
97 | os.write (bb);
98 | os.flush();
99 | try {Thread.sleep (5000); } catch (InterruptedException e){}
100 | try {
101 | /* empty chunk sent here */
102 | os.close();
103 | } catch (IOException e) {
104 | error = true;
105 | e.printStackTrace();
106 | }
107 | t.close();
108 | }
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/src/test/java/B6529200.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2007, 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 6529200
27 | * @run main/othervm B6529200
28 | * @summary lightweight http server does not work with http1.0 clients
29 | */
30 |
31 | import java.io.IOException;
32 | import java.io.InputStream;
33 | import java.io.OutputStream;
34 | import java.net.InetSocketAddress;
35 | import java.net.Socket;
36 | import java.net.SocketTimeoutException;
37 | import java.util.concurrent.ExecutorService;
38 | import java.util.concurrent.Executors;
39 |
40 | import org.jboss.com.sun.net.httpserver.HttpContext;
41 | import org.jboss.com.sun.net.httpserver.HttpExchange;
42 | import org.jboss.com.sun.net.httpserver.HttpHandler;
43 | import org.jboss.com.sun.net.httpserver.HttpServer;
44 |
45 | public class B6529200 {
46 |
47 | public static void main (String[] args) throws Exception {
48 | Handler handler = new Handler();
49 | InetSocketAddress addr = new InetSocketAddress (0);
50 | HttpServer server = HttpServer.create (addr, 0);
51 | HttpContext ctx = server.createContext ("/test", handler);
52 |
53 | ExecutorService executor = Executors.newCachedThreadPool();
54 | server.setExecutor (executor);
55 | server.start ();
56 |
57 | /* test 1: keep-alive */
58 |
59 | Socket sock = new Socket ("localhost", server.getAddress().getPort());
60 | OutputStream os = sock.getOutputStream();
61 | System.out.println ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
62 | os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
63 | os.flush();
64 | InputStream is = sock.getInputStream();
65 | StringBuffer s = new StringBuffer();
66 | boolean finished = false;
67 |
68 | sock.setSoTimeout (10 * 1000);
69 | try {
70 | while (!finished) {
71 | char c = (char) is.read();
72 | s.append (c);
73 | finished = s.indexOf ("\r\n\r\nhello") != -1;
74 | /* test will timeout otherwise */
75 | }
76 | } catch (SocketTimeoutException e) {
77 | server.stop (2);
78 | executor.shutdown ();
79 | throw new RuntimeException ("Test failed in test1");
80 | }
81 |
82 | System.out.println (new String (s));
83 |
84 | /* test 2: even though we request keep-alive, server must close
85 | * because it is sending unknown content length response */
86 |
87 | System.out.println("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n");
88 | os.write ("GET /test/foo HTTP/1.0\r\nConnection: keep-alive\r\n\r\n".getBytes());
89 | os.flush();
90 | int i=0,c;
91 | byte [] buf = new byte [8*1024];
92 | try {
93 | while ((c=is.read()) != -1) {
94 | buf[i++] = (byte)c;
95 | }
96 | } catch (SocketTimeoutException e) {
97 | server.stop (2);
98 | executor.shutdown ();
99 | throw new RuntimeException ("Test failed in test2");
100 | }
101 |
102 | String ss = new String (buf, "ISO-8859-1");
103 | if (ss.indexOf ("\r\n\r\nhello world") == -1) {
104 | server.stop (2);
105 | executor.shutdown ();
106 | throw new RuntimeException ("Test failed in test2: wrong string");
107 | }
108 | System.out.println (ss);
109 | is.close ();
110 | server.stop (2);
111 | executor.shutdown();
112 | }
113 |
114 |
115 | static class Handler implements HttpHandler {
116 | int invocation = 1;
117 | public void handle (HttpExchange t)
118 | throws IOException
119 | {
120 | InputStream is;
121 | OutputStream os;
122 | switch (invocation++) {
123 | case 1:
124 | is = t.getRequestBody();
125 | while (is.read() != -1) ;
126 | is.close();
127 | t.sendResponseHeaders (200, "hello".length());
128 | os = t.getResponseBody();
129 | os.write ("hello".getBytes());
130 | os.close();
131 | break;
132 | case 2:
133 | is = t.getRequestBody();
134 | while (is.read() != -1) ;
135 | is.close();
136 | t.sendResponseHeaders (200, 0);
137 | os = t.getResponseBody();
138 | os.write ("hello world".getBytes());
139 | os.close();
140 | break;
141 | }
142 | }
143 | }
144 | }
145 |
--------------------------------------------------------------------------------
/src/test/java/B6744329.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2008, 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 6744329
27 | * @summary Exception in light weight Http server
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.io.OutputStream;
33 | import java.net.HttpURLConnection;
34 | import java.net.InetSocketAddress;
35 | import java.net.URL;
36 | import java.util.concurrent.ExecutorService;
37 | import java.util.concurrent.Executors;
38 |
39 | import org.jboss.com.sun.net.httpserver.Headers;
40 | import org.jboss.com.sun.net.httpserver.HttpContext;
41 | import org.jboss.com.sun.net.httpserver.HttpExchange;
42 | import org.jboss.com.sun.net.httpserver.HttpHandler;
43 | import org.jboss.com.sun.net.httpserver.HttpServer;
44 |
45 | public class B6744329 {
46 |
47 | public static void main (String[] args) throws Exception {
48 | Handler handler = new Handler();
49 | InetSocketAddress addr = new InetSocketAddress (0);
50 | HttpServer server = HttpServer.create (addr, 0);
51 | HttpContext ctx = server.createContext ("/test", handler);
52 | ExecutorService executor = Executors.newCachedThreadPool();
53 | server.setExecutor (executor);
54 | server.start ();
55 |
56 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
57 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
58 | try {
59 | InputStream is = urlc.getInputStream();
60 | int c = 0;
61 | while (is.read()!= -1) {
62 | c ++;
63 | }
64 | System.out.println ("OK");
65 | } catch (IOException e) {
66 | System.out.println ("exception");
67 | error = true;
68 | }
69 | server.stop(2);
70 | executor.shutdown();
71 | if (error) {
72 | throw new RuntimeException ("Test failed");
73 | }
74 | }
75 |
76 | public static boolean error = false;
77 |
78 | /* this must be the same size as in ChunkedOutputStream.java
79 | */
80 | final static int CHUNK_SIZE = 4096;
81 |
82 | static class Handler implements HttpHandler {
83 | int invocation = 1;
84 | public void handle (HttpExchange t)
85 | throws IOException
86 | {
87 | InputStream is = t.getRequestBody();
88 | Headers map = t.getRequestHeaders();
89 | Headers rmap = t.getResponseHeaders();
90 | while (is.read () != -1) ;
91 | is.close();
92 | /* chunked response */
93 | t.sendResponseHeaders (200, 0);
94 | OutputStream os = t.getResponseBody();
95 | byte[] first = new byte [CHUNK_SIZE * 2];
96 | byte[] second = new byte [2];
97 | os.write (first);
98 | os.write ('x');
99 | os.write ('x');
100 | /* An index out of bounds exception will be thrown
101 | * below, which is caught by server, and connection
102 | * will be closed. resulting in IOException to client
103 | * - if bug present
104 | */
105 | os.write ('x');
106 | os.write ('x');
107 | os.write ('x');
108 | t.close();
109 | }
110 | }
111 | }
112 |
--------------------------------------------------------------------------------
/src/test/java/B6886436.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2009, 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 6886436
27 | * @summary
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.net.HttpURLConnection;
33 | import java.net.InetSocketAddress;
34 | import java.net.URL;
35 | import java.util.concurrent.ExecutorService;
36 | import java.util.concurrent.Executors;
37 | import java.util.logging.ConsoleHandler;
38 | import java.util.logging.Level;
39 | import java.util.logging.Logger;
40 |
41 | import org.jboss.com.sun.net.httpserver.Headers;
42 | import org.jboss.com.sun.net.httpserver.HttpContext;
43 | import org.jboss.com.sun.net.httpserver.HttpExchange;
44 | import org.jboss.com.sun.net.httpserver.HttpHandler;
45 | import org.jboss.com.sun.net.httpserver.HttpServer;
46 |
47 | public class B6886436 {
48 |
49 | public static void main (String[] args) throws Exception {
50 | Logger logger = Logger.getLogger ("com.sun.net.httpserver");
51 | ConsoleHandler c = new ConsoleHandler();
52 | c.setLevel (Level.WARNING);
53 | logger.addHandler (c);
54 | logger.setLevel (Level.WARNING);
55 | Handler handler = new Handler();
56 | InetSocketAddress addr = new InetSocketAddress (0);
57 | HttpServer server = HttpServer.create (addr, 0);
58 | HttpContext ctx = server.createContext ("/test", handler);
59 | ExecutorService executor = Executors.newCachedThreadPool();
60 | server.setExecutor (executor);
61 | server.start ();
62 |
63 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
64 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
65 | try {
66 | InputStream is = urlc.getInputStream();
67 | while (is.read()!= -1) ;
68 | is.close ();
69 | urlc = (HttpURLConnection)url.openConnection ();
70 | urlc.setReadTimeout (3000);
71 | is = urlc.getInputStream();
72 | while (is.read()!= -1);
73 | is.close ();
74 |
75 | } catch (IOException e) {
76 | server.stop(2);
77 | executor.shutdown();
78 | throw new RuntimeException ("Test failed");
79 | }
80 | server.stop(2);
81 | executor.shutdown();
82 | System.out.println ("OK");
83 | }
84 |
85 | public static boolean error = false;
86 |
87 | static class Handler implements HttpHandler {
88 | int invocation = 1;
89 | public void handle (HttpExchange t)
90 | throws IOException
91 | {
92 | InputStream is = t.getRequestBody();
93 | Headers map = t.getRequestHeaders();
94 | Headers rmap = t.getResponseHeaders();
95 | while (is.read () != -1) ;
96 | is.close();
97 | // send a 204 response with an empty chunked body
98 | t.sendResponseHeaders (204, 0);
99 | t.close();
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/src/test/java/DummyVerifier.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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 | import javax.net.ssl.HostnameVerifier;
25 | import javax.net.ssl.SSLSession;
26 |
27 | public class DummyVerifier implements HostnameVerifier {
28 | public boolean verify (String s, SSLSession s1) {
29 | return true;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/HeadTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010, 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 6886723
27 | * @summary light weight http server doesn't return correct status code for HEAD requests
28 | */
29 |
30 | import java.io.IOException;
31 | import java.net.HttpURLConnection;
32 | import java.net.InetSocketAddress;
33 | import java.net.URL;
34 | import java.util.concurrent.ExecutorService;
35 | import java.util.concurrent.Executors;
36 |
37 | import org.jboss.com.sun.net.httpserver.HttpContext;
38 | import org.jboss.com.sun.net.httpserver.HttpExchange;
39 | import org.jboss.com.sun.net.httpserver.HttpHandler;
40 | import org.jboss.com.sun.net.httpserver.HttpServer;
41 |
42 | public class HeadTest {
43 |
44 | @org.junit.Test
45 | public void testThis() {
46 | // Add tests later.
47 | }
48 |
49 | public static void main(String[] args) throws Exception {
50 | server();
51 | }
52 |
53 | static void server() throws Exception {
54 | InetSocketAddress inetAddress = new InetSocketAddress(0);
55 | HttpServer server = HttpServer.create(inetAddress, 5);
56 | try {
57 | server.setExecutor(Executors.newFixedThreadPool(5));
58 | HttpContext chunkedContext = server.createContext("/chunked");
59 | chunkedContext.setHandler(new HttpHandler() {
60 |
61 | public void handle(HttpExchange msg) {
62 | try {
63 | try {
64 | if (msg.getRequestMethod().equals("HEAD")) {
65 | msg.getRequestBody().close();
66 | msg.getResponseHeaders().add("Transfer-encoding", "chunked");
67 | msg.sendResponseHeaders(200, -1);
68 | }
69 | } catch(IOException ioe) {
70 | ioe.printStackTrace();
71 | }
72 | } finally {
73 | msg.close();
74 | }
75 | }
76 | });
77 | HttpContext clContext = server.createContext("/content");
78 | clContext.setHandler(new HttpHandler() {
79 |
80 | public void handle(HttpExchange msg) {
81 | try {
82 | try {
83 | if (msg.getRequestMethod().equals("HEAD")) {
84 | msg.getRequestBody().close();
85 | msg.getResponseHeaders().add("Content-length", "1024");
86 | msg.sendResponseHeaders(200, -1);
87 | }
88 | } catch(IOException ioe) {
89 | ioe.printStackTrace();
90 | }
91 | } finally {
92 | msg.close();
93 | }
94 | }
95 | });
96 | server.start();
97 | String urlStr = "http://localhost:" + server.getAddress().getPort() + "/";
98 | System.out.println("Server is at " + urlStr);
99 |
100 | // Run the chunked client
101 | for(int i=0; i < 10; i++) {
102 | runClient(urlStr + "chunked/");
103 | }
104 | // Run the content length client
105 | for(int i=0; i < 10; i++) {
106 | runClient(urlStr + "content/");
107 | }
108 | } finally {
109 | // Stop the server
110 | ((ExecutorService)server.getExecutor()).shutdown();
111 | server.stop(0);
112 | }
113 | }
114 |
115 | static void runClient(String urlStr) throws Exception {
116 | HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();
117 | conn.setRequestMethod("HEAD");
118 | int status = conn.getResponseCode();
119 | if (status != 200) {
120 | throw new RuntimeException("HEAD request doesn't return 200, but returns " + status);
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/src/test/java/LogFilter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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 | import java.io.File;
25 | import java.io.FileOutputStream;
26 | import java.io.IOException;
27 | import java.io.PrintStream;
28 | import java.text.DateFormat;
29 | import java.util.Date;
30 |
31 | import org.jboss.com.sun.net.httpserver.Filter;
32 | import org.jboss.com.sun.net.httpserver.Headers;
33 | import org.jboss.com.sun.net.httpserver.HttpContext;
34 | import org.jboss.com.sun.net.httpserver.HttpExchange;
35 |
36 | class LogFilter extends Filter {
37 |
38 | PrintStream ps;
39 | DateFormat df;
40 |
41 | LogFilter (File file) throws IOException {
42 | ps = new PrintStream (new FileOutputStream (file));
43 | df = DateFormat.getDateTimeInstance();
44 | }
45 |
46 | /**
47 | * The filter's implementation, which is invoked by the serve r
48 | */
49 | public void doFilter (HttpExchange t, Filter.Chain chain) throws IOException
50 | {
51 | chain.doFilter (t);
52 | HttpContext context = t.getHttpContext();
53 | Headers rmap = t.getRequestHeaders();
54 | String s = df.format (new Date());
55 | s = s +" " + t.getRequestMethod() + " " + t.getRequestURI() + " ";
56 | s = s +" " + t.getResponseCode () +" " + t.getRemoteAddress();
57 | ps.println (s);
58 | }
59 |
60 | public void init (HttpContext ctx) {}
61 |
62 | public String description () {
63 | return "Request logger";
64 | }
65 |
66 | public void destroy (HttpContext c){}
67 | }
68 |
--------------------------------------------------------------------------------
/src/test/java/SimpleSSLContext.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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 | import javax.net.ssl.KeyManagerFactory;
25 | import javax.net.ssl.SSLContext;
26 | import javax.net.ssl.TrustManagerFactory;
27 | import java.io.FileInputStream;
28 | import java.io.IOException;
29 | import java.security.KeyManagementException;
30 | import java.security.KeyStore;
31 | import java.security.KeyStoreException;
32 | import java.security.NoSuchAlgorithmException;
33 | import java.security.UnrecoverableKeyException;
34 | import java.security.cert.CertificateException;
35 |
36 | public class SimpleSSLContext {
37 |
38 | SSLContext ssl;
39 |
40 | SimpleSSLContext (String dir) throws IOException {
41 | try {
42 | String file = dir+"/testkeys";
43 | char[] passphrase = "passphrase".toCharArray();
44 | KeyStore ks = KeyStore.getInstance("JKS");
45 | ks.load(new FileInputStream(file), passphrase);
46 |
47 | KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
48 | kmf.init(ks, passphrase);
49 |
50 | TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
51 | tmf.init(ks);
52 |
53 | ssl = SSLContext.getInstance ("TLS");
54 | ssl.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
55 | } catch (KeyManagementException e) {
56 | throw new RuntimeException (e.getMessage());
57 | } catch (KeyStoreException e) {
58 | throw new RuntimeException (e.getMessage());
59 | } catch (UnrecoverableKeyException e) {
60 | throw new RuntimeException (e.getMessage());
61 | } catch (CertificateException e) {
62 | throw new RuntimeException (e.getMessage());
63 | } catch (NoSuchAlgorithmException e) {
64 | throw new RuntimeException (e.getMessage());
65 | }
66 | }
67 |
68 | SSLContext get () {
69 | return ssl;
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/src/test/java/Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2010, 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 | import java.util.logging.ConsoleHandler;
25 | import java.util.logging.Handler;
26 | import java.util.logging.Level;
27 | import java.util.logging.Logger;
28 |
29 | public class Test {
30 |
31 | static Logger logger;
32 |
33 | static void enableLogging() {
34 | logger = Logger.getLogger("com.sun.net.httpserver");
35 | Handler h = new ConsoleHandler();
36 | h.setLevel(Level.ALL);
37 | logger.setLevel(Level.ALL);
38 | logger.addHandler(h);
39 | }
40 |
41 | static void delay () {
42 | try {
43 | Thread.sleep (1000);
44 | } catch (Exception e) {}
45 | }
46 |
47 | @org.junit.Test
48 | public void testThis() {
49 | // Add tests later.
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/test/java/Test10.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2010, 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 7005016
27 | * @summary pit jdk7 b121 sqe test jhttp/HttpServer150013 failing
28 | * @run main/othervm -Dsun.net.httpserver.clockTick=1000 -Dsun.net.httpserver.idleInterval=3 Test10
29 | */
30 |
31 | import java.io.IOException;
32 | import java.io.InputStream;
33 | import java.io.OutputStream;
34 | import java.net.InetSocketAddress;
35 | import java.net.Socket;
36 | import java.util.concurrent.ExecutorService;
37 | import java.util.concurrent.Executors;
38 |
39 | import org.jboss.com.sun.net.httpserver.Headers;
40 | import org.jboss.com.sun.net.httpserver.HttpContext;
41 | import org.jboss.com.sun.net.httpserver.HttpExchange;
42 | import org.jboss.com.sun.net.httpserver.HttpHandler;
43 | import org.jboss.com.sun.net.httpserver.HttpServer;
44 |
45 | /*
46 | * Test handling of empty Http headers
47 | */
48 |
49 | public class Test10 extends Test {
50 | public static void main (String[] args) throws Exception {
51 | System.out.print ("Test10: ");
52 | Handler handler = new Handler();
53 | InetSocketAddress addr = new InetSocketAddress (0);
54 | HttpServer server = HttpServer.create (addr, 0);
55 | int port = server.getAddress().getPort();
56 | HttpContext c2 = server.createContext ("/test", handler);
57 |
58 | ExecutorService exec = Executors.newCachedThreadPool();
59 | server.setExecutor (exec);
60 | try {
61 | server.start ();
62 | doClient(port);
63 | System.out.println ("OK");
64 | } finally {
65 | delay();
66 | if (server != null)
67 | server.stop(2);
68 | if (exec != null)
69 | exec.shutdown();
70 | }
71 | }
72 |
73 | static class Handler implements HttpHandler {
74 | volatile int invocation = 0;
75 | public void handle (HttpExchange t)
76 | throws IOException
77 | {
78 | InputStream is = t.getRequestBody();
79 | while (is.read() != -1);
80 | Headers map = t.getRequestHeaders();
81 | t.sendResponseHeaders (200, -1);
82 | t.close();
83 | }
84 | }
85 |
86 | public static void doClient (int port) throws Exception {
87 | String s = "GET /test/1.html HTTP/1.1\r\n\r\n";
88 |
89 | Socket socket = new Socket ("localhost", port);
90 | OutputStream os = socket.getOutputStream();
91 | os.write (s.getBytes());
92 | socket.setSoTimeout (10 * 1000);
93 | InputStream is = socket.getInputStream();
94 | int c;
95 | byte[] b = new byte [1024];
96 | while ((c=is.read(b)) != -1) ;
97 | is.close();
98 | socket.close();
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/src/test/java/Test11.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2010, 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 6270015
27 | * @summary Light weight HTTP server
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.io.OutputStream;
33 | import java.net.HttpURLConnection;
34 | import java.net.InetSocketAddress;
35 | import java.net.URL;
36 | import java.util.concurrent.ExecutorService;
37 | import java.util.concurrent.Executors;
38 |
39 | import org.jboss.com.sun.net.httpserver.HttpContext;
40 | import org.jboss.com.sun.net.httpserver.HttpExchange;
41 | import org.jboss.com.sun.net.httpserver.HttpHandler;
42 | import org.jboss.com.sun.net.httpserver.HttpServer;
43 |
44 | public class Test11 {
45 |
46 | @org.junit.Test
47 | public void testThis() {
48 | // Add tests later.
49 | }
50 |
51 | static class Handler implements HttpHandler {
52 | public void handle(HttpExchange t) throws IOException {
53 | read (t.getRequestBody());
54 | String response = "response";
55 | t.sendResponseHeaders (200, response.length());
56 | OutputStream os = t.getResponseBody();
57 | os.write (response.getBytes ("ISO8859_1"));
58 | t.close();
59 | }
60 |
61 | void read (InputStream is ) throws IOException {
62 | byte[] b = new byte [8096];
63 | while (is.read (b) != -1) {}
64 | }
65 | }
66 |
67 | public static void main (String[] args) throws Exception {
68 | System.out.print ("Test 11: ");
69 | HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
70 | ExecutorService s = Executors.newCachedThreadPool();
71 | try {
72 | HttpContext ctx = server.createContext (
73 | "/foo/bar/", new Handler ()
74 | );
75 | s = Executors.newCachedThreadPool();
76 | server.start ();
77 | URL url = new URL ("http://localhost:" + server.getAddress().getPort()+
78 | "/Foo/bar/test.html");
79 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
80 | int r = urlc.getResponseCode();
81 | if (r == 200) {
82 | throw new RuntimeException ("wrong response received");
83 | }
84 | System.out.println ("OK");
85 | } finally {
86 | s.shutdown();
87 | server.stop(2);
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/test/java/Test14.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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 6270015
27 | * @summary Light weight HTTP server
28 | */
29 |
30 | import java.io.File;
31 | import java.io.FilterOutputStream;
32 | import java.io.IOException;
33 | import java.io.InputStream;
34 | import java.io.OutputStream;
35 | import java.net.HttpURLConnection;
36 | import java.net.InetSocketAddress;
37 | import java.net.URL;
38 | import java.util.concurrent.ExecutorService;
39 | import java.util.concurrent.Executors;
40 |
41 | import org.jboss.com.sun.net.httpserver.Filter;
42 | import org.jboss.com.sun.net.httpserver.Headers;
43 | import org.jboss.com.sun.net.httpserver.HttpContext;
44 | import org.jboss.com.sun.net.httpserver.HttpExchange;
45 | import org.jboss.com.sun.net.httpserver.HttpHandler;
46 | import org.jboss.com.sun.net.httpserver.HttpServer;
47 |
48 | /**
49 | * Test filters
50 | */
51 |
52 | public class Test14 extends Test {
53 |
54 | static final String test_input = "Hello world";
55 | static final String test_output = "Ifmmp!xpsme";
56 |
57 | /* an outputstream which transforms the output data
58 | * by adding one to each byte
59 | */
60 | static class OffsetOutputStream extends FilterOutputStream {
61 | OffsetOutputStream (OutputStream os) {
62 | super (os);
63 | }
64 | public void write (int b) throws IOException {
65 | super.write (b+1);
66 | }
67 | }
68 |
69 | static class OffsetFilter extends Filter {
70 | public String description() {
71 | return "Translates outgoing data";
72 | }
73 |
74 | public void destroy(HttpContext c) {}
75 | public void init(HttpContext c) {}
76 |
77 | public void doFilter (HttpExchange exchange, Filter.Chain chain)
78 | throws IOException {
79 | exchange.setStreams (null, new OffsetOutputStream(
80 | exchange.getResponseBody()
81 | ));
82 | chain.doFilter (exchange);
83 | }
84 | }
85 |
86 | public static void main (String[] args) throws Exception {
87 | Handler handler = new Handler();
88 | InetSocketAddress addr = new InetSocketAddress (0);
89 | HttpServer server = HttpServer.create (addr, 0);
90 | HttpContext ctx = server.createContext ("/test", handler);
91 |
92 | File logfile = new File (
93 | System.getProperty ("test.classes")+ "/log.txt"
94 | );
95 |
96 | ctx.getFilters().add (new OffsetFilter());
97 | ctx.getFilters().add (new LogFilter(logfile));
98 | if (ctx.getFilters().size() != 2) {
99 | throw new RuntimeException ("wrong filter list size");
100 | }
101 | ExecutorService executor = Executors.newCachedThreadPool();
102 | server.setExecutor (executor);
103 | server.start ();
104 |
105 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
106 | System.out.print ("Test14: " );
107 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
108 | InputStream is = urlc.getInputStream();
109 | int x = 0;
110 | String output="";
111 | while ((x=is.read())!= -1) {
112 | output = output + (char)x;
113 | }
114 | error = !output.equals (test_output);
115 | server.stop(2);
116 | executor.shutdown();
117 | if (error ) {
118 | throw new RuntimeException ("test failed error");
119 | }
120 | System.out.println ("OK");
121 |
122 | }
123 |
124 | public static boolean error = false;
125 |
126 | static class Handler implements HttpHandler {
127 | int invocation = 1;
128 | public void handle (HttpExchange t)
129 | throws IOException
130 | {
131 | InputStream is = t.getRequestBody();
132 | Headers map = t.getRequestHeaders();
133 | Headers rmap = t.getResponseHeaders();
134 | while (is.read () != -1) ;
135 | is.close();
136 | String response = test_input;
137 | t.sendResponseHeaders (200, response.length());
138 | OutputStream os = t.getResponseBody();
139 | os.write (response.getBytes());
140 | t.close();
141 | }
142 | }
143 | }
144 |
--------------------------------------------------------------------------------
/src/test/java/Test2.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 2006, 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 6270015
27 | * @summary Light weight HTTP server
28 | */
29 |
30 | import java.io.IOException;
31 | import java.io.InputStream;
32 | import java.net.HttpURLConnection;
33 | import java.net.InetSocketAddress;
34 | import java.net.PasswordAuthentication;
35 | import java.net.URL;
36 | import java.util.concurrent.ExecutorService;
37 | import java.util.concurrent.Executors;
38 |
39 | import org.jboss.com.sun.net.httpserver.BasicAuthenticator;
40 | import org.jboss.com.sun.net.httpserver.Headers;
41 | import org.jboss.com.sun.net.httpserver.HttpContext;
42 | import org.jboss.com.sun.net.httpserver.HttpExchange;
43 | import org.jboss.com.sun.net.httpserver.HttpHandler;
44 | import org.jboss.com.sun.net.httpserver.HttpPrincipal;
45 | import org.jboss.com.sun.net.httpserver.HttpServer;
46 |
47 | /**
48 | * Test authentication
49 | */
50 |
51 | public class Test2 extends Test {
52 |
53 | public static void main (String[] args) throws Exception {
54 | Handler handler = new Handler();
55 | InetSocketAddress addr = new InetSocketAddress (0);
56 | HttpServer server = HttpServer.create (addr, 0);
57 | HttpContext ctx = server.createContext ("/test", handler);
58 | BasicAuthenticator a = new BasicAuthenticator ("foobar@test.realm") {
59 | public boolean checkCredentials (String username, String pw) {
60 | return "fred".equals(username) && pw.charAt(0) == 'x';
61 | }
62 | };
63 |
64 | ctx.setAuthenticator (a);
65 | ExecutorService executor = Executors.newCachedThreadPool();
66 | server.setExecutor (executor);
67 | server.start ();
68 | java.net.Authenticator.setDefault (new MyAuthenticator());
69 |
70 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
71 | System.out.print ("Test2: " );
72 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
73 | InputStream is = urlc.getInputStream();
74 | int c = 0;
75 | while (is.read()!= -1) {
76 | c ++;
77 | }
78 | server.stop(2);
79 | executor.shutdown();
80 | if (error ) {
81 | throw new RuntimeException ("test failed error");
82 | }
83 | if (c != 0) {
84 | throw new RuntimeException ("test failed c");
85 | }
86 | if (count != 2) {
87 | throw new RuntimeException ("test failed count = " + count);
88 | }
89 | System.out.println ("OK");
90 |
91 | }
92 |
93 | public static boolean error = false;
94 | public static int count = 0;
95 |
96 | static class MyAuthenticator extends java.net.Authenticator {
97 | public PasswordAuthentication getPasswordAuthentication () {
98 | PasswordAuthentication pw;
99 | if (!getRequestingPrompt().equals ("foobar@test.realm")) {
100 | Test2.error = true;
101 | }
102 | if (count == 0) {
103 | pw = new PasswordAuthentication ("bad", "wrong".toCharArray());
104 | } else {
105 | pw = new PasswordAuthentication ("fred", "xyz".toCharArray());
106 | }
107 | count ++;
108 | return pw;
109 | }
110 | }
111 |
112 | static class Handler implements HttpHandler {
113 | int invocation = 1;
114 | public void handle (HttpExchange t)
115 | throws IOException
116 | {
117 | InputStream is = t.getRequestBody();
118 | Headers map = t.getRequestHeaders();
119 | Headers rmap = t.getResponseHeaders();
120 | while (is.read () != -1) ;
121 | is.close();
122 | t.sendResponseHeaders (200, -1);
123 | HttpPrincipal p = t.getPrincipal ();
124 | if (!p.getUsername().equals("fred")) {
125 | error = true;
126 | }
127 | if (!p.getRealm().equals("foobar@test.realm")) {
128 | error = true;
129 | }
130 | t.close();
131 | }
132 | }
133 | }
134 |
--------------------------------------------------------------------------------
/src/test/java/Test6.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2005, 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 6270015
27 | * @summary Light weight HTTP server
28 | */
29 |
30 | import java.io.BufferedOutputStream;
31 | import java.io.IOException;
32 | import java.io.InputStream;
33 | import java.io.OutputStream;
34 | import java.net.HttpURLConnection;
35 | import java.net.InetSocketAddress;
36 | import java.net.URL;
37 | import java.util.concurrent.ExecutorService;
38 | import java.util.concurrent.Executors;
39 |
40 | import org.jboss.com.sun.net.httpserver.Headers;
41 | import org.jboss.com.sun.net.httpserver.HttpContext;
42 | import org.jboss.com.sun.net.httpserver.HttpExchange;
43 | import org.jboss.com.sun.net.httpserver.HttpHandler;
44 | import org.jboss.com.sun.net.httpserver.HttpServer;
45 |
46 | /**
47 | * Test POST large file via chunked encoding (unusually small chunks)
48 | */
49 |
50 | public class Test6 extends Test {
51 |
52 | public static void main (String[] args) throws Exception {
53 | Handler handler = new Handler();
54 | InetSocketAddress addr = new InetSocketAddress (0);
55 | HttpServer server = HttpServer.create (addr, 0);
56 | HttpContext ctx = server.createContext ("/test", handler);
57 | ExecutorService executor = Executors.newCachedThreadPool();
58 | server.setExecutor (executor);
59 | server.start ();
60 |
61 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
62 | System.out.print ("Test6: " );
63 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
64 | urlc.setDoOutput (true);
65 | urlc.setRequestMethod ("POST");
66 | urlc.setChunkedStreamingMode (32); // small chunks
67 | OutputStream os = new BufferedOutputStream (urlc.getOutputStream());
68 | for (int i=0; i browserCharsetMap = new HashMap();
49 |
50 | static {
51 | browserCharsetMap.put(Pattern.compile(".*Firefox.*"), Charset.forName("8859_1"));
52 | }
53 |
54 | // set up one server instance for all tests to speed things up
55 | @BeforeClass
56 | public static void setUpServer() throws Exception {
57 | Handler handler = new Handler();
58 | InetSocketAddress addr = new InetSocketAddress (0);
59 | server = HttpServer.create (addr, 0);
60 | HttpContext ctx = server.createContext ("/test", handler);
61 |
62 | authenticator = new SimpleAuthenticator();
63 | ctx.setAuthenticator (authenticator);
64 | executor = Executors.newCachedThreadPool();
65 | server.setExecutor (executor);
66 | server.start ();
67 | }
68 |
69 | @AfterClass
70 | public static void shutDownServer() {
71 | server.stop(2);
72 | executor.shutdown();
73 | }
74 |
75 | @After
76 | public void cleanUpAllowedCredentials() {
77 | authenticator.purge();
78 | }
79 |
80 | @Test
81 | public void testASCIIPassword() throws Exception {
82 | authenticator.accept("fred", "xyz");
83 |
84 | final int responseCode = makeCall("fred", "xyz", null, "UTF-8");
85 |
86 | Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);
87 | }
88 |
89 | @Test
90 | public void testNonAsciiPasswordOnUtf8Browser() throws Exception {
91 | authenticator.accept("fred", "test123!ü");
92 |
93 | final int responseCode = makeCall("fred", "test123!ü", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36", "UTF-8");
94 |
95 | Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);
96 | }
97 |
98 | @Test
99 | public void testNonAsciiPasswordOnIso8859Browser() throws Exception {
100 | authenticator.accept("fred", "test123!ü");
101 |
102 | final int responseCode = makeCall("fred", "test123!ü", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1", "8859_1");
103 |
104 | Assert.assertEquals(HttpURLConnection.HTTP_OK, responseCode);
105 | }
106 |
107 | private int makeCall(String username, String password, String userAgent, String encoding) throws IOException {
108 | URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
109 | HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
110 |
111 | final String encodedCredentials = Base64.byteArrayToBase64((username + ":" + password).getBytes(encoding));
112 | urlc.addRequestProperty("Authorization", "Basic " + encodedCredentials);
113 | if (userAgent != null) {
114 | urlc.addRequestProperty("User-Agent", userAgent);
115 | }
116 | urlc.setRequestMethod("GET");
117 |
118 | return urlc.getResponseCode();
119 | }
120 |
121 | public static boolean error = false;
122 |
123 |
124 | static class SimpleAuthenticator extends BasicAuthenticator {
125 | private Map acceptedCredentials = new HashMap();
126 |
127 | SimpleAuthenticator() {
128 | super ("foobar@test.realm", Charset.forName("UTF-8"), BZ1312064.browserCharsetMap);
129 | }
130 |
131 | public boolean checkCredentials (String username, String pw) {
132 | return acceptedCredentials.containsKey(username) && acceptedCredentials.get(username).equals(pw);
133 | }
134 |
135 | public void accept(String username, String password) {
136 | acceptedCredentials.put(username, password);
137 | }
138 |
139 | public void purge() {
140 | acceptedCredentials.clear();
141 | }
142 | }
143 |
144 | static class Handler implements HttpHandler {
145 | public void handle (HttpExchange t)
146 | throws IOException
147 | {
148 | t.sendResponseHeaders (200, -1);
149 | t.close();
150 | }
151 | }
152 | }
153 |
--------------------------------------------------------------------------------
/src/test/java/testkeys:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/jbossas/httpserver/54a58ba95d5b47dc04e0ce047ab4c4b83811c381/src/test/java/testkeys
--------------------------------------------------------------------------------