Skip to content

Fix extra content-type headers in HTTP server #6678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 13, 2023
11 changes: 10 additions & 1 deletion qa/L0_http/generate_endpoint_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def generate_expect_failure(self, model_name, inputs, msg):
r = requests.post(
url, data=inputs if isinstance(inputs, str) else json.dumps(inputs)
)
# Content-Type header should always be JSON for errors
self.assertEqual(r.headers["Content-Type"], "'application/json'")

try:
r.raise_for_status()
self.assertTrue(False, f"Expected failure, success for {inputs}")
Expand All @@ -79,6 +82,9 @@ def generate_expect_failure(self, model_name, inputs, msg):

def generate_stream_expect_failure(self, model_name, inputs, msg):
r = self.generate_stream(model_name, inputs)
# Content-Type header should always be JSON for errors
self.assertEqual(r.headers["Content-Type"], "'application/json'")

try:
r.raise_for_status()
self.assertTrue(False, f"Expected failure, success for {inputs}")
Expand All @@ -90,6 +96,9 @@ def generate_stream_expect_success(
):
r = self.generate_stream(model_name, inputs)
r.raise_for_status()
self.assertEqual(
r.headers["Content-Type"], "'text/event-stream; charset=utf-8'"
)
self.check_sse_responses(r, [{"TEXT": expected_output}] * rep_count)

def check_sse_responses(self, res, expected_res):
Expand Down Expand Up @@ -128,7 +137,7 @@ def test_generate(self):
r.raise_for_status()

self.assertIn("Content-Type", r.headers)
self.assertIn("application/json", r.headers["Content-Type"])
self.assertEqual(r.headers["Content-Type"], "'application/json'")

data = r.json()
self.assertIn("TEXT", data)
Expand Down
7 changes: 7 additions & 0 deletions src/http_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ EVBufferAddErrorJson(evbuffer* buffer, TRITONSERVER_Error* err)
void
AddContentTypeHeader(evhtp_request_t* req, const char* type)
{
// Remove existing header if found
auto content_header =
evhtp_headers_find_header(req->headers_out, kContentTypeHeader);
if (content_header) {
evhtp_header_rm_and_free(req->headers_out, content_header);
}

evhtp_headers_add_header(
req->headers_out, evhtp_header_new(kContentTypeHeader, type, 1, 1));
}
Expand Down