ENH: import of new curl version
[cmake.git] / Utilities / cmcurl-7.19.0 / tests / libtest / lib510.c
blob2a4ec2a55949b083afc47982798c9acf160d4c0f
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib510.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 #include "test.h"
13 static const char *post[]={
14 "one",
15 "two",
16 "three",
17 "and a final longer crap: four",
18 NULL
22 struct WriteThis {
23 int counter;
26 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
28 struct WriteThis *pooh = (struct WriteThis *)userp;
29 const char *data;
31 if(size*nmemb < 1)
32 return 0;
34 data = post[pooh->counter];
36 if(data) {
37 size_t len = strlen(data);
38 memcpy(ptr, data, len);
39 pooh->counter++; /* advance pointer */
40 return len;
42 return 0; /* no more data left to deliver */
45 int test(char *URL)
47 CURL *curl;
48 CURLcode res=CURLE_OK;
49 struct curl_slist *slist = NULL;
51 struct WriteThis pooh;
52 pooh.counter = 0;
54 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
55 fprintf(stderr, "curl_global_init() failed\n");
56 return TEST_ERR_MAJOR_BAD;
59 if ((curl = curl_easy_init()) == NULL) {
60 fprintf(stderr, "curl_easy_init() failed\n");
61 curl_global_cleanup();
62 return TEST_ERR_MAJOR_BAD;
65 slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
66 if (slist == NULL) {
67 fprintf(stderr, "curl_slist_append() failed\n");
68 curl_easy_cleanup(curl);
69 curl_global_cleanup();
70 return TEST_ERR_MAJOR_BAD;
73 /* First set the URL that is about to receive our POST. */
74 curl_easy_setopt(curl, CURLOPT_URL, URL);
76 /* Now specify we want to POST data */
77 curl_easy_setopt(curl, CURLOPT_POST, 1L);
79 /* we want to use our own read function */
80 curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
82 /* pointer to pass to our read function */
83 curl_easy_setopt(curl, CURLOPT_INFILE, &pooh);
85 /* get verbose debug output please */
86 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
88 /* include headers in the output */
89 curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
91 /* enforce chunked transfer by setting the header */
92 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
94 /* Perform the request, res will get the return code */
95 res = curl_easy_perform(curl);
97 /* clean up the headers list */
98 if(slist)
99 curl_slist_free_all(slist);
101 /* always cleanup */
102 curl_easy_cleanup(curl);
103 curl_global_cleanup();
105 return res;