From 612bced38d2045468c825ae0aa2bf7658d2bd138 Mon Sep 17 00:00:00 2001 From: zsx Date: Thu, 28 Dec 2017 17:53:34 +0800 Subject: [PATCH] http_header::add_entry: add param replace for checking if replade the old exist value --- lib_acl_cpp/include/acl_cpp/http/http_header.hpp | 4 +++- lib_acl_cpp/src/http/http_header.cpp | 16 ++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib_acl_cpp/include/acl_cpp/http/http_header.hpp b/lib_acl_cpp/include/acl_cpp/http/http_header.hpp index 6427cba25..dd8b149ef 100644 --- a/lib_acl_cpp/include/acl_cpp/http/http_header.hpp +++ b/lib_acl_cpp/include/acl_cpp/http/http_header.hpp @@ -70,9 +70,11 @@ public: * 向 HTTP 头中添加字段 * @param name {const char*} 字段名,非空指针 * @param value {const char*} 字段值,非空指针 + * @param replace {bool} 如果存在重复项时是否自动覆盖旧数据 * @return {http_header&} 返回本对象的引用,便于用户连续操作 */ - http_header& add_entry(const char* name, const char* value); + http_header& add_entry(const char* name, const char* value, + bool replace = true); /** * 设置 HTTP 头中的 Content-Length 字段 diff --git a/lib_acl_cpp/src/http/http_header.cpp b/lib_acl_cpp/src/http/http_header.cpp index e12383899..1ae423d27 100644 --- a/lib_acl_cpp/src/http/http_header.cpp +++ b/lib_acl_cpp/src/http/http_header.cpp @@ -122,18 +122,22 @@ http_header& http_header::set_request_mode(bool onoff) return *this; } -http_header& http_header::add_entry(const char* name, const char* value) +http_header& http_header::add_entry(const char* name, const char* value, + bool replace /* = true */) { if (name == NULL || *name == 0 || value == NULL || *value == 0) return *this; - std::list::iterator it = entries_.begin(); - for (; it != entries_.end(); ++it) + if (replace) { - if (strcasecmp((*it)->name, name) == 0) + std::list::iterator it = entries_.begin(); + for (; it != entries_.end(); ++it) { - (*it)->value = dbuf_->dbuf_strdup(value); - return *this; + if (strcasecmp((*it)->name, name) == 0) + { + (*it)->value = dbuf_->dbuf_strdup(value); + return *this; + } } }