lib667.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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[]=
  27. "dummy";
  28. struct WriteThis {
  29. char *readptr;
  30. curl_off_t sizeleft;
  31. };
  32. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  33. {
  34. struct WriteThis *pooh = (struct WriteThis *)userp;
  35. int eof = !*pooh->readptr;
  36. if(size*nmemb < 1)
  37. return 0;
  38. eof = pooh->sizeleft <= 0;
  39. if(!eof)
  40. pooh->sizeleft--;
  41. if(!eof) {
  42. *ptr = *pooh->readptr; /* copy one single byte */
  43. pooh->readptr++; /* advance pointer */
  44. return 1; /* we return 1 byte at a time! */
  45. }
  46. return 0; /* no more data left to deliver */
  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 pooh;
  56. /*
  57. * Check proper handling of mime encoder feature when the part read callback
  58. * delivers data bytes one at a time. Use chunked encoding for accurate test.
  59. */
  60. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  61. fprintf(stderr, "curl_global_init() failed\n");
  62. return TEST_ERR_MAJOR_BAD;
  63. }
  64. easy = curl_easy_init();
  65. /* First set the URL that is about to receive our POST. */
  66. test_setopt(easy, CURLOPT_URL, URL);
  67. /* get verbose debug output please */
  68. test_setopt(easy, CURLOPT_VERBOSE, 1L);
  69. /* include headers in the output */
  70. test_setopt(easy, CURLOPT_HEADER, 1L);
  71. /* Prepare the callback structure. */
  72. pooh.readptr = data;
  73. pooh.sizeleft = (curl_off_t) strlen(data);
  74. /* Build the mime tree. */
  75. mime = curl_mime_init(easy);
  76. part = curl_mime_addpart(mime);
  77. curl_mime_name(part, "field");
  78. curl_mime_encoder(part, "base64");
  79. /* Using an undefined length forces chunked transfer. */
  80. curl_mime_data_cb(part, (curl_off_t) -1, read_callback, NULL, NULL, &pooh);
  81. /* Bind mime data to its easy handle. */
  82. test_setopt(easy, CURLOPT_MIMEPOST, mime);
  83. /* Send data. */
  84. result = curl_easy_perform(easy);
  85. if(result) {
  86. fprintf(stderr, "curl_easy_perform() failed\n");
  87. res = (int) result;
  88. }
  89. test_cleanup:
  90. curl_easy_cleanup(easy);
  91. curl_mime_free(mime);
  92. curl_global_cleanup();
  93. return res;
  94. }