Skip to content

Commit

Permalink
[ref-ls] tune request to actually work in all cases, particularly for…
Browse files Browse the repository at this point in the history
… github

It wants the User-Agent set, as well as the protocol version if greater
than 1.
  • Loading branch information
Byron committed Sep 5, 2020
1 parent 2fcb557 commit 6bab2f3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions git-transport/src/client/http/curl/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub fn new() -> (
handle.http_headers(headers)?;
handle.transfer_encoding(false)?;
handle.connect_timeout(Duration::from_secs(20))?;
// handle.proxy("http://localhost:9090")?;
let low_bytes_per_second = 1024;
handle.low_speed_limit(low_bytes_per_second)?;
handle.low_speed_time(Duration::from_secs(20))?;
Expand Down
10 changes: 10 additions & 0 deletions git-transport/src/client/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Transport<H: Http> {
url: String,
user_agent_header: &'static str,
desired_version: crate::Protocol,
actual_version: crate::Protocol,
http: H,
service: Option<Service>,
line_provider: Option<git_packetline::Provider<H::ResponseBody>>,
Expand All @@ -34,6 +35,7 @@ impl Transport<Impl> {
url: url.to_owned(),
user_agent_header: concat!("User-Agent: git/oxide-", env!("CARGO_PKG_VERSION")),
desired_version: version,
actual_version: version,
service: None,
http: Impl::default(),
line_provider: None,
Expand Down Expand Up @@ -123,6 +125,7 @@ impl<H: Http> client::Transport for Transport<H> {
refs,
protocol: actual_protocol,
} = capabilities::recv::v1_or_v2_as_detected(line_reader)?;
self.actual_version = actual_protocol;
self.service = Some(service);
Ok(client::SetServiceResponse {
actual_protocol,
Expand All @@ -144,12 +147,19 @@ impl<H: Http> client::Transport for Transport<H> {
let service = self.service.expect("handshake() must have been called first");
let url = append_url(&self.url, service.as_str());
let static_headers = &[
Cow::Borrowed(self.user_agent_header),
Cow::Owned(format!("Content-Type: application/x-{}-request", service.as_str())),
format!("Accept: application/x-{}-result", service.as_str()).into(),
"Expect:".into(), // needed to avoid sending Expect: 100-continue, which adds another response and only CURL wants that
];
let mut dynamic_headers = Vec::new();
self.add_basic_auth_if_present(&mut dynamic_headers)?;
if self.actual_version != Protocol::V1 {
dynamic_headers.push(Cow::Owned(format!(
"Git-Protocol: version={}",
self.actual_version as usize
)));
}

let PostResponse {
headers,
Expand Down
13 changes: 12 additions & 1 deletion git-transport/tests/client/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Authorization: Basic dXNlcjpwYXNzd29yZA==
"POST /path/not-important/git-upload-pack HTTP/1.1
Host: 127.0.0.1:{}
Transfer-Encoding: chunked
User-Agent: git/oxide-{}
Content-Type: application/x-git-upload-pack-request
Accept: application/x-git-upload-pack-result
Authorization: Basic dXNlcjpwYXNzd29yZA==
Expand All @@ -80,9 +81,11 @@ Authorization: Basic dXNlcjpwYXNzd29yZA==
",
server.addr.port(),
env!("CARGO_PKG_VERSION")
)
.lines()
.collect::<Vec<_>>()
.collect::<Vec<_>>(),
"the authentication information is used in subsequent calls"
);

Ok(())
Expand Down Expand Up @@ -267,6 +270,7 @@ fn clone_v1() -> crate::Result {
"POST /path/not/important/due/to/mock/git-upload-pack HTTP/1.1
Host: 127.0.0.1:{}
Transfer-Encoding: chunked
User-Agent: git/oxide-{}
Content-Type: application/x-git-upload-pack-request
Accept: application/x-git-upload-pack-result
Expand All @@ -279,6 +283,7 @@ Accept: application/x-git-upload-pack-result
",
server.addr.port(),
env!("CARGO_PKG_VERSION")
)
.lines()
.collect::<Vec<_>>()
Expand Down Expand Up @@ -365,8 +370,10 @@ Git-Protocol: version=2
"POST /path/not/important/due/to/mock/git-upload-pack HTTP/1.1
Host: 127.0.0.1:{}
Transfer-Encoding: chunked
User-Agent: git/oxide-{}
Content-Type: application/x-git-upload-pack-request
Accept: application/x-git-upload-pack-result
Git-Protocol: version=2
4c
0014command=ls-refs
Expand All @@ -378,6 +385,7 @@ Accept: application/x-git-upload-pack-result
",
server.addr.port(),
env!("CARGO_PKG_VERSION")
)
.lines()
.collect::<Vec<_>>()
Expand Down Expand Up @@ -415,8 +423,10 @@ Accept: application/x-git-upload-pack-result
"POST /path/not/important/due/to/mock/git-upload-pack HTTP/1.1
Host: 127.0.0.1:{}
Transfer-Encoding: chunked
User-Agent: git/oxide-{}
Content-Type: application/x-git-upload-pack-request
Accept: application/x-git-upload-pack-result
Git-Protocol: version=2
16
0012command=fetch
Expand All @@ -425,6 +435,7 @@ Accept: application/x-git-upload-pack-result
",
server.addr.port(),
env!("CARGO_PKG_VERSION")
)
.lines()
.collect::<Vec<_>>()
Expand Down

0 comments on commit 6bab2f3

Please sign in to comment.