簡體   English   中英

Git 智能 HTTP(S) 協議在它所有的榮耀中完全是什么樣子的?

[英]What does the Git Smart HTTP(S) protocol fully look like in all its glory?

我正在嘗試實現一個模擬 Git 遙控器的網絡服務器。 用戶應該能夠從我的服務器克隆或拉取、編輯文件、提交和推送(使用身份驗證)——與 Git 相關的正常操作。但是,在服務器端不是一個裸露的 Git 存儲庫或任何東西; 數據以其他格式存儲,並且僅在請求時才進行轉換。

我花了很多時間試圖找出 Git Smart HTTP 協議的工作原理,以下是我目前所知道的。

http-protocol 上的 Git 文檔,我知道GET $GIT_URL/info/refs?service=git-upload-pack HTTP/1.1應該引發以下(示例)響應:

HTTP/1.1 200 OK<CRLF>
Content-Type: application/x-git-upload-pack-advertisement<CRLF>
Cache-Control: no-cache<CRLF>
<CRLF>
001e# service=git-upload-pack<LF>
0000<no LF>
004895dcfa3633004da0049d3d0fa03f80589cbcaf31 refs/heads/maint<NUL>multi_ack<LF>
003fd049f6c27a2244e12041955e262a404c7faba355 refs/heads/master<LF>
003c2cb58b79488a98d2721cea644875a8dd0026b115 refs/tags/v1.0<LF>
003fa3c2e2402b99163d1d59756e5f207ae21cccba4c refs/tags/v1.0^{}<LF>
0000

從我自己對很少提交的回購進行的實驗來看,到目前為止,GitHub 似乎完全在文檔中描述的協議限制范圍內:

HTTP/1.1 200 OK<CRLF>
Server: GitHub Babel 2.0<CRLF>
Content-Type: application/x-git-upload-pack-advertisement<CRLF>
Content-Security-Policy: default-src 'none'; sandbox<CRLF>
Transfer-Encoding: chunked<CRLF>
expires: Fri, 01 Jan 1980 00:00:00 GMT<CRLF>
pragma: no-cache<CRLF>
Cache-Control: no-cache, max-age=0, must-revalidate<CRLF>
Vary: Accept-Encoding<CRLF>
X-Frame-Options: DENY<CRLF>
X-GitHub-Request-Id: [redacted]<CRLF>
<CRLF>
001e# service=git-upload-pack<LF>
0000<no LF>0156feee8d0aeff172f5b39e3175175d027f3fd5ecc1 HEAD<NUL>multi_ack thin-pack side-band side-band-64k ofs-delta shallow deepen-since deepen-not deepen-relative no-progress include-tag multi_ack_detailed allow-tip-sha1-in-want allow-reachable-sha1-in-want no-done symref=HEAD:refs/heads/master filter object-format=sha1 agent=git/github-g69d6dd5d35d8<LF>
003ffeee8d0aeff172f5b39e3175175d027f3fd5ecc1 refs/heads/master<LF>
0000

然而,這是簡單的部分結束的地方。 如果我想實際獲取提交數據怎么辦? 有關此事的 Git 文檔給出了要發送的 POST 請求示例和一些語法,然后說“ TODO: Document this further ”。 ????????

我嘗試以我在文檔中看到的格式通過 CURLing GitHub 進行試驗。

(cwd)>curl https://github.com/Kenny2github/ConvoSplit.git/git-upload-pack -o - -i -X POST -d @-
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
0032have 941ea62275547bcbfb78fd97d29be18d09a78190
0009done
0000
^Z
HTTP/1.1 200 OK
Server: GitHub Babel 2.0
Content-Type: application/x-git-upload-pack-result
Content-Security-Policy: default-src 'none'; sandbox
Transfer-Encoding: chunked
expires: Fri, 01 Jan 1980 00:00:00 GMT
pragma: no-cache
Cache-Control: no-cache, max-age=0, must-revalidate
Vary: Accept-Encoding
X-GitHub-Request-Id: [redacted]
X-Frame-Options: DENY

curl: (18) transfer closed with outstanding read data remaining

什么?

我嘗試使用 Python:

>>> import requests
>>> requests.post('https://github.com/Kenny2github/ConvoSplit.git/git-upload-pack', data=b'''
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
0032have 941ea62275547bcbfb78fd97d29be18d09a78190
0009done
0000
'''.strip())
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 572, in _update_chunk_length
    self.chunk_left = int(line, 16)
ValueError: invalid literal for int() with base 16: b''

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 331, in _error_catcher
    yield
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 637, in read_chunked
    self._update_chunk_length()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 576, in _update_chunk_length
    raise httplib.IncompleteRead(line)
http.client.IncompleteRead: IncompleteRead(0 bytes read)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 751, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 461, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 665, in read_chunked
    self._original_response.close()
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 349, in _error_catcher
    raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    requests.post('https://github.com/Kenny2github/ConvoSplit.git/git-upload-pack', data=b'0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1\n0032have 941ea62275547bcbfb78fd97d29be18d09a78190\n0009done\n0000')
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 685, in send
    r.content
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 829, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 754, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(0 bytes read)', IncompleteRead(0 bytes read))

http 協議文檔的 rest 沒有幫助 - 出現了另外六個 TODO。 pack-protocol 文檔至少讓我知道我應該接收什么,但沒有說明如何接收。

