1
0

http.js 2.3 KB

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