-
Notifications
You must be signed in to change notification settings - Fork 2
[#43] Add CORS Configuration #46
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
58f5b59
feat: add configurable CORS support for REST API
bburda b0367c8
fix: address CORS code review feedback
bburda 2607d00
docs: add CORS configuration section to README
bburda 994ac40
fix: address CORS code review feedback
bburda bc0c391
fix: return 403 for disallowed origins on CORS preflight
bburda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/ros2_medkit_gateway/include/ros2_medkit_gateway/config.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright 2025 bburda | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace ros2_medkit_gateway { | ||
|
|
||
| /** | ||
| * @brief CORS (Cross-Origin Resource Sharing) configuration settings | ||
| */ | ||
| struct CorsConfig { | ||
| bool enabled{false}; | ||
| std::vector<std::string> allowed_origins; | ||
| std::vector<std::string> allowed_methods; | ||
| std::vector<std::string> allowed_headers; | ||
| bool allow_credentials{false}; | ||
| int max_age_seconds{86400}; | ||
|
|
||
| // Pre-built header values for performance | ||
| std::string methods_header; | ||
| std::string headers_header; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Builder for CorsConfig with fluent interface | ||
| * | ||
| * Usage: | ||
| * auto config = CorsConfigBuilder() | ||
| * .with_origins({"http://localhost:5173"}) | ||
| * .with_methods({"GET", "PUT", "OPTIONS"}) | ||
| * .with_headers({"Content-Type", "Accept"}) | ||
| * .with_credentials(true) | ||
| * .with_max_age(3600) | ||
| * .build(); | ||
| */ | ||
| class CorsConfigBuilder { | ||
| public: | ||
| CorsConfigBuilder & with_origins(std::vector<std::string> origins); | ||
| CorsConfigBuilder & with_methods(std::vector<std::string> methods); | ||
| CorsConfigBuilder & with_headers(std::vector<std::string> headers); | ||
| CorsConfigBuilder & with_credentials(bool credentials); | ||
| CorsConfigBuilder & with_max_age(int seconds); | ||
| CorsConfig build(); | ||
|
|
||
| private: | ||
| CorsConfig config_; | ||
| }; | ||
|
|
||
| } // namespace ros2_medkit_gateway |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2025 bburda | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "ros2_medkit_gateway/config.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <stdexcept> | ||
| #include <utility> | ||
|
|
||
| namespace ros2_medkit_gateway { | ||
|
|
||
| CorsConfigBuilder & CorsConfigBuilder::with_origins(std::vector<std::string> origins) { | ||
| config_.allowed_origins = std::move(origins); | ||
| return *this; | ||
| } | ||
|
|
||
| CorsConfigBuilder & CorsConfigBuilder::with_methods(std::vector<std::string> methods) { | ||
| config_.allowed_methods = std::move(methods); | ||
| return *this; | ||
| } | ||
|
|
||
| CorsConfigBuilder & CorsConfigBuilder::with_headers(std::vector<std::string> headers) { | ||
| config_.allowed_headers = std::move(headers); | ||
| return *this; | ||
| } | ||
|
|
||
| CorsConfigBuilder & CorsConfigBuilder::with_credentials(bool credentials) { | ||
| config_.allow_credentials = credentials; | ||
| return *this; | ||
| } | ||
|
|
||
| CorsConfigBuilder & CorsConfigBuilder::with_max_age(int seconds) { | ||
| config_.max_age_seconds = seconds; | ||
| return *this; | ||
| } | ||
|
|
||
| CorsConfig CorsConfigBuilder::build() { | ||
| // Enable CORS only if origins are configured | ||
| config_.enabled = !config_.allowed_origins.empty(); | ||
|
|
||
| if (config_.enabled) { | ||
| // Validate: credentials cannot be used with wildcard origin | ||
| if (config_.allow_credentials) { | ||
| auto has_wildcard = std::find(config_.allowed_origins.begin(), config_.allowed_origins.end(), "*"); | ||
| if (has_wildcard != config_.allowed_origins.end()) { | ||
| throw std::invalid_argument("CORS: allow_credentials cannot be true when allowed_origins contains '*'"); | ||
| } | ||
| } | ||
|
|
||
| // Build cached header strings | ||
| for (const auto & method : config_.allowed_methods) { | ||
| if (!config_.methods_header.empty()) { | ||
| config_.methods_header += ", "; | ||
| } | ||
| config_.methods_header += method; | ||
| } | ||
|
|
||
| for (const auto & header : config_.allowed_headers) { | ||
| if (!config_.headers_header.empty()) { | ||
| config_.headers_header += ", "; | ||
| } | ||
| config_.headers_header += header; | ||
| } | ||
| } | ||
|
|
||
| return std::move(config_); | ||
| } | ||
|
|
||
| } // namespace ros2_medkit_gateway | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.