Skip to content

Commit

Permalink
http server (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwand authored Jan 9, 2025
1 parent 0a8cecb commit d4ab5be
Show file tree
Hide file tree
Showing 23 changed files with 563 additions and 218 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ find_package(nlohmann_json REQUIRED)
find_package(vincentlaucsb-csv-parser REQUIRED)
find_package(uchardet REQUIRED)
find_package(utf8cpp REQUIRED)
find_package(httplib REQUIRED)

configure_file("src/odr/internal/project_info.cpp.in" "src/odr/internal/project_info.cpp")

Expand Down Expand Up @@ -66,6 +67,7 @@ set(ODR_SOURCE_FILES
"src/odr/global_params.cpp"
"src/odr/html.cpp"
"src/odr/html_service.cpp"
"src/odr/http_server.cpp"
"src/odr/open_document_reader.cpp"
"src/odr/quantity.cpp"
"src/odr/style.cpp"
Expand Down Expand Up @@ -194,6 +196,7 @@ target_link_libraries(odr
vincentlaucsb-csv-parser::vincentlaucsb-csv-parser
uchardet::uchardet
utf8::cpp
httplib::httplib
)

if (WITH_PDF2HTMLEX)
Expand Down
11 changes: 11 additions & 0 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ target_include_directories(back_translate
PRIVATE
../src
)

add_executable(server src/server.cpp)
target_link_libraries(server
PRIVATE
odr
httplib::httplib
)
target_include_directories(server
PRIVATE
../src
)
54 changes: 54 additions & 0 deletions cli/src/server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <odr/file.hpp>
#include <odr/http_server.hpp>

#include <iostream>
#include <string>

using namespace odr;

int main(int argc, char **argv) {
std::string input{argv[1]};

std::optional<std::string> password;
if (argc >= 3) {
password = argv[2];
}

DecodePreference decode_preference;
decode_preference.engine_priority = {
DecoderEngine::poppler, DecoderEngine::wvware, DecoderEngine::odr};

DecodedFile decoded_file{input, decode_preference};

if (decoded_file.is_document_file()) {
DocumentFile document_file = decoded_file.document_file();
if (document_file.password_encrypted() && !password) {
std::cerr << "document encrypted but no password given" << std::endl;
return 2;
}
if (document_file.password_encrypted() &&
!document_file.decrypt(*password)) {
std::cerr << "wrong password" << std::endl;
return 1;
}
}

HttpServer::Config config;
HttpServer server(config);

{
std::string id = server.host_file(File(input));
std::cout << "hosted file with id: " << id << std::endl;
std::cout << "http://localhost:8080/" << id << std::endl;
}

{
std::string id = server.host_file(decoded_file);
std::cout << "hosted decoded file with id: " << id << std::endl;
std::cout << "http://localhost:8080/" << id << std::endl;
}

server.listen("localhost", 8080);

return 0;
}
1 change: 0 additions & 1 deletion cli/src/translate.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>

Expand Down
1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def requirements(self):
self.requires("pdf2htmlex/0.18.8.rc1-odr-pr1")
if self.options.get_safe("with_wvWare", False):
self.requires("wvware/1.2.9-odr")
self.requires("cpp-httplib/0.16.3")

def build_requirements(self):
self.test_requires("gtest/1.14.0")
Expand Down
35 changes: 23 additions & 12 deletions src/odr/global_params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@
namespace odr {

GlobalParams &GlobalParams::instance() {
static GlobalParams instance;
return instance;
struct HolderAndInitializer {
GlobalParams params;
HolderAndInitializer() {
#ifdef ODR_WITH_PDF2HTMLEX
globalParams = std::make_unique<::GlobalParams>(
params.m_poppler_data_path.empty()
? nullptr
: params.m_poppler_data_path.c_str());
#endif
}
};
static HolderAndInitializer instance;

return instance.params;
}

const std::string &GlobalParams::odr_core_data_path() {
return instance().m_odr_core_data_path;
}

const std::string &GlobalParams::fontforge_data_path() {
return instance().m_fontforge_data_path;
const std::string &GlobalParams::fontconfig_data_path() {
return instance().m_fontconfig_data_path;
}

const std::string &GlobalParams::poppler_data_path() {
Expand All @@ -33,8 +45,8 @@ void GlobalParams::set_odr_core_data_path(const std::string &path) {
instance().m_odr_core_data_path = path;
}

void GlobalParams::set_fontforge_data_path(const std::string &path) {
instance().m_fontforge_data_path = path;
void GlobalParams::set_fontconfig_data_path(const std::string &path) {
instance().m_fontconfig_data_path = path;
}

void GlobalParams::set_poppler_data_path(const std::string &path) {
Expand All @@ -50,11 +62,10 @@ void GlobalParams::set_pdf2htmlex_data_path(const std::string &path) {
instance().m_pdf2htmlex_data_path = path;
}

GlobalParams::GlobalParams() {
set_odr_core_data_path(""); // TODO
set_fontforge_data_path(internal::project_info::fontconfig_data_path());
set_poppler_data_path(internal::project_info::poppler_data_path());
set_pdf2htmlex_data_path(internal::project_info::pdf2htmlex_data_path());
}
GlobalParams::GlobalParams()
: m_odr_core_data_path{}, // TODO
m_fontconfig_data_path{internal::project_info::fontconfig_data_path()},
m_poppler_data_path{internal::project_info::poppler_data_path()},
m_pdf2htmlex_data_path{internal::project_info::pdf2htmlex_data_path()} {}

} // namespace odr
6 changes: 3 additions & 3 deletions src/odr/global_params.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace odr {
class GlobalParams {
public:
static const std::string &odr_core_data_path();
static const std::string &fontforge_data_path();
static const std::string &fontconfig_data_path();
static const std::string &poppler_data_path();
static const std::string &pdf2htmlex_data_path();

static void set_odr_core_data_path(const std::string &path);
static void set_fontforge_data_path(const std::string &path);
static void set_fontconfig_data_path(const std::string &path);
static void set_poppler_data_path(const std::string &path);
static void set_pdf2htmlex_data_path(const std::string &path);

Expand All @@ -22,7 +22,7 @@ class GlobalParams {
GlobalParams();

std::string m_odr_core_data_path;
std::string m_fontforge_data_path;
std::string m_fontconfig_data_path;
std::string m_poppler_data_path;
std::string m_pdf2htmlex_data_path;
};
Expand Down
13 changes: 6 additions & 7 deletions src/odr/html.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include <nlohmann/json.hpp>

using namespace odr::internal;
namespace fs = std::filesystem;

namespace odr {

Expand Down Expand Up @@ -89,13 +88,13 @@ Html html::translate(const DecodedFile &decoded_file,

Html html::translate(const TextFile &text_file, const std::string &output_path,
const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_text_file(text_file, output_path, config);
}

Html html::translate(const ImageFile &image_file,
const std::string &output_path, const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_image_file(image_file, output_path, config);
}

Expand All @@ -112,7 +111,7 @@ Html html::translate(const DocumentFile &document_file,
if (auto wv_document_file =
std::dynamic_pointer_cast<internal::WvWareLegacyMicrosoftFile>(
document_file_impl)) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_wvware_oldms_file(*wv_document_file,
output_path, config);
}
Expand All @@ -128,7 +127,7 @@ Html html::translate(const PdfFile &pdf_file, const std::string &output_path,
#ifdef ODR_WITH_PDF2HTMLEX
if (auto poppler_pdf_file =
std::dynamic_pointer_cast<internal::PopplerPdfFile>(pdf_file_impl)) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_poppler_pdf_file(*poppler_pdf_file,
output_path, config);
}
Expand All @@ -139,14 +138,14 @@ Html html::translate(const PdfFile &pdf_file, const std::string &output_path,

Html html::translate(const Archive &archive, const std::string &output_path,
const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_filesystem(
FileType::unknown, archive.filesystem(), output_path, config);
}

Html html::translate(const Document &document, const std::string &output_path,
const HtmlConfig &config) {
fs::create_directories(output_path);
std::filesystem::create_directories(output_path);
return internal::html::translate_document(document, output_path, config);
}

Expand Down
3 changes: 2 additions & 1 deletion src/odr/html.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ struct HtmlConfig {
std::string page_output_file_name{"page{index}.html"};

// embedding
bool embed_resources{true};
bool embed_images{true};
bool embed_shipped_resources{true};

// resources
std::string external_resource_path;
Expand Down
61 changes: 31 additions & 30 deletions src/odr/html_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@

namespace odr {

HtmlService::HtmlService(std::shared_ptr<internal::abstract::HtmlService> impl)
: m_impl{std::move(impl)} {}
HtmlDocumentService::HtmlDocumentService() = default;

const HtmlConfig &HtmlService::config() const { return m_impl->config(); }
HtmlDocumentService::HtmlDocumentService(
std::shared_ptr<internal::abstract::HtmlDocumentService> impl)
: m_impl{std::move(impl)} {}

const HtmlResourceLocator &HtmlService::resource_locator() const {
return m_impl->resource_locator();
const HtmlConfig &HtmlDocumentService::config() const {
return m_impl->config();
}

std::vector<HtmlFragment> HtmlService::fragments() const {
std::vector<HtmlFragment> result;
for (const auto &fragment : m_impl->fragments()) {
result.emplace_back(fragment);
}
return result;
const HtmlResourceLocator &HtmlDocumentService::resource_locator() const {
return m_impl->resource_locator();
}

HtmlResources HtmlService::write_document(std::ostream &os) const {
HtmlResources HtmlDocumentService::write_document(std::ostream &os) const {
internal::html::HtmlWriter out(os, config());

auto internal_resources = m_impl->write_document(out);
Expand All @@ -36,35 +33,33 @@ HtmlResources HtmlService::write_document(std::ostream &os) const {
return resources;
}

HtmlFragment::HtmlFragment(
std::shared_ptr<internal::abstract::HtmlFragment> impl)
: m_impl{std::move(impl)} {}
const std::shared_ptr<internal::abstract::HtmlDocumentService> &
HtmlDocumentService::impl() const {
return m_impl;
}

std::string HtmlFragment::name() const { return m_impl->name(); }
HtmlFragmentService::HtmlFragmentService(
std::shared_ptr<internal::abstract::HtmlFragmentService> impl)
: m_impl{std::move(impl)} {}

const HtmlConfig &HtmlFragment::config() const { return m_impl->config(); }
const HtmlConfig &HtmlFragmentService::config() const {
return m_impl->config();
}

const HtmlResourceLocator &HtmlFragment::resource_locator() const {
const HtmlResourceLocator &HtmlFragmentService::resource_locator() const {
return m_impl->resource_locator();
}

void HtmlFragment::write_fragment(std::ostream &os,
HtmlResources &resources) const {
void HtmlFragmentService::write_fragment(std::ostream &os,
HtmlResources &resources) const {
internal::html::HtmlWriter out(os, config());

m_impl->write_fragment(out, resources);
}

HtmlResources HtmlFragment::write_document(std::ostream &os) const {
internal::html::HtmlWriter out(os, config());

auto internal_resources = m_impl->write_document(out);

HtmlResources resources;
for (const auto &[resource, location] : internal_resources) {
resources.emplace_back(HtmlResource(resource), location);
}
return resources;
const std::shared_ptr<internal::abstract::HtmlFragmentService> &
HtmlFragmentService::impl() const {
return m_impl;
}

HtmlResource::HtmlResource() = default;
Expand All @@ -75,6 +70,10 @@ HtmlResource::HtmlResource(

HtmlResourceType HtmlResource::type() const { return m_impl->type(); }

const std::string &HtmlResource::mime_type() const {
return m_impl->mime_type();
}

const std::string &HtmlResource::name() const { return m_impl->name(); }

const std::string &HtmlResource::path() const { return m_impl->path(); }
Expand All @@ -85,6 +84,8 @@ bool HtmlResource::is_shipped() const { return m_impl->is_shipped(); }

bool HtmlResource::is_relocatable() const { return m_impl->is_relocatable(); }

bool HtmlResource::is_external() const { return m_impl->is_external(); }

void HtmlResource::write_resource(std::ostream &os) const {
m_impl->write_resource(os);
}
Expand Down
Loading

0 comments on commit d4ab5be

Please sign in to comment.