mirror of https://github.com/kiwix/libkiwix.git
Create a utility builder for 416 response.
Also add a map in the response to store specific headers.
This commit is contained in:
parent
6d5cddca12
commit
8d6567d067
|
@ -86,6 +86,17 @@ std::unique_ptr<Response> Response::build_404(const InternalServer& server, cons
|
||||||
return std::move(response);
|
return std::move(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<Response> Response::build_416(const InternalServer& server, size_t resourceLength)
|
||||||
|
{
|
||||||
|
auto response = Response::build(server);
|
||||||
|
response->set_code(MHD_HTTP_RANGE_NOT_SATISFIABLE);
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "bytes */" << resourceLength;
|
||||||
|
response->add_header(MHD_HTTP_HEADER_CONTENT_RANGE, oss.str());
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<Response> Response::build_500(const InternalServer& server, const std::string& msg)
|
std::unique_ptr<Response> Response::build_500(const InternalServer& server, const std::string& msg)
|
||||||
{
|
{
|
||||||
MustacheData data;
|
MustacheData data;
|
||||||
|
@ -209,13 +220,6 @@ MHD_Response*
|
||||||
Response::create_mhd_response(const RequestContext& request)
|
Response::create_mhd_response(const RequestContext& request)
|
||||||
{
|
{
|
||||||
MHD_Response* response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
|
MHD_Response* response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
|
||||||
if ( m_returnCode == 416 ) {
|
|
||||||
std::ostringstream oss;
|
|
||||||
oss << "bytes */" << m_byteRange.length();
|
|
||||||
|
|
||||||
MHD_add_response_header(response,
|
|
||||||
MHD_HTTP_HEADER_CONTENT_RANGE, oss.str().c_str());
|
|
||||||
}
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -285,6 +289,9 @@ MHD_Result Response::send(const RequestContext& request, MHD_Connection* connect
|
||||||
if ( ! etag.empty() )
|
if ( ! etag.empty() )
|
||||||
MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag.c_str());
|
MHD_add_response_header(response, MHD_HTTP_HEADER_ETAG, etag.c_str());
|
||||||
}
|
}
|
||||||
|
for(auto& p: m_customHeaders) {
|
||||||
|
MHD_add_response_header(response, p.first.c_str(), p.second.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
if (m_returnCode == MHD_HTTP_OK && m_byteRange.kind() == ByteRange::RESOLVED_PARTIAL_CONTENT)
|
if (m_returnCode == MHD_HTTP_OK && m_byteRange.kind() == ByteRange::RESOLVED_PARTIAL_CONTENT)
|
||||||
m_returnCode = MHD_HTTP_PARTIAL_CONTENT;
|
m_returnCode = MHD_HTTP_PARTIAL_CONTENT;
|
||||||
|
@ -381,10 +388,8 @@ std::unique_ptr<Response> EntryResponse::build(const InternalServer& server, con
|
||||||
}
|
}
|
||||||
|
|
||||||
if (byteRange.kind() == ByteRange::RESOLVED_UNSATISFIABLE) {
|
if (byteRange.kind() == ByteRange::RESOLVED_UNSATISFIABLE) {
|
||||||
auto response = Response::build(server);
|
auto response = Response::build_416(server, entry.getSize());
|
||||||
response->set_cacheable();
|
response->set_cacheable();
|
||||||
response->set_code(416);
|
|
||||||
response->m_byteRange = byteRange;
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#define KIWIXLIB_SERVER_RESPONSE_H
|
#define KIWIXLIB_SERVER_RESPONSE_H
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
#include <mustache.hpp>
|
#include <mustache.hpp>
|
||||||
#include "byte_range.h"
|
#include "byte_range.h"
|
||||||
|
@ -52,6 +53,7 @@ class Response {
|
||||||
static std::unique_ptr<Response> build(const InternalServer& server);
|
static std::unique_ptr<Response> build(const InternalServer& server);
|
||||||
static std::unique_ptr<Response> build_304(const InternalServer& server, const ETag& etag);
|
static std::unique_ptr<Response> build_304(const InternalServer& server, const ETag& etag);
|
||||||
static std::unique_ptr<Response> build_404(const InternalServer& server, const RequestContext& request, const std::string& bookName);
|
static std::unique_ptr<Response> build_404(const InternalServer& server, const RequestContext& request, const std::string& bookName);
|
||||||
|
static std::unique_ptr<Response> build_416(const InternalServer& server, size_t resourceLength);
|
||||||
static std::unique_ptr<Response> build_500(const InternalServer& server, const std::string& msg);
|
static std::unique_ptr<Response> build_500(const InternalServer& server, const std::string& msg);
|
||||||
|
|
||||||
MHD_Result send(const RequestContext& request, MHD_Connection* connection);
|
MHD_Result send(const RequestContext& request, MHD_Connection* connection);
|
||||||
|
@ -59,6 +61,7 @@ class Response {
|
||||||
void set_code(int code) { m_returnCode = code; }
|
void set_code(int code) { m_returnCode = code; }
|
||||||
void set_cacheable() { m_etag.set_option(ETag::CACHEABLE_ENTITY); }
|
void set_cacheable() { m_etag.set_option(ETag::CACHEABLE_ENTITY); }
|
||||||
void set_server_id(const std::string& id) { m_etag.set_server_id(id); }
|
void set_server_id(const std::string& id) { m_etag.set_server_id(id); }
|
||||||
|
void add_header(const std::string& name, const std::string& value) { m_customHeaders[name] = value; }
|
||||||
|
|
||||||
int getReturnCode() const { return m_returnCode; }
|
int getReturnCode() const { return m_returnCode; }
|
||||||
|
|
||||||
|
@ -72,6 +75,7 @@ class Response {
|
||||||
int m_returnCode;
|
int m_returnCode;
|
||||||
ByteRange m_byteRange;
|
ByteRange m_byteRange;
|
||||||
ETag m_etag;
|
ETag m_etag;
|
||||||
|
std::map<std::string, std::string> m_customHeaders;
|
||||||
|
|
||||||
friend class EntryResponse; // temporary to allow the builder to change m_mode
|
friend class EntryResponse; // temporary to allow the builder to change m_mode
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue