http.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/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. });