lib1948.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. typedef struct
  26. {
  27. char *buf;
  28. size_t len;
  29. } put_buffer;
  30. static size_t put_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  31. {
  32. put_buffer *putdata = (put_buffer *)stream;
  33. size_t totalsize = size * nmemb;
  34. size_t tocopy = (putdata->len < totalsize) ? putdata->len : totalsize;
  35. memcpy(ptr, putdata->buf, tocopy);
  36. putdata->len -= tocopy;
  37. putdata->buf += tocopy;
  38. return tocopy;
  39. }
  40. CURLcode test(char *URL)
  41. {
  42. CURL *curl;
  43. CURLcode res = CURLE_OK;
  44. const char *testput = "This is test PUT data\n";
  45. put_buffer pbuf;
  46. curl_global_init(CURL_GLOBAL_DEFAULT);
  47. easy_init(curl);
  48. /* PUT */
  49. easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  50. easy_setopt(curl, CURLOPT_HEADER, 1L);
  51. easy_setopt(curl, CURLOPT_READFUNCTION, put_callback);
  52. pbuf.buf = (char *)testput;
  53. pbuf.len = strlen(testput);
  54. easy_setopt(curl, CURLOPT_READDATA, &pbuf);
  55. easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(testput));
  56. easy_setopt(curl, CURLOPT_URL, URL);
  57. res = curl_easy_perform(curl);
  58. if(res)
  59. goto test_cleanup;
  60. /* POST */
  61. easy_setopt(curl, CURLOPT_POST, 1L);
  62. easy_setopt(curl, CURLOPT_POSTFIELDS, testput);
  63. easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(testput));
  64. res = curl_easy_perform(curl);
  65. test_cleanup:
  66. curl_easy_cleanup(curl);
  67. curl_global_cleanup();
  68. return res;
  69. }