-
-
Notifications
You must be signed in to change notification settings - Fork 4
Implement an HTTPResponse helper class
#604
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| #ifndef SOURCEMETA_ONE_SERVER_ACTION_JSONSCHEMA_SERVE_H | ||
| #define SOURCEMETA_ONE_SERVER_ACTION_JSONSCHEMA_SERVE_H | ||
|
|
||
| #include <sourcemeta/one/shared.h> | ||
|
|
||
| #include "action_serve_metapack_file.h" | ||
| #include "helpers.h" | ||
| #include "request.h" | ||
| #include "response.h" | ||
|
|
||
| #include <algorithm> // std::transform | ||
| #include <cctype> // std::tolower | ||
| #include <filesystem> // std::filesystem | ||
| #include <string> // std::string | ||
| #include <string_view> // std::string_view | ||
|
|
||
| static auto action_jsonschema_serve(const std::filesystem::path &base, | ||
| const std::string_view &path, | ||
| sourcemeta::one::HTTPRequest &request, | ||
| sourcemeta::one::HTTPResponse &response) | ||
| -> void { | ||
| // Otherwise we may get unexpected results in case-sensitive file-systems | ||
| std::string lowercase_path{path}; | ||
| std::transform( | ||
| lowercase_path.begin(), lowercase_path.end(), lowercase_path.begin(), | ||
| [](const unsigned char character) { return std::tolower(character); }); | ||
|
|
||
| // Because Visual Studio Code famously does not support `$id` or `id` | ||
| // See | ||
| // https://github.com/microsoft/vscode-json-languageservice/issues/224 | ||
| const auto &user_agent{request.header("user-agent")}; | ||
| const auto is_vscode{user_agent.starts_with("Visual Studio Code") || | ||
| user_agent.starts_with("VSCodium")}; | ||
| const auto is_deno{user_agent.starts_with("Deno/")}; | ||
| const auto bundle{!request.query("bundle").empty()}; | ||
| auto absolute_path{base / "schemas" / lowercase_path / SENTINEL}; | ||
| if (is_vscode) { | ||
| absolute_path /= "editor.metapack"; | ||
| } else if (bundle || is_deno) { | ||
| absolute_path /= "bundle.metapack"; | ||
| } else { | ||
| absolute_path /= "schema.metapack"; | ||
| } | ||
|
|
||
| if (request.method() != "get" && request.method() != "head" && | ||
| !std::filesystem::exists(absolute_path)) { | ||
| json_error(request, response, sourcemeta::one::STATUS_NOT_FOUND, | ||
| "not-found", "There is nothing at this URL"); | ||
| return; | ||
| } | ||
|
|
||
| if (is_deno) { | ||
| action_serve_metapack_file(request, response, absolute_path, | ||
| sourcemeta::one::STATUS_OK, true, | ||
| // For HTTP imports, as Deno won't like the | ||
| // `application/schema+json` one | ||
| "application/json"); | ||
| } else { | ||
| action_serve_metapack_file(request, response, absolute_path, | ||
| sourcemeta::one::STATUS_OK, true); | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
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,119 @@ | ||
| #ifndef SOURCEMETA_ONE_SERVER_ACTION_SERVE_METAPACK_FILE_H | ||
| #define SOURCEMETA_ONE_SERVER_ACTION_SERVE_METAPACK_FILE_H | ||
|
|
||
| #include <sourcemeta/core/time.h> | ||
|
|
||
| #include <sourcemeta/one/shared.h> | ||
|
|
||
| #include "helpers.h" | ||
| #include "request.h" | ||
| #include "response.h" | ||
|
|
||
| #include <chrono> // std::chrono::seconds | ||
| #include <filesystem> // std::filesystem | ||
| #include <optional> // std::optional | ||
| #include <sstream> // std::ostringstream | ||
| #include <string> // std::string | ||
|
|
||
| static auto action_serve_metapack_file( | ||
| const sourcemeta::one::HTTPRequest &request, | ||
| sourcemeta::one::HTTPResponse &response, | ||
| const std::filesystem::path &absolute_path, const char *const code, | ||
| const bool enable_cors = false, | ||
| const std::optional<std::string> &mime = std::nullopt, | ||
| const std::optional<std::string> &link = std::nullopt) -> void { | ||
| if (request.method() != "get" && request.method() != "head") { | ||
| json_error(request, response, sourcemeta::one::STATUS_METHOD_NOT_ALLOWED, | ||
| "method-not-allowed", | ||
| "This HTTP method is invalid for this URL"); | ||
| return; | ||
| } | ||
|
|
||
| auto file{sourcemeta::one::read_stream_raw(absolute_path)}; | ||
| if (!file.has_value()) { | ||
| json_error(request, response, sourcemeta::one::STATUS_NOT_FOUND, | ||
| "not-found", "There is nothing at this URL"); | ||
| return; | ||
| } | ||
|
|
||
| // Note that `If-Modified-Since` can only be used with a `GET` or `HEAD`. | ||
| // See | ||
| // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Modified-Since | ||
| const auto if_modified_since{request.header_gmt("if-modified-since")}; | ||
| // Time comparison can be flaky, but adding a bit of tolerance leads | ||
| // to more consistent behavior. | ||
| if (if_modified_since.has_value() && | ||
| (if_modified_since.value() + std::chrono::seconds(1)) >= | ||
| file.value().last_modified) { | ||
| response.write_status(sourcemeta::one::STATUS_NOT_MODIFIED); | ||
| if (enable_cors) { | ||
| response.write_header("Access-Control-Allow-Origin", "*"); | ||
| } | ||
|
|
||
| send_response(sourcemeta::one::STATUS_NOT_MODIFIED, request, response); | ||
| return; | ||
| } | ||
|
|
||
| const auto &checksum{file.value().checksum}; | ||
| std::ostringstream etag_value_strong; | ||
| std::ostringstream etag_value_weak; | ||
| etag_value_strong << '"' << checksum << '"'; | ||
| etag_value_weak << 'W' << '/' << '"' << checksum << '"'; | ||
| for (const auto &match : request.header_list("if-none-match")) { | ||
| // Cache hit | ||
| if (match.first == "*" || match.first == etag_value_weak.str() || | ||
| match.first == etag_value_strong.str()) { | ||
| response.write_status(sourcemeta::one::STATUS_NOT_MODIFIED); | ||
| if (enable_cors) { | ||
| response.write_header("Access-Control-Allow-Origin", "*"); | ||
| } | ||
|
|
||
| send_response(sourcemeta::one::STATUS_NOT_MODIFIED, request, response); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| response.write_status(code); | ||
|
|
||
| // To support requests from web browsers | ||
| if (enable_cors) { | ||
| response.write_header("Access-Control-Allow-Origin", "*"); | ||
| } | ||
|
|
||
| if (mime.has_value()) { | ||
| response.write_header("Content-Type", mime.value()); | ||
| } else { | ||
| response.write_header("Content-Type", file.value().mime); | ||
| } | ||
|
|
||
| response.write_header("Last-Modified", | ||
| sourcemeta::core::to_gmt(file.value().last_modified)); | ||
|
|
||
| std::ostringstream etag; | ||
| etag << '"' << checksum << '"'; | ||
| response.write_header("ETag", std::move(etag).str()); | ||
|
|
||
| // See | ||
| // https://json-schema.org/draft/2020-12/json-schema-core.html#section-9.5.1.1 | ||
| if (link.has_value()) { | ||
| write_link_header(response, link.value()); | ||
| } else { | ||
| const auto &dialect{file.value().extension}; | ||
| if (dialect.is_string()) { | ||
| write_link_header(response, dialect.to_string()); | ||
| } | ||
| } | ||
|
|
||
| std::ostringstream contents; | ||
| contents << file.value().data.rdbuf(); | ||
|
|
||
| if (file.value().encoding == sourcemeta::one::Encoding::GZIP) { | ||
| send_response(code, request, response, contents.str(), | ||
| sourcemeta::one::Encoding::GZIP); | ||
| } else { | ||
| send_response(code, request, response, contents.str(), | ||
| sourcemeta::one::Encoding::Identity); | ||
| } | ||
| } | ||
|
|
||
| #endif |
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.