]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Implemented String.psize() (for printf())
authorFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 9 Feb 2009 15:47:31 +0000 (16:47 +0100)
committerFrancesco Chemolli <kinkie@squid-cache.org>
Mon, 9 Feb 2009 15:47:31 +0000 (16:47 +0100)
Changed String storage size from 'unsigned short int' to size_t
Implemented String.rfind()
Implemented String.substr() and related tests
Made String.set() private, it's ready to be removed unless there's any dissent
Converted ftp.cc to use rfind in place of rpos

19 files changed:
src/HttpHdrCc.cc
src/HttpHdrSc.cc
src/HttpHeader.cc
src/HttpReply.cc
src/HttpRequest.cc
src/SquidString.h
src/String.cc
src/String.cci
src/client_side.cc
src/errorpage.cc
src/ftp.cc
src/http.cc
src/protos.h
src/store_log.cc
src/tests/testString.cc
src/tests/testString.h
src/url.cc
src/urn.cc
src/whois.cc

index 6e860a59a34c673218db36f8636957c9aec9a4f6..3e5a55d23847443b120b51e413cb3037e32eecbb 100644 (file)
@@ -236,7 +236,7 @@ httpHdrCcPackInto(const HttpHdrCc * cc, Packer * p)
 
             /* print option name */
             packerPrintf(p, (pcount ? ", %.*s" : "%.*s"),
-                CcFieldsInfo[flag].name.size(),
+                CcFieldsInfo[flag].name.psize(),
                 CcFieldsInfo[flag].name.rawBuf());
 
             /* handle options with values */
@@ -254,9 +254,9 @@ httpHdrCcPackInto(const HttpHdrCc * cc, Packer * p)
         }
     }
 
-    if (cc->other.size())
+    if (cc->other.size() != 0)
         packerPrintf(p, (pcount ? ", %.*s" : "%.*s"),
-            cc->other.size(), cc->other.rawBuf());
+            cc->other.psize(), cc->other.rawBuf());
 }
 
 /* negative max_age will clean old max_Age setting */
index 0be8eafd7fd5285dd1cfe11778d67707554df237..eff4d42802d2414bb1b3c4b4edecd6803cdd9182 100644 (file)
@@ -258,7 +258,7 @@ httpHdrScTargetPackInto(const HttpHdrScTarget * sc, Packer * p)
 
             /* print option name */
             packerPrintf(p, (pcount ? ", %.*s" : "%.*s"),
-                ScFieldsInfo[flag].name.size(),
+                ScFieldsInfo[flag].name.psize(),
                 ScFieldsInfo[flag].name.rawBuf());
 
             /* handle options with values */
@@ -267,14 +267,14 @@ httpHdrScTargetPackInto(const HttpHdrScTarget * sc, Packer * p)
                 packerPrintf(p, "=%d", (int) sc->max_age);
 
             if (flag == SC_CONTENT)
-                packerPrintf(p, "=\"%.*s\"", sc->content.size(), sc->content.rawBuf());
+                packerPrintf(p, "=\"%.*s\"", sc->content.psize(), sc->content.rawBuf());
 
             pcount++;
         }
     }
 
     if (sc->target.size())
-        packerPrintf (p, ";%.*s", sc->target.size(), sc->target.rawBuf());
+        packerPrintf (p, ";%.*s", sc->target.psize(), sc->target.rawBuf());
 }
 
 void
index 30407629a547efb99cb7c8ea10f82fcf35c62f02..dfd1a05c9dac9f48fe4f24e1e6f7788679bc41d3 100644 (file)
@@ -1683,7 +1683,7 @@ httpHeaderStoreReport(StoreEntry * e)
 }
 
 http_hdr_type
