1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 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 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24 #include "test.h"
25
26 #ifdef HAVE_SYS_STAT_H
27 #include <sys/stat.h>
28 #endif
29 #ifdef HAVE_FCNTL_H
30 #include <fcntl.h>
31 #endif
32
33 #include "memdebug.h"
34
35 /* build request url */
suburl(const char * base,int i)36 static char *suburl(const char *base, int i)
37 {
38 return curl_maprintf("%s%.4d", base, i);
39 }
40
41 /*
42 * Test GET_PARAMETER: PUT, HEARTBEAT, and POST
43 */
test(char * URL)44 int test(char *URL)
45 {
46 int res;
47 CURL *curl;
48 int params;
49 FILE *paramsf = NULL;
50 struct_stat file_info;
51 char *stream_uri = NULL;
52 int request = 1;
53 struct curl_slist *custom_headers = NULL;
54
55 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
56 fprintf(stderr, "curl_global_init() failed\n");
57 return TEST_ERR_MAJOR_BAD;
58 }
59
60 curl = curl_easy_init();
61 if(!curl) {
62 fprintf(stderr, "curl_easy_init() failed\n");
63 curl_global_cleanup();
64 return TEST_ERR_MAJOR_BAD;
65 }
66
67
68 test_setopt(curl, CURLOPT_HEADERDATA, stdout);
69 test_setopt(curl, CURLOPT_WRITEDATA, stdout);
70 test_setopt(curl, CURLOPT_VERBOSE, 1L);
71
72 test_setopt(curl, CURLOPT_URL, URL);
73
74 /* SETUP */
75 stream_uri = suburl(URL, request++);
76 if(!stream_uri) {
77 res = TEST_ERR_MAJOR_BAD;
78 goto test_cleanup;
79 }
80 test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
81 curl_free(stream_uri);
82 stream_uri = NULL;
83
84 test_setopt(curl, CURLOPT_RTSP_TRANSPORT, "Planes/Trains/Automobiles");
85 test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_SETUP);
86 res = curl_easy_perform(curl);
87 if(res)
88 goto test_cleanup;
89
90 stream_uri = suburl(URL, request++);
91 if(!stream_uri) {
92 res = TEST_ERR_MAJOR_BAD;
93 goto test_cleanup;
94 }
95 test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
96 curl_free(stream_uri);
97 stream_uri = NULL;
98
99 /* PUT style GET_PARAMETERS */
100 params = open(libtest_arg2, O_RDONLY);
101 fstat(params, &file_info);
102 close(params);
103
104 paramsf = fopen(libtest_arg2, "rb");
105 if(!paramsf) {
106 fprintf(stderr, "can't open %s\n", libtest_arg2);
107 res = TEST_ERR_MAJOR_BAD;
108 goto test_cleanup;
109 }
110 test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_GET_PARAMETER);
111
112 test_setopt(curl, CURLOPT_READDATA, paramsf);
113 test_setopt(curl, CURLOPT_UPLOAD, 1L);
114 test_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t) file_info.st_size);
115
116 res = curl_easy_perform(curl);
117 if(res)
118 goto test_cleanup;
119
120 test_setopt(curl, CURLOPT_UPLOAD, 0L);
121 fclose(paramsf);
122 paramsf = NULL;
123
124 /* Heartbeat GET_PARAMETERS */
125 stream_uri = suburl(URL, request++);
126 if(!stream_uri) {
127 res = TEST_ERR_MAJOR_BAD;
128 goto test_cleanup;
129 }
130 test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
131 curl_free(stream_uri);
132 stream_uri = NULL;
133
134 res = curl_easy_perform(curl);
135 if(res)
136 goto test_cleanup;
137
138 /* POST GET_PARAMETERS */
139
140 stream_uri = suburl(URL, request++);
141 if(!stream_uri) {
142 res = TEST_ERR_MAJOR_BAD;
143 goto test_cleanup;
144 }
145 test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
146 curl_free(stream_uri);
147 stream_uri = NULL;
148
149 test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_GET_PARAMETER);
150 test_setopt(curl, CURLOPT_POSTFIELDS, "packets_received\njitter\n");
151
152 res = curl_easy_perform(curl);
153 if(res)
154 goto test_cleanup;
155
156 test_setopt(curl, CURLOPT_POSTFIELDS, NULL);
157
158 /* Make sure we can do a normal request now */
159 stream_uri = suburl(URL, request++);
160 if(!stream_uri) {
161 res = TEST_ERR_MAJOR_BAD;
162 goto test_cleanup;
163 }
164 test_setopt(curl, CURLOPT_RTSP_STREAM_URI, stream_uri);
165 curl_free(stream_uri);
166 stream_uri = NULL;
167
168 test_setopt(curl, CURLOPT_RTSP_REQUEST, CURL_RTSPREQ_OPTIONS);
169 res = curl_easy_perform(curl);
170
171 test_cleanup:
172
173 if(paramsf)
174 fclose(paramsf);
175
176 curl_free(stream_uri);
177
178 if(custom_headers)
179 curl_slist_free_all(custom_headers);
180
181 curl_easy_cleanup(curl);
182 curl_global_cleanup();
183
184 return res;
185 }
186