lib1527.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Vijay Panghal, <vpanghal@maginatics.com>, 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. Same http header will be generated
  26. * for server and proxy
  27. */
  28. #include "test.h"
  29. #include "memdebug.h"
  30. static char data [] = "Hello Cloud!\n";
  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(amount < strlen(data)) {
  35. return strlen(data);
  36. }
  37. (void)stream;
  38. memcpy(ptr, data, strlen(data));
  39. return strlen(data);
  40. }
  41. CURLcode test(char *URL)
  42. {
  43. CURL *curl = NULL;
  44. CURLcode res = CURLE_FAILED_INIT;
  45. /* http header list */
  46. struct curl_slist *hhl = NULL, *tmp = NULL;
  47. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  48. fprintf(stderr, "curl_global_init() failed\n");
  49. return TEST_ERR_MAJOR_BAD;
  50. }
  51. curl = curl_easy_init();
  52. if(!curl) {
  53. fprintf(stderr, "curl_easy_init() failed\n");
  54. curl_global_cleanup();
  55. return TEST_ERR_MAJOR_BAD;
  56. }
  57. hhl = curl_slist_append(hhl, "User-Agent: Http Agent");
  58. if(!hhl) {
  59. goto test_cleanup;
  60. }
  61. tmp = curl_slist_append(hhl, "Expect: 100-continue");
  62. if(!tmp) {
  63. goto test_cleanup;
  64. }
  65. hhl = tmp;
  66. test_setopt(curl, CURLOPT_URL, URL);
  67. test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  68. test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
  69. test_setopt(curl, CURLOPT_POST, 0L);
  70. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  71. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  72. test_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  73. test_setopt(curl, CURLOPT_HEADER, 1L);
  74. test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  75. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  76. test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
  77. test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(data));
  78. test_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_UNIFIED);
  79. res = curl_easy_perform(curl);
  80. test_cleanup:
  81. curl_easy_cleanup(curl);
  82. curl_slist_free_all(hhl);
  83. curl_global_cleanup();
  84. return res;
  85. }