Beispiel #1
0
int http_client::write_head(const http_header& header)
{
	chunked_transfer_ = header.chunked_transfer();
	string buf;
	if (header.is_request())
		header.build_request(buf);
	else
		header.build_response(buf);
	return get_ostream().write(buf.c_str(), buf.length());
}
Beispiel #2
0
bool http_client::write_body(const void* data, size_t len)
{
	ostream& fp = get_ostream();

	if (chunked_transfer_ == false)
	{
		if (data == NULL || len == 0)
			return true;
		if (fp.write(data, len) == -1)
		{
			disconnected_ = true;
			return false;
		}
		else
			return true;
	}

	// 如果设置了 chunked 传输方式,则按块传输方式写数据

	if (data == NULL || len == 0)
	{
		if (fp.format("0\r\n\r\n") == -1)
		{
			disconnected_ = true;
			return false;
		}
		else
			return true;
	}

	if (fp.format("%x\r\n", (int) len) == -1
		|| fp.write(data, len) == -1
		|| fp.write("\r\n", 2) == -1)
	{
		disconnected_ = true;
		return false;
	}
	return true;
}
 // expose good bit of underlying stream object
 bool good() const { return get_ostream().good(); }