lib668.c 3.4 KB

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