2011-03-10 18:48:02 +08:00
|
|
|
/***************************************************************************
|
2007-09-12 06:23:57 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-03-10 18:48:02 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2011-03-10 18:48:02 +08:00
|
|
|
***************************************************************************/
|
2007-09-12 06:23:57 +08:00
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2008-09-20 12:26:55 +08:00
|
|
|
|
2007-09-12 06:23:57 +08:00
|
|
|
/*
|
|
|
|
* Two FTP uploads, the second with no content sent.
|
|
|
|
*/
|
|
|
|
|
2024-05-12 03:36:05 +08:00
|
|
|
CURLcode test(char *URL)
|
2007-09-12 06:23:57 +08:00
|
|
|
{
|
|
|
|
CURL *curl;
|
|
|
|
CURLcode res = CURLE_OK;
|
2016-04-03 17:57:34 +08:00
|
|
|
FILE *hd_src;
|
2014-05-05 21:30:05 +08:00
|
|
|
int hd;
|
2007-09-12 06:23:57 +08:00
|
|
|
struct_stat file_info;
|
|
|
|
|
2016-04-03 17:57:34 +08:00
|
|
|
if(!libtest_arg2) {
|
2007-09-12 06:23:57 +08:00
|
|
|
fprintf(stderr, "Usage: <url> <file-to-upload>\n");
|
2014-05-05 21:30:05 +08:00
|
|
|
return TEST_ERR_USAGE;
|
2007-09-12 06:23:57 +08:00
|
|
|
}
|
|
|
|
|
2010-04-07 13:51:05 +08:00
|
|
|
hd_src = fopen(libtest_arg2, "rb");
|
2021-12-26 04:41:14 +08:00
|
|
|
if(!hd_src) {
|
2016-04-03 17:57:34 +08:00
|
|
|
fprintf(stderr, "fopen failed with error: %d %s\n",
|
2017-07-11 13:53:23 +08:00
|
|
|
errno, strerror(errno));
|
2010-04-07 13:51:05 +08:00
|
|
|
fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
|
2024-05-12 03:36:05 +08:00
|
|
|
return (CURLcode)-2; /* if this happens things are major weird */
|
2010-04-07 13:51:05 +08:00
|
|
|
}
|
|
|
|
|
2007-09-12 06:23:57 +08:00
|
|
|
/* get the file size of the local file */
|
2010-04-07 13:51:05 +08:00
|
|
|
hd = fstat(fileno(hd_src), &file_info);
|
2007-09-12 06:23:57 +08:00
|
|
|
if(hd == -1) {
|
|
|
|
/* can't open file, bail out */
|
2010-04-07 13:51:05 +08:00
|
|
|
fprintf(stderr, "fstat() failed with error: %d %s\n",
|
2017-07-11 13:53:23 +08:00
|
|
|
errno, strerror(errno));
|
2010-04-07 13:51:05 +08:00
|
|
|
fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
|
|
|
|
fclose(hd_src);
|
2014-05-05 21:30:05 +08:00
|
|
|
return TEST_ERR_MAJOR_BAD;
|
2007-09-12 06:23:57 +08:00
|
|
|
}
|
|
|
|
|
2020-10-02 16:58:52 +08:00
|
|
|
if(!file_info.st_size) {
|
2010-04-07 13:51:05 +08:00
|
|
|
fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
|
|
|
|
fclose(hd_src);
|
2014-05-05 21:30:05 +08:00
|
|
|
return TEST_ERR_MAJOR_BAD;
|
2007-09-12 06:23:57 +08:00
|
|
|
}
|
|
|
|
|
2016-04-03 17:57:34 +08:00
|
|
|
if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
|
2007-09-12 06:23:57 +08:00
|
|
|
fprintf(stderr, "curl_global_init() failed\n");
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get a curl handle */
|
2016-12-14 08:29:44 +08:00
|
|
|
curl = curl_easy_init();
|
|
|
|
if(!curl) {
|
2007-09-12 06:23:57 +08:00
|
|
|
fprintf(stderr, "curl_easy_init() failed\n");
|
|
|
|
curl_global_cleanup();
|
|
|
|
fclose(hd_src);
|
|
|
|
return TEST_ERR_MAJOR_BAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* enable uploading */
|
2010-02-06 02:07:19 +08:00
|
|
|
test_setopt(curl, CURLOPT_UPLOAD, 1L);
|
2007-09-12 06:23:57 +08:00
|
|
|
|
|
|
|
/* enable verbose */
|
2010-02-06 02:07:19 +08:00
|
|
|
test_setopt(curl, CURLOPT_VERBOSE, 1L);
|
2007-09-12 06:23:57 +08:00
|
|
|
|
|
|
|
/* specify target */
|
2016-04-03 17:57:34 +08:00
|
|
|
test_setopt(curl, CURLOPT_URL, URL);
|
2007-09-12 06:23:57 +08:00
|
|
|
|
|
|
|
/* now specify which file to upload */
|
2014-06-18 07:17:23 +08:00
|
|
|
test_setopt(curl, CURLOPT_READDATA, hd_src);
|
2007-09-12 06:23:57 +08:00
|
|
|
|
|
|
|
/* Now run off and do what you've been told! */
|
2023-09-30 07:32:48 +08:00
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
if(res)
|
|
|
|
goto test_cleanup;
|
2007-09-12 06:23:57 +08:00
|
|
|
|
|
|
|
/* and now upload the exact same again, but without rewinding so it already
|
|
|
|
is at end of file */
|
|
|
|
res = curl_easy_perform(curl);
|
|
|
|
|
2010-02-06 02:07:19 +08:00
|
|
|
test_cleanup:
|
|
|
|
|
2007-09-12 06:23:57 +08:00
|
|
|
/* close the local file */
|
|
|
|
fclose(hd_src);
|
|
|
|
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
curl_global_cleanup();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|