|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011, 2012, 2013, 2014, 2015 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +// This example demonstrates how to serve binary data (e.g., images) directly |
| 22 | +// from an in-memory buffer using string_response. Despite its name, |
| 23 | +// string_response works with arbitrary binary content because std::string can |
| 24 | +// hold any bytes, including null characters. |
| 25 | +// |
| 26 | +// This is useful when you generate or receive binary data at runtime (e.g., |
| 27 | +// from a camera, an image library, or a database) and want to serve it over |
| 28 | +// HTTP without writing it to disk first. |
| 29 | +// |
| 30 | +// To test: |
| 31 | +// curl -o output.png http://localhost:8080/image |
| 32 | + |
| 33 | +#include <memory> |
| 34 | +#include <string> |
| 35 | +#include <utility> |
| 36 | + |
| 37 | +#include <httpserver.hpp> |
| 38 | + |
| 39 | +// Generate a minimal valid 1x1 red PNG image in memory. |
| 40 | +// In a real application, this could come from a camera capture, image |
| 41 | +// processing library, database blob, etc. |
| 42 | +static std::string generate_png_data() { |
| 43 | + // Minimal 1x1 red pixel PNG (68 bytes) |
| 44 | + static const unsigned char png[] = { |
| 45 | + 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, // PNG signature |
| 46 | + 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, // IHDR chunk |
| 47 | + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, // 1x1 |
| 48 | + 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, // 8-bit RGB |
| 49 | + 0xde, 0x00, 0x00, 0x00, 0x0c, 0x49, 0x44, 0x41, // IDAT chunk |
| 50 | + 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf, 0xc0, 0x00, // compressed data |
| 51 | + 0x00, 0x00, 0x03, 0x00, 0x01, 0x36, 0x28, 0x19, |
| 52 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, // IEND chunk |
| 53 | + 0x44, 0xae, 0x42, 0x60, 0x82 |
| 54 | + }; |
| 55 | + |
| 56 | + return std::string(reinterpret_cast<const char*>(png), sizeof(png)); |
| 57 | +} |
| 58 | + |
| 59 | +class image_resource : public httpserver::http_resource { |
| 60 | + public: |
| 61 | + std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&) { |
| 62 | + // Build binary content as a std::string. The string can contain any |
| 63 | + // bytes — it is not limited to printable characters or null-terminated |
| 64 | + // C strings. The size is tracked internally by std::string::size(). |
| 65 | + std::string image_data = generate_png_data(); |
| 66 | + |
| 67 | + // Use string_response with the appropriate content type. The response |
| 68 | + // will send the exact bytes contained in the string. |
| 69 | + return std::make_shared<httpserver::string_response>( |
| 70 | + std::move(image_data), 200, "image/png"); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +int main() { |
| 75 | + httpserver::webserver ws = httpserver::create_webserver(8080); |
| 76 | + |
| 77 | + image_resource ir; |
| 78 | + ws.register_resource("/image", &ir); |
| 79 | + ws.start(true); |
| 80 | + |
| 81 | + return 0; |
| 82 | +} |
0 commit comments