Skip to content
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

Match forward headers case insensitively. #6889

Merged
merged 16 commits into from
Feb 27, 2024
Merged
8 changes: 7 additions & 1 deletion docs/protocol/extension_parameters.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
# Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright (c) 2023-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
yinggeh marked this conversation as resolved.
Show resolved Hide resolved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -98,6 +98,12 @@ value. For example to forward all the headers that start with 'PREFIX_' from
both HTTP and GRPC, you should add `--http-header-forward-pattern PREFIX_.*
--grpc-header-forward-pattern PREFIX_.*` to your `tritonserver` command.

By default, the regular expression pattern matches headers with case-insensitive
mode according to the HTTP protocol. If you want to enforce case-sensitive mode,
simplying adding the `(?-i)` prefix which turns off case-insensitive mode, e.g.
`--http-header-forward-pattern (?-i)PREFIX_.*`. Note `(?-i)` prefix is not supported
on HTTP Python client.
rmccorm4 marked this conversation as resolved.
Show resolved Hide resolved

The forwarded headers can be accessed using the
[Python](https://github.com/triton-inference-server/python_backend#inference-request-parameters)
or C Backend APIs as inference request parameters.
Expand Down
24 changes: 17 additions & 7 deletions qa/L0_parameters/parameters_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3

# Copyright 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# Copyright 2023-2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
yinggeh marked this conversation as resolved.
Show resolved Hide resolved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -60,7 +60,8 @@ async def asyncSetUp(self):
self.parameter_list.append({"key1": True, "key2": "value2"})
self.parameter_list.append({"triton_": True, "key2": "value2"})

if TEST_HEADER == "1":
# Only "test_params" tests parameters without headers.
if TEST_HEADER != "test_params":
self.headers = {
"header_1": "value_1",
"header_2": "value_2",
Expand All @@ -70,11 +71,20 @@ async def asyncSetUp(self):
}

# only these headers should be forwarded to the model.
self.expected_headers = {
"my_header_1": "my_value_1",
"my_header_2": "my_value_2",
"my_header_3": 'This is a "quoted" string with a backslash\ ',
}
if TEST_HEADER == "test_header_forward_pattern_case_insensitive":
self.expected_headers = {
"my_header_1": "my_value_1",
"my_header_2": "my_value_2",
"my_header_3": 'This is a "quoted" string with a backslash\ ',
}
elif TEST_HEADER == "test_header_forward_pattern_case_sensitive":
self.expected_headers = {}
else:
self.expected_headers = {
"my_header_1": "my_value_1",
"my_header_2": "my_value_2",
"my_header_3": 'This is a "quoted" string with a backslash\ ',
}
else:
self.headers = {}
self.expected_headers = {}
Expand Down
15 changes: 12 additions & 3 deletions qa/L0_parameters/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2023-2024, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -53,13 +53,22 @@ mkdir -p "${MODELDIR}/ensemble/1"
# TODO: Add support and testing for C++ client parameters:
# https://jirasw.nvidia.com/browse/DLIS-4673

all_tests=("test_params"
"test_parameters_and_headers"
"test_header_forward_pattern_case_insensitive"
"test_header_forward_pattern_case_sensitive")

RET=0
for i in {0..1}; do
for i in "${all_tests[@]}"; do

# TEST_HEADER is a parameter used by `parameters_test.py` that controls
# whether the script will test for inclusion of headers in parameters or not.
if [ $i == 1 ]; then
if [ $i == "test_parameters_and_headers" ]; then
SERVER_ARGS="--model-repository=${MODELDIR} --exit-timeout-secs=120 --grpc-header-forward-pattern my_header.* --http-header-forward-pattern my_header.*"
elif [ $i == "test_header_forward_pattern_case_insensitive" ]; then
SERVER_ARGS="--model-repository=${MODELDIR} --exit-timeout-secs=120 --grpc-header-forward-pattern MY_HEADER.* --http-header-forward-pattern MY_HEADER.*"
elif [ $i == "test_grpc_header_forward_pattern_case_sensitive" ]; then
SERVER_ARGS="--model-repository=${MODELDIR} --exit-timeout-secs=120 --grpc-header-forward-pattern (?-i)MY_HEADER.*
yinggeh marked this conversation as resolved.
Show resolved Hide resolved
else
SERVER_ARGS="--model-repository=${MODELDIR} --exit-timeout-secs=120"
fi
Expand Down
11 changes: 9 additions & 2 deletions src/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,11 @@ TritonParser::Parse(int argc, char** argv)
triton::server::grpc::Options& lgrpc_options = lparams.grpc_options_;
#endif // TRITON_ENABLE_GRPC

#if defined TRITON_ENABLE_HTTP || defined TRITON_ENABLE_GRPC
// According to HTTP specification header names are case-insensitive.
const std::string case_insensitive_prefix{"(?i)"};
yinggeh marked this conversation as resolved.
Show resolved Hide resolved
#endif // TRITON_ENABLE_HTTP || TRITON_ENABLE_GRPC

#ifdef TRITON_ENABLE_VERTEX_AI
// Set different default value if specific flag is set
{
Expand Down Expand Up @@ -1345,7 +1350,8 @@ TritonParser::Parse(int argc, char** argv)
lparams.http_address_ = optarg;
break;
case OPTION_HTTP_HEADER_FORWARD_PATTERN:
lparams.http_forward_header_pattern_ = optarg;
lparams.http_forward_header_pattern_ =
std::move(case_insensitive_prefix + optarg);
yinggeh marked this conversation as resolved.
Show resolved Hide resolved
break;
break;
case OPTION_HTTP_THREAD_COUNT:
Expand Down Expand Up @@ -1484,7 +1490,8 @@ TritonParser::Parse(int argc, char** argv)
break;
}
case OPTION_GRPC_HEADER_FORWARD_PATTERN:
lgrpc_options.forward_header_pattern_ = optarg;
lgrpc_options.forward_header_pattern_ =
std::move(case_insensitive_prefix + optarg);
break;
#endif // TRITON_ENABLE_GRPC

Expand Down
Loading