Skip to content

Commit

Permalink
Optimize objects' size in acl_cpp module.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Mar 20, 2023
1 parent 60ef870 commit 639e1af
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 74 deletions.
21 changes: 10 additions & 11 deletions lib_acl_cpp/include/acl_cpp/http/HttpServlet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class HttpServletResponse;
/**
* 处理 HTTP 客户端请求的基类,子类需要继承该类
*/
class ACL_CPP_API HttpServlet : public noncopyable
{
class ACL_CPP_API HttpServlet : public noncopyable {
public:
/**
* 构造函数
Expand All @@ -27,17 +26,18 @@ class ACL_CPP_API HttpServlet : public noncopyable
* @param session {session*} 每一个 HttpServlet 对象一个 session 对象
*/
HttpServlet(socket_stream* stream, session* session);
HttpServlet(socket_stream* stream);

/**
* 构造函数
* 构造函数(该函数已经废弃,请用其它构造方法)
* @param stream {socket_stream*} 当在 acl_master 服务器框架控制下
* 运行时,该参数必须非空;当在 apache 下以 CGI 方式运行时,该参数
* 设为 NULL;另外,该函数内部不会关闭流连接,应用应自行处理流对象
* 的关闭情况,这样可以方便与 acl_master 架构结合
* @param memcache_addr {const char*}
*/
HttpServlet(socket_stream* stream,
const char* memcache_addr = "127.0.0.1|11211");
//@ACL_DEPRECATED
HttpServlet(socket_stream* stream, const char* memcache_addr);

HttpServlet(void);
virtual ~HttpServlet(void) = 0;
Expand Down Expand Up @@ -210,13 +210,12 @@ class ACL_CPP_API HttpServlet : public noncopyable

private:
session* session_;
session* session_ptr_;
socket_stream* stream_;
bool first_;
char local_charset_[32];
int rw_timeout_;
int parse_body_limit_;
bool try_old_ws_;
bool first_;
char* local_charset_;
int rw_timeout_;
int parse_body_limit_;
bool try_old_ws_;

void init();
};
Expand Down
17 changes: 8 additions & 9 deletions lib_acl_cpp/include/acl_cpp/http/HttpServletRequest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,20 @@ class HttpServletResponse;
* 与 HTTP 客户端请求相关的类,该类不应被继承,用户也不需要
* 定义或创建该类对象
*/
class ACL_CPP_API HttpServletRequest : public noncopyable
{
class ACL_CPP_API HttpServletRequest : public noncopyable {
public:
/**
* 构造函数
* @param res {HttpServletResponse&}
* @param store {session&} 存储会话数据的对象
* @param sess {session*} 存储会话数据的对象
* @param stream {socket_stream&} 数据流,内部不会主动关闭流
* @param charset {const char*} 本地字符集,该值非空时,
* 内部会自动将 HTTP 请求的数据转换为本地字符集,否则不转换
* @param body_limit {int} 针对 POST 方法,当数据体为文本参数
* 类型时,此参数限制数据体的长度;当数据体为数据流或 MIME
* 格式或 on 为 false,此参数无效
*/
HttpServletRequest(HttpServletResponse& res, session& store,
HttpServletRequest(HttpServletResponse& res, session* sess,
socket_stream& stream, const char* charset = NULL,
int body_limit = 102400);
~HttpServletRequest(void);
Expand Down Expand Up @@ -401,9 +400,9 @@ class ACL_CPP_API HttpServletRequest : public noncopyable
dbuf_guard* dbuf_internal_;
dbuf_guard* dbuf_;
http_request_error_t req_error_;
char cookie_name_[64];
char* cookie_name_;
HttpServletResponse& res_;
session& store_;
session* sess_;
HttpSession* http_session_;
socket_stream& stream_;
int body_limit_;
Expand All @@ -415,9 +414,9 @@ class ACL_CPP_API HttpServletRequest : public noncopyable
http_method_t method_;
bool cgi_mode_;
http_ctype content_type_;
char localAddr_[32];
char remoteAddr_[32];
char localCharset_[32];
char* localAddr_;
char* remoteAddr_;
char* localCharset_;
int rw_timeout_;
std::vector<HTTP_PARAM*> params_;
http_request_t request_type_;
Expand Down
3 changes: 1 addition & 2 deletions lib_acl_cpp/include/acl_cpp/http/HttpServletResponse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class HttpServletRequest;
* 与 HTTP 客户端响应相关的类,该类不应被继承,用户也不需要
* 定义或创建该类对象
*/
class ACL_CPP_API HttpServletResponse : public noncopyable
{
class ACL_CPP_API HttpServletResponse : public noncopyable {
public:
/**
* 构造函数
Expand Down
73 changes: 38 additions & 35 deletions lib_acl_cpp/src/http/HttpServlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,75 @@

#ifndef ACL_CLIENT_ONLY

namespace acl
namespace acl {

void HttpServlet::init(void)
{
first_ = true;
local_charset_ = NULL;
rw_timeout_ = 60;
parse_body_limit_ = 0;
}

HttpServlet::HttpServlet(socket_stream* stream, session* session)
: req_(NULL)
, res_(NULL)
, parse_body_(true)
, stream_(stream)
HttpServlet::HttpServlet(void)
{
init();

if (session == NULL) {
session_ = NEW memcache_session("127.0.0.1|11211");
session_ptr_ = session_;
} else {
session_ = session;
session_ptr_ = NULL;
}
req_ = NULL;
res_ = NULL;
stream_ = NULL;
session_ = NULL;
}

HttpServlet::HttpServlet(socket_stream* stream,
const char* memcache_addr /* = "127.0.0.1:11211" */)
HttpServlet::HttpServlet(socket_stream* stream)
: req_(NULL)
, res_(NULL)
, session_(NULL)
, stream_(stream)
{
init();

session_ = NEW memcache_session(memcache_addr);
session_ptr_ = session_;
}

HttpServlet::HttpServlet()
HttpServlet::HttpServlet(socket_stream* stream, session* session)
: req_(NULL)
, res_(NULL)
, parse_body_(true)
, session_(session)
, stream_(stream)
{
init();

req_ = NULL;
res_ = NULL;
stream_ = NULL;
session_ = NULL;
session_ptr_ = NULL;
}

void HttpServlet::init()
HttpServlet::HttpServlet(socket_stream* stream, const char*)
: req_(NULL)
, res_(NULL)
, session_(NULL)
, stream_(stream)
{
first_ = true;
local_charset_[0] = 0;
rw_timeout_ = 60;
parse_body_limit_ = 0;
init();
}

HttpServlet::~HttpServlet(void)
{
if (local_charset_) {
acl_myfree(local_charset_);
}
delete req_;
delete res_;
delete session_ptr_;
}

#define COPY(x, y) ACL_SAFE_STRNCPY((x), (y), sizeof((x)))

HttpServlet& HttpServlet::setLocalCharset(const char* charset)
{
if (charset && *charset) {
COPY(local_charset_, charset);
} else {
local_charset_[0] =0;
if (local_charset_) {
acl_myfree(local_charset_);
}
local_charset_ = acl_mystrdup(charset);
} else if (local_charset_) {
acl_myfree(local_charset_);
local_charset_ = NULL;
}
return *this;
}
Expand Down Expand Up @@ -161,7 +164,7 @@ bool HttpServlet::start(void)
delete res_;

res_ = NEW HttpServletResponse(*out);
req_ = NEW HttpServletRequest(*res_, *session_, *in, local_charset_,
req_ = NEW HttpServletRequest(*res_, session_, *in, local_charset_,
parse_body_limit_);
req_->setParseBody(parse_body_);

Expand Down
48 changes: 33 additions & 15 deletions lib_acl_cpp/src/http/HttpServletRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,27 @@
#ifndef ACL_CLIENT_ONLY

#define SKIP_SPACE(x) { while (*x == ' ' || *x == '\t') x++; }
#define ADDR_LEN 64

namespace acl
{
namespace acl {

#define COPY(x, y) ACL_SAFE_STRNCPY((x), (y), sizeof((x)))

HttpServletRequest::HttpServletRequest(HttpServletResponse& res,
session& store, socket_stream& stream,
session* sess, socket_stream& stream,
const char* charset /* = NULL */, int body_limit /* = 102400 */)
: req_error_(HTTP_REQ_OK)
, res_(res)
, store_(store)
, sess_(sess)
, http_session_(NULL)
, stream_(stream)
, body_limit_(body_limit)
, body_parsed_(false)
, cookies_inited_(false)
, client_(NULL)
, method_(HTTP_METHOD_UNKNOWN)
, localAddr_(NULL)
, remoteAddr_(NULL)
, request_type_(HTTP_REQUEST_NORMAL)
, parse_body_(true)
, mime_(NULL)
Expand All @@ -53,17 +55,17 @@ HttpServletRequest::HttpServletRequest(HttpServletResponse& res,
dbuf_internal_ = NEW dbuf_guard(1, 100);
dbuf_ = dbuf_internal_;

COPY(cookie_name_, "ACL_SESSION_ID");
cookie_name_ = dbuf_->dbuf_strdup("ACL_SESSION_ID");
ACL_VSTREAM* in = stream.get_vstream();
if (in == ACL_VSTREAM_IN) {
cgi_mode_ = true;
} else {
cgi_mode_ = false;
}
if (charset && *charset) {
COPY(localCharset_, charset);
localCharset_ = dbuf_->dbuf_strdup(charset);
} else {
localCharset_[0] = 0;
localCharset_ = NULL;
}
rw_timeout_ = 60;
}
Expand Down Expand Up @@ -259,21 +261,25 @@ HttpSession& HttpServletRequest::getSession(bool create /* = true */,
return *http_session_;
}

http_session_ = dbuf_->create<HttpSession, session&>(store_);
if (sess_ == NULL) {
sess_ = dbuf_->create<memcache_session>("127.0.0.1|11211");
}

http_session_ = dbuf_->create<HttpSession, session&>(*sess_);
const char* sid;

if ((sid = getCookieValue(cookie_name_)) != NULL) {
store_.set_sid(sid);
sess_->set_sid(sid);
} else if (create) {
// 获得唯一 ID 标识符
sid = store_.get_sid();
sid = sess_->get_sid();
// 生成 cookie 对象,并分别向请求对象和响应对象添加 cookie
HttpCookie* cookie = dbuf_->create<HttpCookie, const char*,
const char*, dbuf_guard*>(cookie_name_, sid, dbuf_);
res_.addCookie(cookie);
setCookie(cookie_name_, sid);
} else if (sid_in != NULL && *sid_in != 0) {
store_.set_sid(sid_in);
sess_->set_sid(sid_in);
// 生成 cookie 对象,并分别向请求对象和响应对象添加 cookie
HttpCookie* cookie = dbuf_->create<HttpCookie, const char*,
const char*, dbuf_guard*>(cookie_name_, sid_in, dbuf_);
Expand Down Expand Up @@ -338,7 +344,7 @@ const char* HttpServletRequest::getCharacterEncoding(void) const

const char* HttpServletRequest::getLocalCharset(void) const
{
return localCharset_[0] ? localCharset_ : NULL;
return localCharset_ ? localCharset_ : NULL;
}

const char* HttpServletRequest::getLocalAddr(void) const
Expand All @@ -354,8 +360,14 @@ const char* HttpServletRequest::getLocalAddr(void) const
if (*ptr == 0) {
return NULL;
}


if (localAddr_ == NULL) {
const_cast<HttpServletRequest*>(this)->localAddr_ = (char*)
dbuf_->dbuf_alloc(ADDR_LEN);
}
safe_snprintf(const_cast<HttpServletRequest*>(this)->localAddr_,
sizeof(localAddr_), "%s", ptr);
ADDR_LEN, "%s", ptr);
char* p = (char*) strchr(localAddr_, ':');
if (p) {
*p = 0;
Expand Down Expand Up @@ -404,8 +416,14 @@ const char* HttpServletRequest::getRemoteAddr(void) const
logger_warn("get_peer return empty string");
return NULL;
}

if (remoteAddr_ == NULL) {
const_cast<HttpServletRequest*>(this)->remoteAddr_ = (char*)
dbuf_->dbuf_alloc(ADDR_LEN);
}

safe_snprintf(const_cast<HttpServletRequest*>(this)->remoteAddr_,
sizeof(remoteAddr_), "%s", ptr);
ADDR_LEN, "%s", ptr);
char* p = (char*) strchr(remoteAddr_, ':');
if (p) {
*p = 0;
Expand Down Expand Up @@ -671,7 +689,7 @@ void HttpServletRequest::parseParameters(const char* str)
HTTP_PARAM* param = (HTTP_PARAM*)
dbuf_->dbuf_calloc(sizeof(HTTP_PARAM));

if (localCharset_[0] != 0 && requestCharset
if (localCharset_ && requestCharset
&& strcasecmp(requestCharset, localCharset_)) {

buf.clear();
Expand Down
3 changes: 1 addition & 2 deletions lib_acl_cpp/src/http/HttpServletResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

#ifndef ACL_CLIENT_ONLY

namespace acl
{
namespace acl {

HttpServletResponse::HttpServletResponse(socket_stream& stream)
: stream_(stream)
Expand Down

0 comments on commit 639e1af

Please sign in to comment.