HTTP.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {UAS_parser as UAParser} from "../lib/uas_parser.js";
  2. /**
  3. * HTTP operations.
  4. *
  5. * @author n1474335 [n1474335@gmail.com]
  6. * @copyright Crown Copyright 2016
  7. * @license Apache-2.0
  8. *
  9. * @namespace
  10. */
  11. const HTTP = {
  12. /**
  13. * @constant
  14. * @default
  15. */
  16. METHODS: [
  17. "GET", "POST", "HEAD",
  18. "PUT", "PATCH", "DELETE",
  19. "CONNECT", "TRACE", "OPTIONS"
  20. ],
  21. /**
  22. * Strip HTTP headers operation.
  23. *
  24. * @param {string} input
  25. * @param {Object[]} args
  26. * @returns {string}
  27. */
  28. runStripHeaders: function(input, args) {
  29. let headerEnd = input.indexOf("\r\n\r\n");
  30. headerEnd = (headerEnd < 0) ? input.indexOf("\n\n") + 2 : headerEnd + 4;
  31. return (headerEnd < 2) ? input : input.slice(headerEnd, input.length);
  32. },
  33. /**
  34. * Parse User Agent operation.
  35. *
  36. * @param {string} input
  37. * @param {Object[]} args
  38. * @returns {string}
  39. */
  40. runParseUserAgent: function(input, args) {
  41. const ua = UAParser.parse(input);
  42. return "Type: " + ua.type + "\n" +
  43. "Family: " + ua.uaFamily + "\n" +
  44. "Name: " + ua.uaName + "\n" +
  45. "URL: " + ua.uaUrl + "\n" +
  46. "Company: " + ua.uaCompany + "\n" +
  47. "Company URL: " + ua.uaCompanyUrl + "\n\n" +
  48. "OS Family: " + ua.osFamily + "\n" +
  49. "OS Name: " + ua.osName + "\n" +
  50. "OS URL: " + ua.osUrl + "\n" +
  51. "OS Company: " + ua.osCompany + "\n" +
  52. "OS Company URL: " + ua.osCompanyUrl + "\n" +
  53. "Device Type: " + ua.deviceType + "\n";
  54. },
  55. /**
  56. * @constant
  57. * @default
  58. */
  59. MODE: [
  60. "Cross-Origin Resource Sharing",
  61. "No CORS (limited to HEAD, GET or POST)",
  62. ],
  63. /**
  64. * Lookup table for HTTP modes
  65. *
  66. * @private
  67. * @constant
  68. */
  69. _modeLookup: {
  70. "Cross-Origin Resource Sharing": "cors",
  71. "No CORS (limited to HEAD, GET or POST)": "no-cors",
  72. },
  73. /**
  74. * HTTP request operation.
  75. *
  76. * @author tlwr [toby@toby.codes]
  77. * @author n1474335 [n1474335@gmail.com]
  78. * @param {string} input
  79. * @param {Object[]} args
  80. * @returns {string}
  81. */
  82. runHTTPRequest(input, args) {
  83. const method = args[0],
  84. url = args[1],
  85. headersText = args[2],
  86. mode = args[3],
  87. showResponseMetadata = args[4];
  88. if (url.length === 0) return "";
  89. let headers = new Headers();
  90. headersText.split(/\r?\n/).forEach(line => {
  91. line = line.trim();
  92. if (line.length === 0) return;
  93. let split = line.split(":");
  94. if (split.length !== 2) throw `Could not parse header in line: ${line}`;
  95. headers.set(split[0].trim(), split[1].trim());
  96. });
  97. let config = {
  98. method: method,
  99. headers: headers,
  100. mode: HTTP._modeLookup[mode],
  101. cache: "no-cache",
  102. };
  103. if (method !== "GET" && method !== "HEAD") {
  104. config.body = input;
  105. }
  106. return fetch(url, config)
  107. .then(r => {
  108. if (r.status === 0 && r.type === "opaque") {
  109. return "Error: Null response. Try setting the connection mode to CORS.";
  110. }
  111. if (showResponseMetadata) {
  112. let headers = "";
  113. for (let pair of r.headers.entries()) {
  114. headers += " " + pair[0] + ": " + pair[1] + "\n";
  115. }
  116. return r.text().then(b => {
  117. return "####\n Status: " + r.status + " " + r.statusText +
  118. "\n Exposed headers:\n" + headers + "####\n\n" + b;
  119. });
  120. }
  121. return r.text();
  122. })
  123. .catch(e => {
  124. return e.toString() +
  125. "\n\nThis error could be caused by one of the following:\n" +
  126. " - An invalid URL\n" +
  127. " - Making a request to an insecure resource (HTTP) from a secure source (HTTPS)\n" +
  128. " - Making a cross-origin request to a server which does not support CORS\n";
  129. });
  130. },
  131. };
  132. export default HTTP;