test_http2.c (11946B)
1 /* SPDX-License-Identifier: LGPL-2.1-or-later OR (GPL-2.0-or-later WITH eCos-exception-2.0) */ 2 /* 3 This file is part of GNU libmicrohttpd. 4 Copyright (C) 2016, 2024, 2025 Christian Grothoff & Evgeny Grin (Karlson2k) 5 6 GNU libmicrohttpd is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 GNU libmicrohttpd is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 Alternatively, you can redistribute GNU libmicrohttpd and/or 17 modify it under the terms of the GNU General Public License as 18 published by the Free Software Foundation; either version 2 of 19 the License, or (at your option) any later version, together 20 with the eCos exception, as follows: 21 22 As a special exception, if other files instantiate templates or 23 use macros or inline functions from this file, or you compile this 24 file and link it with other works to produce a work based on this 25 file, this file does not by itself cause the resulting work to be 26 covered by the GNU General Public License. However the source code 27 for this file must still be made available in accordance with 28 section (3) of the GNU General Public License v2. 29 30 This exception does not invalidate any other reasons why a work 31 based on this file might be covered by the GNU General Public 32 License. 33 34 You should have received copies of the GNU Lesser General Public 35 License and the GNU General Public License along with this library; 36 if not, see <https://www.gnu.org/licenses/>. 37 */ 38 39 /** 40 * @file test_http2.c 41 * @brief test with client against server 42 * @author Christian Grothoff 43 */ 44 #include "libtest.h" 45 46 47 int 48 main (int argc, char *argv[]) 49 { 50 struct MHD_DaemonOptionAndValue thread1select[] = { 51 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT), 52 MHD_D_OPTION_WM_WORKER_THREADS (1), 53 MHD_D_OPTION_TERMINATE () 54 }; 55 struct MHD_DaemonOptionAndValue thread2select[] = { 56 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_SELECT), 57 MHD_D_OPTION_WM_WORKER_THREADS (2), 58 MHD_D_OPTION_TERMINATE () 59 }; 60 struct MHD_DaemonOptionAndValue thread1poll[] = { 61 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL), 62 MHD_D_OPTION_WM_WORKER_THREADS (1), 63 MHD_D_OPTION_TERMINATE () 64 }; 65 struct MHD_DaemonOptionAndValue thread2poll[] = { 66 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_POLL), 67 MHD_D_OPTION_WM_WORKER_THREADS (1), 68 MHD_D_OPTION_TERMINATE () 69 }; 70 struct MHD_DaemonOptionAndValue thread1epoll[] = { 71 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL), 72 MHD_D_OPTION_WM_WORKER_THREADS (1), 73 MHD_D_OPTION_TERMINATE () 74 }; 75 struct MHD_DaemonOptionAndValue thread2epoll[] = { 76 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_EPOLL), 77 MHD_D_OPTION_WM_WORKER_THREADS (1), 78 MHD_D_OPTION_TERMINATE () 79 }; 80 struct MHD_DaemonOptionAndValue thread1auto[] = { 81 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO), 82 MHD_D_OPTION_WM_WORKER_THREADS (1), 83 MHD_D_OPTION_TERMINATE () 84 }; 85 struct MHD_DaemonOptionAndValue external0auto[] = { 86 MHD_D_OPTION_POLL_SYSCALL (MHD_SPS_AUTO), 87 MHD_D_OPTION_WM_EXTERNAL_PERIODIC (), 88 MHD_D_OPTION_TERMINATE () 89 }; 90 struct ServerType 91 { 92 const char *label; 93 MHDT_ServerSetup server_setup; 94 void *server_setup_cls; 95 MHDT_ServerRunner server_runner; 96 void *server_runner_cls; 97 } configs[] = { 98 #ifdef MHD_SUPPORT_SELECT 99 { 100 .label = "single threaded select", 101 .server_setup = &MHDT_server_setup_minimal, 102 .server_setup_cls = thread1select, 103 .server_runner = &MHDT_server_run_minimal, 104 }, 105 { 106 .label = "multi-threaded select", 107 .server_setup = &MHDT_server_setup_minimal, 108 .server_setup_cls = thread2select, 109 .server_runner = &MHDT_server_run_minimal, 110 }, 111 #endif 112 #ifdef MHD_SUPPORT_POLL 113 { 114 .label = "single threaded poll", 115 .server_setup = &MHDT_server_setup_minimal, 116 .server_setup_cls = thread1poll, 117 .server_runner = &MHDT_server_run_minimal, 118 }, 119 { 120 .label = "multi-threaded poll", 121 .server_setup = &MHDT_server_setup_minimal, 122 .server_setup_cls = thread2poll, 123 .server_runner = &MHDT_server_run_minimal, 124 }, 125 #endif 126 #if MHD_SUPPORT_EPOLL 127 { 128 .label = "single threaded epoll", 129 .server_setup = &MHDT_server_setup_minimal, 130 .server_setup_cls = thread1epoll, 131 .server_runner = &MHDT_server_run_minimal, 132 }, 133 { 134 .label = "multi-threaded epoll", 135 .server_setup = &MHDT_server_setup_minimal, 136 .server_setup_cls = thread2epoll, 137 .server_runner = &MHDT_server_run_minimal, 138 }, 139 #endif 140 { 141 .label = "auto-selected mode, single threaded", 142 .server_setup = &MHDT_server_setup_minimal, 143 .server_setup_cls = thread1auto, 144 .server_runner = &MHDT_server_run_minimal, 145 }, 146 #ifdef MHD_SUPPORT_EPOLL 147 { 148 .label = "external events loop mode, no internal threads", 149 .server_setup = &MHDT_server_setup_external, 150 .server_setup_cls = NULL, 151 .server_runner = &MHDT_server_run_external, 152 }, 153 #endif /* MHD_SUPPORT_EPOLL */ 154 #if 1 155 /* FIXME: remove once MHD_daemon_process_blocking 156 has been implemented */ 157 { 158 .label = "END" 159 }, 160 #endif 161 { 162 .label = "auto-selected external event loop mode, no threads", 163 .server_setup = &MHDT_server_setup_minimal, 164 .server_setup_cls = external0auto, 165 .server_runner = &MHDT_server_run_blocking, 166 }, 167 { 168 .label = "END" 169 } 170 }; 171 static char medium_text[20000]; 172 struct MHDT_Phase phases[] = { 173 { 174 .label = "simple get", 175 .server_cb = &MHDT_server_reply_text, 176 .server_cb_cls = (void *)"Hello world", 177 .client_cb = &MHDT_client_get_root, 178 .client_cb_cls = "Hello world", 179 .timeout_ms = 2500, 180 .http_version = 2, 181 }, 182 { 183 .label = "GET with sendfile", 184 .server_cb = &MHDT_server_reply_file, 185 .server_cb_cls = (void *)"Hello world", 186 .client_cb = &MHDT_client_get_root, 187 .client_cb_cls = "Hello world", 188 .timeout_ms = 2500, 189 .http_version = 2, 190 }, 191 { 192 /* The body is larger than the default HTTP/2 max frame size, 193 so it is sent by several DATA frames */ 194 .label = "medium body get, buffer response", 195 .server_cb = &MHDT_server_reply_text, 196 .server_cb_cls = medium_text, 197 .client_cb = &MHDT_client_get_root, 198 .client_cb_cls = medium_text, 199 .timeout_ms = 5000, 200 .http_version = 2, 201 }, 202 { 203 .label = "simple get, file response, unknown size", 204 .server_cb = &MHDT_server_reply_file_unknown_size, 205 .server_cb_cls = (void *)"Hello world", 206 .client_cb = &MHDT_client_get_root, 207 .client_cb_cls = "Hello world", 208 .timeout_ms = 2500, 209 .http_version = 2, 210 }, 211 { 212 /* The end of the content is detected by the end of the file */ 213 .label = "medium body get, file response, unknown size", 214 .server_cb = &MHDT_server_reply_file_unknown_size, 215 .server_cb_cls = medium_text, 216 .client_cb = &MHDT_client_get_root, 217 .client_cb_cls = medium_text, 218 .timeout_ms = 5000, 219 .http_version = 2, 220 }, 221 { 222 .label = "medium body get, file response", 223 .server_cb = &MHDT_server_reply_file, 224 .server_cb_cls = medium_text, 225 .client_cb = &MHDT_client_get_root, 226 .client_cb_cls = medium_text, 227 .timeout_ms = 5000, 228 .http_version = 2, 229 }, 230 #if 0 231 { 232 .label = "client PUT with content-length", 233 .server_cb = &MHDT_server_reply_check_upload, 234 .server_cb_cls = (void *)"simple-upload-value", 235 .client_cb = &MHDT_client_put_data, 236 .client_cb_cls = "simple-upload-value", 237 .timeout_ms = 2500, 238 .http_version = 2, 239 }, 240 { 241 .label = "client PUT with 2 chunks", 242 .server_cb = &MHDT_server_reply_check_upload, 243 .server_cb_cls = (void *)"chunky-upload-value", 244 .client_cb = &MHDT_client_chunk_data, 245 .client_cb_cls = "chunky-upload-value", 246 .timeout_ms = 2500, 247 .http_version = 2, 248 }, 249 #endif 250 { 251 .label = "client request with custom header", 252 .server_cb = &MHDT_server_reply_check_header, 253 .server_cb_cls = (void *)"C-Header:testvalue", 254 .client_cb = &MHDT_client_set_header, 255 .client_cb_cls = "C-Header:testvalue", 256 .timeout_ms = 2500, 257 .http_version = 2, 258 }, 259 { 260 .label = "server response with custom header", 261 .server_cb = &MHDT_server_reply_with_header, 262 .server_cb_cls = (void *)"X-Header:testvalue", 263 .client_cb = &MHDT_client_expect_header, 264 .client_cb_cls = "X-Header:testvalue", 265 .timeout_ms = 2500, 266 .http_version = 2, 267 }, 268 { 269 .label = "URL with query parameters 1", 270 .server_cb = &MHDT_server_reply_check_query, 271 .server_cb_cls = (void *)"a=b&c", 272 .client_cb = &MHDT_client_get_with_query, 273 .client_cb_cls = "?a=b&c", 274 .timeout_ms = 5000, 275 .num_clients = 4, 276 .http_version = 2, 277 }, 278 { 279 .label = "URL with query parameters 2", 280 .server_cb = &MHDT_server_reply_check_query, 281 .server_cb_cls = (void *)"a=b&c", /* a => b, c => NULL */ 282 .client_cb = &MHDT_client_get_with_query, 283 .client_cb_cls = "?c&a=b", 284 .timeout_ms = 5000, 285 .num_clients = 1, 286 .http_version = 2, 287 }, 288 { 289 .label = "URL with query parameters 3", 290 .server_cb = &MHDT_server_reply_check_query, 291 .server_cb_cls = (void *)"a=&c", /* a => "", c => NULL */ 292 .client_cb = &MHDT_client_get_with_query, 293 .client_cb_cls = "?c&a=", 294 .timeout_ms = 5000, 295 .num_clients = 1, 296 .http_version = 2, 297 }, 298 { 299 .label = "URL with query parameters 4", 300 .server_cb = &MHDT_server_reply_check_query, 301 .server_cb_cls = (void *)"a=", /* a => "" */ 302 .client_cb = &MHDT_client_get_with_query, 303 .client_cb_cls = "?a=", 304 .timeout_ms = 5000, 305 .num_clients = 1, 306 .http_version = 2, 307 }, 308 { 309 .label = "URL with query parameters 5", 310 .server_cb = &MHDT_server_reply_check_query, 311 .server_cb_cls = (void *)"a=b", /* a => "b" */ 312 .client_cb = &MHDT_client_get_with_query, 313 .client_cb_cls = "?a=b", 314 .timeout_ms = 5000, 315 .num_clients = 1, 316 .http_version = 2, 317 }, 318 #if 0 319 { 320 .label = "chunked response get", 321 .server_cb = &MHDT_server_reply_chunked_text, 322 .server_cb_cls = (void *)"Hello world", 323 .client_cb = &MHDT_client_get_root, 324 .client_cb_cls = "Hello world", 325 .timeout_ms = 2500, 326 .http_version = 2, 327 }, 328 { 329 .label = "medium body get, callback response", 330 .server_cb = &MHDT_server_reply_chunked_text, 331 .server_cb_cls = medium_text, 332 .client_cb = &MHDT_client_get_root, 333 .client_cb_cls = medium_text, 334 .timeout_ms = 5000, 335 .http_version = 2, 336 }, 337 #endif 338 // TODO: chunked download 339 { 340 .label = NULL, 341 }, 342 }; 343 unsigned int i; 344 345 (void)argc; /* Unused. Silence compiler warning. */ 346 (void)argv; /* Unused. Silence compiler warning. */ 347 348 MHDT_fill_text_data (medium_text, 349 sizeof(medium_text)); 350 351 for (i = 0; NULL != configs[i].server_setup; i++) 352 { 353 int ret; 354 355 fprintf (stderr, 356 "Running tests with server setup '%s'\n", 357 configs[i].label); 358 ret = MHDT_test (configs[i].server_setup, 359 configs[i].server_setup_cls, 360 configs[i].server_runner, 361 configs[i].server_runner_cls, 362 phases); 363 if (0 != ret) 364 { 365 fprintf (stderr, 366 "Test failed with server of type '%s' (%u)\n", 367 configs[i].label, 368 i); 369 return ret; 370 } 371 } 372 return 0; 373 }