]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
c++-refactor HttpBody
authorFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 4 Dec 2011 13:52:07 +0000 (14:52 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Sun, 4 Dec 2011 13:52:07 +0000 (14:52 +0100)
12 files changed:
src/HttpBody.cc
src/HttpBody.h [new file with mode: 0644]
src/HttpReply.cc
src/HttpReply.h
src/Makefile.am
src/errorpage.cc
src/esi/Esi.cc
src/protos.h
src/store.cc
src/structs.h
src/typedefs.h
src/urn.cc

index cbd03c6c9ca3891a346e30541fba87a1d79d22ba..230698a83cd13acbd9d51fc871bce2f166cefd22 100644 (file)
  */
 
 #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 (file)
index 0000000..b893a2e
--- /dev/null
@@ -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_ */
index e2506b7842f144ce51509ec06fdaa7b71e0ccc5b..79d314430afa3dc41adf22700a686ae2107a1ff5 100644 (file)
@@ -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 */
index d44be465ecf14b514d2ab207ea834c8ba4aee8b0..2ad8427df1eaf314c2f4be9ab2e6acb13c05068e 100644 (file)
@@ -32,6 +32,7 @@
 #ifndef SQUID_HTTPREPLY_H
 #define SQUID_HTTPREPLY_H
 
+#include "HttpBody.h"
 #include "HttpMsg.h"
 #include "HttpStatusLine.h"
 
index 717fb53e34007d02590e02231c69f7a5638aab74..a981692874d4de0486805c2e64c24a2717f33d67 100644 (file)
@@ -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 \
index a42ca60aad7a379b4a08daf027c7c8327ab6d4eb..1197445c52a2054b3cbb3e169d1a2cf7195c48c7 100644 (file)
@@ -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 */
     }
 
index 0c8d78fbbb951186285fa80d154bb3e878ff678e..f547703d9b08aa7c0d70b6347a0ec2b518bc6b6b 100644 (file)
@@ -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,
index e39e135d9606e29a9d3f6a27645ba83ed8bca6b1..dd38345cbd7df872dcdecfec1f53bc818f11c498 100644 (file)
@@ -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);
index c9cfa116baaf07673349590d23bd4a5ea68977e5..f017ff874b0397d7cb081d6a3f5bf6185e00bb14 100644 (file)
@@ -1872,7 +1872,7 @@ StoreEntry::startWriting()
     rep->packHeadersInto(&p);
     mem_obj->markEndOfReplyHeaders();
 
-    httpBodyPackInto(&rep->body, &p);
+    rep->body.packInto(&p);
 
     packerClean(&p);
 }
index 84952a2ebcad7f9902d405a0852c961d29e14318..49e16e9ae1c9a489112202d20a7cb9c188b93cc5 100644 (file)
@@ -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 */
 
index b68c50ed61098f721dcc4ef78bc8868c6de09d6c..6b3f2164b727aa0e06bdbca4f6a4c8cbd927a2a5 100644 (file)
@@ -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;
index 58d220967e9eca1138798f9f646d45175253022e..93126175e5ba96820a2a636143484e3233b54800 100644 (file)
@@ -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();