2016-02-05 10:10:24 +08:00
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "master_service.h"
|
|
|
|
|
#include "http_servlet.h"
|
2019-07-08 22:08:26 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
#include <process.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-07-09 23:13:42 +08:00
|
|
|
|
static acl::atomic_long __counter = 0;
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
|
|
|
|
http_servlet::http_servlet(acl::socket_stream* stream, acl::session* session)
|
2019-07-08 22:08:26 +08:00
|
|
|
|
: HttpServlet(stream, session)
|
|
|
|
|
, uploading_(false)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
, req_(NULL)
|
|
|
|
|
, res_(NULL)
|
|
|
|
|
, content_length_(0)
|
|
|
|
|
, read_length_(0)
|
|
|
|
|
, mime_(NULL)
|
2019-07-09 23:13:42 +08:00
|
|
|
|
, fsize1_(-1)
|
|
|
|
|
, fsize2_(-1)
|
|
|
|
|
, fsize3_(-1)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
2019-07-08 22:08:26 +08:00
|
|
|
|
handlers_["/upload"] = &http_servlet::onUpload;
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
http_servlet::~http_servlet(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
bool http_servlet::doError(request_t&, response_t& res)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
res.setStatus(400);
|
2019-07-08 22:08:26 +08:00
|
|
|
|
res.setContentType("text/xml; charset=utf-8");
|
2016-02-05 10:10:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧͷ
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (!res.sendHeader()) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
return false;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
|
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("<root error='some error happened!' />\r\n");
|
|
|
|
|
(void) res.getOutputStream().write(buf);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
bool http_servlet::doOther(request_t&, response_t& res, const char* method)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
res.setStatus(400);
|
2019-07-08 22:08:26 +08:00
|
|
|
|
res.setContentType("text/xml; charset=utf-8");
|
2016-02-05 10:10:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧͷ
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (!res.sendHeader()) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
return false;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
|
|
|
|
|
acl::string buf;
|
|
|
|
|
buf.format("<root error='unkown request method %s' />\r\n", method);
|
|
|
|
|
(void) res.getOutputStream().write(buf);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
// ҵ<><D2B5><EFBFBD><EFBFBD><DFBC><EFBFBD><EFBFBD><EFBFBD>
|
2016-02-05 10:10:24 +08:00
|
|
|
|
bool http_servlet::run(void)
|
|
|
|
|
{
|
2019-07-08 22:08:26 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڶ<EFBFBD>ȡ<EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (uploading_) {
|
|
|
|
|
if (req_ == NULL) {
|
|
|
|
|
logger_error("req_ null");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (res_ == NULL) {
|
|
|
|
|
logger_error("res_ null");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return doUpload(*req_, *res_);
|
|
|
|
|
}
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>HTTP<54><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̣<EFBFBD>doGet/doPost <20><><EFBFBD>ᱻ<EFBFBD><E1B1BB><EFBFBD><EFBFBD>
|
|
|
|
|
else {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
return doRun();
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
bool http_servlet::doGet(request_t& req, response_t& res)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
return doPost(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
bool http_servlet::doPost(request_t& req, response_t& res)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
2019-07-08 22:08:26 +08:00
|
|
|
|
const char* path = req.getPathInfo();
|
|
|
|
|
handler_t handler = path && *path ? handlers_[path] : NULL;
|
|
|
|
|
return handler ? (this->*handler)(req, res) : onPage(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ȱʡ HTTP <20><><EFBFBD><EFBFBD> upload.html ҳ<>淵<EFBFBD>ظ<EFBFBD> HTTP <20>ͻ<EFBFBD><CDBB><EFBFBD>
|
|
|
|
|
bool http_servlet::onPage(request_t& req, response_t& res)
|
|
|
|
|
{
|
|
|
|
|
res.setContentType("text/html; 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>
|
|
|
|
|
.setChunkedTransferEncoding(true); // <20><><EFBFBD><EFBFBD> chunk <20><><EFBFBD>䷽ʽ
|
|
|
|
|
|
|
|
|
|
const char* page_html = "upload.html";
|
|
|
|
|
acl::string buf;
|
|
|
|
|
|
|
|
|
|
if (!acl::ifstream::load(page_html, &buf)) {
|
|
|
|
|
buf.format("load %s error %s", page_html, acl::last_serror());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.setContentLength(buf.size());
|
|
|
|
|
return res.write(buf) && res.write(NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool http_servlet::onUpload(request_t& req, response_t& res)
|
|
|
|
|
{
|
|
|
|
|
res.setContentType("text/xml; charset=utf-8") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
2016-02-05 10:10:24 +08:00
|
|
|
|
.setKeepAlive(req.isKeepAlive()) // <20><><EFBFBD><EFBFBD><EFBFBD>Ƿֳ<F1B1A3B3><D6B3><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
.setContentEncoding(true) // <20>Զ<EFBFBD>֧<EFBFBD><D6A7>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
.setChunkedTransferEncoding(true); // <20><><EFBFBD><EFBFBD> chunk <20><><EFBFBD>䷽ʽ
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD> name&value <20><>ʽ
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> MIME <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
acl::http_request_t request_type = req.getRequestType();
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (request_type != acl::HTTP_REQUEST_MULTIPART_FORM) {
|
2019-07-08 22:08:26 +08:00
|
|
|
|
logger_warn("should be acl::HTTP_REQUEST_MULTIPART_FORM");
|
|
|
|
|
return onPage(req, res);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>Ȼ<EFBFBD><C8BB><EFBFBD> Content-Type <20><>Ӧ<EFBFBD><D3A6> http_ctype <20><><EFBFBD><EFBFBD>
|
|
|
|
|
mime_ = req.getHttpMime();
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (mime_ == NULL) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
logger_error("http_mime null");
|
2019-07-08 22:08:26 +08:00
|
|
|
|
return onPage(req, res);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
|
|
|
|
|
content_length_ = req.getContentLength();
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (content_length_ <= 0) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
logger_error("body empty");
|
2019-07-08 22:08:26 +08:00
|
|
|
|
return onPage(req, res);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
acl::string path;
|
2019-07-09 23:13:42 +08:00
|
|
|
|
long long n = ++__counter;
|
|
|
|
|
long long i = __counter.value();
|
|
|
|
|
printf("i=%lld, n=%lld\r\n", i, n);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
2019-07-08 22:08:26 +08:00
|
|
|
|
path.format("%s\\mime_file.%u.%lld",
|
|
|
|
|
var_cfg_var_path, (unsigned) _getpid(), n);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
#else
|
2019-07-08 22:08:26 +08:00
|
|
|
|
path.format("%s/mime_file.%u.%lld",
|
|
|
|
|
var_cfg_var_path, (unsigned) getpid(), n);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
2019-07-09 10:25:22 +08:00
|
|
|
|
acl::meter_time(__FUNCTION__, __LINE__, "begin");
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (fp_.open_write(path)) {
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>ԭʼ<D4AD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
|
|
|
|
mime_->set_saved_path(path);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
req_ = &req;
|
|
|
|
|
res_ = &res;
|
|
|
|
|
uploading_ = true;
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
// ֱ<>ӷ<EFBFBD><D3B7>أ<EFBFBD><D8A3>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
logger_error("open %s error %s", path.c_str(), acl::last_serror());
|
|
|
|
|
return doReply(req, res, "open file error");
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_servlet::reset(void)
|
|
|
|
|
{
|
2019-07-08 22:08:26 +08:00
|
|
|
|
uploading_ = false;
|
|
|
|
|
req_ = NULL;
|
|
|
|
|
res_ = NULL;
|
2016-02-05 10:10:24 +08:00
|
|
|
|
content_length_ = 0;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
read_length_ = 0;
|
|
|
|
|
mime_ = NULL;
|
2016-02-05 10:10:24 +08:00
|
|
|
|
fp_.close();
|
2019-07-25 22:32:30 +08:00
|
|
|
|
|
|
|
|
|
param1_.clear();
|
|
|
|
|
param2_.clear();
|
|
|
|
|
param3_.clear();
|
|
|
|
|
file1_.clear();
|
|
|
|
|
file2_.clear();
|
|
|
|
|
file3_.clear();
|
|
|
|
|
fsize1_ = -1;
|
|
|
|
|
fsize2_ = -1;
|
|
|
|
|
fsize3_ = -1;
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
bool http_servlet::doUpload(request_t& req, response_t& res)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
// <20><>δ<EFBFBD><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ҫ<EFBFBD>첽<EFBFBD><ECB2BD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (content_length_ > read_length_) {
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (!upload(req, res)) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
return false;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
// <20><>û<EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>أ<EFBFBD><D8A3>첽<EFBFBD>ȴ<EFBFBD><C8B4>ɶ<EFBFBD>
|
|
|
|
|
if (content_length_ > read_length_) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
return true;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD><EFBFBD> HTTP <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-07-08 22:08:26 +08:00
|
|
|
|
bool ret = parse(req, res);
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> HTTP <20>Ự״̬<D7B4><CCAC><EFBFBD>Ա<EFBFBD><D4B1>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> HTTP <20><><EFBFBD><EFBFBD>
|
|
|
|
|
reset();
|
2019-07-09 10:25:22 +08:00
|
|
|
|
acl::meter_time(__FUNCTION__, __LINE__, "end");
|
2016-02-05 10:10:24 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-10 21:36:00 +08:00
|
|
|
|
#if 0
|
2019-07-08 22:08:26 +08:00
|
|
|
|
|
|
|
|
|
bool http_servlet::upload(request_t& req, response_t& res)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
acl::istream& in = req.getInputStream();
|
2019-07-08 22:08:26 +08:00
|
|
|
|
acl::string buf(8193);
|
|
|
|
|
bool finish = false;
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
|
|
|
|
//logger(">>>>>>>>>>read: %lld, total: %lld<<<<<",
|
|
|
|
|
// read_length_, content_length_);
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ HTTP <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-07-07 13:27:58 +08:00
|
|
|
|
while (content_length_ > read_length_) {
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (!in.read_peek(buf, true)) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
break;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 10:25:22 +08:00
|
|
|
|
#if 0
|
2019-07-08 22:08:26 +08:00
|
|
|
|
printf(">>>size: %ld, space: %ld\r\n",
|
|
|
|
|
(long) buf.size(), (long) buf.capacity());
|
2019-07-09 10:25:22 +08:00
|
|
|
|
#endif
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (fp_.write(buf) == -1) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
logger_error("write error %s", acl::last_serror());
|
|
|
|
|
(void) doReply(req, res, "write error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
read_length_ += buf.size();
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
|
2019-07-08 22:08:26 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٷ<EFBFBD><D9B7><EFBFBD> mime <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (!finish && mime_->update(buf, buf.size())) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
finish = true;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (in.eof()) {
|
2019-07-08 22:08:26 +08:00
|
|
|
|
logger_error("read error from http client");
|
2016-02-05 10:10:24 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
bool http_servlet::upload(request_t& req, response_t& res)
|
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
acl::istream& in = req.getInputStream();
|
|
|
|
|
char buf[8192];
|
|
|
|
|
bool finish = false;
|
|
|
|
|
|
|
|
|
|
//logger(">>>>>>>>>>read: %lld, total: %lld<<<<<",
|
|
|
|
|
// read_length_, content_length_);
|
|
|
|
|
|
|
|
|
|
// <20><>ȡ HTTP <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
while (content_length_ > read_length_) {
|
|
|
|
|
int ret = in.read_peek(buf, sizeof(buf));
|
|
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 10:25:22 +08:00
|
|
|
|
//printf(">>>size: %d\r\n", ret);
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (fp_.write(buf, ret) == -1) {
|
|
|
|
|
logger_error("write error %s", acl::last_serror());
|
|
|
|
|
(void) doReply(req, res, "write error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
read_length_ += (size_t) ret;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>н<EFBFBD><D0BD><EFBFBD>
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD>Ӷ<EFBFBD><D3B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٷ<EFBFBD><D9B7><EFBFBD> mime <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
if (!finish && mime_->update(buf, (size_t) ret)) {
|
|
|
|
|
finish = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (in.eof()) {
|
|
|
|
|
logger_error("read error from http client");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
bool http_servlet::parse(request_t& req, response_t& res)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
const char* ptr = req.getParameter("name1");
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (ptr) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
param1_ = ptr;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
ptr = req.getParameter("name2");
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (ptr) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
param2_ = ptr;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
ptr = req.getParameter("name3");
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (ptr) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
param3_ = ptr;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
|
|
|
|
acl::string path;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> MIME <20><><EFBFBD>㣬<EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2>ֽ<EFBFBD><D6BD><EFBFBD>ת<EFBFBD><D7AA>
|
|
|
|
|
const std::list<acl::http_mime_node*>& nodes = mime_->get_nodes();
|
|
|
|
|
std::list<acl::http_mime_node*>::const_iterator cit = nodes.begin();
|
2019-07-07 13:27:58 +08:00
|
|
|
|
for (; cit != nodes.end(); ++cit) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
const char* name = (*cit)->get_name();
|
2019-07-08 22:08:26 +08:00
|
|
|
|
if (name == NULL) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
continue;
|
2019-07-08 22:08:26 +08:00
|
|
|
|
}
|
2016-02-05 10:10:24 +08:00
|
|
|
|
|
|
|
|
|
acl::http_mime_t mime_type = (*cit)->get_mime_type();
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (mime_type == acl::HTTP_MIME_FILE) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
const char* filename = (*cit)->get_filename();
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (filename == NULL) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
logger("filename null");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IE<49><45><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><>Ҫ<EFBFBD>Ƚ<EFBFBD>·<EFBFBD><C2B7>ȥ<EFBFBD><C8A5>
|
|
|
|
|
filename = acl_safe_basename(filename);
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
path.format("%s\\%s", var_cfg_var_path, filename);
|
|
|
|
|
#else
|
|
|
|
|
path.format("%s/%s", var_cfg_var_path, filename);
|
|
|
|
|
#endif
|
|
|
|
|
(void) (*cit)->save(path.c_str());
|
|
|
|
|
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (strcmp(name, "file1") == 0) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
file1_ = filename;
|
|
|
|
|
fsize1_ = get_fsize(var_cfg_var_path, filename);
|
2019-07-07 13:27:58 +08:00
|
|
|
|
} else if (strcmp(name, "file2") == 0) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
file2_ = filename;
|
|
|
|
|
fsize2_ = get_fsize(var_cfg_var_path, filename);
|
2019-07-07 13:27:58 +08:00
|
|
|
|
} else if (strcmp(name, "file3") == 0) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
file3_ = filename;
|
|
|
|
|
fsize3_ = get_fsize(var_cfg_var_path, filename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ת<EFBFBD><D7AA>
|
|
|
|
|
const acl::http_mime_node* node = mime_->get_node("file1");
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (node && node->get_mime_type() == acl::HTTP_MIME_FILE) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
ptr = node->get_filename();
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (ptr) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
// <20>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IE<49><45><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
// <20><>Ҫ<EFBFBD>Ƚ<EFBFBD>·<EFBFBD><C2B7>ȥ<EFBFBD><C8A5>
|
|
|
|
|
ptr = acl_safe_basename(ptr);
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
path.format("%s\\1_%s", var_cfg_var_path, ptr);
|
|
|
|
|
#else
|
|
|
|
|
path.format("%s/1_%s", var_cfg_var_path, ptr);
|
|
|
|
|
#endif
|
|
|
|
|
(void) node->save(path.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return doReply(req, res, "OK");
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-08 22:08:26 +08:00
|
|
|
|
bool http_servlet::doReply(request_t& req, response_t& res, const char* info)
|
2016-02-05 10:10:24 +08:00
|
|
|
|
{
|
|
|
|
|
// <20><><EFBFBD><EFBFBD> xml <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
acl::xml1 body;
|
|
|
|
|
|
|
|
|
|
body.get_root().add_child("root", true)
|
|
|
|
|
.add_child("content_type", true)
|
|
|
|
|
.add_attr("type", (int) req.getRequestType())
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("info", true)
|
|
|
|
|
.set_text(info)
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("params", true)
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name1", param1_)
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name2", param2_)
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name3", param3_)
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("files", true)
|
|
|
|
|
.add_child("file", true)
|
|
|
|
|
.add_attr("filename", file1_)
|
|
|
|
|
.add_attr("fsize", fsize1_)
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("file", true)
|
|
|
|
|
.add_attr("filename", file2_)
|
|
|
|
|
.add_attr("fsize", fsize2_)
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("file", true)
|
|
|
|
|
.add_attr("filename", file3_)
|
|
|
|
|
.add_attr("fsize", fsize3_);
|
|
|
|
|
acl::string buf;
|
|
|
|
|
body.build_xml(buf);
|
|
|
|
|
|
|
|
|
|
logger(">>%s<<", buf.c_str());
|
|
|
|
|
return res.write(buf) && res.write(NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long long http_servlet::get_fsize(const char* dir, const char* filename)
|
|
|
|
|
{
|
|
|
|
|
acl::string path;
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
|
path.format("%s\\%s", dir, filename);
|
|
|
|
|
#else
|
|
|
|
|
path.format("%s/%s", dir, filename);
|
|
|
|
|
#endif
|
|
|
|
|
acl::ifstream in;
|
2019-07-07 13:27:58 +08:00
|
|
|
|
if (in.open_read(path) == false) {
|
2016-02-05 10:10:24 +08:00
|
|
|
|
logger_error("open %s error %s", path.c_str(), acl::last_serror());
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return in.fsize();
|
|
|
|
|
}
|