Revision: 3947
Author: mikesamuel
Date: Tue Jan 19 16:43:06 2010
Log: Fix content type guessing in ancillary servlet, make all ancillary servlet links relative, and fix typo in one of the help pages.
http://code.google.com/p/google-caja/source/detail?r=3947

Added:
 /trunk/tests/com/google/caja/ancillary/servlet/GuessContentTypeTest.java
Modified:
 /trunk/src/com/google/caja/ancillary/servlet/CajaWebToolsServlet.java
 /trunk/src/com/google/caja/ancillary/servlet/GuessContentType.java
 /trunk/src/com/google/caja/ancillary/servlet/HelpPage.java
 /trunk/src/com/google/caja/ancillary/servlet/Processor.java
 /trunk/src/com/google/caja/ancillary/servlet/Request.java
 /trunk/src/com/google/caja/ancillary/servlet/StaticFiles.java
 /trunk/src/com/google/caja/ancillary/servlet/Verb.java
 /trunk/src/com/google/caja/ancillary/servlet/files/NO_SIDE_EFFECT_tip.html
 /trunk/tests/com/google/caja/ancillary/servlet/CajaWebToolsServletTest.java
 /trunk/tests/com/google/caja/ancillary/servlet/StaticFilesTest.java

=======================================
--- /dev/null
+++ /trunk/tests/com/google/caja/ancillary/servlet/GuessContentTypeTest.java Tue Jan 19 16:43:06 2010
@@ -0,0 +1,35 @@
+// Copyright (C) 2010 Google Inc.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.caja.ancillary.servlet;
+
+import com.google.caja.util.CajaTestCase;
+import com.google.caja.util.ContentType;
+
+public class GuessContentTypeTest extends CajaTestCase {
+  public final void testGuess() throws Exception {
+    assertEquals(
+        ContentType.JS,
+        GuessContentType.guess(null, null, "foo()"));
+    assertEquals(
+        ContentType.CSS,
+        GuessContentType.guess(null, null, "foo { color: red }"));
+    assertEquals(
+        ContentType.HTML,
+        GuessContentType.guess(null, null, "<foo>"));
+    assertEquals(
+        ContentType.JS,
+        GuessContentType.guess(null, null, "foo() ? bar : baz()"));
+  }
+}
=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/CajaWebToolsServlet.java Mon Jan 18 15:50:44 2010 +++ /trunk/src/com/google/caja/ancillary/servlet/CajaWebToolsServlet.java Tue Jan 19 16:43:06 2010
@@ -86,7 +86,8 @@
     if (null == reqPath || "/".equals(reqPath)) {
       try {
         String query = req.getQueryString();
- URI indexUri = new URI(null, null, Verb.INDEX.requestPath, query, null);
+        URI indexUri = new URI(
+            null, null, "/" + Verb.INDEX.relRequestPath, query, null);
         resp.sendRedirect(indexUri.toString());
       } catch (URISyntaxException ex) {
         ex.printStackTrace();
@@ -198,19 +199,21 @@
    */
   Result handle(String reqPath, List<Pair<String, String>> params) {
     MessageQueue mq = new SimpleMessageQueue();
-    Request req;
-    {
+    Request req = null;
+    if (reqPath.startsWith("/")) {
       // The verb is specified in the path, but in the index page, there is
// a select box for the verb, so for /index, the param processing below
       // might set the verb in request.
-      Verb verb = Verb.fromReqPath(reqPath);
-      if (verb == null) {
-        return errorPage(
-            404, "File not found " + reqPath + ".  Expected a path in "
-            + EnumSet.allOf(Verb.class),
-            mq, new Request());
-      }
-      req = Request.create(verb, staticFiles, userAgentDb);
+      Verb verb = Verb.fromRelReqPath(reqPath.substring(1));
+      if (verb != null) {
+        req = Request.create(verb, staticFiles, userAgentDb);
+      }
+    }
+    if (req == null) {
+      return errorPage(
+          404, "File not found " + reqPath + ".  Expected a path in "
+          + EnumSet.allOf(Verb.class),
+          mq, new Request());
     }

     List<Job> inputJobs = Lists.newArrayList();
=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/GuessContentType.java Wed Dec 23 23:20:46 2009 +++ /trunk/src/com/google/caja/ancillary/servlet/GuessContentType.java Tue Jan 19 16:43:06 2010
@@ -64,6 +64,7 @@
       // Try and lex and see what happens.
       CharProducer cp = CharProducer.Factory.fromString(
           code, FilePosition.UNKNOWN);
+      boolean sawColon = false;
       try {
         CssLexer cssLexer = new CssLexer(
             cp, DevNullMessageQueue.singleton(), false);
@@ -71,11 +72,14 @@
         while (cssLexer.hasNext()) {
           Token<CssTokenType> t = cssLexer.next();
           if ("if".equals(t.text) || "while".equals(t.text)
-              || "for".equals(t.text) || "return".equals(t.text)) {
+              || "for".equals(t.text) || "return".equals(t.text)
+              || "?".equals(t.text)) {
             contentType = ContentType.JS;
             break;
           }
-        }
+          if (!sawColon && ":".equals(t.text)) { sawColon = true; }
+        }
+        if (!sawColon) { contentType = ContentType.JS; }
       } catch (ParseException ex) {
         contentType = ContentType.JS;
       }
=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/HelpPage.java Mon Nov 30 20:44:55 2009 +++ /trunk/src/com/google/caja/ancillary/servlet/HelpPage.java Tue Jan 19 16:43:06 2010
@@ -47,7 +47,7 @@
          + " apply the Caja web tools to your web application.  Use the"
          + " <a hr...@index>interactive form</a> to experiment.</p>"
          + "@verbs</body></html>"),
-        "index", Verb.INDEX.requestPath,
+        "index", Verb.INDEX.relRequestPath,
         "verbs", renderVerbHelp(b),
         "cacheId", staticFiles.cacheId));
     return out;
