lib576.c 3.8 KB

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