]> git.ipfire.org Git - thirdparty/squid.git/blobdiff - src/adaptation/ecap/XactionRep.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / adaptation / ecap / XactionRep.cc
index 8349a2d6d3fc7c4902105782e5fad2f745ff4827..7ee3f1d9e3dcbfa6d516575b92a21d2fb389ef3b 100644 (file)
@@ -7,17 +7,17 @@
 #include <libecap/common/named_values.h>
 #include <libecap/common/names.h>
 #include <libecap/adapter/xaction.h>
-#include "HttpRequest.h"
-#include "HttpReply.h"
-#include "SquidTime.h"
-#include "adaptation/ecap/XactionRep.h"
+#include "adaptation/Answer.h"
 #include "adaptation/ecap/Config.h"
+#include "adaptation/ecap/XactionRep.h"
 #include "adaptation/Initiator.h"
 #include "base/TextException.h"
+#include "HttpReply.h"
+#include "HttpRequest.h"
+#include "SquidTime.h"
 
 CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Ecap::XactionRep, XactionRep);
 
-
 /// a libecap Visitor for converting adapter transaction options to HttpHeader
 class OptionsExtractor: public libecap::NamedValueVisitor
 {
@@ -80,11 +80,13 @@ Adaptation::Ecap::XactionRep::option(const libecap::Name &name) const
         return clientIpValue();
     if (name == libecap::metaUserName)
         return usernameValue();
-    if (name == Adaptation::Config::masterx_shared_name)
+    if (Adaptation::Config::masterx_shared_name && name == Adaptation::Config::masterx_shared_name)
         return masterxSharedValue(name);
 
     // TODO: metaServerIp, metaAuthenticatedUser, and metaAuthenticatedGroups
-    return libecap::Area();
+
+    // If the name is unknown, metaValue returns an emtpy area
+    return metaValue(name);
 }
 
 void
@@ -101,6 +103,8 @@ Adaptation::Ecap::XactionRep::visitEachOption(libecap::NamedValueVisitor &visito
             visitor.visit(name, value);
     }
 
+    visitEachMetaHeader(visitor);
+
     // TODO: metaServerIp, metaAuthenticatedUser, and metaAuthenticatedGroups
 }
 
@@ -120,9 +124,9 @@ Adaptation::Ecap::XactionRep::clientIpValue() const
         } else
 #endif
             client_addr = request->client_addr;
-        if (!client_addr.IsAnyAddr() && !client_addr.IsNoAddr()) {
+        if (!client_addr.isAnyAddr() && !client_addr.isNoAddr()) {
             char ntoabuf[MAX_IPSTRLEN] = "";
-            client_addr.NtoA(ntoabuf,MAX_IPSTRLEN);
+            client_addr.toStr(ntoabuf,MAX_IPSTRLEN);
             return libecap::Area::FromTempBuffer(ntoabuf, strlen(ntoabuf));
         }
     }
@@ -132,13 +136,18 @@ Adaptation::Ecap::XactionRep::clientIpValue() const
 const libecap::Area
 Adaptation::Ecap::XactionRep::usernameValue() const
 {
+#if USE_AUTH
     const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ?
                                  theCauseRep->raw().header : theVirginRep.raw().header);
     Must(request);
     if (request->auth_user_request != NULL) {
         if (char const *name = request->auth_user_request->username())
             return libecap::Area::FromTempBuffer(name, strlen(name));
+        else if (request->extacl_user.defined() && request->extacl_user.size())
+            return libecap::Area::FromTempBuffer(request->extacl_user.rawBuf(),
+                                                 request->extacl_user.size());
     }
+#endif
     return libecap::Area();
 }
 
@@ -159,6 +168,48 @@ Adaptation::Ecap::XactionRep::masterxSharedValue(const libecap::Name &name) cons
     return libecap::Area();
 }
 
