Skip to content

Commit 09e237a

Browse files
handling of datatypes and optional fields improved
1 parent 7c0321c commit 09e237a

File tree

4 files changed

+80
-76
lines changed

4 files changed

+80
-76
lines changed

src/functions.h

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,42 @@
33
* value to the calling entity.
44
*/
55

6-
static const char *extract_remote_host(request_rec *r, char *a)
6+
static const json_object *extract_remote_host(request_rec *r, char *a)
77
{
8-
return (char *) ap_get_remote_host(r->connection, r->per_dir_config, REMOTE_NAME, NULL);
8+
return json_object_new_string(ap_get_remote_host(r->connection, r->per_dir_config, REMOTE_NAME, NULL));
99
}
1010

11-
static const char *extract_remote_address(request_rec *r, char *a) __attribute__((unused));
11+
static const json_object *extract_remote_address(request_rec *r, char *a) __attribute__((unused));
1212

13-
static const char *extract_remote_address(request_rec *r, char *a)
13+
static const json_object *extract_remote_address(request_rec *r, char *a)
1414
{
1515
#ifdef WITH_APACHE22
16-
return r->connection->remote_ip;
16+
return json_object_new_string(r->connection->remote_ip);
1717
#else
18-
return r->connection->client_ip;
18+
return json_object_new_string(r->connection->client_ip);
1919
#endif
2020
}
2121

22-
static const char *extract_local_address(request_rec *r, char *a) __attribute__((unused));
22+
static const json_object *extract_local_address(request_rec *r, char *a) __attribute__((unused));
2323

24-
static const char *extract_local_address(request_rec *r, char *a)
24+
static const json_object *extract_local_address(request_rec *r, char *a)
2525
{
26-
return r->connection->local_ip;
26+
return json_object_new_string(r->connection->local_ip);
2727
}
2828

29-
static const char *extract_remote_logname(request_rec *r, char *a)
29+
static const json_object *extract_remote_logname(request_rec *r, char *a)
3030
{
3131
const char *rlogin = ap_get_remote_logname(r);
3232
if (rlogin == NULL) {
33-
rlogin = "-";
33+
return NULL;
3434
} else if (strlen(rlogin) == 0) {
3535
rlogin = "\"\"";
3636
}
3737

38-
return rlogin;
38+
return json_object_new_string(rlogin);
3939
}
4040

41-
static const char *extract_remote_user(request_rec *r, char *a)
41+
static const json_object *extract_remote_user(request_rec *r, char *a)
4242
{
4343
#ifdef WITH_APACHE13
4444
char *rvalue = r->connection->user;
@@ -50,76 +50,77 @@ static const char *extract_remote_user(request_rec *r, char *a)
5050
} else if (strlen(rvalue) == 0) {
5151
rvalue = "\"\"";
5252
}
53-
return rvalue;
53+
return json_object_new_string(rvalue);
5454
}
5555

56-
static const char *extract_request_line(request_rec *r, char *a)
56+
static const json_object *extract_request_line(request_rec *r, char *a)
5757
{
5858
/* Upddated to mod_log_config logic */
5959
/* NOTE: If the original request contained a password, we
6060
* re-write the request line here to contain XXXXXX instead:
6161
* (note the truncation before the protocol string for HTTP/0.9 requests)
6262
* (note also that r->the_request contains the unmodified request)
6363
*/
64-
return (r->parsed_uri.password)
64+
return json_object_new_string(
65+
(r->parsed_uri.password)
6566
? apr_pstrcat(r->pool, r->method, " ",
6667
apr_uri_unparse(r->pool,
6768
&r->parsed_uri, 0),
6869
r->assbackwards ? NULL : " ",
6970
r->protocol, NULL)
70-
: r->the_request;
71+
: r->the_request);
7172
}
7273

73-
static const char *extract_request_file(request_rec *r, char *a)
74+
static const json_object *extract_request_file(request_rec *r, char *a)
7475
{
75-
return r->filename;
76+
return json_object_new_string(r->filename);
7677
}
7778

78-
static const char *extract_request_uri(request_rec *r, char *a)
79+
static const json_object *extract_request_uri(request_rec *r, char *a)
7980
{
80-
return r->uri;
81+
return json_object_new_string(r->uri);
8182
}
8283

83-
static const char *extract_request_method(request_rec *r, char *a)
84+
static const json_object *extract_request_method(request_rec *r, char *a)
8485
{
85-
return r->method;
86+
return json_object_new_string(r->method);
8687
}
8788

88-
static const char *extract_request_protocol(request_rec *r, char *a)
89+
static const json_object *extract_request_protocol(request_rec *r, char *a)
8990
{
90-
return r->protocol;
91+
return json_object_new_string(r->protocol);
9192
}
9293

