2
0

lib668.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "memdebug.h"
  26. static char data[]= "dummy";
  27. struct WriteThis {
  28. char *readptr;
  29. curl_off_t sizeleft;
  30. };
  31. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  32. {
  33. struct WriteThis *pooh = (struct WriteThis *)userp;
  34. size_t len = strlen(pooh->readptr);
  35. (void) size; /* Always 1.*/
  36. if(len > nmemb)
  37. len = nmemb;
  38. if(len) {
  39. memcpy(ptr, pooh->readptr, len);
  40. pooh->readptr += len;
  41. }
  42. return len;
  43. }
  44. int test(char *URL)
  45. {
  46. CURL *easy = NULL;
  47. curl_mime *mime = NULL;
  48. curl_mimepart *part;
  49. CURLcode result;
  50. int res = TEST_ERR_FAILURE;
  51. struct WriteThis pooh1, pooh2;
  52. /*
  53. * Check early end of part data detection.
  54. */
  55. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  56. fprintf(stderr, "curl_global_init() failed\n");
  57. return TEST_ERR_MAJOR_BAD;
  58. }
  59. easy = curl_easy_init();
  60. /* First set the URL that is about to receive our POST. */
  61. test_setopt(easy, CURLOPT_URL, URL);
  62. /* get verbose debug output please */
  63. test_setopt(easy, CURLOPT_VERBOSE, 1L);
  64. /* include headers in the output */
  65. test_setopt(easy, CURLOPT_HEADER, 1L);
  66. /* Prepare the callback structures. */
  67. pooh1.readptr = data;
  68. pooh1.sizeleft = (curl_off_t) strlen(data);
  69. pooh2 = pooh1;
  70. /* Build the mime tree. */
  71. mime = curl_mime_init(easy);
  72. part = curl_mime_addpart(mime);
  73. curl_mime_name(part, "field1");
  74. /* Early end of data detection can be done because the data size is known. */
  75. curl_mime_data_cb(part, (curl_off_t) strlen(data),
  76. read_callback, NULL, NULL, &pooh1);
  77. part = curl_mime_addpart(mime);
  78. curl_mime_name(part, "field2");
  79. /* Using an undefined length forces chunked transfer and disables early
  80. end of data detection for this part. */
  81. curl_mime_data_cb(part, (curl_off_t) -1, read_callback, NULL, NULL, &pooh2);
  82. part = curl_mime_addpart(mime);
  83. curl_mime_name(part, "field3");
  84. /* Regular file part sources early end of data can be detected because
  85. the file size is known. In addition, and EOF test is performed. */
  86. curl_mime_filedata(part, "log/file668.txt");
  87. /* Bind mime data to its easy handle. */
  88. test_setopt(easy, CURLOPT_MIMEPOST, mime);
  89. /* Send data. */
  90. result = curl_easy_perform(easy);
  91. if(result) {
  92. fprintf(stderr, "curl_easy_perform() failed\n");
  93. res = (int) result;
  94. }
  95. test_cleanup:
  96. curl_easy_cleanup(easy);
  97. curl_mime_free(mime);
  98. curl_global_cleanup();
  99. return res;
  100. }