crawler.c 6.5 KB

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