-
Notifications
You must be signed in to change notification settings - Fork 852
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
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 |
|---|---|---|
|
|
@@ -74,9 +74,10 @@ HttpBodyFactory::fabricate_with_old_api(const char *type, HttpTransact::State *c | |
| size_t content_language_buf_size, char *content_type_out_buf, size_t content_type_buf_size, | ||
| int format_size, const char *format) | ||
| { | ||
| char *buffer = nullptr; | ||
| const char *lang_ptr = nullptr; | ||
| const char *charset_ptr = nullptr; | ||
| char *buffer = nullptr; | ||
| const char *lang_ptr = nullptr; | ||
| const char *charset_ptr = nullptr; | ||
| const char *content_type_ptr = nullptr; | ||
| char url[1024]; | ||
| const char *set = nullptr; | ||
| bool found_requested_template = false; | ||
|
|
@@ -145,8 +146,8 @@ HttpBodyFactory::fabricate_with_old_api(const char *type, HttpTransact::State *c | |
| // try to fabricate the desired type of error response // | ||
| ///////////////////////////////////////////////////////// | ||
| if (buffer == nullptr) { | ||
| buffer = | ||
| fabricate(&acpt_language_list, &acpt_charset_list, type, context, resulting_buffer_length, &lang_ptr, &charset_ptr, &set); | ||
| buffer = fabricate(&acpt_language_list, &acpt_charset_list, type, context, resulting_buffer_length, &lang_ptr, &charset_ptr, | ||
| &content_type_ptr, &set); | ||
| found_requested_template = (buffer != nullptr); | ||
| } | ||
| ///////////////////////////////////////////////////////////// | ||
|
|
@@ -159,7 +160,7 @@ HttpBodyFactory::fabricate_with_old_api(const char *type, HttpTransact::State *c | |
| return nullptr; | ||
| } | ||
| buffer = fabricate(&acpt_language_list, &acpt_charset_list, "default", context, resulting_buffer_length, &lang_ptr, | ||
| &charset_ptr, &set); | ||
| &charset_ptr, &content_type_ptr, &set); | ||
| } | ||
|
|
||
| /////////////////////////////////// | ||
|
|
@@ -181,7 +182,8 @@ HttpBodyFactory::fabricate_with_old_api(const char *type, HttpTransact::State *c | |
| if (buffer) { // got an instantiated template | ||
| if (!plain_flag) { | ||
| snprintf(content_language_out_buf, content_language_buf_size, "%s", lang_ptr); | ||
| snprintf(content_type_out_buf, content_type_buf_size, "text/html; charset=%s", charset_ptr); | ||
| const char *mime_type = content_type_ptr ? content_type_ptr : "text/html"; | ||
| snprintf(content_type_out_buf, content_type_buf_size, "%s; charset=%s", mime_type, charset_ptr); | ||
|
Comment on lines
+185
to
+186
|
||
| } | ||
|
|
||
| if (enable_logging) { | ||
|
|
@@ -213,8 +215,9 @@ HttpBodyFactory::dump_template_tables(FILE *fp) | |
| for (const auto &it1 : *table_of_sets.get()) { | ||
| HttpBodySet *body_set = static_cast<HttpBodySet *>(it1.second); | ||
| if (body_set) { | ||
| fprintf(fp, "set %s: name '%s', lang '%s', charset '%s'\n", it1.first.c_str(), body_set->set_name, | ||
| body_set->content_language, body_set->content_charset); | ||
| fprintf(fp, "set %s: name '%s', lang '%s', charset '%s', type '%s'\n", it1.first.c_str(), body_set->set_name, | ||
| body_set->content_language, body_set->content_charset, | ||
| body_set->content_type ? body_set->content_type : "text/html"); | ||
|
|
||
| /////////////////////////////////////////// | ||
| // loop over body-types->body hash table // | ||
|
|
@@ -374,7 +377,7 @@ HttpBodyFactory::~HttpBodyFactory() | |
| char * | ||
| HttpBodyFactory::fabricate(StrList *acpt_language_list, StrList *acpt_charset_list, const char *type, HttpTransact::State *context, | ||
| int64_t *buffer_length_return, const char **content_language_return, const char **content_charset_return, | ||
| const char **set_return) | ||
| const char **content_type_return, const char **set_return) | ||
| { | ||
| char *buffer; | ||
| const char *pType = context->txn_conf->body_factory_template_base; | ||
|
|
@@ -386,6 +389,7 @@ HttpBodyFactory::fabricate(StrList *acpt_language_list, StrList *acpt_charset_li | |
| } | ||
| *content_language_return = nullptr; | ||
| *content_charset_return = nullptr; | ||
| *content_type_return = nullptr; | ||
|
|
||
| Dbg(dbg_ctl_body_factory, "calling fabricate(type '%s')", type); | ||
| *buffer_length_return = 0; | ||
|
|
@@ -442,6 +446,7 @@ HttpBodyFactory::fabricate(StrList *acpt_language_list, StrList *acpt_charset_li | |
|
|
||
| *content_language_return = body_set->content_language; | ||
| *content_charset_return = body_set->content_charset; | ||
| *content_type_return = body_set->content_type; | ||
|
|
||
| // build the custom error page | ||
| buffer = t->build_instantiated_buffer(context, buffer_length_return); | ||
|
|
@@ -523,8 +528,9 @@ HttpBodyFactory::determine_set_by_language(std::unique_ptr<BodySetTable> &table_ | |
|
|
||
| is_the_default_set = (strcmp(set_name, "default") == 0); | ||
|
|
||
| Dbg(dbg_ctl_body_factory_determine_set, " --- SET: %-8s (Content-Language '%s', Content-Charset '%s')", set_name, | ||
| body_set->content_language, body_set->content_charset); | ||
| Dbg(dbg_ctl_body_factory_determine_set, " --- SET: %-8s (Content-Language '%s', Content-Charset '%s', Content-Type '%s')", | ||
| set_name, body_set->content_language, body_set->content_charset, | ||
| body_set->content_type ? body_set->content_type : "text/html"); | ||
|
|
||
| // if no Accept-Language hdr at all, treat as a wildcard that | ||
| // slightly prefers "default". | ||
|
|
@@ -894,6 +900,7 @@ HttpBodySet::HttpBodySet() | |
| set_name = nullptr; | ||
| content_language = nullptr; | ||
| content_charset = nullptr; | ||
| content_type = nullptr; | ||
|
|
||
| table_of_pages = nullptr; | ||
| } | ||
|
|
@@ -903,6 +910,7 @@ HttpBodySet::~HttpBodySet() | |
| ats_free(set_name); | ||
| ats_free(content_language); | ||
| ats_free(content_charset); | ||
| ats_free(content_type); | ||
| table_of_pages.reset(nullptr); | ||
| } | ||
|
|
||
|
|
@@ -991,16 +999,15 @@ HttpBodySet::init(char *set, char *dir) | |
| memcpy(value, value_s, value_e - value_s); | ||
| value[value_e - value_s] = '\0'; | ||
|
|
||
| ////////////////////////////////////////////////// | ||
| // so far, we only support 2 pieces of metadata // | ||
| ////////////////////////////////////////////////// | ||
|
|
||
| if (strcasecmp(name, "Content-Language") == 0) { | ||
| ats_free(this->content_language); | ||
| this->content_language = ats_strdup(value); | ||
| } else if (strcasecmp(name, "Content-Charset") == 0) { | ||
| ats_free(this->content_charset); | ||
| this->content_charset = ats_strdup(value); | ||
| } else if (strcasecmp(name, "Content-Type") == 0) { | ||
| ats_free(this->content_type); | ||
| this->content_type = ats_strdup(value); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| ''' | ||
| 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; charset=utf-8 | ||
| """ | ||
|
|
||
| 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( | ||
| 'Content-Type: text/html; charset=utf-8', 'Default body factory should produce text/html with charset') | ||
|
Comment on lines
+83
to
+84
|
||
| 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( | ||
| 'Content-Type: text/plain; charset=utf-8', 'Custom body factory should produce text/plain with charset') | ||
| 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.
Content-Language: kris not a standard language tag for Korean (commonlyko/ko-KRper BCP 47). Since this section is newly documenting metadata directives, consider updating the example to use a standard tag to avoid propagating incorrect configuration patterns.