1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "test.h"
25
26 #include "testutil.h"
27 #include "memdebug.h"
28
29 struct chunk_data {
30 int remains;
31 int print_content;
32 };
33
34 static
chunk_bgn(const void * f,void * ptr,int remains)35 long chunk_bgn(const void *f, void *ptr, int remains)
36 {
37 const struct curl_fileinfo *finfo = f;
38 struct chunk_data *ch_d = ptr;
39 ch_d->remains = remains;
40
41 printf("=============================================================\n");
42 printf("Remains: %d\n", remains);
43 printf("Filename: %s\n", finfo->filename);
44 if(finfo->strings.perm) {
45 printf("Permissions: %s", finfo->strings.perm);
46 if(finfo->flags & CURLFINFOFLAG_KNOWN_PERM)
47 printf(" (parsed => %o)", finfo->perm);
48 printf("\n");
49 }
50 printf("Size: %ldB\n", (long)finfo->size);
51 if(finfo->strings.user)
52 printf("User: %s\n", finfo->strings.user);
53 if(finfo->strings.group)
54 printf("Group: %s\n", finfo->strings.group);
55 if(finfo->strings.time)
56 printf("Time: %s\n", finfo->strings.time);
57 printf("Filetype: ");
58 switch(finfo->filetype) {
59 case CURLFILETYPE_FILE:
60 printf("regular file\n");
61 break;
62 case CURLFILETYPE_DIRECTORY:
63 printf("directory\n");
64 break;
65 case CURLFILETYPE_SYMLINK:
66 printf("symlink\n");
67 printf("Target: %s\n", finfo->strings.target);
68 break;
69 default:
70 printf("other type\n");
71 break;
72 }
73 if(finfo->filetype == CURLFILETYPE_FILE) {
74 ch_d->print_content = 1;
75 printf("Content:\n"
76 "-------------------------------------------------------------\n");
77 }
78 if(strcmp(finfo->filename, "someothertext.txt") == 0) {
79 printf("# THIS CONTENT WAS SKIPPED IN CHUNK_BGN CALLBACK #\n");
80 return CURL_CHUNK_BGN_FUNC_SKIP;
81 }
82 return CURL_CHUNK_BGN_FUNC_OK;
83 }
84
85 static
chunk_end(void * ptr)86 long chunk_end(void *ptr)
87 {
88 struct chunk_data *ch_d = ptr;
89 if(ch_d->print_content) {
90 ch_d->print_content = 0;
91 printf("-------------------------------------------------------------\n");
92 }
93 if(ch_d->remains == 1)
94 printf("=============================================================\n");
95 return CURL_CHUNK_END_FUNC_OK;
96 }
97
test(char * URL)98 CURLcode test(char *URL)
99 {
100 CURL *handle = NULL;
101 CURLcode res = CURLE_OK;
102 struct chunk_data chunk_data = {0, 0};
103 curl_global_init(CURL_GLOBAL_ALL);
104 handle = curl_easy_init();
105 if(!handle) {
106 res = CURLE_OUT_OF_MEMORY;
107 goto test_cleanup;
108 }
109
110 test_setopt(handle, CURLOPT_URL, URL);
111 test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
112 test_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, chunk_bgn);
113 test_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, chunk_end);
114 test_setopt(handle, CURLOPT_CHUNK_DATA, &chunk_data);
115
116 res = curl_easy_perform(handle);
117
118 test_cleanup:
119 if(handle)
120 curl_easy_cleanup(handle);
121 curl_global_cleanup();
122 return res;
123 }
124