Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/EchoHandler.java
41152 views
1
/*
2
* Copyright (c) 2005, 2019, 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.IOException;
25
import java.io.InputStream;
26
import java.io.OutputStream;
27
import java.nio.charset.StandardCharsets;
28
29
import com.sun.net.httpserver.Headers;
30
import com.sun.net.httpserver.HttpExchange;
31
import com.sun.net.httpserver.HttpHandler;
32
33
/**
34
* Implements a basic static EchoHandler for an HTTP server
35
*/
36
public class EchoHandler implements HttpHandler {
37
public void handle (HttpExchange t)
38
throws IOException
39
{
40
InputStream is = t.getRequestBody();
41
Headers map = t.getRequestHeaders();
42
String fixedrequest = map.getFirst ("XFixed");
43
44
// return the number of bytes received (no echo)
45
String summary = map.getFirst ("XSummary");
46
OutputStream os = t.getResponseBody();
47
byte[] in;
48
in = is.readAllBytes();
49
if (summary != null) {
50
in = Integer.toString(in.length).getBytes(StandardCharsets.UTF_8);
51
}
52
if (fixedrequest != null) {
53
t.sendResponseHeaders(200, in.length == 0 ? -1 : in.length);
54
} else {
55
t.sendResponseHeaders(200, 0);
56
}
57
os.write(in);
58
close(t, os);
59
close(t, is);
60
}
61
62
protected void close(OutputStream os) throws IOException {
63
os.close();
64
}
65
protected void close(InputStream is) throws IOException {
66
is.close();
67
}
68
protected void close(HttpExchange t, OutputStream os) throws IOException {
69
close(os);
70
}
71
protected void close(HttpExchange t, InputStream is) throws IOException {
72
close(is);
73
}
74
}
75
76