Skip to content

Commit

Permalink
Add content type header helper to consolidate
Browse files Browse the repository at this point in the history
  • Loading branch information
rmccorm4 committed Sep 20, 2023
1 parent f253d06 commit c8633c9
Showing 1 changed file with 33 additions and 68 deletions.
101 changes: 33 additions & 68 deletions src/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,22 @@ namespace triton { namespace server {
} \
} while (false)

#define RETURN_AND_RESPOND_IF_ERR(REQ, X) \
do { \
TRITONSERVER_Error* err__ = (X); \
if (err__ != nullptr) { \
evhtp_headers_add_header( \
req->headers_out, \
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1)); \
EVBufferAddErrorJson((REQ)->buffer_out, err__); \
evhtp_send_reply((REQ), EVHTP_RES_BADREQ); \
TRITONSERVER_ErrorDelete(err__); \
return; \
} \
#define RETURN_AND_RESPOND_IF_ERR(REQ, X) \
do { \
TRITONSERVER_Error* err__ = (X); \
if (err__ != nullptr) { \
EVBufferAddErrorJson((REQ)->buffer_out, err__); \
evhtp_send_reply((REQ), EVHTP_RES_BADREQ); \
TRITONSERVER_ErrorDelete(err__); \
return; \
} \
} while (false)

#define RETURN_AND_RESPOND_WITH_ERR(REQ, CODE, MSG) \
do { \
evhtp_headers_add_header( \
req->headers_out, \
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1)); \
EVBufferAddErrorJson((REQ)->buffer_out, MSG); \
evhtp_send_reply((REQ), CODE); \
return; \
#define RETURN_AND_RESPOND_WITH_ERR(REQ, CODE, MSG) \
do { \
EVBufferAddErrorJson((REQ)->buffer_out, MSG); \
evhtp_send_reply((REQ), CODE); \
return; \
} while (false)

namespace {
Expand All @@ -107,6 +101,12 @@ EVBufferAddErrorJson(evbuffer* buffer, TRITONSERVER_Error* err)
EVBufferAddErrorJson(buffer, message);
}

void
AddContentTypeHeader(evhtp_request_t* req, const char* type)
{
evhtp_headers_add_header(
req->headers_out, evhtp_header_new(kContentTypeHeader, type, 1, 1));
}

} // namespace

