Skip to content

Commit da2c408

Browse files
committed
encapsulate oauth2_log_sink_t; version 1.1.0
Signed-off-by: Hans Zandbelt <hans.zandbelt@zmartzone.eu>
1 parent 71b7c29 commit da2c408

File tree

10 files changed

+472
-236
lines changed

10 files changed

+472
-236
lines changed

.cproject

Lines changed: 369 additions & 184 deletions
Large diffs are not rendered by default.

.project

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
<projects>
66
</projects>
77
<buildSpec>
8-
<buildCommand>
9-
<name>org.eclipse.cdt.autotools.core.genmakebuilderV2</name>
10-
<arguments>
11-
</arguments>
12-
</buildCommand>
138
<buildCommand>
149
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
1510
<triggers>clean,full,incremental,</triggers>
@@ -28,6 +23,5 @@
2823
<nature>org.eclipse.cdt.core.ccnature</nature>
2924
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
3025
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
31-
<nature>org.eclipse.cdt.autotools.core.autotoolsNatureV2</nature>
3226
</natures>
3327
</projectDescription>

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
07/01/2019
2+
- encapsulate oauth2_log_sink_t
3+
- bump to version 1.1.0
4+
15
05/20/2019
26
- add Apache Require claim authorization functions
37
- bump to version 1.0.1

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AC_INIT([liboauth2],[1.0.1],[hans.zandbelt@zmartzone.eu])
1+
AC_INIT([liboauth2],[1.1.0],[hans.zandbelt@zmartzone.eu])
22
AC_CONFIG_HEADERS([include/oauth2/config.h])
33

44
AM_INIT_AUTOMAKE([foreign no-define subdir-objects])

include/oauth2/apache.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,26 @@ extern oauth2_uint_t log_level_apache2oauth2[];
7171
oauth2_log_sink_t *sink, const char *filename, unsigned long line, \
7272
const char *function, oauth2_log_level_t level, const char *msg) \
7373
{ \
74-
ap_log_error(filename, line, \
75-
aplog_module_index ? *aplog_module_index \
76-
: APLOG_NO_MODULE, \
77-
log_level_log2apache[level], 0, \
78-
(const server_rec *)sink->ctx, "%s: %s", \
79-
function, msg); \
74+
ap_log_error( \
75+
filename, line, \
76+
aplog_module_index ? *aplog_module_index \
77+
: APLOG_NO_MODULE, \
78+
log_level_log2apache[level], 0, \
79+
(const server_rec *)oauth2_log_sink_ctx_get(sink), \
80+
"%s: %s", function, msg); \
8081
} \
8182
\
8283
static void foo##_log_request( \
8384
oauth2_log_sink_t *sink, const char *filename, unsigned long line, \
8485
const char *function, oauth2_log_level_t level, const char *msg) \
8586
{ \
86-
ap_log_rerror(filename, line, \
87-
aplog_module_index ? *aplog_module_index \
88-
: APLOG_NO_MODULE, \
89-
log_level_log2apache[level], 0, \
90-
(const request_rec *)sink->ctx, "%s: %s", \
91-
function, msg); \
87+
ap_log_rerror( \
88+
filename, line, \
89+
aplog_module_index ? *aplog_module_index \
90+
: APLOG_NO_MODULE, \
91+
log_level_log2apache[level], 0, \
92+
(const request_rec *)oauth2_log_sink_ctx_get(sink), \
93+
"%s: %s", function, msg); \
9294
}
9395

9496
/*

include/oauth2/log.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ typedef void (*oauth2_log_function_t)(oauth2_log_sink_t *sink,
9191
oauth2_log_level_t level,
9292
const char *msg);
9393

94-
typedef struct oauth2_log_sink_t {
95-
oauth2_log_level_t level;
96-
oauth2_log_function_t callback;
97-
void *ctx;
98-
} oauth2_log_sink_t;
99-
10094
/*
10195
* API
10296
*/
@@ -108,6 +102,11 @@ void oauth2_log(oauth2_log_t *log, const char *filename, unsigned long line,
108102
const char *function, oauth2_log_level_t level, const char *fmt,
109103
...);
110104

105+
oauth2_log_sink_t *oauth2_log_sink_create(oauth2_log_level_t level,
106+
oauth2_log_function_t callback,
107+
void *ctx);
108+
void *oauth2_log_sink_ctx_get(oauth2_log_sink_t *sink);
109+
oauth2_log_function_t oauth2_log_sink_callback_get(oauth2_log_sink_t *sink);
111110
void oauth2_log_sink_add(oauth2_log_t *log, oauth2_log_sink_t *add);
112111
void oauth2_log_sink_level_set(oauth2_log_sink_t *sink,
113112
oauth2_log_level_t level);

