xref: /curl/tests/libtest/lib1901.c (revision 25cbc2f7)
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 #include "test.h"
25 
26 #include "testutil.h"
27 #include "warnless.h"
28 #include "memdebug.h"
29 
30 
31 
32 static const char *chunks[]={
33   "one",
34   "two",
35   "three",
36   "four",
37   NULL
38 };
39 
40 
read_callback(char * ptr,size_t size,size_t nmemb,void * stream)41 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
42 {
43   static int ix = 0;
44   (void)size;
45   (void)nmemb;
46   (void)stream;
47   if(chunks[ix]) {
48     size_t len = strlen(chunks[ix]);
49     strcpy(ptr, chunks[ix]);
50     ix++;
51     return len;
52   }
53   return 0;
54 }
55 
test(char * URL)56 CURLcode test(char *URL)
57 {
58   CURL *curl;
59   CURLcode res = CURLE_OK;
60   struct curl_slist *chunk = NULL;
61 
62   curl_global_init(CURL_GLOBAL_ALL);
63 
64   curl = curl_easy_init();
65   if(curl) {
66     /* deliberately setting the size - to a wrong value to make sure libcurl
67        ignores it */
68     easy_setopt(curl, CURLOPT_POSTFIELDSIZE, 4L);
69     easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
70     easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
71     easy_setopt(curl, CURLOPT_POST, 1L);
72     easy_setopt(curl, CURLOPT_VERBOSE, 1L);
73     easy_setopt(curl, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_1_1);
74     easy_setopt(curl, CURLOPT_URL, URL);
75     easy_setopt(curl, CURLOPT_READDATA, NULL);
76 
77     chunk = curl_slist_append(chunk, "Expect:");
78     if(chunk) {
79       struct curl_slist *n =
80         curl_slist_append(chunk, "Transfer-Encoding: chunked");
81       if(n)
82         chunk = n;
83       if(n)
84         easy_setopt(curl, CURLOPT_HTTPHEADER, n);
85     }
86 
87     res = curl_easy_perform(curl);
88   }
89 test_cleanup:
90   curl_easy_cleanup(curl);
91   curl_slist_free_all(chunk);
92 
93   curl_global_cleanup();
94   return res;
95 }
96