Revision: 6036
Author: [email protected]
Date: Sun Aug 30 08:28:05 2009
Log: Changes from review feedback.

http://code.google.com/p/google-web-toolkit/source/detail?r=6036

Modified:
  /changes/jat/plugins/plugins/common/AllowedConnections.cpp
  /changes/jat/plugins/plugins/common/AllowedConnections.h
  /changes/jat/plugins/plugins/common/HostChannel.cpp
  /changes/jat/plugins/plugins/common/Makefile
  /changes/jat/plugins/plugins/config.mk

=======================================
--- /changes/jat/plugins/plugins/common/AllowedConnections.cpp  Sat Aug 29  
13:11:38 2009
+++ /changes/jat/plugins/plugins/common/AllowedConnections.cpp  Sun Aug 30  
08:28:05 2009
@@ -17,18 +17,17 @@
  #include "Debug.h"
  #include <string>
  #include <cstring>
-#include <algorithm>
  #include <vector>

  #include "AllowedConnections.h"

+
  // TODO(jat): do we need to protect against parallel access to static  
state?
  //    current browsers only use one thread, but Chrome is multithreaded,  
though
  //    it isn't clear if plugins see parallel execution.  For now, we will
  //    assume the caller takes responsibility for making sure there are no
  //    concurrent calls into AllowedConnections.
-std::vector<std::string> AllowedConnections::blackList;
-std::vector<std::string> AllowedConnections::whiteList;
+std::vector<AllowedConnections::Rule> AllowedConnections::rules;

  /**
   * Get the host portion of the URL, not including the port.
@@ -36,7 +35,7 @@
   * @return the host portion of the URL, or the unmodified URL if it does  
not appear
   *     to be valid
   */
