Skip to content

Commit 6718e0a

Browse files
authored
file_response: Change default argument for content-type (etr#249)
The default value for content-type should be as broad and unspecific as possible, to allow using file_response and don't care about the content-type. Mozilla recommends to use application/octet-stream for data with unknown type. text/plain is too specific for using files of unknown type with the file_response class, and could lead to browsers misinterpreting, showing garbage, or even breaking sites. Link: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types Fixes: etr#248 Signed-off-by: Alexander Dahl <post@lespocky.de>
1 parent 0a02a1e commit 6718e0a

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/http_utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ const char* http_utils::http_method_patch = MHD_HTTP_METHOD_PATCH;
190190
const char* http_utils::http_post_encoding_form_urlencoded = MHD_HTTP_POST_ENCODING_FORM_URLENCODED;
191191
const char* http_utils::http_post_encoding_multipart_formdata = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA;
192192

193+
const char* http_utils::application_octet_stream = "application/octet-stream";
193194
const char* http_utils::text_plain = "text/plain";
194195

195196
std::vector<std::string> http_utils::tokenize_url(const std::string& str, const char separator) {

src/httpserver/file_response.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class file_response : public http_response {
4040
explicit file_response(
4141
const std::string& filename,
4242
int response_code = http::http_utils::http_ok,
43-
const std::string& content_type = http::http_utils::text_plain):
43+
const std::string& content_type = http::http_utils::application_octet_stream):
4444
http_response(response_code, content_type),
4545
filename(filename) { }
4646

src/httpserver/http_utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ class http_utils {
232232
static const char* http_post_encoding_form_urlencoded;
233233
static const char* http_post_encoding_multipart_formdata;
234234

235+
static const char* application_octet_stream;
235236
static const char* text_plain;
236237

237238
static std::vector<std::string> tokenize_url(const std::string&, const char separator = '/');

test/integ/basic.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ class file_response_resource_empty : public http_resource {
214214
}
215215
};
216216

217+
class file_response_resource_default_content_type : public http_resource {
218+
public:
219+
const shared_ptr<http_response> render_GET(const http_request&) {
220+
return shared_ptr<file_response>(new file_response("test_content", 200));
221+
}
222+
};
223+
217224
class exception_resource : public http_resource {
218225
public:
219226
const shared_ptr<http_response> render_GET(const http_request&) {
@@ -871,6 +878,24 @@ LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource_empty)
871878
curl_easy_cleanup(curl);
872879
LT_END_AUTO_TEST(file_serving_resource_empty)
873880

881+
LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource_default_content_type)
882+
file_response_resource_default_content_type resource;
883+
ws->register_resource("base", &resource);
884+
curl_global_init(CURL_GLOBAL_ALL);
885+
886+
map<string, string> ss;
887+
CURL *curl = curl_easy_init();
888+
CURLcode res;
889+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
890+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
891+
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, headerfunc);
892+
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &ss);
893+
res = curl_easy_perform(curl);
894+
LT_ASSERT_EQ(res, 0);
895+
LT_CHECK_EQ(ss["Content-Type"], "application/octet-stream");
896+
curl_easy_cleanup(curl);
897+
LT_END_AUTO_TEST(file_serving_resource_default_content_type)
898+
874899
LT_BEGIN_AUTO_TEST(basic_suite, exception_forces_500)
875900
exception_resource resource;
876901
ws->register_resource("base", &resource);

0 commit comments

Comments
 (0)