You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2012/08/09 00:50:27 UTC

svn commit: r1371005 - /incubator/mesos/trunk/src/files/files.cpp

Author: benh
Date: Wed Aug  8 22:50:27 2012
New Revision: 1371005

URL: http://svn.apache.org/viewvc?rev=1371005&view=rev
Log:
Added JSONP support to 'files' abstraction
(https://reviews.apache.org/r/6477).

Modified:
    incubator/mesos/trunk/src/files/files.cpp

Modified: incubator/mesos/trunk/src/files/files.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/files/files.cpp?rev=1371005&r1=1371004&r2=1371005&view=diff
==============================================================================
--- incubator/mesos/trunk/src/files/files.cpp (original)
+++ incubator/mesos/trunk/src/files/files.cpp Wed Aug  8 22:50:27 2012
@@ -40,8 +40,8 @@ namespace internal {
 class FilesProcess : public Process<FilesProcess>
 {
 public:
-  FilesProcess() {}
-  virtual ~FilesProcess() {}
+  FilesProcess();
+  virtual ~FilesProcess();
 
   // Files implementation.
   Future<bool> attach(const string& path, const string& name);
@@ -51,7 +51,7 @@ protected:
   virtual void initialize();
 
 private:
-  // JSON endpoints.
+  // HTTP endpoints.
   Future<Response> browse(const Request& request);
   Future<Response> read(const Request& request);
 
@@ -59,6 +59,15 @@ private:
 };
 
 
+FilesProcess::FilesProcess()
+  : ProcessBase("files")
+{}
+
+
+FilesProcess::~FilesProcess()
+{}
+
+
 void FilesProcess::initialize()
 {
   route("/browse.json", &FilesProcess::browse);
@@ -144,6 +153,12 @@ Future<Response> FilesProcess::read(cons
     length = result.get();
   }
 
+  Option<string> jsonp;
+
+  if (pairs.count("jsonp") > 0 && pairs["jsonp"].size() > 0) {
+    jsonp = pairs["jsonp"].back();
+  }
+
   // Now try and see if this name has been attached. We check for the
   // longest possible prefix match and if found append any suffix to
   // the attached path (provided the path is to a directory).
@@ -158,9 +173,9 @@ Future<Response> FilesProcess::read(cons
       // Determine the final path: if it's a directory, append the
       // suffix, if it's not a directory and there is a suffix, return
       // '404 Not Found'.
-      string path;
-      if (os::exists(paths[prefix], true)) {
-        path = paths[prefix] + "/" + suffix;
+      string path = paths[prefix];
+      if (utils::os::exists(path, true)) {
+        path += "/" + suffix;
 
         // Canonicalize the absolute path and make sure the result
         // doesn't break out of the chroot (i.e., resolving any '..'
@@ -175,6 +190,8 @@ Future<Response> FilesProcess::read(cons
 
         path = result.get();
       } else if (suffix != "") {
+        // Request is assuming attached path is a directory, but it is
+        // not! Rather than 'Bad Request', treat this as 'Not Found'.
         return NotFound();
       }
 
@@ -243,12 +260,24 @@ Future<Response> FilesProcess::read(cons
 
       std::ostringstream out;
 
+      if (jsonp.isSome()) {
+        out << jsonp.get() << "(";
+      }
+
       JSON::render(out, object);
 
       OK response;
-      response.headers["Content-Type"] = "application/json";
-      response.headers["Content-Length"] = stringify(out.str().size());
+
+      if (jsonp.isSome()) {
+        out << ");";
+        response.headers["Content-Type"] = "text/javascript";
+      } else {
+        response.headers["Content-Type"] = "application/json";
+      }
+
+      response.headers["Content-Length"] = utils::stringify(out.str().size());
       response.body = out.str().data();
+
       return response;
     }
   }