-
Notifications
You must be signed in to change notification settings - Fork 95
/
HttpServerTest.java
executable file
·119 lines (101 loc) · 4.24 KB
/
HttpServerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright 2015 Odnoklassniki Ltd, Mail.Ru Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package one.nio.http;
import one.nio.net.Socket;
import one.nio.net.SslOption;
import one.nio.util.Utf8;
import java.io.IOException;
public class HttpServerTest extends HttpServer {
public HttpServerTest(HttpServerConfig config) throws IOException {
super(config);
}
@Path("/simple")
public Response handleSimple() {
return Response.ok("Simple");
}
@Path({"/multi1", "/multi2"})
public void handleMultiple(Request request, HttpSession session) throws IOException {
Response response = Response.ok("Multiple: " + request.getPath());
session.sendResponse(response);
}
@Override
public void handleRequest(Request request, HttpSession session) throws IOException {
try {
super.handleRequest(request, session);
} catch (RuntimeException e) {
session.sendError(Response.BAD_REQUEST, e.toString());
}
}
@Path("/param")
public Response handleParam(@Param("i") int i,
@Param("l=123") Long l,
@Param(value = "s", required = true) String s,
@Header(value = "Host", required = true) String host,
@Header("User-Agent") String agent) throws IOException {
String params = "i = " + i + "\r\nl = " + l + "\r\ns = " + s + "\r\n";
String headers = "host = " + host + "\r\nagent = " + agent + "\r\n";
Response response = Response.ok(Utf8.toBytes("<html><body><pre>" + params + headers + "</pre></body></html>"));
response.addHeader("Content-Type: text/html");
return response;
}
@Path("/cert")
public Response handleCert(HttpSession session) {
System.out.println("handleCert");
Socket socket = session.socket();
byte[] cert = socket.getSslOption(SslOption.PEER_CERTIFICATE);
String subject = socket.getSslOption(SslOption.PEER_SUBJECT);
String issuer = socket.getSslOption(SslOption.PEER_ISSUER);
return Response.ok("Client certificate: " + (cert == null ? "none" : cert.length + " bytes") + "\n"
+ "Subject: " + subject + "\n"
+ "Issuer: " + issuer + "\n");
}
@Path("/session")
public Response handleSession(HttpSession session) {
Socket socket = session.socket();
Boolean reused = socket.getSslOption(SslOption.SESSION_REUSED);
Integer ticket = socket.getSslOption(SslOption.SESSION_TICKET);
StringBuilder result = new StringBuilder("SSL session flags:");
if (reused != null && reused) {
result.append(" SESSION_REUSED");
}
if (ticket != null) {
if (ticket == 1) {
result.append(" TICKET_REUSED");
} else if (ticket == 2) {
result.append(" OLD_TICKET_REUSED");
} else if (ticket == 3) {
result.append(" NEW_TICKET");
}
}
return Response.ok(result.toString());
}
@Override
public void handleDefault(Request request, HttpSession session) throws IOException {
Response response = Response.ok(Utf8.toBytes("<html><body><pre>Default</pre></body></html>"));
response.addHeader("Content-Type: text/html");
session.sendResponse(response);
}
public static void main(String[] args) throws Exception {
HttpServerConfig config;
if (args.length > 0) {
config = HttpServerConfigFactory.fromFile(args[0]);
} else {
config = HttpServerConfigFactory.create(8080);
}
HttpServerTest server = new HttpServerTest(config);
server.start();
}
}