-
Notifications
You must be signed in to change notification settings - Fork 854
Add Content-Type directive to body factory .body_factory_info #12947
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
base: master
Are you sure you want to change the base?
Changes from all commits
f4ff90d
1b51b53
0bc6d99
92275ad
0522902
2f9838e
0c9b447
61fb5b1
ca58fc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8359,6 +8359,8 @@ HttpTransact::build_error_response(State *s, HTTPStatus status_code, const char | |
| if (len > 0) { | ||
| s->hdr_info.client_response.value_set(static_cast<std::string_view>(MIME_FIELD_CONTENT_TYPE), body_type); | ||
| s->hdr_info.client_response.value_set(static_cast<std::string_view>(MIME_FIELD_CONTENT_LANGUAGE), body_language); | ||
| ats_free(s->internal_msg_buffer_type); | ||
| s->internal_msg_buffer_type = ats_strdup(body_type); | ||
|
Comment on lines
8359
to
+8363
|
||
| } else { | ||
| s->hdr_info.client_response.field_delete(static_cast<std::string_view>(MIME_FIELD_CONTENT_TYPE)); | ||
| s->hdr_info.client_response.field_delete(static_cast<std::string_view>(MIME_FIELD_CONTENT_LANGUAGE)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| ''' | ||
| Tests that the Content-Type directive in .body_factory_info is honored | ||
| for body factory error responses. | ||
| ''' | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
|
|
||
| import os | ||
|
|
||
| Test.Summary = 'Verify Content-Type directive in .body_factory_info controls error response MIME type' | ||
| Test.ContinueOnFail = True | ||
|
|
||
|
|
||
| class BodyFactoryContentTypeTest: | ||
| """ | ||
| Test that the Content-Type directive in .body_factory_info is used for | ||
| body factory error responses instead of the hardcoded text/html default. | ||
|
|
||
| Two scenarios: | ||
| 1. Default: no Content-Type directive -> text/html; charset=utf-8 | ||
| 2. Custom: Content-Type: text/plain -> text/plain | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| self._setupDefaultTS() | ||
| self._setupCustomTS() | ||
|
|
||
| def _setupDefaultTS(self): | ||
| """ATS instance with default body factory (no Content-Type directive).""" | ||
| self._ts_default = Test.MakeATSProcess("ts_default") | ||
| self._ts_default.Disk.records_config.update( | ||
| { | ||
| 'proxy.config.body_factory.enable_customizations': 1, | ||
| 'proxy.config.url_remap.remap_required': 1, | ||
| }) | ||
| self._ts_default.Disk.remap_config.AddLine('map http://mapped.example.com http://127.0.0.1:65535') | ||
|
|
||
| body_factory_dir = self._ts_default.Variables.BODY_FACTORY_TEMPLATE_DIR | ||
| info_path = os.path.join(body_factory_dir, 'default', '.body_factory_info') | ||
| self._ts_default.Disk.File(info_path).WriteOn("Content-Language: en\nContent-Charset: utf-8\n") | ||
|
|
||
| def _setupCustomTS(self): | ||
| """ATS instance with Content-Type: text/plain in .body_factory_info.""" | ||
| self._ts_custom = Test.MakeATSProcess("ts_custom") | ||
| self._ts_custom.Disk.records_config.update( | ||
| { | ||
| 'proxy.config.body_factory.enable_customizations': 1, | ||
| 'proxy.config.url_remap.remap_required': 1, | ||
| }) | ||
| self._ts_custom.Disk.remap_config.AddLine('map http://mapped.example.com http://127.0.0.1:65535') | ||
|
|
||
| body_factory_dir = self._ts_custom.Variables.BODY_FACTORY_TEMPLATE_DIR | ||
| info_path = os.path.join(body_factory_dir, 'default', '.body_factory_info') | ||
| self._ts_custom.Disk.File(info_path).WriteOn("Content-Type: text/plain\n") | ||
|
|
||
| def run(self): | ||
| self._testDefaultContentType() | ||
| self._testCustomContentType() | ||
|
|
||
| def _testDefaultContentType(self): | ||
| """Without Content-Type directive, error responses should use text/html.""" | ||
| tr = Test.AddTestRun('Default body factory Content-Type is text/html') | ||
| tr.Processes.Default.StartBefore(self._ts_default) | ||
| tr.Processes.Default.Command = ( | ||
| f'curl -s -D- -o /dev/null' | ||
| f' -H "Host: unmapped.example.com"' | ||
| f' http://127.0.0.1:{self._ts_default.Variables.port}/') | ||
| tr.Processes.Default.ReturnCode = 0 | ||
| tr.Processes.Default.TimeOut = 5 | ||
| tr.Processes.Default.Streams.stdout += Testers.ContainsExpression( | ||
| '(?i)Content-Type:\\s*text/html\\s*;\\s*charset=utf-8(?:\\s|\\r|$)', | ||
| 'Default body factory should produce text/html with charset') | ||
| tr.Processes.Default.Streams.stdout += Testers.ContainsExpression('HTTP/1.1 404', 'Unmapped request should get 404') | ||
| tr.StillRunningAfter = self._ts_default | ||
|
|
||
| def _testCustomContentType(self): | ||
| """With Content-Type: text/plain, error responses should use text/plain.""" | ||
| tr = Test.AddTestRun('Custom body factory Content-Type is text/plain') | ||
| tr.Processes.Default.StartBefore(self._ts_custom) | ||
| tr.Processes.Default.Command = ( | ||
| f'curl -s -D- -o /dev/null' | ||
| f' -H "Host: unmapped.example.com"' | ||
| f' http://127.0.0.1:{self._ts_custom.Variables.port}/') | ||
| tr.Processes.Default.ReturnCode = 0 | ||
| tr.Processes.Default.TimeOut = 5 | ||
| tr.Processes.Default.Streams.stdout += Testers.ContainsExpression( | ||
| '(?i)Content-Type:\\s*text/plain(?:\\s|\\r|$)', 'Custom body factory should produce text/plain') | ||
| tr.Processes.Default.Streams.stdout += Testers.ContainsExpression('HTTP/1.1 404', 'Unmapped request should get 404') | ||
| tr.StillRunningAfter = self._ts_custom | ||
|
|
||
|
|
||
| BodyFactoryContentTypeTest().run() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Content-Charsetdirective is documented as always being appended to theContent-Typeheader as acharsetparameter, but the current implementation only appends; charset=...when aContent-Typedirective is present and does not already includecharset=. Consider updating this section to reflect the actual behavior (including what happens whenContent-Typeis omitted or already has a charset parameter).