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/13 02:55:06 UTC
svn commit: r1397752 - in /incubator/mesos/trunk/third_party/libprocess:
include/process/http.hpp src/decoder.hpp src/encoder.hpp src/tests.cpp
Author: benh
Date: Sat Oct 13 00:55:06 2012
New Revision: 1397752
URL: http://svn.apache.org/viewvc?rev=1397752&view=rev
Log:
Updated process::http::Response (and subclasses) to require more
explicit distinction between an empty body and no body. Also put in
place the foundation for streaming via pipes.
Review: https://reviews.apache.org/r/7542
Modified:
incubator/mesos/trunk/third_party/libprocess/include/process/http.hpp
incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp
incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp
incubator/mesos/trunk/third_party/libprocess/src/tests.cpp
Modified: incubator/mesos/trunk/third_party/libprocess/include/process/http.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/process/http.hpp?rev=1397752&r1=1397751&r2=1397752&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/process/http.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/include/process/http.hpp Sat Oct 13 00:55:06 2012
@@ -38,51 +38,71 @@ struct Request
struct Response
{
- Response(const std::string& _body = "")
+ Response()
+ : type(NONE)
+ {}
+
+ Response(const std::string& _body)
: type(BODY),
body(_body)
{
- if (!body.empty()) {
- headers["Content-Length"] = stringify(body.size());
- }
+ headers["Content-Length"] = stringify(body.size());
}
// TODO(benh): Add major/minor version.
std::string status;
hashmap<std::string, std::string> headers;
- // TODO(benh): Make body a stream (channel) instead, and allow a
- // response to be returned without forcing the stream to be
- // finished.
-
- // Either provide a 'body' or an absolute 'path' to a file. If a
- // path is specified then we will attempt to perform a 'sendfile'
- // operation on the file. In either case you are expected to
- // properly specify the 'Content-Type' header, but the
- // 'Content-Length' header will be filled in for you if you specify
- // a path. Distinguish between the two using 'type' below.
+ // Either provide a "body", an absolute "path" to a file, or a
+ // "pipe" for streaming a response. Distinguish between the cases
+ // using 'type' below.
+ //
+ // BODY: Uses 'body' as the body of the response.
+ //
+ // PATH: Attempts to perform a 'sendfile' operation on the file
+ // found at 'path'.
+ //
+ // PIPE: Splices data from 'pipe' using 'Transfer-Encoding=chunked'.
+ // Note that the read end of the pipe will be closed by libprocess
+ // either after the write end has been closed or if the socket the
+ // data is being spliced to has been closed (i.e., no body is
+ // listening any longer). This can cause writes to the pipe to
+ // generate a SIGPIPE (which will terminate your program unless you
+ // explicitly ignore them or handle them).
+ //
+ // In all cases (BODY, PATH, PIPE), you are expected to properly
+ // specify the 'Content-Type' header, but the 'Content-Length' and
+ // or 'Transfer-Encoding' headers will be filled in for you.
enum {
+ NONE,
BODY,
- PATH
+ PATH,
+ PIPE
} type;
std::string body;
std::string path;
+ int pipe; // See comment above regarding the semantics for closing.
};
struct OK : Response
{
- OK(const std::string& body = "")
- : Response(body)
+ OK()
+ {
+ status = "200 OK";
+ }
+
+ OK(const std::string& body) : Response(body)
{
status = "200 OK";
}
OK(const JSON::Value& value,
const Option<std::string>& jsonp = Option<std::string>::none())
- : Response()
{
+ type = BODY;
+
status = "200 OK";
std::ostringstream out;
@@ -108,7 +128,7 @@ struct OK : Response
struct TemporaryRedirect : Response
{
- TemporaryRedirect(const std::string& url) : Response("")
+ TemporaryRedirect(const std::string& url)
{
status = "307 Temporary Redirect";
headers["Location"] = url;
@@ -118,7 +138,13 @@ struct TemporaryRedirect : Response
struct BadRequest : Response
{
- BadRequest(const std::string& body = "") : Response(body)
+ BadRequest()
+ {
+ status = "400 Bad Request";
+ }
+
+ BadRequest(const std::string& body)
+ : Response(body)
{
status = "400 Bad Request";
}
@@ -127,7 +153,12 @@ struct BadRequest : Response
struct NotFound : Response
{
- NotFound(const std::string& body = "") : Response(body)
+ NotFound()
+ {
+ status = "404 Not Found";
+ }
+
+ NotFound(const std::string& body) : Response(body)
{
status = "404 Not Found";
}
@@ -136,7 +167,12 @@ struct NotFound : Response
struct InternalServerError : Response
{
- InternalServerError(const std::string& body = "") : Response(body)
+ InternalServerError()
+ {
+ status = "500 Internal Server Error";
+ }
+
+ InternalServerError(const std::string& body) : Response(body)
{
status = "500 Internal Server Error";
}
@@ -145,7 +181,12 @@ struct InternalServerError : Response
struct ServiceUnavailable : Response
{
- ServiceUnavailable(const std::string& body = "") : Response(body)
+ ServiceUnavailable()
+ {
+ status = "503 Service Unavailable";
+ }
+
+ ServiceUnavailable(const std::string& body) : Response(body)
{
status = "503 Service Unavailable";
}
@@ -185,6 +226,8 @@ inline hashmap<std::string, std::string>
return result;
}
+} // namespace query {
+
// Returns a percent-encoded string according to RFC 3986.
// The input string must not already be percent encoded.
@@ -271,8 +314,6 @@ inline Try<std::string> decode(const std
}
-} // namespace query {
-
// Sends a blocking HTTP GET request to the process with the given upid.
// Returns the HTTP response from the process, read asynchronously.
// The query is the string version of the path and arguments:
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=1397752&r1=1397751&r2=1397752&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/decoder.hpp Sat Oct 13 00:55:06 2012
@@ -113,7 +113,7 @@ private:
// std::cout << " method: " << decoder->request->method << std::endl;
// std::cout << " path: " << decoder->request->path << std::endl;
// Parse the query key/values.
- Try<std::string> decoded = http::query::decode(decoder->query);
+ Try<std::string> decoded = http::decode(decoder->query);
if (decoded.isError()) {
return 1;
}
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=1397752&r1=1397751&r2=1397752&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/encoder.hpp Sat Oct 13 00:55:06 2012
@@ -143,15 +143,23 @@ public:
out << key << ": " << value << "\r\n";
}
- // Make sure at least the "Content-Length" header is present in
- // order to signal to a client the end of a response.
- if (headers.count("Content-Length") == 0) {
+ // Add a Content-Length header if the response is of type "none"
+ // or "body" and no Content-Length header has been supplied.
+ if (response.type == http::Response::NONE &&
+ headers.contains("Content-Length")) {
+ 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";
}
+ // Use a CRLF to mark end of headers.
out << "\r\n";
- out.write(response.body.data(), response.body.size());
+ // Add the body if necessary.
+ if (response.type == http::Response::BODY) {
+ out.write(response.body.data(), response.body.size());
+ }
return out.str();
}
Modified: incubator/mesos/trunk/third_party/libprocess/src/tests.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/tests.cpp?rev=1397752&r1=1397751&r2=1397752&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/tests.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/tests.cpp Sat Oct 13 00:55:06 2012
@@ -1304,20 +1304,20 @@ TEST(Process, async)
TEST(HTTP, encode)
{
std::string unencoded = "a$&+,/:;=?@ \"<>#%{}|\\^~[]`\x19\x80\xFF\x00";
- std::string encoded = http::query::encode(unencoded);
+ std::string encoded = http::encode(unencoded);
EXPECT_EQ("a%24%26%2B%2C%2F%3A$3B%3D%3F%40%20%22%3C%3E%32"
"%25%7B%7D%7C%5C%5E%7E%5B%5D%60%19%80%FF%00",
encoded);
- Try<std::string> decoded = http::query::decode(encoded);
+ Try<std::string> decoded = http::decode(encoded);
EXPECT_TRUE(decoded.isSome()) << decoded.error();
EXPECT_EQ(unencoded, decoded.get());
- EXPECT_TRUE(http::query::decode("%").isError());
- EXPECT_TRUE(http::query::decode("%1").isError());
- EXPECT_TRUE(http::query::decode("%;1").isError());
- EXPECT_TRUE(http::query::decode("%1;").isError());
+ EXPECT_TRUE(http::decode("%").isError());
+ EXPECT_TRUE(http::decode("%1").isError());
+ EXPECT_TRUE(http::decode("%;1").isError());
+ EXPECT_TRUE(http::decode("%1;").isError());
}