• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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.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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 #define CURL_DISABLE_DEPRECATION  /* Using and testing the form api */
25 #include "test.h"
26 
27 #include "memdebug.h"
28 
29 static char buffer[17000]; /* more than 16K */
30 
test(char * URL)31 int test(char *URL)
32 {
33   CURL *curl;
34   CURLcode res = CURLE_OK;
35   CURLFORMcode formrc;
36   struct curl_httppost *formpost = NULL;
37   struct curl_httppost *lastptr = NULL;
38 
39   /* create a buffer with AAAA...BBBBB...CCCC...etc */
40   int i;
41   int size = (int)sizeof(buffer)/1000;
42 
43   for(i = 0; i < size ; i++)
44     memset(&buffer[i * 1000], 65 + i, 1000);
45 
46   buffer[ sizeof(buffer)-1] = 0; /* null-terminate */
47 
48   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
49     fprintf(stderr, "curl_global_init() failed\n");
50     return TEST_ERR_MAJOR_BAD;
51   }
52 
53   /* Check proper name and data copying. */
54   formrc = curl_formadd(&formpost, &lastptr,
55                         CURLFORM_COPYNAME, "hello",
56                         CURLFORM_COPYCONTENTS, buffer,
57                         CURLFORM_END);
58 
59   if(formrc)
60     printf("curl_formadd(1) = %d\n", (int) formrc);
61 
62 
63   curl = curl_easy_init();
64   if(!curl) {
65     fprintf(stderr, "curl_easy_init() failed\n");
66     curl_formfree(formpost);
67     curl_global_cleanup();
68     return TEST_ERR_MAJOR_BAD;
69   }
70 
71   /* First set the URL that is about to receive our POST. */
72   test_setopt(curl, CURLOPT_URL, URL);
73 
74   /* send a multi-part formpost */
75   test_setopt(curl, CURLOPT_HTTPPOST, formpost);
76 
77   /* get verbose debug output please */
78   test_setopt(curl, CURLOPT_VERBOSE, 1L);
79 
80   /* include headers in the output */
81   test_setopt(curl, CURLOPT_HEADER, 1L);
82 
83   /* Perform the request, res will get the return code */
84   res = curl_easy_perform(curl);
85 
86 test_cleanup:
87 
88   /* always cleanup */
89   curl_easy_cleanup(curl);
90 
91   /* now cleanup the formpost chain */
92   curl_formfree(formpost);
93 
94   curl_global_cleanup();
95 
96   return res;
97 }
98