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
25 /*
26 * This unit test PUT http data over proxy. Proxy header will be different
27 * from server http header
28 */
29
30 #include "test.h"
31 #include <stdio.h>
32 #include "memdebug.h"
33
34 /*
35 * carefully not leak memory on OOM
36 */
trailers_callback(struct curl_slist ** list,void * userdata)37 static int trailers_callback(struct curl_slist **list, void *userdata)
38 {
39 struct curl_slist *nlist = NULL;
40 struct curl_slist *nlist2 = NULL;
41 (void)userdata;
42 nlist = curl_slist_append(*list, "my-super-awesome-trailer: trail1");
43 if(nlist)
44 nlist2 = curl_slist_append(nlist, "my-other-awesome-trailer: trail2");
45 if(nlist2) {
46 *list = nlist2;
47 return CURL_TRAILERFUNC_OK;
48 }
49 else {
50 curl_slist_free_all(nlist);
51 return CURL_TRAILERFUNC_ABORT;
52 }
53 }
54
55 static const char *post_data = "xxx=yyy&aaa=bbbbb";
56
test(char * URL)57 CURLcode test(char *URL)
58 {
59 CURL *curl = NULL;
60 CURLcode res = CURLE_FAILED_INIT;
61 /* http and proxy header list */
62 struct curl_slist *hhl = NULL, *list;
63
64 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
65 fprintf(stderr, "curl_global_init() failed\n");
66 return TEST_ERR_MAJOR_BAD;
67 }
68
69
70 curl = curl_easy_init();
71 if(!curl) {
72 fprintf(stderr, "curl_easy_init() failed\n");
73 curl_global_cleanup();
74 return TEST_ERR_MAJOR_BAD;
75 }
76
77 hhl = curl_slist_append(hhl, "Trailer: my-super-awesome-trailer,"
78 " my-other-awesome-trailer");
79 if(!hhl)
80 goto test_cleanup;
81 if(hhl) {
82 list = curl_slist_append(hhl, "Transfer-Encoding: chunked");
83 if(!list)
84 goto test_cleanup;
85 hhl = list;
86 }
87
88 test_setopt(curl, CURLOPT_URL, URL);
89 test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
90 test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(post_data));
91 test_setopt(curl, CURLOPT_POSTFIELDS, post_data);
92 test_setopt(curl, CURLOPT_TRAILERFUNCTION, trailers_callback);
93 test_setopt(curl, CURLOPT_TRAILERDATA, NULL);
94 test_setopt(curl, CURLOPT_VERBOSE, 1L);
95
96 res = curl_easy_perform(curl);
97
98 test_cleanup:
99
100 curl_easy_cleanup(curl);
101
102 curl_slist_free_all(hhl);
103
104 curl_global_cleanup();
105
106 return res;
107 }
108