lib576.c 3.7 KB

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