Skip to content

style: automate and enforce header ordering guidelines.#793

Merged
mattklein123 merged 3 commits intoenvoyproxy:masterfrom
htuch:header-cleanup
Apr 20, 2017
Merged

style: automate and enforce header ordering guidelines.#793
mattklein123 merged 3 commits intoenvoyproxy:masterfrom
htuch:header-cleanup

Conversation

@htuch
Copy link
Member

@htuch htuch commented Apr 19, 2017

Script to automate header ordering check/fix (tools/header_order.py) and resulting reorderings. See
the comment at the top of header_order.py for why the header order looks the way it does.

Also made check_format.py standalone and introduced a check_format CI target in addition to
fix_format in anticipation of removing the old CI targets.

There is considerable overlap with what this does and clang-format's IncludeCategories (see
https://clang.llvm.org/docs/ClangFormatStyleOptions.html). But, clang-format doesn't seem smart
enough to handle block splitting and correctly detecting the main header subject to the Envoy
canonical paths.

htuch added 2 commits April 19, 2017 17:45
Script to automate header ordering check/fix (tools/header_order.py) and resulting reorderings. See
the comment at the top of header_order.py for why the header order looks the way it does.

Also made check_format.py standalone and introduced a check_format CI target in addition to
fix_format in anticipation of removing the old CI targets.

There is considerable overlap with what this does and clang-format's IncludeCategories (see
https://clang.llvm.org/docs/ClangFormatStyleOptions.html). But, clang-format doesn't seem smart
enough to handle block splitting and correctly detecting the main header subject to the Envoy
canonical paths.
Copy link
Member

@mattklein123 mattklein123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice, small nits.

