2
0

lib667.c 3.3 KB

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