1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2019 - 2021, 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  ***************************************************************************/
22 #include "test.h"
23 
24 #include "testutil.h"
25 #include "warnless.h"
26 #include "memdebug.h"
27 
test(char * URL)28 int test(char *URL)
29 {
30   CURLSH *sh = NULL;
31   CURL *ch = NULL;
32   int unfinished;
33   CURLM *cm;
34 
35   curl_global_init(CURL_GLOBAL_ALL);
36 
37   cm = curl_multi_init();
38   if(!cm) {
39     curl_global_cleanup();
40     return 1;
41   }
42   sh = curl_share_init();
43   if(!sh)
44     goto cleanup;
45 
46   curl_share_setopt(sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
47   curl_share_setopt(sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
48 
49   ch = curl_easy_init();
50   if(!ch)
51     goto cleanup;
52 
53   curl_easy_setopt(ch, CURLOPT_SHARE, sh);
54   curl_easy_setopt(ch, CURLOPT_URL, URL);
55   curl_easy_setopt(ch, CURLOPT_COOKIEFILE, "log/cookies1905");
56   curl_easy_setopt(ch, CURLOPT_COOKIEJAR, "log/cookies1905");
57 
58   curl_multi_add_handle(cm, ch);
59 
60   unfinished = 1;
61   while(unfinished) {
62     int MAX = 0;
63     long max_tout;
64     fd_set R, W, E;
65     struct timeval timeout;
66 
67     FD_ZERO(&R);
68     FD_ZERO(&W);
69     FD_ZERO(&E);
70     curl_multi_perform(cm, &unfinished);
71 
72     curl_multi_fdset(cm, &R, &W, &E, &MAX);
73     curl_multi_timeout(cm, &max_tout);
74 
75     if(max_tout > 0) {
76       timeout.tv_sec = max_tout / 1000;
77       timeout.tv_usec = (max_tout % 1000) * 1000;
78     }
79     else {
80       timeout.tv_sec = 0;
81       timeout.tv_usec = 1000;
82     }
83 
84     select(MAX + 1, &R, &W, &E, &timeout);
85   }
86 
87   curl_easy_setopt(ch, CURLOPT_COOKIELIST, "FLUSH");
88   curl_easy_setopt(ch, CURLOPT_SHARE, NULL);
89 
90   curl_multi_remove_handle(cm, ch);
91   cleanup:
92   curl_easy_cleanup(ch);
93   curl_share_cleanup(sh);
94   curl_multi_cleanup(cm);
95   curl_global_cleanup();
96 
97   return 0;
98 }
99