lib554.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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 https://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 
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 
67 static int once(char *URL, bool oldstyle)
68 {
69  CURL *curl;
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  pooh.readptr = data;
79  pooh.sizeleft = strlen(data);
80 
81  /* Fill in the file upload field */
82  if(oldstyle) {
83  formrc = curl_formadd(&formpost,
84  &lastptr,
85  CURLFORM_COPYNAME, "sendfile",
86  CURLFORM_STREAM, &pooh,
87  CURLFORM_CONTENTSLENGTH, (long)pooh.sizeleft,
88  CURLFORM_FILENAME, "postit2.c",
89  CURLFORM_END);
90  }
91  else {
92  /* new style */
93  formrc = curl_formadd(&formpost,
94  &lastptr,
95  CURLFORM_COPYNAME, "sendfile alternative",
96  CURLFORM_STREAM, &pooh,
97  CURLFORM_CONTENTLEN, (curl_off_t)pooh.sizeleft,
98  CURLFORM_FILENAME, "file name 2",
99  CURLFORM_END);
100  }
101 
102  if(formrc)
103  printf("curl_formadd(1) = %d\n", (int)formrc);
104 
105  /* Now add the same data with another name and make it not look like
106  a file upload but still using the callback */
107 
108  pooh2.readptr = data;
109  pooh2.sizeleft = strlen(data);
110 
111  /* Fill in the file upload field */
112  formrc = curl_formadd(&formpost,
113  &lastptr,
114  CURLFORM_COPYNAME, "callbackdata",
115  CURLFORM_STREAM, &pooh2,
116  CURLFORM_CONTENTSLENGTH, (long)pooh2.sizeleft,
117  CURLFORM_END);
118 
119  if(formrc)
120  printf("curl_formadd(1) = %d\n", (int)formrc);
121 
122  /* Fill in the filename field */
123  formrc = curl_formadd(&formpost,
124  &lastptr,
125  CURLFORM_COPYNAME, "filename",
127  /* ASCII representation with escape
128  sequences for non-ASCII platforms */
129  CURLFORM_COPYCONTENTS,
130  "\x70\x6f\x73\x74\x69\x74\x32\x2e\x63",
131 #else
132  CURLFORM_COPYCONTENTS, "postit2.c",
133 #endif
134  CURLFORM_END);
135 
136  if(formrc)
137  printf("curl_formadd(2) = %d\n", (int)formrc);
138 
139  /* Fill in a submit field too */
140  formrc = curl_formadd(&formpost,
141  &lastptr,
142  CURLFORM_COPYNAME, "submit",
144  /* ASCII representation with escape
145  sequences for non-ASCII platforms */
146  CURLFORM_COPYCONTENTS, "\x73\x65\x6e\x64",
147 #else
148  CURLFORM_COPYCONTENTS, "send",
149 #endif
150  CURLFORM_END);
151 
152  if(formrc)
153  printf("curl_formadd(3) = %d\n", (int)formrc);
154 
155  formrc = curl_formadd(&formpost, &lastptr,
156  CURLFORM_COPYNAME, "somename",
157  CURLFORM_BUFFER, "somefile.txt",
158  CURLFORM_BUFFERPTR, "blah blah",
159  CURLFORM_BUFFERLENGTH, (long)9,
160  CURLFORM_END);
161 
162  if(formrc)
163  printf("curl_formadd(4) = %d\n", (int)formrc);
164 
165  curl = curl_easy_init();
166  if(!curl) {
167  fprintf(stderr, "curl_easy_init() failed\n");
168  curl_formfree(formpost);
170  return TEST_ERR_MAJOR_BAD;
171  }
172 
173  /* First set the URL that is about to receive our POST. */
174  test_setopt(curl, CURLOPT_URL, URL);
175 
176  /* Now specify we want to POST data */
177  test_setopt(curl, CURLOPT_POST, 1L);
178 
179  /* Set the expected POST size */
180  test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
181 
182  /* we want to use our own read function */
183  test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
184 
185  /* send a multi-part formpost */
186  test_setopt(curl, CURLOPT_HTTPPOST, formpost);
187 
188  /* get verbose debug output please */
189  test_setopt(curl, CURLOPT_VERBOSE, 1L);
190 
191  /* include headers in the output */
192  test_setopt(curl, CURLOPT_HEADER, 1L);
193 
194  /* Perform the request, res will get the return code */
195  res = curl_easy_perform(curl);
196 
197 test_cleanup:
198 
199  /* always cleanup */
200  curl_easy_cleanup(curl);
201 
202  /* now cleanup the formpost chain */
203  curl_formfree(formpost);
204 
205  return res;
206 }
207 
208 int test(char *URL)
209 {
210  int res;
211 
213  fprintf(stderr, "curl_global_init() failed\n");
214  return TEST_ERR_MAJOR_BAD;
215  }
216 
217  res = once(URL, TRUE); /* old */
218  if(!res)
219  res = once(URL, FALSE); /* new */
220 
222 
223  return res;
224 }
size_t sizeleft
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
Definition: lib554.c:41
#define test_setopt(A, B, C)
Definition: test.h:47
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
static char data[]
Definition: lib554.c:26
static int res
#define TEST_ERR_MAJOR_BAD
Definition: test.h:85
CURL_EXTERN void curl_formfree(struct curl_httppost *form)
Definition: formdata.c:800
#define FALSE
static int once(char *URL, bool oldstyle)
Definition: lib554.c:67
CURL_EXTERN CURLFORMcode curl_formadd(struct curl_httppost **httppost, struct curl_httppost **last_post,...)
Definition: formdata.c:743
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
#define CURL_DOES_CONVERSIONS
Definition: config-tpf.h:769
CURLFORMcode
Definition: curl.h:2166
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
Definition: curl.h:455
const char * readptr
#define CURL_READFUNC_ABORT
Definition: curl.h:350
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURL
Definition: curl.h:102
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
#define TRUE
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
static CURL * curl
Definition: sessioninfo.c:35
Definition: debug.c:29
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
int test(char *URL)
Definition: lib554.c:208


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15