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