1/* 2 * Copyright 2022 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6#ifndef _B_HTTP_BUFFER_H_ 7#define _B_HTTP_BUFFER_H_ 8 9#include <functional> 10#include <optional> 11#include <string_view> 12#include <vector> 13 14class BDataIO; 15class BString; 16 17 18namespace BPrivate { 19 20namespace Network { 21 22using HttpTransferFunction = std::function<size_t(const std::byte*, size_t)>; 23 24 25class HttpBuffer 26{ 27public: 28 HttpBuffer(size_t capacity = 8 * 1024); 29 30 ssize_t ReadFrom(BDataIO* source, 31 std::optional<size_t> maxSize = std::nullopt); 32 size_t WriteTo(HttpTransferFunction func, 33 std::optional<size_t> maxSize = std::nullopt); 34 void WriteExactlyTo(HttpTransferFunction func, 35 std::optional<size_t> maxSize = std::nullopt); 36 std::optional<BString> GetNextLine(); 37 38 size_t RemainingBytes() const noexcept; 39 40 void Flush() noexcept; 41 void Clear() noexcept; 42 43 std::string_view Data() const noexcept; 44 45 // load data into the buffer 46 HttpBuffer& operator<<(const std::string_view& data); 47 48private: 49 std::vector<std::byte> fBuffer; 50 size_t fCurrentOffset = 0; 51}; 52 53 54} // namespace Network 55 56} // namespace BPrivate 57 58#endif // _B_HTTP_BUFFER_H_ 59