docs(http2): 添加完整的中文 Doxygen 注释

为 http2.c 所有 29 个函数添加中文文档注释:
- 压缩辅助函数(is_compressible_mime, gzip_compress, brotli_compress)
- 会话管理(init, cleanup, create, destroy, is_http2, get, set_context, upgrade)
- 数据收发(recv, send_pending, want_read, want_write)
- nghttp2 回调(send, begin_headers, header, frame_recv, stream_close, data_chunk)
- 静态文件服务(format_http_time, generate_etag, parse_http_time, match_etag,
  data_source_read, serve_directory, serve_static)
- 连接处理(on_connection_accepted)

所有注释包含功能说明、参数说明和返回值说明。
This commit is contained in:
xfy911 2026-06-04 21:07:05 +08:00
parent 606c7b6b4e
commit d0f3bb860f

281
http2.c
View File

@ -27,6 +27,15 @@
#define MAX_HTTP2_SESSIONS 1024
/* 压缩辅助函数(与 static.c 独立,保持模块边界) */
/**
* is_compressible_mime - MIME
*
* MIME
* gzip/brotli
*
* @param mime_type MIME
* @return true false
*/
static bool is_compressible_mime(const char *mime_type) {
if (!mime_type) return false;
return (
@ -39,6 +48,18 @@ static bool is_compressible_mime(const char *mime_type) {
);
}
/**
* gzip_compress - gzip
*
* 使 zlib gzip
* < 95% 0
*
* @param src
* @param src_len
* @param dst
* @param dst_cap
* @return 0 -1
*/
static ssize_t gzip_compress(const char *src, size_t src_len,
char *dst, size_t dst_cap) {
z_stream strm = {0};
@ -60,6 +81,18 @@ static ssize_t gzip_compress(const char *src, size_t src_len,
return (ssize_t)compressed_len;
}
/**
* brotli_compress - Brotli
*
* 使 Brotli
* gzip CPU
*
* @param src
* @param src_len
* @param dst
* @param dst_cap
* @return 0 -1
*/
static ssize_t brotli_compress(const char *src, size_t src_len,
char *dst, size_t dst_cap) {
size_t encoded_size = dst_cap;
@ -115,12 +148,25 @@ static void http2_serve_static(http2_session_t *h2, http2_stream_data_t *stream)
/* ===================== 会话管理 ===================== */
/**
* http2_init - HTTP/2
*
*
*
* @return 0
*/
int http2_init(void) {
memset(g_sessions, 0, sizeof(g_sessions));
g_session_count = 0;
return 0;
}
/**
* http2_cleanup - HTTP/2
*
*
*
*/
void http2_cleanup(void) {
for (int i = 0; i < MAX_HTTP2_SESSIONS; i++) {
if (g_sessions[i]) {
@ -131,6 +177,16 @@ void http2_cleanup(void) {
g_session_count = 0;
}
/**
* http2_session_create - HTTP/2
*
* fd nghttp2 SETTINGS
* fd
*
* @param fd socket
* @param tls_mode TLS
* @return NULL
*/
http2_session_t *http2_session_create(int fd, bool tls_mode) {
if (fd < 0 || fd >= MAX_HTTP2_SESSIONS) {
return NULL;
@ -192,6 +248,13 @@ http2_session_t *http2_session_create(int fd, bool tls_mode) {
return h2;
}
/**
* http2_session_destroy - HTTP/2
*
* nghttp2
*
* @param h2
*/
void http2_session_destroy(http2_session_t *h2) {
if (!h2) return;
@ -220,16 +283,38 @@ void http2_session_destroy(http2_session_t *h2) {
free(h2);
}
/**
* http2_session_is_http2 - fd HTTP/2
*
* @param fd socket
* @return true HTTP/2
*/
bool http2_session_is_http2(int fd) {
if (fd < 0 || fd >= MAX_HTTP2_SESSIONS) return false;
return g_sessions[fd] != NULL;
}
/**
* http2_session_get - fd HTTP/2
*
* @param fd socket
* @return NULL
*/
http2_session_t *http2_session_get(int fd) {
if (fd < 0 || fd >= MAX_HTTP2_SESSIONS) return NULL;
return g_sessions[fd];
}
/**
* http2_session_set_context -
*
* root_dir 使
*
* @param h2
* @param root_dir
* @param gzip_enabled gzip
* @param brotli_enabled brotli
*/
void http2_session_set_context(http2_session_t *h2, const char *root_dir, bool gzip_enabled, bool brotli_enabled) {
if (!h2) return;
h2->root_dir = root_dir;
@ -237,6 +322,16 @@ void http2_session_set_context(http2_session_t *h2, const char *root_dir, bool g
h2->brotli_enabled = brotli_enabled;
}
/**
* http2_session_upgrade - HTTP/1.1 HTTP/2
*
* nghttp2_session_upgrade2 stream 1 HTTP/1.1
* h2c Upgrade: h2c
*
* @param h2
* @param req HTTP/1.1
* @return 0 -1
*/
int http2_session_upgrade(http2_session_t *h2, const http_request_t *req) {
if (!h2 || !h2->session || !req) return -1;
@ -270,6 +365,16 @@ int http2_session_upgrade(http2_session_t *h2, const http_request_t *req) {
/* ===================== 数据收发 ===================== */
/**
* http2_recv - HTTP/2
*
* nghttp2
*
* @param h2
* @param buf
* @param len
* @return 0 -1
*/
int http2_recv(http2_session_t *h2, const uint8_t *buf, size_t len) {
if (!h2 || !h2->session) return -1;
@ -283,6 +388,14 @@ int http2_recv(http2_session_t *h2, const uint8_t *buf, size_t len) {
return http2_send_pending(h2);
}
/**
* http2_send_pending - HTTP/2
*
* nghttp2_session_send
*
* @param h2
* @return 0 -1
*/
int http2_send_pending(http2_session_t *h2) {
if (!h2 || !h2->session) return -1;
@ -294,11 +407,23 @@ int http2_send_pending(http2_session_t *h2) {
return 0;
}
/**
* http2_want_read -
*
* @param h2
* @return true
*/
bool http2_want_read(http2_session_t *h2) {
if (!h2 || !h2->session) return false;
return nghttp2_session_want_read(h2->session) != 0;
}
/**
* http2_want_write -
*
* @param h2
* @return true
*/
bool http2_want_write(http2_session_t *h2) {
if (!h2 || !h2->session) return false;
return nghttp2_session_want_write(h2->session) != 0;
@ -306,6 +431,19 @@ bool http2_want_write(http2_session_t *h2) {
/* ===================== nghttp2 回调 ===================== */
/**
* send_callback - nghttp2
*
* nghttp2 TLS socket
* nghttp2 cocoon I/O
*
* @param session nghttp2 使
* @param data
* @param length
* @param flags 使
* @param user_data http2_session_t
* @return NGHTTP2_ERR_CALLBACK_FAILURE
*/
static ssize_t send_callback(nghttp2_session *session __attribute__((unused)),
const uint8_t *data, size_t length,
int flags __attribute__((unused)),
@ -329,6 +467,17 @@ static ssize_t send_callback(nghttp2_session *session __attribute__((unused)),
return (int)sent;
}
/**
* on_begin_headers_callback -
*
* nghttp2 HEADERS http2_stream_data_t
* nghttp2
*
* @param session nghttp2
* @param frame
* @param user_data
* @return 0 NGHTTP2_ERR_CALLBACK_FAILURE
*/
static int on_begin_headers_callback(nghttp2_session *session __attribute__((unused)),
const nghttp2_frame *frame,
void *user_data) {
@ -358,6 +507,22 @@ static int on_begin_headers_callback(nghttp2_session *session __attribute__((unu
return 0;
}
/**
* on_header_callback -
*
* HTTP/2 :path, :method
* If-None-Match, If-Modified-Since
*
* @param session nghttp2
* @param frame
* @param name
* @param namelen
* @param value
* @param valuelen
* @param flags 使
* @param user_data 使
* @return 0
*/
static int on_header_callback(nghttp2_session *session,
const nghttp2_frame *frame, const uint8_t *name,
size_t namelen, const uint8_t *value,
@ -438,6 +603,16 @@ static int on_header_callback(nghttp2_session *session,
return 0;
}
/**
* on_frame_recv_callback -
*
* END_STREAM
*
* @param session nghttp2
* @param frame
* @param user_data
* @return 0
*/
static int on_frame_recv_callback(nghttp2_session *session,
const nghttp2_frame *frame, void *user_data) {
http2_session_t *h2 = (http2_session_t *)user_data;
@ -466,6 +641,17 @@ static int on_frame_recv_callback(nghttp2_session *session,
return 0;
}
/**
* on_stream_close_callback -
*
* body
*
* @param session nghttp2 使
* @param stream_id ID
* @param error_code 使
* @param user_data
* @return 0
*/
static int on_stream_close_callback(nghttp2_session *session __attribute__((unused)),
int32_t stream_id, uint32_t error_code __attribute__((unused)),
void *user_data) {
@ -492,6 +678,19 @@ static int on_stream_close_callback(nghttp2_session *session __attribute__((unus
return 0;
}
/**
* on_data_chunk_recv_callback - DATA
*
* TODO
*
* @param session nghttp2 使
* @param flags 使
* @param stream_id ID
* @param data
* @param len
* @param user_data 使
* @return 0
*/
static int on_data_chunk_recv_callback(nghttp2_session *session __attribute__((unused)),
uint8_t flags __attribute__((unused)),
int32_t stream_id, const uint8_t *data,
@ -511,6 +710,15 @@ static int on_data_chunk_recv_callback(nghttp2_session *session __attribute__((u
/* ===================== HTTP/2 静态文件服务 ===================== */
/**
* format_http_time - HTTP
*
* time_t "Mon, 01 Jan 2000 00:00:00 GMT"
*
* @param t
* @param buf
* @param buf_size
*/
static void format_http_time(time_t t, char *buf, size_t buf_size) {
struct tm *gmt = gmtime(&t);
if (gmt) {
@ -520,10 +728,27 @@ static void format_http_time(time_t t, char *buf, size_t buf_size) {
}
}
/**
* generate_etag - ETag
*
* ETag
*
* @param st
* @param buf
* @param buf_size
*/
static void generate_etag(const struct stat *st, char *buf, size_t buf_size) {
snprintf(buf, buf_size, "\"%lx-%lx\"", (unsigned long)st->st_size, (unsigned long)st->st_mtime);
}
/**
* parse_http_time - HTTP
*
* RFC 1123RFC 850ANSI C's asctime()
*
* @param str
* @return -1
*/
static time_t parse_http_time(const char *str) {
struct tm tm = {0};
if (strptime(str, "%a, %d %b %Y %H:%M:%S GMT", &tm) != NULL ||
@ -534,6 +759,15 @@ static time_t parse_http_time(const char *str) {
return -1;
}
/**
* match_etag - ETag
*
* "*" W/
*
* @param etag ETag
* @param if_none_match If-None-Match
* @return true
*/
static bool match_etag(const char *etag, const char *if_none_match) {
if (!etag || !if_none_match) return false;
if (strcmp(if_none_match, "*") == 0) return true;
@ -542,6 +776,21 @@ static bool match_etag(const char *etag, const char *if_none_match) {
return strcmp(client, etag) == 0;
}
/**
* http2_data_source_read_callback - nghttp2
*
* response_body nghttp2 DATA
* EOF
*
* @param session nghttp2 使
* @param stream_id ID使
* @param buf
* @param length
* @param data_flags EOF
* @param source stream
* @param user_data 使
* @return
*/
static ssize_t http2_data_source_read_callback(
nghttp2_session *session __attribute__((unused)),
int32_t stream_id __attribute__((unused)),
@ -572,6 +821,18 @@ static ssize_t http2_data_source_read_callback(
return (ssize_t)to_send;
}
/**
* http2_serve_directory - HTTP/2
*
* HTML nghttp2
* HTML
*
* @param h2
* @param stream
* @param real_path
* @param request_path
* @return true false
*/
static bool http2_serve_directory(http2_session_t *h2, http2_stream_data_t *stream,
const char *real_path, const char *request_path) {
struct stat st;
@ -743,6 +1004,16 @@ static bool http2_serve_directory(http2_session_t *h2, http2_stream_data_t *stre
return true;
}
/**
* http2_serve_static - HTTP/2
*
* index.html 304
* brotli/gzip
* HEAD body
*
* @param h2
* @param stream
*/
static void http2_serve_static(http2_session_t *h2, http2_stream_data_t *stream) {
if (!h2->root_dir) {
nghttp2_nv hdrs[] = {
@ -972,6 +1243,16 @@ static void http2_serve_static(http2_session_t *h2, http2_stream_data_t *stream)
/* ===================== 连接处理 ===================== */
/**
* http2_on_connection_accepted - HTTP/2
*
* TLS HTTP/2
* 0 server.c PRI
*
* @param fd socket
* @param tls_mode TLS
* @return 0 -1
*/
int http2_on_connection_accepted(int fd, bool tls_mode) {
/* 检查是否应启用 HTTP/2 */
if (!tls_mode) {