From 7e5e016695efe574f36286cab3a1e002aae7adc3 Mon Sep 17 00:00:00 2001 From: Eugene Gancharov Date: Fri, 27 Feb 2026 13:23:16 +0700 Subject: [PATCH] Add buffer_response for return to client images from buffer --- configure.ac | 20 ++++++--- src/Makefile.am | 4 +- src/buffer_response.cpp | 35 ++++++++++++++++ src/httpserver.hpp | 1 + src/httpserver/buffer_response.hpp | 66 ++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 src/buffer_response.cpp create mode 100644 src/httpserver/buffer_response.hpp diff --git a/configure.ac b/configure.ac index 66b8e757..821ad687 100644 --- a/configure.ac +++ b/configure.ac @@ -19,13 +19,13 @@ # -*- Autoconf -*- # Process this file with autoconf to produce a configure script. -AC_PREREQ(2.57) +AC_PREREQ([2.71]) m4_define([libhttpserver_MAJOR_VERSION],[0])dnl m4_define([libhttpserver_MINOR_VERSION],[19])dnl m4_define([libhttpserver_REVISION],[0])dnl m4_define([libhttpserver_PKG_VERSION],[libhttpserver_MAJOR_VERSION.libhttpserver_MINOR_VERSION.libhttpserver_REVISION])dnl m4_define([libhttpserver_LDF_VERSION],[libhttpserver_MAJOR_VERSION:libhttpserver_MINOR_VERSION:libhttpserver_REVISION])dnl -AC_INIT([libhttpserver], libhttpserver_PKG_VERSION, [electrictwister2000@gmail.com]) +AC_INIT([libhttpserver],[libhttpserver_PKG_VERSION],[electrictwister2000@gmail.com]) AM_INIT_AUTOMAKE([subdir-objects]) AC_CONFIG_HEADERS([config.h]) AC_CONFIG_MACRO_DIR([m4]) @@ -83,7 +83,15 @@ case "$host" in esac # Checks for header files. -AC_HEADER_STDC +m4_warn([obsolete], +[The preprocessor macro `STDC_HEADERS' is obsolete. + Except in unusual embedded environments, you can safely include all + ISO C90 headers unconditionally.])dnl +# Autoupdate added the next two lines to ensure that your configure +# script's behavior did not change. They are probably safe to remove. +AC_CHECK_INCLUDES_DEFAULT +AC_PROG_EGREP + AC_CHECK_HEADER([stdint.h],[],[AC_MSG_ERROR("stdint.h not found")]) AC_CHECK_HEADER([inttypes.h],[],[AC_MSG_ERROR("inttypes.h not found")]) AC_CHECK_HEADER([errno.h],[],[AC_MSG_ERROR("errno.h not found")]) @@ -284,14 +292,14 @@ AC_CONFIG_FILES([examples/cert.pem:examples/cert.pem]) AC_CONFIG_FILES([examples/key.pem:examples/key.pem]) AC_CONFIG_FILES([examples/test_content:examples/test_content]) -AC_OUTPUT( - libhttpserver.pc +AC_CONFIG_FILES([libhttpserver.pc Makefile doc/Makefile src/Makefile test/Makefile examples/Makefile -) +]) +AC_OUTPUT AC_MSG_NOTICE([Configuration Summary: Operating System: ${host_os} diff --git a/src/Makefile.am b/src/Makefile.am index 41b8cd61..5b2eb327 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,9 +19,9 @@ AM_CPPFLAGS = -I../ -I$(srcdir)/httpserver/ METASOURCES = AUTO lib_LTLIBRARIES = libhttpserver.la -libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp file_info.cpp http_request.cpp http_response.cpp string_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp details/http_endpoint.cpp +libhttpserver_la_SOURCES = string_utilities.cpp webserver.cpp http_utils.cpp file_info.cpp http_request.cpp http_response.cpp string_response.cpp buffer_response.cpp basic_auth_fail_response.cpp digest_auth_fail_response.cpp deferred_response.cpp file_response.cpp http_resource.cpp details/http_endpoint.cpp noinst_HEADERS = httpserver/string_utilities.hpp httpserver/details/modded_request.hpp gettext.h -nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/basic_auth_fail_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp httpserver/http_arg_value.hpp +nobase_include_HEADERS = httpserver.hpp httpserver/create_webserver.hpp httpserver/webserver.hpp httpserver/http_utils.hpp httpserver/file_info.hpp httpserver/details/http_endpoint.hpp httpserver/http_request.hpp httpserver/http_response.hpp httpserver/http_resource.hpp httpserver/string_response.hpp httpserver/buffer_response.hpp httpserver/basic_auth_fail_response.hpp httpserver/digest_auth_fail_response.hpp httpserver/deferred_response.hpp httpserver/file_response.hpp httpserver/http_arg_value.hpp AM_CXXFLAGS += -fPIC -Wall diff --git a/src/buffer_response.cpp b/src/buffer_response.cpp new file mode 100644 index 00000000..3746eda0 --- /dev/null +++ b/src/buffer_response.cpp @@ -0,0 +1,35 @@ +/* + This file is part of libhttpserver + Copyright (C) 2011-2019 Sebastiano Merlino + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + USA +*/ + +#include "httpserver/buffer_response.hpp" +#include +#include +#include + +struct MHD_Response; + +namespace httpserver { + +MHD_Response* buffer_response::get_raw_response() { + // Need to use a const cast here to satisfy MHD interface that requires a void* + return MHD_create_response_from_buffer(dataSize, reinterpret_cast(dataPtr.get()), MHD_RESPMEM_PERSISTENT); +} + +} // namespace httpserver diff --git a/src/httpserver.hpp b/src/httpserver.hpp index 8b706613..41c7f434 100644 --- a/src/httpserver.hpp +++ b/src/httpserver.hpp @@ -38,6 +38,7 @@ #include "httpserver/http_utils.hpp" #include "httpserver/file_info.hpp" #include "httpserver/string_response.hpp" +#include "httpserver/buffer_response.hpp" #include "httpserver/webserver.hpp" #endif // SRC_HTTPSERVER_HPP_ diff --git a/src/httpserver/buffer_response.hpp b/src/httpserver/buffer_response.hpp new file mode 100644 index 00000000..83ac5211 --- /dev/null +++ b/src/httpserver/buffer_response.hpp @@ -0,0 +1,66 @@ +/* + This file is part of libhttpserver + Copyright (C) 2011-2019 Sebastiano Merlino + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + USA +*/ + +#if !defined (_HTTPSERVER_HPP_INSIDE_) && !defined (HTTPSERVER_COMPILATION) +#error "Only or can be included directly." +#endif + +#ifndef SRC_HTTPSERVER_BUFFER_RESPONSE_HPP_ +#define SRC_HTTPSERVER_BUFFER_RESPONSE_HPP_ + +#include +#include "httpserver/http_utils.hpp" +#include "httpserver/http_response.hpp" +#include + +struct MHD_Response; + +namespace httpserver { + +class buffer_response : public http_response { + public: + buffer_response() = default; + + explicit buffer_response( + std::unique_ptr& data, + int size, + int response_code = http::http_utils::http_ok, + const std::string& content_type = http::http_utils::text_plain): + http_response(response_code, content_type), + dataPtr(std::move(data)), + dataSize(size) { } + + buffer_response(const buffer_response& other) = default; + buffer_response(buffer_response&& other) noexcept = default; + + buffer_response& operator=(const buffer_response& b) = default; + buffer_response& operator=(buffer_response&& b) = default; + + ~buffer_response() = default; + + MHD_Response* get_raw_response(); + + private: + std::unique_ptr dataPtr; + int dataSize = 0; +}; + +} // namespace httpserver +#endif // SRC_HTTPSERVER_BUFFER_RESPONSE_HPP_