#![allow(dead_code)]
use crate::{
error::Error,
request::{random, request_error},
};
use bytes::Bytes;
use hyper::body::{Body, Frame, SizeHint};
use std::{
collections::VecDeque,
fmt, io,
pin::Pin,
sync::{Mutex, PoisonError},
task::{Context, Poll},
};
use tokio::io::{AsyncRead, ReadBuf};
const DEFAULT_CONTENT_TYPE: &str = "application/octet-stream";
pub struct Upload {
source: Source,
filename: Option<String>,
content_type: String,
}
struct SyncReader(Mutex<Pin<Box<dyn AsyncRead + Send>>>);
impl SyncReader {
fn new(reader: impl AsyncRead + Send + 'static) -> Self {
Self(Mutex::new(Box::pin(reader)))
}
}
impl AsyncRead for SyncReader {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let reader = self
.get_mut()
.0
.get_mut()
.unwrap_or_else(PoisonError::into_inner);
reader.as_mut().poll_read(cx, buf)
}
}
impl fmt::Debug for Upload {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let length = match &self.source {
Source::Bytes(bytes) => Some(bytes.len() as u64),
Source::Reader { length, .. } => *length,
};
f.debug_struct("Upload")
.field("filename", &self.filename)
.field("content_type", &self.content_type)
.field("length", &length)
.finish()
}
}
enum Source {
Bytes(Bytes),
Reader {
reader: Option<SyncReader>,
length: Option<u64>,
},
}
impl Upload {
#[must_use]
pub fn bytes(bytes: impl Into<Bytes>) -> Self {
Self {
source: Source::Bytes(bytes.into()),
filename: None,
content_type: DEFAULT_CONTENT_TYPE.into(),
}
}
#[must_use]
pub fn reader(reader: impl AsyncRead + Send + 'static, length: Option<u64>) -> Self {
Self {
source: Source::Reader {
reader: Some(SyncReader::new(reader)),
length,
},
filename: None,
content_type: DEFAULT_CONTENT_TYPE.into(),
}
}
#[must_use]
pub fn with_filename(mut self, filename: impl Into<String>) -> Self {
self.filename = Some(filename.into());
self
}
#[must_use]
pub fn with_content_type(mut self, content_type: impl Into<String>) -> Self {
self.content_type = content_type.into();
self
}
pub(crate) fn replayable(&self) -> bool {
matches!(self.source, Source::Bytes(_))
}
fn length(&self) -> Option<u64> {
match &self.source {
Source::Bytes(bytes) => Some(bytes.len() as u64),
Source::Reader { length, .. } => *length,
}
}
fn segment(&mut self) -> Result<Segment, Error> {
match &mut self.source {
Source::Bytes(bytes) => Ok(Segment::Bytes(bytes.clone())),
Source::Reader { reader, length } => Ok(Segment::Reader {
reader: reader
.take()
.ok_or_else(|| invalid("upload reader has already been consumed"))?,
remaining: *length,
tail: None,
}),
}
}
pub(crate) fn body(&mut self) -> Result<RequestBody, Error> {
Ok(RequestBody {
remaining: self.length(),
segments: [self.segment()?].into(),
})
}
}
fn invalid(message: &str) -> Error {
request_error(message.to_owned())
}
fn quoted(value: &str) -> Result<String, Error> {
if value.chars().any(char::is_control) {
return Err(invalid(
"multipart names and filenames cannot contain control characters",
));
}
Ok(value.replace('\\', "\\\\").replace('"', "\\\""))
}
pub(crate) struct Multipart {
boundary: String,
parts: Vec<(Bytes, Upload)>,
}
impl Default for Multipart {
fn default() -> Self {
Self::new()
}
}
impl Multipart {
pub(crate) fn new() -> Self {
Self {
boundary: format!("perseid-{:016x}{:016x}", random(), random()),
parts: Vec::new(),
}
}
pub(crate) fn field(self, name: &str, value: impl serde::Serialize) -> Result<Self, Error> {
self.field_with(name, value, None)
}
pub(crate) fn field_as(
self,
name: &str,
value: impl serde::Serialize,
content_type: &str,
) -> Result<Self, Error> {
self.field_with(name, value, Some(content_type))
}
fn field_with(
mut self,
name: &str,
value: impl serde::Serialize,
declared: Option<&str>,
) -> Result<Self, Error> {
let value = serde_json::to_value(value).map_err(request_error)?;
if let serde_json::Value::Array(items) = value {
for item in items {
self = self.field_with(name, item, declared)?;
}
return Ok(self);
}
let content_type = match declared {
Some(declared) => {
http::HeaderValue::from_str(declared).map_err(request_error)?;
format!("Content-Type: {declared}\r\n")
}
None if value.is_object() || value.is_array() => {
"Content-Type: application/json\r\n".to_owned()
}
None => String::new(),
};
let value = match value {
serde_json::Value::String(s) => s,
other => other.to_string(),
};
let header = format!(
"--{}\r\nContent-Disposition: form-data; name=\"{}\"\r\n{content_type}\r\n",
self.boundary,
quoted(name)?
);
self.parts.push((Bytes::from(header), Upload::bytes(value)));
Ok(self)
}
pub(crate) fn file_as(
self,
name: &str,
mut upload: Upload,
content_type: &str,
) -> Result<Self, Error> {
if upload.content_type == DEFAULT_CONTENT_TYPE {
upload.content_type = content_type.to_owned();
}
self.file(name, upload)
}
pub(crate) fn file(mut self, name: &str, upload: Upload) -> Result<Self, Error> {
let filename = quoted(upload.filename.as_deref().unwrap_or("file"))?;
let name = quoted(name)?;
http::HeaderValue::from_str(&upload.content_type).map_err(request_error)?;
let header = format!("--{}\r\nContent-Disposition: form-data; name=\"{name}\"; filename=\"{filename}\"\r\nContent-Type: {}\r\n\r\n", self.boundary, upload.content_type);
self.parts.push((Bytes::from(header), upload));
Ok(self)
}
pub(crate) fn content_type(&self) -> String {
format!("multipart/form-data; boundary={}", self.boundary)
}
pub(crate) fn replayable(&self) -> bool {
self.parts.iter().all(|(_, upload)| upload.replayable())
}
pub(crate) fn body(&mut self) -> Result<RequestBody, Error> {
let mut segments = VecDeque::new();
let mut length = Some(0u64);
for (header, upload) in &mut self.parts {
length = length
.and_then(|n| n.checked_add(header.len() as u64))
.and_then(|n| upload.length().and_then(|len| n.checked_add(len)))
.and_then(|n| n.checked_add(2));
segments.push_back(Segment::Bytes(header.clone()));
segments.push_back(upload.segment()?);
segments.push_back(Segment::Bytes(Bytes::from_static(b"\r\n")));
}
let end = Bytes::from(format!("--{}--\r\n", self.boundary));
length = length.and_then(|n| n.checked_add(end.len() as u64));
segments.push_back(Segment::Bytes(end));
Ok(RequestBody {
segments,
remaining: length,
})
}
}
enum Segment {
Bytes(Bytes),
Reader {
reader: SyncReader,
remaining: Option<u64>,
tail: Option<Bytes>,
},
}
pub struct RequestBody {
segments: VecDeque<Segment>,
remaining: Option<u64>,
}
impl RequestBody {
pub(crate) fn empty() -> Self {
Self {
segments: VecDeque::new(),
remaining: Some(0),
}
}
pub(crate) fn length(&self) -> Option<u64> {
self.remaining
}
pub(crate) async fn validate_empty(&mut self) -> io::Result<()> {
if self.remaining == Some(0) {
while let Some(frame) =
std::future::poll_fn(|cx| Pin::new(&mut *self).poll_frame(cx)).await
{
frame?;
}
}
Ok(())
}
}
impl Body for RequestBody {
type Data = Bytes;
type Error = io::Error;
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Bytes>, io::Error>>> {
loop {
let Some(segment) = self.segments.front_mut() else {
return Poll::Ready(None);
};
let next = match segment {
Segment::Bytes(bytes) => {
let bytes = bytes.clone();
self.segments.pop_front();
if bytes.is_empty() {
continue;
}
Ok(bytes)
}
Segment::Reader {
reader,
remaining,
tail,
} => {
let mut buffer = [0u8; 16 * 1024];
let capacity = remaining.map_or(buffer.len(), |n| {
usize::try_from(n.saturating_add(1))
.map_or(buffer.len(), |n| n.min(buffer.len()))
});
let mut read = ReadBuf::new(&mut buffer[..capacity]);
match Pin::new(&mut *reader).poll_read(cx, &mut read) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(error)) => Err(error),
Poll::Ready(Ok(())) => {
let data = read.filled();
if remaining.is_some_and(|n| (data.len() as u64) > n) {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"upload reader exceeds its declared length",
))
} else if data.is_empty() {
if remaining.is_some_and(|n| n > 0) {
Err(io::Error::new(
io::ErrorKind::UnexpectedEof,
"upload reader is shorter than its declared length",
))
} else {
let last = tail.take();
self.segments.pop_front();
if let Some(last) = last {
Ok(last)
} else {
continue;
}
}
} else {
if let Some(n) = remaining {
*n -= data.len() as u64;
}
let bytes = Bytes::copy_from_slice(data);
if *remaining == Some(0) {
*tail = Some(bytes);
continue;
}
Ok(bytes)
}
}
}
}
};
match next {
Ok(bytes) => {
if let Some(remaining) = &mut self.remaining {
*remaining -= bytes.len() as u64;
}
return Poll::Ready(Some(Ok(Frame::data(bytes))));
}
Err(error) => {
self.segments.clear();
return Poll::Ready(Some(Err(error)));
}
}
}
}
fn is_end_stream(&self) -> bool {
self.segments.is_empty()
}
fn size_hint(&self) -> SizeHint {
let mut hint = SizeHint::new();
if let Some(length) = self.remaining {
hint.set_exact(length);
}
hint
}
}