acl/lib_acl_cpp/include/acl_cpp/http/HttpServletResponse.hpp

269 lines
9.0 KiB
C++
Raw Normal View History

#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "../stdlib/noncopyable.hpp"
2014-11-19 00:25:21 +08:00
#ifndef ACL_CLIENT_ONLY
2014-11-19 00:25:21 +08:00
namespace acl {
class dbuf_guard;
2014-11-19 00:25:21 +08:00
class string;
class ostream;
class socket_stream;
class http_header;
class http_client;
2014-11-19 00:25:21 +08:00
class HttpCookie;
class HttpServletRequest;
2014-11-19 00:25:21 +08:00
/**
* HTTP
*
2014-11-19 00:25:21 +08:00
*/
class ACL_CPP_API HttpServletResponse : public noncopyable
2014-11-19 00:25:21 +08:00
{
public:
/**
*
* @param stream {socket_stream&}
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse(socket_stream& stream);
2014-11-19 00:25:21 +08:00
~HttpServletResponse(void);
/**
* HTTP
* @param n {acl_int64}
2014-11-19 00:25:21 +08:00
*/
#if defined(_WIN32) || defined(_WIN64)
2014-11-19 00:25:21 +08:00
HttpServletResponse& setContentLength(__int64 n);
#else
HttpServletResponse& setContentLength(long long int n);
#endif
/**
* HTTP chunked
* @param on {bool} true使 setContentLength
* chunked HTTP RFC
* chunked conteng-length
2014-11-19 00:25:21 +08:00
* @return {HttpServletResponse&}
*/
HttpServletResponse& setChunkedTransferEncoding(bool on);
/**
* HTTP
2014-11-19 00:25:21 +08:00
* @param on {bool}
* @return {HttpServletResponse&}
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setKeepAlive(bool on);
/**
* HTTP Content-Type
* text/html text/html; charset=utf8
* @param value {const char*}
* @return {HttpServletResponse&}
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setContentType(const char* value);
/**
* HTTP gzip
* @param gzip {bool} gzip
* @return {HttpServletResponse&}
*/
HttpServletResponse& setContentEncoding(bool gzip);
2014-11-19 00:25:21 +08:00
/**
* HTTP setContentType
*
* @param charset {const char*}
* @return {HttpServletResponse&}
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setCharacterEncoding(const char* charset);
/**
* HTTP
* @param name {const char*} HTTP
* @param value {time_t}
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setDateHeader(const char* name, time_t value);
/**
* HTTP
* @param name {const char*} HTTP
* @param value {const char*}
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setHeader(const char* name, const char* value);
/**
* HTTP
* @param name {const char*} HTTP
* @param value {int}
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setHeader(const char* name, int value);
/**
* 0
* @param from {http_off_t} 0
* @param to {http_off_t}
* @param total {http_off_t}
*
* @return {HttpServletResponse&}
*/
#if defined(_WIN32) || defined(_WIN64)
HttpServletResponse& setRange(__int64 from,
__int64 to, __int64 total);
#else
HttpServletResponse& setRange(long long from,
long long to, long long total);
#endif
2014-11-19 00:25:21 +08:00
/**
* HTTP 1xx, 2xx, 3xx, 4xx, 5xx
* @param status {int} HTTP , 200
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setStatus(int status);
/**
* CGI HttpServlet
* CGI
* @param on {bool} CGI
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setCgiMode(bool on);
/**
* HTTP location
* @param location {const char*} URL
* @param status {int} HTTP 3xx
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& setRedirect(const char* location, int status = 302);
/**
* cookie
*
2014-11-19 00:25:21 +08:00
* @param cookie {HttpCookie*}
*/
HttpServletResponse& addCookie(HttpCookie* cookie);
/**
* cookie
* @param name {const char*} cookie
* @param value {const char*} cookie
* @param domain {const char*} cookie
* @param path {const char*} cookie
* @param expires {time_t} cookie
* cookie ()
2014-11-19 00:25:21 +08:00
*/
HttpServletResponse& addCookie(const char* name, const char* value,
const char* domain = NULL, const char* path = NULL,
time_t expires = 0);
/**
* url url
* @param out {string&}
* @param url {const char*} url
2014-11-19 00:25:21 +08:00
*/
void encodeUrl(string& out, const char* url);
/**
* HTTP
2014-11-19 00:25:21 +08:00
* @return {http_header&}
*/
http_header& getHttpHeader(void) const;
/**
* HTTP
* setChunkedTransferEncoding chunked
* chunked
* sendHeader HTTP
* HTTP 使 chunked
* 0
* @param data {const void*}
* @param len {size_t} data
* @return {bool} false
2014-11-19 00:25:21 +08:00
*/
bool write(const void* data, size_t len);
/**
* HTTP
* HttpServletResponse::write(const void*, size_t)
* 使 chunked
* buf.empty() == true
* @param buf {const string&}
* @return {bool} false
2014-11-19 00:25:21 +08:00
*/
bool write(const string& buf);
/**
* HTTP
* HttpServletResponse::write(const void*, size_t) 使
* chunked write(NULL, 0)
*
* @param fmt {const char*}
* @return {int} > 0 -1
2014-11-19 00:25:21 +08:00
*/
int format(const char* fmt, ...) ACL_CPP_PRINTF(2, 3);
/**
* HTTP
* HttpServletResponse::write(const string&) 使 chunked
* write(NULL, 0)
* @param fmt {const char*}
* @param ap {va_list}
* @return {int} > 0 -1
2014-11-19 00:25:21 +08:00
*/
int vformat(const char* fmt, va_list ap);
///////////////////////////////////////////////////////////////////
/**
* HTTP HTTP
*
* @return {bool} false
*
* getOutputStream socket
*
2014-11-19 00:25:21 +08:00
*/
bool sendHeader(void);
/**
* HTTP sendHeader
* HTTP HTTP
2014-11-19 00:25:21 +08:00
* @return {ostream&}
*/
ostream& getOutputStream(void) const;
/**
* HTTP
2016-09-20 19:55:38 +08:00
* @return {socket_stream&}
*/
2016-09-20 19:55:38 +08:00
socket_stream& getSocketStream(void) const;
/**
* http_client
* @return {http_client*} NULL
*/
http_client* getClient() const
{
return client_;
}
2016-09-20 19:55:38 +08:00
/**
* http HttpServlet
2016-09-20 19:55:38 +08:00
* @param request {HttpServletRequest*}
*/
void setHttpServletRequest(HttpServletRequest* request);
2014-11-19 00:25:21 +08:00
private:
dbuf_guard* dbuf_internal_;
dbuf_guard* dbuf_;
socket_stream& stream_; // 客户端连接流
HttpServletRequest* request_; // http 请求对象
http_client* client_; // http 响应流对象
http_header* header_; // http 响应头
char charset_[32]; // 字符集
char content_type_[32]; // content-type 类型
bool head_sent_; // 是否已经发送了 HTTP 响应头
2014-11-19 00:25:21 +08:00
};
} // namespace acl
#endif // ACL_CLIENT_ONLY