Skip to content

Commit 34f3327

Browse files
author
Chrono Law
committed
upstream module
1 parent 5f92061 commit 34f3327

File tree

5 files changed

+246
-0
lines changed

5 files changed

+246
-0
lines changed

modules/upstream/ModNdgUpstream.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 "NdgUpstreamInit.hpp"
5+
6+
auto ndg_upstream_module = NdgUpstreamInit::module();
7+

modules/upstream/NdgUpstreamConf.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NDG_UPSTREAM_CONF_HPP
4+
#define _NDG_UPSTREAM_CONF_HPP
5+
6+
#include "NgxAll.hpp"
7+
8+
class NdgUpstreamConf final
9+
{
10+
public:
11+
typedef NdgUpstreamConf this_type;
12+
public:
13+
NdgUpstreamConf() = default;
14+
~NdgUpstreamConf() = default;
15+
public:
16+
ngx_http_upstream_conf_t upstream;
17+
public:
18+
static void* create(ngx_conf_t* cf)
19+
{
20+
auto& c = *NgxPool(cf).alloc<this_type>();
21+
22+
c.upstream.connect_timeout = 1000*30;
23+
c.upstream.send_timeout = 1000*30;
24+
c.upstream.read_timeout = 1000*30;
25+
c.upstream.buffer_size = ngx_pagesize;
26+
27+
return &c;
28+
}
29+
30+
};
31+
32+
NGX_MOD_INSTANCE(NdgUpstreamModule, ndg_upstream_module, NdgUpstreamConf)
33+
34+
#endif //_NDG_UPSTREAM_CONF_HPP
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright (c) 2015
2+
// Author: Chrono Law
3+
#ifndef _NDG_UPSTREAM_HANDLER_HPP
4+
#define _NDG_UPSTREAM_HANDLER_HPP
5+
6+
#include "NdgUpstreamConf.hpp"
7+
8+
class NdgUpstreamCallback final
9+
{
10+
public:
11+
typedef NgxUpstream upstream_type;
12+
typedef NgxLogDebug log;
13+
public:
14+
static ngx_int_t create_request(ngx_http_request_t* r)
15+
{
16+
ngx_str_t msgs[] = {
17+
r->request_line,
18+
ngx_string("\r\n"),
19+
ngx_string("Host: localhost\r\n"),
20+
ngx_string("\r\n"),
21+
ngx_null_string
22+
};
23+
24+
NgxPool pool(r);
25+
NgxUpstream u(r);
26+
27+
NgxString s = msgs[0];
28+
for(auto i = 0u;!s.empty();s = msgs[++i])
29+
{
30+
NgxBuf buf = pool.buffer();
31+
buf.range(s);
32+
33+
NgxChainNode ch = pool.chain();
34+
ch.set(buf);
35+
36+
log(r).print("proxy %V", s.get());
37+
u.request(ch);
38+
}
39+
40+
log(r).print("send:%V", &r->request_line);
41+
42+
return NGX_OK;
43+
}
44+
45+
static ngx_int_t process_header(ngx_http_request_t* r)
46+
{
47+
NgxUpstream u(r);
48+
49+
u.headers().status_n = NGX_HTTP_OK;
50+
u.headers().content_length_n = 192;
51+
52+
u.state().status = NGX_HTTP_OK;
53+
54+
// do others ...
55+
56+
return NGX_OK;
57+
}
58+
59+
// input_filter
60+
};
61+
62+
class NdgUpstreamHandler final
63+
{
64+
public:
65+
typedef NdgUpstreamHandler this_type;
66+
typedef NdgUpstreamCallback callback_type;
67+
68+
typedef NdgUpstreamModule this_module;
69+
typedef NgxUpstreamHelper<&callback_type::create_request,
70+
&callback_type::process_header>
71+
this_upstream;
72+
public:
73+
static ngx_int_t handler(ngx_http_request_t *r)
74+
try
75+
{
76+
NgxRequest req(r);
77+
if(!req.method(NGX_HTTP_GET))
78+
{
79+
return NGX_HTTP_NOT_ALLOWED;
80+
}
81+
82+
this_upstream u(r);
83+
84+
// get config
85+
auto& cf = this_module::instance().conf().loc(r);
86+
87+
// set upstream confg
88+
u.conf(cf.upstream);
89+
//u.buffering(cf.upstream.buffering);
90+
91+
//u->resolved = NgxPool(r).alloc<ngx_http_upstream_resolved_t>();
92+
//u->resolved->sockaddr = local_addr();
93+
//u->resolved->socklen = sizeof(sockaddr_in);
94+
//u->resolved->naddrs = 1;
95+
96+
return u.start();
97+
}
98+
catch(const NgxException& e)
99+
{
100+
return e.code();
101+
}
102+
103+
static sockaddr* local_addr()
104+
{
105+
static sockaddr_in addr;
106+
107+
addr.sin_family = AF_INET;
108+
addr.sin_port = htons(80);
109+
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
110+
111+
return (sockaddr*)&addr;
112+
}
113+
};
114+
115+
#endif //_NDG_UPSTREAM_HANDLER_HPP

modules/upstream/NdgUpstreamInit.hpp

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

modules/upstream/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/upstream
2+
3+
ngx_addon_name=ndg_upstream_module
4+
5+
HTTP_MODULES="$HTTP_MODULES ndg_upstream_module"
6+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ModNdgUpstream.cpp"

0 commit comments

Comments
 (0)