Skip to content

Commit ce56c24

Browse files
committed
new echo example
1 parent c8947b0 commit ce56c24

File tree

5 files changed

+15
-86
lines changed

5 files changed

+15
-86
lines changed

http/echo/NdgEchoConf.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
// Copyright (c) 2015-2017
1+
// Copyright (c) 2015-2018
22
// Author: Chrono Law
33
#ifndef _NDG_ECHO_CONF_HPP
44
#define _NDG_ECHO_CONF_HPP
55

66
#include "NgxAll.hpp"
77

8-
#define ENABLE_SCRIPT
9-
108
class NdgEchoConf final
119
{
1210
public:
@@ -16,9 +14,6 @@ class NdgEchoConf final
1614
~NdgEchoConf() = default;
1715
public:
1816
ngx_str_t msg;
19-
#ifdef ENABLE_SCRIPT
20-
NgxComplexValue var;
21-
#endif
2217
public:
2318
static void* create(ngx_conf_t* cf)
2419
{
@@ -31,6 +26,6 @@ class NdgEchoConf final
3126
}
3227
};
3328

34-
NGX_MOD_INSTANCE(NdgEchoModule, ndg_echo_module, NdgEchoConf)
29+
NGX_MOD_INSTANCE(NdgEchoModule, ngx_http_ndg_echo_module, NdgEchoConf)
3530

3631
#endif //_NDG_ECHO_CONF_HPP

http/echo/NdgEchoHandler.hpp

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2015-2017
1+
// Copyright (c) 2015-2018
22
// Author: Chrono Law
33
#ifndef _NDG_ECHO_HANDLER_HPP
44
#define _NDG_ECHO_HANDLER_HPP
@@ -14,14 +14,6 @@ class NdgEchoHandler final
1414
typedef NdgEchoModule this_module;
1515
public:
1616
static ngx_int_t handler(ngx_http_request_t *r)
17-
{
18-
//req_line(r);
19-
//req_headers(r);
20-
21-
return process(r);
22-
}
23-
private:
24-
static ngx_int_t process(ngx_http_request_t *r)
2517
try
2618
{
2719
using namespace std;
@@ -36,22 +28,16 @@ class NdgEchoHandler final
3628
req.body().discard();
3729

3830
auto& cf = this_module::conf().loc(r);
39-
#ifdef ENABLE_SCRIPT
40-
auto str = cf.var.str(r);
41-
NgxString msg = str;
4231

43-
// warning. do not use like this
44-
//NgxString msg = cf.var.str(r);
45-
#else
4632
NgxString msg = cf.msg;
47-
#endif
33+
4834
// check args
4935
NgxString args = req->args;
5036

5137
auto len = msg.size();
5238
if(!args.empty())
5339
{
54-
len += args.size()+1;
40+
len += args.size() + 1;
5541
}
5642

5743
NgxResponse resp(r);
@@ -68,58 +54,13 @@ class NdgEchoHandler final
6854
}
6955

7056
resp.send(msg);
71-
resp.flush();
7257

7358
return resp.eof();
7459
}
7560
catch(const NgxException& e)
7661
{
7762
return e.code();
7863
}
79-
80-
static void req_line(ngx_http_request_t *r)
81-
{
82-
using namespace std;
83-
84-
cout << r->request_line<< endl;
85-
cout << r->method_name << endl;
86-
cout << r->http_protocol<< endl;
87-
cout << r->uri<< endl;
88-
cout << r->args<< endl;
89-
cout << r->exten<< endl;
90-
cout << r->unparsed_uri<< endl;
91-
92-
//NgxBuf b(r->header_in);
93-
//cout << b.boundary().len << endl;
94-
NgxVarManager var(r);
95-
cout << "var:";
96-
//cout << var.get("request_method")<< endl;
97-
cout << var["request_method"]<< endl;
98-
cout << var["var1"]<< endl;
99-
assert(!var["abc"].get().len);
100-
101-
//var.set("var1", "1234567");
102-
var["var1"] = "1234567";
103-
cout << var["var1"]<< endl;
104-
}
105-
106-
static void req_headers(ngx_http_request_t *r)
107-
{
108-
using namespace std;
109-
110-
NgxRequest req(r);
111-
112-
auto hi = req.headers();
113-
114-
cout << "host = "<<hi.has("host") << endl;
115-
cout << "agent= "<<hi.has("user-agent") << endl;
116-
117-
hi.list().visit(
118-
[](ngx_table_elt_t& kv){
119-
cout << kv.key << "=>"<< kv.value
120-
<< ":"<<kv.hash << endl;
121-
});
122-
}
12364
};
12465

12566
#endif //_NDG_ECHO_HANDLER_HPP

http/echo/NdgEchoInit.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
class NdgEchoInit final
1010
{
1111
public:
12-
typedef NdgEchoConf conf_type;
13-
typedef NdgEchoHandler handler_type;
14-
typedef NdgEchoInit this_type;
12+
typedef NdgEchoConf conf_type;
13+
typedef NdgEchoHandler handler_type;
14+
typedef NdgEchoInit this_type;
1515
public:
1616
static ngx_command_t* cmds()
1717
{
1818
static ngx_command_t n[] =
1919
{
2020
{
2121
ngx_string("ndg_echo"),
22-
NgxTake(NGX_HTTP_LOC_CONF, 1),
22+
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
2323
&this_type::set_echo,
2424
NGX_HTTP_LOC_CONF_OFFSET,
2525
offsetof(conf_type, msg),
@@ -63,19 +63,12 @@ class NdgEchoInit final
6363
private:
6464
static char* set_echo(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
6565
{
66-
#ifdef ENABLE_SCRIPT
67-
NgxStrArray args(cf->args);
68-
auto& lcf = conf_type::cast(conf);
69-
70-
lcf.var.init(cf, args[1]);
71-
#else
7266
auto rc = ngx_conf_set_str_slot(cf, cmd, conf);
7367

7468
if(rc != NGX_CONF_OK)
7569
{
7670
return rc;
7771
}
78-
#endif
7972

8073
NgxHttpCoreModule::handler(
8174
cf, &handler_type::handler);

http/echo/ModNdgEcho.cpp renamed to http/echo/NdgEchoModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33

44
#include "NdgEchoInit.hpp"
55

6-
auto ndg_echo_module = NdgEchoInit::module();
6+
auto ngx_http_ndg_echo_module = NdgEchoInit::module();
77

http/echo/config

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#./configure --add-module=$HOME/ngx_cpp_dev/modules/echo
22

3-
ngx_addon_name=ndg_echo_module
3+
ngx_addon_name=ngx_http_ndg_echo_module
44

55

66
if test -n "$ngx_module_link"; then
77
ngx_module_type=HTTP
8-
ngx_module_name=ndg_echo_module
9-
ngx_module_srcs="$ngx_addon_dir/ModNdgEcho.cpp"
8+
ngx_module_name=ngx_http_ndg_echo_module
9+
ngx_module_srcs="$ngx_addon_dir/NdgEchoModule.cpp"
1010

1111
. auto/module
1212
else
13-
HTTP_MODULES="$HTTP_MODULES ndg_echo_module"
14-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ModNdgEcho.cpp"
13+
HTTP_MODULES="$HTTP_MODULES ngx_http_ndg_echo_module"
14+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/NdgEchoModule.cpp"
1515
fi
1616

0 commit comments

Comments
 (0)