1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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 http://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
23#include "test.h"
24
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <fcntl.h>
28
29#include "testutil.h"
30#include "warnless.h"
31#include "memdebug.h"
32
33/* 3x download!
34 * 1. normal
35 * 2. dup handle
36 * 3. with multi interface
37 */
38
39int test(char *URL)
40{
41  CURLMcode m;
42  CURL *handle = NULL, *duphandle;
43  CURLM *mhandle = NULL;
44  int res = 0;
45  int still_running = 0;
46
47  if(curl_global_init(CURL_GLOBAL_ALL)) {
48    fprintf(stderr, "curl_global_init() failed\n");
49    goto test_cleanup;
50  }
51
52  handle = curl_easy_init();
53  if(!handle) {
54    res = CURLE_OUT_OF_MEMORY;
55    goto test_cleanup;
56  }
57
58  test_setopt(handle, CURLOPT_URL, URL);
59  test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
60  test_setopt(handle, CURLOPT_VERBOSE, 1L);
61
62  res = curl_easy_perform(handle);
63  if(res)
64    goto test_cleanup;
65
66  res = curl_easy_perform(handle);
67  if(res)
68    goto test_cleanup;
69
70  duphandle = curl_easy_duphandle(handle);
71  if(!duphandle)
72    goto test_cleanup;
73  curl_easy_cleanup(handle);
74  handle = duphandle;
75
76  mhandle = curl_multi_init();
77  if(!mhandle) {
78    fprintf(stderr, "curl_multi_init() failed\n");
79    goto test_cleanup;
80  }
81
82  curl_multi_add_handle(mhandle, handle);
83
84  while(CURLM_CALL_MULTI_PERFORM ==
85        curl_multi_perform(mhandle, &still_running));
86
87  while(still_running) {
88    static struct timeval timeout = /* 100 ms */ { 0, 100000L };
89    int rc;
90    fd_set fdread;
91    fd_set fdwrite;
92    fd_set fdexcep;
93    int max_fdset = -1;
94    FD_ZERO(&fdread);
95    FD_ZERO(&fdwrite);
96    FD_ZERO(&fdexcep);
97
98    m = curl_multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &max_fdset);
99    if(m != CURLM_OK) {
100      fprintf(stderr, "curl_multi_fdset() error\n");
101      goto test_cleanup;
102    }
103    /* We call select(max_fdset + 1, ...), specially in case of (maxfd == -1),
104     * we call select(0, ...), which is basically equal to sleep. */
105    rc = select(max_fdset + 1, &fdread, &fdwrite, &fdexcep, &timeout);
106    if(rc == -1) {
107      fprintf(stderr, "select() error\n");
108      goto test_cleanup;
109    }
110    else {
111      while(CURLM_CALL_MULTI_PERFORM ==
112          curl_multi_perform(mhandle, &still_running));
113    }
114  }
115
116test_cleanup:
117  if(mhandle)
118    curl_multi_cleanup(mhandle);
119  if(handle)
120    curl_easy_cleanup(handle);
121  curl_global_cleanup();
122  return res;
123}
124