aboutsummaryrefslogtreecommitdiff
path: root/src/Scraping/ScrapingHttp.c
blob: 665a729e3fb124a881d7c65db1dbcaa9786190c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "../Proxy/Proxy.h"
#include "Config.h"
#include "Scraping.h"
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define HTTP_DELAY_MIN_US 100000
#define HTTP_DELAY_RANGE_US 100000

static const char *USER_AGENTS[] = {
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, "
    "like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 "
    "(KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like "
    "Gecko) Chrome/120.0.0.0 Safari/537.36",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 "
    "Firefox/121.0",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 "
    "(KHTML, like Gecko) Version/17.2 Safari/605.1.15"};

#define USER_AGENT_COUNT (sizeof(USER_AGENTS) / sizeof(USER_AGENTS[0]))

size_t write_memory_callback(void *contents, size_t size, size_t nmemb,
                             void *userp) {
  size_t realsize = size * nmemb;
  MemoryBuffer *mem = (MemoryBuffer *)userp;

  if (mem->size + realsize + 1 > mem->capacity) {
    size_t new_cap =
        mem->capacity == 0 ? INITIAL_BUFFER_SIZE : mem->capacity * 2;
    while (new_cap < mem->size + realsize + 1)
      new_cap *= 2;

    char *ptr = (char *)realloc(mem->memory, new_cap);
    if (!ptr)
      return 0;
    mem->memory = ptr;
    mem->capacity = new_cap;
  }

  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;

  return realsize;
}

const char *get_random_user_agent(void) {
  return USER_AGENTS[rand() % USER_AGENT_COUNT];
}

const char *get_first_user_agent(void) { return USER_AGENTS[0]; }

void configure_curl_handle(CURL *curl, const char *full_url,
                           MemoryBuffer *chunk, struct curl_slist *headers) {
  curl_easy_setopt(curl, CURLOPT_URL, full_url);
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
  curl_easy_setopt(curl, CURLOPT_USERAGENT, get_random_user_agent());

  curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
  curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
  curl_easy_setopt(curl, CURLOPT_DNS_CACHE_TIMEOUT, CURL_DNS_TIMEOUT_SECS);
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  curl_easy_setopt(curl, CURLOPT_TIMEOUT, CURL_TIMEOUT_SECS);
  curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
  curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");

  apply_proxy_settings(curl);
}

char *build_search_url(const char *base_url, const char *page_param,
                       int page_multiplier, int page_base,
                       const char *encoded_query, int page) {
  long long page_value =
      (long long)(page > 1 ? page - 1 : 0) * page_multiplier + page_base;
  char *url = malloc(BUFFER_SIZE_LARGE);
  if (!url)
    return NULL;
  snprintf(url, BUFFER_SIZE_LARGE, "%s%s&%s=%lld", base_url, encoded_query,
           page_param, page_value);
  return url;
}

char *build_ddg_next_page_data(const char *html, size_t size) {
  htmlDocPtr doc = htmlReadMemory(html, size, NULL, NULL,
                                HTML_PARSE_RECOVER | HTML_PARSE_NOERROR |
                                    HTML_PARSE_NOWARNING);
  if (!doc)
    return NULL;

  xmlXPathContextPtr ctx = create_xpath_context(doc);
  if (!ctx) {
    xmlFreeDoc(doc);
    return NULL;
  }

  xmlXPathObjectPtr inputs = xmlXPathEvalExpression(
      (const xmlChar *)"(//form[.//input[@name='s'] and "
                       ".//input[@name='vqd' and string-length(@value)>0] and "
                       "(.//input[@type='submit' and contains(@value, 'Next')] or "
                       ".//button[contains(., 'Next')])])[1]"
                       "//input[@name and not(@disabled) and "
                       "(not(@type) or @type='hidden')]",
      ctx);

  if (!inputs || !inputs->nodesetval) {
    free_xpath_objects(ctx, inputs);
    xmlFreeDoc(doc);
    return NULL;
  }

  char *data = NULL;
  size_t length = 0;
  int failed = 0;
  for (int i = 0; i < inputs->nodesetval->nodeNr; i++) {
    xmlNodePtr node = inputs->nodesetval->nodeTab[i];
    xmlChar *name = xmlGetProp(node, (const xmlChar *)"name");
    xmlChar *value = xmlGetProp(node, (const xmlChar *)"value");
    char *key = curl_easy_escape(NULL, (const char *)name, 0);
    char *val = curl_easy_escape(NULL, value ? (const char *)value : "", 0);
    size_t needed = key && val ? strlen(key) + strlen(val) + 2 : 0;
    char *expanded = needed ? realloc(data, length + needed + 1) : NULL;
    if (expanded) {
      data = expanded;
      length += snprintf(data + length, needed + 1, "%s%s=%s",
                         length ? "&" : "", key, val);
    } else {
      failed = 1;
    }
    curl_free(key);
    curl_free(val);
    xmlFree(name);
    xmlFree(value);
    if (failed)
      break;
  }

  if (failed) {
    free(data);
    data = NULL;
  }
  free_xpath_objects(ctx, inputs);
  xmlFreeDoc(doc);
  return data;
}

struct curl_slist *build_request_headers(const char *host_header,
                                         const char *referer) {
  struct curl_slist *headers = NULL;
  char host_buf[BUFFER_SIZE_MEDIUM], ref_buf[BUFFER_SIZE_MEDIUM];

  if (host_header && host_header[0] != '\0') {
    snprintf(host_buf, sizeof(host_buf), "Host: %s", host_header);
    headers = curl_slist_append(headers, host_buf);
  }
  if (referer && referer[0] != '\0') {
    snprintf(ref_buf, sizeof(ref_buf), "Referer: %s", referer);
    headers = curl_slist_append(headers, ref_buf);
  }
  headers = curl_slist_append(
      headers,
      "Accept: "
      "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.5");
  headers = curl_slist_append(headers, "DNT: 1");

  return headers;
}

void http_delay(void) {
  usleep(HTTP_DELAY_MIN_US + (rand() % HTTP_DELAY_RANGE_US));
}