http.js 3.4 KB

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