Avoid HLS cookies retaining HTTP sockets while preserving Session info (#4841)

This commit is contained in:
YuLi
2026-09-30 20:37:51 +08:00
committed by GitHub
parent de040511d2
commit 57f206e726
3 changed files with 134 additions and 10 deletions

View File

@@ -18,7 +18,15 @@ namespace mediakit {
class SockInfoImp : public Session {
public:
using Ptr = std::shared_ptr<SockInfoImp>;
SockInfoImp(const Socket::Ptr &sock) : Session(sock) {}
SockInfoImp(const std::shared_ptr<Session> &session) : Session(session->getSock()) {
_identifier = session->getIdentifier();
_peer_ip = session->get_peer_ip();
_peer_port = session->get_peer_port();
_local_ip = session->get_local_ip();
_local_port = session->get_local_port();
// Keep the Session interface without retaining the HTTP connection.
setSock(nullptr);
}
std::string get_local_ip() override { return _local_ip; }
@@ -30,6 +38,7 @@ public:
std::string getIdentifier() const override { return _identifier; }
private:
void onRecv(const Buffer::Ptr &buf) override {}
void onError(const SockException &err) override {}
void onManager() override {}
@@ -37,19 +46,13 @@ public:
std::string _local_ip;
std::string _peer_ip;
std::string _identifier;
uint16_t _local_port;
uint16_t _peer_port;
uint16_t _local_port = 0;
uint16_t _peer_port = 0;
};
HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<Session> &session) {
_info = info;
auto sock_info = std::make_shared<SockInfoImp>(session->getSock());
sock_info->_identifier = session->getIdentifier();
sock_info->_peer_ip = session->get_peer_ip();
sock_info->_peer_port = session->get_peer_port();
sock_info->_local_ip = session->get_local_ip();
sock_info->_local_port = session->get_local_port();
_sock_info = sock_info;
_sock_info = std::make_shared<SockInfoImp>(session);
_added = std::make_shared<bool>(false);
addReaderCount();
}

View File

@@ -41,6 +41,7 @@ find_package(PCAP QUIET)
# test_webrtc_regression are built only with WebRTC enabled and are not verified yet.
set(GATING_TESTS
test_amf
test_hls_cookie_data
test_sortor)
foreach(TEST_SRC ${TEST_SRC_LIST})

View File

@@ -0,0 +1,120 @@
/*
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#include <iostream>
#include <memory>
#include "Network/Session.h"
#include "Record/HlsMediaSource.h"
using namespace mediakit;
using namespace toolkit;
namespace {
class TestSession : public Session {
public:
explicit TestSession(const Socket::Ptr &sock) : Session(sock) {}
std::string get_local_ip() override { return "192.0.2.1"; }
uint16_t get_local_port() override { return 8080; }
std::string get_peer_ip() override { return "198.51.100.1"; }
uint16_t get_peer_port() override { return 54321; }
std::string getIdentifier() const override { return "hls-cookie-test"; }
void onRecv(const Buffer::Ptr &buf) override {}
void onError(const SockException &err) override {}
void onManager() override {}
};
bool testCookieDoesNotOwnSocket() {
auto socket = Socket::createSocket();
std::weak_ptr<Socket> weak_socket = socket;
auto session = std::make_shared<TestSession>(socket);
socket.reset();
auto cookie_data = std::make_shared<HlsCookieData>(MediaInfo(), session);
session.reset();
// HLS cookie metadata may outlive the HTTP session, but must not extend the
// lifetime of its network socket. Retaining it delays descriptor closure.
if (!weak_socket.expired()) {
std::cerr << "[FAIL] HlsCookieData retained the HTTP session socket" << std::endl;
return false;
}
std::cout << "[ OK ] HlsCookieData retains only copied socket metadata" << std::endl;
return true;
}
bool testPlayerInfoPreservesSession() {
auto poller = EventPollerPool::Instance().getPoller();
auto socket = Socket::createSocket(poller);
std::weak_ptr<Socket> weak_socket = socket;
auto session = std::make_shared<TestSession>(socket);
socket.reset();
MediaInfo info("hls://127.0.0.1/test/cookie-player-info");
auto source = std::make_shared<HlsMediaSource>(info.schema, info);
source->setIndexFile("#EXTM3U\n");
auto cookie_data = std::make_shared<HlsCookieData>(info, session);
cookie_data->setMediaSource(source);
cookie_data->addByteUsage(1);
session.reset();
std::list<Any> players;
bool called = false;
source->getPlayerList([&](const std::list<Any> &info_list) {
players = info_list;
called = true;
}, [](Any &&player) { return std::move(player); });
// The reader and query run on the same poller, so the callback is synchronous.
if (!called || players.size() != 1 || !players.front().is<Session>()) {
std::cerr << "[FAIL] HLS player info must remain Any<Session>" << std::endl;
return false;
}
auto &player = players.front().get<Session>();
if (player.getSock() || !weak_socket.expired()) {
std::cerr << "[FAIL] HLS player metadata retained the HTTP socket" << std::endl;
return false;
}
if (player.getIdentifier() != "hls-cookie-test" || player.get_local_ip() != "192.0.2.1"
|| player.get_local_port() != 8080 || player.get_peer_ip() != "198.51.100.1"
|| player.get_peer_port() != 54321) {
std::cerr << "[FAIL] HLS player metadata changed after HTTP session destruction" << std::endl;
return false;
}
if (player.getPoller() != poller) {
std::cerr << "[FAIL] HLS player metadata lost its poller" << std::endl;
return false;
}
std::cout << "[ OK ] HLS player info preserves Session and copied metadata without a socket" << std::endl;
return true;
}
} // namespace
int main() {
EventPollerPool::setPoolSize(1);
bool passed = false;
EventPollerPool::Instance().getPoller()->sync([&]() {
try {
bool ownership = testCookieDoesNotOwnSocket();
bool player_info = testPlayerInfoPreservesSession();
passed = ownership && player_info;
} catch (const std::exception &ex) {
std::cerr << "[FAIL] " << ex.what() << std::endl;
}
});
return passed ? 0 : 1;
}