lib668.c 3.4 KB

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