-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add mime support (form api is deprecated according to libcurl) #537
Open
tsnoam
wants to merge
4
commits into
alexcrichton:main
Choose a base branch
from
tsnoam:mimepart
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+216
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM ubuntu:16.04 | ||
FROM ubuntu:18.04 | ||
|
||
RUN apt-get update | ||
RUN apt-get install -y --no-install-recommends \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,4 @@ zlib-ng-compat = ["libz-sys/zlib-ng", "static-curl"] | |
upkeep_7_62_0 = [] | ||
poll_7_68_0 = [] | ||
ntlm = [] | ||
mime = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
use crate::easy::Easy2; | ||
use crate::error::Error; | ||
use curl_sys::{ | ||
curl_mime, curl_mime_addpart, curl_mime_data, curl_mime_filename, curl_mime_free, | ||
curl_mime_init, curl_mime_name, curl_mime_type, curl_mimepart, CURLcode, CURL, CURLE_OK, | ||
}; | ||
use std::ffi::CString; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug)] | ||
pub(super) struct MimeHandle(pub *mut curl_mime); | ||
|
||
impl MimeHandle { | ||
fn new(easy: *mut CURL) -> Self { | ||
let handle = unsafe { curl_mime_init(easy) }; | ||
assert!(!handle.is_null()); | ||
|
||
Self(handle) | ||
} | ||
} | ||
|
||
impl Drop for MimeHandle { | ||
fn drop(&mut self) { | ||
unsafe { curl_mime_free(self.0) } | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Mime<'e, E> { | ||
pub(super) handle: MimeHandle, | ||
easy: &'e mut Easy2<E>, | ||
} | ||
|
||
impl<'a, T> Mime<'a, T> { | ||
/// Create a mime handle | ||
pub(super) fn new(easy: &'a mut Easy2<T>) -> Self { | ||
let handle = MimeHandle::new(easy.raw()); | ||
|
||
Self { handle, easy } | ||
} | ||
|
||
/// Finalize creation of a mime post. | ||
pub fn post(self) -> Result<(), Error> { | ||
// We give ownership on `MimeHandle` to `Easy2`. `Easy2` will keep record of this object | ||
// until it is safe to free (drop) it. | ||
self.easy.mimepost(self.handle) | ||
} | ||
|
||
/// Append a new empty part to a mime structure | ||
pub fn add_part(&mut self) -> MimePart<'a> { | ||
MimePart::new(self) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct MimePart<'a> { | ||
handle: *mut curl_mimepart, | ||
// attach to the lifetime of our [Mime] handle, but without taking ownership | ||
_lifetime: PhantomData<&'a ()>, | ||
} | ||
|
||
impl<'a> MimePart<'a> { | ||
fn new<E>(mime: &mut Mime<E>) -> Self { | ||
let handle = unsafe { curl_mime_addpart(mime.handle.0) }; | ||
assert!(!handle.is_null()); | ||
|
||
Self { | ||
handle, | ||
_lifetime: Default::default(), | ||
} | ||
} | ||
|
||
/// Set a mime part's body data | ||
pub fn set_data(self, data: impl AsRef<[u8]>) -> Result<Self, Error> { | ||
let data = data.as_ref(); | ||
let code = unsafe { curl_mime_data(self.handle, data.as_ptr() as *const _, data.len()) }; | ||
code_ok(code).map(|_| self) | ||
} | ||
|
||
/// Set a mime part's name | ||
/// | ||
/// # Panics | ||
/// If `name` contains nul bytes, panic will occur. | ||
pub fn set_name(self, name: &str) -> Result<Self, Error> { | ||
let data = CString::new(name).unwrap(); | ||
let code = unsafe { curl_mime_name(self.handle, data.as_ptr()) }; | ||
code_ok(code).map(|_| self) | ||
} | ||
|
||
/// Set a mime part's remote file name | ||
/// | ||
/// # Panics | ||
/// If `filename` contains nul bytes, panic will occur. | ||
pub fn set_filename(self, filename: &str) -> Result<Self, Error> { | ||
let data = CString::new(filename).unwrap(); | ||
let code = unsafe { curl_mime_filename(self.handle, data.as_ptr()) }; | ||
code_ok(code).map(|_| self) | ||
} | ||
|
||
/// Set a mime part's content type | ||
/// | ||
/// # Panics | ||
/// If `content_type` contains nul bytes, panic will occur. | ||
pub fn set_content_type(self, content_type: &str) -> Result<Self, Error> { | ||
let data = CString::new(content_type).unwrap(); | ||
let code = unsafe { curl_mime_type(self.handle, data.as_ptr()) }; | ||
code_ok(code).map(|_| self) | ||
} | ||
} | ||
|
||
fn code_ok(code: CURLcode) -> Result<(), Error> { | ||
if code == CURLE_OK { | ||
Ok(()) | ||
} else { | ||
Err(Error::new(code)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::easy::Easy; | ||
|
||
/// Trivial test which checks that objects can be used as planned. | ||
#[test] | ||
fn test_ownership() { | ||
let mut easy = Easy::new(); | ||
let mut mime = easy.add_mime(); | ||
|
||
for i in 1..5 { | ||
let name = format!("name{i}"); | ||
let data = format!("data{i}"); | ||
let fname = format!("fname{i}"); | ||
|
||
mime.add_part() | ||
.set_data(name) | ||
.unwrap() | ||
.set_data(data) | ||
.unwrap() | ||
.set_filename(&fname) | ||
.unwrap() | ||
.set_content_type("plain/text") | ||
.unwrap(); | ||
} | ||
|
||
mime.post().unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mime
is kinda its own API that isn't specific to the easy API, so I would makemime
a top-level module.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see what you're saying about "kinda its own API".
However, mime API doesn't have a meaning without the easy API, quite similar to the form API. So basically, I've put the mime module in the same level as the form module.
I don't mind extracting it, just thought it is worth double checking this before doing so.
Your call though, I'll do what you ask :)