lib576.c 3.7 KB

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