with open(path, 'w') as f:
f.write(reorderd_source)
sys.exit(0)
print 'Usage: %s [--rewrite] <.h path>' % sys.argv[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: .h path is kind of confusing. It's just a source file path right?


after_includes_lines += list(all_lines)

def file_header_filter():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it took me 60s to figure out that this is figuring out the associated files header, add comment?

@mattklein123 mattklein123 merged commit 33a765b into envoyproxy:master Apr 20, 2017
htuch added a commit to htuch/envoy that referenced this pull request Apr 20, 2017
Since Bazel doesn't support precompiled headers (bazelbuild/bazel#1215),
we actually suffer a slowdown rather than speedup by having all files implicitly include
precompiled.h in the Bazel build. I found ~10% performance improvement on my workstation, going from
181s to 163s for a "bazel build //test/..." from a "bazel clean" state.

This patch makes all includes now explicit. This will slowdown the cmake build, but since this is
not long for this world it seems alright. The header reordering in this patch was achieved using envoyproxy#793.
The addition of the missing headers in each file was done using a modified version of the tool in envoyproxy#793,
which substituted in headers based on simple substring matches (e.g. whenever it saw std::string in
a file, it added #include <string>). Here's the patterns I ended up using that seemed to work:

  sys_extra_includes = [
      ('std::runtime_error', 'stdexcept'),
      ('strcmp', 'string.h'),
      ('strcasecmp', 'strings.h'),
      ('std::unique_ptr', 'memory'),
      ('std::shared_ptr', 'memory'),
      ('std::unordered_set', 'unordered_set'),
      ('std::transform', 'algorithm'),
      ('std::vector', 'vector'),
      ('std::regex', 'regex'),
      ('spdlog::', 'spdlog/spdlog.h'),
      ('fmt::format', 'spdlog/spdlog.h'),
      ('std::string', 'string'),
      ('uint32_t', 'cstdint'),
      ('uint64_t', 'cstdint'),
      ('std::array', 'array'),
      ('sockaddr_un', 'sys/un.h'),
      ('sockaddr_in', 'netinet/ip.h'),
      ('std::chrono', 'chrono'),
      ('std::list', 'list'),
      ('std::multimap', 'map'),
      ('std::unordered_map', 'unordered_map'),
      ('std::stringstream', 'sstream'),
      ('std::condition_variable', 'condition_variable'),
      ('std::mutex', 'mutex'),
      (' ::close', 'unistd.h'),
      ('inet_ntop', 'arpa/inet.h'),
      ('inet_pton', 'arpa/inet.h'),
      ('htonl', 'arpa/inet.h'),
      ('setsockopt', 'sys/types.h'),
      ('setsockopt', 'sys/socket.h'),
      ('TCP_NODELAY', 'netinet/tcp.h'),
      ('SIGTERM', 'signal.h'),
      ('SIGPIPE', 'signal.h'),
      ('std::function', 'functional'),
      ('std::ifstream', 'fstream'),
      ('std::ofstream', 'fstream'),
      ('std::forward_list', 'forward_list'),
      ('std::atomic', 'atomic'),
      ('std::cerr', 'iostream'),
      ('hostent', 'netdb.h'),
      (' bind(', 'sys/types.h'),
      (' bind(', 'sys/socket.h'),
      ('std::ostream', 'iostream'),
  ]

  extra_includes = [
      ('MOCK_METHOD', 'gmock/gmock.h'),
      ('MOCK_CONST_METHOD', 'gmock/gmock.h'),
      ('MATCHER_P', 'gmock/gmock.h'),
      ('ON_CALL', 'gmock/gmock.h'),
      ('using testing::_', 'gmock/gmock.h'),
      ('testing::Test', 'gtest/gtest.h'),
      ('EXPECT_', 'gtest/gtest.h'),
      ('\nTEST(', 'gtest/gtest.h'),
      ('\nTEST_F(', 'gtest/gtest.h'),
      ('\nTEST_P(', 'gtest/gtest.h'),
      ('HeaderMapImpl', 'test/test_common/printers.h'),
      ('Buffer::', 'test/test_common/printers.h'),
      ('RespValue', 'test/test_common/printers.h'),
  ]
htuch added a commit to htuch/envoy that referenced this pull request Apr 20, 2017
Since Bazel doesn't support precompiled headers (bazelbuild/bazel#1215),
we actually suffer a slowdown rather than speedup by having all files implicitly include
precompiled.h in the Bazel build. I found ~10% performance improvement on my workstation, going from
181s to 163s for a "bazel build //test/..." from a "bazel clean" state.

This patch makes all includes now explicit. This will slowdown the cmake build, but since this is
not long for this world it seems alright. The header reordering in this patch was achieved using envoyproxy#793.
The addition of the missing headers in each file was done using a modified version of the tool in envoyproxy#793,
which substituted in headers based on simple substring matches (e.g. whenever it saw std::string in
a file, it added #include <string>). Here's the patterns I ended up using that seemed to work:

  sys_extra_includes = [
      ('std::runtime_error', 'stdexcept'),
      ('strcmp', 'string.h'),
      ('strcasecmp', 'strings.h'),
      ('std::unique_ptr', 'memory'),
      ('std::shared_ptr', 'memory'),
      ('std::unordered_set', 'unordered_set'),
      ('std::transform', 'algorithm'),
      ('std::vector', 'vector'),
      ('std::regex', 'regex'),
      ('spdlog::', 'spdlog/spdlog.h'),
      ('fmt::format', 'spdlog/spdlog.h'),
      ('std::string', 'string'),
      ('uint32_t', 'cstdint'),
      ('uint64_t', 'cstdint'),
      ('std::array', 'array'),
      ('sockaddr_un', 'sys/un.h'),
      ('sockaddr_in', 'netinet/ip.h'),
      ('std::chrono', 'chrono'),
      ('std::list', 'list'),
      ('std::multimap', 'map'),
      ('std::unordered_map', 'unordered_map'),
      ('std::stringstream', 'sstream'),
      ('std::condition_variable', 'condition_variable'),
      ('std::mutex', 'mutex'),
      (' ::close', 'unistd.h'),
      ('inet_ntop', 'arpa/inet.h'),
      ('inet_pton', 'arpa/inet.h'),
      ('htonl', 'arpa/inet.h'),
      ('setsockopt', 'sys/types.h'),
      ('setsockopt', 'sys/socket.h'),
      ('TCP_NODELAY', 'netinet/tcp.h'),
      ('SIGTERM', 'signal.h'),
      ('SIGPIPE', 'signal.h'),
      ('std::function', 'functional'),
      ('std::ifstream', 'fstream'),
      ('std::ofstream', 'fstream'),
      ('std::forward_list', 'forward_list'),
      ('std::atomic', 'atomic'),
      ('std::cerr', 'iostream'),
      ('hostent', 'netdb.h'),
      (' bind(', 'sys/types.h'),
      (' bind(', 'sys/socket.h'),
      ('std::ostream', 'iostream'),
  ]

  extra_includes = [
      ('MOCK_METHOD', 'gmock/gmock.h'),
      ('MOCK_CONST_METHOD', 'gmock/gmock.h'),
      ('MATCHER_P', 'gmock/gmock.h'),
      ('ON_CALL', 'gmock/gmock.h'),
      ('using testing::_', 'gmock/gmock.h'),
      ('testing::Test', 'gtest/gtest.h'),
      ('EXPECT_', 'gtest/gtest.h'),
      ('\nTEST(', 'gtest/gtest.h'),
      ('\nTEST_F(', 'gtest/gtest.h'),
      ('\nTEST_P(', 'gtest/gtest.h'),
      ('HeaderMapImpl', 'test/test_common/printers.h'),
      ('Buffer::', 'test/test_common/printers.h'),
      ('RespValue', 'test/test_common/printers.h'),
  ]
mathetake added a commit that referenced this pull request Mar 3, 2026
**Description**

This commit removes the handwritten header matching code from the
extproc, and instead starts utilizing the hardened envoy native router.

Historically, we had only one giant extproc filter where we did all
logics including model name extraction, routing and then body
transformation & upstream authorization. Since #599, we split into two
external processor filters; one sits at the normal HTTP router and the
other is configured at the per-cluster upstream HTTP filter. In theory,
the one at HTTP router has only one job on request path: extracting
model name from the request body. However, due to the historical reason,
the handwritten router logic component remained, and that comes with not
only a maintenance cost (forcing a complex extproc & control plane
orchestration) but also a potential security vulnerability. In fact,
writing header matching logic can be an easy attack surface, so if it's
possible, we should avoid writing our own header matching (routing
logic) but should rely on the battle-tested hardened envoy native
router.

With this commit, now a regex matching is available as well as there's
no difference between HTTPRoute's matching and AIGatewayRoute's matching
implementation. This also opens up a possibility to support path
matching in our rule.

**Related Issues/PRs (if applicable)**

Ref #612 
Ref #73

---------

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
mathetake added a commit that referenced this pull request Mar 3, 2026
**Description**

Before #793, the case where no matching route found was handled in the
extproc and the 404 immediate response was returned from there, but
after that, it naturally results in the "unreachable" default route and
swallowed the indication of no matching and it made it impossible to
reason about the 500 error on that case. In other words, this fixes the
regression in #793 to return the proper 404 response.

---------

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants