Enable HttpMessage getting output body.

This commit is contained in:
Xie Han
2023-08-23 18:39:39 +08:00
parent 67931f0491
commit c6f182892f
2 changed files with 56 additions and 2 deletions

View File

@@ -67,6 +67,48 @@ bool HttpMessage::append_output_body_nocopy(const void *buf, size_t size)
return false;
}
size_t HttpMessage::get_output_body_blocks(const void *buf[], size_t size[],
size_t max) const
{
struct HttpMessageBlock *block;
struct list_head *pos;
size_t n = 0;
list_for_each(pos, &this->output_body)
{
if (n == max)
break;
block = list_entry(pos, struct HttpMessageBlock, list);
buf[n] = block->ptr;
size[n] = block->size;
}
return n;
}
bool HttpMessage::get_output_body_merged(void *buf, size_t *size) const
{
struct HttpMessageBlock *block;
struct list_head *pos;
if (*size < this->output_body_size)
{
errno = ENOSPC;
return false;
}
list_for_each(pos, &this->output_body)
{
block = list_entry(pos, struct HttpMessageBlock, list);
memcpy(buf, block->ptr, block->size);
buf = (char *)buf + block->size;
}
*size = this->output_body_size;
return true;
}
void HttpMessage::clear_output_body()
{
struct HttpMessageBlock *block;

View File

@@ -115,13 +115,18 @@ public:
return this->append_output_body_nocopy(buf, strlen(buf));
}
void clear_output_body();
size_t get_output_body_size() const
{
return this->output_body_size;
}
size_t get_output_body_blocks(const void *buf[], size_t size[],
size_t max) const;
bool get_output_body_merged(void *buf, size_t *size) const;
void clear_output_body();
/* std::string interfaces */
public:
bool get_http_version(std::string& version) const
@@ -166,6 +171,13 @@ public:
return this->append_output_body_nocopy(buf.c_str(), buf.size());
}
bool get_output_body_merged(std::string& body) const
{
size_t size = this->output_body_size;
body.resize(size);
return this->get_output_body_merged((void *)body.data(), &size);
}
/* for http task implementations. */
public:
bool is_header_complete() const