]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Cleanup ASState
authorAmos Jeffries <squid3@treenet.co.nz>
Fri, 25 Oct 2013 19:07:30 +0000 (12:07 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Fri, 25 Oct 2013 19:07:30 +0000 (12:07 -0700)
* convert from struct to class
 - moving initialization of members from transaction setup to ctor.

* implement with CBDATA_CLASS2 macro
 - removing uses of cbdataAlloc/cbdataFree and CBDATA_TYPE/CBDATA_INIT_TYPE

* set bool member dataRead using true/false instead of 0/1

* upgrade HttpRequest pointer member to Pointer type

src/acl/Asn.cc

index 00c313ad6d667ec0b40b6e9be862deeaefa903ba..5ef8091232eec75a98c684fac8484c55000b06a0 100644 (file)
@@ -89,17 +89,45 @@ struct as_info {
     time_t expires;            /* NOTUSED */
 };
 
-struct ASState {
+class ASState
+{
+public:
+    ASState();
+    ~ASState();
+
     StoreEntry *entry;
     store_client *sc;
-    HttpRequest *request;
+    HttpRequest::Pointer request;
     int as_number;
     int64_t offset;
     int reqofs;
     char reqbuf[AS_REQBUF_SZ];
     bool dataRead;
+private:
+    CBDATA_CLASS2(ASState);
 };
 
+CBDATA_CLASS_INIT(ASState);
+
+ASState::ASState() :
+        entry(NULL),
+        sc(NULL),
+        request(NULL),
+        as_number(0),
+        offset(0),
+        reqofs(0),
+        dataRead(false)
+{
+    memset(reqbuf, 0, AS_REQBUF_SZ);
+}
+
+ASState::~ASState()
+{
+    debugs(53, 3, entry->url());
+    storeUnregister(sc, entry, this);
+    entry->unlock();
+}
+
 /** entry into the radix tree */
 struct rtentry_t {
     struct squid_radix_node e_nodes[2];
@@ -127,8 +155,6 @@ extern "C" {
 
 void asnAclInitialize(ACL * acls);
 
-static void asStateFree(void *data);
-
 static void destroyRadixNodeInfo(as_info *);
 
 static OBJH asnStats;
@@ -197,13 +223,11 @@ asnRegisterWithCacheManager(void)
 
 SQUIDCEXTERN int squid_max_keylen;     /* yuck.. this is in lib/radix.c */
 
-CBDATA_TYPE(ASState);
 void
 asnInit(void)
 {
     static bool inited = false;
     squid_max_keylen = 40;
-    CBDATA_INIT_TYPE(ASState);
 
     if (!inited) {
         inited = true;
@@ -237,35 +261,25 @@ asnCacheStart(int as)
 {
     LOCAL_ARRAY(char, asres, 4096);
     StoreEntry *e;
-    ASState *asState;
-    asState = cbdataAlloc(ASState);
-    asState->dataRead = 0;
-    debugs(53, 3, "asnCacheStart: AS " << as);
+    ASState *asState = new ASState;
+    debugs(53, 3, "AS " << as);
     snprintf(asres, 4096, "whois://%s/!gAS%d", Config.as_whois_server, as);
     asState->as_number = as;
     asState->request = HttpRequest::CreateFromUrl(asres);
-    assert(NULL != asState->request);
-    HTTPMSGLOCK(asState->request);
+    assert(asState->request != NULL);
 
     if ((e = storeGetPublic(asres, Http::METHOD_GET)) == NULL) {
         e = storeCreateEntry(asres, asres, RequestFlags(), Http::METHOD_GET);
         asState->sc = storeClientListAdd(e, asState);
-        FwdState::fwdStart(Comm::ConnectionPointer(), e, asState->request);
+        FwdState::fwdStart(Comm::ConnectionPointer(), e, asState->request.getRaw());
     } else {
-
         e->lock();
         asState->sc = storeClientListAdd(e, asState);
     }
 
     asState->entry = e;
-    asState->offset = 0;
-    asState->reqofs = 0;
     StoreIOBuffer readBuffer (AS_REQBUF_SZ, asState->offset, asState->reqbuf);
-    storeClientCopy(asState->sc,
-                    e,
-                    readBuffer,
-                    asHandleReply,
-                    asState);
+    storeClientCopy(asState->sc, e, readBuffer, asHandleReply, asState);
 }
 
 static void
@@ -284,21 +298,21 @@ asHandleReply(void *data, StoreIOBuffer result)
     /* First figure out whether we should abort the request */
 
     if (EBIT_TEST(e->flags, ENTRY_ABORTED)) {
-        asStateFree(asState);
+        delete asState;
         return;
     }
 
     if (result.length == 0 && asState->dataRead) {
-        debugs(53, 3, "asHandleReply: Done: " << e->url()  );
-        asStateFree(asState);
+        debugs(53, 3, "asHandleReply: Done: " << e->url());
+        delete asState;
         return;
     } else if (result.flags.error) {
         debugs(53, DBG_IMPORTANT, "asHandleReply: Called with Error set and size=" << (unsigned int) result.length);
-        asStateFree(asState);
+        delete asState;
         return;
     } else if (e->getReply()->sline.status() != Http::scOkay) {
         debugs(53, DBG_IMPORTANT, "WARNING: AS " << asState->as_number << " whois request failed");
-        asStateFree(asState);
+        delete asState;
         return;
     }
 
@@ -326,7 +340,7 @@ asHandleReply(void *data, StoreIOBuffer result)
         debugs(53, 3, "asHandleReply: AS# " << s << " (" << asState->as_number << ")");
         asnAddNet(s, asState->as_number);
         s = t + 1;
-        asState->dataRead = 1;
+        asState->dataRead = true;
     }
 
     /*
@@ -377,17 +391,6 @@ asHandleReply(void *data, StoreIOBuffer result)
     }
 }
 
-static void
-asStateFree(void *data)
-{
-    ASState *asState = (ASState *)data;
-    debugs(53, 3, "asnStateFree: " << asState->entry->url()  );
-    storeUnregister(asState->sc, asState->entry, asState);
-    asState->entry->unlock();
-    HTTPMSGUNLOCK(asState->request);
-    cbdataFree(asState);
-}
-
 /**
  * add a network (addr, mask) to the radix tree, with matching AS number
  */