http.js 2.6 KB

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