1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2018, 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.haxx.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
32static char data [] = "Hello Cloud!\r\n";
33static size_t consumed = 0;
34
35static size_t read_callback(void *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
53static int trailers_callback(struct curl_slist **list, void *userdata)
54{
55 (void)userdata;
56 *list = curl_slist_append(*list, "my-super-awesome-trailer: trail1");
57 *list = curl_slist_append(*list, "my-other-awesome-trailer: trail2");
58 return CURL_TRAILERFUNC_OK;
59}
60
61int test(char *URL)
62{
63 CURL *curl = NULL;
64 CURLcode res = CURLE_FAILED_INIT;
65 /* http and proxy header list*/
66 struct curl_slist *hhl = NULL;
67
68 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
69 fprintf(stderr, "curl_global_init() failed\n");
70 return TEST_ERR_MAJOR_BAD;
71 }
72
73
74 curl = curl_easy_init();
75 if(!curl) {
76 fprintf(stderr, "curl_easy_init() failed\n");
77 curl_global_cleanup();
78 return TEST_ERR_MAJOR_BAD;
79 }
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
87 test_setopt(curl, CURLOPT_URL, URL);
88 test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
89 test_setopt(curl, CURLOPT_PUT, 1L);
90 test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
91 test_setopt(curl, CURLOPT_TRAILERFUNCTION, trailers_callback);
92 test_setopt(curl, CURLOPT_TRAILERDATA, NULL);
93
94 res = curl_easy_perform(curl);
95
96test_cleanup:
97
98 curl_easy_cleanup(curl);
99
100 curl_slist_free_all(hhl);
101
102 curl_global_cleanup();
103
104 return (int)res;
105}
106