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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
* Copyright (c) 2017-2024 Free Software Foundation, Inc.
*
* This file is part of libwget.
*
* Libwget is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Libwget is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libwget. If not, see <https://www.gnu.org/licenses/>.
*
*
* Read URLs from stdin and print numbers of HTTP / HTTPS links found in HTML.
* Input format is Alexa top-x, e.g. <id>,<domain>
*
*/
#include <stdio.h>
#include <string.h>
#ifndef _WIN32
# include <signal.h>
#endif
#include <wget.h>
typedef struct {
int
id,
http_links, https_links,
http_links_same_host, https_links_same_host,
status,
redirs,
redir_insecure,
landed_on_https;
char
host[256];
} stats_t;
static stats_t stats;
static void write_stats(void)
{
FILE *fp;
if ((fp = fopen("out.csv", "a"))) {
fprintf(fp, "%d,%s,%d,%d,%d,%d,%d,%d,%d,%d\n",
stats.id, stats.host, stats.status, stats.redir_insecure, stats.redirs, stats.landed_on_https,
stats.http_links_same_host, stats.http_links,
stats.https_links_same_host, stats.https_links);
fclose(fp);
}
}
/*
* helper function: percent-unescape, convert to utf-8, create URL string using base
*/
static int _normalize_uri(wget_iri *base, wget_string *url, const char *encoding, wget_buffer *buf)
{
char *urlpart_encoded;
size_t urlpart_encoded_length;
int rc;
if (url->len == 0 || (url->len >= 1 && *url->p == '#')) // ignore e.g. href='#'
return -1;
char *urlpart = wget_strmemdup(url->p, url->len);
if (!urlpart)
return -2;
wget_iri_unescape_url_inline(urlpart);
rc = wget_memiconv(encoding, urlpart, strlen(urlpart), "utf-8", &urlpart_encoded, &urlpart_encoded_length);
wget_xfree(urlpart);
if (rc)
return -3;
rc = !wget_iri_relative_to_abs(base, urlpart_encoded, urlpart_encoded_length, buf);
wget_xfree(urlpart_encoded);
if (rc)
return -4;
return 0;
}
static char *_normalize_location(const char *base, const char *url)
{
wget_buffer buf;
wget_string url_s = { .p = url, .len = strlen(url) };
wget_iri *base_iri = wget_iri_parse(base, "utf-8");
char sbuf[1024], *norm_url = NULL;
if (!base_iri)
return NULL;
wget_buffer_init(&buf, sbuf, sizeof(sbuf));
if (_normalize_uri(base_iri, &url_s, "utf-8", &buf) == 0) {
norm_url = wget_strmemdup(buf.data, buf.length);
}
wget_buffer_deinit(&buf);
wget_iri_free(&base_iri);
return norm_url;
}
static void html_parse(const char *html, size_t html_len, const char *encoding, const char *hosturl)
{
wget_iri *base = wget_iri_parse(hosturl, "utf-8");
wget_iri *allocated_base = NULL;
const char *reason;
char *utf8 = NULL;
wget_buffer buf;
char sbuf[1024];
// https://html.spec.whatwg.org/#determining-the-character-encoding
if ((unsigned char)html[0] == 0xFE && (unsigned char)html[1] == 0xFF) {
// Big-endian UTF-16
encoding = "UTF-16BE";
reason = "set by BOM";
// adjust behind BOM, ignore trailing single byte
html += 2;
html_len -= 2;
} else if ((unsigned char)html[0] == 0xFF && (unsigned char)html[1] == 0xFE) {
// Little-endian UTF-16
encoding = "UTF-16LE";
reason = "set by BOM";
// adjust behind BOM
html += 2;
html_len -= 2;
} else if ((unsigned char)html[0] == 0xEF && (unsigned char)html[1] == 0xBB && (unsigned char)html[2] == 0xBF) {
// UTF-8
encoding = "UTF-8";
reason = "set by BOM";
// adjust behind BOM
html += 3;
html_len -= 3;
} else {
reason = "set by server response";
}
// size_t n;
// html_len -= html_len & 3; // ignore single trailing byte, else charset conversion fails
if (wget_memiconv(encoding, html, html_len, "UTF-8", &utf8, NULL) == 0) {
wget_info_printf(" Convert encoding '%s' (%s) to UTF-8\n", encoding ? encoding : "iso-8859-1", reason);
html = utf8;
} else {
wget_info_printf("Failed to convert non-ASCII encoding '%s' (%s) to UTF-8, skip parsing\n", encoding, reason);
return;
}
wget_html_parsed_result *parsed = wget_html_get_urls_inline(html, NULL, NULL);
if (!encoding) {
if (parsed->encoding) {
encoding = parsed->encoding;
reason = "set by document";
} else {
encoding = "CP1252"; // default encoding for HTML5 (pre-HTML5 is iso-8859-1)
reason = "default, encoding not specified";
}
}
wget_info_printf(" URI content encoding = '%s' (%s)\n", encoding, reason);
wget_buffer_init(&buf, sbuf, sizeof(sbuf));
if (parsed->base.p) {
wget_info_printf(" base='%.*s'\n", (int) parsed->base.len, parsed->base.p);
if (_normalize_uri(base, &parsed->base, encoding, &buf) == 0) {
if (buf.length) {
wget_iri *newbase = wget_iri_parse(buf.data, "utf-8");
if (newbase) {
wget_iri_free(&base);
base = newbase;
}
}
}
}
for (int it = 0; it < wget_vector_size(parsed->uris); it++) {
wget_html_parsed_url *html_url = wget_vector_get(parsed->uris, it);
wget_string *url = &html_url->url;
if (_normalize_uri(base, url, encoding, &buf) || buf.length == 0)
continue;
wget_iri *canon_url = wget_iri_parse(buf.data, "utf-8");
if (!canon_url)
continue;
int same_host = !wget_strcasecmp(canon_url->host, base->host);
if (canon_url->scheme == WGET_IRI_SCHEME_HTTPS) {
stats.https_links++;
stats.https_links_same_host += same_host;
}
else if (canon_url->scheme == WGET_IRI_SCHEME_HTTP) {
stats.http_links++;
stats.http_links_same_host += same_host;
// if (same_host)
// wget_info_printf(" '%s'\n", canon_url->uri);
}
wget_iri_free(&canon_url);
}
wget_buffer_deinit(&buf);
wget_iri_free(&allocated_base);
wget_html_free_urls_inline(&parsed);
wget_iri_free(&base);
wget_xfree(utf8);
wget_info_printf(" same host: http=%d https=%d\n", stats.http_links_same_host, stats.https_links_same_host);
wget_info_printf(" total: http=%d https=%d\n", stats.http_links, stats.https_links);
}
int main(int argc WGET_GCC_UNUSED, const char *const *argv WGET_GCC_UNUSED)
{
// wget_http_connection *conn = NULL;
wget_http_response *resp = NULL;
char *url = NULL;
// set up libwget global configuration
wget_global_init(
// WGET_DEBUG_STREAM, stderr,
WGET_ERROR_STREAM, stdout,
WGET_INFO_STREAM, stdout,
WGET_DNS_CACHING, 1,
0);
#ifndef _WIN32
struct sigaction sig_action = { .sa_handler = SIG_IGN };
sigaction(SIGPIPE, &sig_action, NULL); // this forces socket error return
#endif
// set global timeouts to 5s
wget_tcp_set_timeout(NULL, 5000);
wget_tcp_set_connect_timeout(NULL, 5000);
wget_dns_set_timeout(NULL, 5000);
// OCSP off
wget_ssl_set_config_int(WGET_SSL_OCSP, 0);
wget_ssl_set_config_int(WGET_SSL_OCSP_STAPLING, 0);
while (fscanf(stdin, "%d,%255s", &stats.id, stats.host) == 2) {
wget_xfree(url);
if (!wget_strncasecmp_ascii(stats.host, "http://", 7))
url = wget_aprintf("https://%s", stats.host + 7);
else if (wget_strncasecmp_ascii(stats.host, "https://", 8))
url = wget_aprintf("https://%s", stats.host);
else
url = wget_strdup(stats.host);
stats.http_links = stats. https_links = 0;
stats.http_links_same_host = stats.https_links_same_host = 0;
stats.status = -1;
stats.redirs = stats.redir_insecure = stats.landed_on_https = 0;
// follow up to max 10 redirections, stop if one is plain text
for (int redirs = 0, max = 10; redirs < max; redirs++) {
wget_http_free_response(&resp);
// wget_http_close(&conn);
wget_info_printf("%s%s\n", redirs ? " -> " : "", url);
// execute an HTTP GET request and return the response
resp = wget_http_get(
WGET_HTTP_URL, url,
WGET_HTTP_HEADER_ADD, "User-Agent", "Mozilla/5.0",
WGET_HTTP_HEADER_ADD, "Accept-Encoding", "gzip, br",
WGET_HTTP_HEADER_ADD, "Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", /* some sites need this */
WGET_HTTP_HEADER_ADD, "Accept-Encoding", "gzip, br",
// WGET_HTTP_HEADER_ADD, "Upgrade-Insecure-Requests", "1",
WGET_HTTP_MAX_REDIRECTIONS, 0,
// WGET_HTTP_CONNECTION_PTR, &conn,
0);
if (!resp) {
wget_info_printf(" No connection / response\n");
break;
}
stats.status = resp->code;
if (resp->code != 200) {
if (resp->location) {
stats.redirs++;
wget_info_printf(" Response code %hd, %s\n", resp->code, resp->location);
char *newurl = _normalize_location(url, resp->location);
if (!newurl) {
wget_info_printf(" Failed to normalize '%s', '%s'\n", url, resp->location);
break;
}
wget_xfree(url);
url = newurl;
if (wget_strncasecmp(url, "https://", 8))
stats.redir_insecure++;
continue;
}
wget_info_printf(" Response code %hd\n", resp->code);
break;
}
if (wget_strncasecmp(url, "https://", 8))
break; // no need to parse, we landed on HTTP
// wget_info_printf("conn %p\n", conn);
// stats.landed_on_https = 1 + (conn->protocol == WGET_PROTOCOL_HTTP_2_0);
stats.landed_on_https = 1;
if (wget_strcasecmp_ascii(resp->content_type, "text/html")) {
wget_info_printf(" No HTML: %s\n", resp->content_type);
break;
}
if (resp->body)
html_parse(resp->body->data, resp->body->length, resp->content_type_encoding, url);
break;
}
// free the response
wget_http_free_response(&resp);
// close connection if still open
// wget_http_close(&conn);
write_stats();
}
wget_xfree(url);
// free resources - needed for valgrind testing
wget_global_deinit();
return 0;
}
|