上篇博文讲到了如何使用multicurl来进行http并发访问,今天继续有关curl的主题,来八一八如何使用curl来上传文件,在介绍具体方法之前了解下目前http文件上传的基本实现。
rfc1867描述了如何使用http协议来上传客户端文件,目前基本上所有的浏览器和web服务器都支持http文件上传,它的使用也十分的简单,具体的来说就是在页面上创建一个form表单,表单的enctype属性为multipart/form-data,action为接收上传文件的cgi url,请求方式为post,在表单中添加type属性为file的input,file input里面选择需要上传的文件,选择好后点击submit,服务器端收到multipart post请求后,会根据相关协议解析请求,然后保存上传的文件内容,Multipart表单示例:
[html] view plaincopy
- <form enctype="multipart/form-data" action="http://host:port/UploadFile" method=post>
- Upload :<br>
- <input name="userfile" type="file"><br>
- text field :<input type="text" name="text" value="text"><br>
- <input type="submit" value="提交"><input type=reset>
- </form>
好了,现在来讲一讲curl的文件上传,对于curl来讲,其实它要完成的任务就是构建一个multipart/formdata HTTP POST请求。类似于往multipart form表单中添加type为file或者text的input item一样,curl也需要我们构造表单中的input item,curl_formadd函数可以帮助我们完成这个任务,它即可以添加普通的name-value section,也可以添加file upload section,下面举几个具体例子:
1、添加name/content section
[cpp] view plaincopy
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
2、添加name/content/contenttype section
[cpp] view plaincopy
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "name", CURLFORM_COPYCONTENTS, "content", CURLFORM_CONTENTTYPE, "type", CURLFORM_END);
3、添加 file/filename section
[cpp] view plaincopy
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic", CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic", CURLFORM_END);
4、添加file/contenttype section
[cpp] view plaincopy
- curl_formadd(&post, &last, CURLFORM_COPYNAME, "pic", CURLFORM_FILE, "demo.jpg", CURLFORM_FILENAME, "upload.pic", CURLFORM_CONTENTTYPE, "image/jpeg", CURLFORM_END);
上面的post 和 last都是指向curl_httppost对象的指针, post指向的就是一个由所有section组成的链表的开端,last是该链表的尾指针。当我们添加完所有的form section之后,使用curl_easy_setopt(curl, CURLOPT_HTTPPOST,post)函数设置curl的http post,最后就是调用curl_easy_perform执行请求。需要注意的是,当使用libcurl的POST方式时,如果POST数据的大小大于1024个字节,libcurl不会直接发送POST请求,而是会分为两步执行请求:
1、发送一个请求,该请求头部包含一个Expect: 100-continue的字段,用来询问server是否愿意接受数据
2、当接收到从server返回的100-continue的应答后,它才会真正的发起POST请求,将数据发送给server。
对于文件上传来说,文件大小往往会超过1024个字节,所以如果你确认你的服务器不会拒绝你的文件上传请求的话,可以禁止curl的Expect请求头,具体方法可以去看看我的另外一篇文章《libcurl的使用问题“Expect100-continue” 》。
最后附上curl官网上提供的文件上传例子:
[cpp] view plaincopy
- /* This is an example application source code using the multi interface
- * to do a multipart formpost without "blocking". */
- #include <stdio.h>
- #include <string.h>
- #include <sys/time.h>
- #include <curl/curl.h>
- int main(void)
- {
- CURL *curl;
- CURLM *multi_handle;
- int still_running;
- struct curl_httppost *formpost=NULL;
- struct curl_httppost *lastptr=NULL;
- struct curl_slist *headerlist=NULL;
- static const char buf[] = "Expect:";
- /* Fill in the file upload field. This makes libcurl load data from
- the given file name when curl_easy_perform() is called. */
- curl_formadd(&formpost,
- &lastptr,
- CURLFORM_COPYNAME, "sendfile",
- CURLFORM_FILE, "postit2.c",
- CURLFORM_END);
- /* Fill in the filename field */
- curl_formadd(&formpost,
- &lastptr,
- CURLFORM_COPYNAME, "filename",
- CURLFORM_COPYCONTENTS, "postit2.c",
- CURLFORM_END);
- /* Fill in the submit field too, even if this is rarely needed */
- curl_formadd(&formpost,
- &lastptr,
- CURLFORM_COPYNAME, "submit",
- CURLFORM_COPYCONTENTS, "send",
- CURLFORM_END);
- curl = curl_easy_init();
- multi_handle = curl_multi_init();
- /* initalize custom header list (stating that Expect: 100-continue is not
- wanted */
- headerlist = curl_slist_append(headerlist, buf);
- if(curl && multi_handle) {
- /* what URL that receives this POST */
- curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/upload.cgi");
- curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
- curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
- curl_multi_add_handle(multi_handle, curl);
- curl_multi_perform(multi_handle, &still_running);
- do {
- struct timeval timeout;
- int rc; /* select() return code */
- fd_set fdread;
- fd_set fdwrite;
- fd_set fdexcep;
- int maxfd = -1;
- long curl_timeo = -1;
- FD_ZERO(&fdread);
- FD_ZERO(&fdwrite);
- FD_ZERO(&fdexcep);
- /* set a suitable timeout to play around with */
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- curl_multi_timeout(multi_handle, &curl_timeo);
- if(curl_timeo >= 0) {
- timeout.tv_sec = curl_timeo / 1000;
- if(timeout.tv_sec > 1)
- timeout.tv_sec = 1;
- else
- timeout.tv_usec = (curl_timeo % 1000) * 1000;
- }
- /* get file descriptors from the transfers */
- curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
- /* In a real-world program you OF COURSE check the return code of the
- function calls. On success, the value of maxfd is guaranteed to be
- greater or equal than -1. We call select(maxfd + 1, ...), specially in
- case of (maxfd == -1), we call select(0, ...), which is basically equal
- to sleep. */
- rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
- switch(rc) {
- case -1:
- /* select error */
- break;
- case 0:
- default:
- /* timeout or readable/writable sockets */
- printf("perform!\n");
- curl_multi_perform(multi_handle, &still_running);
- printf("running: %d!\n", still_running);
- break;
- }
- } while(still_running);
- curl_multi_cleanup(multi_handle);
- /* always cleanup */
- curl_easy_cleanup(curl);
- /* then cleanup the formpost chain */
- curl_formfree(formpost);
- /* free slist */
- curl_slist_free_all (headerlist);
- }
- return 0;
- }