lib1591.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /*
  25. * This unit test PUT http data over proxy. Proxy header will be different
  26. * from server http header
  27. */
  28. #include "test.h"
  29. #include <stdio.h>
  30. #include "memdebug.h"
  31. static char data [] = "Hello Cloud!\r\n";
  32. static size_t consumed = 0;
  33. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  34. {
  35. size_t amount = nmemb * size; /* Total bytes curl wants */
  36. if(consumed == strlen(data)) {
  37. return 0;
  38. }
  39. if(amount > strlen(data)-consumed) {
  40. amount = strlen(data);
  41. }
  42. consumed += amount;
  43. (void)stream;
  44. memcpy(ptr, data, amount);
  45. return amount;
  46. }
  47. /*
  48. * carefully not leak memory on OOM
  49. */
  50. static int trailers_callback(struct curl_slist **list, void *userdata)
  51. {
  52. struct curl_slist *nlist = NULL;
  53. struct curl_slist *nlist2 = NULL;
  54. (void)userdata;
  55. nlist = curl_slist_append(*list, "my-super-awesome-trailer: trail1");
  56. if(nlist)
  57. nlist2 = curl_slist_append(nlist, "my-other-awesome-trailer: trail2");
  58. if(nlist2) {
  59. *list = nlist2;
  60. return CURL_TRAILERFUNC_OK;
  61. }
  62. else {
  63. curl_slist_free_all(nlist);
  64. return CURL_TRAILERFUNC_ABORT;
  65. }
  66. }
  67. CURLcode test(char *URL)
  68. {
  69. CURL *curl = NULL;
  70. CURLcode res = CURLE_FAILED_INIT;
  71. /* http and proxy header list */
  72. struct curl_slist *hhl = NULL;
  73. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  74. fprintf(stderr, "curl_global_init() failed\n");
  75. return TEST_ERR_MAJOR_BAD;
  76. }
  77. curl = curl_easy_init();
  78. if(!curl) {
  79. fprintf(stderr, "curl_easy_init() failed\n");
  80. curl_global_cleanup();
  81. return TEST_ERR_MAJOR_BAD;
  82. }
  83. hhl = curl_slist_append(hhl, "Trailer: my-super-awesome-trailer,"
  84. " my-other-awesome-trailer");
  85. if(!hhl) {
  86. goto test_cleanup;
  87. }
  88. test_setopt(curl, CURLOPT_URL, URL);
  89. test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
  90. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  91. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  92. test_setopt(curl, CURLOPT_TRAILERFUNCTION, trailers_callback);
  93. test_setopt(curl, CURLOPT_TRAILERDATA, NULL);
  94. res = curl_easy_perform(curl);
  95. test_cleanup:
  96. curl_easy_cleanup(curl);
  97. curl_slist_free_all(hhl);
  98. curl_global_cleanup();
  99. return res;
  100. }