THRIFT-1657 Chrome browser sending OPTIONS method before POST in xmlHttpRequest
authorRoger Meier <roger@apache.org>
Sun, 23 Dec 2012 19:23:39 +0000 (20:23 +0100)
committerRoger Meier <roger@apache.org>
Sun, 23 Dec 2012 19:23:39 +0000 (20:23 +0100)
Patch: Shantanu Choudhary

lib/cpp/src/thrift/transport/THttpServer.cpp

index 1b3fe7b..1135270 100644 (file)
@@ -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);
 }