lib668.c 3.5 KB

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