Skip to content

Commit

Permalink
Make transport::Service Debug and add http constants from libgit2…
Browse files Browse the repository at this point in the history
…'s http transport implementation.
  • Loading branch information
vcfxb committed Jul 18, 2024
1 parent f1f09ce commit bdd05c0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bitflags = "2.1.0"
libc = "0.2"
log = "0.4.8"
libgit2-sys = { path = "libgit2-sys", version = "0.17.0" }
http = "1.1"

[target."cfg(all(unix, not(target_os = \"macos\")))".dependencies]
openssl-sys = { version = "0.9.45", optional = true }
Expand Down
47 changes: 46 additions & 1 deletion src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait SmartSubtransport: Send + 'static {
}

/// Actions that a smart transport can ask a subtransport to perform
#[derive(Copy, Clone, PartialEq)]
#[derive(Copy, Clone, PartialEq, Debug)]
#[allow(missing_docs)]
pub enum Service {
UploadPackLs,
Expand All @@ -63,6 +63,51 @@ pub enum Service {
ReceivePack,
}

/// HTTP implementation related info for various services.
///
/// This information was pulled from
/// <https://github.com/libgit2/libgit2/blob/2ecc8586f7eec4063b5da1563d0a33f9e9f9fcf7/src/libgit2/transports/http.c#L68-L95>.
impl Service {
/// The HTTP Method used by libgit2's implementation for http(s) transport.
pub const fn http_method(self) -> http::Method {
use http::Method;

match self {
Service::UploadPackLs | Service::ReceivePackLs => Method::GET,
Service::UploadPack | Service::ReceivePack => Method::POST,
}
}

/// The value of the HTTP "Accept" header that is used by libgit2's implementation for http(s) transport.
pub const fn http_accept_header(self) -> &'static str {
match self {
Service::UploadPackLs => "application/x-git-upload-pack-advertisement",
Service::ReceivePackLs => "application/x-git-receive-pack-advertisement",
Service::UploadPack => "application/x-git-upload-pack-result",
Service::ReceivePack => "application/x-git-receive-pack-result",
}
}

/// The value of the HTTP "Content-Type" header that is used by libgit2's implementation for http(s) transport.
pub const fn http_content_type_header(self) -> Option<&'static str> {
match self {
Service::UploadPackLs | Service::ReceivePackLs => None,
Service::ReceivePack => Some("application/x-git-receive-pack-request"),
Service::UploadPack => Some("application/x-git-upload-pack-request"),
}
}

/// The HTTP Url path and query suffix used by libgit2's implementation for http(s) transport.
pub const fn http_path_and_query(self) -> &'static str {
match self {
Service::UploadPackLs => "/info/refs?service=git-upload-pack",
Service::UploadPack => "/git-upload-pack",
Service::ReceivePackLs => "/info/refs?service=git-receive-pack",
Service::ReceivePack => "/git-receive-pack",
}
}
}

/// An instance of a stream over which a smart transport will communicate with a
/// remote.
///
Expand Down

0 comments on commit bdd05c0

Please sign in to comment.