93-
static const char *extract_request_query(request_rec *r, char *a)
94+
static const json_object *extract_request_query(request_rec *r, char *a)
9495
{
95-
return (r->args) ? apr_pstrcat(r->pool, "?",
96+
return json_object_new_string((r->args) ? apr_pstrcat(r->pool, "?",
9697
r->args, NULL)
97-
: "";
98+
: "");
9899
}
99100

100-
static const char *extract_status(request_rec *r, char *a)
101+
static const json_object *extract_status(request_rec *r, char *a)
101102
{
102103
if (r->status <= 0) {
103-
return "-";
104+
return NULL;
104105
} else {
105-
return apr_psprintf(r->pool, "%d", r->status);
106+
return json_object_new_int(r->status);
106107
}
107108
}
108109

109-
static const char *extract_virtual_host(request_rec *r, char *a)
110+
static const json_object *extract_virtual_host(request_rec *r, char *a)
110111
{
111-
return r->server->server_hostname;
112+
return json_object_new_string(r->server->server_hostname);
112113
}
113114

114-
static const char *extract_server_name(request_rec *r, char *a)
115+
static const json_object *extract_server_name(request_rec *r, char *a)
115116
{
116-
return ap_get_server_name(r);
117+
return json_object_new_string(ap_get_server_name(r));
117118
}
118119

119-
static const char *extract_server_port(request_rec *r, char *a)
120+
static const json_object *extract_server_port(request_rec *r, char *a)
120121
{
121-
return apr_psprintf(r->pool, "%u",
122-
r->server->port ? r->server->port : ap_default_port(r));
122+
return json_object_new_string(apr_psprintf(r->pool, "%u",
123+
r->server->port ? r->server->port : ap_default_port(r)));
123124
}
124125

125126
/* This respects the setting of UseCanonicalName so that
@@ -131,63 +132,63 @@ static const char *log_server_name(request_rec *r, char *a)
131132
return ap_get_server_name(r);
132133
}
133134

134-
static const char *extract_child_pid(request_rec *r, char *a)
135+
static const json_object *extract_child_pid(request_rec *r, char *a)
135136
{
136137
if (*a == '\0' || !strcmp(a, "pid")) {
137-
return apr_psprintf(r->pool, "%" APR_PID_T_FMT, getpid());
138+
return json_object_new_string(apr_psprintf(r->pool, "%" APR_PID_T_FMT, getpid()));
138139
}
139140
else if (!strcmp(a, "tid")) {
140141
#if APR_HAS_THREADS
141142
apr_os_thread_t tid = apr_os_thread_current();
142143
#else
143144
int tid = 0; /* APR will format "0" anyway but an arg is needed */
144145
#endif
145-
return apr_psprintf(r->pool, "%pT", &tid);
146+
return json_object_new_string(apr_psprintf(r->pool, "%pT", &tid));
146147
}
147148
/* bogus format */
148-
return a;
149+
return json_object_new_string(a);
149150
}
150151

151-
static const char *extract_header(request_rec *r, char *a)
152+
static const json_object *extract_header(request_rec *r, char *a)
152153
{
153154
const char *tempref;
154155

155156
tempref = apr_table_get(r->headers_in, a);
156157
if (!tempref)
157158
{
158-
return "-";
159+
return NULL;
159160
} else {
160-
return tempref;
161+
return json_object_new_string(tempref);
161162
}
162163
}
163164

164-
static const char *extract_referer(request_rec *r, char *a)
165+
static const json_object *extract_referer(request_rec *r, char *a)
165166
{
166167
const char *tempref;
167168

168169
tempref = apr_table_get(r->headers_in, "Referer");
169170
if (!tempref)
170171
{
171-
return "-";
172+
return NULL;
172173
} else {
173-
return tempref;
174+
return json_object_new_string(tempref);
174175
}
175176
}
176177

177-
static const char *extract_agent(request_rec *r, char *a)
178+
static const json_object *extract_agent(request_rec *r, char *a)
178179
{
179180
const char *tempag;
180181

181182
tempag = apr_table_get(r->headers_in, "User-Agent");
182183
if (!tempag)
183184
{
184-
return "-";
185+
return NULL;
185186
} else {
186-
return tempag;
187+
return json_object_new_string(tempag);
187188
}
188189
}
189190

190-
static const char *extract_specific_cookie(request_rec *r, char *a)
191+
static const json_object *extract_specific_cookie(request_rec *r, char *a)
191192
{
192193
const char *cookiestr;
193194
char *cookieend;
@@ -215,7 +216,7 @@ static const char *extract_specific_cookie(request_rec *r, char *a)
215216
cookieend = ap_strchr(cookiebuf, ';');
216217
if (cookieend != NULL)
217218
*cookieend = '\0';
218-
return cookiebuf;
219+
return json_object_new_string(cookiebuf);
219220
}
220221
}
221222

@@ -230,7 +231,7 @@ static const char *extract_specific_cookie(request_rec *r, char *a)
230231
cookieend = ap_strchr(cookiebuf, ';');
231232
if (cookieend != NULL)
232233
*cookieend = '\0';
233-
return cookiebuf;
234+
return json_object_new_string(cookiebuf);
234235
}
235236
}
236237

