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/10/27 01:41:17 UTC

svn commit: r1402721 - in /incubator/mesos/trunk/third_party/libprocess/src: decoder.hpp encoder.hpp

Author: benh
Date: Fri Oct 26 23:41:17 2012
New Revision: 1402721

URL: http://svn.apache.org/viewvc?rev=1402721&view=rev
Log:
Encoding HTTP Responses with gzip by default. Decoding incoming
compressed messages.

From: Ben Mahler <be...@gmail.com>
Review: https://reviews.apache.org/r/7681

Modified:
    incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp
    incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp

Modified: incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp?rev=1402721&r1=1402720&r2=1402721&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp Fri Oct 26 23:41:17 2012
@@ -11,6 +11,7 @@
 #include <process/socket.hpp>
 
 #include <stout/foreach.hpp>
+#include <stout/gzip.hpp>
 #include <stout/try.hpp>
 
 
@@ -117,9 +118,20 @@ private:
     if (decoded.isError()) {
       return 1;
     }
+    decoder->request->query = http::query::parse(decoded.get());
+
+    Option<std::string> encoding =
+      decoder->request->headers.get("Content-Encoding");
+    if (encoding.isSome() && encoding.get() == "gzip") {
+      Try<std::string> decompressed = gzip::decompress(decoder->request->body);
+      if (decompressed.isError()) {
+        return 1;
+      }
+      decoder->request->body = decompressed.get();
+      decoder->request->headers["Content-Length"] =
+        decoder->request->body.length();
+    }
 
-    decoder->query = decoded.get();
-    decoder->request->query = http::query::parse(decoder->query);
     decoder->requests.push_back(decoder->request);
     decoder->request = NULL;
     return 0;
@@ -297,8 +309,20 @@ private:
     hashmap<uint16_t, std::string>::const_iterator it =
         http::statuses.find(decoder->parser.status_code);
     assert(it != http::statuses.end());
-
     decoder->response->status = it->second;
+
+    Option<std::string> encoding =
+      decoder->response->headers.get("Content-Encoding");
+    if (encoding.isSome() && encoding.get() == "gzip") {
+      Try<std::string> decompressed = gzip::decompress(decoder->response->body);
+      if (decompressed.isError()) {
+        return 1;
+      }
+      decoder->response->body = decompressed.get();
+      decoder->response->headers["Content-Length"] =
+        decoder->response->body.length();
+    }
+
     decoder->responses.push_back(decoder->response);
     decoder->response = NULL;
     return 0;

Modified: incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp?rev=1402721&r1=1402720&r2=1402721&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp Fri Oct 26 23:41:17 2012
@@ -9,6 +9,7 @@
 #include <process/process.hpp>
 
 #include <stout/foreach.hpp>
+#include <stout/gzip.hpp>
 #include <stout/hashmap.hpp>
 #include <stout/numify.hpp>
 
@@ -140,6 +141,22 @@ public:
 
     headers["Date"] = date;
 
+    // Swap the body with a gzip compressed version, if no encoding has
+    // been specified.
+    std::string body = response.body;
+
+    if (response.type == http::Response::BODY &&
+        !headers.contains("Content-Encoding")) {
+      Try<std::string> compressed = gzip::compress(body);
+      if (compressed.isError()) {
+        LOG(WARNING) << "Failed to gzip response body: " << compressed.error();
+      } else {
+        body = compressed.get();
+        headers["Content-Length"] = stringify(body.length());
+        headers["Content-Encoding"] = "gzip";
+      }
+    }
+
     foreachpair (const std::string& key, const std::string& value, headers) {
       out << key << ": " << value << "\r\n";
     }
@@ -151,7 +168,7 @@ public:
       out << "Content-Length: 0\r\n";
     } else if (response.type == http::Response::BODY &&
                !headers.contains("Content-Length")) {
-      out << "Content-Length: " << response.body.size() << "\r\n";
+      out << "Content-Length: " << body.size() << "\r\n";
     }
 
     // Use a CRLF to mark end of headers.
@@ -162,10 +179,10 @@ public:
       // If the Content-Length header was supplied, only write as much data
       // as the length specifies.
       Result<uint32_t> length = numify<uint32_t>(headers.get("Content-Length"));
-      if (length.isSome() && length.get() <= response.body.length()) {
-        out.write(response.body.data(), length.get());
+      if (length.isSome() && length.get() <= body.length()) {
+        out.write(body.data(), length.get());
       } else {
-        out.write(response.body.data(), response.body.size());
+        out.write(body.data(), body.size());
       }
     }