StripHTTPHeaders.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /**
  2. * @author n1474335 [n1474335@gmail.com]
  3. * @copyright Crown Copyright 2016
  4. * @license Apache-2.0
  5. */
  6. import Operation from "../Operation.mjs";
  7. /**
  8. * Strip HTTP headers operation
  9. */
  10. class StripHTTPHeaders extends Operation {
  11. /**
  12. * StripHTTPHeaders constructor
  13. */
  14. constructor() {
  15. super();
  16. this.name = "Strip HTTP headers";
  17. this.module = "Default";
  18. this.description = "Removes HTTP headers from a request or response by looking for the first instance of a double newline.";
  19. this.infoURL = "https://wikipedia.org/wiki/Hypertext_Transfer_Protocol#Message_format";
  20. this.inputType = "string";
  21. this.outputType = "string";
  22. this.args = [];
  23. this.checks = {
  24. input: {
  25. regex: [
  26. {
  27. match: "^\\s*HTTP(.|\\s)+?(\\r?\\n){2}",
  28. flags: "",
  29. args: []
  30. }
  31. ]
  32. }
  33. };
  34. }
  35. /**
  36. * @param {string} input
  37. * @param {Object[]} args
  38. * @returns {string}
  39. */
  40. run(input, args) {
  41. let headerEnd = input.indexOf("\r\n\r\n");
  42. headerEnd = (headerEnd < 0) ? input.indexOf("\n\n") + 2 : headerEnd + 4;
  43. return (headerEnd < 2) ? input : input.slice(headerEnd, input.length);
  44. }
  45. }
  46. export default StripHTTPHeaders;