/
mod_wiki.c
488 lines (425 loc) · 12.9 KB
/
mod_wiki.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
** mod_wiki.c - Git based wiki system for Apache module
** Copyright (C) 2011 Tsukasa Hamano <code@cuspy.org>
**
** [Autogenerated via ``apxs -n wiki -g'']
**
** To play with this sample module first compile it into a
** DSO file and install it into Apache's modules directory
** by running:
**
** $ apxs -c -i mod_wiki.c
**
** Then activate it in Apache's httpd.conf file for instance
** for the URL /wiki in as follows:
**
** # httpd.conf
** LoadModule wiki_module modules/mod_wiki.so
** <Location />
** SetHandler wiki
** WikiRepository /path/to/repository.git
** WikiName "My Wiki"
** WikiCss /style.css
** </Location>
**
** Then after restarting Apache via
**
** $ apachectl restart
**
** you immediately can request the URL /wiki and watch for the
** output of this module. This can be achieved for instance via:
**
** $ lynx -mime_header http://localhost/
**
** The output should be similar to the following one:
**
** HTTP/1.1 200 OK
** Date: Tue, 31 Mar 1998 14:42:22 GMT
** Server: Apache/1.3.4 (Unix)
** Connection: close
** Content-Type: text/html
**
** The sample page from mod_wiki.c
*/
#include <sys/stat.h>
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_log.h"
#include "ap_config.h"
#include "apr_strings.h"
#include "git2.h"
#include "mkdio.h"
#include "ClearSilver.h"
#include "mod_wiki.h"
module AP_MODULE_DECLARE_DATA wiki_module;
static char* DEFAULT_TEMPLATE = "\
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
<!DOCTYPE html PUBLIC \n\
\"-//W3C//DTD XHTML 1.0 Strict//EN\"\n\
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n\
<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\
<head>\n\
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n\
<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n\
<?cs each:item = css ?>\
<link rel=\"stylesheet\" href=\"<?cs var:item ?>\" type=\"text/css\" />\n\
<?cs /each ?>\
<title><?cs var:title ?> - <?cs var:wikiname ?></title>\n\
</head>\n\
<body>\n\
<h1 class=\"title\"><?cs var:title ?></h1>\n\
<div class=\"subtitle\"> From <?cs var:wikiname ?></div>\n\
<?cs var:document ?>\n\
<body>\n\
";
static NEOERR *cs_output(void *ctx, char *s)
{
ap_rputs(s, (request_rec *)ctx);
return STATUS_OK;
}
static void wiki_output(MMIOT *doc, request_rec *r)
{
char *title;
int ret;
int size;
char *p;
wiki_conf *conf;
list_t *css;
HDF *hdf;
CSPARSE *cs;
int i;
conf =
(wiki_conf *) ap_get_module_config(r->per_dir_config,
&wiki_module);
ret = mkd_compile(doc, MKD_TOC | MKD_AUTOLINK);
hdf_init(&hdf);
if(conf->name){
hdf_set_value(hdf, "wikiname", conf->name);
}
title = mkd_doc_title(doc);
if(title == NULL){
title = "notitle";
}
hdf_set_value(hdf, "title", title);
for(i=0, css = conf->css; css; i++, css = (list_t *) css->next){
hdf_set_valuef(hdf, "css.%d=%s", i, (char *)css->data);
}
if ((size = mkd_document(doc, &p)) != EOF) {
hdf_set_value(hdf, "document", p);
}
cs_init(&cs, hdf);
cs_parse_string(cs, strdup(DEFAULT_TEMPLATE), strlen(DEFAULT_TEMPLATE));
cs_render(cs, r, cs_output);
hdf_destroy(&hdf);
cs_destroy(&cs);
}
static void log_oid(request_rec *r,
const char *name,
const git_oid *oid)
{
char hash[41];
hash[40] = '\0';
git_oid_fmt(hash, oid);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "%s: %s", name, hash);
}
const git_oid *lookup_master_tree(git_repository *repo)
{
int ret;
git_reference *ref;
const git_oid *commit_oid;
const git_oid *tree_oid;
git_commit *commit;
ret = git_reference_lookup(&ref, repo, "refs/heads/master");
if (ret) {
printf("git_reference_lookup() error: %s\n", git_strerror(ret));
return NULL;
}
if (git_reference_type(ref) != GIT_REF_OID) {
perror("reference_type is not oid reference.");
return NULL;
}
commit_oid = git_reference_oid(ref);
ret = git_commit_lookup(&commit, repo, commit_oid);
tree_oid = git_commit_tree_oid(commit);
git_commit_free(commit);
return tree_oid;
}
static int lookup_object_by_name(request_rec *r,
git_repository *repo,
const git_oid *tree_oid,
const char *name,
const git_oid **oid)
{
const git_tree_entry *entry;
int ret;
git_tree *tree;
char *extname;
int type = WIKI_NOTFOUND;
ret = git_tree_lookup(&tree, repo, tree_oid);
extname = apr_pstrcat(r->pool, name, ".md", NULL);
entry = git_tree_entry_byname(tree, extname);
//printf("extname: %s\n", extname);
if (entry) {
type = WIKI_MARKDOWN;
}else{
entry = git_tree_entry_byname(tree, name);
if (entry) {
int attr = git_tree_entry_attributes(entry);
if(S_ISDIR(attr)){
type = WIKI_DIR;
}else{
type = WIKI_FOUND;
}
}
}
git_tree_free(tree);
if (entry == NULL) {
return WIKI_NOTFOUND;
}
*oid = git_tree_entry_id(entry); // is this safe?
return type;
}
static int lookup_object_oid(request_rec *r,
git_repository *repo,
const git_oid *master,
const char *path,
const git_oid **oid)
{
const git_oid *object_oid = NULL;
int type;
char *saveptr = NULL;
char *part = NULL;
char *token = apr_pstrdup(r->pool, path);
const git_oid *tree_oid;
tree_oid = master;
while((part = apr_strtok(token, "/", &saveptr)) != NULL){
/*
if (*part == '\0') {
printf("continue\n");
continue;
}
*/
//printf("part=%s\n", part);
type = lookup_object_by_name(r, repo, tree_oid, part, &object_oid);
if(type == WIKI_NOTFOUND) {
return type;
}else if (type == WIKI_DIR) {
tree_oid = object_oid;
}
//log_oid(r, "oid", object_oid);
token = NULL;
}
*oid = object_oid;
return type;
}
char *ltrim(const char *base, const char *path)
{
int i = 0;
size_t base_len = strlen(base);
//size_t path_len = strlen(path);
for (i = 0; i < base_len; i++) {
if (base[i] != path[i]) {
continue;
}
}
while (path[i] && path[i] == '/') {
i++;
}
return (char *)(path + i);
}
/* The wiki handler */
static int wiki_handler(request_rec *r)
{
wiki_conf *conf;
git_repository *repo;
const git_oid *tree_oid;
char *path;
const git_oid *oid;
char hash[41];
hash[40] = '\0';
int ret;
int type;
MMIOT *doc;
if (strcmp(r->handler, "wiki")) {
return DECLINED;
}
if (r->header_only) {
return OK;
}
conf =
(wiki_conf *) ap_get_module_config(r->per_dir_config,
&wiki_module);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "filename=%s",
r->filename);
if (conf->basepath) {
path = ltrim(conf->basepath, r->parsed_uri.path);
} else {
path = r->parsed_uri.path + 1;
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "path=%s", path);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"r->content_type=%s", r->content_type);
if (conf->repo == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"WikiRepository is not set.");
return HTTP_INTERNAL_SERVER_ERROR;
}
ret = git_repository_open(&repo, conf->repo);
if (ret) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"Cannot open git repository.(%s)", conf->repo);
return HTTP_INTERNAL_SERVER_ERROR;
}
tree_oid = lookup_master_tree(repo);
if (tree_oid == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"Not found master tree.");
git_repository_free(repo);
}
type = lookup_object_oid(r, repo, tree_oid, path, &oid);
if (type == WIKI_NOTFOUND) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r,
"Object is not found.");
git_repository_free(repo);
return HTTP_NOT_FOUND;
} else if(type == WIKI_DIR) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Directory listing is not implement yet.");
git_repository_free(repo);
return HTTP_FORBIDDEN;
} else if(type == WIKI_ERROR) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Error at lookup.");
git_repository_free(repo);
return HTTP_NOT_FOUND;
}
git_oid_fmt(hash, oid);
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "object hash=%s", hash);
const char *data;
size_t size;
git_odb *odb;
ret = git_repository_odb(&odb, repo);
git_odb_object *odb_object;
ret = git_odb_read(&odb_object, odb, oid);
data = git_odb_object_data(odb_object);
size = git_odb_object_size(odb_object);
git_odb_object_free(odb_object); // is this safe?
if (type == WIKI_FOUND) {
ap_rwrite(data, size, r);
} else if(type == WIKI_MARKDOWN) {
r->content_type = "text/html";
doc = mkd_string(data, size, 0);
if (doc == NULL) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"mkd_string() returned NULL\n");
git_repository_free(repo);
return HTTP_INTERNAL_SERVER_ERROR;
}
wiki_output(doc, r);
mkd_cleanup(doc);
} else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
"Unknown Error.\n");
git_repository_free(repo);
return HTTP_INTERNAL_SERVER_ERROR;
}
git_repository_free(repo);
return OK;
}
static void *wiki_config(apr_pool_t * p, char *dummy)
{
wiki_conf *c = (wiki_conf *) apr_pcalloc(p, sizeof(wiki_conf));
memset(c, 0, sizeof(wiki_conf));
return (void *)c;
}
static const char *set_wiki_repo(cmd_parms * cmd, void *conf,
const char *arg)
{
wiki_conf *c = (wiki_conf *) conf;
c->repo = arg;
return NULL;
}
static const char *set_wiki_name(cmd_parms * cmd, void *conf,
const char *arg)
{
wiki_conf *c = (wiki_conf *) conf;
c->name = arg;
return NULL;
}
static const char *set_wiki_basepath(cmd_parms * cmd, void *conf,
const char *arg)
{
wiki_conf *c = (wiki_conf *) conf;
c->basepath = arg;
return NULL;
}
static const char *set_wiki_css(cmd_parms * cmd, void *conf,
const char *arg)
{
wiki_conf *c = (wiki_conf *) conf;
list_t *item = (list_t *) malloc(sizeof(list_t));
item->data = arg;
item->next = NULL;
list_t *tail;
if (c->css) {
tail = c->css;
while (tail->next)
tail = (list_t *) tail->next;
tail->next = (struct list_t *)item;
} else {
c->css = item;
}
return NULL;
}
static const char *set_wiki_header(cmd_parms * cmd, void *conf,
const char *arg)
{
wiki_conf *c = (wiki_conf *) conf;
c->header = arg;
return NULL;
}
static const char *set_wiki_footer(cmd_parms * cmd, void *conf,
const char *arg)
{
wiki_conf *c = (wiki_conf *) conf;
c->footer = arg;
return NULL;
}
static const command_rec wiki_cmds[] = {
AP_INIT_TAKE1("WikiRepository", set_wiki_repo, NULL, OR_ALL,
"set Repository"),
AP_INIT_TAKE1("WikiName", set_wiki_name, NULL, OR_ALL,
"set WikiName"),
AP_INIT_TAKE1("WikiBasePath", set_wiki_basepath, NULL, OR_ALL,
"set Base Path"),
AP_INIT_TAKE1("WikiCSS", set_wiki_css, NULL, OR_ALL,
"set CSS"),
AP_INIT_TAKE1("WikiHeaderHtml", set_wiki_header, NULL, OR_ALL,
"set Header HTML"),
AP_INIT_TAKE1("WikiFooterHtml", set_wiki_footer, NULL, OR_ALL,
"set Footer HTML"),
{NULL}
};
static void wiki_register_hooks(apr_pool_t * p)
{
ap_hook_handler(wiki_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Dispatch list for API hooks */
module AP_MODULE_DECLARE_DATA wiki_module = {
STANDARD20_MODULE_STUFF,
wiki_config, /* create per-dir config structures */
NULL, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
wiki_cmds, /* table of config file commands */
wiki_register_hooks /* register hooks */
};
/*
* Local Variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
* vim: sw=4 ts=4 sts=4 et
*/