@@ -58,7 +58,7 @@
     Element verbs = b.getDocument().createElementNS(HTML_NS, "ul");
     verbs.setAttributeNS(HTML_NS, "class", "verbs-help");
     for (Verb v : Verb.values()) {
-      Object reqPath = v.requestPath;
+      Object reqPath = v.relRequestPath;
       if (v == Verb.INDEX) {
reqPath = b.substV("<a hr...@reqpath>@reqPath</a>", "reqPath", reqPath);
       }
=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/Processor.java Mon Jan 18 15:50:44 2010 +++ /trunk/src/com/google/caja/ancillary/servlet/Processor.java Tue Jan 19 16:43:06 2010
@@ -24,6 +24,7 @@
 import com.google.caja.lang.html.HTML;
 import com.google.caja.lexer.CharProducer;
 import com.google.caja.lexer.CssTokenType;
+import com.google.caja.lexer.ExternalReference;
 import com.google.caja.lexer.FilePosition;
 import com.google.caja.lexer.HtmlLexer;
 import com.google.caja.lexer.HtmlTokenType;
@@ -391,7 +392,18 @@
   private void extractJobs(Node node, URI baseUri, List<Job> out) {
     HtmlEmbeddedContentFinder f = new HtmlEmbeddedContentFinder(
         req.htmlSchema, req.baseUri, mq, req.mc);
-    PluginEnvironment env = null;
+    PluginEnvironment env = new PluginEnvironment() {
+      @Override
+      public CharProducer loadExternalResource(
+          ExternalReference ref, String mimeType) {
+        return null;
+      }
+
+      @Override
+      public String rewriteUri(ExternalReference uri, String mimeType) {
+        return null;
+      }
+    };
     for (EmbeddedContent c : f.findEmbeddedContent(node)) {
       if (c.getType() != null) {
         Node src = c.getSource();
=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/Request.java Mon Jan 18 15:50:44 2010 +++ /trunk/src/com/google/caja/ancillary/servlet/Request.java Tue Jan 19 16:43:06 2010
@@ -141,7 +141,7 @@
         public void handle(String name, String val, Request c)
             throws BadInputException {
           throw new BadInputException(
-              "Param " + name + " not allowed on " + v.requestPath, null);
+ "Param " + name + " not allowed on " + v.relRequestPath, null);
         }
public String manual() { throw new UnsupportedOperationException(); }
       };
=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/StaticFiles.java Mon Jan 18 15:50:44 2010 +++ /trunk/src/com/google/caja/ancillary/servlet/StaticFiles.java Tue Jan 19 16:43:06 2010
@@ -111,7 +111,7 @@
             min.otype = t;
Processor p = new Processor(min, DevNullMessageQueue.singleton());
             try {
-              Job j = p.parse(cp, t, null, is.getUri());
+              Job j = p.parse(cp.clone(), t, null, is.getUri());
               List<Job> out = p.process(Lists.newArrayList(j));
               if (out.size() == 1) {
                 content = p.reduce(out);
@@ -154,6 +154,10 @@
       }
     } else {
       resp.setStatus(404);
+      resp.setContentType("text/plain");
+      Writer out = resp.getWriter();
+      out.write("404 - I have no response to that.");
+      out.close();
     }
   }

=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/Verb.java Mon Nov 30 20:44:55 2009 +++ /trunk/src/com/google/caja/ancillary/servlet/Verb.java Tue Jan 19 16:43:06 2010
@@ -10,31 +10,34 @@
  * @author [email protected]
  */
 enum Verb {
-  DOC("/doc", "generates JSDoc documentation"),
-  ECHO("/echo", "echoes the input after optimizing&|minifying"),
-  HELP("/help", "shows help info"),
-  INDEX("/index", "an interactive HTML form"),
-  LINT("/lint", "checks the input for problematic coding practices"),
+  DOC("doc", "generates JSDoc documentation"),
+  ECHO("echo", "echoes the input after optimizing&|minifying"),
+  HELP("help", "shows help info"),
+  INDEX("index", "an interactive HTML form"),
+  LINT("lint", "checks the input for problematic coding practices"),
   ;

-  final String requestPath;
+  /** Relative request path. */
+  final String relRequestPath;
+  /** Human readable description. */
   final String manual;

-  Verb(String requestPath, String manual) {
-    this.requestPath = requestPath;
+  Verb(String relRequestPath, String manual) {
+    this.relRequestPath = relRequestPath;
     this.manual = manual;
   }

private static final Map<String, Verb> BY_REQUEST_PATH = Maps.newHashMap();
   static {
     for (Verb verb : Verb.values()) {
-      BY_REQUEST_PATH.put(verb.requestPath, verb);
+      BY_REQUEST_PATH.put(verb.relRequestPath, verb);
     }
   }
-  static Verb fromReqPath(String reqPath) {
-    return BY_REQUEST_PATH.get(reqPath);
+  /** @param relReqPath relative request path. */
+  static Verb fromRelReqPath(String relReqPath) {
+    return BY_REQUEST_PATH.get(relReqPath);
   }

   @Override
-  public String toString() { return requestPath; }
-}
+  public String toString() { return relRequestPath; }
+}
=======================================
--- /trunk/src/com/google/caja/ancillary/servlet/files/NO_SIDE_EFFECT_tip.html Mon Nov 30 20:44:55 2009 +++ /trunk/src/com/google/caja/ancillary/servlet/files/NO_SIDE_EFFECT_tip.html Tue Jan 19 16:43:06 2010
@@ -52,6 +52,6 @@
 The optimizer will take out the unnecessary <tt>void</tt>.

 <p>As always, to turn this off, put the error message name
-<code>NO_SIDE_EFFECT</code> in the <a href="ign_help.html>ignores list</a>.
+<code>NO_SIDE_EFFECT</code> in the <a href="ign_help.html">ignores list</a>.
 </body>
 </html>
=======================================
--- /trunk/tests/com/google/caja/ancillary/servlet/CajaWebToolsServletTest.java Mon Jan 18 15:50:44 2010 +++ /trunk/tests/com/google/caja/ancillary/servlet/CajaWebToolsServletTest.java Tue Jan 19 16:43:06 2010
@@ -442,7 +442,7 @@
         .get("/help")
         .expectStatus(200)
         .expectContentType("text/html; charset=UTF-8")
-        .expectContentMatches("<a href=\"/index\">/index</a>")
+        .expectContentMatches("<a href=\"index\">index</a>")
         .expectContentMatches(
             "<tr><th>&amp;i=\u2026</th><td>an input source file</td></tr>")
         .send();
=======================================
--- /trunk/tests/com/google/caja/ancillary/servlet/StaticFilesTest.java Mon Jan 18 15:50:44 2010 +++ /trunk/tests/com/google/caja/ancillary/servlet/StaticFilesTest.java Tue Jan 19 16:43:06 2010
@@ -65,4 +65,14 @@
     f.serve("files/nosuchfile.txt", req, resp);
     assertEquals(404, resp.getStatus());
   }
-}
+
+  public final void testTip() throws Exception {
+    StaticFiles f = new StaticFiles("cacheId");
+    TestHttpServletRequest req = new TestHttpServletRequest("");
+    TestHttpServletResponse resp = new TestHttpServletResponse();
+    f.serve("files/NO_SIDE_EFFECT_tip.html", req, resp);
+    assertEquals(200, resp.getStatus());
+    assertTrue(((String) resp.getOutputObject())
+               .contains("Operation Has No Effect</title>"));
+  }
+}

Reply via email to