From fd34b5526b84c44705ea6f65e3f2b2a86d9846ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E9=94=8B?= <40498300+xfxhn@users.noreply.github.com> Date: Mon, 30 Jun 2025 20:46:18 +0800 Subject: [PATCH] =?UTF-8?q?feature:=E6=B7=BB=E5=8A=A0http=20head=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E6=94=AF=E6=8C=81=20(#4321)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加http head请求的支持 --- src/Http/HttpClient.cpp | 2 +- tests/test_http_head.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/test_http_head.cpp diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index a3651064..ea39c826 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -264,7 +264,7 @@ ssize_t HttpClient::onRecvHeader(const char *data, size_t len) { _total_body_size = -1; } - if (_total_body_size == 0) { + if (_total_body_size == 0 || _method == "HEAD") { // 后续没content,本次http请求结束 [AUTO-TRANSLATED:8532172f] // There is no content afterwards, this http request ends onResponseCompleted_l(SockException(Err_success, "The request is successful but has no body")); diff --git a/tests/test_http_head.cpp b/tests/test_http_head.cpp new file mode 100644 index 00000000..385bc372 --- /dev/null +++ b/tests/test_http_head.cpp @@ -0,0 +1,27 @@ +#include "Http/HttpRequester.h" + +int main() { + auto requester = std::make_shared(); + requester->setMethod("HEAD"); + + requester->startRequester( + "http://baidu.com", + + [](const toolkit::SockException &ex, const mediakit::Parser &parser) { + if (ex) { + PrintI("HEAD请求失败: %s", ex.what()); + return; + } + + // 检查HTTP状态码 + if (parser.status() != "200") { + PrintI("HEAD请求返回错误状态: %s", parser.status().c_str()); + return; + } + for (auto &header : parser.getHeader()) { + PrintI("key=%s, val=%s", header.first.c_str(), header.second.c_str()); + } + }); + getchar(); + return 0; +} \ No newline at end of file