src/log.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
#include <stdarg.h>
2727
#include <stdlib.h>
2828

29+
typedef struct oauth2_log_sink_t {
30+
oauth2_log_level_t level;
31+
oauth2_log_function_t callback;
32+
void *ctx;
33+
} oauth2_log_sink_t;
34+
2935
// note this is fastest, but must maintain the order of the enum...
3036
static const char *_oauth2_log_level2str[] = {"ERR", "WRN", "NOT", "INF",
3137
"DBG", "TR1", "TR2"};
@@ -44,6 +50,27 @@ typedef struct oauth2_log_t {
4450
oauth2_log_sink_list_t sinks;
4551
} oauth2_log_t;
4652

53+
oauth2_log_sink_t *oauth2_log_sink_create(oauth2_log_level_t level,
54+
oauth2_log_function_t callback,
55+
void *ctx)
56+
{
57+
oauth2_log_sink_t *sink = oauth2_mem_alloc(sizeof(oauth2_log_sink_t));
58+
sink->callback = callback;
59+
sink->level = level;
60+
sink->ctx = ctx;
61+
return sink;
62+
}
63+
64+
void *oauth2_log_sink_ctx_get(oauth2_log_sink_t *sink)
65+
{
66+
return sink->ctx;
67+
}
68+
69+
oauth2_log_function_t oauth2_log_sink_callback_get(oauth2_log_sink_t *sink)
70+
{
71+
return sink->callback;
72+
}
73+
4774
void oauth2_log_sink_add(oauth2_log_t *log, oauth2_log_sink_t *add)
4875
{
4976
oauth2_log_sink_list_elem_t *ptr =

src/server/apache.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,12 @@ void *oauth2_apache_cfg_srv_create(apr_pool_t *pool, server_rec *s,
112112
(oauth2_apache_cfg_srv_t *)oauth2_mem_alloc(
113113
sizeof(oauth2_apache_cfg_srv_t));
114114

115-
cfg->sink = oauth2_mem_alloc(sizeof(oauth2_log_sink_t));
116-
cfg->sink->callback = server_log_cb;
117115
// NB: this is not actually set to the/a configured level here...
118-
cfg->sink->level = (s && (s->log.level != -1))
119-
? log_level_apache2oauth2[s->log.level]
120-
: OAUTH2_LOG_TRACE1;
121-
cfg->sink->ctx = s;
122-
cfg->log = oauth2_log_init(cfg->sink->level, cfg->sink);
116+
oauth2_uint_t level = (s && (s->log.level != -1))
117+
? log_level_apache2oauth2[s->log.level]
118+
: OAUTH2_LOG_TRACE1;
119+
cfg->sink = oauth2_log_sink_create(level, server_log_cb, s);
120+
cfg->log = oauth2_log_init(level, cfg->sink);
123121

124122
return cfg;
125123
}
@@ -128,7 +126,8 @@ void *oauth2_apache_cfg_srv_merge(apr_pool_t *pool, void *b, void *a)
128126
{
129127
oauth2_apache_cfg_srv_t *add = (oauth2_apache_cfg_srv_t *)a;
130128
oauth2_apache_cfg_srv_t *cfg = oauth2_apache_cfg_srv_create(
131-
pool, (server_rec *)add->sink->ctx, add->sink->callback);
129+
pool, (server_rec *)oauth2_log_sink_ctx_get(add->sink),
130+
oauth2_log_sink_ctx_get(add->sink));
132131
return cfg;
133132
}
134133

@@ -194,9 +193,10 @@ int oauth2_apache_post_config(apr_pool_t *pool, apr_pool_t *p1, apr_pool_t *p2,
194193
cfg = (oauth2_apache_cfg_srv_t *)ap_get_module_config(
195194
sp->module_config, m);
196195
// only now the level has been set according to the config!
197-
cfg->sink->level = (sp && (sp->log.level != -1))
198-
? log_level_apache2oauth2[sp->log.level]
199-
: OAUTH2_LOG_TRACE1;
196+
oauth2_log_sink_level_set(
197+
cfg->sink, (sp && (sp->log.level != -1))
198+
? log_level_apache2oauth2[sp->log.level]
199+
: OAUTH2_LOG_TRACE1);
200200
}
201201

