Hi all,
yet another struct-turned-class.
It is more or less a straightforward porting, with minimal interface
extensions aimed at keeping the MemBuf data-member private.
For simplicity's sake I've added one invariant condition: during a
HttpBody lifetime, the MemBuf data member is never NULL.
Please review
--
/kinkie
=== modified file 'src/HttpBody.cc'
--- src/HttpBody.cc 2009-01-21 03:47:47 +0000
+++ src/HttpBody.cc 2011-12-03 21:09:59 +0000
@@ -34,43 +34,40 @@
*/
#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;
+ 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());
}
=== added file 'src/HttpBody.h'
--- src/HttpBody.h 1970-01-01 00:00:00 +0000
+++ src/HttpBody.h 2011-12-03 21:09:59 +0000
@@ -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.
+ *
+ * HttpBody.h
+ *
+ * Created on: Dec 1, 2011
+ * 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: contents are not cleared by the output operation
+ */
+ void packInto(Packer *) const;
+ /// clear the HttpBody contents
+ void clear();
+
+ /// check whether there is any contents in the HttpBody
+ bool hasContent() const { return (mb->contentSize()>0); }
+ /// return the size of the HttpBody's message contents
+ mb_size_t contentSize() const { return mb->contentSize(); }
+ /// return a 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_ */
=== modified file 'src/HttpReply.cc'
--- src/HttpReply.cc 2011-12-03 12:40:23 +0000
+++ src/HttpReply.cc 2011-12-03 21:09:59 +0000
@@ -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 @@
void
HttpReply::init()
{
- httpBodyInit(&body);
hdrCacheInit();
httpStatusLineInit(&sline);
pstate = psReadyToParseStartLine;
@@ -121,7 +121,7 @@
// 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 @@
HttpReply::packInto(Packer * p)
{
packHeadersInto(p);
- httpBodyPackInto(&body, p);
+ body.packInto(p);
}
/* create memBuf, create mem-based packer, pack, destroy packer, return MemBuf */
=== modified file 'src/HttpReply.h'
--- src/HttpReply.h 2010-09-11 23:58:15 +0000
+++ src/HttpReply.h 2011-12-03 21:09:59 +0000
@@ -32,6 +32,7 @@
#ifndef SQUID_HTTPREPLY_H
#define SQUID_HTTPREPLY_H
+#include "HttpBody.h"
#include "HttpMsg.h"
#include "HttpStatusLine.h"
=== modified file 'src/Makefile.am'
--- src/Makefile.am 2011-11-26 12:27:23 +0000
+++ src/Makefile.am 2011-12-03 21:09:59 +0000
@@ -358,6 +358,7 @@
HttpHeaderMask.h \
HttpHeaderRange.h \
HttpHeaderTools.cc \
+ HttpBody.h \
HttpBody.cc \
HttpControlMsg.h \
HttpMsg.cc \
@@ -1042,6 +1043,7 @@
cbdata.cc \
cbdata.h \
ETag.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHdrCc.h \
HttpHdrCc.cc \
@@ -1307,6 +1309,7 @@
HelperChildConfig.cc \
$(HTCPSOURCE) \
http.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHeader.cc \
HttpHeaderTools.cc \
@@ -1447,6 +1450,7 @@
event.cc \
fd.cc \
filemap.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHdrCc.h \
HttpHdrCc.cc \
@@ -1623,6 +1627,7 @@
hier_code.h \
$(HTCPSOURCE) \
http.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHeader.cc \
HttpHeaderTools.cc \
@@ -1811,6 +1816,7 @@
hier_code.h \
$(HTCPSOURCE) \
http.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHeader.cc \
HttpHeaderTools.cc \
@@ -1997,6 +2003,7 @@
hier_code.h \
$(HTCPSOURCE) \
http.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHdrCc.h \
HttpHdrCc.cc \
@@ -2225,6 +2232,7 @@
hier_code.h \
$(HTCPSOURCE) \
http.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHeader.cc \
HttpHeaderTools.cc \
@@ -2414,6 +2422,8 @@
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 @@
fd.cc \
disk.cc \
filemap.cc \
+ HttpBody.h \
HttpBody.cc \
HttpReply.cc \
HttpStatusLine.cc \
@@ -2680,6 +2691,7 @@
event.cc \
fd.cc \
filemap.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHdrCc.cc \
HttpHdrContRange.cc \
@@ -2809,6 +2821,7 @@
fd.cc \
disk.cc \
filemap.cc \
+ HttpBody.h \
HttpBody.cc \
HttpReply.cc \
HttpStatusLine.cc \
@@ -2940,6 +2953,7 @@
fd.cc \
disk.cc \
filemap.cc \
+ HttpBody.h \
HttpBody.cc \
HttpReply.cc \
HttpStatusLine.cc \
@@ -3098,6 +3112,7 @@
hier_code.h \
$(HTCPSOURCE) \
http.cc \
+ HttpBody.h \
HttpBody.cc \
HttpHdrCc.h \
HttpHdrCc.cc \
=== modified file 'src/errorpage.cc'
--- src/errorpage.cc 2011-11-27 21:34:50 +0000
+++ src/errorpage.cc 2011-12-03 21:09:59 +0000
@@ -1213,7 +1213,7 @@
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 */
}
=== modified file 'src/esi/Esi.cc'
--- src/esi/Esi.cc 2011-12-03 12:40:23 +0000
+++ src/esi/Esi.cc 2011-12-03 21:09:59 +0000
@@ -1465,15 +1465,15 @@
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,
=== modified file 'src/protos.h'
--- src/protos.h 2011-11-03 10:02:02 +0000
+++ src/protos.h 2011-12-03 21:09:59 +0000
@@ -222,18 +222,6 @@
#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);
=== modified file 'src/store.cc'
--- src/store.cc 2011-11-22 11:12:32 +0000
+++ src/store.cc 2011-12-03 21:09:59 +0000
@@ -1872,7 +1872,7 @@
rep->packHeadersInto(&p);
mem_obj->markEndOfReplyHeaders();
- httpBodyPackInto(&rep->body, &p);
+ rep->body.packInto(&p);
packerClean(&p);
}
=== modified file 'src/structs.h'
--- src/structs.h 2011-11-08 10:54:37 +0000
+++ src/structs.h 2011-12-03 21:09:59 +0000
@@ -704,18 +704,6 @@
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 */
=== modified file 'src/typedefs.h'
--- src/typedefs.h 2011-12-01 06:36:36 +0000
+++ src/typedefs.h 2011-12-03 21:09:59 +0000
@@ -62,8 +62,6 @@
typedef struct _HttpHeaderStat HttpHeaderStat;
-typedef struct _HttpBody HttpBody;
-
typedef struct _domain_ping domain_ping;
typedef struct _domain_type domain_type;
=== modified file 'src/urn.cc'
--- src/urn.cc 2011-11-27 12:37:35 +0000
+++ src/urn.cc 2011-12-03 21:09:59 +0000
@@ -444,7 +444,7 @@
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();