lib510.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 const char *post[]={
  27. "one",
  28. "two",
  29. "three",
  30. "and a final longer crap: four",
  31. NULL
  32. };
  33. struct WriteThis {
  34. int counter;
  35. };
  36. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
  37. {
  38. struct WriteThis *pooh = (struct WriteThis *)userp;
  39. const char *data;
  40. if(size*nmemb < 1)
  41. return 0;
  42. data = post[pooh->counter];
  43. if(data) {
  44. size_t len = strlen(data);
  45. if(size*nmemb < len) {
  46. fprintf(stderr, "read buffer is too small to run test\n");
  47. return 0;
  48. }
  49. memcpy(ptr, data, len);
  50. pooh->counter++; /* advance pointer */
  51. return len;
  52. }
  53. return 0; /* no more data left to deliver */
  54. }
  55. int test(char *URL)
  56. {
  57. CURL *curl;
  58. CURLcode res = CURLE_OK;
  59. struct curl_slist *slist = NULL;
  60. struct WriteThis pooh;
  61. pooh.counter = 0;
  62. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  63. fprintf(stderr, "curl_global_init() failed\n");
  64. return TEST_ERR_MAJOR_BAD;
  65. }
  66. curl = curl_easy_init();
  67. if(!curl) {
  68. fprintf(stderr, "curl_easy_init() failed\n");
  69. curl_global_cleanup();
  70. return TEST_ERR_MAJOR_BAD;
  71. }
  72. slist = curl_slist_append(slist, "Transfer-Encoding: chunked");
  73. if(!slist) {
  74. fprintf(stderr, "curl_slist_append() failed\n");
  75. curl_easy_cleanup(curl);
  76. curl_global_cleanup();
  77. return TEST_ERR_MAJOR_BAD;
  78. }
  79. /* First set the URL that is about to receive our POST. */
  80. test_setopt(curl, CURLOPT_URL, URL);
  81. /* Now specify we want to POST data */
  82. test_setopt(curl, CURLOPT_POST, 1L);
  83. /* we want to use our own read function */
  84. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  85. /* pointer to pass to our read function */
  86. test_setopt(curl, CURLOPT_READDATA, &pooh);
  87. /* get verbose debug output please */
  88. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  89. /* include headers in the output */
  90. test_setopt(curl, CURLOPT_HEADER, 1L);
  91. /* enforce chunked transfer by setting the header */
  92. test_setopt(curl, CURLOPT_HTTPHEADER, slist);
  93. #ifdef LIB565
  94. test_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_DIGEST);
  95. test_setopt(curl, CURLOPT_USERPWD, "foo:bar");
  96. #endif
  97. /* Perform the request, res will get the return code */
  98. res = curl_easy_perform(curl);
  99. test_cleanup:
  100. /* clean up the headers list */
  101. if(slist)
  102. curl_slist_free_all(slist);
  103. /* always cleanup */
  104. curl_easy_cleanup(curl);
  105. curl_global_cleanup();
  106. return res;
  107. }