1use std::ffi::CString;
2use std::fmt;
3use std::path::Path;
4use std::ptr;
5
6use crate::easy::{list, List};
7use crate::FormError;
8
9pub struct Form {
14 head: *mut curl_sys::curl_httppost,
15 tail: *mut curl_sys::curl_httppost,
16 headers: Vec<List>,
17 buffers: Vec<Vec<u8>>,
18 strings: Vec<CString>,
19}
20
21pub struct Part<'form, 'data> {
23 form: &'form mut Form,
24 name: &'data str,
25 array: Vec<curl_sys::curl_forms>,
26 error: Option<FormError>,
27}
28
29pub fn raw(form: &Form) -> *mut curl_sys::curl_httppost {
30 form.head
31}
32
33impl Form {
34 pub fn new() -> Form {
36 Form {
37 head: ptr::null_mut(),
38 tail: ptr::null_mut(),
39 headers: Vec::new(),
40 buffers: Vec::new(),
41 strings: Vec::new(),
42 }
43 }
44
45 pub fn part<'a, 'data>(&'a mut self, name: &'data str) -> Part<'a, 'data> {
50 Part {
51 error: None,
52 form: self,
53 name,
54 array: vec![curl_sys::curl_forms {
55 option: curl_sys::CURLFORM_END,
56 value: ptr::null_mut(),
57 }],
58 }
59 }
60}
61
62impl fmt::Debug for Form {
63 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
64 f.debug_struct("Form").field("fields", &"...").finish()
66 }
67}
68
69impl Drop for Form {
70 fn drop(&mut self) {
71 unsafe {
72 curl_sys::curl_formfree(self.head);
73 }
74 }
75}
76
77impl<'form, 'data> Part<'form, 'data> {
78 pub fn contents(&mut self, contents: &'data [u8]) -> &mut Self {
80 let pos = self.array.len() - 1;
81
82 let ptr = if contents.is_empty() {
86 b"\x00"
87 } else {
88 contents
89 }
90 .as_ptr();
91
92 self.array.insert(
93 pos,
94 curl_sys::curl_forms {
95 option: curl_sys::CURLFORM_COPYCONTENTS,
96 value: ptr as *mut _,
97 },
98 );
99 self.array.insert(
100 pos + 1,
101 curl_sys::curl_forms {
102 option: curl_sys::CURLFORM_CONTENTSLENGTH,
103 value: contents.len() as *mut _,
104 },
105 );
106 self
107 }
108
109 pub fn file_content<P>(&mut self, file: P) -> &mut Self
120 where
121 P: AsRef<Path>,
122 {
123 self._file_content(file.as_ref())
124 }
125
126 fn _file_content(&mut self, file: &Path) -> &mut Self {
127 if let Some(bytes) = self.path2cstr(file) {
128 let pos = self.array.len() - 1;
129 self.array.insert(
130 pos,
131 curl_sys::curl_forms {
132 option: curl_sys::CURLFORM_FILECONTENT,
133 value: bytes.as_ptr() as *mut _,
134 },
135 );
136 self.form.strings.push(bytes);
137 }
138 self
139 }
140
141 pub fn file<P: ?Sized>(&mut self, file: &'data P) -> &mut Self
162 where
163 P: AsRef<Path>,
164 {
165 self._file(file.as_ref())
166 }
167
168 fn _file(&mut self, file: &'data Path) -> &mut Self {
169 if let Some(bytes) = self.path2cstr(file) {
170 let pos = self.array.len() - 1;
171 self.array.insert(
172 pos,
173 curl_sys::curl_forms {
174 option: curl_sys::CURLFORM_FILE,
175 value: bytes.as_ptr() as *mut _,
176 },
177 );
178 self.form.strings.push(bytes);
179 }
180 self
181 }
182
183 pub fn content_type(&mut self, content_type: &'data str) -> &mut Self {
191 if let Some(bytes) = self.bytes2cstr(content_type.as_bytes()) {
192 let pos = self.array.len() - 1;
193 self.array.insert(
194 pos,
195 curl_sys::curl_forms {
196 option: curl_sys::CURLFORM_CONTENTTYPE,
197 value: bytes.as_ptr() as *mut _,
198 },
199 );
200 self.form.strings.push(bytes);
201 }
202 self
203 }
204
205 pub fn filename<P: ?Sized>(&mut self, name: &'data P) -> &mut Self
214 where
215 P: AsRef<Path>,
216 {
217 self._filename(name.as_ref())
218 }
219
220 fn _filename(&mut self, name: &'data Path) -> &mut Self {
221 if let Some(bytes) = self.path2cstr(name) {
222 let pos = self.array.len() - 1;
223 self.array.insert(
224 pos,
225 curl_sys::curl_forms {
226 option: curl_sys::CURLFORM_FILENAME,
227 value: bytes.as_ptr() as *mut _,
228 },
229 );
230 self.form.strings.push(bytes);
231 }
232 self
233 }
234
235 pub fn buffer<P: ?Sized>(&mut self, name: &'data P, data: Vec<u8>) -> &mut Self
247 where
248 P: AsRef<Path>,
249 {
250 self._buffer(name.as_ref(), data)
251 }
252
253 fn _buffer(&mut self, name: &'data Path, mut data: Vec<u8>) -> &mut Self {
254 if let Some(bytes) = self.path2cstr(name) {
255 let length = data.len();
259 if length == 0 {
260 data.push(0);
261 }
262
263 let pos = self.array.len() - 1;
264 self.array.insert(
265 pos,
266 curl_sys::curl_forms {
267 option: curl_sys::CURLFORM_BUFFER,
268 value: bytes.as_ptr() as *mut _,
269 },
270 );
271 self.form.strings.push(bytes);
272 self.array.insert(
273 pos + 1,
274 curl_sys::curl_forms {
275 option: curl_sys::CURLFORM_BUFFERPTR,
276 value: data.as_ptr() as *mut _,
277 },
278 );
279 self.array.insert(
280 pos + 2,
281 curl_sys::curl_forms {
282 option: curl_sys::CURLFORM_BUFFERLENGTH,
283 value: length as *mut _,
284 },
285 );
286 self.form.buffers.push(data);
287 }
288 self
289 }
290
291 pub fn content_header(&mut self, headers: List) -> &mut Self {
295 let pos = self.array.len() - 1;
296 self.array.insert(
297 pos,
298 curl_sys::curl_forms {
299 option: curl_sys::CURLFORM_CONTENTHEADER,
300 value: list::raw(&headers) as *mut _,
301 },
302 );
303 self.form.headers.push(headers);
304 self
305 }
306
307 pub fn add(&mut self) -> Result<(), FormError> {
312 if let Some(err) = self.error.clone() {
313 return Err(err);
314 }
315 let rc = unsafe {
316 curl_sys::curl_formadd(
317 &mut self.form.head,
318 &mut self.form.tail,
319 curl_sys::CURLFORM_COPYNAME,
320 self.name.as_ptr(),
321 curl_sys::CURLFORM_NAMELENGTH,
322 self.name.len(),
323 curl_sys::CURLFORM_ARRAY,
324 self.array.as_ptr(),
325 curl_sys::CURLFORM_END,
326 )
327 };
328 if rc == curl_sys::CURL_FORMADD_OK {
329 Ok(())
330 } else {
331 Err(FormError::new(rc))
332 }
333 }
334
335 #[cfg(unix)]
336 fn path2cstr(&mut self, p: &Path) -> Option<CString> {
337 use std::os::unix::prelude::*;
338 self.bytes2cstr(p.as_os_str().as_bytes())
339 }
340
341 #[cfg(windows)]
342 fn path2cstr(&mut self, p: &Path) -> Option<CString> {
343 match p.to_str() {
344 Some(bytes) => self.bytes2cstr(bytes.as_bytes()),
345 None if self.error.is_none() => {
346 self.error = Some(FormError::new(curl_sys::CURL_FORMADD_INCOMPLETE));
348 None
349 }
350 None => None,
351 }
352 }
353
354 fn bytes2cstr(&mut self, bytes: &[u8]) -> Option<CString> {
355 match CString::new(bytes) {
356 Ok(c) => Some(c),
357 Err(..) if self.error.is_none() => {
358 self.error = Some(FormError::new(curl_sys::CURL_FORMADD_INCOMPLETE));
360 None
361 }
362 Err(..) => None,
363 }
364 }
365}
366
367impl<'form, 'data> fmt::Debug for Part<'form, 'data> {
368 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
369 f.debug_struct("Part")
371 .field("name", &self.name)
372 .field("form", &self.form)
373 .finish()
374 }
375}