@@ -245,10 +246,10 @@ static const char *extract_specific_cookie(request_rec *r, char *a)
245246
cookieend = ap_strchr(cookiebuf, ';');
246247
if (cookieend != NULL)
247248
*cookieend = '\0';
248-
return cookiebuf;
249+
return json_object_new_string(cookiebuf);
249250
}
250251
}
251252
}
252253

253-
return "-";
254+
return NULL;
254255
}

src/functions20.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
static const char *extract_bytes_sent(request_rec *r, char *a)
1+
static const json_object *extract_bytes_sent(request_rec *r, char *a)
22
{
33
if (!r->sent_bodyct || !r->bytes_sent) {
4-
return "-";
4+
return NULL;
55
} else {
6-
return apr_psprintf(r->pool, "%" APR_OFF_T_FMT, r->bytes_sent);
6+
return json_object_new_int(r->bytes_sent);
77
}
88
}
99

10-
static const char *extract_request_time_custom(request_rec *r, char *a,
10+
static const json_object *extract_request_time_custom(request_rec *r, char *a,
1111
apr_time_exp_t *xt)
1212
{
1313
apr_size_t retcode;
1414
char tstr[MAX_STRING_LEN];
1515
apr_strftime(tstr, &retcode, sizeof(tstr), a, xt);
16-
return apr_pstrdup(r->pool, tstr);
16+
return json_object_new_string(apr_pstrdup(r->pool, tstr));
1717
}
1818

1919
#define DEFAULT_REQUEST_TIME_SIZE 32
@@ -27,7 +27,7 @@ typedef struct {
2727
#define TIME_CACHE_MASK 3
2828
static cached_request_time request_time_cache[TIME_CACHE_SIZE];
2929

30-
static const char *extract_request_time(request_rec *r, char *a)
30+
static const json_object *extract_request_time(request_rec *r, char *a)
3131
{
3232
apr_time_exp_t xt;
3333

@@ -78,26 +78,26 @@ static const char *extract_request_time(request_rec *r, char *a)
7878
memcpy(&(request_time_cache[i]), cached_time,
7979
sizeof(*cached_time));
8080
}
81-
return cached_time->timestr;
81+
return json_object_new_string(cached_time->timestr);
8282
}
8383
}
8484

85-
static const char *extract_request_duration(request_rec *r, char *a)
85+
static const json_object *extract_request_duration(request_rec *r, char *a)
8686
{
8787
apr_time_t duration = apr_time_now() - r->request_time;
88-
return apr_psprintf(r->pool, "%ld", apr_time_usec(duration));
88+
return json_object_new_int(apr_time_usec(duration));
8989
}
9090

91-
static const char *extract_connection_status(request_rec *r, char *a) __attribute__((unused));
92-
static const char *extract_connection_status(request_rec *r, char *a)
91+
static const json_object *extract_connection_status(request_rec *r, char *a) __attribute__((unused));
92+
static const json_object *extract_connection_status(request_rec *r, char *a)
9393
{
9494
if (r->connection->aborted)
95-
return "X";
95+
return json_object_new_string("X");
9696

9797
if (r->connection->keepalive == AP_CONN_KEEPALIVE &&
9898
(!r->server->keep_alive_max ||
9999
(r->server->keep_alive_max - r->connection->keepalives) > 0)) {
100-
return "+";
100+
return json_object_new_string("+");
101101
}
102-
return "-";
102+
return NULL;
103103
}

src/mod_log_gelf.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ char * log_gelf_make_json(request_rec *request) {
457457
/* attach field pairs to json root */
458458
json_add_string(object, "version", "1.1");
459459
json_add_string(object, "host", config->source);
460-
json_add_string(object, "short_message", extract_request_line(request, NULL));
460+
// json_add_string(object, "short_message", extract_request_line(request, NULL));
461+
json_object_object_add(object, "short_message", extract_request_line(request, NULL));
461462
json_add_string(object, "facility", config->facility);
462463
json_add_int(object, "level", 6); /*0=Emerg, 1=Alert, 2=Crit, 3=Error, 4=Warn, 5=Notice, 6=Info */
463464
json_add_double(object, "timestamp", log_gelf_get_timestamp());
@@ -466,11 +467,13 @@ char * log_gelf_make_json(request_rec *request) {
466467
length = strlen(config->fields);
467468
for (i = 0; i<length; i++) {
468469
log_item *item = config->parsed_fields[i];
470+
json_object* fval;
469471
if (item != NULL) {
470-
if (item->arg)
471-
json_add_string(object, item->field_name, item->func(request, (char*)item->arg));
472-
else
473-
json_add_string(object, item->field_name, item->func(request, ""));
472+
fval = item->func(request, (char*)item->arg);
473+
if (fval != NULL) {
474+
json_object_object_add(object, item->field_name, fval);
475+
}
476+
474477
}
475478
}
476479

src/mod_log_gelf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
typedef const char *item_func(request_rec *r, char *a);
1+
typedef const json_object* item_func(request_rec *r, char *a);
22
typedef struct transferDataS {
33
void * data;
44
int size;

0 commit comments

Comments
 (0)