Polyphase Game Engine
Loading...
Searching...
No Matches
HttpResponse.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4#include <string>
5#include <vector>
6
7#include "PolyphaseAPI.h"
10
11class Stream;
12class Texture;
13class SoundWave;
14
16{
17public:
18 // Out-of-line so the engine DLL is the single owner of the class's
19 // construction/destruction/copy/move code paths. Forces every consumer
20 // through one canonical implementation regardless of the TU's view of
21 // std::string's ABI.
26 HttpResponse& operator=(const HttpResponse&);
27 HttpResponse& operator=(HttpResponse&&) noexcept;
28
29 int GetStatus() const { return mStatusCode; }
30 HttpError GetError() const { return mError; }
31 const std::string& GetErrorMessage()const { return mErrorMessage; }
32 const HttpHeaderMap& GetHeaders() const { return mHeaders; }
33 const std::vector<uint8_t>& GetBody() const { return mBody; }
34 const std::string& GetFinalUrl() const { return mFinalUrl; }
35
36 bool IsSuccess() const { return HttpStatusIsSuccess(mStatusCode) && mError == HttpError::None; }
37
38 // Header lookup (case-insensitive). Returns "" when missing.
39 const std::string& GetHeader(const std::string& name) const;
40 bool HasHeader(const std::string& name) const;
41
42 // Body-as-string view. Doesn't copy; reinterprets the byte buffer as a
43 // std::string_view-equivalent (returned by value as std::string for ABI
44 // simplicity). Use GetBody() for raw byte access.
45 std::string GetBodyAsString() const;
46
47 // Wraps the body as a Stream without copying. The Stream's lifetime must
48 // not exceed this HttpResponse's lifetime.
49 Stream GetStream() const;
50
51 // Decode the body as a PNG/JPG/TGA texture. Returns nullptr on failure.
52 // The returned Texture is allocated via the engine factory; callers own
53 // the lifetime via AssetRef in normal use.
54 Texture* GetTexture() const;
55
56 // Decode the body as a WAV or OGG sound. Format detected via magic bytes.
57 // Returns nullptr on failure.
58 SoundWave* GetSoundWave() const;
59
60 // Mutators (used by HttpClient internals).
61 void SetStatus(int code) { mStatusCode = code; }
62 void SetError(HttpError err, std::string msg = {}) { mError = err; mErrorMessage = std::move(msg); }
63 void SetFinalUrl(std::string url) { mFinalUrl = std::move(url); }
64 HttpHeaderMap& MutableHeaders() { return mHeaders; }
65 std::vector<uint8_t>& MutableBody() { return mBody; }
66
67private:
68 int mStatusCode = 0;
70 std::string mErrorMessage;
71 HttpHeaderMap mHeaders;
72 std::vector<uint8_t> mBody;
73 std::string mFinalUrl;
74};
std::map< std::string, std::string, HttpHeaderLess > HttpHeaderMap
Definition HttpRequest.h:17
bool HttpStatusIsSuccess(int statusCode)
Definition HttpTypes.cpp:52
HttpError
Definition HttpTypes.h:21
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
Definition HttpResponse.h:16
bool IsSuccess() const
Definition HttpResponse.h:36
HttpHeaderMap & MutableHeaders()
Definition HttpResponse.h:64
void SetStatus(int code)
Definition HttpResponse.h:61
void SetFinalUrl(std::string url)
Definition HttpResponse.h:63
const std::vector< uint8_t > & GetBody() const
Definition HttpResponse.h:33
HttpResponse(HttpResponse &&) noexcept
void SetError(HttpError err, std::string msg={})
Definition HttpResponse.h:62
std::vector< uint8_t > & MutableBody()
Definition HttpResponse.h:65
const HttpHeaderMap & GetHeaders() const
Definition HttpResponse.h:32
const std::string & GetFinalUrl() const
Definition HttpResponse.h:34
const std::string & GetErrorMessage() const
Definition HttpResponse.h:31
HttpResponse(const HttpResponse &)
HttpError GetError() const
Definition HttpResponse.h:30
Definition SoundWave.h:6
Definition Stream.h:21
Definition Texture.h:10