Move some header setting in response's constructors.

It make easier to understand what is somehow constant and what depends
of the context.
This commit is contained in:
Matthieu Gautier 2020-07-30 16:05:58 +02:00
parent 3352c95314
commit 47436f7bdd
1 changed files with 5 additions and 8 deletions

View File

@ -58,6 +58,7 @@ Response::Response(bool verbose)
: m_verbose(verbose), : m_verbose(verbose),
m_returnCode(MHD_HTTP_OK) m_returnCode(MHD_HTTP_OK)
{ {
add_header(MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*");
} }
std::unique_ptr<Response> Response::build(const InternalServer& server) std::unique_ptr<Response> Response::build(const InternalServer& server)
@ -119,8 +120,6 @@ std::unique_ptr<Response> Response::build_redirect(const InternalServer& server,
return response; return response;
} }
static MHD_Result print_key_value (void *cls, enum MHD_ValueKind kind, static MHD_Result print_key_value (void *cls, enum MHD_ValueKind kind,
const char *key, const char *value) const char *key, const char *value)
{ {
@ -276,7 +275,6 @@ ContentResponse::create_mhd_response(const RequestContext& request)
MHD_add_response_header( MHD_add_response_header(
response, MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate"); response, MHD_HTTP_HEADER_CONTENT_ENCODING, "deflate");
} }
MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType.c_str());
return response; return response;
} }
@ -284,7 +282,6 @@ MHD_Result Response::send(const RequestContext& request, MHD_Connection* connect
{ {
MHD_Response* response = create_mhd_response(request); MHD_Response* response = create_mhd_response(request);
MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL, MHD_add_response_header(response, MHD_HTTP_HEADER_CACHE_CONTROL,
m_etag.get_option(ETag::CACHEABLE_ENTITY) ? "max-age=2723040, public" : "no-cache, no-store, must-revalidate"); m_etag.get_option(ETag::CACHEABLE_ENTITY) ? "max-age=2723040, public" : "no-cache, no-store, must-revalidate");
const std::string etag = m_etag.get_etag(); const std::string etag = m_etag.get_etag();
@ -323,7 +320,9 @@ ContentResponse::ContentResponse(const std::string& root, bool verbose, bool wit
m_compress(is_compressible_mime_type(mimetype)), m_compress(is_compressible_mime_type(mimetype)),
m_bookName(""), m_bookName(""),
m_bookTitle("") m_bookTitle("")
{} {
add_header(MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType);
}
std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype) std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& server, const std::string& content, const std::string& mimetype)
{ {
@ -342,7 +341,6 @@ std::unique_ptr<ContentResponse> ContentResponse::build(const InternalServer& se
return ContentResponse::build(server, content, mimetype); return ContentResponse::build(server, content, mimetype);
} }
EntryResponse::EntryResponse(bool verbose, const Entry& entry, const std::string& mimetype, const ByteRange& byterange) : EntryResponse::EntryResponse(bool verbose, const Entry& entry, const std::string& mimetype, const ByteRange& byterange) :
Response(verbose), Response(verbose),
m_entry(entry), m_entry(entry),
@ -350,6 +348,7 @@ EntryResponse::EntryResponse(bool verbose, const Entry& entry, const std::string
{ {
m_byteRange = byterange; m_byteRange = byterange;
set_cacheable(); set_cacheable();
add_header(MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType);
} }
std::unique_ptr<Response> EntryResponse::build(const InternalServer& server, const RequestContext& request, const Entry& entry) std::unique_ptr<Response> EntryResponse::build(const InternalServer& server, const RequestContext& request, const Entry& entry)
@ -389,8 +388,6 @@ EntryResponse::create_mhd_response(const RequestContext& request)
callback_reader_from_entry, callback_reader_from_entry,
new RunningResponse(m_entry, m_byteRange.first()), new RunningResponse(m_entry, m_byteRange.first()),
callback_free_response); callback_free_response);
MHD_add_response_header(response,
MHD_HTTP_HEADER_CONTENT_TYPE, m_mimeType.c_str());
MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes"); MHD_add_response_header(response, MHD_HTTP_HEADER_ACCEPT_RANGES, "bytes");
if ( m_byteRange.kind() == ByteRange::RESOLVED_PARTIAL_CONTENT ) { if ( m_byteRange.kind() == ByteRange::RESOLVED_PARTIAL_CONTENT ) {
std::ostringstream oss; std::ostringstream oss;