lib650.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";
32 #else
33  "this is what we post to the silly web server";
34 #endif
35 
36 static char name[] = "fieldname";
37 
38 
39 /* This test attempts to use all form API features that are not
40  * used elsewhere.
41  */
42 
43 /* curl_formget callback to count characters. */
44 static size_t count_chars(void *userp, const char *buf, size_t len)
45 {
46  size_t *pcounter = (size_t *) userp;
47 
48  (void) buf;
49  *pcounter += len;
50  return len;
51 }
52 
53 
54 int test(char *URL)
55 {
56  CURL *curl;
58  CURLFORMcode formrc;
59  struct curl_slist *headers = NULL;
60  struct curl_httppost *formpost = NULL;
61  struct curl_httppost *lastptr = NULL;
62  struct curl_forms formarray[3];
63  size_t formlength = 0;
64  char flbuf[32];
65 
67  fprintf(stderr, "curl_global_init() failed\n");
68  return TEST_ERR_MAJOR_BAD;
69  }
70 
71  /* Check proper name and data copying. */
72  formrc = curl_formadd(&formpost, &lastptr,
73  CURLFORM_COPYNAME, &name,
74  CURLFORM_COPYCONTENTS, &data,
75  CURLFORM_END);
76 
77  if(formrc)
78  printf("curl_formadd(1) = %d\n", (int) formrc);
79 
80  /* Use a form array for the non-copy test. */
81  formarray[0].option = CURLFORM_PTRCONTENTS;
82  formarray[0].value = data;
83  formarray[1].option = CURLFORM_CONTENTSLENGTH;
84  formarray[1].value = (char *) strlen(data) - 1;
85  formarray[2].option = CURLFORM_END;
86  formarray[2].value = NULL;
87  formrc = curl_formadd(&formpost,
88  &lastptr,
89  CURLFORM_PTRNAME, name,
90  CURLFORM_NAMELENGTH, strlen(name) - 1,
91  CURLFORM_ARRAY, formarray,
92  CURLFORM_FILENAME, "remotefile.txt",
93  CURLFORM_END);
94 
95  if(formrc)
96  printf("curl_formadd(2) = %d\n", (int) formrc);
97 
98  /* Now change in-memory data to affect CURLOPT_PTRCONTENTS value.
99  Copied values (first field) must not be affected.
100  CURLOPT_PTRNAME actually copies the name thus we do not test this here. */
101  data[0]++;
102 
103  /* Check multi-files and content type propagation. */
104  formrc = curl_formadd(&formpost,
105  &lastptr,
106  CURLFORM_COPYNAME, "multifile",
107  CURLFORM_FILE, libtest_arg2, /* Set in first.c. */
108  CURLFORM_FILE, libtest_arg2,
109  CURLFORM_CONTENTTYPE, "text/whatever",
110  CURLFORM_FILE, libtest_arg2,
111  CURLFORM_END);
112 
113  if(formrc)
114  printf("curl_formadd(3) = %d\n", (int) formrc);
115 
116  /* Check data from file content and headers. */
117  headers = curl_slist_append(headers, "X-customheader-1: Header 1 data");
118  headers = curl_slist_append(headers, "X-customheader-2: Header 2 data");
119  formrc = curl_formadd(&formpost,
120  &lastptr,
121  CURLFORM_COPYNAME, "filecontents",
122  CURLFORM_FILECONTENT, libtest_arg2,
123  CURLFORM_CONTENTHEADER, headers,
124  CURLFORM_END);
125 
126  if(formrc)
127  printf("curl_formadd(3) = %d\n", (int) formrc);
128 
129  /* Measure the current form length.
130  * This is done before including stdin data because we want to reuse it
131  * and stdin cannot be rewound.
132  */
133  curl_formget(formpost, (void *) &formlength, count_chars);
134 
135  /* Include length in data for external check. */
136  curl_msnprintf(flbuf, sizeof flbuf, "%lu", (unsigned long) formlength);
137  formrc = curl_formadd(&formpost,
138  &lastptr,
139  CURLFORM_COPYNAME, "formlength",
140  CURLFORM_COPYCONTENTS, &flbuf,
141  CURLFORM_END);
142 
143  /* Check stdin (may be problematic on some platforms). */
144  formrc = curl_formadd(&formpost,
145  &lastptr,
146  CURLFORM_COPYNAME, "standardinput",
147  CURLFORM_FILE, "-",
148  CURLFORM_END);
149 
150  if(formrc)
151  printf("curl_formadd(4) = %d\n", (int) formrc);
152 
153  curl = curl_easy_init();
154  if(!curl) {
155  fprintf(stderr, "curl_easy_init() failed\n");
156  curl_slist_free_all(headers);
157  curl_formfree(formpost);
159  return TEST_ERR_MAJOR_BAD;
160  }
161 
162  /* First set the URL that is about to receive our POST. */
163  test_setopt(curl, CURLOPT_URL, URL);
164 
165  /* send a multi-part formpost */
166  test_setopt(curl, CURLOPT_HTTPPOST, formpost);
167 
168  /* get verbose debug output please */
169  test_setopt(curl, CURLOPT_VERBOSE, 1L);
170 
171  /* include headers in the output */
172  test_setopt(curl, CURLOPT_HEADER, 1L);
173 
174  /* Perform the request, res will get the return code */
175  res = curl_easy_perform(curl);
176 
177 test_cleanup:
178 
179  /* always cleanup */
180  curl_easy_cleanup(curl);
181 
182  /* now cleanup the formpost chain */
183  curl_formfree(formpost);
184  curl_slist_free_all(headers);
185 
187 
188  return res;
189 }
CURLformoption option
Definition: curl.h:2146
#define test_setopt(A, B, C)
Definition: test.h:47
static char data[]
Definition: lib650.c:26
CURLcode
Definition: curl.h:454
static int res
size_t len
Definition: curl_sasl.c:55
#define TEST_ERR_MAJOR_BAD
Definition: test.h:85
char * libtest_arg2
Definition: first.c:75
CURL_EXTERN void curl_formfree(struct curl_httppost *form)
Definition: formdata.c:800
CURL_EXTERN CURLFORMcode curl_formadd(struct curl_httppost **httppost, struct curl_httppost **last_post,...)
Definition: formdata.c:743
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
int test(char *URL)
Definition: lib650.c:54
CURLFORMcode
Definition: curl.h:2166
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_EXTERN int curl_msnprintf(char *buffer, size_t maxlength, const char *format,...)
Definition: mprintf.c:1018
Definition: curl.h:455
static size_t count_chars(void *userp, const char *buf, size_t len)
Definition: lib650.c:44
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
CURL_EXTERN int curl_formget(struct curl_httppost *form, void *arg, curl_formget_callback append)
Definition: formdata.c:762
const char * value
Definition: curl.h:2147
char buf[3]
Definition: unit1398.c:32
void CURL
Definition: curl.h:102
#define fprintf
Definition: curl_printf.h:41
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
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
Definition: debug.c:29
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
static char name[]
Definition: lib650.c:36


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