2014-07-23 22:36:24 +08:00
|
|
|
|
// main.cpp : <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Ӧ<CCA8>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ㡣
|
2013-08-18 17:42:25 +08:00
|
|
|
|
//
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include <assert.h>
|
2014-07-21 22:32:11 +08:00
|
|
|
|
#include "lib_acl.h"
|
2013-08-18 17:42:25 +08:00
|
|
|
|
#include "acl_cpp/lib_acl.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace acl;
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
class http_servlet : public HttpServlet
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
http_servlet(void)
|
|
|
|
|
{
|
|
|
|
|
param1_ = NULL;
|
|
|
|
|
param2_ = NULL;
|
|
|
|
|
param3_ = NULL;
|
|
|
|
|
file1_ = NULL;
|
|
|
|
|
file2_ = NULL;
|
|
|
|
|
file3_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~http_servlet(void)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// GET <20><><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
virtual bool doGet(HttpServletRequest& req, HttpServletResponse& res)
|
|
|
|
|
{
|
|
|
|
|
return doPost(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// POST <20><><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
virtual bool doPost(HttpServletRequest& req, HttpServletResponse& res)
|
|
|
|
|
{
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD> session <20><EFBFBD><EEB2BB><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2014-07-21 22:32:11 +08:00
|
|
|
|
#if 0
|
2013-08-18 17:42:25 +08:00
|
|
|
|
const char* sid = req.getSession().getAttribute("sid");
|
|
|
|
|
if (*sid == 0)
|
|
|
|
|
req.getSession().setAttribute("sid", "xxxxxx");
|
2014-07-21 22:32:11 +08:00
|
|
|
|
#endif
|
2013-08-18 17:42:25 +08:00
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> HTTP <20><>Ӧͷ
|
2013-08-18 17:42:25 +08:00
|
|
|
|
res.addCookie("name1", "value1");
|
|
|
|
|
res.addCookie("name2", "value2", ".test.com", "/", 3600 * 24);
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// res.setStatus(400); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7>ص<EFBFBD>״̬<D7B4><CCAC>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD>ַ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (0)
|
|
|
|
|
res.setContentType("text/xml; charset=gb2312");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
res.setContentType("text/xml");
|
|
|
|
|
res.setCharacterEncoding("gb2312");
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD> HTTP <20><><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>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
http_request_t request_type = req.getRequestType();
|
|
|
|
|
if (request_type == HTTP_REQUEST_NORMAL)
|
|
|
|
|
return doParams(req, res);
|
|
|
|
|
else if (request_type == HTTP_REQUEST_MULTIPART_FORM)
|
|
|
|
|
return doUpload(req, res);
|
|
|
|
|
assert(request_type == HTTP_REQUEST_OCTET_STREAM);
|
|
|
|
|
return doOctetStream(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool doResponse(HttpServletRequest& req, HttpServletResponse& res)
|
|
|
|
|
{
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> cookie ֵ
|
2013-08-18 17:42:25 +08:00
|
|
|
|
const char* cookie1 = req.getCookieValue("name1");
|
|
|
|
|
const char* cookie2 = req.getCookieValue("name2");
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD> sid session ֵ
|
2014-07-21 22:32:11 +08:00
|
|
|
|
#if 0
|
2013-08-18 17:42:25 +08:00
|
|
|
|
const char* sid = req.getSession().getAttribute("sid");
|
2014-07-21 22:32:11 +08:00
|
|
|
|
#else
|
|
|
|
|
const char* sid = "test_sid";
|
|
|
|
|
#endif
|
2013-08-18 17:42:25 +08:00
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> xml <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
xml body;
|
|
|
|
|
body.get_root().add_child("root", true)
|
|
|
|
|
.add_child("content_type", true)
|
|
|
|
|
.add_attr("type", (int) req.getRequestType())
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("sessions", true)
|
|
|
|
|
.add_child("session", true)
|
|
|
|
|
.add_attr("sid", sid ? sid : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("cookies", true)
|
|
|
|
|
.add_child("cookie", true)
|
|
|
|
|
.add_attr("name1", cookie1 ? cookie1 : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("cookie", true)
|
|
|
|
|
.add_attr("name2", cookie2 ? cookie2 : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("params", true)
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name1", param1_ ? param1_ : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name2", param2_ ? param2_ : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("param", true)
|
|
|
|
|
.add_attr("name3", param3_ ? param3_ : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("files", true)
|
|
|
|
|
.add_child("file", true)
|
|
|
|
|
.add_attr("filename", file1_ ? file1_ : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("file", true)
|
|
|
|
|
.add_attr("filename", file2_ ? file2_ : "null")
|
|
|
|
|
.get_parent()
|
|
|
|
|
.add_child("file", true)
|
|
|
|
|
.add_attr("filename", file3_ ? file3_ : "null");
|
|
|
|
|
string buf;
|
|
|
|
|
body.build_xml(buf);
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧͷ
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (res.sendHeader() == false)
|
|
|
|
|
return false;
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD> http <20><>Ӧ<EFBFBD><D3A6>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (res.getOutputStream().write(buf) == -1)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// GET <20><>ʽ<EFBFBD><CABD> POST <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>㣺
|
2013-08-18 17:42:25 +08:00
|
|
|
|
// Content-Type: application/x-www-form-urlencoded
|
|
|
|
|
bool doParams(HttpServletRequest& req, HttpServletResponse& res)
|
|
|
|
|
{
|
|
|
|
|
param1_ = req.getParameter("name1");
|
|
|
|
|
param2_ = req.getParameter("name2");
|
|
|
|
|
param3_ = req.getParameter("name2");
|
|
|
|
|
return doResponse(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// POST <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>㣺
|
2013-08-18 17:42:25 +08:00
|
|
|
|
// Content-Type: multipart/form-data; boundary=xxx
|
|
|
|
|
bool doUpload(HttpServletRequest& req, HttpServletResponse& res)
|
|
|
|
|
{
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20>Ȼ<EFBFBD><C8BB> Content-Type <20><>Ӧ<EFBFBD><D3A6> http_ctype <20><><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
http_mime* mime = req.getHttpMime();
|
|
|
|
|
if (mime == NULL)
|
|
|
|
|
{
|
|
|
|
|
logger_error("http_mime null");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><C4B3><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
long long int len = req.getContentLength();
|
|
|
|
|
if (len <= 0)
|
|
|
|
|
{
|
|
|
|
|
logger_error("body empty");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
istream& in = req.getInputStream();
|
|
|
|
|
char buf[8192];
|
|
|
|
|
int ret;
|
|
|
|
|
bool n = false;
|
|
|
|
|
|
|
|
|
|
const char* filepath = "./var/mime_file";
|
|
|
|
|
ofstream out;
|
|
|
|
|
out.open_write(filepath);
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>ԭʼ<D4AD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
mime->set_saved_path(filepath);
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><>ȡ HTTP <20>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
while (len > 0)
|
|
|
|
|
{
|
|
|
|
|
ret = in.read(buf, sizeof(buf), false);
|
|
|
|
|
if (ret == -1)
|
|
|
|
|
{
|
|
|
|
|
logger_error("read POST data error");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
out.write(buf, ret);
|
|
|
|
|
|
|
|
|
|
len -= ret;
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <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>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (mime->update(buf, ret) == true)
|
|
|
|
|
{
|
|
|
|
|
n = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
if (len != 0 || n == false)
|
|
|
|
|
logger_warn("not read all data from client");
|
|
|
|
|
|
|
|
|
|
param1_ = req.getParameter("name1");
|
|
|
|
|
param2_ = req.getParameter("name2");
|
|
|
|
|
param3_ = req.getParameter("name3");
|
|
|
|
|
|
|
|
|
|
string path;
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> MIME <20><>㣬<EFBFBD>ҳ<EFBFBD><D2B3><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2>ֽ<EFBFBD><D6BD><EFBFBD>ת<EFBFBD><D7AA>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
const std::list<http_mime_node*>& nodes = mime->get_nodes();
|
|
|
|
|
std::list<http_mime_node*>::const_iterator cit = nodes.begin();
|
|
|
|
|
for (; cit != nodes.end(); ++cit)
|
|
|
|
|
{
|
|
|
|
|
const char* name = (*cit)->get_name();
|
|
|
|
|
http_mime_t mime_type = (*cit)->get_mime_type();
|
|
|
|
|
if (mime_type == HTTP_MIME_FILE)
|
|
|
|
|
{
|
|
|
|
|
const char* filename = (*cit)->get_filename();
|
|
|
|
|
if (filename == NULL)
|
|
|
|
|
{
|
|
|
|
|
logger("filename null");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(name, "file1") == 0)
|
|
|
|
|
file1_ = filename;
|
|
|
|
|
else if (strcmp(name, "file2") == 0)
|
|
|
|
|
file2_ = filename;
|
|
|
|
|
else if (strcmp(name, "file3") == 0)
|
|
|
|
|
file3_ = filename;
|
2014-07-21 22:32:11 +08:00
|
|
|
|
filename = acl_safe_basename(filename);
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
path.format("var\\%s", filename);
|
|
|
|
|
#else
|
2013-08-18 17:42:25 +08:00
|
|
|
|
path.format("./var/%s", filename);
|
2014-07-21 22:32:11 +08:00
|
|
|
|
#endif
|
2013-08-18 17:42:25 +08:00
|
|
|
|
(void) (*cit)->save(path.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ת<EFBFBD><D7AA>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
const http_mime_node* node = mime->get_node("file1");
|
|
|
|
|
if (node && node->get_mime_type() == HTTP_MIME_FILE)
|
|
|
|
|
{
|
|
|
|
|
const char* ptr = node->get_filename();
|
|
|
|
|
if (ptr)
|
|
|
|
|
{
|
|
|
|
|
path.format("./var/1_%s", ptr);
|
|
|
|
|
(void) node->save(path.c_str());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return doResponse(req, res);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// POST <20><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD>㣺
|
2013-08-18 17:42:25 +08:00
|
|
|
|
// Content-Type: application/octet-stream
|
|
|
|
|
bool doOctetStream(HttpServletRequest&, HttpServletResponse&)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
private:
|
|
|
|
|
const char* param1_;
|
|
|
|
|
const char* param2_;
|
|
|
|
|
const char* param3_;
|
|
|
|
|
const char* file1_;
|
|
|
|
|
const char* file2_;
|
|
|
|
|
const char* file3_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static void do_run(socket_stream* stream)
|
|
|
|
|
{
|
|
|
|
|
memcache_session session("127.0.0.1:11211");
|
|
|
|
|
http_servlet servlet;
|
|
|
|
|
servlet.setLocalCharset("gb2312");
|
|
|
|
|
servlet.doRun(session, stream);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>ʱ<EFBFBD>ķ<EFBFBD><C4B7><EFBFBD><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
class master_service : public master_proc
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
master_service() {}
|
|
|
|
|
~master_service() {}
|
|
|
|
|
protected:
|
|
|
|
|
virtual void on_accept(socket_stream* stream)
|
|
|
|
|
{
|
|
|
|
|
do_run(stream);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// WEB <20><><EFBFBD><EFBFBD>ģʽ
|
2013-08-18 17:42:25 +08:00
|
|
|
|
static void do_alone(void)
|
|
|
|
|
{
|
|
|
|
|
master_service service;
|
2014-07-21 22:32:11 +08:00
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
const char* addr = "0.0.0.0:8081";
|
|
|
|
|
printf("listen: %s ...\r\n", addr);
|
2014-07-23 22:36:24 +08:00
|
|
|
|
service.run_alone(addr, NULL, 1); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>з<EFBFBD>ʽ
|
2013-08-18 17:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WEB CGI ģʽ
|
|
|
|
|
static void do_cgi(void)
|
|
|
|
|
{
|
|
|
|
|
do_run(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
|
{
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-07-23 22:36:24 +08:00
|
|
|
|
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (argc >= 2 && strcmp(argv[1], "alone") == 0)
|
|
|
|
|
do_alone();
|
|
|
|
|
else
|
|
|
|
|
do_cgi();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|