lib540.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.haxx.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  ***************************************************************************/
22 /* This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
23  * mailing list on 10 Jul 2007, converted to a test case.
24  *
25  * argv1 = URL
26  * argv2 = proxy
27  * argv3 = proxyuser:password
28  * argv4 = host name to use for the custom Host: header
29  */
30 
31 #include "test.h"
32 
33 #ifdef HAVE_LIMITS_H
34 #include <limits.h>
35 #endif
36 
37 #include "testutil.h"
38 #include "warnless.h"
39 #include "memdebug.h"
40 
41 #define TEST_HANG_TIMEOUT 60 * 1000
42 
43 #define PROXY libtest_arg2
44 #define PROXYUSERPWD libtest_arg3
45 #define HOST test_argv[4]
46 
47 #define NUM_HANDLES 2
48 
49 static CURL *eh[NUM_HANDLES];
50 
51 static int init(int num, CURLM *cm, const char *url, const char *userpwd,
52  struct curl_slist *headers)
53 {
54  int res = 0;
55 
56  res_easy_init(eh[num]);
57  if(res)
58  goto init_failed;
59 
60  res_easy_setopt(eh[num], CURLOPT_URL, url);
61  if(res)
62  goto init_failed;
63 
64  res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
65  if(res)
66  goto init_failed;
67 
68  res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
69  if(res)
70  goto init_failed;
71 
72  res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
73  if(res)
74  goto init_failed;
75 
76  res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
77  if(res)
78  goto init_failed;
79 
80  res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
81  if(res)
82  goto init_failed;
83 
84  res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
85  if(res)
86  goto init_failed;
87 
88  res_multi_add_handle(cm, eh[num]);
89  if(res)
90  goto init_failed;
91 
92  return 0; /* success */
93 
94 init_failed:
95 
96  curl_easy_cleanup(eh[num]);
97  eh[num] = NULL;
98 
99  return res; /* failure */
100 }
101 
102 static int loop(int num, CURLM *cm, const char *url, const char *userpwd,
103  struct curl_slist *headers)
104 {
105  CURLMsg *msg;
106  long L;
107  int Q, U = -1;
108  fd_set R, W, E;
109  struct timeval T;
110  int res = 0;
111 
112  res = init(num, cm, url, userpwd, headers);
113  if(res)
114  return res;
115 
116  while(U) {
117 
118  int M = -99;
119 
120  res_multi_perform(cm, &U);
121  if(res)
122  return res;
123 
125  if(res)
126  return res;
127 
128  if(U) {
129  FD_ZERO(&R);
130  FD_ZERO(&W);
131  FD_ZERO(&E);
132 
133  res_multi_fdset(cm, &R, &W, &E, &M);
134  if(res)
135  return res;
136 
137  /* At this point, M is guaranteed to be greater or equal than -1. */
138 
139  res_multi_timeout(cm, &L);
140  if(res)
141  return res;
142 
143  /* At this point, L is guaranteed to be greater or equal than -1. */
144 
145  if(L != -1) {
146  int itimeout = (L > (long)INT_MAX) ? INT_MAX : (int)L;
147  T.tv_sec = itimeout/1000;
148  T.tv_usec = (itimeout%1000)*1000;
149  }
150  else {
151  T.tv_sec = 5;
152  T.tv_usec = 0;
153  }
154 
155  res_select_test(M + 1, &R, &W, &E, &T);
156  if(res)
157  return res;
158  }
159 
160  while((msg = curl_multi_info_read(cm, &Q)) != NULL) {
161  if(msg->msg == CURLMSG_DONE) {
162  int i;
163  CURL *e = msg->easy_handle;
164  fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
168  for(i = 0; i < NUM_HANDLES; i++) {
169  if(eh[i] == e) {
170  eh[i] = NULL;
171  break;
172  }
173  }
174  }
175  else
176  fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
177  }
178 
180  if(res)
181  return res;
182  }
183 
184  return 0; /* success */
185 }
186 
187 int test(char *URL)
188 {
189  CURLM *cm = NULL;
190  struct curl_slist *headers = NULL;
191  char buffer[246]; /* naively fixed-size */
192  int res = 0;
193  int i;
194 
195  for(i = 0; i < NUM_HANDLES; i++)
196  eh[i] = NULL;
197 
199 
200  if(test_argc < 4)
201  return 99;
202 
203  snprintf(buffer, sizeof(buffer), "Host: %s", HOST);
204 
205  /* now add a custom Host: header */
206  headers = curl_slist_append(headers, buffer);
207  if(!headers) {
208  fprintf(stderr, "curl_slist_append() failed\n");
209  return TEST_ERR_MAJOR_BAD;
210  }
211 
213  if(res) {
214  curl_slist_free_all(headers);
215  return res;
216  }
217 
218  res_multi_init(cm);
219  if(res) {
221  curl_slist_free_all(headers);
222  return res;
223  }
224 
225  res = loop(0, cm, URL, PROXYUSERPWD, headers);
226  if(res)
227  goto test_cleanup;
228 
229  fprintf(stderr, "lib540: now we do the request again\n");
230 
231  res = loop(1, cm, URL, PROXYUSERPWD, headers);
232 
233 test_cleanup:
234 
235  /* proper cleanup sequence - type PB */
236 
237  for(i = 0; i < NUM_HANDLES; i++) {
239  curl_easy_cleanup(eh[i]);
240  }
241 
242  curl_multi_cleanup(cm);
244 
245  curl_slist_free_all(headers);
246 
247  return res;
248 }
#define res_multi_add_handle(A, B)
Definition: test.h:229
#define HOST
Definition: lib540.c:45
#define res_global_init(A)
Definition: test.h:419
CURL * easy_handle
Definition: multi.h:95
#define res_multi_perform(A, B)
Definition: test.h:284
Definition: multi.h:93
#define PROXYUSERPWD
Definition: lib540.c:44
static int res
char buffer[]
Definition: unit1308.c:48
static CURL * eh[NUM_HANDLES]
Definition: lib540.c:49
unsigned int i
Definition: unit1303.c:79
#define TEST_ERR_MAJOR_BAD
Definition: test.h:85
CURL_EXTERN struct curl_slist * curl_slist_append(struct curl_slist *, const char *)
Definition: slist.c:89
CURLMSG msg
Definition: multi.h:94
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
int test(char *URL)
Definition: lib540.c:187
#define NUM_HANDLES
Definition: lib540.c:47
#define res_easy_setopt(A, B, C)
Definition: test.h:181
#define PROXY
Definition: lib540.c:43
CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle, CURL *curl_handle)
static int init(int num, CURLM *cm, const char *url, const char *userpwd, struct curl_slist *headers)
Definition: lib540.c:51
void CURLM
Definition: multi.h:58
#define res_select_test(A, B, C, D, E)
Definition: test.h:369
#define res_test_timedout()
Definition: test.h:395
void CURL
Definition: curl.h:102
CURL_EXTERN CURLMsg * curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
union CURLMsg::@6 data
int test_argc
Definition: first.c:77
#define fprintf
Definition: curl_printf.h:41
#define snprintf
Definition: curl_printf.h:42
static int loop(int num, CURLM *cm, const char *url, const char *userpwd, struct curl_slist *headers)
Definition: lib540.c:102
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
#define CURLAUTH_ANY
Definition: curl.h:708
CURLcode result
Definition: multi.h:98
#define res_multi_timeout(A, B)
Definition: test.h:344
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN void curl_slist_free_all(struct curl_slist *)
Definition: slist.c:129
#define start_test_timing()
Definition: test.h:383
#define res_multi_fdset(A, B, C, D, E)
Definition: test.h:314
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)
#define res_multi_init(A)
Definition: test.h:157
#define res_easy_init(A)
Definition: test.h:136


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15