2
0

h2-upgrade-extreme.c 7.3 KB

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