import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HeaderInstrumenter {
private static final String CONFIG_FILE = "instrument_config.txt";
private static final Pattern HEADER_PATTERN = Pattern.compile("(?i)(.*)\\s+:\\s+(.*)"); // Matches header lines
public static void instrumentHeaders(String filePath, Map<String, String> instrumentationRules) throws IOException {
// Load instrumentation rules from the configuration file
Map<String, String> rules = loadInstrumentationRules(CONFIG_FILE);
if (rules == null) {
System.err.println("Error: Could not load instrumentation rules from " + CONFIG_FILE);
return;
}
File file = new File(filePath);
if (!file.exists()) {
System.err.println("Error: File not found: " + filePath);
return;
}
try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(file))) {
String line;
java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(filePath, false)); // Overwrite the file
while ((line = reader.readLine()) != null) {
Matcher matcher = HEADER_PATTERN.matcher(line);
if (matcher.matches()) {
String headerName = matcher.group(1).trim();
String headerValue = matcher.group(2).trim();
// Apply instrumentation rule if available
if (rules.containsKey(headerName)) {
String replacement = rules.get(headerName);
String newHeaderValue = applyReplacement(headerValue, replacement);
line = matcher.appendReplacement(line, headerName + ": " + newHeaderValue);
}
}
writer.write(line);
}
writer.flush(); // Ensure all data is written
}
}
private static Map<String, String> loadInstrumentationRules(String configFilePath) throws IOException {
Map<String, String> rules = new HashMap<>();
File configFile = new File(configFilePath);
if (!configFile.exists()) {
return null; // Return null if the config file doesn't exist.
}
try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(configFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(":", 2); // Split only at the first colon
if (parts.length == 2) {
String headerName = parts[0].trim();
String replacement = parts[1].trim();
rules.put(headerName, replacement);
}
}
}
return rules;
}
private static String applyReplacement(String value, String replacement) {
//Simple replacement example. Can be expanded.
if (replacement.equals("SANDBOXED")) {
return "SANDBOXED_" + value;
}
return value;
}
public static void main(String[] args) throws IOException {
// Example Usage
String filePath = "input.txt"; // Replace with your file path
//Create dummy input file
File file = new File(filePath);
java.io.FileWriter writer = new java.io.FileWriter(file, "Content-Type: text/html\n" +
"Content-Length: 123\n" +
"X-Custom-Header: original_value\n" +
"AnotherHeader: another_value");
writer.close();
Map<String, String> rules = new HashMap<>();
rules.put("X-Custom-Header", "SANDBOXED");
instrumentHeaders(filePath, rules);
System.out.println("Headers instrumented in " + filePath);
}
}
Add your comment