author | dfuchs |
Fri, 30 Aug 2019 15:42:27 +0100 | |
branch | JDK-8229867-branch |
changeset 57968 | 8595871a5446 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
3059 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
3059 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
3059 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
3059 | 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 |
* |
|
5506 | 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. |
|
3059 | 24 |
*/ |
25 |
||
26 |
package sun.net.www.http; |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3217
diff
changeset
|
27 |
|
3059 | 28 |
import java.io.*; |
29 |
import java.util.ArrayList; |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3217
diff
changeset
|
30 |
import java.util.regex.*; |
3059 | 31 |
import sun.net.NetProperties; |
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3217
diff
changeset
|
32 |
import sun.util.logging.PlatformLogger; |
3059 | 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 |
*/ |
|
57968 | 56 |
// Using synchronized should be safe here. |
3059 | 57 |
public class HttpCapture { |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
58 |
private File file; |
3059 | 59 |
private boolean incoming = true; |
34774
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
60 |
private BufferedWriter out; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
61 |
private static boolean initialized; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
62 |
private static volatile ArrayList<Pattern> patterns; |
03b4e6dc367b
8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents:
29986
diff
changeset
|
63 |
private static volatile ArrayList<String> capFiles; |
3059 | 64 |
|
65 |
private static synchronized void init() { |
|
66 |
initialized = true; |
|
67 |
String rulesFile = java.security.AccessController.doPrivileged( |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
68 |
new java.security.PrivilegedAction<>() { |
3059 | 69 |
public String run() { |
70 |
return NetProperties.get("sun.net.http.captureRules"); |
|
71 |
} |
|
72 |
}); |
|
73 |
if (rulesFile != null && !rulesFile.isEmpty()) { |
|
74 |
BufferedReader in; |
|
75 |
try { |
|
76 |
in = new BufferedReader(new FileReader(rulesFile)); |
|
77 |
} catch (FileNotFoundException ex) { |
|
78 |
return; |
|
79 |
} |
|
80 |
try { |
|
81 |
String line = in.readLine(); |
|
82 |
while (line != null) { |
|
83 |
line = line.trim(); |
|
84 |
if (!line.startsWith("#")) { |
|
85 |
// skip line if it's a comment |
|
86 |
String[] s = line.split(","); |
|
87 |
if (s.length == 2) { |
|
88 |
if (patterns == null) { |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
89 |
patterns = new ArrayList<>(); |
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
25859
diff
changeset
|
90 |
capFiles = new ArrayList<>(); |
3059 | 91 |
} |
92 |
patterns.add(Pattern.compile(s[0].trim())); |
|
93 |
capFiles.add(s[1].trim()); |
|
94 |
} |
|
95 |
} |
|
96 |
line = in.readLine(); |
|
97 |
} |
|
98 |
} catch (IOException ioe) { |
|
99 |
||
100 |
} finally { |
|
101 |
try { |
|
102 |
in.close(); |
|
103 |
} catch (IOException ex) { |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
private static synchronized boolean isInitialized() { |
|
110 |
return initialized; |
|
111 |
} |
|
112 |
||
113 |
private HttpCapture(File f, java.net.URL url) { |
|
114 |
file = f; |
|
115 |
try { |
|
116 |
out = new BufferedWriter(new FileWriter(file, true)); |
|
117 |
out.write("URL: " + url + "\n"); |
|
118 |
} catch (IOException ex) { |
|
3866
34cd368bd2dd
6882384: Update http protocol handler to use PlatformLogger
chegar
parents:
3217
diff
changeset
|
119 |
PlatformLogger.getLogger(HttpCapture.class.getName()).severe(null, ex); |
3059 | 120 |
} |
121 |
} |
|
122 |
||
123 |
public synchronized void sent(int c) throws IOException { |
|
124 |
if (incoming) { |
|
125 |
out.write("\n------>\n"); |
|
126 |
incoming = false; |
|
127 |
out.flush(); |
|
128 |
} |
|
129 |
out.write(c); |
|
130 |
} |
|
131 |
||
132 |
public synchronized void received(int c) throws IOException { |
|
133 |
if (!incoming) { |
|
134 |
out.write("\n<------\n"); |
|
135 |
incoming = true; |
|
136 |
out.flush(); |
|
137 |
} |
|
138 |
out.write(c); |
|
139 |
} |
|
140 |
||
141 |
public synchronized void flush() throws IOException { |
|
142 |
out.flush(); |
|
143 |
} |
|
144 |
||
145 |
public static HttpCapture getCapture(java.net.URL url) { |
|
146 |
if (!isInitialized()) { |
|
147 |
init(); |
|
148 |
} |
|
149 |
if (patterns == null || patterns.isEmpty()) { |
|
150 |
return null; |
|
151 |
} |
|
152 |
String s = url.toString(); |
|
153 |
for (int i = 0; i < patterns.size(); i++) { |
|
154 |
Pattern p = patterns.get(i); |
|
155 |
if (p.matcher(s).find()) { |
|
156 |
String f = capFiles.get(i); |
|
157 |
File fi; |
|
158 |
if (f.indexOf("%d") >= 0) { |
|
159 |
java.util.Random rand = new java.util.Random(); |
|
160 |
do { |
|
161 |
String f2 = f.replace("%d", Integer.toString(rand.nextInt())); |
|
162 |
fi = new File(f2); |
|
163 |
} while (fi.exists()); |
|
164 |
} else { |
|
165 |
fi = new File(f); |
|
166 |
} |
|
167 |
return new HttpCapture(fi, url); |
|
168 |
} |
|
169 |
} |
|
170 |
return null; |
|
171 |
} |
|
172 |
} |