-httpHeaderIdByName(const char *name, int name_len, const HttpHeaderFieldInfo * info, int end)
+httpHeaderIdByName(const char *name, size_t name_len, const HttpHeaderFieldInfo * info, int end)
 {
     int i;
 
index 9d2c4aacc2470bb92505d88885f14f3d9e28b40c..0ec76cb1bd75d56c2f482fa6e5403e3cb1dd39f5 100644 (file)
@@ -439,7 +439,7 @@ HttpReply::bodySize(const HttpRequestMethod& method) const
 
 bool HttpReply::sanityCheckStartLine(MemBuf *buf, http_status *error)
 {
-    if (buf->contentSize() >= protoPrefix.size() && protoPrefix.cmp(buf->content(), protoPrefix.size()) != 0) {
+    if (buf->contentSize() >= protoPrefix.psize() && protoPrefix.cmp(buf->content(), protoPrefix.size()) != 0) { //hack
         debugs(58, 3, "HttpReply::sanityCheckStartLine: missing protocol prefix (" << protoPrefix << ") in '" << buf->content() << "'");
         *error = HTTP_INVALID_HEADER;
         return false;
index c575684a47b6de020ec4ef9ec04f4591a4cebe6a..e3ad2a6a8d69d01c96acf6c8be3cdffa34bfc18a 100644 (file)
@@ -299,7 +299,7 @@ HttpRequest::pack(Packer * p)
     assert(p);
     /* pack request-line */
     packerPrintf(p, "%s %.*s HTTP/1.0\r\n",
-                 RequestMethodStr(method), urlpath.size(), urlpath.rawBuf());
+                 RequestMethodStr(method), urlpath.psize(), urlpath.rawBuf());
     /* headers */
     header.packInto(p);
     /* trailer */
index 6d380a399a4104fbd377b3c6e2322014aab9be59..a3ef838fd7d8910ac364377ccd5d606924d8725b 100644 (file)
@@ -36,6 +36,8 @@
 #define SQUID_STRING_H
 
 #include "config.h"
+#include "TextException.h"
+
 
 /** todo checks to wrap this include properly */
 #include <ostream>
@@ -97,7 +99,10 @@ public:
      */
     _SQUID_INLINE_ char operator [](unsigned int pos) const;
 
-    _SQUID_INLINE_ int size() const;
+    _SQUID_INLINE_ size_t size() const;
+    /// variant of size() suited to be used for printf-alikes.
+    /// throws when size() > MAXINT
+    _SQUID_INLINE_ int psize() const;
     _SQUID_INLINE_ char const * unsafeBuf() const;
 
     /**
@@ -132,6 +137,7 @@ public:
     /// returns std::string::npos if ch is not found
     _SQUID_INLINE_ size_t find(char const ch) const;
     _SQUID_INLINE_ const char * rpos(char const ch) const;
+    _SQUID_INLINE_ size_t rfind(char const ch) const;
     _SQUID_INLINE_ int cmp (char const *) const;
     _SQUID_INLINE_ int cmp (char const *, size_t count) const;
     _SQUID_INLINE_ int cmp (String const &) const;
@@ -139,15 +145,13 @@ public:
     _SQUID_INLINE_ int caseCmp (char const *, size_t count) const;
     _SQUID_INLINE_ int caseCmp (String const &) const;
 
+    String substr(size_t from, size_t to) const;
+
     /** \deprecated Use assignment to [] position instead.
      *              ie   str[newLength] = '\0';
      */
     _SQUID_INLINE_ void cut(size_t newLength);
 
-    /** \deprecated Use assignment to [] position instead.
-     *              ie   str[newLength] = '\0';
-     */
-    _SQUID_INLINE_ void cutPointer(char const *loc);
 
 #if DEBUGSTRINGS
 
@@ -163,11 +167,15 @@ private:
     void setBuffer(char *buf, size_t sz);
 
     /* never reference these directly! */
-    unsigned short int size_; /* buffer size; 64K limit */
+    size_t size_; /* buffer size; 64K limit */
 
-    unsigned short int len_;  /* current length  */
+    size_t len_;  /* current length  */
 
     char *buf_;
+
+    _SQUID_INLINE_ void set(char const *loc, char const ch);
+    _SQUID_INLINE_ void cutPointer(char const *loc);
+
 };
 
 _SQUID_INLINE_ std::ostream & operator<<(std::ostream& os, String const &aString);
index f90168ace64b03c6873996a2b2b64ac4e96b8314..1edb99111928a99933aa7ffc1495389d97865a92 100644 (file)
@@ -233,6 +233,19 @@ String::absorb(String &old)
     old.len_ = 0;
 }
 
+String
+String::substr(size_t from, size_t to) const
+{
+    Must(from >= 0 && from < size());
+    Must(to > 0 && to <= size());
+    Must(to > from);
+
+    String rv;
+    rv.limitInit(rawBuf()+from,to-from);
+    return rv;
+}
+
+
 #if DEBUGSTRINGS
 void
 String::stat(StoreEntry *entry) const
@@ -412,6 +425,7 @@ checkNullString(const char *p)
     return p ? p : "(NULL)";
 }
 
+
 #ifndef _USE_INLINE_
 #include "String.cci"
 #endif
index 4795c898d2af83abca328608bbde9881585d7800..a5fd17acd92f9ebad80be99a65d034e7565d9407 100644 (file)
 
 #include "assert.h"
 #include <cstring>
