The authenticators utilize a "statedata" structure to store and pass the callback and Auth::UserRequest an auth lookup is about.

This patch converts the structure from a CBDATA_GLOBAL_TYPE struct to a CBDATA_CLASS2 and adds a parameterized constructor for it.

The result is that all the code using it no longer has to explicitly manage fields assignments and cbdata referencing. Simply new the object when submitting to the helper system and delete once its handler has been called.


Amos
--
Please be using
  Current Stable Squid 2.7.STABLE9 or 3.1.12
  Beta testers wanted for 3.2.0.8 and 3.1.12.2
=== modified file 'src/auth/State.cc'
--- src/auth/State.cc	2010-02-12 10:51:58 +0000
+++ src/auth/State.cc	2011-06-04 11:24:37 +0000
@@ -1,11 +1,4 @@
 #include "config.h"
 #include "auth/State.h"
 
-CBDATA_GLOBAL_TYPE(authenticateStateData);
-
-void
-authenticateStateFree(authenticateStateData * r)
-{
-    r->auth_user_request = NULL;
-    cbdataFree(r);
-}
+CBDATA_NAMESPACED_CLASS_INIT(Auth, StateData);

=== modified file 'src/auth/State.h'
--- src/auth/State.h	2011-02-07 10:27:53 +0000
+++ src/auth/State.h	2011-06-04 11:00:12 +0000
@@ -4,19 +4,36 @@
 #if USE_AUTH
 
 #include "auth/UserRequest.h"
+#include "cbdata.h"
+
+namespace Auth
+{
 
 /**
  * CBDATA state for NTLM, Negotiate, and Digest stateful authentication.
  */
-typedef struct {
+class StateData {
+public:
+    StateData(const AuthUserRequest::Pointer &r, RH *h, void *d) :
+        data(cbdataReference(d)),
+        auth_user_request(r),
+        handler(h)
+    {}
+
+    ~StateData() {
+        auth_user_request = NULL;
+        cbdataReferenceDone(data);
+    }
+
     void *data;
     AuthUserRequest::Pointer auth_user_request;
     RH *handler;
-} authenticateStateData;
-
-extern CBDATA_GLOBAL_TYPE(authenticateStateData);
-
-extern void authenticateStateFree(authenticateStateData * r);
+
+private:
+    CBDATA_CLASS2(StateData);
+};
+
+} // namespace Auth
 
 #endif /* USE_AUTH */
 #endif /* __AUTH_AUTHENTICATE_STATE_T__ */

=== modified file 'src/auth/basic/auth_basic.cc'
--- src/auth/basic/auth_basic.cc	2011-04-14 02:40:59 +0000
+++ src/auth/basic/auth_basic.cc	2011-06-04 06:00:14 +0000
@@ -138,7 +138,7 @@
 static void
 authenticateBasicHandleReply(void *data, char *reply)
 {
-    authenticateStateData *r = static_cast<authenticateStateData *>(data);
+    Auth::StateData *r = static_cast<Auth::StateData *>(data);
     BasicAuthQueueNode *tmpnode;
     char *t = NULL;
     void *cbdata;
@@ -188,7 +188,7 @@
         basic_auth->auth_queue = tmpnode;
     }
 
-    authenticateStateFree(r);
+    delete r;
 }
 
 void
@@ -416,8 +416,6 @@
         basicauthenticators->ipc_type = IPC_STREAM;
 
         helperOpenServers(basicauthenticators);
-
-        CBDATA_INIT_TYPE(authenticateStateData);
     }
 }
 
@@ -450,13 +448,8 @@
 {
     /* mark the user as having verification in progress */
     credentials(Auth::Pending);
-    authenticateStateData *r = NULL;
     char buf[8192];
     char user[1024], pass[1024];
-    r = cbdataAlloc(authenticateStateData);
-    r->handler = handler;
-    r->data = cbdataReference(data);
-    r->auth_user_request = auth_user_request;
     if (static_cast<Auth::Basic::Config*>(config)->utf8) {
         latin1_to_utf8(user, sizeof(user), username());
         latin1_to_utf8(pass, sizeof(pass), passwd);
@@ -467,5 +460,6 @@
         xstrncpy(pass, rfc1738_escape(passwd), sizeof(pass));
     }
     snprintf(buf, sizeof(buf), "%s %s\n", user, pass);
-    helperSubmit(basicauthenticators, buf, authenticateBasicHandleReply, r);
+    helperSubmit(basicauthenticators, buf, authenticateBasicHandleReply,
+                 new Auth::StateData(auth_user_request, handler, data));
 }

