Skip to content

Commit 5f92061

Browse files
author
Chrono Law
committed
echo mdule
1 parent 097d0b7 commit 5f92061

File tree

5 files changed

+257
-0
lines changed

5 files changed

+257
-0
lines changed

modules/echo/ModNdgEcho.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
4+
#include "NdgEchoInit.hpp"
5+
6+
auto ndg_echo_module = NdgEchoInit::module();
7+

modules/echo/NdgEchoConf.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NDG_ECHO_CONF_HPP
4+
#define _NDG_ECHO_CONF_HPP
5+
6+
#include "NgxAll.hpp"
7+
8+
class NdgEchoConf final
9+
{
10+
public:
11+
typedef NdgEchoConf this_type;
12+
public:
13+
NdgEchoConf() = default;
14+
~NdgEchoConf() = default;
15+
public:
16+
ngx_str_t msg;
17+
public:
18+
static void* create(ngx_conf_t* cf)
19+
{
20+
return NgxPool(cf).alloc<this_type>();
21+
}
22+
23+
};
24+
25+
NGX_MOD_INSTANCE(NdgEchoModule, ndg_echo_module, NdgEchoConf)
26+
27+
#endif //_NDG_ECHO_CONF_HPP

modules/echo/NdgEchoHandler.hpp

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NDG_ECHO_HANDLER_HPP
4+
#define _NDG_ECHO_HANDLER_HPP
5+
6+
#include <iostream>
7+
8+
#include "NdgEchoConf.hpp"
9+
10+
class NdgEchoHandler final
11+
{
12+
public:
13+
typedef NdgEchoHandler this_type;
14+
public:
15+
static ngx_int_t handler(ngx_http_request_t *r)
16+
{
17+
//req_line(r);
18+
//req_headers(r);
19+
20+
return process(r);
21+
}
22+
private:
23+
static ngx_int_t process(ngx_http_request_t *r)
24+
try
25+
{
26+
using namespace std;
27+
28+
NgxRequest req(r);
29+
//NgxPool pool(r);
30+
31+
if(!req.method(NGX_HTTP_GET))
32+
{
33+
return NGX_HTTP_NOT_ALLOWED;
34+
}
35+
36+
req.body().discard();
37+
38+
auto& cf = NdgEchoModule::instance().conf().loc(r);
39+
NgxString msg = cf.msg;
40+
cout << "msg = " << msg << endl;
41+
42+
// check args
43+
NgxString args = req->args;
44+
45+
auto len = msg.size();
46+
if(!args.empty())
47+
{
48+
len += args.size()+1;
49+
}
50+
//cout << "len = "<< len << endl;
51+
52+
NgxResponse resp(r);
53+
54+
//auto h = resp.headers();
55+
56+
resp.length(len);
57+
resp.status(NGX_HTTP_OK);
58+
//h->content_length_n = len;
59+
//h->status = NGX_HTTP_OK;
60+
//h->content_length_n = msg.size();
61+
//h->status = NGX_HTTP_OK;
62+
63+
//NgxBuf buf = pool.buffer();
64+
//buf.range(msg);
65+
//resp.send(buf);
66+
if(!args.empty())
67+
{
68+
resp.send(args);
69+
resp.send(",");
70+
}
71+
72+
resp.send(msg);
73+
cout << "send buf" << endl;
74+
//buf.finish();
75+
76+
//buf->memory= true;
77+
//buf->last_buf = true;
78+
//cout << buf.range() << endl;
79+
80+
//NgxChainNode ch = pool.chain();
81+
//ch.set(buf);
82+
//ch.finish();
83+
//assert(!ch->next);
84+
//assert(ch->buf == buf.get());
85+
86+
resp.flush();
87+
cout << "flush" << endl;
88+
return resp.eof();
89+
}
90+
catch(const NgxException& e)
91+
{
92+
return e.code();
93+
}
94+
95+
static void req_line(ngx_http_request_t *r)
96+
{
97+
using namespace std;
98+
99+
cout << r->request_line<< endl;
100+
cout << r->method_name << endl;
101+
cout << r->http_protocol<< endl;
102+
cout << r->uri<< endl;
103+
cout << r->args<< endl;
104+
cout << r->exten<< endl;
105+
cout << r->unparsed_uri<< endl;
106+
107+
NgxVarManager var(r);
108+
cout << "var:";
109+
//cout << var.get("request_method")<< endl;
110+
cout << var["request_method"]<< endl;
111+
cout << var["var1"]<< endl;
112+
assert(!var.get("abc").len);
113+
114+
//var.set("var1", "1234567");
115+
var["var1"] = "1234567";
116+
cout << var["var1"]<< endl;
117+
}
118+
119+
static void req_headers(ngx_http_request_t *r)
120+
{
121+
using namespace std;
122+
123+
NgxRequest req(r);
124+
125+
auto hi = req.headers();
126+
127+
cout << "host = "<<hi.has("host") << endl;
128+
cout << "agent= "<<hi.has("user-agent") << endl;
129+
130+
hi.list().visit(
131+
[](ngx_table_elt_t& kv){
132+
cout << kv.key << "=>"<< kv.value
133+
<< ":"<<kv.hash << endl;
134+
});
135+
}
136+
};
137+
138+
#endif //_NDG_ECHO_HANDLER_HPP

