Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib554.c
blobb985f5710f3d8409146b80f2b5e77d3f9f1674d6
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib554.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 #include "test.h"
13 static char data[]="this is what we post to the silly web server\n";
15 struct WriteThis {
16 char *readptr;
17 size_t sizeleft;
20 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
22 struct WriteThis *pooh = (struct WriteThis *)userp;
24 if(size*nmemb < 1)
25 return 0;
27 if(pooh->sizeleft) {
28 *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
29 pooh->readptr++; /* advance pointer */
30 pooh->sizeleft--; /* less data left */
31 return 1; /* we return 1 byte at a time! */
34 return 0; /* no more data left to deliver */
37 int test(char *URL)
39 CURL *curl;
40 CURLcode res=CURLE_OK;
41 CURLFORMcode formrc;
43 struct curl_httppost *formpost=NULL;
44 struct curl_httppost *lastptr=NULL;
45 struct WriteThis pooh;
47 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
48 fprintf(stderr, "curl_global_init() failed\n");
49 return TEST_ERR_MAJOR_BAD;
52 pooh.readptr = data;
53 pooh.sizeleft = strlen(data);
55 /* Fill in the file upload field */
56 formrc = curl_formadd(&formpost,
57 &lastptr,
58 CURLFORM_COPYNAME, "sendfile",
59 CURLFORM_STREAM, &pooh,
60 CURLFORM_CONTENTSLENGTH, pooh.sizeleft,
61 CURLFORM_FILENAME, "postit2.c",
62 CURLFORM_END);
64 if(formrc)
65 printf("curl_formadd(1) = %d\n", (int)formrc);
67 /* Fill in the filename field */
68 formrc = curl_formadd(&formpost,
69 &lastptr,
70 CURLFORM_COPYNAME, "filename",
71 CURLFORM_COPYCONTENTS, "postit2.c",
72 CURLFORM_END);
74 if(formrc)
75 printf("curl_formadd(2) = %d\n", (int)formrc);
77 /* Fill in a submit field too */
78 formrc = curl_formadd(&formpost,
79 &lastptr,
80 CURLFORM_COPYNAME, "submit",
81 CURLFORM_COPYCONTENTS, "send",
82 CURLFORM_END);
84 if(formrc)
85 printf("curl_formadd(3) = %d\n", (int)formrc);
87 if ((curl = curl_easy_init()) == NULL) {
88 fprintf(stderr, "curl_easy_init() failed\n");
89 curl_formfree(formpost);
90 curl_global_cleanup();
91 return TEST_ERR_MAJOR_BAD;
94 /* First set the URL that is about to receive our POST. */
95 curl_easy_setopt(curl, CURLOPT_URL, URL);
97 /* Now specify we want to POST data */
98 curl_easy_setopt(curl, CURLOPT_POST, 1L);
100 /* Set the expected POST size */
101 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
103 /* we want to use our own read function */
104 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
106 /* pointer to pass to our read function */
107 curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);
109 /* send a multi-part formpost */
110 curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
112 /* get verbose debug output please */
113 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
115 /* include headers in the output */
116 curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
118 /* Perform the request, res will get the return code */
119 res = curl_easy_perform(curl);
121 /* always cleanup */
122 curl_easy_cleanup(curl);
123 curl_global_cleanup();
125 /* now cleanup the formpost chain */
126 curl_formfree(formpost);
128 return res;