From b2f29d84025d9dc2472632cc9bff6c8b5f9bc5b1 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Sun, 23 Dec 2012 20:23:39 +0100 Subject: [PATCH] THRIFT-1657 Chrome browser sending OPTIONS method before POST in xmlHttpRequest Patch: Shantanu Choudhary --- lib/cpp/src/thrift/transport/THttpServer.cpp | 28 ++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/cpp/src/thrift/transport/THttpServer.cpp b/lib/cpp/src/thrift/transport/THttpServer.cpp index 1b3fe7bc..11352705 100644 --- a/lib/cpp/src/thrift/transport/THttpServer.cpp +++ b/lib/cpp/src/thrift/transport/THttpServer.cpp @@ -73,6 +73,34 @@ bool THttpServer::parseStatusLine(char* status) { // POST method ok, looking for content. return true; } + else if (strcmp(method, "OPTIONS") == 0) { + // preflight OPTIONS method, we don't need further content. + // how to graciously close connection? + uint8_t* buf; + uint32_t len; + writeBuffer_.getBuffer(&buf, &len); + + // Construct the HTTP header + std::ostringstream h; + h << + "HTTP/1.1 200 OK" << CRLF << + "Date: " << getTimeRFC1123() << CRLF << + "Access-Control-Allow-Origin: *" << CRLF << + "Access-Control-Allow-Methods: POST, OPTIONS" << CRLF << + "Access-Control-Allow-Headers: Content-Type" << CRLF << + CRLF; + string header = h.str(); + + // Write the header, then the data, then flush + transport_->write((const uint8_t*)header.c_str(), header.size()); + transport_->write(buf, len); + transport_->flush(); + + // Reset the buffer and header variables + writeBuffer_.resetBuffer(); + readHeaders_ = true; + return true; + } throw TTransportException(string("Bad Status (unsupported method): ") + status); } -- 2.17.1