+#include "squid.h"
+
+#ifdef HAVE_STDINT_H
+#include <stdint.h> //for INT_MAX
+#else /* HAVE_STDINT_H */
+#ifndef INT_MAX
+#define INT_MAX 1<<31 //hack but a safe bet
+#endif /* INT_MAX */
+#endif /* HAVE_STDINT_H */
+
 
 String::String() : size_(0), len_(0), buf_ (NULL)
 {
@@ -42,12 +52,19 @@ String::String() : size_(0), len_(0), buf_ (NULL)
 #endif
 }
 
-int
+size_t
 String::size() const
 {
     return len_;
 }
 
+int
+String::psize() const
+{
+    Must(size() < INT_MAX);
+    return size();
+}
+
 char const *
 String::unsafeBuf() const
 {
@@ -112,6 +129,17 @@ String::rpos(char const ch) const
     return strrchr(termedBuf(), (ch));
 }
 
+size_t
+String::rfind(char const ch) const
+{
+    const char *c;
+    c=rpos(ch);
+    if (c==NULL)
+        return std::string::npos;
+    return c-rawBuf();
+}
+
+
 int
 String::cmp (char const *aString) const
 {
@@ -184,6 +212,15 @@ String::caseCmp(const String &str) const
     return caseCmp(str.rawBuf(),str.size());
 }
 
