use std::ffi::CString;
use std::fmt;
use std::path::Path;
use curl_sys;
use easy::{list, List};
use FormError;
pub struct Form {
head: *mut curl_sys::curl_httppost,
tail: *mut curl_sys::curl_httppost,
headers: Vec<List>,
buffers: Vec<Vec<u8>>,
strings: Vec<CString>,
}
pub struct Part<'form, 'data> {
form: &'form mut Form,
name: &'data str,
array: Vec<curl_sys::curl_forms>,
error: Option<FormError>,
}
pub fn raw(form: &Form) -> *mut curl_sys::curl_httppost {
form.head
}
impl Form {
pub fn new() -> Form {
Form {
head: 0 as *mut _,
tail: 0 as *mut _,
headers: Vec::new(),
buffers: Vec::new(),
strings: Vec::new(),
}
}
pub fn part<'a, 'data>(&'a mut self, name: &'data str) -> Part<'a, 'data> {
Part {
error: None,
form: self,
name: name,
array: vec![curl_sys::curl_forms {
option: curl_sys::CURLFORM_END,
value: 0 as *mut _,
}],
}
}
}
impl fmt::Debug for Form {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Form").field("fields", &"...").finish()
}
}
impl Drop for Form {
fn drop(&mut self) {
unsafe {
curl_sys::curl_formfree(self.head);
}
}
}
impl<'form, 'data> Part<'form, 'data> {
pub fn contents(&mut self, contents: &'data [u8]) -> &mut Self {
let pos = self.array.len() - 1;
let ptr = if contents.is_empty() {
b"\x00"
} else {
contents
}
.as_ptr();
self.array.insert(
pos,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_COPYCONTENTS,
value: ptr as *mut _,
},
);
self.array.insert(
pos + 1,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_CONTENTSLENGTH,
value: contents.len() as *mut _,
},
);
self
}
pub fn file_content<P>(&mut self, file: P) -> &mut Self
where
P: AsRef<Path>,
{
self._file_content(file.as_ref())
}
fn _file_content(&mut self, file: &Path) -> &mut Self {
if let Some(bytes) = self.path2cstr(file) {
let pos = self.array.len() - 1;
self.array.insert(
pos,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_FILECONTENT,
value: bytes.as_ptr() as *mut _,
},
);
self.form.strings.push(bytes);
}
self
}
pub fn file<P: ?Sized>(&mut self, file: &'data P) -> &mut Self
where
P: AsRef<Path>,
{
self._file(file.as_ref())
}
fn _file(&mut self, file: &'data Path) -> &mut Self {
if let Some(bytes) = self.path2cstr(file) {
let pos = self.array.len() - 1;
self.array.insert(
pos,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_FILE,
value: bytes.as_ptr() as *mut _,
},
);
self.form.strings.push(bytes);
}
self
}
pub fn content_type(&mut self, content_type: &'data str) -> &mut Self {
if let Some(bytes) = self.bytes2cstr(content_type.as_bytes()) {
let pos = self.array.len() - 1;
self.array.insert(
pos,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_CONTENTTYPE,
value: bytes.as_ptr() as *mut _,
},
);
self.form.strings.push(bytes);
}
self
}
pub fn filename<P: ?Sized>(&mut self, name: &'data P) -> &mut Self
where
P: AsRef<Path>,
{
self._filename(name.as_ref())
}
fn _filename(&mut self, name: &'data Path) -> &mut Self {
if let Some(bytes) = self.path2cstr(name) {
let pos = self.array.len() - 1;
self.array.insert(
pos,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_FILENAME,
value: bytes.as_ptr() as *mut _,
},
);
self.form.strings.push(bytes);
}
self
}
pub fn buffer<P: ?Sized>(&mut self, name: &'data P, data: Vec<u8>) -> &mut Self
where
P: AsRef<Path>,
{
self._buffer(name.as_ref(), data)
}
fn _buffer(&mut self, name: &'data Path, mut data: Vec<u8>) -> &mut Self {
if let Some(bytes) = self.path2cstr(name) {
let length = data.len();
if length == 0 {
data.push(0);
}
let pos = self.array.len() - 1;
self.array.insert(
pos,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_BUFFER,
value: bytes.as_ptr() as *mut _,
},
);
self.form.strings.push(bytes);
self.array.insert(
pos + 1,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_BUFFERPTR,
value: data.as_ptr() as *mut _,
},
);
self.array.insert(
pos + 2,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_BUFFERLENGTH,
value: length as *mut _,
},
);
self.form.buffers.push(data);
}
self
}
pub fn content_header(&mut self, headers: List) -> &mut Self {
let pos = self.array.len() - 1;
self.array.insert(
pos,
curl_sys::curl_forms {
option: curl_sys::CURLFORM_CONTENTHEADER,
value: list::raw(&headers) as *mut _,
},
);
self.form.headers.push(headers);
self
}
pub fn add(&mut self) -> Result<(), FormError> {
if let Some(err) = self.error.clone() {
return Err(err);
}
let rc = unsafe {
curl_sys::curl_formadd(
&mut self.form.head,
&mut self.form.tail,
curl_sys::CURLFORM_COPYNAME,
self.name.as_ptr(),
curl_sys::CURLFORM_NAMELENGTH,
self.name.len(),
curl_sys::CURLFORM_ARRAY,
self.array.as_ptr(),
curl_sys::CURLFORM_END,
)
};
if rc == curl_sys::CURL_FORMADD_OK {
Ok(())
} else {
Err(FormError::new(rc))
}
}
#[cfg(unix)]
fn path2cstr(&mut self, p: &Path) -> Option<CString> {
use std::os::unix::prelude::*;
self.bytes2cstr(p.as_os_str().as_bytes())
}
#[cfg(windows)]
fn path2cstr(&mut self, p: &Path) -> Option<CString> {
match p.to_str() {
Some(bytes) => self.bytes2cstr(bytes.as_bytes()),
None if self.error.is_none() => {
self.error = Some(FormError::new(curl_sys::CURL_FORMADD_INCOMPLETE));
None
}
None => None,
}
}
fn bytes2cstr(&mut self, bytes: &[u8]) -> Option<CString> {
match CString::new(bytes) {
Ok(c) => Some(c),
Err(..) if self.error.is_none() => {
self.error = Some(FormError::new(curl_sys::CURL_FORMADD_INCOMPLETE));
None
}
Err(..) => None,
}
}
}
impl<'form, 'data> fmt::Debug for Part<'form, 'data> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Part")
.field("name", &self.name)
.field("form", &self.form)
.finish()
}
}