From: Francesco Chemolli Date: Sun, 4 Dec 2011 13:52:07 +0000 (+0100) Subject: c++-refactor HttpBody X-Git-Tag: BumpSslServerFirst.take05~12^2~135 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0521f8be24b99ce4d7bf8f52bbbd54c81aa673ec;p=thirdparty%2Fsquid.git c++-refactor HttpBody --- diff --git a/src/HttpBody.cc b/src/HttpBody.cc index cbd03c6c9c..230698a83c 100644 --- a/src/HttpBody.cc +++ b/src/HttpBody.cc @@ -34,43 +34,44 @@ */ #include "squid.h" +#include "HttpBody.h" #include "MemBuf.h" -void -httpBodyInit(HttpBody * body) +HttpBody::HttpBody() : mb(new MemBuf) +{} + +HttpBody::~HttpBody() { - body->mb = new MemBuf; + clear(); + delete mb; } void -httpBodyClean(HttpBody * body) +HttpBody::clear() { - assert(body); - - if (!body->mb->isNull()) - body->mb->clean(); - - delete body->mb; - - body->mb = NULL; + if(!mb->isNull()) + mb->clean(); } /* set body by absorbing mb */ void -httpBodySet(HttpBody * body, MemBuf * mb) +HttpBody::setMb(MemBuf * mb_) { - assert(body); - assert(body->mb->isNull()); - delete body->mb; - body->mb = mb; /* absorb */ + clear(); + delete mb; + /* note: protection against assign-to-self is not needed + * as MemBuf doesn't have a copy-constructor. If such a constructor + * is ever added, add such protection here. + */ + mb = mb_; /* absorb */ } void -httpBodyPackInto(const HttpBody * body, Packer * p) +HttpBody::packInto(Packer * p) const { - assert(body && p); + assert(p); - if (body->mb->contentSize()) - packerAppend(p, body->mb->content(), body->mb->contentSize()); + if (mb->contentSize()) + packerAppend(p, mb->content(), mb->contentSize()); } diff --git a/src/HttpBody.h b/src/HttpBody.h new file mode 100644 index 0000000000..b893a2e2d2 --- /dev/null +++ b/src/HttpBody.h @@ -0,0 +1,77 @@ +/* + * SQUID Web Proxy Cache http://www.squid-cache.org/ + * ---------------------------------------------------------- + * + * Squid is the result of efforts by numerous individuals from + * the Internet community; see the CONTRIBUTORS file for full + * details. Many organizations have provided support for Squid's + * development; see the SPONSORS file for full details. Squid is + * Copyrighted (C) 2001 by the Regents of the University of + * California; see the COPYRIGHT file for full details. Squid + * incorporates software developed and/or copyrighted by other + * sources; see the CREDITS file for full details. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + * Author: kinkie + * + */ + +#ifndef HTTPBODY_H_ +#define HTTPBODY_H_ + +#include "MemBuf.h" +class Packer; + +/** Representation of a short predetermined message + * + * This class is useful to represent short HTTP messages, whose + * contents are known in advance, e.g. error messages + */ +class HttpBody { +public: + HttpBody(); + ~HttpBody(); + /** absorb the MemBuf, discarding anything currently stored + * + * After this call the lifetime of the passed MemBuf is managed + * by the HttpBody. + */ + void setMb(MemBuf *); + /** output the HttpBody contents into the supplied packer + * + * \note content is not cleared by the output operation + */ + void packInto(Packer *) const; + + /// clear the HttpBody content + void clear(); + + /// \return true if there is any content in the HttpBody + bool hasContent() const { return (mb->contentSize()>0); } + + /// \return size of the HttpBody's message content + mb_size_t contentSize() const { return mb->contentSize(); } + + /// \return pointer to the storage of the HttpBody + char *content() const { return mb->content(); } +private: + HttpBody& operator=(const HttpBody&); //not implemented + HttpBody(const HttpBody&); // not implemented + MemBuf *mb; +}; + + +#endif /* HTTPBODY_H_ */ diff --git a/src/HttpReply.cc b/src/HttpReply.cc index e2506b7842..79d314430a 100644 --- a/src/HttpReply.cc +++ b/src/HttpReply.cc @@ -36,6 +36,7 @@ #include "squid.h" #include "SquidTime.h" #include "Store.h" +#include "HttpBody.h" #include "HttpReply.h" #include "HttpHdrContRange.h" #include "HttpHdrCc.h" @@ -94,7 +95,6 @@ HttpReply::~HttpReply() void HttpReply::init() { - httpBodyInit(&body); hdrCacheInit(); httpStatusLineInit(&sline); pstate = psReadyToParseStartLine; @@ -121,7 +121,7 @@ HttpReply::clean() // points to a pipe that is owned and initiated by another object. body_pipe = NULL; - httpBodyClean(&body); + body.clear(); hdrCacheClean(); header.clean(); httpStatusLineClean(&sline); @@ -140,7 +140,7 @@ void HttpReply::packInto(Packer * p) { packHeadersInto(p); - httpBodyPackInto(&body, p); + body.packInto(p); } /* create memBuf, create mem-based packer, pack, destroy packer, return MemBuf */ diff --git a/src/HttpReply.h b/src/HttpReply.h index d44be465ec..2ad8427df1 100644 --- a/src/HttpReply.h +++ b/src/HttpReply.h @@ -32,6 +32,7 @@ #ifndef SQUID_HTTPREPLY_H #define SQUID_HTTPREPLY_H +#include "HttpBody.h" #include "HttpMsg.h" #include "HttpStatusLine.h" diff --git a/src/Makefile.am b/src/Makefile.am index 717fb53e34..a981692874 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -358,6 +358,7 @@ squid_SOURCES = \ HttpHeaderMask.h \ HttpHeaderRange.h \ HttpHeaderTools.cc \ + HttpBody.h \ HttpBody.cc \ HttpControlMsg.h \ HttpMsg.cc \ @@ -1042,6 +1043,7 @@ tests_testHttpReply_SOURCES=\ cbdata.cc \ cbdata.h \ ETag.cc \ + HttpBody.h \ HttpBody.cc \ HttpHdrCc.h \ HttpHdrCc.cc \ @@ -1307,6 +1309,7 @@ tests_testCacheManager_SOURCES = \ HelperChildConfig.cc \ $(HTCPSOURCE) \ http.cc \ + HttpBody.h \ HttpBody.cc \ HttpHeader.cc \ HttpHeaderTools.cc \ @@ -1447,6 +1450,7 @@ tests_testDiskIO_SOURCES = \ event.cc \ fd.cc \ filemap.cc \ + HttpBody.h \ HttpBody.cc \ HttpHdrCc.h \ HttpHdrCc.cc \ @@ -1623,6 +1627,7 @@ tests_testEvent_SOURCES = \ hier_code.h \ $(HTCPSOURCE) \ http.cc \ + HttpBody.h \ HttpBody.cc \ HttpHeader.cc \ HttpHeaderTools.cc \ @@ -1811,6 +1816,7 @@ tests_testEventLoop_SOURCES = \ hier_code.h \ $(HTCPSOURCE) \ http.cc \ + HttpBody.h \ HttpBody.cc \ HttpHeader.cc \ HttpHeaderTools.cc \ @@ -1997,6 +2003,7 @@ tests_test_http_range_SOURCES = \ hier_code.h \ $(HTCPSOURCE) \ http.cc \ + HttpBody.h \ HttpBody.cc \ HttpHdrCc.h \ HttpHdrCc.cc \ @@ -2225,6 +2232,7 @@ tests_testHttpRequest_SOURCES = \ hier_code.h \ $(HTCPSOURCE) \ http.cc \ + HttpBody.h \ HttpBody.cc \ HttpHeader.cc \ HttpHeaderTools.cc \ @@ -2414,6 +2422,8 @@ tests_testStore_SOURCES= \ tests/stub_helper.cc \ tests/stub_HelperChildConfig.cc \ tests/stub_http.cc \ + HttpBody.h \ + HttpBody.cc \ tests/stub_HttpReply.cc \ tests/stub_HttpRequest.cc \ tests/stub_libcomm.cc \ @@ -2551,6 +2561,7 @@ tests_testUfs_SOURCES = \ fd.cc \ disk.cc \ filemap.cc \ + HttpBody.h \ HttpBody.cc \ HttpReply.cc \ HttpStatusLine.cc \ @@ -2680,6 +2691,7 @@ tests_testRock_SOURCES = \ event.cc \ fd.cc \ filemap.cc \ + HttpBody.h \ HttpBody.cc \ HttpHdrCc.cc \ HttpHdrContRange.cc \ @@ -2809,6 +2821,7 @@ tests_testCoss_SOURCES = \ fd.cc \ disk.cc \ filemap.cc \ + HttpBody.h \ HttpBody.cc \ HttpReply.cc \ HttpStatusLine.cc \ @@ -2940,6 +2953,7 @@ tests_testNull_SOURCES = \ fd.cc \ disk.cc \ filemap.cc \ + HttpBody.h \ HttpBody.cc \ HttpReply.cc \ HttpStatusLine.cc \ @@ -3098,6 +3112,7 @@ tests_testURL_SOURCES = \ hier_code.h \ $(HTCPSOURCE) \ http.cc \ + HttpBody.h \ HttpBody.cc \ HttpHdrCc.h \ HttpHdrCc.cc \ diff --git a/src/errorpage.cc b/src/errorpage.cc index a42ca60aad..1197445c52 100644 --- a/src/errorpage.cc +++ b/src/errorpage.cc @@ -1213,7 +1213,7 @@ ErrorState::BuildHttpReply() rep->header.putStr(HDR_CONTENT_LANGUAGE, "en"); } - httpBodySet(&rep->body, content); + rep->body.setMb(content); /* do not memBufClean() or delete the content, it was absorbed by httpBody */ } diff --git a/src/esi/Esi.cc b/src/esi/Esi.cc index 0c8d78fbbb..f547703d9b 100644 --- a/src/esi/Esi.cc +++ b/src/esi/Esi.cc @@ -1465,15 +1465,15 @@ ESIContext::fail () err->err_msg = errormessage; errormessage = NULL; rep = err->BuildHttpReply(); - assert (rep->body.mb->contentSize() >= 0); - size_t errorprogress = rep->body.mb->contentSize(); + assert (rep->body.hasContent()); + size_t errorprogress = rep->body.contentSize(); /* Tell esiSend where to start sending from */ outbound_offset = 0; /* copy the membuf from the reply to outbound */ - while (errorprogress < (size_t)rep->body.mb->contentSize()) { + while (errorprogress < (size_t)rep->body.contentSize()) { appendOutboundData(new ESISegment); - errorprogress += outboundtail->append(rep->body.mb->content() + errorprogress, rep->body.mb->contentSize() - errorprogress); + errorprogress += outboundtail->append(rep->body.content() + errorprogress, rep->body.contentSize() - errorprogress); } /* the esiCode now thinks that the error is the outbound, diff --git a/src/protos.h b/src/protos.h index e39e135d96..dd38345cbd 100644 --- a/src/protos.h +++ b/src/protos.h @@ -222,18 +222,6 @@ SQUIDCEXTERN const char *httpMakeVaryMark(HttpRequest * request, HttpReply const #include "HttpStatusCode.h" SQUIDCEXTERN const char *httpStatusString(http_status status); -/* Http Body */ -/* init/clean */ -SQUIDCEXTERN void httpBodyInit(HttpBody * body); -SQUIDCEXTERN void httpBodyClean(HttpBody * body); -/* get body ptr (always use this) */ -SQUIDCEXTERN const char *httpBodyPtr(const HttpBody * body); -/* set body, does not clone mb so you should not reuse it */ -SQUIDCEXTERN void httpBodySet(HttpBody * body, MemBuf * mb); - -/* pack */ -SQUIDCEXTERN void httpBodyPackInto(const HttpBody * body, Packer * p); - /* Http Cache Control Header Field */ SQUIDCEXTERN void httpHdrCcInitModule(void); SQUIDCEXTERN void httpHdrCcCleanModule(void); diff --git a/src/store.cc b/src/store.cc index c9cfa116ba..f017ff874b 100644 --- a/src/store.cc +++ b/src/store.cc @@ -1872,7 +1872,7 @@ StoreEntry::startWriting() rep->packHeadersInto(&p); mem_obj->markEndOfReplyHeaders(); - httpBodyPackInto(&rep->body, &p); + rep->body.packInto(&p); packerClean(&p); } diff --git a/src/structs.h b/src/structs.h index 84952a2ebc..49e16e9ae1 100644 --- a/src/structs.h +++ b/src/structs.h @@ -704,18 +704,6 @@ struct _fileMap { unsigned long *file_map; }; -/* - * Note: HttpBody is used only for messages with a small content that is - * known a priory (e.g., error messages). - */ - -class MemBuf; - -struct _HttpBody { - /* private */ - MemBuf *mb; -}; - #include "SquidString.h" /* http header extention field */ diff --git a/src/typedefs.h b/src/typedefs.h index b68c50ed61..6b3f2164b7 100644 --- a/src/typedefs.h +++ b/src/typedefs.h @@ -62,8 +62,6 @@ typedef struct _HttpHeaderFieldAttrs HttpHeaderFieldAttrs; typedef struct _HttpHeaderStat HttpHeaderStat; -typedef struct _HttpBody HttpBody; - typedef struct _domain_ping domain_ping; typedef struct _domain_type domain_type; diff --git a/src/urn.cc b/src/urn.cc index 58d220967e..93126175e5 100644 --- a/src/urn.cc +++ b/src/urn.cc @@ -444,7 +444,7 @@ urnHandleReply(void *data, StoreIOBuffer result) rep->header.putStr(HDR_LOCATION, min_u->url); } - httpBodySet(&rep->body, mb); + rep->body.setMb(mb); /* don't clean or delete mb; rep->body owns it now */ e->replaceHttpReply(rep); e->complete();