lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2016, 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.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 |
| 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-----------------------" |
| 78 | "--------------------------------------\n"); |
| 79 | } |
| 80 | if(strcmp(finfo->filename, "someothertext.txt") == 0) { |
| 81 | printf("# THIS CONTENT WAS SKIPPED IN CHUNK_BGN CALLBACK #\n"); |
| 82 | return CURL_CHUNK_BGN_FUNC_SKIP; |
| 83 | } |
| 84 | return CURL_CHUNK_BGN_FUNC_OK; |
| 85 | } |
| 86 | |
| 87 | static |
| 88 | long chunk_end(void *ptr) |
| 89 | { |
| 90 | chunk_data_t *ch_d = ptr; |
| 91 | if(ch_d->print_content) { |
| 92 | ch_d->print_content = 0; |
| 93 | printf("-------------------------------------------------------------\n"); |
| 94 | } |
| 95 | if(ch_d->remains == 1) |
| 96 | printf("=============================================================\n"); |
| 97 | return CURL_CHUNK_END_FUNC_OK; |
| 98 | } |
| 99 | |
| 100 | int test(char *URL) |
| 101 | { |
| 102 | CURL *handle = NULL; |
| 103 | CURLcode res = CURLE_OK; |
| 104 | chunk_data_t chunk_data = {0, 0}; |
| 105 | curl_global_init(CURL_GLOBAL_ALL); |
| 106 | handle = curl_easy_init(); |
| 107 | if(!handle) { |
| 108 | res = CURLE_OUT_OF_MEMORY; |
| 109 | goto test_cleanup; |
| 110 | } |
| 111 | |
| 112 | test_setopt(handle, CURLOPT_URL, URL); |
| 113 | test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L); |
| 114 | test_setopt(handle, CURLOPT_CHUNK_BGN_FUNCTION, chunk_bgn); |
| 115 | test_setopt(handle, CURLOPT_CHUNK_END_FUNCTION, chunk_end); |
| 116 | test_setopt(handle, CURLOPT_CHUNK_DATA, &chunk_data); |
| 117 | |
| 118 | res = curl_easy_perform(handle); |
| 119 | |
| 120 | test_cleanup: |
| 121 | if(handle) |
| 122 | curl_easy_cleanup(handle); |
| 123 | curl_global_cleanup(); |
| 124 | return res; |
| 125 | } |