History

[clone] Improve usability of posts…

…it's much easier for callers to get a write handle to write into from their own code instead of passing a Read that captures everything they care about. This just requires them to signal that they are done writing by dropping the writer. Fair enough, that's all done in our custom transport code who knows the system. Thinking about it, it will probably yield that handle to code in git-protocol, which then also needs to know about that.

Sebastian Thiel committed 6 years ago

Commit e1b944e1 parent d0bdbe42 files · +38 −19Browse files

Unified · Split

2 files
  1. git-transport/src/client/http/curl.rs +14 −16
  2. git-transport/src/client/http/mod.rs +24 −3
git-transport/src/client/http/curl.rs+14−16
@@ -3,7 +3,6 @@ use curl::easy::Easy2; · 2 unmodified lines
33 use git_features::pipe;
44 use std::{
55 io,
6− io::Read,
76 sync::mpsc::{sync_channel, Receiver, SyncSender, TrySendError},
87 thread,
98 };
@@ -37,8 +36,7 @@ impl Curl { · 27 unmodified lines
3736 &mut self,
3837 url: &str,
3938 headers: impl IntoIterator<Item = impl AsRef<str>>,
40− post_body: Option<impl Read>,
41− ) -> Result<(pipe::Reader, pipe::Reader), http::Error> {
39+ ) -> Result<http::PostResponse<pipe::Reader, pipe::Reader, pipe::Writer>, http::Error> {
4240 let mut list = curl::easy::List::new();
4341 for header in headers {
4442 list.append(header.as_ref())?;
@@ -56,16 +54,16 @@ impl Curl { · 11 unmodified lines
5654 let Response {
5755 headers,
5856 body,
59− mut upload_body,
57+ upload_body,
6058 } = match self.res.recv() {
6159 Ok(res) => res,
6260 Err(_) => return Err(self.restore_thread_after_failure()),
6361 };
64− if let Some(mut body) = post_body {
65− io::copy(&mut body, &mut upload_body)?;
66− }
67−
68− Ok((headers, body))
62+ Ok(http::PostResponse {
63+ post_body: upload_body,
64+ headers,
65+ body,
66+ })
6967 }
7068
7169 fn restore_thread_after_failure(&mut self) -> http::Error {
@@ -170,21 +168,21 @@ impl From<curl::Error> for http::Error { · 98 unmodified lines
170168 impl crate::client::http::Http for Curl {
171169 type Headers = pipe::Reader;
172170 type ResponseBody = pipe::Reader;
171+ type PostBody = pipe::Writer;
173172
174173 fn get(
175174 &mut self,
176175 url: &str,
177176 headers: impl IntoIterator<Item = impl AsRef<str>>,
178− ) -> Result<(Self::Headers, Self::ResponseBody), http::Error> {
179− self.make_request(url, headers, None::<std::fs::File>)
177+ ) -> Result<http::GetResponse<Self::Headers, Self::ResponseBody>, http::Error> {
178+ self.make_request(url, headers).map(Into::into)
180179 }
181180
182181 fn post(
183182 &mut self,
184− _url: &str,
185− _headers: impl IntoIterator<Item = impl AsRef<str>>,
186− _body: impl Read,
187− ) -> Result<(Self::Headers, Self::ResponseBody), http::Error> {
188− unimplemented!()
183+ url: &str,
184+ headers: impl IntoIterator<Item = impl AsRef<str>>,
185+ ) -> Result<http::PostResponse<Self::Headers, Self::ResponseBody, Self::PostBody>, http::Error> {
186+ self.make_request(url, headers)
189187 }
190188 }
git-transport/src/client/http/mod.rs+24−3
@@ -20,21 +20,42 @@ quick_error! { · 19 unmodified lines
2020 #[cfg(feature = "http-client-curl")]
2121 pub(crate) mod curl;
2222
23+pub struct GetResponse<H, B> {
24+ headers: H,
25+ body: B,
26+}
27+
28+pub struct PostResponse<H, B, PB> {
29+ /// **Note**: Implementations should drop the handle to avoid deadlocks
30+ post_body: PB,
31+ headers: H,
32+ body: B,
33+}
34+
35+impl<A, B, C> From<PostResponse<A, B, C>> for GetResponse<A, B> {
36+ fn from(v: PostResponse<A, B, C>) -> Self {
37+ GetResponse {
38+ headers: v.headers,
39+ body: v.body,
40+ }
41+ }
42+}
43+
2344 trait Http {
2445 type Headers: io::BufRead;
2546 type ResponseBody: io::BufRead;
47+ type PostBody: io::Write;
2648
2749 fn get(
2850 &mut self,
2951 url: &str,
3052 headers: impl IntoIterator<Item = impl AsRef<str>>,
31− ) -> Result<(Self::Headers, Self::ResponseBody), Error>;
53+ ) -> Result<GetResponse<Self::Headers, Self::ResponseBody>, Error>;
3254 fn post(
3355 &mut self,
3456 url: &str,
3557 headers: impl IntoIterator<Item = impl AsRef<str>>,
36− body: impl io::Read,
37− ) -> Result<(Self::Headers, Self::ResponseBody), Error>;
58+ ) -> Result<PostResponse<Self::Headers, Self::ResponseBody, Self::PostBody>, Error>;
3859 }
3960
4061 #[cfg(feature = "http-client-curl")]