Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,29 @@ std::map<std::string, int> zim::read_valuesmap(const std::string &s) {
return result;
}

std::string zim::stripMimeParameters(const std::string& rawMimeType) {
size_t pos = rawMimeType.find(';');

// string is clean if no semicolon found
if (pos == std::string::npos){
return rawMimeType;
}

std::string cleanMime = rawMimeType.substr(0, pos);

// removing trailing whitespaces before the semicolon
size_t end = cleanMime.find_last_not_of(" \t");
if (end != std::string::npos){
cleanMime = cleanMime.substr(0, end+1);
}
else {
// fallback if string is just empty space before the semicolon
cleanMime = "";
}

return cleanMime;
}

namespace
{
// The counter metadata format is a list of item separated by a `;` :
Expand Down
2 changes: 2 additions & 0 deletions src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ namespace zim {

std::map<std::string, int> read_valuesmap(const std::string& s);

std::string LIBZIM_PRIVATE_API stripMimeParameters(const std::string& rawMimeType);

using MimeCounterType = std::map<const std::string, zim::entry_index_type>;
MimeCounterType LIBZIM_PRIVATE_API parseMimetypeCounter(const std::string& counterData);

Expand Down
11 changes: 10 additions & 1 deletion src/writer/counterHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "counterHandler.h"
#include "creatordata.h"
#include "../tools.h"

#include <zim/writer/contentProvider.h>
#include <zim/blob.h>
Expand Down Expand Up @@ -59,6 +60,7 @@ DirentHandler::ContentProviders CounterHandler::getContentProviders() const {
return ret;
}


void CounterHandler::handle(Dirent* dirent, const Hints& hints)
{
}
Expand All @@ -72,5 +74,12 @@ void CounterHandler::handle(Dirent* dirent, std::shared_ptr<Item> item)
if (mimetype.empty()) {
return;
}
m_mimetypeCounter[mimetype] += 1;

auto cleanMimetype = zim::stripMimeParameters(mimetype);

if (cleanMimetype.empty()){
return;
}

m_mimetypeCounter[cleanMimetype] += 1;
}
1 change: 1 addition & 0 deletions src/writer/counterHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "handler.h"

#include <map>
#include <string>

namespace zim {
namespace writer {
Expand Down
33 changes: 33 additions & 0 deletions test/tooltesting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ namespace {
ASSERT_THROW(zim::parseIllustrationPathToSize("Illustration_1 28x1 28@1"), std::runtime_error);
}

TEST(Tools, stripMimeParameters) {
// Basic MIME types without parameters - should be unchanged
ASSERT_EQ(zim::stripMimeParameters("text/html"), "text/html");
ASSERT_EQ(zim::stripMimeParameters("application/json"), "application/json");
ASSERT_EQ(zim::stripMimeParameters("image/png"), "image/png");

// Empty string
ASSERT_EQ(zim::stripMimeParameters(""), "");

// MIME types with simple parameters - should strip parameter
ASSERT_EQ(zim::stripMimeParameters("text/html;charset=utf-8"), "text/html");
ASSERT_EQ(zim::stripMimeParameters("text/plain;charset=us-ascii"), "text/plain");

// MIME types with space before semicolon - should trim trailing whitespace
ASSERT_EQ(zim::stripMimeParameters("text/html ;charset=utf-8"), "text/html");
ASSERT_EQ(zim::stripMimeParameters("text/html ;charset=utf-8"), "text/html");
ASSERT_EQ(zim::stripMimeParameters("text/html\t;charset=utf-8"), "text/html");

// Multiple parameters
ASSERT_EQ(zim::stripMimeParameters("text/html;charset=utf-8;boundary=something"), "text/html");

// Edge case: only whitespace before semicolon
ASSERT_EQ(zim::stripMimeParameters(" ;charset=utf-8"), "");
ASSERT_EQ(zim::stripMimeParameters(" ;charset=utf-8"), "");
ASSERT_EQ(zim::stripMimeParameters("\t;charset=utf-8"), "");

// Edge case: semicolon at start
ASSERT_EQ(zim::stripMimeParameters(";charset=utf-8"), "");

// Edge case: just a semicolon
ASSERT_EQ(zim::stripMimeParameters(";"), "");
}

#if defined(ENABLE_XAPIAN)
TEST(Tools, removeAccents) {
ASSERT_EQ(zim::removeAccents("bépoàǹ"), "bepoan");
Expand Down
Loading