net.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. const mimeCategoryMap = {
  6. "text/plain": "txt",
  7. "application/octet-stream": "bin",
  8. "text/html": "html",
  9. "text/xml": "html",
  10. "application/xml": "html",
  11. "application/rss+xml": "html",
  12. "application/atom+xml": "html",
  13. "application/xhtml+xml": "html",
  14. "application/mathml+xml": "html",
  15. "application/rdf+xml": "html",
  16. "text/css": "css",
  17. "application/x-javascript": "js",
  18. "text/javascript": "js",
  19. "application/javascript": "js",
  20. "text/ecmascript": "js",
  21. "application/ecmascript": "js",
  22. "image/jpeg": "image",
  23. "image/jpg": "image",
  24. "image/gif": "image",
  25. "image/png": "image",
  26. "image/bmp": "image",
  27. "application/x-shockwave-flash": "plugin",
  28. "application/x-silverlight-app": "plugin",
  29. "video/x-flv": "media",
  30. "audio/mpeg3": "media",
  31. "audio/x-mpeg-3": "media",
  32. "video/mpeg": "media",
  33. "video/x-mpeg": "media",
  34. "video/webm": "media",
  35. "video/mp4": "media",
  36. "video/ogg": "media",
  37. "audio/ogg": "media",
  38. "application/ogg": "media",
  39. "application/x-ogg": "media",
  40. "application/x-midi": "media",
  41. "audio/midi": "media",
  42. "audio/x-mid": "media",
  43. "audio/x-midi": "media",
  44. "music/crescendo": "media",
  45. "audio/wav": "media",
  46. "audio/x-wav": "media",
  47. "application/x-woff": "font",
  48. "application/font-woff": "font",
  49. "application/x-font-woff": "font",
  50. "application/x-ttf": "font",
  51. "application/x-font-ttf": "font",
  52. "font/ttf": "font",
  53. "font/woff": "font",
  54. "application/x-otf": "font",
  55. "application/x-font-otf": "font"
  56. };
  57. var NetUtils = {};
  58. NetUtils.isImage = function (contentType) {
  59. if (!contentType) {
  60. return false;
  61. }
  62. contentType = contentType.split(";")[0];
  63. contentType = contentType.trim();
  64. return mimeCategoryMap[contentType] == "image";
  65. };
  66. NetUtils.isHTML = function (contentType) {
  67. if (!contentType) {
  68. return false;
  69. }
  70. contentType = contentType.split(";")[0];
  71. contentType = contentType.trim();
  72. return mimeCategoryMap[contentType] == "html";
  73. };
  74. NetUtils.getHeaderValue = function (headers, name) {
  75. if (!headers) {
  76. return null;
  77. }
  78. name = name.toLowerCase();
  79. for (let i = 0; i < headers.length; ++i) {
  80. let headerName = headers[i].name.toLowerCase();
  81. if (headerName == name) {
  82. return headers[i].value;
  83. }
  84. }
  85. };
  86. NetUtils.parseXml = function (content) {
  87. let contentType = content.mimeType.split(";")[0];
  88. contentType = contentType.trim();
  89. let parser = new DOMParser();
  90. let doc = parser.parseFromString(content.text, contentType);
  91. let root = doc.documentElement;
  92. // Error handling
  93. let nsURI = "http://www.mozilla.org/newlayout/xml/parsererror.xml";
  94. if (root.namespaceURI == nsURI && root.nodeName == "parsererror") {
  95. return null;
  96. }
  97. return doc;
  98. };
  99. NetUtils.isURLEncodedRequest = function (file) {
  100. let mimeType = "application/x-www-form-urlencoded";
  101. let postData = file.request.postData;
  102. if (postData && postData.text) {
  103. let text = postData.text.toLowerCase();
  104. if (text.startsWith("content-type: " + mimeType)) {
  105. return true;
  106. }
  107. }
  108. let value = NetUtils.getHeaderValue(file.request.headers, "content-type");
  109. return value && value.startsWith(mimeType);
  110. };
  111. NetUtils.isMultiPartRequest = function (file) {
  112. let mimeType = "multipart/form-data";
  113. let value = NetUtils.getHeaderValue(file.request.headers, "content-type");
  114. return value && value.startsWith(mimeType);
  115. };
  116. // Exports from this module
  117. module.exports = NetUtils;