Skip to content

Commit 22b5a88

Browse files
committed
Add a method to check of responses are empty
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 14889ae commit 22b5a88

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/http/include/sourcemeta/hydra/http_response.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class SOURCEMETA_HYDRA_HTTP_EXPORT Response {
2323

2424
auto status() const noexcept -> Status;
2525
auto header(const std::string &key) const -> std::optional<std::string>;
26+
auto empty() noexcept -> bool;
2627
auto body() -> std::istringstream &;
2728

2829
private:

src/http/response.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <sourcemeta/hydra/http_response.h>
22
#include <sourcemeta/hydra/http_status.h>
33

4+
#include <cassert> // assert
45
#include <map> // std::map
56
#include <optional> // std::optional, std::nullopt
67
#include <sstream> // std::ostringstream, std::istringstream
@@ -26,6 +27,11 @@ auto Response::header(const std::string &key) const
2627
return this->headers_.at(key);
2728
}
2829

29-
auto Response::body() -> std::istringstream & { return this->stream_; }
30+
auto Response::empty() noexcept -> bool { return this->stream_.peek() == -1; }
31+
32+
auto Response::body() -> std::istringstream & {
33+
assert(!this->empty());
34+
return this->stream_;
35+
}
3036

3137
} // namespace sourcemeta::hydra::http

0 commit comments

Comments
 (0)