=== modified file 'src/auth/digest/UserRequest.cc'
--- src/auth/digest/UserRequest.cc	2011-05-08 13:53:10 +0000
+++ src/auth/digest/UserRequest.cc	2011-06-04 11:23:03 +0000
@@ -247,7 +247,6 @@
 void
 AuthDigestUserRequest::module_start(RH * handler, void *data)
 {
-    authenticateStateData *r = NULL;
     char buf[8192];
 
     assert(user() != NULL && user()->auth_type == Auth::AUTH_DIGEST);
@@ -259,10 +258,6 @@
         return;
     }
 
-    r = cbdataAlloc(authenticateStateData);
-    r->handler = handler;
-    r->data = cbdataReference(data);
-    r->auth_user_request = static_cast<AuthUserRequest*>(this);
     if (static_cast<Auth::Digest::Config*>(Auth::Config::Find("digest"))->utf8) {
         char userstr[1024];
         latin1_to_utf8(userstr, sizeof(userstr), user()->username());
@@ -271,13 +266,14 @@
         snprintf(buf, 8192, "\"%s\":\"%s\"\n", user()->username(), realm);
     }
 
-    helperSubmit(digestauthenticators, buf, AuthDigestUserRequest::HandleReply, r);
+    helperSubmit(digestauthenticators, buf, AuthDigestUserRequest::HandleReply,
+                 new Auth::StateData(this, handler, data));
 }
 
 void
 AuthDigestUserRequest::HandleReply(void *data, char *reply)
 {
-    authenticateStateData *replyData = static_cast < authenticateStateData * >(data);
+    Auth::StateData *replyData = static_cast<Auth::StateData *>(data);
     char *t = NULL;
     void *cbdata;
     debugs(29, 9, HERE << "{" << (reply ? reply : "<NULL>") << "}");
@@ -315,7 +311,5 @@
     if (cbdataReferenceValidDone(replyData->data, &cbdata))
         replyData->handler(cbdata, NULL);
 
-    replyData->auth_user_request = NULL;
-
-    cbdataFree(replyData);
+    delete replyData;
 }

=== modified file 'src/auth/digest/auth_digest.cc'
--- src/auth/digest/auth_digest.cc	2011-04-14 02:40:59 +0000
+++ src/auth/digest/auth_digest.cc	2011-06-04 05:45:13 +0000
@@ -65,8 +65,6 @@
 static int authdigest_initialised = 0;
 static MemAllocator *digest_nonce_pool = NULL;
 
-// CBDATA_TYPE(DigestAuthenticateStateData);
-
 enum http_digest_attr_type {
     DIGEST_USERNAME,
     DIGEST_REALM,
@@ -590,8 +588,6 @@
         digestauthenticators->ipc_type = IPC_STREAM;
 
         helperOpenServers(digestauthenticators);
-
-        CBDATA_INIT_TYPE(authenticateStateData);
     }
 }
 

=== modified file 'src/auth/negotiate/UserRequest.cc'
--- src/auth/negotiate/UserRequest.cc	2011-05-08 13:53:10 +0000
+++ src/auth/negotiate/UserRequest.cc	2011-06-04 06:02:33 +0000
@@ -97,11 +97,6 @@
 
     debugs(29, 8, HERE << "credentials state is '" << user()->credentials() << "'");
 