modules/echo/NdgEchoInit.hpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NDG_ECHO_INIT_HPP
4+
#define _NDG_ECHO_INIT_HPP
5+
6+
#include "NdgEchoConf.hpp"
7+
#include "NdgEchoHandler.hpp"
8+
9+
class NdgEchoInit final
10+
{
11+
public:
12+
typedef NdgEchoConf conf_type;
13+
typedef NdgEchoHandler handler_type;
14+
typedef NdgEchoInit this_type;
15+
public:
16+
static ngx_command_t* cmds()
17+
{
18+
static ngx_command_t n[] =
19+
{
20+
NgxCommand(
21+
ngx_string("ndg_echo"),
22+
NgxTake(NGX_HTTP_LOC_CONF, 1),
23+
&this_type::set_echo,
24+
NGX_HTTP_LOC_CONF_OFFSET,
25+
offsetof(conf_type, msg)
26+
),
27+
28+
NgxCommand()
29+
};
30+
31+
return n;
32+
}
33+
public:
34+
static ngx_http_module_t* ctx()
35+
{
36+
static ngx_http_module_t c =
37+
{
38+
NGX_MODULE_NULL(6),
39+
&conf_type::create,
40+
NGX_MODULE_NULL(1),
41+
};
42+
43+
return &c;
44+
}
45+
public:
46+
static const ngx_module_t& module()
47+
{
48+
static ngx_module_t m =
49+
{
50+
NGX_MODULE_V1,
51+
52+
ctx(),
53+
cmds(),
54+
55+
NGX_HTTP_MODULE,
56+
NGX_MODULE_NULL(7),
57+
NGX_MODULE_V1_PADDING
58+
};
59+
60+
return m;
61+
}
62+
private:
63+
static char* set_echo(ngx_conf_t* cf, ngx_command_t* cmd, void* conf)
64+
{
65+
auto rc = ngx_conf_set_str_slot(cf, cmd, conf);
66+
67+
if(rc != NGX_CONF_OK)
68+
{
69+
return rc;
70+
}
71+
72+
NgxHttpCoreModule::instance().handler(
73+
cf, &handler_type::handler);
74+
75+
return NGX_CONF_OK;
76+
}
77+
};
78+
79+
#endif //_NDG_ECHO_INIT_HPP

modules/echo/config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#./configure --add-module=$HOME/ngx_cpp_dev/modules/echo
2+
3+
ngx_addon_name=ndg_echo_module
4+
5+
HTTP_MODULES="$HTTP_MODULES ndg_echo_module"
6+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ModNdgEcho.cpp"

0 commit comments

Comments
 (0)