Skip to content

Commit 0df099c

Browse files
为 compat.curl 添加可选 HTTP/2 支持 (#970)
* 为 compat.curl 添加可选 HTTP/2 支持 * 修正 Windows 下 nghttp2 旧回调的返回类型
1 parent 427c41e commit 0df099c

6 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎mcpp.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ members = [
115115
"tests/examples/cuda-curand",
116116
"tests/examples/cuda-driver",
117117
"tests/examples/curl",
118+
"tests/examples/curl-http2",
118119
"tests/examples/djgpp-platform",
119120
"tests/examples/mcpp-platforms",
120121
"tests/examples/doctest",

‎pkgs/c/compat.curl.lua‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ package = {
110110
features = {
111111
["default"] = { implies = { "staticlib" } },
112112
["staticlib"] = { defines = { "CURL_STATICLIB" } },
113+
["http2"] = {
114+
implies = { "staticlib" },
115+
defines = { "USE_NGHTTP2" },
116+
deps = { ["compat.nghttp2"] = "1.71.0" },
117+
},
113118
},
114119

115120
cflags = {

‎pkgs/c/compat.nghttp2.lua‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ package = {
3333
["mcpp_generated/include/nghttp2/nghttp2.h"] = [==[
3434
#pragma once
3535
#if defined(_MSC_VER)
36-
// Match the upstream CMake fallback for the legacy ssize_t API on MSVC.
37-
# include <sys/types.h>
36+
// Match the pointer-sized ssize_t used by Windows consumers such as curl.
37+
# include <BaseTsd.h>
3838
# ifndef BUILDING_NGHTTP2
3939
# pragma push_macro("ssize_t")
4040
# endif
4141
# undef ssize_t
42-
# define ssize_t int
42+
# define ssize_t SSIZE_T
4343
#endif
4444
#include <nghttp2.h>
4545
#if defined(_MSC_VER) && !defined(BUILDING_NGHTTP2)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "curl-http2-tests"
3+
version = "0.1.0"
4+
5+
[dependencies.compat]
6+
curl = { version = "8.21.0", features = ["http2"] }
7+
8+
[indices]
9+
compat = { path = "../../.." }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <curl/curl.h>
2+
3+
#include <cstdio>
4+
5+
int main() {
6+
if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) return 1;
7+
8+
const auto* info = curl_version_info(CURLVERSION_NOW);
9+
const bool http2 = info && (info->features & CURL_VERSION_HTTP2) != 0;
10+
CURL* handle = curl_easy_init();
11+
const bool configured = handle &&
12+
curl_easy_setopt(handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS) == CURLE_OK;
13+
if (handle) curl_easy_cleanup(handle);
14+
curl_global_cleanup();
15+
16+
if (!http2 || !configured) {
17+
std::fprintf(stderr, "libcurl HTTP/2 support is unavailable\n");
18+
return 2;
19+
}
20+
std::puts("libcurl reports HTTP/2 and accepts CURL_HTTP_VERSION_2TLS");
21+
return 0;
22+
}

‎tests/examples/curl/tests/transfer.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ int main() {
4848
return 3;
4949
}
5050

51+
if ((info->features & CURL_VERSION_HTTP2) != 0) {
52+
std::println("libcurl enabled HTTP/2 without the http2 feature");
53+
curl_global_cleanup();
54+
return 7;
55+
}
56+
5157
bool haveHttps = false;
5258
bool haveHttp = false;
5359
for (const char* const* p = info->protocols; p != nullptr && *p != nullptr; ++p) {

0 commit comments

Comments
 (0)