/
mod_mediarss.c
179 lines (145 loc) · 5.51 KB
/
mod_mediarss.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
/*
* (C) Copyright 2008, Stefan Arentz, Arentz Consulting.
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
#include <apr_strings.h>
#include <httpd.h>
#include <http_core.h>
#include <http_log.h>
#include <http_protocol.h>
#include <http_request.h>
#include <http_config.h>
#include <util_md5.h>
static int mediarss_is_media_content(char const* content_type)
{
static char* types[3] = { "image/", "video/", "audio/" };
if (content_type != NULL) {
int i;
for (i = 0; i < 3; ++i) {
if (strncmp(content_type, types[i], strlen(types[i])) == 0) {
return 1;
}
}
}
return 0;
}
static int mediarss_index_directory(request_rec* r)
{
apr_status_t status;
apr_dir_t* dir;
apr_finfo_t dirent;
if ((status = apr_dir_open(&dir, r->filename, r->pool)) != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, "Can't open directory for index: %s", r->filename);
return HTTP_FORBIDDEN;
}
/* Content header */
char* url;
url = ap_construct_url(r->pool, r->uri, r);
ap_set_content_type(r, "text/xml; charset=utf-8");
ap_rputs("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n", r);
if (strcmp(r->args, "format=mediarss") == 0) {
ap_rputs("<rss version=\"2.0\" xmlns:media=\"http://search.yahoo.com/mrss/\">\n", r);
} else {
ap_rputs("<rss version=\"2.0\">\n", r);
}
ap_rputs(" <channel>\n", r);
ap_rvputs(r, " <title>Index of ", url, "</title>\n", NULL);
ap_rvputs(r, " <link>", url, "</link>\n", NULL);
/* Collect information about the files in the directory */
while (1)
{
status = apr_dir_read(&dirent, APR_FINFO_MIN | APR_FINFO_NAME, dir);
if (APR_STATUS_IS_INCOMPLETE(status)) {
continue; /* ignore un-stat()able files */
} else if (status != APR_SUCCESS) {
break;
}
/* We are only interested in regular files. TODO Deal with symlinks. */
if (dirent.filetype == APR_REG)
{
request_rec* rr;
rr = ap_sub_req_lookup_dirent(&dirent, r, AP_SUBREQ_NO_ARGS, NULL);
if (rr != NULL)
{
if (rr->finfo.filetype == APR_REG && rr->status == HTTP_OK)
{
/* In case of media rss, only include the item if it is a media type */
if (strcmp(r->args, "format=mediarss") == 0 && mediarss_is_media_content(rr->content_type) == 0) {
continue;
}
char size[16];
snprintf(size, sizeof(size), "%d", dirent.size);
char date[APR_RFC822_DATE_LEN];
apr_rfc822_date(date, dirent.mtime);
char* guid = ap_md5(r->pool, (unsigned char*) apr_pstrcat(r->pool, url, dirent.name, NULL));
ap_rputs(" <item>\n", r);
ap_rvputs(r, " <guid>", guid, "</guid>\n", NULL);
ap_rvputs(r, " <title>", dirent.name, "</title>\n", NULL);
ap_rvputs(r, " <pubDate>", date, "</pubDate>\n", NULL);
ap_rvputs(r, " <enclosure url=\"", url, dirent.name, "\" length=\"", size, "\"\n", NULL);
ap_rvputs(r, " type=\"", rr->content_type, "\"/>\n", NULL);
if (strcmp(r->args, "format=mediarss") == 0) {
ap_rvputs(r, " <media:content url=\"", url, dirent.name, "\" fileSize=\"", size, "\"\n", NULL);
ap_rvputs(r, " type=\"", rr->content_type, "\"/>\n", NULL);
}
ap_rputs(" </item>\n", r);
}
ap_destroy_sub_req(rr);
}
}
}
/* Content footer */
ap_rputs(" </channel>\n", r);
ap_rputs("</rss>\n", r);
apr_dir_close(dir);
return OK;
}
static int mediarss_handler(request_rec* r)
{
/* This needs to be a request for a directory */
if (strcmp(r->handler, DIR_MAGIC_TYPE) != 0) {
return DECLINED;
}
/* We require the format=mediarss or format=rss query parameter */
if (r->args == NULL || (strcmp(r->args, "format=mediarss") != 0 && strcmp(r->args, "format=rss") != 0)) {
return DECLINED;
}
int allow_opts = ap_allow_options(r);
r->allowed |= (AP_METHOD_BIT << M_GET);
if (r->method_number != M_GET) {
return DECLINED;
}
if (allow_opts & OPT_INDEXES) {
return mediarss_index_directory(r);
} else {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "Directory index forbidden by rule: %s", r->filename);
return HTTP_FORBIDDEN;
}
}
static void mediarss_hooks(apr_pool_t* pool)
{
ap_hook_handler(mediarss_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
module AP_MODULE_DECLARE_DATA mediarss_module = {
STANDARD20_MODULE_STUFF,
NULL,
NULL,
NULL,
NULL,
NULL,
mediarss_hooks
};