-static std::string getHostFromUrl(const std::string& url) {
+std::string AllowedConnections::getHostFromUrl(const std::string& url) {
    int protoEnd = url.find("://");
    if (protoEnd == std::string::npos) {
      Debug::log(Debug::Debugging) << "getHostFromUrl(" << url
@@ -56,45 +55,42 @@
    return host;
  }

-static bool matchesList(const std::string& str, const  
std::vector<std::string>& list) {
-  // TODO(jat): support regex matches, commented code will be used then
-//    for (std::vector<std::string>::const_iterator it = list.begin();
-//         it != list.end(); ++it) {
-//      printf("  comparing to %s\n", it->c_str());
-//      if (str == *it) {
-//        return true;
-//      }
-//    }
-//    return false;
-  return std::find(list.begin(), list.end(), str) != list.end();
-}
-
-bool AllowedConnections::isAllowed(const std::string& url) {
+bool AllowedConnections::matchesRule(const std::string& url,
+    bool* allowed) {
    std::string host = getHostFromUrl(url);
    // always allow localhost, localhost.* or 127.0.0.1 for the host
-  if (host == "localhost" || host.find("localhost.") == 0 || host  
== "127.0.0.1") {
+  // TODO(jat): try and get IP addresses of local interfaces?
+  if (host == "localhost" || host.find("localhost.") == 0
+      || host == "127.0.0.1") {
+    *allowed = true;
      return true;
    }
-  if (matchesList(host, blackList)) {
-    return false;
-  }
-  if (matchesList(host, whiteList)) {
-    return true;
-  }
+  Debug::log(Debug::Spam) << "Checking host " << host << Debug::flush;
+  for (std::vector<AllowedConnections::Rule>::const_iterator it =  
rules.begin();
+       it != rules.end(); ++it) {
+    Debug::log(Debug::Spam) << "  comparing to " << it->getPattern()
+        << Debug::flush;
+    // TODO(jat): add support for regexes
+    if (host == it->getPattern()) {
+      *allowed = !it->isExcluded();
+      return true;
+    }
+  }
+  Debug::log(Debug::Info)
+      << "GWT Development Mode connection requested by unknown web server "
+      << host << Debug::flush;
    return false;
  }

-void AllowedConnections::addRule(const std::string& pattern, bool exclude)  
{
-  if (exclude) {
-    blackList.push_back(pattern);
-  } else {
-    whiteList.push_back(pattern);
-  }
+void AllowedConnections::addRule(const std::string& pattern,
+    bool exclude) {
+  Debug::log(Debug::Spam) << "AllowedConnections::addRule(pattern=" <<  
pattern
+      << ", excl=" << exclude << Debug::flush;
+  rules.push_back(AllowedConnections::Rule(pattern, exclude));
  }

  void AllowedConnections::clearRules() {
-  blackList.clear();
-  whiteList.clear();
+  rules.clear();
  }

  void AllowedConnections::initFromAccessList(const std::string& accessList)  
{
@@ -103,8 +99,8 @@
    for (int i = 0; i < n; ) {
      bool exclude = false;
      if (accessList[i] == '!') {
-         exclude = true;
-           ++i;
+      exclude = true;
+      ++i;
      }
      int comma = i - 1; // for pre-increment below
      while (++comma < n && accessList[comma] != ','); // empty
=======================================
--- /changes/jat/plugins/plugins/common/AllowedConnections.h    Sat Aug 29  
13:11:38 2009
+++ /changes/jat/plugins/plugins/common/AllowedConnections.h    Sun Aug 30  
08:28:05 2009
@@ -18,6 +18,7 @@

  #include <string>
  #include <vector>
+#include <utility>

  /**
   * Manages rules to control access to other sites from the plugin.  This is
@@ -33,43 +34,69 @@
     * @param pattern pattern to match
     * @param exclude true if matches should be excluded instead of included
     */
-  static void addRule(const std::string& pattern, bool exclude = true);
+  static void addRule(const std::string& pattern, bool exclude = false);

    /**
     * Clear all rules.
     */
    static void clearRules();

+  /**
+   * Get the host portion of the URL, not including the port.
+   *
+   * @return the host portion of the URL, or the unmodified URL if it does  
not
+   *     appear to be valid
+   */
+  static std::string getHostFromUrl(const std::string& url);
+
    /**
     * Clear any existing rules and reinitialize from the supplied access  
list.
     *
     * This access list is of the form:
     *    [!]pattern,[!]pattern...
-   * where the optional exlamation indicates the following pattern is to be
+   * where the optional exclamation indicates the following pattern is to  
be
     * excluded, and an arbitrary number of patterns may be supplied with the
     * first match being used.  Each pattern currently is only an exact  
literal
     * match against the host name, but will be extended to support simple
-   * wildcad patterns.
+   * wildcard patterns.
     */
    static void initFromAccessList(const std::string& accessList);

    /**
-   * Returns true if a connection to the requested target is allowed.
-   * If any excluded pattern matches, the url is not allowed, and if no
-   * included pattern matches the url is not allowed.  A host name of
-   * localhost or 127.0.0.1 is always allowed.
+   * Returns true if the server for the requested URL matched any rule in
+   * our access list, and sets a flag based on whether that rule permits or
+   * denies the request.  A host name of localhost or 127.0.0.1 is always
+   * allowed.
     *
     * @param url url of page initiating connection
+   * @param allowed pointer to return value indiciating that this URL  
should
+   *     be allowed to initiate GWT development mode connections
+   * @return true if url matched a rule
     */
-  static bool isAllowed(const std::string& url);
+  static bool matchesRule(const std::string& url, bool* allowed);

  private:
    AllowedConnections() {
    }

-  static std::vector<std::string> blackList;
-  static std::vector<std::string> whiteList;
-
+  /**
+   * Internal class used for representing a rule.
+   */
+  class Rule : std::pair<std::string, bool> {
+  public:
+    Rule(const std::string& pattern, bool exclude)
+        : std::pair<std::string, bool>(pattern, exclude) {}
+
+    const std::string& getPattern() const {
+      return first;
+    }
+
+    bool isExcluded() const {
+      return second;
+    }
+  };
+
+  static std::vector<Rule> rules;
  };

  #endif
=======================================
--- /changes/jat/plugins/plugins/common/HostChannel.cpp Sat Aug 29 13:11:38  
2009
+++ /changes/jat/plugins/plugins/common/HostChannel.cpp Sun Aug 30 08:28:05  
2009
@@ -57,18 +57,20 @@
  ByteOrder HostChannel::byteOrder;

  bool HostChannel::connectToHost(const char* host, unsigned port) {
-  Debug::log(Debug::Info) << "Initiating GWT hosted mode connection to  
host "
-      << host << ", port " << port << Debug::flush;
    if (!port) {
      port = 9997;
    }
+  Debug::log(Debug::Info)
+      << "Initiating GWT Development Mode connection to host " << host
+      << ", port " << port << Debug::flush;
    return sock.connect(host, port);
  }

  bool HostChannel::init(SessionHandler* handler, int minProtoVers,
      int maxProtoVers, const std::string& hostedHtmlVers) {
-  Debug::log(Debug::Info) << "initializing connection: proto=" <<  
minProtoVers
-      << "-" << maxProtoVers << ", hostedHtmlVersion=" << hostedHtmlVers
+  Debug::log(Debug::Debugging)
+      << "  negotiating versions - we support protocol " << minProtoVers
+      << " through " << maxProtoVers << ", hostedHtmlVersion=" <<  
hostedHtmlVers
        << Debug::flush;
    // TODO(jat): support transport selection
    CheckVersionsMessage::send(*this, minProtoVers, maxProtoVers,  
hostedHtmlVers);
=======================================
--- /changes/jat/plugins/plugins/common/Makefile        Sat Aug 29 13:11:38 2009
+++ /changes/jat/plugins/plugins/common/Makefile        Sun Aug 30 08:28:05 2009
@@ -44,6 +44,8 @@
  $(OBJDIR)/%.o: %.cpp
        $(CXX) $(BASECFLAGS) $(ALLARCHCFLAGS) -c $< -o $@

+$(OBJDIR)/%.o: DebugLevel.h
+
  .PHONY: clean depend testdebug

  testdebug:
=======================================
--- /changes/jat/plugins/plugins/config.mk      Sat Aug 29 13:11:38 2009
+++ /changes/jat/plugins/plugins/config.mk      Sun Aug 30 08:28:05 2009
@@ -78,7 +78,7 @@
  # Set OS as well as CFLAGS, CXX, and other common make variables
  ifeq ($(shell uname),Linux)
  OS=linux
-BASECFLAGS= -g -O2 -fPIC $(INC) -rdynamic -m$(FLAG32BIT)
+BASECFLAGS= -g -O2 -fPIC $(INC) -rdynamic
  ARCHCFLAGS=-m$(FLAG32BIT)
  ALLARCHCFLAGS=$(ARCHCFLAGS)
  endif

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to