lib1526.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. Proxy header will be different
  26. * from server http header
  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. int test(char *URL)
  42. {
  43. CURL *curl = NULL;
  44. CURLcode res = CURLE_FAILED_INIT;
  45. /* http and proxy header list */
  46. struct curl_slist *hhl = NULL, *phl = 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. phl = curl_slist_append(phl, "User-Agent: Proxy Agent");
  59. if(!hhl || !phl) {
  60. goto test_cleanup;
  61. }
  62. tmp = curl_slist_append(phl, "Expect:");
  63. if(!tmp) {
  64. goto test_cleanup;
  65. }
  66. phl = tmp;
  67. test_setopt(curl, CURLOPT_URL, URL);
  68. test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  69. test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
  70. test_setopt(curl, CURLOPT_PROXYHEADER, phl);
  71. test_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE);
  72. test_setopt(curl, CURLOPT_POST, 0L);
  73. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  74. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  75. test_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  76. test_setopt(curl, CURLOPT_HEADER, 1L);
  77. test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  78. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  79. test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
  80. test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(data));
  81. res = curl_easy_perform(curl);
  82. test_cleanup:
  83. curl_easy_cleanup(curl);
  84. curl_slist_free_all(hhl);
  85. curl_slist_free_all(phl);
  86. curl_global_cleanup();
  87. return (int)res;
  88. }