This repository has been archived by the owner on Mar 23, 2019. It is now read-only.
/
mod_google_analytics.c
291 lines (237 loc) · 10.1 KB
/
mod_google_analytics.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
$ perldoc mod_google_analytics.c
=encoding utf8
=head1 NAME
mod_google_analytics.c -- Apache mod_google_analytics module
=head1 SYNOPSIS
LoadModule google_analytics_module modules/mod_google_analytics.so
AddOutputFilterByType GOOGLE_ANALYTICS text/html
GoogleAnalyticsAccountNumber UA-1234567-8
=head1 AUTHOR
Ryuzo Yamamoto (dragon3)
=head1 LICENSE
Copyright 2008 Ryuzo Yamamoto
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=cut
*/
#include "httpd.h"
#include "http_config.h"
#include "util_filter.h"
#include "ap_config.h"
#include "ap_regex.h"
#include "apr_strmatch.h"
#include "apr_strings.h"
#define VERSION "0.2"
static const char *google_analytics_filter_name = "GOOGLE_ANALYTICS";
static const char *body_end_tag = "</body>";
static const unsigned int body_end_tag_length = 7;
static const char *replace_base = "<script type=\"text/javascript\"><!-- \n var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\");document.write(unescape(\"%%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%%3E%%3C/script%%3E\"));\n//--></script><script type=\"text/javascript\"><!-- \n try {var pageTracker = _gat._getTracker(\"%s\");pageTracker._trackPageview();} catch(err) {}; \n//--></script></body>";
static const char *tag_exists = "google-analytics\\.com/(ga|urchin)\\.js";
static const ap_regex_t *regex_tag_exists;
static const apr_strmatch_pattern *pattern_body_end_tag;
module AP_MODULE_DECLARE_DATA google_analytics_module;
typedef struct {
char *account_number;
char *replace;
} google_analytics_filter_config;
typedef struct {
apr_bucket_brigade *bbsave;
} google_analytics_filter_ctx;
static apr_status_t google_analytics_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
{
request_rec *r = f->r;
google_analytics_filter_ctx *ctx = f->ctx;
google_analytics_filter_config *c;
apr_bucket *b = APR_BRIGADE_FIRST(bb);
apr_size_t bytes;
apr_size_t fbytes;
apr_size_t offs;
const char *buf;
const char *le = NULL;
const char *le_n;
const char *le_r;
const char *bufp;
const char *subs;
unsigned int match;
apr_bucket *b1;
char *fbuf;
int found = 0;
apr_status_t rv;
apr_bucket_brigade *bbline;
// サブリクエストならなにもしない
if (r->main) {
ap_remove_output_filter(f);
return ap_pass_brigade(f->next, bb);
}
c = ap_get_module_config(r->per_dir_config, &google_analytics_module);
if (ctx == NULL) {
ctx = f->ctx = apr_pcalloc(r->pool, sizeof(google_analytics_filter_ctx));
ctx->bbsave = apr_brigade_create(r->pool, f->c->bucket_alloc);
}
// length かわってしまうので unset で OK?
apr_table_unset(r->headers_out, "Content-Length");
apr_table_unset(r->headers_out, "Content-MD5");
apr_table_unset(r->headers_out, "Accept-Ranges");
apr_table_unset(r->headers_out, "ETag");
bbline = apr_brigade_create(r->pool, f->c->bucket_alloc);
// 改行毎なbucketに編成しなおす
while ( b != APR_BRIGADE_SENTINEL(bb) ) {
if ( !APR_BUCKET_IS_METADATA(b) ) {
if ( apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ) == APR_SUCCESS ) {
if ( bytes == 0 ) {
APR_BUCKET_REMOVE(b);
} else {
while ( bytes > 0 ) {
le_n = memchr(buf, '\n', bytes);
le_r = memchr(buf, '\r', bytes);
if ( le_n != NULL ) {
if ( le_n == le_r + sizeof(char)) {
le = le_n;
}
else if ( (le_r < le_n) && (le_r != NULL) ) {
le = le_r;
}
else {
le = le_n;
}
}
else {
le = le_r;
}
if ( le ) {
offs = 1 + ((unsigned int)le-(unsigned int)buf) / sizeof(char);
apr_bucket_split(b, offs);
bytes -= offs;
buf += offs;
b1 = APR_BUCKET_NEXT(b);
APR_BUCKET_REMOVE(b);
if ( !APR_BRIGADE_EMPTY(ctx->bbsave) ) {
APR_BRIGADE_INSERT_TAIL(ctx->bbsave, b);
rv = apr_brigade_pflatten(ctx->bbsave, &fbuf, &fbytes, r->pool);
b = apr_bucket_pool_create(fbuf, fbytes, r->pool,
r->connection->bucket_alloc);
apr_brigade_cleanup(ctx->bbsave);
}
APR_BRIGADE_INSERT_TAIL(bbline, b);
b = b1;
} else {
APR_BUCKET_REMOVE(b);
APR_BRIGADE_INSERT_TAIL(ctx->bbsave, b);
bytes = 0;
}
} /* while bytes > 0 */
}
} else {
APR_BUCKET_REMOVE(b);
}
} else if ( APR_BUCKET_IS_EOS(b) ) {
if ( !APR_BRIGADE_EMPTY(ctx->bbsave) ) {
rv = apr_brigade_pflatten(ctx->bbsave, &fbuf, &fbytes, r->pool);
b1 = apr_bucket_pool_create(fbuf, fbytes, r->pool,
r->connection->bucket_alloc);
APR_BRIGADE_INSERT_TAIL(bbline, b1);
}
apr_brigade_cleanup(ctx->bbsave);
f->ctx = NULL;
APR_BUCKET_REMOVE(b);
APR_BRIGADE_INSERT_TAIL(bbline, b);
} else {
apr_bucket_delete(b);
}
b = APR_BRIGADE_FIRST(bb);
}
// 改行毎なbucketをまわす
for ( b = APR_BRIGADE_FIRST(bbline);
b != APR_BRIGADE_SENTINEL(bbline);
b = APR_BUCKET_NEXT(b) ) {
if ( !APR_BUCKET_IS_METADATA(b)
&& (apr_bucket_read(b, &buf, &bytes, APR_BLOCK_READ) == APR_SUCCESS)) {
bufp = buf;
if (ap_regexec(regex_tag_exists, bufp, 0, NULL, 0) == 0) {
break;
}
subs = apr_strmatch(pattern_body_end_tag, bufp, bytes);
if (subs != NULL) {
match = ((unsigned int)subs - (unsigned int)bufp) / sizeof(char);
bytes -= match;
bufp += match;
apr_bucket_split(b, match);
b1 = APR_BUCKET_NEXT(b);
apr_bucket_split(b1, body_end_tag_length);
b = APR_BUCKET_NEXT(b1);
apr_bucket_delete(b1);
bytes -= body_end_tag_length;
bufp += body_end_tag_length;
b1 = apr_bucket_immortal_create(c->replace, strlen(c->replace),
r->connection->bucket_alloc);
APR_BUCKET_INSERT_BEFORE(b, b1);
}
}
}
rv = ap_pass_brigade(f->next, bbline);
for ( b = APR_BRIGADE_FIRST(ctx->bbsave);
b != APR_BRIGADE_SENTINEL(ctx->bbsave);
b = APR_BUCKET_NEXT(b)) {
apr_bucket_setaside(b, r->pool);
}
return rv;
}
static void * create_dir_config(apr_pool_t *p, char *dir)
{
google_analytics_filter_config *c = apr_pcalloc(p, sizeof(google_analytics_filter_config));
return (void *)c;
}
static void * merge_dir_config(apr_pool_t *p, void *basev, void *overridesv)
{
google_analytics_filter_config *c = apr_palloc(p, sizeof(google_analytics_filter_config));
google_analytics_filter_config *base = (google_analytics_filter_config *)basev;
google_analytics_filter_config *overrides = (google_analytics_filter_config *)overridesv;
c->account_number = overrides->account_number ? overrides->account_number : base->account_number;
c->replace = overrides->replace ? overrides->replace : base->replace;
return c;
}
static const char * set_account_number(cmd_parms *cmd, void *mconfig, const char *arg)
{
google_analytics_filter_config *c = (google_analytics_filter_config *)mconfig;
// TODO check arg
// return "specify your account number.";
c->account_number = apr_pstrdup(cmd->pool, arg);
c->replace = apr_psprintf(cmd->pool, replace_base, c->account_number);
return NULL;
}
static int google_analytics_post_config(apr_pool_t *p, apr_pool_t *plog,
apr_pool_t *ptemp, server_rec *s)
{
regex_tag_exists = ap_pregcomp(p, tag_exists, (AP_REG_EXTENDED | AP_REG_ICASE | AP_REG_NOSUB));
ap_assert(regex_tag_exists != NULL);
pattern_body_end_tag = apr_strmatch_precompile(p, body_end_tag, 0);
return OK;
}
static const command_rec cmds[] = {
AP_INIT_TAKE1("GoogleAnalyticsAccountNumber", set_account_number, NULL, OR_FILEINFO,
"Set the Google Analytics account number."),
{NULL}
};
static void register_hooks(apr_pool_t *p)
{
ap_hook_post_config(google_analytics_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_register_output_filter(google_analytics_filter_name, google_analytics_out_filter, NULL,
AP_FTYPE_RESOURCE);
}
module AP_MODULE_DECLARE_DATA google_analytics_module = {
STANDARD20_MODULE_STUFF,
create_dir_config, /* create per-dir config structures */
merge_dir_config, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
cmds, /* table of config file commands */
register_hooks /* register hooks */
};