Files
jak-project/third-party/curl/docs/examples/crawler.c
T
Alexander J. Semenuk 5d5e35fb9b fix: Windows toolchain compatibility (curl 8.21 re-vendor, endless reconfigure loop) (#4355)
## Problem

Windows builds break with a current local toolchain (Scoop LLVM 22.1.8,
CMake 4.4.0, VS 2026), in two independent ways:

1. The build stops at curl's deliberate guard: `#error "no non-blocking
method was found/used/set"` in `third-party/curl/lib/nonblock.c`.
2. From the second configure onward, `cmake --build` re-runs CMake in an
endless loop (observed 42 consecutive reconfigure cycles in a single
build). Likely the same mechanism behind the "endlessly building" VS
2026 note in `docs/setup/dev/vs.md`.

## Root cause

1. `third-party/curl/CMake/CurlTests.c` passes `int *` to
`ioctlsocket()`, whose third parameter is `u_long *`. Clang 22 promotes
`-Wincompatible-pointer-types` to a hard error in C, so the
`HAVE_IOCTLSOCKET_FIONBIO` try_compile silently fails and
`curl_config.h` never defines it. Upstream CI does not see this because
the windows-2022 runner image ships an older LLVM. GCC 14 promotes the
same warning to a hard error, which is very likely the `CurlTests.c.obj`
failure reported from MSYS2 in open-goal/jak-project#3551. Upstream curl
hit the identical problem with GCC 14 and fixed the probe in curl 8.8.0
(curl/curl#13578).
2. The root CMakeLists copies the build tree's `compile_commands.json`
into `<src>/build/` for clangd using `configure_file()`, which registers
its input as a configure dependency. CMake rewrites
`compile_commands.json` late in every generation, after
`CTestTestfile.cmake` and `cmake_install.cmake` (outputs of the same
Ninja regen rule), so once the dependency is registered the rule is
deterministically dirty and every `ninja` invocation re-runs CMake. A
pristine first configure is safe (the file does not exist yet, so the
`if(EXISTS ...)` guard skips the copy), which is why the loop looks
machine- or IDE-specific.

## Fix

1. Per review, re-vendor `third-party/curl` at the `curl-8_21_0` tag
(previously `curl-8_3_0`), which carries the upstream probe fix plus two
years of upstream development; `vendor.yaml` updated to match.
Adjustments the version jump forced:
- curl 8.15 removed the native macOS Secure Transport backend
(`CURL_USE_SECTRANSP`), so macOS now builds curl against OpenSSL like
Linux. The two macOS workflows install Homebrew `openssl@3` and export
`OPENSSL_ROOT_DIR` (keg-only), and the macOS setup docs gained the same
two lines.
- `CURL_BROTLI` / `CURL_ZSTD` switched to AUTO-detection in curl 8.10;
pinned OFF to keep the previous no-compression behavior and avoid
silently linking whatever the CI images happen to have.
- curl's new top-level `BUILD_EXAMPLES` cache option (default ON) leaked
into discord-rpc's identically named option and broke configure at a
nonexistent `examples/send-presence` directory; pinned OFF ahead of the
third-party subdirectories.

The diff is dominated by the mechanical tag-tree swap under
`third-party/curl` (linguist-vendored, collapsed in review). The
hand-written changes are `CMakeLists.txt`, the two macOS workflows,
`docs/setup/system/macos.md`, and `vendor.yaml`.
2. Swap `configure_file()` for `file(COPY ...)`: the same clangd copy
with no configure dependency registered. (`file(COPY_FILE ...
ONLY_IF_DIFFERENT)` would be cleaner still but requires CMake 3.21,
above the declared `cmake_minimum_required(VERSION 3.10)`.)

## Test plan

- [x] Fresh `cmake --preset Release-windows-clang` (LLVM 22, no cache
seeding) completes and logs `Enabled SSL backends: Schannel`; the
FIONBIO probe passes without the previous `#error`
- [x] Full Windows Release build from scratch in the branch worktree
(all 1422 targets)
- [x] goalc-test suite: 1509 passed, 0 failed
- [x] Second consecutive configure with `compile_commands.json` present:
the regen rule in `build.ninja` has no `compile_commands.json` input;
`<src>/build/compile_commands.json` is still refreshed for clangd
- [x] Repeated `ninja` invocations after a full build no longer re-run
CMake
- [x] macOS Intel and ARM CI green (first exercise of the OpenSSL
backend switch)

---

I work off a self-hosted forge, so this GitHub account is quiet; the
configure logs and ninja dirty-node traces from the investigation are
available if anyone wants the raw data.

(AI-assisted)
2026-07-27 19:19:18 -04:00

262 lines
7.9 KiB
C
Vendored
Generated

/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Jeroen Ooms <jeroenooms@gmail.com>
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* <DESC>
* Web crawler based on curl and libxml2 to stress-test curl with
* hundreds of concurrent connections to various servers.
* </DESC>
*/
/*
* To compile:
* gcc crawler.c $(pkg-config --cflags --libs libxml-2.0 libcurl)
*/
#include <libxml/HTMLparser.h>
#include <libxml/xpath.h>
#include <libxml/uri.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <signal.h>
#include <curl/curl.h>
/* Parameters */
static int max_con = 200;
static int max_total = 20000;
static int max_requests = 500;
static size_t max_link_per_page = 5;
static int follow_relative_links = 0;
static const char *start_page = "https://www.reuters.com/";
static int pending_interrupt = 0;
static void sighandler(int dummy)
{
(void)dummy;
pending_interrupt = 1;
}
/* resizable buffer */
struct memory {
char *buf;
size_t size;
};
static size_t write_cb(char *contents, size_t sz, size_t nmemb, void *ctx)
{
size_t realsize = sz * nmemb;
struct memory *mem = (struct memory *)ctx;
char *ptr = realloc(mem->buf, mem->size + realsize);
if(!ptr) {
/* out of memory */
printf("not enough memory (realloc returned NULL)\n");
return 0;
}
mem->buf = ptr;
memcpy(&(mem->buf[mem->size]), contents, realsize);
mem->size += realsize;
return realsize;
}
static CURL *make_handle(const char *url)
{
CURL *curl = curl_easy_init();
struct memory *mem;
/* Important: use HTTP2 over HTTPS */
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
curl_easy_setopt(curl, CURLOPT_URL, url);
/* buffer body */
mem = malloc(sizeof(*mem));
mem->size = 0;
mem->buf = malloc(1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, mem);
curl_easy_setopt(curl, CURLOPT_PRIVATE, mem);
/* For completeness */
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
/* only allow redirects to HTTP and HTTPS URLs */
curl_easy_setopt(curl, CURLOPT_REDIR_PROTOCOLS_STR, "http,https");
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1L);
curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 10L);
/* each transfer needs to be done within 20 seconds! */
curl_easy_setopt(curl, CURLOPT_TIMEOUT_MS, 20000L);
/* connect fast or fail */
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 2000L);
/* skip files larger than a gigabyte */
curl_easy_setopt(curl, CURLOPT_MAXFILESIZE_LARGE,
(curl_off_t)1024 * 1024 * 1024);
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "mini crawler");
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_UNRESTRICTED_AUTH, 1L);
curl_easy_setopt(curl, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
curl_easy_setopt(curl, CURLOPT_EXPECT_100_TIMEOUT_MS, 0L);
return curl;
}
/* HREF finder implemented in libxml2 but could be any HTML parser */
static size_t follow_links(CURLM *multi, struct memory *mem, const char *url)
{
int opts = HTML_PARSE_NOBLANKS | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING |
HTML_PARSE_NONET;
htmlDocPtr doc = htmlReadMemory(mem->buf, (int)mem->size, url, NULL, opts);
size_t count;
int i;
xmlChar *xpath;
xmlNodeSetPtr nodeset;
xmlXPathContextPtr context;
xmlXPathObjectPtr object;
if(!doc)
return 0;
xpath = (xmlChar *)"//a/@href";
context = xmlXPathNewContext(doc);
object = xmlXPathEvalExpression(xpath, context);
xmlXPathFreeContext(context);
if(!object)
return 0;
nodeset = object->nodesetval;
if(xmlXPathNodeSetIsEmpty(nodeset)) {
xmlXPathFreeObject(object);
return 0;
}
count = 0;
for(i = 0; i < nodeset->nodeNr; i++) {
double r = rand();
int x = (int)(r * nodeset->nodeNr / RAND_MAX);
const xmlNode *node = nodeset->nodeTab[x]->xmlChildrenNode;
xmlChar *href = xmlNodeListGetString(doc, node, 1);
char *link;
if(follow_relative_links) {
xmlChar *orig = href;
href = xmlBuildURI(href, (xmlChar *)url);
xmlFree(orig);
}
link = (char *)href;
if(!link || strlen(link) < 20)
continue;
if(!strncmp(link, "http://", 7) || !strncmp(link, "https://", 8)) {
curl_multi_add_handle(multi, make_handle(link));
if(count++ == max_link_per_page)
break;
}
xmlFree(link);
}
xmlXPathFreeObject(object);
return count;
}
static int is_html(const char *ctype)
{
return ctype && strlen(ctype) > 10 && strstr(ctype, "text/html");
}
int main(void)
{
CURLM *multi;
int msgs_left;
int pending;
int complete;
int still_running;
CURLcode result;
result = curl_global_init(CURL_GLOBAL_ALL);
if(result != CURLE_OK)
return (int)result;
signal(SIGINT, sighandler);
LIBXML_TEST_VERSION
multi = curl_multi_init();
if(multi) {
curl_multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, max_con);
curl_multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 6L);
/* enables http/2 if available */
#ifdef CURLPIPE_MULTIPLEX
curl_multi_setopt(multi, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX);
#endif
/* sets html start page */
curl_multi_add_handle(multi, make_handle(start_page));
pending = 0;
complete = 0;
still_running = 1;
while(still_running && !pending_interrupt) {
int numfds;
CURLMsg *m;
curl_multi_wait(multi, NULL, 0, 1000, &numfds);
curl_multi_perform(multi, &still_running);
/* See how the transfers went */
m = NULL;
while((m = curl_multi_info_read(multi, &msgs_left))) {
if(m->msg == CURLMSG_DONE) {
CURL *curl = m->easy_handle;
char *url;
struct memory *mem;
curl_easy_getinfo(curl, CURLINFO_PRIVATE, &mem);
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if(m->data.result == CURLE_OK) {
long res_status;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &res_status);
if(res_status == 200) {
char *ctype;
curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ctype);
printf("[%d] HTTP 200 (%s): %s\n", complete, ctype, url);
if(is_html(ctype) && mem->size > 100) {
if(pending < max_requests &&
(complete + pending) < max_total) {
pending += follow_links(multi, mem, url);
still_running = 1;
}
}
}
else {
printf("[%d] HTTP %d: %s\n", complete, (int)res_status, url);
}
}
else {
printf("[%d] Connection failure: %s\n", complete, url);
}
curl_multi_remove_handle(multi, curl);
curl_easy_cleanup(curl);
free(mem->buf);
free(mem);
complete++;
pending--;
}
}
}
curl_multi_cleanup(multi);
}
curl_global_cleanup();
return 0;
}