lib510.c 3.4 KB

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