202202
apr_pool_cleanup_register(pool, s, parent_cleanup, child_cleanup);
@@ -254,14 +254,11 @@ oauth2_apache_request_context_init(request_rec *r,
254254
ctx->r = r;
255255

256256
// TODO: more elegant log-for-request handling
257-
log_sink_apache = oauth2_mem_alloc(sizeof(oauth2_log_sink_t));
258-
log_sink_apache->callback = request_log_cb;
259-
log_sink_apache->level = (r && r->log)
260-
? log_level_apache2oauth2[r->log->level]
261-
: OAUTH2_LOG_TRACE1;
262-
log_sink_apache->level = OAUTH2_LOG_TRACE1;
263-
log_sink_apache->ctx = r;
264-
ctx->log = oauth2_log_init(log_sink_apache->level, log_sink_apache);
257+
oauth2_log_level_t level = (r && r->log)
258+
? log_level_apache2oauth2[r->log->level]
259+
: OAUTH2_LOG_TRACE1;
260+
log_sink_apache = oauth2_log_sink_create(level, request_log_cb, r);
261+
ctx->log = oauth2_log_init(level, log_sink_apache);
265262

266263
ctx->request = oauth2_http_request_init(ctx->log);
267264

src/server/nginx.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ void oauth2_nginx_log(oauth2_log_sink_t *sink, const char *filename,
6262
oauth2_log_level_t level, const char *msg)
6363
{
6464
// TODO: ngx_err_t?
65-
ngx_log_error_core(log_level_log2nginx[level], (ngx_log_t *)sink->ctx,
66-
0, "# %s: %s", function, msg);
65+
ngx_log_error_core(log_level_log2nginx[level],
66+
(ngx_log_t *)oauth2_log_sink_ctx_get(sink), 0,
67+
"# %s: %s", function, msg);
6768
}
6869

6970
oauth2_nginx_request_context_t *
@@ -80,13 +81,11 @@ oauth2_nginx_request_context_init(ngx_http_request_t *r)
8081

8182
ctx->r = r;
8283

83-
// TODO: more elegant log-for-request handling
84-
log_sink_nginx = oauth2_mem_alloc(sizeof(oauth2_log_sink_t));
85-
log_sink_nginx->callback = oauth2_nginx_log;
8684
// TODO: get the log level from NGINX...
87-
log_sink_nginx->level = OAUTH2_LOG_TRACE1;
88-
log_sink_nginx->ctx = r->connection->log;
89-
ctx->log = oauth2_log_init(log_sink_nginx->level, log_sink_nginx);
85+
oauth2_log_level_t level = OAUTH2_LOG_TRACE1;
86+
log_sink_nginx =
87+
oauth2_log_sink_create(level, oauth2_nginx_log, r->connection->log);
88+
ctx->log = oauth2_log_init(level, log_sink_nginx);
9089

9190
oauth2_debug(ctx->log, "created NGINX request context: %p", ctx);
9291

test/check_log.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,34 @@ START_TEST(test_log)
5151
}
5252
END_TEST
5353

54+
static int check_log_test_sink_callback_dummy = 0;
55+
56+
static void
57+
check_log_test_sink_callback(oauth2_log_sink_t *sink, const char *filename,
58+
unsigned long line, const char *function,
59+
oauth2_log_level_t level, const char *msg)
60+
{
61+
check_log_test_sink_callback_dummy = 1;
62+
}
63+
64+
START_TEST(test_sink)
65+
{
66+
char *dummy = "dummy";
67+
oauth2_log_sink_t *sink = oauth2_log_sink_create(
68+
OAUTH2_LOG_TRACE1, check_log_test_sink_callback, dummy);
69+
70+
oauth2_log_sink_add(log, sink);
71+
72+
ck_assert_ptr_eq(oauth2_log_sink_callback_get(sink),
73+
check_log_test_sink_callback);
74+
ck_assert_ptr_eq(oauth2_log_sink_ctx_get(sink), dummy);
75+
76+
check_log_test_sink_callback_dummy = 0;
77+
oauth2_info(log, "");
78+
ck_assert_int_eq(check_log_test_sink_callback_dummy, 1);
79+
}
80+
END_TEST
81+
5482
Suite *oauth2_check_log_suite()
5583
{
5684
Suite *s = suite_create("log");
@@ -59,6 +87,7 @@ Suite *oauth2_check_log_suite()
5987
tcase_add_checked_fixture(c, setup, teardown);
6088

6189
tcase_add_test(c, test_log);
90+
tcase_add_test(c, test_sink);
6291

6392
suite_add_tcase(s, c);
6493

0 commit comments

Comments
 (0)