Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/client/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ use std::{fmt, io};

use super::{HttpRequest, HttpStream};

macro_rules! try_lazy (
($field:expr, $try:expr) => (
match $try {
Ok(ok) => ok,
Err(e) => return Err(LazyError::with_field($field.into(), e)),
}
);
($try:expr) => (
match $try {
Ok(ok) => ok,
Err(e) => return Err(LazyError::without_field(e)),
}
)
);

/// A `LazyError` wrapping `std::io::Error`.
#[allow(clippy::module_name_repetitions)]
pub type LazyIoError<'a> = LazyError<'a, io::Error>;
Expand Down Expand Up @@ -199,9 +184,15 @@ impl<'n, 'd> Multipart<'n, 'd> {

req.apply_headers(prepared.boundary(), prepared.content_len());

let mut stream = try_lazy!(req.open_stream());
let mut stream = match req.open_stream() {
Ok(s) => s,
Err(e) => return Err(LazyError::without_field(e)),
};

try_lazy!(io::copy(&mut prepared, &mut stream));
match io::copy(&mut prepared, &mut stream) {
Ok(ok) => ok,
Err(e) => return Err(LazyError::without_field(e)),
};

stream.finish().map_err(LazyError::without_field)
}
Expand Down Expand Up @@ -379,8 +370,15 @@ impl<'d> PreparedField<'d> {
) -> Result<(Self, u64), LazyIoError<'n>> {
let (content_type, filename) = super::mime_filename(path);

let file = try_lazy!(name, File::open(path));
let content_len = try_lazy!(name, file.metadata()).len();
let file = match File::open(path) {
Ok(f) => f,
Err(e) => return Err(LazyError::with_field(name, e)),
};

let content_len = match file.metadata() {
Ok(m) => m.len(),
Err(e) => return Err(LazyError::with_field(name, e)),
};

let stream = Self::from_stream(&name, boundary, &content_type, filename, Box::new(file));

Expand Down
36 changes: 15 additions & 21 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@ pub use self::sized::SizedRequest;

const BOUNDARY_LEN: usize = 16;

macro_rules! map_self {
($selff:expr, $try:expr) => {
match $try {
Ok(()) => Ok($selff),
Err(err) => Err(err.into()),
}
};
}

/// The entry point of the client-side multipart API.
///
/// Though they perform I/O, the `.write_*()` methods do not return `io::Result<_>` in order to
Expand Down Expand Up @@ -65,7 +56,10 @@ impl<S: HttpStream> Multipart<S> {
name: N,
val: V,
) -> Result<&mut Self, S::Error> {
map_self!(self, self.writer.write_text(name.as_ref(), val.as_ref()))
match self.writer.write_text(name.as_ref(), val.as_ref()) {
Err(err) => Err(err.into()),
Ok(()) => Ok(self),
}
}

/// Open a file pointed to by `path` and write its contents to the multipart request,
Expand All @@ -84,10 +78,10 @@ impl<S: HttpStream> Multipart<S> {
name: N,
path: P,
) -> Result<&mut Self, S::Error> {
let name = name.as_ref();
let path = path.as_ref();

map_self!(self, self.writer.write_file(name, path))
match self.writer.write_file(name.as_ref(), path.as_ref()) {
Err(err) => Err(err.into()),
Ok(()) => Ok(self),
}
}

/// Write a byte stream to the multipart request as a file field, supplying `filename` if given,
Expand Down Expand Up @@ -117,13 +111,13 @@ impl<S: HttpStream> Multipart<S> {
filename: Option<&str>,
content_type: Option<Mime>,
) -> Result<&mut Self, S::Error> {
let name = name.as_ref();

map_self!(
self,
self.writer
.write_stream(stream, name, filename, content_type)
)
match self
.writer
.write_stream(stream, name.as_ref(), filename, content_type)
{
Err(err) => Err(err.into()),
Ok(()) => Ok(self),
}
}

/// Finalize the request and return the response from the server, or the last error if set.
Expand Down
34 changes: 16 additions & 18 deletions src/server/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ use thiserror::Error;

const EMPTY_STR_HEADER: StrHeader<'static> = StrHeader { name: "", val: "" };

