2013-08-18 17:42:25 +08:00
|
|
|
|
#include "stdafx.h"
|
2014-04-07 17:27:03 +08:00
|
|
|
|
#include "file_tmpl.h"
|
2013-08-18 17:42:25 +08:00
|
|
|
|
#include "http_creator.h"
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool set_cookies(tpl_t* tpl)
|
|
|
|
|
{
|
|
|
|
|
printf("Please enter cookie name: "); fflush(stdout);
|
|
|
|
|
char line[256];
|
|
|
|
|
int n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, line, sizeof(line));
|
|
|
|
|
if (n == ACL_VSTREAM_EOF)
|
|
|
|
|
{
|
|
|
|
|
printf("enter cookie name error!\r\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string buf;
|
|
|
|
|
buf.format("const char* %s = req.getCookieValue(\"%s\");\r\n", line, line);
|
|
|
|
|
buf.format_append("\tif (%s == NULL)\r\n\t\tres.addCookie(\"%s\", \"{xxx}\");", line, line);
|
|
|
|
|
|
|
|
|
|
tpl_set_field_fmt_global(tpl, "GET_COOKIES", "%s", buf.c_str());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 17:27:03 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
static bool create_threads(file_tmpl& tmpl)
|
2013-08-18 17:42:25 +08:00
|
|
|
|
{
|
2014-04-07 17:27:03 +08:00
|
|
|
|
string file(tmpl.get_project_name());
|
|
|
|
|
file << ".cf";
|
|
|
|
|
if (tmpl.copy_and_replace("master_threads.cf", file.c_str()) == false)
|
|
|
|
|
return false;
|
2013-08-18 17:42:25 +08:00
|
|
|
|
|
2014-04-07 17:27:03 +08:00
|
|
|
|
const char* name = "master_threads";
|
|
|
|
|
const FILE_FROM_TO tab[] = {
|
|
|
|
|
{ "main_threads.cpp", "main.cpp" },
|
|
|
|
|
{ "master_threads.h", "master_service.h" },
|
|
|
|
|
{ "master_threads.cpp", "master_service.cpp" },
|
|
|
|
|
{ "http_servlet.h", "http_servlet.h" },
|
|
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return tmpl.files_copy(name, tab);
|
|
|
|
|
}
|
2013-08-18 17:42:25 +08:00
|
|
|
|
|
2014-04-07 17:27:03 +08:00
|
|
|
|
static bool create_proc(file_tmpl& tmpl)
|
2013-08-18 17:42:25 +08:00
|
|
|
|
{
|
2014-04-07 17:27:03 +08:00
|
|
|
|
string file(tmpl.get_project_name());
|
|
|
|
|
file << ".cf";
|
|
|
|
|
if (tmpl.copy_and_replace("master_proc.cf", file.c_str()) == false)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const char* name = "master_proc";
|
|
|
|
|
const FILE_FROM_TO tab[] = {
|
|
|
|
|
{ "main_proc.cpp", "main.cpp" },
|
|
|
|
|
{ "master_proc.h", "master_service.h" },
|
|
|
|
|
{ "master_proc.cpp", "master_service.cpp" },
|
|
|
|
|
{ "http_servlet.h", "http_servlet.h" },
|
|
|
|
|
{ NULL, NULL }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return tmpl.files_copy(name, tab);
|
2013-08-18 17:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 17:27:03 +08:00
|
|
|
|
static bool create_service(file_tmpl& tmpl)
|
2013-08-18 17:42:25 +08:00
|
|
|
|
{
|
2014-04-07 17:27:03 +08:00
|
|
|
|
printf("choose master_service type:\r\n");
|
|
|
|
|
printf(" t: for master_threads\r\n"
|
|
|
|
|
" p: for master_proc\r\n");
|
|
|
|
|
printf(">");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
|
int n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf));
|
|
|
|
|
if (n == ACL_VSTREAM_EOF)
|
|
|
|
|
return false;
|
|
|
|
|
else if (strcasecmp(buf, "t") == 0)
|
|
|
|
|
create_threads(tmpl);
|
|
|
|
|
else if (strcasecmp(buf, "p") == 0)
|
|
|
|
|
create_proc(tmpl);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
printf("invalid: %s\r\n", buf);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-18 17:42:25 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-07 17:27:03 +08:00
|
|
|
|
static bool create_http_servlet(file_tmpl& tmpl)
|
2013-08-18 17:42:25 +08:00
|
|
|
|
{
|
2014-04-07 17:27:03 +08:00
|
|
|
|
tpl_t* tpl = tmpl.open_tpl("http_servlet.cpp");
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (tpl == NULL)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
printf("Do you want add cookie? y[y/n]: "); fflush(stdout);
|
|
|
|
|
int ch = getchar();
|
|
|
|
|
if (ch != 'n')
|
|
|
|
|
set_cookies(tpl);
|
|
|
|
|
|
|
|
|
|
string filepath;
|
2014-04-07 17:27:03 +08:00
|
|
|
|
filepath.format("%s/http_servlet.cpp", tmpl.get_project_name());
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (tpl_save_as(tpl, filepath.c_str()) != TPL_OK)
|
|
|
|
|
{
|
|
|
|
|
printf("save to %s error: %s\r\n", filepath.c_str(),
|
|
|
|
|
last_serror());
|
|
|
|
|
tpl_free(tpl);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("create %s ok.\r\n", filepath.c_str());
|
|
|
|
|
tpl_free(tpl);
|
2014-04-07 17:27:03 +08:00
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD>÷<EFBFBD><C3B7><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
return create_service(tmpl);
|
2013-08-18 17:42:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void http_creator()
|
|
|
|
|
{
|
2014-04-07 17:27:03 +08:00
|
|
|
|
file_tmpl tmpl;
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ¼
|
|
|
|
|
tmpl.set_path_from("tmpl/http");
|
2013-08-18 17:42:25 +08:00
|
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2014-04-07 17:27:03 +08:00
|
|
|
|
printf("please input your program name: ");
|
|
|
|
|
fflush(stdout);
|
2013-08-18 17:42:25 +08:00
|
|
|
|
|
|
|
|
|
char buf[256];
|
2014-04-07 17:27:03 +08:00
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf));
|
|
|
|
|
if (n == ACL_VSTREAM_EOF)
|
|
|
|
|
break;
|
|
|
|
|
if (n == 0)
|
|
|
|
|
snprintf(buf, sizeof(buf), "http_demo");
|
|
|
|
|
|
|
|
|
|
tmpl.set_project_name(buf);
|
|
|
|
|
// <20><><EFBFBD><EFBFBD>Ŀ¼
|
|
|
|
|
tmpl.create_dirs();
|
|
|
|
|
|
|
|
|
|
printf("please choose one http application type:\r\n");
|
|
|
|
|
printf("s: http servlet\r\n");
|
|
|
|
|
printf(">");
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
|
|
n = acl_vstream_gets_nonl(ACL_VSTREAM_IN, buf, sizeof(buf));
|
2013-08-18 17:42:25 +08:00
|
|
|
|
if (n == ACL_VSTREAM_EOF)
|
|
|
|
|
break;
|
|
|
|
|
else if (strcasecmp(buf, "s") == 0)
|
2014-04-07 17:27:03 +08:00
|
|
|
|
{
|
|
|
|
|
create_http_servlet(tmpl);
|
|
|
|
|
tmpl.create_common();
|
|
|
|
|
}
|
2013-08-18 17:42:25 +08:00
|
|
|
|
else
|
|
|
|
|
printf("unknown flag: %s\r\n", buf);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|