http-protocol.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. HTTP transfer protocols
  2. =======================
  3. Git supports two HTTP based transfer protocols. A "dumb" protocol
  4. which requires only a standard HTTP server on the server end of the
  5. connection, and a "smart" protocol which requires a Git aware CGI
  6. (or server module). This document describes both protocols.
  7. As a design feature smart clients can automatically upgrade "dumb"
  8. protocol URLs to smart URLs. This permits all users to have the
  9. same published URL, and the peers automatically select the most
  10. efficient transport available to them.
  11. URL Format
  12. ----------
  13. URLs for Git repositories accessed by HTTP use the standard HTTP
  14. URL syntax documented by RFC 1738, so they are of the form:
  15. http://<host>:<port>/<path>?<searchpart>
  16. Within this documentation the placeholder `$GIT_URL` will stand for
  17. the http:// repository URL entered by the end-user.
  18. Servers SHOULD handle all requests to locations matching `$GIT_URL`, as
  19. both the "smart" and "dumb" HTTP protocols used by Git operate
  20. by appending additional path components onto the end of the user
  21. supplied `$GIT_URL` string.
  22. An example of a dumb client requesting for a loose object:
  23. $GIT_URL: http://example.com:8080/git/repo.git
  24. URL request: http://example.com:8080/git/repo.git/objects/d0/49f6c27a2244e12041955e262a404c7faba355
  25. An example of a smart request to a catch-all gateway:
  26. $GIT_URL: http://example.com/daemon.cgi?svc=git&q=
  27. URL request: http://example.com/daemon.cgi?svc=git&q=/info/refs&service=git-receive-pack
  28. An example of a request to a submodule:
  29. $GIT_URL: http://example.com/git/repo.git/path/submodule.git
  30. URL request: http://example.com/git/repo.git/path/submodule.git/info/refs
  31. Clients MUST strip a trailing `/`, if present, from the user supplied
  32. `$GIT_URL` string to prevent empty path tokens (`//`) from appearing
  33. in any URL sent to a server. Compatible clients MUST expand
  34. `$GIT_URL/info/refs` as `foo/info/refs` and not `foo//info/refs`.
  35. Authentication
  36. --------------
  37. Standard HTTP authentication is used if authentication is required
  38. to access a repository, and MAY be configured and enforced by the
  39. HTTP server software.
  40. Because Git repositories are accessed by standard path components
  41. server administrators MAY use directory based permissions within
  42. their HTTP server to control repository access.
  43. Clients SHOULD support Basic authentication as described by RFC 2617.
  44. Servers SHOULD support Basic authentication by relying upon the
  45. HTTP server placed in front of the Git server software.
  46. Servers SHOULD NOT require HTTP cookies for the purposes of
  47. authentication or access control.
  48. Clients and servers MAY support other common forms of HTTP based
  49. authentication, such as Digest authentication.
  50. SSL
  51. ---
  52. Clients and servers SHOULD support SSL, particularly to protect
  53. passwords when relying on Basic HTTP authentication.
  54. Session State
  55. -------------
  56. The Git over HTTP protocol (much like HTTP itself) is stateless
  57. from the perspective of the HTTP server side. All state MUST be
  58. retained and managed by the client process. This permits simple
  59. round-robin load-balancing on the server side, without needing to
  60. worry about state management.
  61. Clients MUST NOT require state management on the server side in
  62. order to function correctly.
  63. Servers MUST NOT require HTTP cookies in order to function correctly.
  64. Clients MAY store and forward HTTP cookies during request processing
  65. as described by RFC 2616 (HTTP/1.1). Servers SHOULD ignore any
  66. cookies sent by a client.
  67. General Request Processing
  68. --------------------------
  69. Except where noted, all standard HTTP behavior SHOULD be assumed
  70. by both client and server. This includes (but is not necessarily
  71. limited to):
  72. If there is no repository at `$GIT_URL`, or the resource pointed to by a
  73. location matching `$GIT_URL` does not exist, the server MUST NOT respond
  74. with `200 OK` response. A server SHOULD respond with
  75. `404 Not Found`, `410 Gone`, or any other suitable HTTP status code
  76. which does not imply the resource exists as requested.
  77. If there is a repository at `$GIT_URL`, but access is not currently
  78. permitted, the server MUST respond with the `403 Forbidden` HTTP
  79. status code.
  80. Servers SHOULD support both HTTP 1.0 and HTTP 1.1.
  81. Servers SHOULD support chunked encoding for both request and response
  82. bodies.
  83. Clients SHOULD support both HTTP 1.0 and HTTP 1.1.
  84. Clients SHOULD support chunked encoding for both request and response
  85. bodies.
  86. Servers MAY return ETag and/or Last-Modified headers.
  87. Clients MAY revalidate cached entities by including If-Modified-Since
  88. and/or If-None-Match request headers.
  89. Servers MAY return `304 Not Modified` if the relevant headers appear
  90. in the request and the entity has not changed. Clients MUST treat
  91. `304 Not Modified` identical to `200 OK` by reusing the cached entity.
  92. Clients MAY reuse a cached entity without revalidation if the
  93. Cache-Control and/or Expires header permits caching. Clients and
  94. servers MUST follow RFC 2616 for cache controls.
  95. Discovering References
  96. ----------------------
  97. All HTTP clients MUST begin either a fetch or a push exchange by
  98. discovering the references available on the remote repository.
  99. Dumb Clients
  100. ~~~~~~~~~~~~
  101. HTTP clients that only support the "dumb" protocol MUST discover
  102. references by making a request for the special info/refs file of
  103. the repository.
  104. Dumb HTTP clients MUST make a `GET` request to `$GIT_URL/info/refs`,
  105. without any search/query parameters.
  106. C: GET $GIT_URL/info/refs HTTP/1.0
  107. S: 200 OK
  108. S:
  109. S: 95dcfa3633004da0049d3d0fa03f80589cbcaf31 refs/heads/maint
  110. S: d049f6c27a2244e12041955e262a404c7faba355 refs/heads/master
  111. S: 2cb58b79488a98d2721cea644875a8dd0026b115 refs/tags/v1.0
  112. S: a3c2e2402b99163d1d59756e5f207ae21cccba4c refs/tags/v1.0^{}
  113. The Content-Type of the returned info/refs entity SHOULD be
  114. `text/plain; charset=utf-8`, but MAY be any content type.
  115. Clients MUST NOT attempt to validate the returned Content-Type.
  116. Dumb servers MUST NOT return a return type starting with
  117. `application/x-git-`.
  118. Cache-Control headers MAY be returned to disable caching of the
  119. returned entity.
  120. When examining the response clients SHOULD only examine the HTTP
  121. status code. Valid responses are `200 OK`, or `304 Not Modified`.
  122. The returned content is a UNIX formatted text file describing
  123. each ref and its known value. The file SHOULD be sorted by name
  124. according to the C locale ordering. The file SHOULD NOT include
  125. the default ref named `HEAD`.
  126. info_refs = *( ref_record )
  127. ref_record = any_ref / peeled_ref
  128. any_ref = obj-id HTAB refname LF
  129. peeled_ref = obj-id HTAB refname LF
  130. obj-id HTAB refname "^{}" LF
  131. Smart Clients
  132. ~~~~~~~~~~~~~
  133. HTTP clients that support the "smart" protocol (or both the
  134. "smart" and "dumb" protocols) MUST discover references by making
  135. a parameterized request for the info/refs file of the repository.
  136. The request MUST contain exactly one query parameter,
  137. `service=$servicename`, where `$servicename` MUST be the service
  138. name the client wishes to contact to complete the operation.
  139. The request MUST NOT contain additional query parameters.
  140. C: GET $GIT_URL/info/refs?service=git-upload-pack HTTP/1.0
  141. dumb server reply:
  142. S: 200 OK
  143. S:
  144. S: 95dcfa3633004da0049d3d0fa03f80589cbcaf31 refs/heads/maint
  145. S: d049f6c27a2244e12041955e262a404c7faba355 refs/heads/master
  146. S: 2cb58b79488a98d2721cea644875a8dd0026b115 refs/tags/v1.0
  147. S: a3c2e2402b99163d1d59756e5f207ae21cccba4c refs/tags/v1.0^{}
  148. smart server reply:
  149. S: 200 OK
  150. S: Content-Type: application/x-git-upload-pack-advertisement
  151. S: Cache-Control: no-cache
  152. S:
  153. S: 001e# service=git-upload-pack\n
  154. S: 0000
  155. S: 004895dcfa3633004da0049d3d0fa03f80589cbcaf31 refs/heads/maint\0multi_ack\n
  156. S: 003fd049f6c27a2244e12041955e262a404c7faba355 refs/heads/master\n
  157. S: 003c2cb58b79488a98d2721cea644875a8dd0026b115 refs/tags/v1.0\n
  158. S: 003fa3c2e2402b99163d1d59756e5f207ae21cccba4c refs/tags/v1.0^{}\n
  159. S: 0000
  160. The client may send Extra Parameters (see
  161. Documentation/technical/pack-protocol.txt) as a colon-separated string
  162. in the Git-Protocol HTTP header.
  163. Dumb Server Response
  164. ^^^^^^^^^^^^^^^^^^^^
  165. Dumb servers MUST respond with the dumb server reply format.
  166. See the prior section under dumb clients for a more detailed
  167. description of the dumb server response.
  168. Smart Server Response
  169. ^^^^^^^^^^^^^^^^^^^^^
  170. If the server does not recognize the requested service name, or the
  171. requested service name has been disabled by the server administrator,
  172. the server MUST respond with the `403 Forbidden` HTTP status code.
  173. Otherwise, smart servers MUST respond with the smart server reply
  174. format for the requested service name.
  175. Cache-Control headers SHOULD be used to disable caching of the
  176. returned entity.
  177. The Content-Type MUST be `application/x-$servicename-advertisement`.
  178. Clients SHOULD fall back to the dumb protocol if another content
  179. type is returned. When falling back to the dumb protocol clients
  180. SHOULD NOT make an additional request to `$GIT_URL/info/refs`, but
  181. instead SHOULD use the response already in hand. Clients MUST NOT
  182. continue if they do not support the dumb protocol.
  183. Clients MUST validate the status code is either `200 OK` or
  184. `304 Not Modified`.
  185. Clients MUST validate the first five bytes of the response entity
  186. matches the regex `^[0-9a-f]{4}#`. If this test fails, clients
  187. MUST NOT continue.
  188. Clients MUST parse the entire response as a sequence of pkt-line
  189. records.
  190. Clients MUST verify the first pkt-line is `# service=$servicename`.
  191. Servers MUST set $servicename to be the request parameter value.
  192. Servers SHOULD include an LF at the end of this line.
  193. Clients MUST ignore an LF at the end of the line.
  194. Servers MUST terminate the response with the magic `0000` end
  195. pkt-line marker.
  196. The returned response is a pkt-line stream describing each ref and
  197. its known value. The stream SHOULD be sorted by name according to
  198. the C locale ordering. The stream SHOULD include the default ref
  199. named `HEAD` as the first ref. The stream MUST include capability
  200. declarations behind a NUL on the first ref.
  201. The returned response contains "version 1" if "version=1" was sent as an
  202. Extra Parameter.
  203. smart_reply = PKT-LINE("# service=$servicename" LF)
  204. "0000"
  205. *1("version 1")
  206. ref_list
  207. "0000"
  208. ref_list = empty_list / non_empty_list
  209. empty_list = PKT-LINE(zero-id SP "capabilities^{}" NUL cap-list LF)
  210. non_empty_list = PKT-LINE(obj-id SP name NUL cap_list LF)
  211. *ref_record
  212. cap-list = capability *(SP capability)
  213. capability = 1*(LC_ALPHA / DIGIT / "-" / "_")
  214. LC_ALPHA = %x61-7A
  215. ref_record = any_ref / peeled_ref
  216. any_ref = PKT-LINE(obj-id SP name LF)
  217. peeled_ref = PKT-LINE(obj-id SP name LF)
  218. PKT-LINE(obj-id SP name "^{}" LF
  219. Smart Service git-upload-pack
  220. ------------------------------
  221. This service reads from the repository pointed to by `$GIT_URL`.
  222. Clients MUST first perform ref discovery with
  223. `$GIT_URL/info/refs?service=git-upload-pack`.
  224. C: POST $GIT_URL/git-upload-pack HTTP/1.0
  225. C: Content-Type: application/x-git-upload-pack-request
  226. C:
  227. C: 0032want 0a53e9ddeaddad63ad106860237bbf53411d11a7\n
  228. C: 0032have 441b40d833fdfa93eb2908e52742248faf0ee993\n
  229. C: 0000
  230. S: 200 OK
  231. S: Content-Type: application/x-git-upload-pack-result
  232. S: Cache-Control: no-cache
  233. S:
  234. S: ....ACK %s, continue
  235. S: ....NAK
  236. Clients MUST NOT reuse or revalidate a cached response.
  237. Servers MUST include sufficient Cache-Control headers
  238. to prevent caching of the response.
  239. Servers SHOULD support all capabilities defined here.
  240. Clients MUST send at least one "want" command in the request body.
  241. Clients MUST NOT reference an id in a "want" command which did not
  242. appear in the response obtained through ref discovery unless the
  243. server advertises capability `allow-tip-sha1-in-want` or
  244. `allow-reachable-sha1-in-want`.
  245. compute_request = want_list
  246. have_list
  247. request_end
  248. request_end = "0000" / "done"
  249. want_list = PKT-LINE(want SP cap_list LF)
  250. *(want_pkt)
  251. want_pkt = PKT-LINE(want LF)
  252. want = "want" SP id
  253. cap_list = capability *(SP capability)
  254. have_list = *PKT-LINE("have" SP id LF)
  255. TODO: Document this further.
  256. The Negotiation Algorithm
  257. ~~~~~~~~~~~~~~~~~~~~~~~~~
  258. The computation to select the minimal pack proceeds as follows
  259. (C = client, S = server):
  260. 'init step:'
  261. C: Use ref discovery to obtain the advertised refs.
  262. C: Place any object seen into set `advertised`.
  263. C: Build an empty set, `common`, to hold the objects that are later
  264. determined to be on both ends.
  265. C: Build a set, `want`, of the objects from `advertised` the client
  266. wants to fetch, based on what it saw during ref discovery.
  267. C: Start a queue, `c_pending`, ordered by commit time (popping newest
  268. first). Add all client refs. When a commit is popped from
  269. the queue its parents SHOULD be automatically inserted back.
  270. Commits MUST only enter the queue once.
  271. 'one compute step:'
  272. C: Send one `$GIT_URL/git-upload-pack` request:
  273. C: 0032want <want #1>...............................
  274. C: 0032want <want #2>...............................
  275. ....
  276. C: 0032have <common #1>.............................
  277. C: 0032have <common #2>.............................
  278. ....
  279. C: 0032have <have #1>...............................
  280. C: 0032have <have #2>...............................
  281. ....
  282. C: 0000
  283. The stream is organized into "commands", with each command
  284. appearing by itself in a pkt-line. Within a command line,
  285. the text leading up to the first space is the command name,
  286. and the remainder of the line to the first LF is the value.
  287. Command lines are terminated with an LF as the last byte of
  288. the pkt-line value.
  289. Commands MUST appear in the following order, if they appear
  290. at all in the request stream:
  291. * "want"
  292. * "have"
  293. The stream is terminated by a pkt-line flush (`0000`).
  294. A single "want" or "have" command MUST have one hex formatted
  295. object name as its value. Multiple object names MUST be sent by sending
  296. multiple commands. Object names MUST be given using the object format
  297. negotiated through the `object-format` capability (default SHA-1).
  298. The `have` list is created by popping the first 32 commits
  299. from `c_pending`. Less can be supplied if `c_pending` empties.
  300. If the client has sent 256 "have" commits and has not yet
  301. received one of those back from `s_common`, or the client has
  302. emptied `c_pending` it SHOULD include a "done" command to let
  303. the server know it won't proceed:
  304. C: 0009done
  305. S: Parse the git-upload-pack request:
  306. Verify all objects in `want` are directly reachable from refs.
  307. The server MAY walk backwards through history or through
  308. the reflog to permit slightly stale requests.
  309. If no "want" objects are received, send an error:
  310. TODO: Define error if no "want" lines are requested.
  311. If any "want" object is not reachable, send an error:
  312. TODO: Define error if an invalid "want" is requested.
  313. Create an empty list, `s_common`.
  314. If "have" was sent:
  315. Loop through the objects in the order supplied by the client.
  316. For each object, if the server has the object reachable from
  317. a ref, add it to `s_common`. If a commit is added to `s_common`,
  318. do not add any ancestors, even if they also appear in `have`.
  319. S: Send the git-upload-pack response:
  320. If the server has found a closed set of objects to pack or the
  321. request ends with "done", it replies with the pack.
  322. TODO: Document the pack based response
  323. S: PACK...
  324. The returned stream is the side-band-64k protocol supported
  325. by the git-upload-pack service, and the pack is embedded into
  326. stream 1. Progress messages from the server side MAY appear
  327. in stream 2.
  328. Here a "closed set of objects" is defined to have at least
  329. one path from every "want" to at least one "common" object.
  330. If the server needs more information, it replies with a
  331. status continue response:
  332. TODO: Document the non-pack response
  333. C: Parse the upload-pack response:
  334. TODO: Document parsing response
  335. 'Do another compute step.'
  336. Smart Service git-receive-pack
  337. ------------------------------
  338. This service reads from the repository pointed to by `$GIT_URL`.
  339. Clients MUST first perform ref discovery with
  340. `$GIT_URL/info/refs?service=git-receive-pack`.
  341. C: POST $GIT_URL/git-receive-pack HTTP/1.0
  342. C: Content-Type: application/x-git-receive-pack-request
  343. C:
  344. C: ....0a53e9ddeaddad63ad106860237bbf53411d11a7 441b40d833fdfa93eb2908e52742248faf0ee993 refs/heads/maint\0 report-status
  345. C: 0000
  346. C: PACK....
  347. S: 200 OK
  348. S: Content-Type: application/x-git-receive-pack-result
  349. S: Cache-Control: no-cache
  350. S:
  351. S: ....
  352. Clients MUST NOT reuse or revalidate a cached response.
  353. Servers MUST include sufficient Cache-Control headers
  354. to prevent caching of the response.
  355. Servers SHOULD support all capabilities defined here.
  356. Clients MUST send at least one command in the request body.
  357. Within the command portion of the request body clients SHOULD send
  358. the id obtained through ref discovery as old_id.
  359. update_request = command_list
  360. "PACK" <binary data>
  361. command_list = PKT-LINE(command NUL cap_list LF)
  362. *(command_pkt)
  363. command_pkt = PKT-LINE(command LF)
  364. cap_list = *(SP capability) SP
  365. command = create / delete / update
  366. create = zero-id SP new_id SP name
  367. delete = old_id SP zero-id SP name
  368. update = old_id SP new_id SP name
  369. TODO: Document this further.
  370. References
  371. ----------
  372. http://www.ietf.org/rfc/rfc1738.txt[RFC 1738: Uniform Resource Locators (URL)]
  373. http://www.ietf.org/rfc/rfc2616.txt[RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1]
  374. link:technical/pack-protocol.html
  375. link:technical/protocol-capabilities.html