xref: /curl/tests/libtest/lib1509.c (revision 8403e5a7)
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 size_t WriteOutput(char *ptr, size_t size, size_t nmemb, void *stream);
31 size_t WriteHeader(char *ptr, size_t size, size_t nmemb, void *stream);
32 
33 static unsigned long realHeaderSize = 0;
34 
test(char * URL)35 CURLcode test(char *URL)
36 {
37   long headerSize;
38   CURLcode code;
39   CURL *curl = NULL;
40   CURLcode res = CURLE_OK;
41 
42   global_init(CURL_GLOBAL_ALL);
43 
44   easy_init(curl);
45 
46   easy_setopt(curl, CURLOPT_PROXY, libtest_arg2); /* set in first.c */
47 
48   easy_setopt(curl, CURLOPT_WRITEFUNCTION, *WriteOutput);
49   easy_setopt(curl, CURLOPT_HEADERFUNCTION, *WriteHeader);
50 
51   easy_setopt(curl, CURLOPT_HEADER, 1L);
52   easy_setopt(curl, CURLOPT_VERBOSE, 1L);
53   easy_setopt(curl, CURLOPT_URL, URL);
54   easy_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
55 
56   code = curl_easy_perform(curl);
57   if(CURLE_OK != code) {
58     fprintf(stderr, "%s:%d curl_easy_perform() failed, "
59             "with code %d (%s)\n",
60             __FILE__, __LINE__, code, curl_easy_strerror(code));
61     res = TEST_ERR_MAJOR_BAD;
62     goto test_cleanup;
63   }
64 
65   code = curl_easy_getinfo(curl, CURLINFO_HEADER_SIZE, &headerSize);
66   if(CURLE_OK != code) {
67     fprintf(stderr, "%s:%d curl_easy_getinfo() failed, "
68             "with code %d (%s)\n",
69             __FILE__, __LINE__, code, curl_easy_strerror(code));
70     res = TEST_ERR_MAJOR_BAD;
71     goto test_cleanup;
72   }
73 
74   printf("header length is ........: %ld\n", headerSize);
75   printf("header length should be..: %lu\n", realHeaderSize);
76 
77 test_cleanup:
78 
79   curl_easy_cleanup(curl);
80   curl_global_cleanup();
81 
82   return res;
83 }
84 
WriteOutput(char * ptr,size_t size,size_t nmemb,void * stream)85 size_t WriteOutput(char *ptr, size_t size, size_t nmemb, void *stream)
86 {
87   fwrite(ptr, size, nmemb, stream);
88   return nmemb * size;
89 }
90 
WriteHeader(char * ptr,size_t size,size_t nmemb,void * stream)91 size_t WriteHeader(char *ptr, size_t size, size_t nmemb, void *stream)
92 {
93   (void)ptr;
94   (void)stream;
95 
96   realHeaderSize += curlx_uztoul(size * nmemb);
97 
98   return nmemb * size;
99 }
100