lib652.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 buffer[17000]; /* more than 16K */
  27. int test(char *URL)
  28. {
  29. CURL *curl = NULL;
  30. CURLcode res = CURLE_OK;
  31. curl_mime *mime = NULL;
  32. curl_mimepart *part;
  33. struct curl_slist *recipients = NULL;
  34. /* create a buffer with AAAA...BBBBB...CCCC...etc */
  35. int i;
  36. int size = (int)sizeof(buffer) / 10;
  37. for(i = 0; i < size ; i++)
  38. memset(&buffer[i * 10], 65 + (i % 26), 10);
  39. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  40. fprintf(stderr, "curl_global_init() failed\n");
  41. return TEST_ERR_MAJOR_BAD;
  42. }
  43. curl = curl_easy_init();
  44. if(!curl) {
  45. fprintf(stderr, "curl_easy_init() failed\n");
  46. res = (CURLcode) TEST_ERR_MAJOR_BAD;
  47. goto test_cleanup;
  48. }
  49. /* Build mime structure. */
  50. mime = curl_mime_init(curl);
  51. if(!mime) {
  52. fprintf(stderr, "curl_mime_init() failed\n");
  53. res = (CURLcode) TEST_ERR_MAJOR_BAD;
  54. goto test_cleanup;
  55. }
  56. part = curl_mime_addpart(mime);
  57. if(!part) {
  58. fprintf(stderr, "curl_mime_addpart() failed\n");
  59. res = (CURLcode) TEST_ERR_MAJOR_BAD;
  60. goto test_cleanup;
  61. }
  62. res = curl_mime_filename(part, "myfile.jpg");
  63. if(res) {
  64. fprintf(stderr, "curl_mime_filename() failed\n");
  65. goto test_cleanup;
  66. }
  67. res = curl_mime_type(part, "image/jpeg");
  68. if(res) {
  69. fprintf(stderr, "curl_mime_type() failed\n");
  70. goto test_cleanup;
  71. }
  72. res = curl_mime_data(part, buffer, sizeof(buffer));
  73. if(res) {
  74. fprintf(stderr, "curl_mime_data() failed\n");
  75. goto test_cleanup;
  76. }
  77. res = curl_mime_encoder(part, "base64");
  78. if(res) {
  79. fprintf(stderr, "curl_mime_encoder() failed\n");
  80. goto test_cleanup;
  81. }
  82. /* Prepare recipients. */
  83. recipients = curl_slist_append(NULL, "someone@example.com");
  84. if(!recipients) {
  85. fprintf(stderr, "curl_slist_append() failed\n");
  86. goto test_cleanup;
  87. }
  88. /* First set the URL that is about to receive our mime mail. */
  89. test_setopt(curl, CURLOPT_URL, URL);
  90. /* Set sender. */
  91. test_setopt(curl, CURLOPT_MAIL_FROM, "somebody@example.com");
  92. /* Set recipients. */
  93. test_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
  94. /* send a multi-part mail */
  95. test_setopt(curl, CURLOPT_MIMEPOST, mime);
  96. /* Shorten upload buffer. */
  97. test_setopt(curl, CURLOPT_UPLOAD_BUFFERSIZE, 16411L);
  98. /* get verbose debug output please */
  99. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  100. /* Perform the request, res will get the return code */
  101. res = curl_easy_perform(curl);
  102. test_cleanup:
  103. /* always cleanup */
  104. curl_easy_cleanup(curl);
  105. /* now cleanup the mime structure */
  106. curl_mime_free(mime);
  107. /* cleanup the recipients. */
  108. curl_slist_free_all(recipients);
  109. curl_global_cleanup();
  110. return res;
  111. }