1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. public class HeaderInstrumenter {
  8. private static final String CONFIG_FILE = "instrument_config.txt";
  9. private static final Pattern HEADER_PATTERN = Pattern.compile("(?i)(.*)\\s+:\\s+(.*)"); // Matches header lines
  10. public static void instrumentHeaders(String filePath, Map<String, String> instrumentationRules) throws IOException {
  11. // Load instrumentation rules from the configuration file
  12. Map<String, String> rules = loadInstrumentationRules(CONFIG_FILE);
  13. if (rules == null) {
  14. System.err.println("Error: Could not load instrumentation rules from " + CONFIG_FILE);
  15. return;
  16. }
  17. File file = new File(filePath);
  18. if (!file.exists()) {
  19. System.err.println("Error: File not found: " + filePath);
  20. return;
  21. }
  22. try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(file))) {
  23. String line;
  24. java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(filePath, false)); // Overwrite the file
  25. while ((line = reader.readLine()) != null) {
  26. Matcher matcher = HEADER_PATTERN.matcher(line);
  27. if (matcher.matches()) {
  28. String headerName = matcher.group(1).trim();
  29. String headerValue = matcher.group(2).trim();
  30. // Apply instrumentation rule if available
  31. if (rules.containsKey(headerName)) {
  32. String replacement = rules.get(headerName);
  33. String newHeaderValue = applyReplacement(headerValue, replacement);
  34. line = matcher.appendReplacement(line, headerName + ": " + newHeaderValue);
  35. }
  36. }
  37. writer.write(line);
  38. }
  39. writer.flush(); // Ensure all data is written
  40. }
  41. }
  42. private static Map<String, String> loadInstrumentationRules(String configFilePath) throws IOException {
  43. Map<String, String> rules = new HashMap<>();
  44. File configFile = new File(configFilePath);
  45. if (!configFile.exists()) {
  46. return null; // Return null if the config file doesn't exist.
  47. }
  48. try (java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(configFile))) {
  49. String line;
  50. while ((line = reader.readLine()) != null) {
  51. String[] parts = line.split(":", 2); // Split only at the first colon
  52. if (parts.length == 2) {
  53. String headerName = parts[0].trim();
  54. String replacement = parts[1].trim();
  55. rules.put(headerName, replacement);
  56. }
  57. }
  58. }
  59. return rules;
  60. }
  61. private static String applyReplacement(String value, String replacement) {
  62. //Simple replacement example. Can be expanded.
  63. if (replacement.equals("SANDBOXED")) {
  64. return "SANDBOXED_" + value;
  65. }
  66. return value;
  67. }
  68. public static void main(String[] args) throws IOException {
  69. // Example Usage
  70. String filePath = "input.txt"; // Replace with your file path
  71. //Create dummy input file
  72. File file = new File(filePath);
  73. java.io.FileWriter writer = new java.io.FileWriter(file, "Content-Type: text/html\n" +
  74. "Content-Length: 123\n" +
  75. "X-Custom-Header: original_value\n" +
  76. "AnotherHeader: another_value");
  77. writer.close();
  78. Map<String, String> rules = new HashMap<>();
  79. rules.put("X-Custom-Header", "SANDBOXED");
  80. instrumentHeaders(filePath, rules);
  81. System.out.println("Headers instrumented in " + filePath);
  82. }
  83. }

Add your comment