http_get: status marker without backslash/newline (robust to shell/curl variation on Linux); keeps body + non-404 tolerant

这个提交包含在:
JGZYES
2026-09-01 18:31:47 +08:00
父节点 6a93e16e24
当前提交 f74ed27f8f
+14 -8
查看文件
@@ -72,22 +72,28 @@ static char *curl_capture(const char *args, const char *url, size_t *out_len) {
} }
char *http_get(const char *url, int *status) { char *http_get(const char *url, int *status) {
/* -w appends the HTTP status code on its own last line */ /* The status marker has NO backslash/newline, so it's immune to shell
* interpretation differences: body + "__PMM_HTTP_<code>" */
size_t len = 0; size_t len = 0;
char *body = curl_capture("-sSL --max-time 60 -w \\n__PMM_HTTP__%{http_code}", url, &len); char *body = curl_capture("-sSL --max-time 60 -w __PMM_HTTP_%{http_code}", url, &len);
if (!body) return NULL; if (!body) return NULL;
char *marker = body + len - 1; /* find the LAST marker occurrence, take the digits after it */
while (marker > body && *marker != '\n') marker--; const char *mk = "__PMM_HTTP_";
if (strncmp(marker + 1, "__PMM_HTTP__", 12) == 0) { size_t mkl = strlen(mk);
int code = atoi(marker + 13); char *last = NULL;
*marker = '\0'; /* strip marker + newline */ for (char *p = body; (p = strstr(p, mk)) != NULL; p += mkl) last = p;
if (code <= 0) { /* 000 = failed transfer (reset/refused/DNS), not success */ if (last) {
int code = atoi(last + mkl);
*last = '\0'; /* strip the marker from the body */
if (code <= 0) { /* 000 = failed transfer, not success */
if (status) *status = -1; if (status) *status = -1;
free(body); free(body);
return NULL; return NULL;
} }
if (status) *status = code; if (status) *status = code;
} else { } else {
/* no marker (some curl/quirk): keep body, status unknown -> later logic
* in install accepts non-404 bodies */
if (status) *status = -1; if (status) *status = -1;
} }
return body; return body;