2
0

lib1948.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. int test(char *URL)
  41. {
  42. CURL *curl;
  43. CURLcode res = CURLE_OUT_OF_MEMORY;
  44. curl_global_init(CURL_GLOBAL_DEFAULT);
  45. curl = curl_easy_init();
  46. if(curl) {
  47. const char *testput = "This is test PUT data\n";
  48. put_buffer pbuf;
  49. /* PUT */
  50. curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  51. curl_easy_setopt(curl, CURLOPT_HEADER, 1L);
  52. curl_easy_setopt(curl, CURLOPT_READFUNCTION, put_callback);
  53. pbuf.buf = (char *)testput;
  54. pbuf.len = strlen(testput);
  55. curl_easy_setopt(curl, CURLOPT_READDATA, &pbuf);
  56. curl_easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(testput));
  57. res = curl_easy_setopt(curl, CURLOPT_URL, URL);
  58. if(!res)
  59. res = curl_easy_perform(curl);
  60. if(!res) {
  61. /* POST */
  62. curl_easy_setopt(curl, CURLOPT_POST, 1L);
  63. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, testput);
  64. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(testput));
  65. res = curl_easy_perform(curl);
  66. }
  67. curl_easy_cleanup(curl);
  68. }
  69. curl_global_cleanup();
  70. return (int)res;
  71. }