lib652.c 3.6 KB

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