tls-session-reuse.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * TLS session reuse
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <inttypes.h>
  33. /* #include <error.h> */
  34. #include <errno.h>
  35. #include <curl/curl.h>
  36. #include <curl/mprintf.h>
  37. static void log_line_start(FILE *log, const char *idsbuf, curl_infotype type)
  38. {
  39. /*
  40. * This is the trace look that is similar to what libcurl makes on its
  41. * own.
  42. */
  43. static const char * const s_infotype[] = {
  44. "* ", "< ", "> ", "{ ", "} ", "{ ", "} "
  45. };
  46. if(idsbuf && *idsbuf)
  47. fprintf(log, "%s%s", idsbuf, s_infotype[type]);
  48. else
  49. fputs(s_infotype[type], log);
  50. }
  51. #define TRC_IDS_FORMAT_IDS_1 "[%" CURL_FORMAT_CURL_OFF_T "-x] "
  52. #define TRC_IDS_FORMAT_IDS_2 "[%" CURL_FORMAT_CURL_OFF_T "-%" \
  53. CURL_FORMAT_CURL_OFF_T "] "
  54. /*
  55. ** callback for CURLOPT_DEBUGFUNCTION
  56. */
  57. static int debug_cb(CURL *handle, curl_infotype type,
  58. char *data, size_t size,
  59. void *userdata)
  60. {
  61. FILE *output = stderr;
  62. static int newl = 0;
  63. static int traced_data = 0;
  64. char idsbuf[60];
  65. curl_off_t xfer_id, conn_id;
  66. (void)handle; /* not used */
  67. (void)userdata;
  68. if(!curl_easy_getinfo(handle, CURLINFO_XFER_ID, &xfer_id) && xfer_id >= 0) {
  69. if(!curl_easy_getinfo(handle, CURLINFO_CONN_ID, &conn_id) &&
  70. conn_id >= 0) {
  71. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_2,
  72. xfer_id, conn_id);
  73. }
  74. else {
  75. curl_msnprintf(idsbuf, sizeof(idsbuf), TRC_IDS_FORMAT_IDS_1, xfer_id);
  76. }
  77. }
  78. else
  79. idsbuf[0] = 0;
  80. switch(type) {
  81. case CURLINFO_HEADER_OUT:
  82. if(size > 0) {
  83. size_t st = 0;
  84. size_t i;
  85. for(i = 0; i < size - 1; i++) {
  86. if(data[i] == '\n') { /* LF */
  87. if(!newl) {
  88. log_line_start(output, idsbuf, type);
  89. }
  90. (void)fwrite(data + st, i - st + 1, 1, output);
  91. st = i + 1;
  92. newl = 0;
  93. }
  94. }
  95. if(!newl)
  96. log_line_start(output, idsbuf, type);
  97. (void)fwrite(data + st, i - st + 1, 1, output);
  98. }
  99. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  100. traced_data = 0;
  101. break;
  102. case CURLINFO_TEXT:
  103. case CURLINFO_HEADER_IN:
  104. if(!newl)
  105. log_line_start(output, idsbuf, type);
  106. (void)fwrite(data, size, 1, output);
  107. newl = (size && (data[size - 1] != '\n')) ? 1 : 0;
  108. traced_data = 0;
  109. break;
  110. case CURLINFO_DATA_OUT:
  111. case CURLINFO_DATA_IN:
  112. case CURLINFO_SSL_DATA_IN:
  113. case CURLINFO_SSL_DATA_OUT:
  114. if(!traced_data) {
  115. if(!newl)
  116. log_line_start(output, idsbuf, type);
  117. fprintf(output, "[%ld bytes data]\n", (long)size);
  118. newl = 0;
  119. traced_data = 1;
  120. }
  121. break;
  122. default: /* nada */
  123. newl = 0;
  124. traced_data = 1;
  125. break;
  126. }
  127. return 0;
  128. }
  129. static size_t write_cb(char *ptr, size_t size, size_t nmemb, void *opaque)
  130. {
  131. (void)ptr;
  132. (void)opaque;
  133. return size * nmemb;
  134. }
  135. static void add_transfer(CURLM *multi, CURLSH *share,
  136. struct curl_slist *resolve, const char *url)
  137. {
  138. CURL *easy;
  139. CURLMcode mc;
  140. easy = curl_easy_init();
  141. if(!easy) {
  142. fprintf(stderr, "curl_easy_init failed\n");
  143. exit(1);
  144. }
  145. curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
  146. curl_easy_setopt(easy, CURLOPT_DEBUGFUNCTION, debug_cb);
  147. curl_easy_setopt(easy, CURLOPT_URL, url);
  148. curl_easy_setopt(easy, CURLOPT_SHARE, share);
  149. curl_easy_setopt(easy, CURLOPT_NOSIGNAL, 1L);
  150. curl_easy_setopt(easy, CURLOPT_AUTOREFERER, 1L);
  151. curl_easy_setopt(easy, CURLOPT_FAILONERROR, 1L);
  152. curl_easy_setopt(easy, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  153. curl_easy_setopt(easy, CURLOPT_WRITEFUNCTION, write_cb);
  154. curl_easy_setopt(easy, CURLOPT_WRITEDATA, NULL);
  155. curl_easy_setopt(easy, CURLOPT_HTTPGET, 1L);
  156. curl_easy_setopt(easy, CURLOPT_SSL_VERIFYPEER, 0L);
  157. if(resolve)
  158. curl_easy_setopt(easy, CURLOPT_RESOLVE, resolve);
  159. mc = curl_multi_add_handle(multi, easy);
  160. if(mc != CURLM_OK) {
  161. fprintf(stderr, "curl_multi_add_handle: %s\n",
  162. curl_multi_strerror(mc));
  163. exit(1);
  164. }
  165. }
  166. int main(int argc, char *argv[])
  167. {
  168. const char *url;
  169. CURLM *multi;
  170. CURLMcode mc;
  171. int running_handles = 0, numfds;
  172. CURLMsg *msg;
  173. CURLSH *share;
  174. CURLU *cu;
  175. struct curl_slist resolve;
  176. char resolve_buf[1024];
  177. int msgs_in_queue;
  178. int add_more, waits, ongoing = 0;
  179. char *host, *port;
  180. if(argc != 2) {
  181. fprintf(stderr, "%s URL\n", argv[0]);
  182. exit(2);
  183. }
  184. url = argv[1];
  185. cu = curl_url();
  186. if(!cu) {
  187. fprintf(stderr, "out of memory\n");
  188. exit(1);
  189. }
  190. if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
  191. fprintf(stderr, "not a URL: '%s'\n", url);
  192. exit(1);
  193. }
  194. if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
  195. fprintf(stderr, "could not get host of '%s'\n", url);
  196. exit(1);
  197. }
  198. if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
  199. fprintf(stderr, "could not get port of '%s'\n", url);
  200. exit(1);
  201. }
  202. memset(&resolve, 0, sizeof(resolve));
  203. curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1,
  204. "%s:%s:127.0.0.1", host, port);
  205. curl_slist_append(&resolve, resolve_buf);
  206. multi = curl_multi_init();
  207. if(!multi) {
  208. fprintf(stderr, "curl_multi_init failed\n");
  209. exit(1);
  210. }
  211. share = curl_share_init();
  212. if(!share) {
  213. fprintf(stderr, "curl_share_init failed\n");
  214. exit(1);
  215. }
  216. curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
  217. add_transfer(multi, share, &resolve, url);
  218. ++ongoing;
  219. add_more = 6;
  220. waits = 3;
  221. do {
  222. mc = curl_multi_perform(multi, &running_handles);
  223. if(mc != CURLM_OK) {
  224. fprintf(stderr, "curl_multi_perform: %s\n",
  225. curl_multi_strerror(mc));
  226. exit(1);
  227. }
  228. if(running_handles) {
  229. mc = curl_multi_poll(multi, NULL, 0, 1000000, &numfds);
  230. if(mc != CURLM_OK) {
  231. fprintf(stderr, "curl_multi_poll: %s\n",
  232. curl_multi_strerror(mc));
  233. exit(1);
  234. }
  235. }
  236. if(waits) {
  237. --waits;
  238. }
  239. else {
  240. while(add_more) {
  241. add_transfer(multi, share, &resolve, url);
  242. ++ongoing;
  243. --add_more;
  244. }
  245. }
  246. /* Check for finished handles and remove. */
  247. while((msg = curl_multi_info_read(multi, &msgs_in_queue))) {
  248. if(msg->msg == CURLMSG_DONE) {
  249. long status = 0;
  250. curl_off_t xfer_id;
  251. curl_easy_getinfo(msg->easy_handle, CURLINFO_XFER_ID, &xfer_id);
  252. curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &status);
  253. if(msg->data.result == CURLE_SEND_ERROR ||
  254. msg->data.result == CURLE_RECV_ERROR) {
  255. /* We get these if the server had a GOAWAY in transit on
  256. * re-using a connection */
  257. }
  258. else if(msg->data.result) {
  259. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
  260. ": failed with %d\n", xfer_id, msg->data.result);
  261. exit(1);
  262. }
  263. else if(status != 200) {
  264. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T
  265. ": wrong http status %ld (expected 200)\n", xfer_id, status);
  266. exit(1);
  267. }
  268. curl_multi_remove_handle(multi, msg->easy_handle);
  269. curl_easy_cleanup(msg->easy_handle);
  270. --ongoing;
  271. fprintf(stderr, "transfer #%" CURL_FORMAT_CURL_OFF_T" retiring "
  272. "(%d now running)\n", xfer_id, running_handles);
  273. }
  274. }
  275. fprintf(stderr, "running_handles=%d, yet_to_start=%d\n",
  276. running_handles, add_more);
  277. } while(ongoing || add_more);
  278. fprintf(stderr, "exiting\n");
  279. exit(EXIT_SUCCESS);
  280. }