lib1591.c 3.1 KB

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