Commit f6c6b7e2 authored by jan.koester's avatar jan.koester
Browse files

compress support added

parent 8128962b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ find_package(SQLite3 REQUIRED)
find_package(OpenLDAP)
find_package(Hiredis REQUIRED)
find_package(libsecureid REQUIRED)
find_package(Brotli REQUIRED)

include_directories(
    ${NETPLUS_INCLUDE_DIRS}

cmake/FindBrotli.cmake

0 → 100644
+43 −0
Original line number Diff line number Diff line
#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################
include(FindPackageHandleStandardArgs)

find_path(BROTLI_INCLUDE_DIR "brotli/decode.h")

find_library(BROTLICOMMON_LIBRARY NAMES brotlicommon)
find_library(BROTLIDEC_LIBRARY NAMES brotlidec)

find_package_handle_standard_args(Brotli
    FOUND_VAR
      BROTLI_FOUND
    REQUIRED_VARS
      BROTLIDEC_LIBRARY
      BROTLICOMMON_LIBRARY
      BROTLI_INCLUDE_DIR
    FAIL_MESSAGE
      "Could NOT find Brotli"
)

set(BROTLI_INCLUDE_DIRS ${BROTLI_INCLUDE_DIR})
set(BROTLI_LIBRARIES ${BROTLICOMMON_LIBRARY} ${BROTLIDEC_LIBRARY})
+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ target_link_libraries(blogidev
    ${PostgreSQL_LIBRARIES}
    ${SQLite3_LIBRARIES}
    ${OPENLDAP_LIBRARIES}
    ${BROTLI_LIBRARIES}
    confplus
)

@@ -40,6 +41,7 @@ add_executable(blogi
include_directories(
    ${CMAKE_SOURCE_DIR}/data
    ${CMAKE_SOURCE_DIR}/src/database
    ${BROTLI_INCLUDE_DIRS}
)


+26 −3
Original line number Diff line number Diff line
@@ -25,7 +25,9 @@
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <algorithm>
#include <iostream>
#include <cstring>
#include <fstream>
#include <compare>

@@ -34,6 +36,8 @@
#include <unistd.h>
#include <sys/stat.h>

#include <brotli/encode.h>

#include <httppp/exception.h>

#include <htmlpp/exception.h>
@@ -41,6 +45,21 @@
#include "theme.h"
#include "conf.h"

template<typename T>
void compress(const std::string in,std::string &out){
    size_t len=0;
    unsigned char *buf = new unsigned char[BrotliEncoderMaxCompressedSize(in.length())];

    BrotliEncoderCompress(BROTLI_DEFAULT_QUALITY,BROTLI_DEFAULT_WINDOW,BROTLI_MODE_TEXT,
                      in.length(),(const unsigned char*)in.c_str(),&len,buf);

    out.resize(len);

    std::copy(buf,buf+len,out.begin());

    delete[] buf;
}

blogi::Template::Template(blogi::TemplateConfig& config){
    _Config=config;

@@ -74,7 +93,6 @@ blogi::Template::Template(blogi::TemplateConfig& config){

                tfile.Path=std::string("/theme/public/").append(direntStruct->d_name);
                tfile.Content=buf;
                tfile.Compress=false;

                std::string fname=direntStruct->d_name;
                tfile.Ending=fname.substr(fname.rfind(".")+1,fname.length()-(fname.rfind(".")+1));
@@ -82,9 +100,12 @@ blogi::Template::Template(blogi::TemplateConfig& config){
                if(tfile.Ending=="png" || tfile.Ending=="jpg" || tfile.Ending=="webp"){
                    tfile.Type=TemplateFilesTypes::IMAGE;
                }else if(tfile.Ending=="css" || tfile.Ending=="html"){
                    compress<std::string>(tfile.Content,tfile.Compressed);
                    tfile.Type=TemplateFilesTypes::TEXT;
                }else if(tfile.Ending=="js"){
                    compress<std::string>(tfile.Content,tfile.Compressed);
                    tfile.Type=TemplateFilesTypes::JAVASCRIPT;

                }else{
                    tfile.Type=TemplateFilesTypes::GENERIC;
                }
@@ -120,7 +141,6 @@ bool blogi::Template::Controller(netplus::con *curcon,libhttppp::HttpRequest *re
                resp.setState(HTTP200);
                *resp.setData("cache-control") << "max-age=31536000";


                if(curfile->Type==TemplateFilesTypes::IMAGE){
                    resp.setContentType(std::string("image/").append(curfile->Ending).c_str());
                }else if(curfile->Type==TemplateFilesTypes::TEXT){
@@ -131,6 +151,9 @@ bool blogi::Template::Controller(netplus::con *curcon,libhttppp::HttpRequest *re
                    resp.setContentType("application/octet-stream");
                }

                if(!curfile->Compressed.empty() && strstr(req->getData(req->getData("accept-encoding")),"br"))
                    resp.send(curcon,curfile->Compressed.c_str(),curfile->Compressed.length());
                else
                    resp.send(curcon,curfile->Content.c_str(),curfile->Content.length());
                return true;
            }
+1 −1
Original line number Diff line number Diff line
@@ -52,8 +52,8 @@ namespace blogi {
        std::string Path;
        std::string Content;
        std::string Ending;
        std::string Compressed;
        int         Type;
        bool        Compress;
    };

    struct TemplateConfig{