Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/net/www/http/HttpCapture.java
41161 views
1
/*
2
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. 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 sun.net.www.http;
27
28
import java.io.*;
29
import java.util.ArrayList;
30
import java.util.regex.*;
31
import sun.net.NetProperties;
32
import sun.util.logging.PlatformLogger;
33
34
/**
35
* Main class of the HTTP traffic capture tool.
36
* Captures are triggered by the sun.net.http.captureRules system property.
37
* If set, it should point to a file containing the capture rules.
38
* Format for the file is simple:
39
* - 1 rule per line
40
* - Lines starting with a # are considered comments and ignored
41
* - a rule is a pair of a regular expression and file pattern, separated by a comma
42
* - The regular expression is applied to URLs, if it matches, the traffic for
43
* that URL will be captured in the associated file.
44
* - if the file name contains a '%d', then that sequence will be replaced by a
45
* unique random number for each URL. This allow for multi-threaded captures
46
* of URLs matching the same pattern.
47
* - Rules are checked in sequence, in the same order as in the file, until a
48
* match is found or the end of the list is reached.
49
*
50
* Examples of rules:
51
* www\.sun\.com , sun%d.log
52
* yahoo\.com\/.*asf , yahoo.log
53
*
54
* @author jccollet
55
*/
56
public class HttpCapture {
57
// HttpCapture does blocking I/O operations while holding monitors.
58
// This is not a concern because it is rarely used.
59
private File file;
60
private boolean incoming = true;
61
private BufferedWriter out;
62
private static boolean initialized;
63
private static volatile ArrayList<Pattern> patterns;
64
private static volatile ArrayList<String> capFiles;
65
66
private static synchronized void init() {
67
initialized = true;
68
@SuppressWarnings("removal")
69
String rulesFile = java.security.AccessController.doPrivileged(
70
new java.security.PrivilegedAction<>() {
71
public String run() {
72
return NetProperties.get("sun.net.http.captureRules");
73
}
74
});
75
if (rulesFile != null && !rulesFile.isEmpty()) {
76
BufferedReader in;
77
try {
78
in = new BufferedReader(new FileReader(rulesFile));
79
} catch (FileNotFoundException ex) {
80
return;
81
}
82
try {
83
String line = in.readLine();
84
while (line != null) {
85
line = line.trim();
86
if (!line.startsWith("#")) {
87
// skip line if it's a comment
88
String[] s = line.split(",");
89
if (s.length == 2) {
90
if (patterns == null) {
91
patterns = new ArrayList<>();
92
capFiles = new ArrayList<>();
93
}
94
patterns.add(Pattern.compile(s[0].trim()));
95
capFiles.add(s[1].trim());
96
}
97
}
98
line = in.readLine();
99
}
100
} catch (IOException ioe) {
101
102
} finally {
103
try {
104
in.close();
105
} catch (IOException ex) {
106
}
107
}
108
}
109
}
110
111
private static synchronized boolean isInitialized() {
112
return initialized;
113
}
114
115
private HttpCapture(File f, java.net.URL url) {
116
file = f;
117
try {
118
out = new BufferedWriter(new FileWriter(file, true));
119
out.write("URL: " + url + "\n");
120
} catch (IOException ex) {
121
PlatformLogger.getLogger(HttpCapture.class.getName()).severe(null, ex);
122
}
123
}
124
125
public synchronized void sent(int c) throws IOException {
126
if (incoming) {
127
out.write("\n------>\n");
128
incoming = false;
129
out.flush();
130
}
131
out.write(c);
132
}
133
134
public synchronized void received(int c) throws IOException {
135
if (!incoming) {
136
out.write("\n<------\n");
137
incoming = true;
138
out.flush();
139
}
140
out.write(c);
141
}
142
143
public synchronized void flush() throws IOException {
144
out.flush();
145
}
146
147
public static HttpCapture getCapture(java.net.URL url) {
148
if (!isInitialized()) {
149
init();
150
}
151
if (patterns == null || patterns.isEmpty()) {
152
return null;
153
}
154
String s = url.toString();
155
for (int i = 0; i < patterns.size(); i++) {
156
Pattern p = patterns.get(i);
157
if (p.matcher(s).find()) {
158
String f = capFiles.get(i);
159
File fi;
160
if (f.indexOf("%d") >= 0) {
161
java.util.Random rand = new java.util.Random();
162
do {
163
String f2 = f.replace("%d", Integer.toString(rand.nextInt()));
164
fi = new File(f2);
165
} while (fi.exists());
166
} else {
167
fi = new File(f);
168
}
169
return new HttpCapture(fi, url);
170
}
171
}
172
return null;
173
}
174
}
175
176