Expand Down Expand Up @@ -1259,6 +1259,7 @@ void
HTTPAPIServer::HandleRepositoryIndex(
evhtp_request_t* req, const std::string& repository_name)
{
AddContentTypeHeader(req, "application/json");
if (req->method != htp_method_POST) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
Expand Down Expand Up @@ -1296,10 +1297,6 @@ HTTPAPIServer::HandleRepositoryIndex(
}
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

if (err == nullptr) {
uint32_t flags = 0;
if (ready) {
Expand Down Expand Up @@ -1329,15 +1326,12 @@ HTTPAPIServer::HandleRepositoryControl(
evhtp_request_t* req, const std::string& repository_name,
const std::string& model_name, const std::string& action)
{
AddContentTypeHeader(req, "application/json");
if (req->method != htp_method_POST) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

TRITONSERVER_Error* err = nullptr;
if (!repository_name.empty()) {
err = TRITONSERVER_ErrorNew(
Expand Down Expand Up @@ -1519,6 +1513,7 @@ HTTPAPIServer::HandleModelMetadata(
evhtp_request_t* req, const std::string& model_name,
const std::string& model_version_str)
{
AddContentTypeHeader(req, "application/json");
if (req->method != htp_method_GET) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
Expand All @@ -1529,10 +1524,6 @@ HTTPAPIServer::HandleModelMetadata(
req, EVHTP_RES_BADREQ, "Missing model name in ModelMetadata request");
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

TRITONSERVER_Message* message = nullptr;

int64_t requested_model_version;
Expand Down Expand Up @@ -1561,6 +1552,7 @@ HTTPAPIServer::HandleModelConfig(
evhtp_request_t* req, const std::string& model_name,
const std::string& model_version_str)
{
AddContentTypeHeader(req, "application/json");
if (req->method != htp_method_GET) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
Expand All @@ -1571,10 +1563,6 @@ HTTPAPIServer::HandleModelConfig(
req, EVHTP_RES_BADREQ, "Missing model name in ModelConfig request");
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

TRITONSERVER_Message* message = nullptr;

int64_t requested_model_version;
Expand Down Expand Up @@ -1604,15 +1592,12 @@ HTTPAPIServer::HandleModelStats(
evhtp_request_t* req, const std::string& model_name,
const std::string& model_version_str)
{
AddContentTypeHeader(req, "application/json");
if (req->method != htp_method_GET) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

#ifdef TRITON_ENABLE_STATS
TRITONSERVER_Message* model_stats_message = nullptr;

Expand Down Expand Up @@ -1649,16 +1634,13 @@ HTTPAPIServer::HandleModelStats(
void
HTTPAPIServer::HandleTrace(evhtp_request_t* req, const std::string& model_name)
{
AddContentTypeHeader(req, "application/json");
if ((req->method != htp_method_GET) && (req->method != htp_method_POST)) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
return;
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

#ifdef TRITON_ENABLE_TRACING
TRITONSERVER_InferenceTraceLevel level = TRITONSERVER_TRACE_LEVEL_DISABLED;
uint32_t rate;
Expand Down Expand Up @@ -1905,13 +1887,11 @@ HTTPAPIServer::HandleTrace(evhtp_request_t* req, const std::string& model_name)
void
HTTPAPIServer::HandleLogging(evhtp_request_t* req)
{
AddContentTypeHeader(req, "application/json");
if ((req->method != htp_method_GET) && (req->method != htp_method_POST)) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
}
evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

#ifdef TRITON_ENABLE_LOGGING
// Perform log setting update if requested
Expand Down Expand Up @@ -2056,15 +2036,12 @@ HTTPAPIServer::HandleLogging(evhtp_request_t* req)
void
HTTPAPIServer::HandleServerMetadata(evhtp_request_t* req)
{
AddContentTypeHeader(req, "application/json");
if (req->method != htp_method_GET) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

if (server_metadata_err_ == nullptr) {
evbuffer_add(
req->buffer_out, server_metadata_.c_str(), server_metadata_.size());
Expand All @@ -2080,6 +2057,7 @@ HTTPAPIServer::HandleSystemSharedMemory(
evhtp_request_t* req, const std::string& region_name,
const std::string& action)
{
AddContentTypeHeader(req, "application/json");
if ((action == "status") && (req->method != htp_method_GET)) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
Expand All @@ -2088,10 +2066,6 @@ HTTPAPIServer::HandleSystemSharedMemory(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

TRITONSERVER_Error* err = nullptr;
if (action == "status") {
triton::common::TritonJson::Value shm_status(
Expand Down Expand Up @@ -2186,6 +2160,7 @@ HTTPAPIServer::HandleCudaSharedMemory(
evhtp_request_t* req, const std::string& region_name,
const std::string& action)
{
AddContentTypeHeader(req, "application/json");
if ((action == "status") && (req->method != htp_method_GET)) {
RETURN_AND_RESPOND_WITH_ERR(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
Expand All @@ -2194,10 +2169,6 @@ HTTPAPIServer::HandleCudaSharedMemory(
req, EVHTP_RES_METHNALLOWED, "Method Not Allowed");
}

evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));

TRITONSERVER_Error* err = nullptr;
if (action == "status") {
triton::common::TritonJson::Value shm_status(
Expand Down Expand Up @@ -3097,9 +3068,7 @@ HTTPAPIServer::HandleInfer(
if (error != nullptr) {
LOG_VERBOSE(1) << "[request id: " << request_id << "] "
<< "Infer failed: " << TRITONSERVER_ErrorMessage(error);
evhtp_headers_add_header(
req->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));
AddContentTypeHeader(req, "application/json");
EVBufferAddErrorJson(req->buffer_out, error);
evhtp_send_reply(req, EVHTP_RES_BADREQ);
if (connection_paused) {
Expand Down Expand Up @@ -3559,17 +3528,13 @@ HTTPAPIServer::InferRequestClass::SetResponseHeader(
bool has_binary_data, size_t header_length)
{
if (has_binary_data) {
evhtp_headers_add_header(
req_->headers_out,
evhtp_header_new(kContentTypeHeader, "application/octet-stream", 1, 1));
AddContentTypeHeader(req_, "application/octet-stream");
evhtp_headers_add_header(
req_->headers_out, evhtp_header_new(
kInferHeaderContentLengthHTTPHeader,
std::to_string(header_length).c_str(), 1, 1));
} else {
evhtp_headers_add_header(
req_->headers_out,
evhtp_header_new(kContentTypeHeader, "application/json", 1, 1));
AddContentTypeHeader(req_, "application/json");
}

switch (response_compression_type_) {
Expand Down

0 comments on commit c8633c9

Please sign in to comment.