傳輸協議文檔沒有告訴我任何新內容,然后說“看看 Git 源代碼”。 我試過了,但它是核心 C,我必須基本上了解 Git 本身的整個基礎設施。 (我可能會嘗試這樣做,但現在不是時候。)

我確實設法收集到涉及git upload-pack ,並且運行git upload-pack --stateless-rpc --advertise-refs.git確實像以前一樣給了我 /info/refs 列表。 然而,從中取出實際包的嘗試失敗了,不僅失敗了,而且在平台之間的失敗也不一致。

在 Windows 上:

(cwd)>git upload-pack --stateless-rpc .git
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
0009done # I hit Enter and nothing else
fatal: protocol error: bad line length character:
000

(cwd)>git upload-pack --stateless-rpc .git
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
0000 # likewise
fatal: protocol error: bad line length character:
000

(cwd)>py -c "print('0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1\n0009done\n0000')" | git upload-pack --stateless-rpc .git
fatal: protocol error: bad line length character:
000

懷疑是回車引起的問題,我嘗試了WSL:

$ git upload-pack --stateless-rpc .git
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
0000 # I hit Enter and then ^D after 0000
fatal: The remote end hung up unexpectedly

$ git upload-pack --stateless-rpc .git
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
0009done # I hit Enter and did NOT hit ^D
fatal: git upload-pack: protocol error, expected to get sha, not 'done'

$ # using Python to pipe each of the above inputs yielded the same results

我究竟做錯了什么? 如何讓 GitHub/git-upload-pack 尊重我?

首先,不可能在 StackOverflow 的回答中解釋整個協議; 解釋太長了。 但是,我會嘗試指出一些需要注意的事項。

首先,當你說協議時,你需要非常准確; 這不是容忍行尾差異和額外字節的情況。 因此,如果您要合成數據以傳遞到遠程,則應使用printf(1)或編程語言來完成。 不要在 shell 上打字。

Git 使用 pkt-line 格式,這意味着每一行或每一塊數據都以一個代表數據長度和前綴的四個十六進制字符序列為前綴。 如果序列為 0000,則這是一個刷新數據包,它表示該數據塊的結尾。 如果序列為 0001,則這是一個定界符數據包,它在協議 v2 中用於定界該數據塊的各個部分。 否則,十六進制序列的值不能超過 65519。

在您發送want和線路的情況下,您have進行多次迭代,直到服務器向您發送一個包。 在 HTTP 中,這是多個請求。 服務器將向您發送您指定have arguments 的確認。 服務器期望找到從每個want指令到雙方都有的 object 的路徑(否則,客戶端什么都沒有,在這種情況下存儲庫為空)。

請注意,此任務實際上非常復雜。 現在有一個用於提取的協議的 v2(舊的是 v0,還有一個 v1,它是相同的但帶有版本標頭)。 您還應該期望能夠支持 SHA-256 存儲庫,這些存儲庫當前不與 SHA-1 存儲庫互操作,但在其他方面受到支持。 並且 Git 還提供了大量您實際上想要支持的擴展,例如邊帶功能,如果您想向 output 用戶提供有關您一方正在做什么的信息,則這是必需的。

該文檔主要位於 Git 存儲庫中的Documentation/technical中。 它在某些地方是不完整的,但你應該能夠通過一些閱讀和測試來辨別它。

好吧,經過更多的實驗,我偶然發現了正確的組合,如果你願意的話。

$ git upload-pack --stateless-rpc .git > tmp.pack
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
00000009done # Enter with NO ^D
Counting objects: 16, done.
Compressing objects: 100% (14/14), done.
Total 16 (delta 3), reused 0 (delta 0)
$ hd tmp.bin
00000000  30 30 30 38 4e 41 4b 0a  50 41 43 4b 00 00 00 02  |0008NAK.PACK....|
00000010  00 00 00 10 94 2f 78 9c  a5 92 4f 6f db 30 0c c5  |...../x...Oo.0..|
...
>>> import requests
>>> # omitting the trailing \n results in a 200 OK blank response
>>> r = requests.post('https://github.com/Kenny2github/ConvoSplit.git/git-upload-pack', data=b'0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1\n00000009done\n')
>>> r.text[:20]
'0008NAK\nPACK\x00\x00\x00\x02\x00\x00\x00\x10'

但是,這只能讓我控制我想要的提交。 如果我嘗試指定我有哪些提交(就像我應該能夠的那樣),我只會得到我擁有的 ACK:

>>> print(requests.post('https://github.com/Kenny2github/ConvoSplit.git/git-upload-pack', data=b'''
0032want feee8d0aeff172f5b39e3175175d027f3fd5ecc1
00000032have 941ea62275547bcbfb78fd97d29be18d09a78190
0032have 93dbc9cfb21d23c6eb5313419bfaa8213619c73c
0032have 648508d6359b3e8992ee5a6d9fee6f86110202fd
00000009done
'''.lstrip()).text)
0031ACK 941ea62275547bcbfb78fd97d29be18d09a78190
0031ACK 93dbc9cfb21d23c6eb5313419bfaa8213619c73c
0031ACK 648508d6359b3e8992ee5a6d9fee6f86110202fd

(如果我嘗試使用git upload-pack一樣。)如何正確處理整個過程的 rest ? 再一次,我的目標是模擬一個(本質上)完整的 git 遙控器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM