You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.7 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("http", function() {
  13. function failFirstLine(stream, state) {
  14. stream.skipToEnd();
  15. state.cur = header;
  16. return "error";
  17. }
  18. function start(stream, state) {
  19. if (stream.match(/^HTTP\/\d\.\d/)) {
  20. state.cur = responseStatusCode;
  21. return "keyword";
  22. } else if (stream.match(/^[A-Z]+/) && /[ \t]/.test(stream.peek())) {
  23. state.cur = requestPath;
  24. return "keyword";
  25. } else {
  26. return failFirstLine(stream, state);
  27. }
  28. }
  29. function responseStatusCode(stream, state) {
  30. var code = stream.match(/^\d+/);
  31. if (!code) return failFirstLine(stream, state);
  32. state.cur = responseStatusText;
  33. var status = Number(code[0]);
  34. if (status >= 100 && status < 200) {
  35. return "positive informational";
  36. } else if (status >= 200 && status < 300) {
  37. return "positive success";
  38. } else if (status >= 300 && status < 400) {
  39. return "positive redirect";
  40. } else if (status >= 400 && status < 500) {
  41. return "negative client-error";
  42. } else if (status >= 500 && status < 600) {
  43. return "negative server-error";
  44. } else {
  45. return "error";
  46. }
  47. }
  48. function responseStatusText(stream, state) {
  49. stream.skipToEnd();
  50. state.cur = header;
  51. return null;
  52. }
  53. function requestPath(stream, state) {
  54. stream.eatWhile(/\S/);
  55. state.cur = requestProtocol;
  56. return "string-2";
  57. }
  58. function requestProtocol(stream, state) {
  59. if (stream.match(/^HTTP\/\d\.\d$/)) {
  60. state.cur = header;
  61. return "keyword";
  62. } else {
  63. return failFirstLine(stream, state);
  64. }
  65. }
  66. function header(stream) {
  67. if (stream.sol() && !stream.eat(/[ \t]/)) {
  68. if (stream.match(/^.*?:/)) {
  69. return "atom";
  70. } else {
  71. stream.skipToEnd();
  72. return "error";
  73. }
  74. } else {
  75. stream.skipToEnd();
  76. return "string";
  77. }
  78. }
  79. function body(stream) {
  80. stream.skipToEnd();
  81. return null;
  82. }
  83. return {
  84. token: function(stream, state) {
  85. var cur = state.cur;
  86. if (cur != header && cur != body && stream.eatSpace()) return null;
  87. return cur(stream, state);
  88. },
  89. blankLine: function(state) {
  90. state.cur = body;
  91. },
  92. startState: function() {
  93. return {cur: start};
  94. }
  95. };
  96. });
  97. CodeMirror.defineMIME("message/http", "http");
  98. });