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