httpdomain.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. """
  2. sphinxcontrib.httpdomain
  3. ~~~~~~~~~~~~~~~~~~~~~~~~
  4. The HTTP domain for documenting RESTful HTTP APIs.
  5. :copyright: Copyright 2011 by Hong Minhee
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from docutils import nodes
  10. from docutils.parsers.rst.roles import set_classes
  11. from pygments.lexer import RegexLexer, bygroups
  12. from pygments.lexers import get_lexer_by_name
  13. from pygments.token import Literal, Text, Operator, Keyword, Name, Number
  14. from pygments.util import ClassNotFound
  15. from sphinx import addnodes
  16. from sphinx.roles import XRefRole
  17. from sphinx.domains import Domain, ObjType, Index
  18. from sphinx.directives import ObjectDescription
  19. from sphinx.util.nodes import make_refnode
  20. from sphinx.util.docfields import GroupedField, TypedField
  21. class DocRef(object):
  22. """Represents a link to an RFC which defines an HTTP method."""
  23. def __init__(self, base_url, anchor, section):
  24. """Stores the specified attributes which represent a URL which links to
  25. an RFC which defines an HTTP method.
  26. """
  27. self.base_url = base_url
  28. self.anchor = anchor
  29. self.section = section
  30. def __repr__(self):
  31. """Returns the URL which this object represents, which points to the
  32. location of the RFC which defines some HTTP method.
  33. """
  34. return '{0}#{1}{2}'.format(self.base_url, self.anchor, self.section)
  35. #: The URL of the HTTP/1.1 RFC which defines the HTTP methods OPTIONS, GET,
  36. #: HEAD, POST, PUT, DELETE, TRACE, and CONNECT.
  37. RFC2616 = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html'
  38. #: The name to use for section anchors in RFC2616.
  39. RFC2616ANCHOR = 'sec'
  40. #: The URL of the RFC which defines the HTTP PATCH method.
  41. RFC5789 = 'http://tools.ietf.org/html/rfc5789'
  42. #: The name to use for section anchors in RFC5789.
  43. RFC5789ANCHOR = 'section-'
  44. #: Mapping from lowercase HTTP method name to :class:`DocRef` object which
  45. #: maintains the URL which points to the section of the RFC which defines that
  46. #: HTTP method.
  47. DOCREFS = {
  48. 'patch': DocRef(RFC5789, RFC5789ANCHOR, 2),
  49. 'options': DocRef(RFC2616, RFC2616ANCHOR, 9.2),
  50. 'get': DocRef(RFC2616, RFC2616ANCHOR, 9.3),
  51. 'head': DocRef(RFC2616, RFC2616ANCHOR, 9.4),
  52. 'post': DocRef(RFC2616, RFC2616ANCHOR, 9.5),
  53. 'put': DocRef(RFC2616, RFC2616ANCHOR, 9.6),
  54. 'delete': DocRef(RFC2616, RFC2616ANCHOR, 9.7),
  55. 'trace': DocRef(RFC2616, RFC2616ANCHOR, 9.8),
  56. 'connect': DocRef(RFC2616, RFC2616ANCHOR, 9.9)
  57. }
  58. HTTP_STATUS_CODES = {
  59. 100: 'Continue',
  60. 101: 'Switching Protocols',
  61. 102: 'Processing',
  62. 200: 'OK',
  63. 201: 'Created',
  64. 202: 'Accepted',
  65. 203: 'Non Authoritative Information',
  66. 204: 'No Content',
  67. 205: 'Reset Content',
  68. 206: 'Partial Content',
  69. 207: 'Multi Status',
  70. 226: 'IM Used', # see RFC 3229
  71. 300: 'Multiple Choices',
  72. 301: 'Moved Permanently',
  73. 302: 'Found',
  74. 303: 'See Other',
  75. 304: 'Not Modified',
  76. 305: 'Use Proxy',
  77. 307: 'Temporary Redirect',
  78. 400: 'Bad Request',
  79. 401: 'Unauthorized',
  80. 402: 'Payment Required', # unused
  81. 403: 'Forbidden',
  82. 404: 'Not Found',
  83. 405: 'Method Not Allowed',
  84. 406: 'Not Acceptable',
  85. 407: 'Proxy Authentication Required',
  86. 408: 'Request Timeout',
  87. 409: 'Conflict',
  88. 410: 'Gone',
  89. 411: 'Length Required',
  90. 412: 'Precondition Failed',
  91. 413: 'Request Entity Too Large',
  92. 414: 'Request URI Too Long',
  93. 415: 'Unsupported Media Type',
  94. 416: 'Requested Range Not Satisfiable',
  95. 417: 'Expectation Failed',
  96. 418: "I'm a teapot", # see RFC 2324
  97. 422: 'Unprocessable Entity',
  98. 423: 'Locked',
  99. 424: 'Failed Dependency',
  100. 426: 'Upgrade Required',
  101. 449: 'Retry With', # proprietary MS extension
  102. 500: 'Internal Server Error',
  103. 501: 'Not Implemented',
  104. 502: 'Bad Gateway',
  105. 503: 'Service Unavailable',
  106. 504: 'Gateway Timeout',
  107. 505: 'HTTP Version Not Supported',
  108. 507: 'Insufficient Storage',
  109. 510: 'Not Extended'
  110. }
  111. http_sig_param_re = re.compile(r'\((?:(?P<type>[^:)]+):)?(?P<name>[\w_]+)\)',
  112. re.VERBOSE)
  113. def http_resource_anchor(method, path):
  114. path = re.sub(r'[<>:/]', '-', path)
  115. return method.lower() + '-' + path
  116. class HTTPResource(ObjectDescription):
  117. doc_field_types = [
  118. TypedField('parameter', label='Parameters',
  119. names=('param', 'parameter', 'arg', 'argument'),
  120. typerolename='obj', typenames=('paramtype', 'type')),
  121. TypedField('jsonparameter', label='JSON Parameters',
  122. names=('jsonparameter', 'jsonparam', 'json'),
  123. typerolename='obj', typenames=('jsonparamtype', 'jsontype')),
  124. TypedField('queryparameter', label='Query Parameters',
  125. names=('queryparameter', 'queryparam', 'qparam', 'query'),
  126. typerolename='obj', typenames=('queryparamtype', 'querytype', 'qtype')),
  127. GroupedField('formparameter', label='Form Parameters',
  128. names=('formparameter', 'formparam', 'fparam', 'form')),
  129. GroupedField('requestheader', label='Request Headers',
  130. rolename='mailheader',
  131. names=('reqheader', 'requestheader')),
  132. GroupedField('responseheader', label='Response Headers',
  133. rolename='mailheader',
  134. names=('resheader', 'responseheader')),
  135. GroupedField('statuscode', label='Status Codes',
  136. rolename='statuscode',
  137. names=('statuscode', 'status', 'code'))
  138. ]
  139. method = NotImplemented
  140. def handle_signature(self, sig, signode):
  141. method = self.method.upper() + ' '
  142. signode += addnodes.desc_name(method, method)
  143. offset = 0
  144. for match in http_sig_param_re.finditer(sig):
  145. path = sig[offset:match.start()]
  146. signode += addnodes.desc_name(path, path)
  147. params = addnodes.desc_parameterlist()
  148. typ = match.group('type')
  149. if typ:
  150. typ = typ + ': '
  151. params += addnodes.desc_annotation(typ, typ)
  152. name = match.group('name')
  153. params += addnodes.desc_parameter(name, name)
  154. signode += params
  155. offset = match.end()
  156. if offset < len(sig):
  157. path = sig[offset:len(sig)]
  158. signode += addnodes.desc_name(path, path)
  159. fullname = self.method.upper() + ' ' + path
  160. signode['method'] = self.method
  161. signode['path'] = sig
  162. signode['fullname'] = fullname
  163. return (fullname, self.method, sig)
  164. def needs_arglist(self):
  165. return False
  166. def add_target_and_index(self, name_cls, sig, signode):
  167. signode['ids'].append(http_resource_anchor(*name_cls[1:]))
  168. self.env.domaindata['http'][self.method][sig] = (self.env.docname, '')
  169. def get_index_text(self, modname, name):
  170. return ''
  171. class HTTPOptions(HTTPResource):
  172. method = 'options'
  173. class HTTPHead(HTTPResource):
  174. method = 'head'
  175. class HTTPPatch(HTTPResource):
  176. method = 'patch'
  177. class HTTPPost(HTTPResource):
  178. method = 'post'
  179. class HTTPGet(HTTPResource):
  180. method = 'get'
  181. class HTTPPut(HTTPResource):
  182. method = 'put'
  183. class HTTPDelete(HTTPResource):
  184. method = 'delete'
  185. class HTTPTrace(HTTPResource):
  186. method = 'trace'
  187. def http_statuscode_role(name, rawtext, text, lineno, inliner,
  188. options={}, content=[]):
  189. if text.isdigit():
  190. code = int(text)
  191. try:
  192. status = HTTP_STATUS_CODES[code]
  193. except KeyError:
  194. msg = inliner.reporter.error('%d is invalid HTTP status code'
  195. % code, lineno=lineno)
  196. prb = inliner.problematic(rawtext, rawtext, msg)
  197. return [prb], [msg]
  198. else:
  199. try:
  200. code, status = re.split(r'\s', text.strip(), 1)
  201. code = int(code)
  202. except ValueError:
  203. msg = inliner.reporter.error(
  204. 'HTTP status code must be an integer (e.g. `200`) or '
  205. 'start with an integer (e.g. `200 OK`); %r is invalid' %
  206. text,
  207. line=lineno
  208. )
  209. prb = inliner.problematic(rawtext, rawtext, msg)
  210. return [prb], [msg]
  211. nodes.reference(rawtext)
  212. if code == 226:
  213. url = 'http://www.ietf.org/rfc/rfc3229.txt'
  214. if code == 418:
  215. url = 'http://www.ietf.org/rfc/rfc2324.txt'
  216. if code == 449:
  217. url = 'http://msdn.microsoft.com/en-us/library' \
  218. '/dd891478(v=prot.10).aspx'
  219. elif code in HTTP_STATUS_CODES:
  220. url = 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html' \
  221. '#sec10.' + ('%d.%d' % (code // 100, 1 + code % 100))
  222. else:
  223. url = ''
  224. set_classes(options)
  225. node = nodes.reference(rawtext, '%d %s' % (code, status),
  226. refuri=url, **options)
  227. return [node], []
  228. def http_method_role(name, rawtext, text, lineno, inliner,
  229. options={}, content=[]):
  230. method = str(text).lower()
  231. if method not in DOCREFS:
  232. msg = inliner.reporter.error('%s is not valid HTTP method' % method,
  233. lineno=lineno)
  234. prb = inliner.problematic(rawtext, rawtext, msg)
  235. return [prb], [msg]
  236. url = str(DOCREFS[method])
  237. node = nodes.reference(rawtext, method.upper(), refuri=url, **options)
  238. return [node], []
  239. class HTTPXRefRole(XRefRole):
  240. def __init__(self, method, **kwargs):
  241. XRefRole.__init__(self, **kwargs)
  242. self.method = method
  243. def process_link(self, env, refnode, has_explicit_title, title, target):
  244. if not target.startswith('/'):
  245. pass
  246. if not has_explicit_title:
  247. title = self.method.upper() + ' ' + title
  248. return title, target
  249. class HTTPIndex(Index):
  250. name = 'routingtable'
  251. localname = 'HTTP Routing Table'
  252. shortname = 'routing table'
  253. def __init__(self, *args, **kwargs):
  254. super(HTTPIndex, self).__init__(*args, **kwargs)
  255. self.ignore = [[l for l in x.split('/') if l]
  256. for x in self.domain.env.config['http_index_ignore_prefixes']]
  257. self.ignore.sort(key=lambda x: -len(x))
  258. def grouping_prefix(self, path):
  259. letters = [x for x in path.split('/') if x]
  260. for prefix in self.ignore:
  261. if letters[:len(prefix)] == prefix:
  262. return '/' + '/'.join(letters[:len(prefix) + 1])
  263. return '/%s' % (letters[0] if letters else '',)
  264. def generate(self, docnames=None):
  265. content = {}
  266. items = ((method, path, info)
  267. for method, routes in self.domain.routes.items()
  268. for path, info in routes.items())
  269. items = sorted(items, key=lambda item: item[1])
  270. for method, path, info in items:
  271. entries = content.setdefault(self.grouping_prefix(path), [])
  272. entries.append([
  273. method.upper() + ' ' + path, 0, info[0],
  274. http_resource_anchor(method, path), '', '', info[1]
  275. ])
  276. content = sorted(content.items(), key=lambda k: k[0])
  277. return (content, True)
  278. class HTTPDomain(Domain):
  279. """HTTP domain."""
  280. name = 'http'
  281. label = 'HTTP'
  282. object_types = {
  283. 'options': ObjType('options', 'options', 'obj'),
  284. 'head': ObjType('head', 'head', 'obj'),
  285. 'post': ObjType('post', 'post', 'obj'),
  286. 'get': ObjType('get', 'get', 'obj'),
  287. 'put': ObjType('put', 'put', 'obj'),
  288. 'patch': ObjType('patch', 'patch', 'obj'),
  289. 'delete': ObjType('delete', 'delete', 'obj'),
  290. 'trace': ObjType('trace', 'trace', 'obj')
  291. }
  292. directives = {
  293. 'options': HTTPOptions,
  294. 'head': HTTPHead,
  295. 'post': HTTPPost,
  296. 'get': HTTPGet,
  297. 'put': HTTPPut,
  298. 'patch': HTTPPatch,
  299. 'delete': HTTPDelete,
  300. 'trace': HTTPTrace
  301. }
  302. roles = {
  303. 'options': HTTPXRefRole('options'),
  304. 'head': HTTPXRefRole('head'),
  305. 'post': HTTPXRefRole('post'),
  306. 'get': HTTPXRefRole('get'),
  307. 'put': HTTPXRefRole('put'),
  308. 'patch': HTTPXRefRole('patch'),
  309. 'delete': HTTPXRefRole('delete'),
  310. 'trace': HTTPXRefRole('trace'),
  311. 'statuscode': http_statuscode_role,
  312. 'method': http_method_role
  313. }
  314. initial_data = {
  315. 'options': {}, # path: (docname, synopsis)
  316. 'head': {},
  317. 'post': {},
  318. 'get': {},
  319. 'put': {},
  320. 'patch': {},
  321. 'delete': {},
  322. 'trace': {}
  323. }
  324. # indices = [HTTPIndex]
  325. indices = []
  326. @property
  327. def routes(self):
  328. return dict((key, self.data[key]) for key in self.object_types)
  329. def clear_doc(self, docname):
  330. for typ, routes in self.routes.items():
  331. for path, info in list(routes.items()):
  332. if info[0] == docname:
  333. del routes[path]
  334. def resolve_xref(self, env, fromdocname, builder, typ, target,
  335. node, contnode):
  336. try:
  337. info = self.data[str(typ)][target]
  338. except KeyError:
  339. return
  340. else:
  341. anchor = http_resource_anchor(typ, target)
  342. title = typ.upper() + ' ' + target
  343. return make_refnode(builder, fromdocname, info[0], anchor,
  344. contnode, title)
  345. def get_objects(self):
  346. for method, routes in self.routes.items():
  347. for path, info in routes.items():
  348. anchor = http_resource_anchor(method, path)
  349. yield (path, path, method, info[0], anchor, 1)
  350. class HTTPLexer(RegexLexer):
  351. """Lexer for HTTP sessions."""
  352. name = 'HTTP'
  353. aliases = ['http']
  354. flags = re.DOTALL
  355. def header_callback(self, match):
  356. if match.group(1).lower() == 'content-type':
  357. content_type = match.group(5).strip()
  358. if ';' in content_type:
  359. content_type = content_type[:content_type.find(';')].strip()
  360. self.content_type = content_type
  361. yield match.start(1), Name.Attribute, match.group(1)
  362. yield match.start(2), Text, match.group(2)
  363. yield match.start(3), Operator, match.group(3)
  364. yield match.start(4), Text, match.group(4)
  365. yield match.start(5), Literal, match.group(5)
  366. yield match.start(6), Text, match.group(6)
  367. def continuous_header_callback(self, match):
  368. yield match.start(1), Text, match.group(1)
  369. yield match.start(2), Literal, match.group(2)
  370. yield match.start(3), Text, match.group(3)
  371. def content_callback(self, match):
  372. content_type = getattr(self, 'content_type', None)
  373. content = match.group()
  374. offset = match.start()
  375. if content_type:
  376. from pygments.lexers import get_lexer_for_mimetype
  377. try:
  378. lexer = get_lexer_for_mimetype(content_type)
  379. except ClassNotFound:
  380. pass
  381. else:
  382. for idx, token, value in lexer.get_tokens_unprocessed(content):
  383. yield offset + idx, token, value
  384. return
  385. yield offset, Text, content
  386. tokens = {
  387. 'root': [
  388. (r'(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|TRACE)( +)([^ ]+)( +)'
  389. r'(HTTPS?)(/)(1\.[01])(\r?\n|$)',
  390. bygroups(Name.Function, Text, Name.Namespace, Text,
  391. Keyword.Reserved, Operator, Number, Text),
  392. 'headers'),
  393. (r'(HTTPS?)(/)(1\.[01])( +)(\d{3})( +)([^\r\n]+)(\r?\n|$)',
  394. bygroups(Keyword.Reserved, Operator, Number, Text, Number,
  395. Text, Name.Exception, Text),
  396. 'headers'),
  397. ],
  398. 'headers': [
  399. (r'([^\s:]+)( *)(:)( *)([^\r\n]+)(\r?\n|$)', header_callback),
  400. (r'([\t ]+)([^\r\n]+)(\r?\n|$)', continuous_header_callback),
  401. (r'\r?\n', Text, 'content')
  402. ],
  403. 'content': [
  404. (r'.+', content_callback)
  405. ]
  406. }
  407. def setup(app):
  408. app.add_domain(HTTPDomain)
  409. try:
  410. get_lexer_by_name('http')
  411. except ClassNotFound:
  412. app.add_lexer('http', HTTPLexer())
  413. app.add_config_value('http_index_ignore_prefixes', [], None)