本文整理汇总了C++中acl::HttpServletRequest::getLocalAddr方法的典型用法代码示例。如果您正苦于以下问题:C++ HttpServletRequest::getLocalAddr方法的具体用法?C++ HttpServletRequest::getLocalAddr怎么用?C++ HttpServletRequest::getLocalAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类acl::HttpServletRequest
的用法示例。
在下文中一共展示了HttpServletRequest::getLocalAddr方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: doPost
bool http_servlet::doPost(acl::HttpServletRequest& req,
acl::HttpServletResponse& res)
{
handled_ = true;
acl::http_client* conn = req.getClient();
conn->header_disable("Accept-Encoding");
logger_request(req);
// 生成完整的 url,以备下面使用
const char* host = req.getRemoteHost();
const char* uri = req.getRequestUri();
if (host == NULL || *host == 0)
host = req.getLocalAddr();
url_.format("http://%s%s", host, uri ? uri : "/");
out_.format(">>> request url: %s\r\n", url_.c_str());
https_client client(out_, client_ssl_conf_);
return client.http_request(req, res);
}
示例2: doPost
bool WebsocketServlet_impl::doPost(acl::HttpServletRequest& req,
acl::HttpServletResponse& res)
{
res.setContentType("text/html; charset=utf-8") // 设置响应字符集
.setContentEncoding(false) // 设置是否压缩数据
.setChunkedTransferEncoding(true); // 采用 chunk 传输方式
const char* ip = req.getLocalAddr();
if (ip == NULL || *ip == 0)
{
logger_error("getLocalAddr error");
return false;
}
unsigned short port = req.getLocalPort();
if (port == 0)
{
logger_error("getLocalPort error");
return false;
}
acl::string local_addr;
local_addr << ip << ":" << port;
printf("getLocalAddr: %s\r\n", local_addr.c_str());
acl::string html_file;
html_file << "www/upload.html";
acl::string buf;
if (acl::ifstream::load(html_file, &buf) == false)
{
logger_error("load %s error %s",
html_file.c_str(), acl::last_serror());
return doError(req, res);
}
buf << "<script>g_url='ws://" << local_addr << "/'</script>";
// 发送 http 响应体,因为设置了 chunk 传输模式,所以需要多调用一次
// res.write 且两个参数均为 0 以表示 chunk 传输数据结束
return res.write(buf) && res.write(NULL, 0);
}
示例3: http_request
bool https_client::http_request(acl::HttpServletRequest& req,
acl::HttpServletResponse& res)
{
const char* host = req.getRemoteHost();
if (host == NULL || *host == 0)
host = req.getLocalAddr();
acl::string server_addr;
if (ssl_conf_ == NULL)
server_addr.format("%s:80", host);
else
server_addr.format("%s:443", host);
std::auto_ptr<acl::http_client> backend(new acl::http_client);
// 连接服务器
if (connect_server(server_addr, *backend) == false)
return false;
acl::http_client* front = req.getClient();
// 取得 HTTP 请求头数据
acl::string req_hdr;
front->sprint_header(req_hdr, NULL);
// 转发 HTTP 请求头至服务器
if (backend->get_ostream().write(req_hdr) == -1)
{
out_.puts(">>>>write header error");
return false;
}
if (backend->get_ostream().write("\r\n") == -1)
{
out_.puts(">>>>write CRLF error");
return false;
}
// 如果还有数据体,则转发请求的数据体给服务器
long long int len = req.getContentLength();
if (len > 0)
{
char req_body[8192];
int ret;
while (true)
{
ret = front->read_body(req_body, sizeof(req_body) - 1);
if (ret < 0)
{
out_.puts(">>> read req body error");
return false;
}
if (ret == 0)
break;
req_body[ret] = 0;
out_.write(req_body, ret);
if (backend->get_ostream().write(req_body, ret) == -1)
{
out_.puts(">>> write body to server error");
return false;
}
}
}
out_.puts("");
// 开始从后端服务器读取响应头和响应体数据
out_.puts(">>>> begin read res header<<<<<");
if (backend->read_head() == false)
{
out_.puts(">>>>>>>>read header error<<<<<<<<<<");
return false;
}
acl::string res_hdr;
backend->sprint_header(res_hdr, NULL);
if (res.getOutputStream().write(res_hdr) == -1)
{
out_.puts(">>>>>write res hdr error<<<<<<");
return false;
}
if (res.getOutputStream().write("\r\n") == -1)
{
out_.puts(">>>write CRLF error");
return false;
}
out_.puts("------------------res hdr----------------");
out_.write(res_hdr);
out_.puts("------------------res hdr end------------");
char buf[8192];
while (true)
{
int ret = backend->read_body(buf, sizeof(buf) - 1);
if (ret < 0)
//.........这里部分代码省略.........