lib1525.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) 2014, Vijay Panghal, <vpanghal@maginatics.com>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. ***************************************************************************/
  23. /*
  24. * This unit test PUT http data over proxy. Proxy header will be different
  25. * from server http header
  26. */
  27. #include "test.h"
  28. #include "memdebug.h"
  29. static char data [] = "Hello Cloud!\n";
  30. static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
  31. {
  32. size_t amount = nmemb * size; /* Total bytes curl wants */
  33. if(amount < strlen(data)) {
  34. return strlen(data);
  35. }
  36. (void)stream;
  37. memcpy(ptr, data, strlen(data));
  38. return strlen(data);
  39. }
  40. int test(char *URL)
  41. {
  42. CURL *curl = NULL;
  43. CURLcode res = CURLE_FAILED_INIT;
  44. /* http and proxy header list*/
  45. struct curl_slist *hhl = NULL;
  46. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  47. fprintf(stderr, "curl_global_init() failed\n");
  48. return TEST_ERR_MAJOR_BAD;
  49. }
  50. curl = curl_easy_init();
  51. if(!curl) {
  52. fprintf(stderr, "curl_easy_init() failed\n");
  53. curl_global_cleanup();
  54. return TEST_ERR_MAJOR_BAD;
  55. }
  56. hhl = curl_slist_append(hhl, "User-Agent: Http Agent");
  57. if(!hhl) {
  58. goto test_cleanup;
  59. }
  60. test_setopt(curl, CURLOPT_URL, URL);
  61. test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
  62. test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
  63. test_setopt(curl, CURLOPT_PROXYHEADER, hhl);
  64. test_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_UNIFIED);
  65. test_setopt(curl, CURLOPT_POST, 0L);
  66. test_setopt(curl, CURLOPT_UPLOAD, 1L);
  67. test_setopt(curl, CURLOPT_VERBOSE, 1L);
  68. test_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
  69. test_setopt(curl, CURLOPT_HEADER, 1L);
  70. test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
  71. test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
  72. test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
  73. test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(data));
  74. res = curl_easy_perform(curl);
  75. test_cleanup:
  76. curl_easy_cleanup(curl);
  77. curl_slist_free_all(hhl);
  78. curl_global_cleanup();
  79. return (int)res;
  80. }