Copilot commented on code in PR #13390:
URL: https://github.com/apache/trafficserver/pull/13390#discussion_r3590442442
##########
plugins/webp_transform/ImageTransform.cc:
##########
@@ -125,13 +170,11 @@ class ImageTransform : public TransformationPlugin
produce(std::string_view(reinterpret_cast<const char
*>(output_blob.data()), output_blob.length()));
} catch (Magick::Warning &warning) {
TSError("ImageMagick++ warning: %s", warning.what());
- produce(std::string_view(reinterpret_cast<const char
*>(input_blob.data()), input_blob.length()));
- _transform_image_type = _input_image_type; // Revert to original
encoding on error
+ pass_through(std::string_view(reinterpret_cast<const char
*>(input_blob.data()), input_blob.length()));
} catch (Magick::Error &error) {
TSError("ImageMagick++ error: %s _image_type: %d input_data.length():
%zd", error.what(), (int)_transform_image_type,
input_data.length());
Review Comment:
`input_data.length()` is a `size_t`, but the format string uses `%zd`
(signed `ssize_t`). In a variadic call this is undefined behavior on platforms
where the types differ. Use `%zu` for `size_t` (or cast to the correct signed
type).
##########
plugins/webp_transform/ImageTransform.cc:
##########
@@ -58,40 +59,75 @@ bool config_convert_to_jpeg = false;
Stat stat_convert_to_webp;
Stat stat_convert_to_jpeg;
+
+bool
+has_signature_for(std::string_view data, ImageEncoding encoding)
+{
+ constexpr std::string_view png_signature{"\x89PNG\r\n\x1a\n", 8};
+
+ switch (encoding) {
+ case ImageEncoding::webp:
+ return data.size() >= 12 && data.substr(0, 4) == "RIFF" && data.substr(8,
4) == "WEBP";
+ case ImageEncoding::jpeg:
+ return data.size() >= 3 && static_cast<unsigned char>(data[0]) == 0xff &&
static_cast<unsigned char>(data[1]) == 0xd8 &&
+ static_cast<unsigned char>(data[2]) == 0xff;
+ case ImageEncoding::png:
+ return data.starts_with(png_signature);
+ case ImageEncoding::unknown:
+ return false;
+ }
+
+ return false;
+}
} // namespace
class ImageTransform : public TransformationPlugin
{
public:
- ImageTransform(Transaction &transaction, ImageEncoding input_image_type,
ImageEncoding transform_image_type)
+ ImageTransform(Transaction &transaction, std::string input_content_type,
ImageEncoding input_image_type,
+ ImageEncoding transform_image_type)
: TransformationPlugin(transaction,
TransformationPlugin::RESPONSE_TRANSFORMATION),
+ _input_content_type(std::move(input_content_type)),
_input_image_type(input_image_type),
_transform_image_type(transform_image_type)
{
TransformationPlugin::registerHook(HOOK_READ_RESPONSE_HEADERS);
+ TransformationPlugin::registerHook(HOOK_SEND_RESPONSE_HEADERS);
}
void
handleReadResponseHeaders(Transaction &transaction) override
{
+ transaction.getServerResponse().getHeaders()["Vary"] = "Accept"; // to
have a separate cache entry
+
+ Dbg(webp_dbg_ctl, "url %s",
transaction.getServerRequest().getUrl().getUrlString().c_str());
+ transaction.resume();
+ }
+
+ void
+ handleSendResponseHeaders(Transaction &transaction) override
+ {
+ if (_transform_image_type == _input_image_type) {
+ transaction.getClientResponse().getHeaders()["Content-Type"] =
_input_content_type;
+ transaction.resume();
Review Comment:
`Vary: Accept` is currently set on the *server* response headers, but this
hook later edits the *client* response headers (via `getClientResponse()`), and
`server_response` / `client_response` are distinct header handles in the C++
API. To ensure the response actually sent to the client carries `Vary: Accept`
(and downstream caches/clients honor content negotiation), set it on the client
response headers in `handleSendResponseHeaders()` too (or instead).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]