style: 应用 clang-format
This commit is contained in:
1 parent
016a45bc6e
commit
d1612f1d31
65 files changed
+7288
-5983
No files matched your search
+30
-28
@@ -9,33 +9,32 @@
|
||||
static int failures = 0;
|
||||
static int checks = 0;
|
||||
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
checks++; \
|
||||
if (!(cond)) { \
|
||||
failures++; \
|
||||
printf(" ✗ %s:%d: ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} \
|
||||
#define CHECK(cond, ...) \
|
||||
do { \
|
||||
checks++; \
|
||||
if (!(cond)) { \
|
||||
failures++; \
|
||||
printf(" ✗ %s:%d: ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
printf("\n"); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
puts("重试判定: 只重试服务端明确报错的情况");
|
||||
|
||||
/* 应当重试 */
|
||||
long retryable[] = { 408, 409, 429, 500, 502, 503, 504 };
|
||||
long retryable[] = {408, 409, 429, 500, 502, 503, 504};
|
||||
for (size_t i = 0; i < sizeof retryable / sizeof retryable[0]; i++)
|
||||
CHECK(http_status_retryable(retryable[i]) == 1,
|
||||
"HTTP %ld 应当可重试", retryable[i]);
|
||||
CHECK(http_status_retryable(retryable[i]) == 1, "HTTP %ld 应当可重试",
|
||||
retryable[i]);
|
||||
|
||||
/* 绝不应当重试: 这些是确定性错误, 重试只是浪费时间和钱 */
|
||||
long permanent[] = { 400, 401, 403, 404, 405, 413, 415, 422, 501,
|
||||
200, 201, 204, 301, 0, -1 };
|
||||
long permanent[] = {400, 401, 403, 404, 405, 413, 415, 422,
|
||||
501, 200, 201, 204, 301, 0, -1};
|
||||
for (size_t i = 0; i < sizeof permanent / sizeof permanent[0]; i++)
|
||||
CHECK(http_status_retryable(permanent[i]) == 0,
|
||||
"HTTP %ld 不应重试", permanent[i]);
|
||||
CHECK(http_status_retryable(permanent[i]) == 0, "HTTP %ld 不应重试",
|
||||
permanent[i]);
|
||||
|
||||
/* 默认策略与上限的合理性 */
|
||||
HttpRetry def = HTTP_RETRY_DEFAULT;
|
||||
@@ -53,19 +52,22 @@ int main(void)
|
||||
size_t sz = 0;
|
||||
FILE *f = open_memstream(&buf, &sz);
|
||||
|
||||
/* 这条正是曾经漏出去的: libcurl 的 HTTP/2 调试行走的是 CURLINFO_TEXT,
|
||||
/* 这条正是曾经漏出去的: libcurl 的 HTTP/2 调试行走的是 CURLINFO_TEXT,
|
||||
* 不是 HEADER_OUT, 所以只处理 HEADER_OUT 的做法根本挡不住 */
|
||||
const char *h2 = "== Info: [HTTP/2] [1] [authorization: Bearer sk-0f114e3dac8249f0a36fd8fd5c517360]\n";
|
||||
const char *h2 = "== Info: [HTTP/2] [1] [authorization: Bearer "
|
||||
"sk-0f114e3dac8249f0a36fd8fd5c517360]\n";
|
||||
http_write_redacted(f, h2, strlen(h2), key);
|
||||
|
||||
/* 整块请求头 (HEADER_OUT 是整块传来的, 不是逐行) */
|
||||
const char *hdrs = "POST /v1/chat/completions HTTP/2\r\n"
|
||||
"Authorization: Bearer sk-0f114e3dac8249f0a36fd8fd5c517360\r\n"
|
||||
"Content-Type: application/json\r\n";
|
||||
const char *hdrs =
|
||||
"POST /v1/chat/completions HTTP/2\r\n"
|
||||
"Authorization: Bearer sk-0f114e3dac8249f0a36fd8fd5c517360\r\n"
|
||||
"Content-Type: application/json\r\n";
|
||||
http_write_redacted(f, hdrs, strlen(hdrs), key);
|
||||
|
||||
/* 其它内容不能被改动 */
|
||||
const char *plain = "== Info: TLSv1.3 (IN), TLS handshake, Finished (20):\n";
|
||||
const char *plain =
|
||||
"== Info: TLSv1.3 (IN), TLS handshake, Finished (20):\n";
|
||||
http_write_redacted(f, plain, strlen(plain), key);
|
||||
|
||||
/* 没有密钥时原样输出 */
|
||||
@@ -87,9 +89,9 @@ int main(void)
|
||||
puts("\n空请求体: POST 分支也要扛得住 body == NULL");
|
||||
{
|
||||
/* body 允许为 NULL (http_probe 就是这么调的), 但 POST 分支以前写的是
|
||||
* 无条件的 strlen(body) —— 传 NULL 会在 curl_easy_setopt 期间就段错误.
|
||||
* 无条件的 strlen(body) —— 传 NULL 会在 curl_easy_setopt 期间就段错误.
|
||||
*
|
||||
* 这里故意用一个格式非法的 URL: libcurl 会在真正联网之前就拒绝它,
|
||||
* 这里故意用一个格式非法的 URL: libcurl 会在真正联网之前就拒绝它,
|
||||
* 所以本测试仍然不碰网络. 而 strlen(NULL) 发生在设置 POSTFIELDS 期间、
|
||||
* curl_easy_perform 之前, 也就是说旧代码在这里必崩 —— 这个断言对旧
|
||||
* 代码是有效的回归守卫, 不是走过场. */
|
||||
@@ -99,8 +101,8 @@ int main(void)
|
||||
long status = 0;
|
||||
|
||||
http_global_init();
|
||||
int rc = http_post(":::", "sk-test", NULL, 5, 0, &no_retry,
|
||||
NULL, &status, &attempts, errbuf, sizeof errbuf);
|
||||
int rc = http_post(":::", "sk-test", NULL, 5, 0, &no_retry, NULL,
|
||||
&status, &attempts, errbuf, sizeof errbuf);
|
||||
http_global_cleanup();
|
||||
|
||||
CHECK(rc == -1, "非法 URL 应当返回 -1 (实际返回 %d)", rc);
|
||||
|
||||
Reference in new issue
Block a user