macro_rules! invalid_cont_disp {
($reason: expr, $cause: expr) => {
return Err(ParseHeaderError::InvalidContDisp(
$reason,
$cause.to_string(),
))
};
}

/// Not exposed
#[derive(Copy, Clone, Debug)]
pub struct StrHeader<'a> {
Expand Down Expand Up @@ -156,22 +147,29 @@ impl ContentDisp {
// assert Content-Disposition: form-data
// but needs to be parsed out to trim the spaces (allowed by spec IIRC)
if disp_type.trim() != "form-data" {
invalid_cont_disp!("unexpected Content-Disposition value", disp_type);
return Err(ParseHeaderError::InvalidContDisp(
"unexpected Content-Disposition value",
disp_type.to_string(),
));
}
after_disp_type
}
None => invalid_cont_disp!(
"expected additional data after Content-Disposition type",
header.val
),
None => {
return Err(ParseHeaderError::InvalidContDisp(
"expected additional data after Content-Disposition type",
header.val.to_string(),
))
}
};

// Content-Disposition: form-data; name=?
let (field_name, filename) = match get_str_after("name=", ';', after_disp_type) {
None => invalid_cont_disp!(
"expected field name and maybe filename, got",
after_disp_type
),
None => {
return Err(ParseHeaderError::InvalidContDisp(
"expected field name and maybe filename, got",
after_disp_type.to_string(),
))
}
// Content-Disposition: form-data; name={field_name}; filename=?
Some((field_name, after_field_name)) => {
let field_name = trim_quotes(field_name);
Expand Down
74 changes: 36 additions & 38 deletions src/server/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,38 +1082,37 @@ fn try_read_buf<R: BufRead, Wb: FnMut(&[u8]) -> SaveResult<usize, usize>>(
) -> SaveResult<u64, u64> {
let mut total_copied = 0u64;

macro_rules! try_here (
($try:expr) => (
match $try {
Ok(val) => val,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return if total_copied == 0 { Error(e) }
else { Partial(total_copied, e.into()) },
}
)
);

loop {
let res = {
let buf = try_here!(src.fill_buf());
if buf.is_empty() {
break;
let buf = match src.fill_buf() {
Ok(val) => val,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => {
return if total_copied == 0 {
SaveResult::Error(e)
} else {
SaveResult::Partial(total_copied, e.into())
}
}
with_buf(buf)
};

if buf.is_empty() {
break;
}

let res = with_buf(buf);

match res {
Full(copied) => {
SaveResult::Full(copied) => {
src.consume(copied);
total_copied += copied as u64;
}
Partial(copied, reason) => {
SaveResult::Partial(copied, reason) => {
src.consume(copied);
total_copied += copied as u64;
return Partial(total_copied, reason);
return SaveResult::Partial(total_copied, reason);
}
Error(err) => {
return Partial(total_copied, err.into());
SaveResult::Error(err) => {
return SaveResult::Partial(total_copied, err.into());
}
}
}
Expand All @@ -1124,27 +1123,26 @@ fn try_read_buf<R: BufRead, Wb: FnMut(&[u8]) -> SaveResult<usize, usize>>(
fn try_write_all<W: Write>(mut buf: &[u8], mut dest: W) -> SaveResult<usize, usize> {
let mut total_copied = 0;

macro_rules! try_here (
($try:expr) => (
match $try {
Ok(val) => val,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return if total_copied == 0 { Error(e) }
else { Partial(total_copied, e.into()) },
}
)
);

while !buf.is_empty() {
match try_here!(dest.write(buf)) {
0 => try_here!(Err(io::Error::new(
io::ErrorKind::WriteZero,
"failed to write whole buffer"
))),
copied => {
match dest.write(buf) {
Ok(0) => {
return SaveResult::Error(io::Error::new(
io::ErrorKind::WriteZero,
"failed to write whole buffer",
))
}
Ok(copied) => {
buf = &buf[copied..];
total_copied += copied;
}
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => {
return if total_copied == 0 {
SaveResult::Error(e)
} else {
SaveResult::Partial(total_copied, e.into())
}
}
}
}

Expand Down