| 367 | |
| 368 | |
| 369 | Future<Response> Http::api( |
| 370 | const Request& request, |
| 371 | const Option<Principal>& principal) const |
| 372 | { |
| 373 | // TODO(anand): Add metrics for rejected requests. |
| 374 | |
| 375 | if (slave->state == Slave::RECOVERING) { |
| 376 | return ServiceUnavailable("Agent has not finished recovery"); |
| 377 | } |
| 378 | |
| 379 | if (request.method != "POST") { |
| 380 | return MethodNotAllowed({"POST"}, request.method); |
| 381 | } |
| 382 | |
| 383 | Option<string> contentType_ = request.headers.get("Content-Type"); |
| 384 | if (contentType_.isNone()) { |
| 385 | return BadRequest("Expecting 'Content-Type' to be present"); |
| 386 | } |
| 387 | |
| 388 | ContentType contentType; |
| 389 | if (contentType_.get() == APPLICATION_JSON) { |
| 390 | contentType = ContentType::JSON; |
| 391 | } else if (contentType_.get() == APPLICATION_PROTOBUF) { |
| 392 | contentType = ContentType::PROTOBUF; |
| 393 | } else if (contentType_.get() == APPLICATION_RECORDIO) { |
| 394 | contentType = ContentType::RECORDIO; |
| 395 | } else { |
| 396 | return UnsupportedMediaType( |
| 397 | string("Expecting 'Content-Type' of ") + |
| 398 | APPLICATION_JSON + " or " + APPLICATION_PROTOBUF + |
| 399 | + " or " + APPLICATION_RECORDIO); |
| 400 | } |
| 401 | |
| 402 | Option<ContentType> messageContentType; |
| 403 | Option<string> messageContentType_ = |
| 404 | request.headers.get(MESSAGE_CONTENT_TYPE); |
| 405 | |
| 406 | if (streamingMediaType(contentType)) { |
| 407 | if (messageContentType_.isNone()) { |
| 408 | return BadRequest( |
| 409 | "Expecting '" + stringify(MESSAGE_CONTENT_TYPE) + "' to be" + |
| 410 | " set for streaming requests"); |
| 411 | } |
| 412 | |
| 413 | if (messageContentType_.get() == APPLICATION_JSON) { |
| 414 | messageContentType = Option<ContentType>(ContentType::JSON); |
| 415 | } else if (messageContentType_.get() == APPLICATION_PROTOBUF) { |
| 416 | messageContentType = Option<ContentType>(ContentType::PROTOBUF); |
| 417 | } else { |
| 418 | return UnsupportedMediaType( |
| 419 | string("Expecting '") + MESSAGE_CONTENT_TYPE + "' of " + |
| 420 | APPLICATION_JSON + " or " + APPLICATION_PROTOBUF); |
| 421 | } |
| 422 | } else { |
| 423 | // Validate that a client has not set the "Message-Content-Type" |
| 424 | // header for a non-streaming request. |
| 425 | if (messageContentType_.isSome()) { |
| 426 | return UnsupportedMediaType( |