h2-upgrade-extreme.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /* <DESC>
  25. * HTTP/2 Upgrade test
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <inttypes.h>
  32. /* #include <error.h> */
  33. #include <errno.h>
  34. #include <curl/curl.h>
  35. #include <curl/mprintf.h>
  36. static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
  37. {
  38. /*
  39. * This is the trace look that is similar to what libcurl makes on its
  40. * own.
  41. */
  42. static const char * const s_infotype[] = {
  43. "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
  44. };
  45. if(idsbuf && *idsbuf)
  46. fprintf(log, "%s%s", idsbuf, s_infotype[type]);
  47. else
  48. fputs(s_infotype[type], log);
  49. }
  50. #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
  51. #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
  52. CURL_FORMAT_CURL_OFF_T "] "
  53. /*
  54. ** callback for CURLOPT_DEBUGFUNCTION
  55. */
  56. static int debug_cb(CURL *handle, curl_infotype type,
  57. char *data, size_t size,
  58. void *userdata)
  59. {
  60. FILE *output = stderr;
  61. static int newl = 0;
  62. static int traced_data = 0;
  63. char idsbuf[60];
  64. curl_off_t xfer_id, conn_id;
  65. (void)handle; /* not used */
  66. (void)userdata;
  67. if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
  68. if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
  69. conn_id >= 0) {
  70. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
  71. xfer_id, conn_id);
  72. }
  73. else {
  74. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
  75. }
  76. }
  77. else
  78. idsbuf[0] = 0;
  79. switch(type) {
  80. case CURLINFO_HEADER_OUT:
  81. if(size > 0) {
  82. size_t st = 0;
  83. size_t i;
  84. for(i = 0; i < size - 1; i++) {
  85. if(data[i] == '\n') { /* LF */
  86. if(!newl) {
  87. log_line_start(output, idsbuf, type);
  88. }
  89. (void)fwrite(data + st, i - st + 1, 1, output);
  90. st = i + 1;
  91. newl = 0;
  92. }
  93. }
  94. if(!newl)
  95. log_line_start(output, idsbuf, type);
  96. (void)fwrite(data + st, i - st + 1, 1, output);
  97. }
  98. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  99. traced_data = 0;
  100. break;
  101. case CURLINFO_TEXT:
  102. case CURLINFO_HEADER_IN:
  103. if(!newl)
  104. log_line_start(output, idsbuf, type);
  105. (void)fwrite(data, size, 1, output);
  106. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  107. traced_data = 0;
  108. break;
  109. case CURLINFO_DATA_OUT:
  110. case CURLINFO_DATA_IN:
  111. case CURLINFO_SSL_DATA_IN:
  112. case CURLINFO_SSL_DATA_OUT:
  113. if(!traced_data) {
  114. if(!newl)
  115. log_line_start(output, idsbuf, type);
  116. fprintf(output, "[%ld bytes data]\n", (long)size);
  117. newl = 0;
  118. traced_data = 1;
  119. }
  120. break;
  121. default: /* nada */
  122. newl = 0;
  123. traced_data = 1;
  124. break;
  125. }
  126. return 0;
  127. }
  128. static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *opaque)
  129. {
  130. (void)ptr;
  131. (void)opaque;
  132. return size * nmemb;
  133. }
  134. int main(int argc, char *argv[])
  135. {
  136. const char *url;
  137. CURLM *multi;
  138. CURL *easy;
  139. CURLMcode mc;
  140. int running_handles = 0, start_count, numfds;
  141. CURLMsg *msg;
  142. int msgs_in_queue;
  143. char range[128];
  144. if(argc != 2) {
  145. fprintf(stderr, "%s URL\n", argv[0]);
  146. exit(2);
  147. }
  148. url = argv[1];
  149. multi = curl_multi_init();
  150. if(!multi) {
  151. fprintf(stderr, "curl_multi_init failed\n");
  152. exit(1);
  153. }
  154. start_count = 200;
  155. do {
  156. if(start_count) {
  157. easy = curl_easy_init();
  158. if(!easy) {
  159. fprintf(stderr, "curl_easy_init failed\n");
  160. exit(1);
  161. }
  162. curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
  163. curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, debug_cb);
  164. curl_easy_setopt(easy, CURLOPT_URL, url);
  165. curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1L);
  166. curl_easy_setopt(easy, CURLOPT_AUTOREFERER, 1L);
  167. curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
  168. curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  169. curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb);
  170. curl_easy_setopt(easy, CURLOPT_WRITEDATA, NULL);
  171. curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
  172. curl_msnprintf(range, sizeof(range), "%" PRIu64 "-%" PRIu64,
  173. UINT64_C(0), UINT64_C(16384));
  174. curl_easy_setopt(easy, CURLOPT_RANGE, range);
  175. mc = curl_multi_add_handle(multi, easy);
  176. if(mc != CURLM_OK) {
  177. fprintf(stderr, "curl_multi_add_handle: %s\n",
  178. curl_multi_strerror(mc));
  179. exit(1);
  180. }
  181. --start_count;
  182. }
  183. mc = curl_multi_perform(multi, &running_handles);
  184. if(mc != CURLM_OK) {
  185. fprintf(stderr, "curl_multi_perform: %s\n",
  186. curl_multi_strerror(mc));
  187. exit(1);
  188. }
  189. if(running_handles) {
  190. mc = curl_multi_poll(multi, NULL, 0, 1000000, &numfds);
  191. if(mc != CURLM_OK) {
  192. fprintf(stderr, "curl_multi_poll: %s\n",
  193. curl_multi_strerror(mc));
  194. exit(1);
  195. }
  196. }
  197. /* Check for finished handles and remove. */
  198. while((msg = curl_multi_info_read(multi, &msgs_in_queue))) {
  199. if(msg->msg == CURLMSG_DONE) {
  200. long status = 0;
  201. curl_off_t xfer_id;
  202. curl_easy_getinfo(msg->easy_handle, CURLINFO_XFER_ID, &xfer_id);
  203. curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &status);
  204. if(msg->data.result == CURLE_SEND_ERROR ||
  205. msg->data.result == CURLE_RECV_ERROR) {
  206. /* We get these if the server had a GOAWAY in transit on
  207. * re-using a connection */
  208. }
  209. else if(msg->data.result) {
  210. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
  211. ": failed with %d\n", xfer_id, msg->data.result);
  212. exit(1);
  213. }
  214. else if(status != 206) {
  215. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
  216. ": wrong http status %ld (expected 206)\n", xfer_id, status);
  217. exit(1);
  218. }
  219. curl_multi_remove_handle(multi, msg->easy_handle);
  220. curl_easy_cleanup(msg->easy_handle);
  221. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T" retiring "
  222. "(%d now running)\n", xfer_id, running_handles);
  223. }
  224. }
  225. fprintf(stderr, "running_handles=%d, yet_to_start=%d\n",
  226. running_handles, start_count);
  227. } while(running_handles > 0 || start_count);
  228. fprintf(stderr, "exiting\n");
  229. exit(EXIT_SUCCESS);
  230. }