Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/net/www/http/HttpCapture.java
38923 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.www.http;2627import java.io.*;28import java.util.ArrayList;29import java.util.regex.*;30import sun.net.NetProperties;31import sun.util.logging.PlatformLogger;3233/**34* Main class of the HTTP traffic capture tool.35* Captures are triggered by the sun.net.http.captureRules system property.36* If set, it should point to a file containing the capture rules.37* Format for the file is simple:38* - 1 rule per line39* - Lines starting with a # are considered comments and ignored40* - a rule is a pair of a regular expression and file pattern, separated by a comma41* - The regular expression is applied to URLs, if it matches, the traffic for42* that URL will be captured in the associated file.43* - if the file name contains a '%d', then that sequence will be replaced by a44* unique random number for each URL. This allow for multi-threaded captures45* of URLs matching the same pattern.46* - Rules are checked in sequence, in the same order as in the file, until a47* match is found or the end of the list is reached.48*49* Examples of rules:50* www\.sun\.com , sun%d.log51* yahoo\.com\/.*asf , yahoo.log52*53* @author jccollet54*/55public class HttpCapture {56private File file = null;57private boolean incoming = true;58private BufferedWriter out = null;59private static boolean initialized = false;60private static volatile ArrayList<Pattern> patterns = null;61private static volatile ArrayList<String> capFiles = null;6263private static synchronized void init() {64initialized = true;65String rulesFile = java.security.AccessController.doPrivileged(66new java.security.PrivilegedAction<String>() {67public String run() {68return NetProperties.get("sun.net.http.captureRules");69}70});71if (rulesFile != null && !rulesFile.isEmpty()) {72BufferedReader in;73try {74in = new BufferedReader(new FileReader(rulesFile));75} catch (FileNotFoundException ex) {76return;77}78try {79String line = in.readLine();80while (line != null) {81line = line.trim();82if (!line.startsWith("#")) {83// skip line if it's a comment84String[] s = line.split(",");85if (s.length == 2) {86if (patterns == null) {87patterns = new ArrayList<Pattern>();88capFiles = new ArrayList<String>();89}90patterns.add(Pattern.compile(s[0].trim()));91capFiles.add(s[1].trim());92}93}94line = in.readLine();95}96} catch (IOException ioe) {9798} finally {99try {100in.close();101} catch (IOException ex) {102}103}104}105}106107private static synchronized boolean isInitialized() {108return initialized;109}110111private HttpCapture(File f, java.net.URL url) {112file = f;113try {114out = new BufferedWriter(new FileWriter(file, true));115out.write("URL: " + url + "\n");116} catch (IOException ex) {117PlatformLogger.getLogger(HttpCapture.class.getName()).severe(null, ex);118}119}120121public synchronized void sent(int c) throws IOException {122if (incoming) {123out.write("\n------>\n");124incoming = false;125out.flush();126}127out.write(c);128}129130public synchronized void received(int c) throws IOException {131if (!incoming) {132out.write("\n<------\n");133incoming = true;134out.flush();135}136out.write(c);137}138139public synchronized void flush() throws IOException {140out.flush();141}142143public static HttpCapture getCapture(java.net.URL url) {144if (!isInitialized()) {145init();146}147if (patterns == null || patterns.isEmpty()) {148return null;149}150String s = url.toString();151for (int i = 0; i < patterns.size(); i++) {152Pattern p = patterns.get(i);153if (p.matcher(s).find()) {154String f = capFiles.get(i);155File fi;156if (f.indexOf("%d") >= 0) {157java.util.Random rand = new java.util.Random();158do {159String f2 = f.replace("%d", Integer.toString(rand.nextInt()));160fi = new File(f2);161} while (fi.exists());162} else {163fi = new File(f);164}165return new HttpCapture(fi, url);166}167}168return null;169}170}171172173