-    authenticateStateData *r = cbdataAlloc(authenticateStateData);
-    r->handler = handler;
-    r->data = cbdataReference(data);
-    r->auth_user_request = this;
-
     if (user()->credentials() == Auth::Pending) {
         snprintf(buf, sizeof(buf), "YR %s\n", client_blob); //CHECKME: can ever client_blob be 0 here?
     } else {
@@ -111,7 +106,9 @@
     waiting = 1;
 
     safe_free(client_blob);
-    helperStatefulSubmit(negotiateauthenticators, buf, AuthNegotiateUserRequest::HandleReply, r, authserver);
+
+    helperStatefulSubmit(negotiateauthenticators, buf, AuthNegotiateUserRequest::HandleReply,
+                         new Auth::StateData(this, handler, data), authserver);
 }
 
 /**
@@ -234,18 +231,15 @@
 void
 AuthNegotiateUserRequest::HandleReply(void *data, void *lastserver, char *reply)
 {
-    authenticateStateData *r = static_cast<authenticateStateData *>(data);
+    Auth::StateData *r = static_cast<Auth::StateData *>(data);
 
-    int valid;
     char *blob, *arg = NULL;
 
     debugs(29, 8, HERE << "helper: '" << lastserver << "' sent us '" << (reply ? reply : "<NULL>") << "'");
-    valid = cbdataReferenceValid(r->data);
 
-    if (!valid) {
+    if (!cbdataReferenceValid(r->data)) {
         debugs(29, DBG_IMPORTANT, "ERROR: Negotiate Authentication invalid callback data. helper '" << lastserver << "'.");
-        cbdataReferenceDone(r->data);
-        authenticateStateFree(r);
+        delete r;
         return;
     }
 
@@ -367,8 +361,7 @@
 
     lm_request->request = NULL;
     r->handler(r->data, NULL);
-    cbdataReferenceDone(r->data);
-    authenticateStateFree(r);
+    delete r;
 }
 
 void

=== modified file 'src/auth/negotiate/auth_negotiate.cc'
--- src/auth/negotiate/auth_negotiate.cc	2011-04-14 02:40:59 +0000
+++ src/auth/negotiate/auth_negotiate.cc	2011-06-04 05:51:09 +0000
@@ -181,8 +181,6 @@
         negotiateauthenticators->ipc_type = IPC_STREAM;
 
         helperStatefulOpenServers(negotiateauthenticators);
-
-        CBDATA_INIT_TYPE(authenticateStateData);
     }
 }
 

=== modified file 'src/auth/ntlm/UserRequest.cc'
--- src/auth/ntlm/UserRequest.cc	2011-05-08 13:53:10 +0000
+++ src/auth/ntlm/UserRequest.cc	2011-06-04 05:57:37 +0000
@@ -92,11 +92,6 @@
 
     debugs(29, 8, HERE << "credentials state is '" << user()->credentials() << "'");
 
-    authenticateStateData *r = cbdataAlloc(authenticateStateData);
-    r->handler = handler;
-    r->data = cbdataReference(data);
-    r->auth_user_request = this;
-
     if (user()->credentials() == Auth::Pending) {
         snprintf(buf, sizeof(buf), "YR %s\n", client_blob); //CHECKME: can ever client_blob be 0 here?
     } else {
@@ -106,7 +101,8 @@
     waiting = 1;
 
     safe_free(client_blob);
-    helperStatefulSubmit(ntlmauthenticators, buf, AuthNTLMUserRequest::HandleReply, r, authserver);
+    helperStatefulSubmit(ntlmauthenticators, buf, AuthNTLMUserRequest::HandleReply,
+                         new Auth::StateData(this, handler, data), authserver);
 }
 
 /**
@@ -229,18 +225,14 @@
 void
 AuthNTLMUserRequest::HandleReply(void *data, void *lastserver, char *reply)
 {
-    authenticateStateData *r = static_cast<authenticateStateData *>(data);
-
-    int valid;
+    Auth::StateData *r = static_cast<Auth::StateData *>(data);
     char *blob;
 
     debugs(29, 8, HERE << "helper: '" << lastserver << "' sent us '" << (reply ? reply : "<NULL>") << "'");
-    valid = cbdataReferenceValid(r->data);
 
-    if (!valid) {
+    if (!cbdataReferenceValid(r->data)) {
         debugs(29, DBG_IMPORTANT, "ERROR: NTLM Authentication invalid callback data. helper '" << lastserver << "'.");
-        cbdataReferenceDone(r->data);
-        authenticateStateFree(r);
+        delete r;
         return;
     }
 
@@ -349,6 +341,5 @@
         lm_request->request = NULL;
     }
     r->handler(r->data, NULL);
-    cbdataReferenceDone(r->data);
-    authenticateStateFree(r);
+    delete r;
 }

=== modified file 'src/auth/ntlm/auth_ntlm.cc'
--- src/auth/ntlm/auth_ntlm.cc	2011-04-14 02:40:59 +0000
+++ src/auth/ntlm/auth_ntlm.cc	2011-06-04 05:57:55 +0000
@@ -168,8 +168,6 @@
         ntlmauthenticators->ipc_type = IPC_STREAM;
 
         helperStatefulOpenServers(ntlmauthenticators);
-
-        CBDATA_INIT_TYPE(authenticateStateData);
     }
 }
 

Reply via email to