2019-07-28 10:31:56 +08:00
|
|
|
|
#include "stdafx.h"
|
2017-05-16 23:22:07 +08:00
|
|
|
|
#include "qr.h"
|
|
|
|
|
#include "http_servlet.h"
|
|
|
|
|
|
|
|
|
|
http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session)
|
|
|
|
|
: acl::HttpServlet(stream, session)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http_servlet::~http_servlet(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::doError(acl::HttpServletRequest&,
|
|
|
|
|
acl::HttpServletResponse& res)
|
|
|
|
|
{
|
|
|
|
|
res.setStatus(400);
|
|
|
|
|
res.setContentType("text/xml; charset=utf-8");
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
|
2017-05-16 23:22:07 +08:00
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("<root error='some error happened!' />\r\n");
|
|
|
|
|
res.write(buf);
|
|
|
|
|
res.write(NULL, 0);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::doOther(acl::HttpServletRequest&,
|
|
|
|
|
acl::HttpServletResponse& res, const char* method)
|
|
|
|
|
{
|
|
|
|
|
res.setStatus(400);
|
|
|
|
|
res.setContentType("text/xml; charset=utf-8");
|
2019-07-28 10:31:56 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
|
2017-05-16 23:22:07 +08:00
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("<root error='unkown request method %s' />\r\n", method);
|
|
|
|
|
res.write(buf);
|
|
|
|
|
res.write(NULL, 0);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::doGet(acl::HttpServletRequest& req,
|
|
|
|
|
acl::HttpServletResponse& res)
|
|
|
|
|
{
|
|
|
|
|
return doPost(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::doPost(acl::HttpServletRequest& req,
|
|
|
|
|
acl::HttpServletResponse& res)
|
|
|
|
|
{
|
|
|
|
|
int errcode = QR_ERR_NONE;
|
|
|
|
|
QRCode* qr = qrInit(10, QR_EM_8BIT, 2, -1, &errcode);
|
|
|
|
|
if (qr == NULL)
|
|
|
|
|
{
|
|
|
|
|
logger_error("qrInit error!");
|
|
|
|
|
return replyf(req, res, "qrInit error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char* url = req.getParameter("url");
|
|
|
|
|
if (url == NULL || *url == 0)
|
|
|
|
|
url = "http://www.qq.com";
|
|
|
|
|
|
|
|
|
|
qrAddData(qr, (const qr_byte_t*) url, strlen(url));
|
|
|
|
|
if (!qrFinalize(qr))
|
|
|
|
|
{
|
|
|
|
|
logger_error("qrFinalize error!");
|
|
|
|
|
qrDestroy(qr);
|
|
|
|
|
return replyf(req, res, "qrFinalize error");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
|
qr_byte_t *buffer = qrSymbolToPNG(qr, 5, 5, &size);
|
|
|
|
|
if (buffer == NULL)
|
|
|
|
|
{
|
|
|
|
|
logger_error("qrSymbolToPNG error");
|
|
|
|
|
qrDestroy(qr);
|
|
|
|
|
return replyf(req, res, "qrSymbolToPNG error");
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
res.setContentType("image/png") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|
|
|
|
.setKeepAlive(req.isKeepAlive()) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
.setContentEncoding(true) // <20>Զ<EFBFBD>֧<EFBFBD><D6A7>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-05-16 23:22:07 +08:00
|
|
|
|
.setContentLength(size);
|
|
|
|
|
bool ret = res.write(buffer, size) && res.write(NULL, 0);
|
|
|
|
|
|
|
|
|
|
qrDestroy(qr);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::replyf(acl::HttpServletRequest& req,
|
|
|
|
|
acl::HttpServletResponse& res,
|
|
|
|
|
const char* fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
acl::string buf;
|
|
|
|
|
va_list ap;
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
buf.vformat(fmt, ap);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
2019-07-28 10:31:56 +08:00
|
|
|
|
res.setContentType("text/plain; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|
|
|
|
.setKeepAlive(req.isKeepAlive()) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
.setContentEncoding(true) // <20>Զ<EFBFBD>֧<EFBFBD><D6A7>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-05-16 23:22:07 +08:00
|
|
|
|
.setContentLength(buf.size());
|
|
|
|
|
|
|
|
|
|
return res.write(buf) && res.write(NULL, 0);
|
|
|
|
|
}
|