xref: /curl/tests/libtest/lib1591.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 
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 static char data [] = "Hello Cloud!\r\n";
35 static size_t consumed = 0;
36 
read_callback(char * ptr,size_t size,size_t nmemb,void * stream)37 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
38 {
39   size_t  amount = nmemb * size; /* Total bytes curl wants */
40 
41   if(consumed == strlen(data)) {
42     return 0;
43   }
44 
45   if(amount > strlen(data)-consumed) {
46     amount = strlen(data);
47   }
48 
49   consumed += amount;
50   (void)stream;
51   memcpy(ptr, data, amount);
52   return amount;
53 }
54 
55 /*
56  * carefully not leak memory on OOM
57  */
trailers_callback(struct curl_slist ** list,void * userdata)58 static int trailers_callback(struct curl_slist **list, void *userdata)
59 {
60   struct curl_slist *nlist = NULL;
61   struct curl_slist *nlist2 = NULL;
62   (void)userdata;
63   nlist = curl_slist_append(*list, "my-super-awesome-trailer: trail1");
64   if(nlist)
65     nlist2 = curl_slist_append(nlist, "my-other-awesome-trailer: trail2");
66   if(nlist2) {
67     *list = nlist2;
68     return CURL_TRAILERFUNC_OK;
69   }
70   else {
71     curl_slist_free_all(nlist);
72     return CURL_TRAILERFUNC_ABORT;
73   }
74 }
75 
test(char * URL)76 CURLcode test(char *URL)
77 {
78   CURL *curl = NULL;
79   CURLcode res = CURLE_FAILED_INIT;
80   /* http and proxy header list */
81   struct curl_slist *hhl = NULL;
82 
83   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
84     fprintf(stderr, "curl_global_init() failed\n");
85     return TEST_ERR_MAJOR_BAD;
86   }
87 
88 
89   curl = curl_easy_init();
90   if(!curl) {
91     fprintf(stderr, "curl_easy_init() failed\n");
92     curl_global_cleanup();
93     return TEST_ERR_MAJOR_BAD;
94   }
95 
96   hhl = curl_slist_append(hhl, "Trailer: my-super-awesome-trailer,"
97                                " my-other-awesome-trailer");
98   if(!hhl) {
99     goto test_cleanup;
100   }
101 
102   test_setopt(curl, CURLOPT_URL, URL);
103   test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
104   test_setopt(curl, CURLOPT_UPLOAD, 1L);
105   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
106   test_setopt(curl, CURLOPT_TRAILERFUNCTION, trailers_callback);
107   test_setopt(curl, CURLOPT_TRAILERDATA, NULL);
108 
109   res = curl_easy_perform(curl);
110 
111 test_cleanup:
112 
113   curl_easy_cleanup(curl);
114 
115   curl_slist_free_all(hhl);
116 
117   curl_global_cleanup();
118 
119   return res;
120 }
121