+
+void
+String::set(char const *loc, char const ch)
+{
+    if (loc < buf_ || loc > (buf_ + size_) ) return;
+
+    buf_[loc-buf_] = ch;
+}
+
 void
 String::cut(size_t newLength)
 {
index df4b9f37d96efcd1e59ea4d8c7a86cf07d636764..021ecfd135f634a729fc3f9817c9d4714801d14f 100644 (file)
@@ -852,7 +852,7 @@ ClientSocketContext::sendBody(HttpReply * rep, StoreIOBuffer bodyData)
 static void
 clientPackTermBound(String boundary, MemBuf * mb)
 {
-    mb->Printf("\r\n--%.*s--\r\n", boundary.size(), boundary.rawBuf());
+    mb->Printf("\r\n--%.*s--\r\n", boundary.psize(), boundary.rawBuf());
     debugs(33, 6, "clientPackTermBound: buf offset: " << mb->size);
 }
 
@@ -868,7 +868,7 @@ clientPackRangeHdr(const HttpReply * rep, const HttpHdrRangeSpec * spec, String
     /* put boundary */
     debugs(33, 5, "clientPackRangeHdr: appending boundary: " << boundary);
     /* rfc2046 requires to _prepend_ boundary with <crlf>! */
-    mb->Printf("\r\n--%.*s\r\n", boundary.size(), boundary.rawBuf());
+    mb->Printf("\r\n--%.*s\r\n", boundary.psize(), boundary.rawBuf());
 
     /* stuff the header with required entries and pack it */
 
index 4d0ec4317b9adb369b89697cf2abfe727fb40d89..f5657778da2438d542ad29db89a5e7d43b37d3e0 100644 (file)
@@ -929,7 +929,7 @@ ErrorState::BuildContent()
     if (!Config.errorDirectory && request && request->header.getList(HDR_ACCEPT_LANGUAGE, &hdr) ) {
 
         const char *unsafeBuf = hdr.unsafeBuf(); // raw header string for parsing
-        int pos = 0; // current parsing position in header string
+        size_t pos = 0; // current parsing position in header string
         char *reset = NULL; // where to reset the p pointer for each new tag file
         char *dt = NULL;
 
index 86a4f6dac870ce17e692c7f325b2863d62fbd41f..096278205f492198ca83df109d842b0effa2577f 100644 (file)
@@ -1445,16 +1445,17 @@ FtpStateData::checkAuth(const HttpHeader * req_hdr)
     return 0;                  /* different username */
 }
 
+static const String str_type_eq("type=");
 void
 FtpStateData::checkUrlpath()
 {
     int l;
-    const char *t;
+    size_t t;
 
-    if ((t = request->urlpath.rpos(';')) != NULL) {
-        if (strncasecmp(t + 1, "type=", 5) == 0) {
-            typecode = (char) xtoupper(*(t + 6));
-            request->urlpath.cutPointer(t);
+    if ((t = request->urlpath.rfind(';')) != std::string::npos) {
+        if (request->urlpath.substr(t+1,t+1+str_type_eq.size())==str_type_eq) {
+            typecode = (char)xtoupper(request->urlpath[t+str_type_eq.size()+1]);
+            request->urlpath.cut(t);
         }
     }
 
index 92e56359297025468495db6289bec2fcc33b7971..7a0a1a27ef76fd5b73d01dde76f24d18a57a5b65 100644 (file)
@@ -1543,9 +1543,9 @@ HttpStateData::httpBuildRequestHeader(HttpRequest * request,
             if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size()) {
                 char loginbuf[256];
                 snprintf(loginbuf, sizeof(loginbuf), "%.*s:%.*s",
-                    orig_request->extacl_user.size(),
+                    orig_request->extacl_user.psize(),
                     orig_request->extacl_user.rawBuf(),
-                    orig_request->extacl_passwd.size(),
+                    orig_request->extacl_passwd.psize(),
                     orig_request->extacl_passwd.rawBuf());
                 httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s",
                                   base64_encode(loginbuf));
@@ -1574,9 +1574,9 @@ HttpStateData::httpBuildRequestHeader(HttpRequest * request,
             } else if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size()) {
                 char loginbuf[256];
                 snprintf(loginbuf, sizeof(loginbuf), "%.*s:%.*s",
-                    orig_request->extacl_user.size(),
+                    orig_request->extacl_user.psize(),
                     orig_request->extacl_user.rawBuf(),
-                    orig_request->extacl_passwd.size(),
+                    orig_request->extacl_passwd.psize(),
                     orig_request->extacl_passwd.rawBuf());
                 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s",
                                   base64_encode(loginbuf));
index 7df1d35f54dbbe7e2b00bbfba0b8e804898bf6d9..4df70c98aa69cf3587d6cf335a06413204eab91c 100644 (file)
@@ -255,7 +255,7 @@ SQUIDCEXTERN void httpHdrCcStatDumper(StoreEntry * sentry, int idx, double val,
 class HttpHeaderFieldInfo;
 SQUIDCEXTERN HttpHeaderFieldInfo *httpHeaderBuildFieldsInfo(const HttpHeaderFieldAttrs * attrs, int count);
 SQUIDCEXTERN void httpHeaderDestroyFieldsInfo(HttpHeaderFieldInfo * info, int count);
-SQUIDCEXTERN http_hdr_type httpHeaderIdByName(const char *name, int name_len, const HttpHeaderFieldInfo * attrs, int end);
+SQUIDCEXTERN http_hdr_type httpHeaderIdByName(const char *name, size_t name_len, const HttpHeaderFieldInfo * attrs, int end);
 SQUIDCEXTERN http_hdr_type httpHeaderIdByNameDef(const char *name, int name_len);
 SQUIDCEXTERN const char *httpHeaderNameById(int id);
 SQUIDCEXTERN int httpHeaderHasConnDir(const HttpHeader * hdr, const char *directive);
index 2ab3acd5849217c20d21efc9f68beb508d8ac081..9003443206881fbacdd968a47c4989fd97e5cd1e 100644 (file)
@@ -52,6 +52,8 @@ static OBJH storeLogTagsHist;
 
 static Logfile *storelog = NULL;
 
+static const String str_unknown="unknown";
+
 void
 storeLog(int tag, const StoreEntry * e)
 {
@@ -75,7 +77,10 @@ storeLog(int tag, const StoreEntry * e)
          * Because if we print it before the swap file number, it'll break
          * the existing log format.
          */
-        logfilePrintf(storelog, "%9d.%03d %-7s %02d %08X %s %4d %9d %9d %9d %s %"PRId64"/%"PRId64" %s %s\n",
+
+        String ctype=(reply->content_type.size() ? reply->content_type.termedBuf() : str_unknown);
+
+        logfilePrintf(storelog, "%9d.%03d %-7s %02d %08X %s %4d %9d %9d %9d %.*s %"PRId64"/%"PRId64" %s %s\n",
                       (int) current_time.tv_sec,
                       (int) current_time.tv_usec / 1000,
                       storeLogTags[tag],
@@ -86,7 +91,7 @@ storeLog(int tag, const StoreEntry * e)
                       (int) reply->date,
                       (int) reply->last_modified,
                       (int) reply->expires,
-                      reply->content_type.size() ? reply->content_type.unsafeBuf() : "unknown",
+                      ctype.psize(), ctype.rawBuf(),
                       reply->content_length,
                       e->contentLen(),
                       RequestMethodStr(mem->method),
index 1cf18fb0dc31d29dad3a79497026b5e05e5a8e2d..f615b43a460df0ab9338690eeed7c77e9ebf40b2 100644 (file)
@@ -59,3 +59,11 @@ testString::testCmpNotEmptyDefault()
     CPPUNIT_ASSERT(right.cmp("foo") < 0);
     CPPUNIT_ASSERT(right.cmp("foo", 1) < 0);
 }
+
+void testString::testSubstr()
+{
+    String s("0123456789");
+    String check=s.substr(3,5);
+    String ref("34");
+    CPPUNIT_ASSERT(s1 == ref);
+}
index 7b3581d9e56f7446aa65861e228100577e1d0f42..cfa32646de08d39ea02ee6d381225f85faf03bf2 100644 (file)
@@ -14,6 +14,8 @@ class testString : public CPPUNIT_NS::TestFixture
     CPPUNIT_TEST( testCmpDefault );
     CPPUNIT_TEST( testCmpEmptyString );
     CPPUNIT_TEST( testCmpNotEmptyDefault );
+    CPPUNIT_TEST( testSubstr );
+
     CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -23,6 +25,7 @@ protected:
     void testCmpDefault();
     void testCmpEmptyString();
     void testCmpNotEmptyDefault();
+    void testSubstr();
 };
 
 #endif
index 13b62b19047692c5d4f3db7e5ef974a713a6be06..5f7e839e7e540dbb537ee196dc1a1b94757360c1 100644 (file)
@@ -440,7 +440,7 @@ urlCanonical(HttpRequest * request)
 
     if (request->protocol == PROTO_URN) {
         snprintf(urlbuf, MAX_URL, "urn:%.*s",
-            request->urlpath.size(),
+            request->urlpath.psize(),
             request->urlpath.rawBuf());
     } else {
 /// \todo AYJ: this could use "if..else and method == METHOD_CONNECT" easier.
@@ -462,7 +462,7 @@ urlCanonical(HttpRequest * request)
                      *request->login ? "@" : null_string,
                      request->GetHost(),
                      portbuf,
-                     request->urlpath.size(),
+                     request->urlpath.psize(),
                      request->urlpath.rawBuf());
 
             break;
@@ -486,7 +486,7 @@ urlCanonicalClean(const HttpRequest * request)
 
     if (request->protocol == PROTO_URN) {
         snprintf(buf, MAX_URL, "urn:%.*s",
-            request->urlpath.size(), request->urlpath.rawBuf());
+            request->urlpath.psize(), request->urlpath.rawBuf());
     } else {
 /// \todo AYJ: this could use "if..else and method == METHOD_CONNECT" easier.
         switch (request->method.id()) {
@@ -519,7 +519,7 @@ urlCanonicalClean(const HttpRequest * request)
                      loginbuf,
                      request->GetHost(),
                      portbuf,
-                     request->urlpath.size(),
+                     request->urlpath.psize(),
                      request->urlpath.rawBuf());
             /*
              * strip arguments AFTER a question-mark
@@ -590,7 +590,7 @@ urlMakeAbsolute(const HttpRequest * req, const char *relUrl)
 
     if (req->protocol == PROTO_URN) {
         snprintf(urlbuf, MAX_URL, "urn:%.*s",
-            req->urlpath.size(),
+            req->urlpath.psize(),
             req->urlpath.rawBuf());
         return (urlbuf);
     }
index f20f3f9b57b2c47cd76414c2594ac396c9d88aa3..0c77a47fb05c131aa6ecff785b0069e045e37c91 100644 (file)
@@ -201,7 +201,7 @@ UrnState::createUriResRequest (String &uri)
 {
     LOCAL_ARRAY(char, local_urlres, 4096);
     char *host = getHost (uri);
-    snprintf(local_urlres, 4096, "http://%s/uri-res/N2L?urn:%.*s", host, uri.size(), uri.rawBuf());
+    snprintf(local_urlres, 4096, "http://%s/uri-res/N2L?urn:%.*s", host, uri.psize(), uri.rawBuf());
     safe_free (host);
     safe_free (urlres);
     urlres = xstrdup (local_urlres);
index 7818b8e9bbe1ba39adac392591127cb5f18f9b4d..c72e7ea025bbf8b7df69f7cd150140b784dab521 100644 (file)
@@ -98,7 +98,7 @@ whoisStart(FwdState * fwd)
 
     buf = (char *)xmalloc(l);
 
-    snprintf(buf, l, "%.*s\r\n", p->request->urlpath.size()-1, p->request->urlpath.rawBuf() + 1);
+    snprintf(buf, l, "%.*s\r\n", p->request->urlpath.psize()-1, p->request->urlpath.rawBuf() + 1);
 
     comm_write(fd, buf, strlen(buf), whoisWriteComplete, p, NULL);
     comm_read(fd, p->buf, BUFSIZ, whoisReadReply, p);