2
0

crawler.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Jeroen Ooms <jeroenooms@gmail.com>
  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. * To compile:
  24. * gcc crawler.c $(pkg-config --cflags --libs libxml-2.0 libcurl)
  25. *
  26. */
  27. /* <DESC>
  28. * Web crawler based on curl and libxml2 to stress-test curl with
  29. * hundreds of concurrent connections to various servers.
  30. * </DESC>
  31. */
  32. /* Parameters */
  33. int max_con = 200;
  34. int max_total = 20000;
  35. int max_requests = 500;
  36. int max_link_per_page = 5;
  37. int follow_relative_links = 0;
  38. char *start_page = "https://www.reuters.com";
  39. #include <libxml/HTMLparser.h>
  40. #include <libxml/xpath.h>
  41. #include <libxml/uri.h>
  42. #include <curl/curl.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <math.h>
  46. #include <signal.h>
  47. int pending_interrupt = 0;
  48. void sighandler(int dummy)
  49. {
  50. pending_interrupt = 1;
  51. }
  52. /* resizable buffer */
  53. typedef struct {
  54. char *buf;
  55. size_t size;
  56. } memory;
  57. size_t grow_buffer(void *contents, size_t sz, size_t nmemb, void *ctx)
  58. {
  59. size_t realsize = sz * nmemb;
  60. memory *mem = (memory*) ctx;
  61. char *ptr = realloc(mem->buf, mem->size + realsize);
  62. if(!ptr) {
  63. /* out of memory */
  64. printf("not enough memory (realloc returned NULL)\n");
  65. return 0;
  66. }
  67. mem->buf = ptr;
  68. memcpy(&(mem->buf[mem->size]), contents, realsize);
  69. mem->size += realsize;
  70. return realsize;
  71. }
  72. CURL *make_handle(char *url)
  73. {
  74. CURL *handle = curl_easy_init();
  75. /* Important: use HTTP2 over HTTPS */
  76. curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
  77. curl_easy_setopt(handle, CURLOPT_URL, url);
  78. /* buffer body */
  79. memory *mem = malloc(sizeof(memory));
  80. mem->size = 0;
  81. mem->buf = malloc(1);
  82. curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, grow_buffer);
  83. curl_easy_setopt(handle, CURLOPT_WRITEDATA, mem);
  84. curl_easy_setopt(handle, CURLOPT_PRIVATE, mem);
  85. /* For completeness */
  86. curl_easy_setopt(handle, CURLOPT_ACCEPT_ENCODING, "");
  87. curl_easy_setopt(handle, CURLOPT_TIMEOUT, 5L);
  88. curl_easy_setopt(handle, CURLOPT_FOLLOWLOCATION, 1L);
  89. /* only allow redirects to HTTP and HTTPS URLs */
  90. curl_easy_setopt(handle, CURLOPT_REDIR_PROTOCOLS_STR, "http,https");
  91. curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
  92. curl_easy_setopt(handle, CURLOPT_MAXREDIRS, 10L);
  93. /* each transfer needs to be done within 20 seconds! */
  94. curl_easy_setopt(handle, CURLOPT_TIMEOUT_MS, 20000L);
  95. /* connect fast or fail */
  96. curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT_MS, 2000L);
  97. /* skip files larger than a gigabyte */
  98. curl_easy_setopt(handle, CURLOPT_MAXFILESIZE_LARGE,
  99. (curl_off_t)1024*1024*1024);
  100. curl_easy_setopt(handle, CURLOPT_COOKIEFILE, "");
  101. curl_easy_setopt(handle, CURLOPT_FILETIME, 1L);
  102. curl_easy_setopt(handle, CURLOPT_USERAGENT, "mini crawler");
  103. curl_easy_setopt(handle, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  104. curl_easy_setopt(handle, CURLOPT_UNRESTRICTED_AUTH, 1L);
  105. curl_easy_setopt(handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
  106. curl_easy_setopt(handle, CURLOPT_EXPECT_100_TIMEOUT_MS, 0L);
  107. return handle;
  108. }
  109. /* HREF finder implemented in libxml2 but could be any HTML parser */
  110. size_t follow_links(CURLM *multi_handle, memory *mem, char *url)
  111. {
  112. int opts = HTML_PARSE_NOBLANKS | HTML_PARSE_NOERROR | \
  113. HTML_PARSE_NOWARNING | HTML_PARSE_NONET;
  114. htmlDocPtr doc = htmlReadMemory(mem->buf, mem->size, url, NULL, opts);
  115. if(!doc)
  116. return 0;
  117. xmlChar *xpath = (xmlChar*) "//a/@href";
  118. xmlXPathContextPtr context = xmlXPathNewContext(doc);
  119. xmlXPathObjectPtr result = xmlXPathEvalExpression(xpath, context);
  120. xmlXPathFreeContext(context);
  121. if(!result)
  122. return 0;
  123. xmlNodeSetPtr nodeset = result->nodesetval;
  124. if(xmlXPathNodeSetIsEmpty(nodeset)) {
  125. xmlXPathFreeObject(result);
  126. return 0;
  127. }
  128. size_t count = 0;
  129. int i;
  130. for(i = 0; i < nodeset->nodeNr; i++) {
  131. double r = rand();
  132. int x = r * nodeset->nodeNr / RAND_MAX;
  133. const xmlNode *node = nodeset->nodeTab[x]->xmlChildrenNode;
  134. xmlChar *href = xmlNodeListGetString(doc, node, 1);
  135. if(follow_relative_links) {
  136. xmlChar *orig = href;
  137. href = xmlBuildURI(href, (xmlChar *) url);
  138. xmlFree(orig);
  139. }
  140. char *link = (char *) href;
  141. if(!link || strlen(link) < 20)
  142. continue;
  143. if(!strncmp(link, "http://", 7) || !strncmp(link, "https://", 8)) {
  144. curl_multi_add_handle(multi_handle, make_handle(link));
  145. if(count++ == max_link_per_page)
  146. break;
  147. }
  148. xmlFree(link);
  149. }
  150. xmlXPathFreeObject(result);
  151. return count;
  152. }
  153. int is_html(char *ctype)
  154. {
  155. return ctype != NULL && strlen(ctype) > 10 && strstr(ctype, "text/html");
  156. }
  157. int main(void)
  158. {
  159. signal(SIGINT, sighandler);
  160. LIBXML_TEST_VERSION;
  161. curl_global_init(CURL_GLOBAL_DEFAULT);
  162. CURLM *multi_handle = curl_multi_init();
  163. curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, max_con);
  164. curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 6L);
  165. /* enables http/2 if available */
  166. #ifdef CURLPIPE_MULTIPLEX
  167. curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
  168. #endif
  169. /* sets html start page */
  170. curl_multi_add_handle(multi_handle, make_handle(start_page));
  171. int msgs_left;
  172. int pending = 0;
  173. int complete = 0;
  174. int still_running = 1;
  175. while(still_running && !pending_interrupt) {
  176. int numfds;
  177. curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
  178. curl_multi_perform(multi_handle, &still_running);
  179. /* See how the transfers went */
  180. CURLMsg *m = NULL;
  181. while((m = curl_multi_info_read(multi_handle, &msgs_left))) {
  182. if(m->msg == CURLMSG_DONE) {
  183. CURL *handle = m->easy_handle;
  184. char *url;
  185. memory *mem;
  186. curl_easy_getinfo(handle, CURLINFO_PRIVATE, &mem);
  187. curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url);
  188. if(m->data.result == CURLE_OK) {
  189. long res_status;
  190. curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &res_status);
  191. if(res_status == 200) {
  192. char *ctype;
  193. curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &ctype);
  194. printf("[%d] HTTP 200 (%s): %s\n", complete, ctype, url);
  195. if(is_html(ctype) && mem->size > 100) {
  196. if(pending < max_requests && (complete + pending) < max_total) {
  197. pending += follow_links(multi_handle, mem, url);
  198. still_running = 1;
  199. }
  200. }
  201. }
  202. else {
  203. printf("[%d] HTTP %d: %s\n", complete, (int) res_status, url);
  204. }
  205. }
  206. else {
  207. printf("[%d] Connection failure: %s\n", complete, url);
  208. }
  209. curl_multi_remove_handle(multi_handle, handle);
  210. curl_easy_cleanup(handle);
  211. free(mem->buf);
  212. free(mem);
  213. complete++;
  214. pending--;
  215. }
  216. }
  217. }
  218. curl_multi_cleanup(multi_handle);
  219. curl_global_cleanup();
  220. return 0;
  221. }