1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
12 #include <sys/types.h>
19 #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
20 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
29 struct_stat file_info;
33 struct timeval ml_start;
34 struct timeval mp_start;
35 char ml_timedout = FALSE;
36 char mp_timedout = FALSE;
39 fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
43 hd_src = fopen(libtest_arg2, "rb");
46 fprintf(stderr, "fopen() failed with error: %d %s\n",
47 error, strerror(error));
48 fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
49 return TEST_ERR_MAJOR_BAD;
52 /* get the file size of the local file */
53 hd = fstat(fileno(hd_src), &file_info);
55 /* can't open file, bail out */
57 fprintf(stderr, "fstat() failed with error: %d %s\n",
58 error, strerror(error));
59 fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
64 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
65 fprintf(stderr, "curl_global_init() failed\n");
67 return TEST_ERR_MAJOR_BAD;
70 if ((curl = curl_easy_init()) == NULL) {
71 fprintf(stderr, "curl_easy_init() failed\n");
73 curl_global_cleanup();
74 return TEST_ERR_MAJOR_BAD;
77 /* enable uploading */
78 test_setopt(curl, CURLOPT_UPLOAD, 1L);
81 test_setopt(curl,CURLOPT_URL, URL);
84 test_setopt(curl, CURLOPT_VERBOSE, 1L);
87 test_setopt(curl, CURLOPT_FTPPORT, "-");
89 /* now specify which file to upload */
90 test_setopt(curl, CURLOPT_READDATA, hd_src);
92 /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
93 MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
94 do so will give you a crash since a DLL may not use the variable's memory
95 when passed in to it from an app like this. */
97 /* Set the size of the file to upload (optional). If you give a *_LARGE
98 option you MUST make sure that the type of the passed-in argument is a
99 curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
100 make sure that to pass in a type 'long' argument. */
101 test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
102 (curl_off_t)file_info.st_size);
104 if ((m = curl_multi_init()) == NULL) {
105 fprintf(stderr, "curl_multi_init() failed\n");
106 curl_easy_cleanup(curl);
107 curl_global_cleanup();
109 return TEST_ERR_MAJOR_BAD;
112 if ((res = (int)curl_multi_add_handle(m, curl)) != CURLM_OK) {
113 fprintf(stderr, "curl_multi_add_handle() failed, "
114 "with code %d\n", res);
115 curl_multi_cleanup(m);
116 curl_easy_cleanup(curl);
117 curl_global_cleanup();
119 return TEST_ERR_MAJOR_BAD;
123 ml_start = tutil_tvnow();
128 struct timeval interval;
131 interval.tv_usec = 0;
133 if (tutil_tvdiff(tutil_tvnow(), ml_start) >
134 MAIN_LOOP_HANG_TIMEOUT) {
139 mp_start = tutil_tvnow();
141 while (res == CURLM_CALL_MULTI_PERFORM) {
142 res = (int)curl_multi_perform(m, &running);
143 if (tutil_tvdiff(tutil_tvnow(), mp_start) >
144 MULTI_PERFORM_HANG_TIMEOUT) {
153 if (mp_timedout || done)
156 if (res != CURLM_OK) {
157 fprintf(stderr, "not okay???\n");
166 if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
167 fprintf(stderr, "unexpected failured of fdset.\n");
172 if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
173 fprintf(stderr, "bad select??\n");
178 res = CURLM_CALL_MULTI_PERFORM;
181 if (ml_timedout || mp_timedout) {
182 if (ml_timedout) fprintf(stderr, "ml_timedout\n");
183 if (mp_timedout) fprintf(stderr, "mp_timedout\n");
184 fprintf(stderr, "ABORTING TEST, since it seems "
185 "that it would have run forever.\n");
186 res = TEST_ERR_RUNS_FOREVER;
194 curl_multi_remove_handle(m, curl);
195 curl_multi_cleanup(m);
197 curl_easy_cleanup(curl);
201 curl_multi_remove_handle(m, curl);
202 curl_easy_cleanup(curl);
204 curl_multi_cleanup(m);
207 fclose(hd_src); /* close the local file */
209 curl_global_cleanup();