1
0

httpdomain.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 Keyword, Literal, Name, Number, Operator, Text
  14. from pygments.util import ClassNotFound
  15. from sphinx import addnodes
  16. from sphinx.directives import ObjectDescription
  17. from sphinx.domains import Domain, Index, ObjType
  18. from sphinx.roles import XRefRole
  19. from sphinx.util.docfields import GroupedField, TypedField
  20. from sphinx.util.nodes import make_refnode
  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(
  112. r"\((?:(?P<type>[^:)]+):)?(?P<name>[\w_]+)\)", re.VERBOSE
  113. )
  114. def http_resource_anchor(method, path):
  115. path = re.sub(r"[<>:/]", "-", path)
  116. return method.lower() + "-" + path
  117. class HTTPResource(ObjectDescription):
  118. doc_field_types = [
  119. TypedField(
  120. "parameter",
  121. label="Parameters",
  122. names=("param", "parameter", "arg", "argument"),
  123. typerolename="obj",
  124. typenames=("paramtype", "type"),
  125. ),
  126. TypedField(
  127. "jsonparameter",
  128. label="JSON Parameters",
  129. names=("jsonparameter", "jsonparam", "json"),
  130. typerolename="obj",
  131. typenames=("jsonparamtype", "jsontype"),
  132. ),
  133. TypedField(
  134. "queryparameter",
  135. label="Query Parameters",
  136. names=("queryparameter", "queryparam", "qparam", "query"),
  137. typerolename="obj",
  138. typenames=("queryparamtype", "querytype", "qtype"),
  139. ),
  140. GroupedField(
  141. "formparameter",
  142. label="Form Parameters",
  143. names=("formparameter", "formparam", "fparam", "form"),
  144. ),
  145. GroupedField(
  146. "requestheader",
  147. label="Request Headers",
  148. rolename="mailheader",
  149. names=("reqheader", "requestheader"),
  150. ),
  151. GroupedField(
  152. "responseheader",
  153. label="Response Headers",
  154. rolename="mailheader",
  155. names=("resheader", "responseheader"),
  156. ),
  157. GroupedField(
  158. "statuscode",
  159. label="Status Codes",
  160. rolename="statuscode",
  161. names=("statuscode", "status", "code"),
  162. ),
  163. ]
  164. method = NotImplemented
  165. def handle_signature(self, sig, signode):
  166. method = self.method.upper() + " "
  167. signode += addnodes.desc_name(method, method)
  168. offset = 0
  169. for match in http_sig_param_re.finditer(sig):
  170. path = sig[offset : match.start()]
  171. signode += addnodes.desc_name(path, path)
  172. params = addnodes.desc_parameterlist()
  173. typ = match.group("type")
  174. if typ:
  175. typ = typ + ": "
  176. params += addnodes.desc_annotation(typ, typ)
  177. name = match.group("name")
  178. params += addnodes.desc_parameter(name, name)
  179. signode += params
  180. offset = match.end()
  181. if offset < len(sig):
  182. path = sig[offset : len(sig)]
  183. signode += addnodes.desc_name(path, path)
  184. fullname = self.method.upper() + " " + path
  185. signode["method"] = self.method
  186. signode["path"] = sig
  187. signode["fullname"] = fullname
  188. return (fullname, self.method, sig)
  189. def needs_arglist(self):
  190. return False
  191. def add_target_and_index(self, name_cls, sig, signode):
  192. signode["ids"].append(http_resource_anchor(*name_cls[1:]))
  193. self.env.domaindata["http"][self.method][sig] = (self.env.docname, "")
  194. def get_index_text(self, modname, name):
  195. return ""
  196. class HTTPOptions(HTTPResource):
  197. method = "options"
  198. class HTTPHead(HTTPResource):
  199. method = "head"
  200. class HTTPPatch(HTTPResource):
  201. method = "patch"
  202. class HTTPPost(HTTPResource):
  203. method = "post"
  204. class HTTPGet(HTTPResource):
  205. method = "get"
  206. class HTTPPut(HTTPResource):
  207. method = "put"
  208. class HTTPDelete(HTTPResource):
  209. method = "delete"
  210. class HTTPTrace(HTTPResource):
  211. method = "trace"
  212. def http_statuscode_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
  213. if text.isdigit():
  214. code = int(text)
  215. try:
  216. status = HTTP_STATUS_CODES[code]
  217. except KeyError:
  218. msg = inliner.reporter.error(
  219. "%d is invalid HTTP status code" % code, lineno=lineno
  220. )
  221. prb = inliner.problematic(rawtext, rawtext, msg)
  222. return [prb], [msg]
  223. else:
  224. try:
  225. code, status = re.split(r"\s", text.strip(), 1)
  226. code = int(code)
  227. except ValueError:
  228. msg = inliner.reporter.error(
  229. "HTTP status code must be an integer (e.g. `200`) or "
  230. "start with an integer (e.g. `200 OK`); %r is invalid" % text,
  231. line=lineno,
  232. )
  233. prb = inliner.problematic(rawtext, rawtext, msg)
  234. return [prb], [msg]
  235. nodes.reference(rawtext)
  236. if code == 226:
  237. url = "http://www.ietf.org/rfc/rfc3229.txt"
  238. if code == 418:
  239. url = "http://www.ietf.org/rfc/rfc2324.txt"
  240. if code == 449:
  241. url = "http://msdn.microsoft.com/en-us/library" "/dd891478(v=prot.10).aspx"
  242. elif code in HTTP_STATUS_CODES:
  243. url = "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html" "#sec10." + (
  244. "%d.%d" % (code // 100, 1 + code % 100)
  245. )
  246. else:
  247. url = ""
  248. set_classes(options)
  249. node = nodes.reference(rawtext, "%d %s" % (code, status), refuri=url, **options)
  250. return [node], []
  251. def http_method_role(name, rawtext, text, lineno, inliner, options={}, content=[]):
  252. method = str(text).lower()
  253. if method not in DOCREFS:
  254. msg = inliner.reporter.error(
  255. "%s is not valid HTTP method" % method, lineno=lineno
  256. )
  257. prb = inliner.problematic(rawtext, rawtext, msg)
  258. return [prb], [msg]
  259. url = str(DOCREFS[method])
  260. node = nodes.reference(rawtext, method.upper(), refuri=url, **options)
  261. return [node], []
  262. class HTTPXRefRole(XRefRole):
  263. def __init__(self, method, **kwargs):
  264. XRefRole.__init__(self, **kwargs)
  265. self.method = method
  266. def process_link(self, env, refnode, has_explicit_title, title, target):
  267. if not target.startswith("/"):
  268. pass
  269. if not has_explicit_title:
  270. title = self.method.upper() + " " + title
  271. return title, target
  272. class HTTPIndex(Index):
  273. name = "routingtable"
  274. localname = "HTTP Routing Table"
  275. shortname = "routing table"
  276. def __init__(self, *args, **kwargs):
  277. super(HTTPIndex, self).__init__(*args, **kwargs)
  278. self.ignore = [
  279. [l for l in x.split("/") if l]
  280. for x in self.domain.env.config["http_index_ignore_prefixes"]
  281. ]
  282. self.ignore.sort(key=lambda x: -len(x))
  283. def grouping_prefix(self, path):
  284. letters = [x for x in path.split("/") if x]
  285. for prefix in self.ignore:
  286. if letters[: len(prefix)] == prefix:
  287. return "/" + "/".join(letters[: len(prefix) + 1])
  288. return "/%s" % (letters[0] if letters else "",)
  289. def generate(self, docnames=None):
  290. content = {}
  291. items = (
  292. (method, path, info)
  293. for method, routes in self.domain.routes.items()
  294. for path, info in routes.items()
  295. )
  296. items = sorted(items, key=lambda item: item[1])
  297. for method, path, info in items:
  298. entries = content.setdefault(self.grouping_prefix(path), [])
  299. entries.append(
  300. [
  301. method.upper() + " " + path,
  302. 0,
  303. info[0],
  304. http_resource_anchor(method, path),
  305. "",
  306. "",
  307. info[1],
  308. ]
  309. )
  310. content = sorted(content.items(), key=lambda k: k[0])
  311. return (content, True)
  312. class HTTPDomain(Domain):
  313. """HTTP domain."""
  314. name = "http"
  315. label = "HTTP"
  316. object_types = {
  317. "options": ObjType("options", "options", "obj"),
  318. "head": ObjType("head", "head", "obj"),
  319. "post": ObjType("post", "post", "obj"),
  320. "get": ObjType("get", "get", "obj"),
  321. "put": ObjType("put", "put", "obj"),
  322. "patch": ObjType("patch", "patch", "obj"),
  323. "delete": ObjType("delete", "delete", "obj"),
  324. "trace": ObjType("trace", "trace", "obj"),
  325. }
  326. directives = {
  327. "options": HTTPOptions,
  328. "head": HTTPHead,
  329. "post": HTTPPost,
  330. "get": HTTPGet,
  331. "put": HTTPPut,
  332. "patch": HTTPPatch,
  333. "delete": HTTPDelete,
  334. "trace": HTTPTrace,
  335. }
  336. roles = {
  337. "options": HTTPXRefRole("options"),
  338. "head": HTTPXRefRole("head"),
  339. "post": HTTPXRefRole("post"),
  340. "get": HTTPXRefRole("get"),
  341. "put": HTTPXRefRole("put"),
  342. "patch": HTTPXRefRole("patch"),
  343. "delete": HTTPXRefRole("delete"),
  344. "trace": HTTPXRefRole("trace"),
  345. "statuscode": http_statuscode_role,
  346. "method": http_method_role,
  347. }
  348. initial_data = {
  349. "options": {}, # path: (docname, synopsis)
  350. "head": {},
  351. "post": {},
  352. "get": {},
  353. "put": {},
  354. "patch": {},
  355. "delete": {},
  356. "trace": {},
  357. }
  358. # indices = [HTTPIndex]
  359. indices = []
  360. @property
  361. def routes(self):
  362. return dict((key, self.data[key]) for key in self.object_types)
  363. def clear_doc(self, docname):
  364. for typ, routes in self.routes.items():
  365. for path, info in list(routes.items()):
  366. if info[0] == docname:
  367. del routes[path]
  368. def resolve_xref(self, env, fromdocname, builder, typ, target, node, contnode):
  369. try:
  370. info = self.data[str(typ)][target]
  371. except KeyError:
  372. return
  373. else:
  374. anchor = http_resource_anchor(typ, target)
  375. title = typ.upper() + " " + target
  376. return make_refnode(builder, fromdocname, info[0], anchor, contnode, title)
  377. def get_objects(self):
  378. for method, routes in self.routes.items():
  379. for path, info in routes.items():
  380. anchor = http_resource_anchor(method, path)
  381. yield (path, path, method, info[0], anchor, 1)
  382. class HTTPLexer(RegexLexer):
  383. """Lexer for HTTP sessions."""
  384. name = "HTTP"
  385. aliases = ["http"]
  386. flags = re.DOTALL
  387. def header_callback(self, match):
  388. if match.group(1).lower() == "content-type":
  389. content_type = match.group(5).strip()
  390. if ";" in content_type:
  391. content_type = content_type[: content_type.find(";")].strip()
  392. self.content_type = content_type
  393. yield match.start(1), Name.Attribute, match.group(1)
  394. yield match.start(2), Text, match.group(2)
  395. yield match.start(3), Operator, match.group(3)
  396. yield match.start(4), Text, match.group(4)
  397. yield match.start(5), Literal, match.group(5)
  398. yield match.start(6), Text, match.group(6)
  399. def continuous_header_callback(self, match):
  400. yield match.start(1), Text, match.group(1)
  401. yield match.start(2), Literal, match.group(2)
  402. yield match.start(3), Text, match.group(3)
  403. def content_callback(self, match):
  404. content_type = getattr(self, "content_type", None)
  405. content = match.group()
  406. offset = match.start()
  407. if content_type:
  408. from pygments.lexers import get_lexer_for_mimetype
  409. try:
  410. lexer = get_lexer_for_mimetype(content_type)
  411. except ClassNotFound:
  412. pass
  413. else:
  414. for idx, token, value in lexer.get_tokens_unprocessed(content):
  415. yield offset + idx, token, value
  416. return
  417. yield offset, Text, content
  418. tokens = {
  419. "root": [
  420. (
  421. r"(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS|TRACE)( +)([^ ]+)( +)"
  422. r"(HTTPS?)(/)(1\.[01])(\r?\n|$)",
  423. bygroups(
  424. Name.Function,
  425. Text,
  426. Name.Namespace,
  427. Text,
  428. Keyword.Reserved,
  429. Operator,
  430. Number,
  431. Text,
  432. ),
  433. "headers",
  434. ),
  435. (
  436. r"(HTTPS?)(/)(1\.[01])( +)(\d{3})( +)([^\r\n]+)(\r?\n|$)",
  437. bygroups(
  438. Keyword.Reserved,
  439. Operator,
  440. Number,
  441. Text,
  442. Number,
  443. Text,
  444. Name.Exception,
  445. Text,
  446. ),
  447. "headers",
  448. ),
  449. ],
  450. "headers": [
  451. (r"([^\s:]+)( *)(:)( *)([^\r\n]+)(\r?\n|$)", header_callback),
  452. (r"([\t ]+)([^\r\n]+)(\r?\n|$)", continuous_header_callback),
  453. (r"\r?\n", Text, "content"),
  454. ],
  455. "content": [(r".+", content_callback)],
  456. }
  457. def setup(app):
  458. app.add_domain(HTTPDomain)
  459. try:
  460. get_lexer_by_name("http")
  461. except ClassNotFound:
  462. app.add_lexer("http", HTTPLexer())
  463. app.add_config_value("http_index_ignore_prefixes", [], None)