forked from apache/arrow-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody.rs
221 lines (194 loc) · 6.63 KB
/
body.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use crate::client::connection::{HttpError, HttpErrorKind};
use crate::{collect_bytes, PutPayload};
use bytes::Bytes;
use futures::stream::BoxStream;
use futures::StreamExt;
use http_body_util::combinators::BoxBody;
use http_body_util::{BodyExt, Full};
use hyper::body::{Body, Frame, SizeHint};
use std::pin::Pin;
use std::task::{Context, Poll};
/// An HTTP Request
pub type HttpRequest = http::Request<HttpRequestBody>;
/// The [`Body`] of an [`HttpRequest`]
#[derive(Debug, Clone)]
pub struct HttpRequestBody(Inner);
impl HttpRequestBody {
/// An empty [`HttpRequestBody`]
pub fn empty() -> Self {
Self(Inner::Bytes(Bytes::new()))
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn into_reqwest(self) -> reqwest::Body {
match self.0 {
Inner::Bytes(b) => b.into(),
Inner::PutPayload(_, payload) => reqwest::Body::wrap_stream(futures::stream::iter(
payload.into_iter().map(Ok::<_, HttpError>),
)),
}
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub(crate) fn into_reqwest(self) -> reqwest::Body {
match self.0 {
Inner::Bytes(b) => b.into(),
Inner::PutPayload(_, payload) => Into::<Bytes>::into(payload).into(),
}
}
/// Returns true if this body is empty
pub fn is_empty(&self) -> bool {
match &self.0 {
Inner::Bytes(x) => x.is_empty(),
Inner::PutPayload(_, x) => x.iter().any(|x| !x.is_empty()),
}
}
/// Returns the total length of the [`Bytes`] in this body
pub fn content_length(&self) -> usize {
match &self.0 {
Inner::Bytes(x) => x.len(),
Inner::PutPayload(_, x) => x.content_length(),
}
}
/// If this body consists of a single contiguous [`Bytes`], returns it
pub fn as_bytes(&self) -> Option<&Bytes> {
match &self.0 {
Inner::Bytes(x) => Some(x),
_ => None,
}
}
}
impl From<Bytes> for HttpRequestBody {
fn from(value: Bytes) -> Self {
Self(Inner::Bytes(value))
}
}
impl From<Vec<u8>> for HttpRequestBody {
fn from(value: Vec<u8>) -> Self {
Self(Inner::Bytes(value.into()))
}
}
impl From<String> for HttpRequestBody {
fn from(value: String) -> Self {
Self(Inner::Bytes(value.into()))
}
}
impl From<PutPayload> for HttpRequestBody {
fn from(value: PutPayload) -> Self {
Self(Inner::PutPayload(0, value))
}
}
#[derive(Debug, Clone)]
enum Inner {
Bytes(Bytes),
PutPayload(usize, PutPayload),
}
impl Body for HttpRequestBody {
type Data = Bytes;
type Error = HttpError;
fn poll_frame(
mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
Poll::Ready(match &mut self.0 {
Inner::Bytes(bytes) => {
let out = bytes.split_off(0);
if out.is_empty() {
None
} else {
Some(Ok(Frame::data(out)))
}
}
Inner::PutPayload(offset, payload) => {
let slice = payload.as_ref();
if *offset == slice.len() {
None
} else {
Some(Ok(Frame::data(
slice[std::mem::replace(offset, *offset + 1)].clone(),
)))
}
}
})
}
fn is_end_stream(&self) -> bool {
match self.0 {
Inner::Bytes(ref bytes) => bytes.is_empty(),
Inner::PutPayload(offset, ref body) => offset == body.as_ref().len(),
}
}
fn size_hint(&self) -> SizeHint {
match self.0 {
Inner::Bytes(ref bytes) => SizeHint::with_exact(bytes.len() as u64),
Inner::PutPayload(offset, ref payload) => {
let iter = payload.as_ref().iter().skip(offset);
SizeHint::with_exact(iter.map(|x| x.len() as u64).sum())
}
}
}
}
/// An HTTP response
pub type HttpResponse = http::Response<HttpResponseBody>;
/// The body of an [`HttpResponse`]
#[derive(Debug)]
pub struct HttpResponseBody(BoxBody<Bytes, HttpError>);
impl HttpResponseBody {
/// Create an [`HttpResponseBody`] from the provided [`Body`]
///
/// Note: [`BodyExt::map_err`] can be used to alter error variants
pub fn new<B>(body: B) -> Self
where
B: Body<Data = Bytes, Error = HttpError> + Send + Sync + 'static,
{
Self(BoxBody::new(body))
}
/// Collects this response into a [`Bytes`]
pub async fn bytes(self) -> Result<Bytes, HttpError> {
let size_hint = self.0.size_hint().lower();
let s = self.0.into_data_stream();
collect_bytes(s, Some(size_hint)).await
}
/// Returns a stream of this response data
pub fn bytes_stream(self) -> BoxStream<'static, Result<Bytes, HttpError>> {
self.0.into_data_stream().boxed()
}
/// Returns the response as a [`String`]
pub(crate) async fn text(self) -> Result<String, HttpError> {
let b = self.bytes().await?;
String::from_utf8(b.into()).map_err(|e| HttpError::new(HttpErrorKind::Decode, e))
}
#[cfg(any(feature = "aws", feature = "gcp", feature = "azure"))]
pub(crate) async fn json<B: serde::de::DeserializeOwned>(self) -> Result<B, HttpError> {
let b = self.bytes().await?;
serde_json::from_slice(&b).map_err(|e| HttpError::new(HttpErrorKind::Decode, e))
}
}
impl From<Bytes> for HttpResponseBody {
fn from(value: Bytes) -> Self {
Self::new(Full::new(value).map_err(|e| match e {}))
}
}
impl From<Vec<u8>> for HttpResponseBody {
fn from(value: Vec<u8>) -> Self {
Bytes::from(value).into()
}
}
impl From<String> for HttpResponseBody {
fn from(value: String) -> Self {
Bytes::from(value).into()
}
}