Git init
[external/curl.git] / tests / libtest / lib554.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #include "memdebug.h"
13
14 static char data[]=
15 #ifdef CURL_DOES_CONVERSIONS
16   /* ASCII representation with escape sequences for non-ASCII platforms */
17   "\x74\x68\x69\x73\x20\x69\x73\x20\x77\x68\x61\x74\x20\x77\x65\x20\x70"
18   "\x6f\x73\x74\x20\x74\x6f\x20\x74\x68\x65\x20\x73\x69\x6c\x6c\x79\x20"
19   "\x77\x65\x62\x20\x73\x65\x72\x76\x65\x72\x0a";
20 #else
21   "this is what we post to the silly web server\n";
22 #endif
23
24 struct WriteThis {
25   char *readptr;
26   size_t sizeleft;
27 };
28
29 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
30 {
31   struct WriteThis *pooh = (struct WriteThis *)userp;
32
33   if(size*nmemb < 1)
34     return 0;
35
36   if(pooh->sizeleft) {
37     *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
38     pooh->readptr++;                 /* advance pointer */
39     pooh->sizeleft--;                /* less data left */
40     return 1;                        /* we return 1 byte at a time! */
41   }
42
43   return 0;                         /* no more data left to deliver */
44 }
45
46 int test(char *URL)
47 {
48   CURL *curl;
49   CURLcode res=CURLE_OK;
50   CURLFORMcode formrc;
51
52   struct curl_httppost *formpost=NULL;
53   struct curl_httppost *lastptr=NULL;
54   struct WriteThis pooh;
55
56   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
57     fprintf(stderr, "curl_global_init() failed\n");
58     return TEST_ERR_MAJOR_BAD;
59   }
60
61   pooh.readptr = data;
62   pooh.sizeleft = strlen(data);
63
64   /* Fill in the file upload field */
65   formrc = curl_formadd(&formpost,
66                         &lastptr,
67                         CURLFORM_COPYNAME, "sendfile",
68                         CURLFORM_STREAM, &pooh,
69                         CURLFORM_CONTENTSLENGTH, pooh.sizeleft,
70                         CURLFORM_FILENAME, "postit2.c",
71                         CURLFORM_END);
72
73   if(formrc)
74     printf("curl_formadd(1) = %d\n", (int)formrc);
75
76   /* Fill in the filename field */
77   formrc = curl_formadd(&formpost,
78                         &lastptr,
79                         CURLFORM_COPYNAME, "filename",
80 #ifdef CURL_DOES_CONVERSIONS
81                         /* ASCII representation with escape
82                            sequences for non-ASCII platforms */
83                         CURLFORM_COPYCONTENTS,
84                            "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
85 #else
86                         CURLFORM_COPYCONTENTS, "postit2.c",
87 #endif
88                         CURLFORM_END);
89
90   if(formrc)
91     printf("curl_formadd(2) = %d\n", (int)formrc);
92
93   /* Fill in a submit field too */
94   formrc = curl_formadd(&formpost,
95                         &lastptr,
96                         CURLFORM_COPYNAME, "submit",
97 #ifdef CURL_DOES_CONVERSIONS
98                         /* ASCII representation with escape
99                            sequences for non-ASCII platforms */
100                         CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
101 #else
102                         CURLFORM_COPYCONTENTS, "send",
103 #endif
104                         CURLFORM_END);
105
106   if(formrc)
107     printf("curl_formadd(3) = %d\n", (int)formrc);
108
109   if ((curl = curl_easy_init()) == NULL) {
110     fprintf(stderr, "curl_easy_init() failed\n");
111     curl_formfree(formpost);
112     curl_global_cleanup();
113     return TEST_ERR_MAJOR_BAD;
114   }
115
116   /* First set the URL that is about to receive our POST. */
117   test_setopt(curl, CURLOPT_URL, URL);
118
119   /* Now specify we want to POST data */
120   test_setopt(curl, CURLOPT_POST, 1L);
121
122   /* Set the expected POST size */
123   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
124
125   /* we want to use our own read function */
126   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
127
128   /* send a multi-part formpost */
129   test_setopt(curl, CURLOPT_HTTPPOST, formpost);
130
131   /* get verbose debug output please */
132   test_setopt(curl, CURLOPT_VERBOSE, 1L);
133
134   /* include headers in the output */
135   test_setopt(curl, CURLOPT_HEADER, 1L);
136
137   /* Perform the request, res will get the return code */
138   res = curl_easy_perform(curl);
139
140 test_cleanup:
141
142   /* always cleanup */
143   curl_easy_cleanup(curl);
144   curl_global_cleanup();
145
146   /* now cleanup the formpost chain */
147   curl_formfree(formpost);
148
149   return res;
150 }