+const libecap::Area
+Adaptation::Ecap::XactionRep::metaValue(const libecap::Name &name) const
+{
+    HttpRequest *request = dynamic_cast<HttpRequest*>(theCauseRep ?
+                           theCauseRep->raw().header : theVirginRep.raw().header);
+    Must(request);
+    HttpReply *reply = dynamic_cast<HttpReply*>(theVirginRep.raw().header);
+
+    if (name.known()) { // must check to avoid empty names matching unset cfg
+        typedef Notes::iterator ACAMLI;
+        for (ACAMLI i = Adaptation::Config::metaHeaders.begin(); i != Adaptation::Config::metaHeaders.end(); ++i) {
+            if (name == (*i)->key.termedBuf()) {
+                if (const char *value = (*i)->match(request, reply))
+                    return libecap::Area::FromTempString(value);
+                else
+                    return libecap::Area();
+            }
+        }
+    }
+
+    return libecap::Area();
+}
+
+void
+Adaptation::Ecap::XactionRep::visitEachMetaHeader(libecap::NamedValueVisitor &visitor) const
+{
+    HttpRequest *request = dynamic_cast<HttpRequest*>(theCauseRep ?
+                           theCauseRep->raw().header : theVirginRep.raw().header);
+    Must(request);
+    HttpReply *reply = dynamic_cast<HttpReply*>(theVirginRep.raw().header);
+
+    typedef Notes::iterator ACAMLI;
+    for (ACAMLI i = Adaptation::Config::metaHeaders.begin(); i != Adaptation::Config::metaHeaders.end(); ++i) {
+        const char *v = (*i)->match(request, reply);
+        if (v) {
+            const libecap::Name name((*i)->key.termedBuf());
+            const libecap::Area value = libecap::Area::FromTempString(v);
+            visitor.visit(name, value);
+        }
+    }
+}
+
 void
 Adaptation::Ecap::XactionRep::start()
 {
@@ -167,13 +218,26 @@ Adaptation::Ecap::XactionRep::start()
     if (!theVirginRep.raw().body_pipe)
         makingVb = opNever; // there is nothing to deliver
 
-    const HttpRequest *request = dynamic_cast<const HttpRequest*> (theCauseRep ?
-                                 theCauseRep->raw().header : theVirginRep.raw().header);
+    HttpRequest *request = dynamic_cast<HttpRequest*> (theCauseRep ?
+                           theCauseRep->raw().header : theVirginRep.raw().header);
     Must(request);
+
+    HttpReply *reply = dynamic_cast<HttpReply*>(theVirginRep.raw().header);
+
     Adaptation::History::Pointer ah = request->adaptLogHistory();
     if (ah != NULL) {
         // retrying=false because ecap never retries transactions
         adaptHistoryId = ah->recordXactStart(service().cfg().key, current_time, false);
+        typedef Notes::iterator ACAMLI;
+        for (ACAMLI i = Adaptation::Config::metaHeaders.begin(); i != Adaptation::Config::metaHeaders.end(); ++i) {
+            const char *v = (*i)->match(request, reply);
+            if (v) {
+                if (ah->metaHeaders == NULL)
+                    ah->metaHeaders = new NotePairs();
+                if (!ah->metaHeaders->hasPair((*i)->key.termedBuf(), v))
+                    ah->metaHeaders->add((*i)->key.termedBuf(), v);
+            }
+        }
     }
 
     theMaster->start();
@@ -412,7 +476,6 @@ Adaptation::Ecap::XactionRep::updateHistory(HttpMsg *adapted)
         adaptedReq->adaptHistoryImport(*request);
 }
 
-
 void
 Adaptation::Ecap::XactionRep::vbDiscard()
 {
@@ -626,7 +689,6 @@ Adaptation::Ecap::XactionRep::status() const
     if (vbProductionFinished)
         buf.append(".", 1);
 
-
     buf.Printf(" A%d", static_cast<int>(proxyingAb));
 
     if (proxyingAb == opOn) {