NetServices: Refactor the parsing code into a HttpParser helper class
Change-Id: Ib03aacc36d46318c8870554f794ff66db4cce063
This commit is contained in:
@@ -20,7 +20,9 @@ namespace BPrivate {
|
||||
|
||||
namespace Network {
|
||||
|
||||
class BHttpFields;
|
||||
class BHttpRequest;
|
||||
class BHttpStatus;
|
||||
|
||||
|
||||
class BAbstractDataStream {
|
||||
@@ -57,16 +59,16 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class HttpBuffer {
|
||||
public:
|
||||
using WriteFunction = std::function<size_t (const std::byte*, size_t)>;
|
||||
using HttpTransferFunction = std::function<size_t (const std::byte*, size_t)>;
|
||||
|
||||
|
||||
class HttpBuffer {
|
||||
public:
|
||||
HttpBuffer(size_t capacity = 8*1024);
|
||||
|
||||
ssize_t ReadFrom(BDataIO* source);
|
||||
void WriteExactlyTo(BDataIO* target);
|
||||
void WriteTo(WriteFunction func);
|
||||
void WriteTo(HttpTransferFunction func);
|
||||
std::optional<BString> GetNextLine();
|
||||
|
||||
size_t RemainingBytes() noexcept;
|
||||
@@ -80,6 +82,46 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class HttpParser {
|
||||
public:
|
||||
HttpParser() {};
|
||||
|
||||
// HTTP Header
|
||||
bool ParseStatus(HttpBuffer& buffer, BHttpStatus& status);
|
||||
bool ParseFields(HttpBuffer& buffer, BHttpFields& fields);
|
||||
|
||||
// HTTP Body
|
||||
void SetGzipCompression(bool compression = true);
|
||||
void SetContentLength(std::optional<off_t> contentLength) noexcept;
|
||||
|
||||
size_t ParseBody(HttpBuffer& buffer, HttpTransferFunction writeToBody);
|
||||
std::optional<off_t> BodyBytesTotal() const noexcept { return fBodyBytesTotal; };
|
||||
off_t BodyBytesTransferred() const noexcept { return fTransferredBodySize; };
|
||||
bool Complete() const noexcept;
|
||||
|
||||
private:
|
||||
size_t _ReadChunk(HttpBuffer& buffer, HttpTransferFunction writeToBody,
|
||||
size_t maxSize, bool flush);
|
||||
bool _IsChunked() const noexcept;
|
||||
|
||||
private:
|
||||
off_t fHeaderBytes = 0;
|
||||
|
||||
// Support for chunked transfers
|
||||
std::optional<off_t> fRemainingChunkSize;
|
||||
bool fChunkedTransferComplete = false;
|
||||
|
||||
// Receive stats
|
||||
std::optional<off_t> fBodyBytesTotal = 0;
|
||||
off_t fTransferredBodySize = 0;
|
||||
|
||||
// Optional decompression
|
||||
std::unique_ptr<BMallocIO> fDecompressorStorage = nullptr;
|
||||
std::unique_ptr<BDataIO> fDecompressingStream = nullptr;
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace Network
|
||||
|
||||
} // namespace BPrivate
|
||||
|
||||
Reference in New Issue
Block a user