1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22 #include "test.h"
23
24 #include "memdebug.h"
25
26 static char data[]=
27 #ifdef CURL_DOES_CONVERSIONS
28 /* ASCII representation with escape sequences for non-ASCII platforms */
29 "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
30 "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
31 "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
32 #else
33 "this is what we post to the silly web server\n";
34 #endif
35
36 struct WriteThis {
37 char *readptr;
38 size_t sizeleft;
39 };
40
read_callback(void * ptr,size_t size,size_t nmemb,void * userp)41 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
42 {
43 #ifdef LIB587
44 (void)ptr;
45 (void)size;
46 (void)nmemb;
47 (void)userp;
48 return CURL_READFUNC_ABORT;
49 #else
50
51 struct WriteThis *pooh = (struct WriteThis *)userp;
52
53 if(size*nmemb < 1)
54 return 0;
55
56 if(pooh->sizeleft) {
57 *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
58 pooh->readptr++; /* advance pointer */
59 pooh->sizeleft--; /* less data left */
60 return 1; /* we return 1 byte at a time! */
61 }
62
63 return 0; /* no more data left to deliver */
64 #endif
65 }
66
test(char * URL)67 int test(char *URL)
68 {
69 CURL *curl;
70 CURLcode res=CURLE_OK;
71 CURLFORMcode formrc;
72
73 struct curl_httppost *formpost=NULL;
74 struct curl_httppost *lastptr=NULL;
75 struct WriteThis pooh;
76 struct WriteThis pooh2;
77
78 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
79 fprintf(stderr, "curl_global_init() failed\n");
80 return TEST_ERR_MAJOR_BAD;
81 }
82
83 pooh.readptr = data;
84 pooh.sizeleft = strlen(data);
85
86 /* Fill in the file upload field */
87 formrc = curl_formadd(&formpost,
88 &lastptr,
89 CURLFORM_COPYNAME, "sendfile",
90 CURLFORM_STREAM, &pooh,
91 CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
92 CURLFORM_FILENAME, "postit2.c",
93 CURLFORM_END);
94
95 if(formrc)
96 printf("curl_formadd(1) = %d\n", (int)formrc);
97
98 /* Now add the same data with another name and make it not look like
99 a file upload but still using the callback */
100
101 pooh2.readptr = data;
102 pooh2.sizeleft = strlen(data);
103
104 /* Fill in the file upload field */
105 formrc = curl_formadd(&formpost,
106 &lastptr,
107 CURLFORM_COPYNAME, "callbackdata",
108 CURLFORM_STREAM, &pooh2,
109 CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
110 CURLFORM_END);
111
112 if(formrc)
113 printf("curl_formadd(1) = %d\n", (int)formrc);
114
115 /* Fill in the filename field */
116 formrc = curl_formadd(&formpost,
117 &lastptr,
118 CURLFORM_COPYNAME, "filename",
119 #ifdef CURL_DOES_CONVERSIONS
120 /* ASCII representation with escape
121 sequences for non-ASCII platforms */
122 CURLFORM_COPYCONTENTS,
123 "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
124 #else
125 CURLFORM_COPYCONTENTS, "postit2.c",
126 #endif
127 CURLFORM_END);
128
129 if(formrc)
130 printf("curl_formadd(2) = %d\n", (int)formrc);
131
132 /* Fill in a submit field too */
133 formrc = curl_formadd(&formpost,
134 &lastptr,
135 CURLFORM_COPYNAME, "submit",
136 #ifdef CURL_DOES_CONVERSIONS
137 /* ASCII representation with escape
138 sequences for non-ASCII platforms */
139 CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
140 #else
141 CURLFORM_COPYCONTENTS, "send",
142 #endif
143 CURLFORM_END);
144
145 if(formrc)
146 printf("curl_formadd(3) = %d\n", (int)formrc);
147
148 formrc = curl_formadd(&formpost, &lastptr,
149 CURLFORM_COPYNAME, "somename",
150 CURLFORM_BUFFER, "somefile.txt",
151 CURLFORM_BUFFERPTR, "blah blah",
152 CURLFORM_BUFFERLENGTH, (long)9,
153 CURLFORM_END);
154
155 if(formrc)
156 printf("curl_formadd(4) = %d\n", (int)formrc);
157
158 if ((curl = curl_easy_init()) == NULL) {
159 fprintf(stderr, "curl_easy_init() failed\n");
160 curl_formfree(formpost);
161 curl_global_cleanup();
162 return TEST_ERR_MAJOR_BAD;
163 }
164
165 /* First set the URL that is about to receive our POST. */
166 test_setopt(curl, CURLOPT_URL, URL);
167
168 /* Now specify we want to POST data */
169 test_setopt(curl, CURLOPT_POST, 1L);
170
171 /* Set the expected POST size */
172 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
173
174 /* we want to use our own read function */
175 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
176
177 /* send a multi-part formpost */
178 test_setopt(curl, CURLOPT_HTTPPOST, formpost);
179
180 /* get verbose debug output please */
181 test_setopt(curl, CURLOPT_VERBOSE, 1L);
182
183 /* include headers in the output */
184 test_setopt(curl, CURLOPT_HEADER, 1L);
185
186 /* Perform the request, res will get the return code */
187 res = curl_easy_perform(curl);
188
189 test_cleanup:
190
191 /* always cleanup */
192 curl_easy_cleanup(curl);
193 curl_global_cleanup();
194
195 /* now cleanup the formpost chain */
196 curl_formfree(formpost);
197
198 return res;
199 }
200