-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmod_sass.c
More file actions
258 lines (221 loc) · 7.88 KB
/
mod_sass.c
File metadata and controls
258 lines (221 loc) · 7.88 KB
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
/*
** mod_sass.c -- Apache sass module
**
** Then activate it in Apache's httpd.conf file:
**
** # httpd.conf
** LoadModule sass_module modules/mod_sass.so
** <IfModule sass_module>
** AddHandler sass-script .scss
** SassCompressed Off (On | Off)
** SassOutput Off (On | Off)
** SassDisplayError Off (On | Off)
** SassIncludePaths path/to/sass
** </IfModule sass_module>
*/
/* httpd */
#include "httpd.h"
#include "http_config.h"
#include "http_protocol.h"
#include "http_main.h"
#include "http_log.h"
#include "ap_config.h"
#include "apr_strings.h"
/* libsass */
#include "sass.h"
#include "sass/interface.h"
#ifdef HAVE_CONFIG_H
# undef PACKAGE_NAME
# undef PACKAGE_STRING
# undef PACKAGE_TARNAME
# undef PACKAGE_VERSION
# include "config.h"
#endif
/* log */
#ifdef AP_SASS_DEBUG_LOG_LEVEL
#define SASS_DEBUG_LOG_LEVEL AP_SASS_DEBUG_LOG_LEVEL
#else
#define SASS_DEBUG_LOG_LEVEL APLOG_DEBUG
#endif
#define _RERR(r, format, args...) \
ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, \
r, "[SASS] %s(%d): "format, __FILE__, __LINE__, ##args)
#define _SERR(s, format, args...) \
ap_log_error(APLOG_MARK, APLOG_CRIT, 0, \
s, "[SASS] %s(%d): "format, __FILE__, __LINE__, ##args)
#define _PERR(p, format, args...) \
ap_log_perror(APLOG_MARK, APLOG_CRIT, 0, \
p, "[SASS] %s(%d): "format, __FILE__, __LINE__, ##args)
#define _RDEBUG(r, format, args...) \
ap_log_rerror(APLOG_MARK, SASS_DEBUG_LOG_LEVEL, 0, \
r, "[SASS_DEBUG] %s(%d): "format, __FILE__, __LINE__, ##args)
#define _SDEBUG(s, format, args...) \
ap_log_error(APLOG_MARK, SASS_DEBUG_LOG_LEVEL, 0, \
s, "[SASS_DEBUG] %s(%d): "format, __FILE__, __LINE__, ##args)
#define _PDEBUG(p, format, args...) \
ap_log_perror(APLOG_MARK, SASS_DEBUG_LOG_LEVEL, 0, \
p, "[SASS_DEBUG] %s(%d): "format, __FILE__, __LINE__, ##args)
/* default parameter */
#define SASS_DEFAULT_CONTENT_TYPE "text/css"
/* sass dir config */
typedef struct {
int is_compressed;
int is_output;
int display_error;
char *include_paths;
} sass_dir_config_t;
module AP_MODULE_DECLARE_DATA sass_module;
/* output css file */
static void
sass_output_file(request_rec *r, char *data)
{
char *ext, *fbase, *fname;
apr_status_t rc;
apr_size_t bytes;
apr_file_t *file = NULL;
ext = strstr(r->filename, ".");
if (!data || !ext) {
return;
}
fbase = apr_pstrndup(r->pool, r->filename, ext - r->filename);
fname = apr_psprintf(r->pool, "%s.css", fbase);
rc = apr_file_open(&file, fname, APR_WRITE | APR_CREATE,
APR_UREAD | APR_UWRITE | APR_GREAD, r->pool);
if (rc == APR_SUCCESS) {
bytes = strlen(data);
rc = apr_file_write(file, data, &bytes);
if (rc != APR_SUCCESS) {
_RERR(r, "Can't create/write to file: %s", fname);
}
apr_file_close(file);
} else {
_RERR(r, "Can't create/write to file: %s", fname);
}
}
/* content handler */
static int
sass_handler(request_rec *r)
{
int retval = OK;
sass_dir_config_t *config;
struct sass_file_context *context;
if (strcmp(r->handler, "sass-script")) {
return DECLINED;
}
/* content type */
r->content_type = SASS_DEFAULT_CONTENT_TYPE;
if (!r->header_only) {
config = ap_get_module_config(r->per_dir_config, &sass_module);
context = sass_new_file_context();
if (!context) {
return HTTP_INTERNAL_SERVER_ERROR;
}
context->options.include_paths = config->include_paths;
if (config->is_compressed) {
context->options.output_style = SASS_STYLE_COMPRESSED;
} else {
context->options.output_style = SASS_STYLE_NESTED;
}
context->input_path = r->filename;
sass_compile_file(context);
if (context->error_status) {
if (context->error_message) {
ap_rprintf(r, "%s", context->error_message);
} else {
ap_rputs("An error occured; no error message available.", r);
}
if (!config->display_error) {
retval = HTTP_INTERNAL_SERVER_ERROR;
}
} else if (context->output_string) {
ap_rprintf(r, "%s", context->output_string);
if (config->is_output) {
sass_output_file(r, context->output_string);
}
} else {
ap_rputs("Unknown internal error.", r);
if (!config->display_error) {
retval = HTTP_INTERNAL_SERVER_ERROR;
}
}
sass_free_file_context(context);
}
return retval;
}
/* create dir config */
static void *
sass_create_dir_config(apr_pool_t *p, char *dir)
{
sass_dir_config_t *config = apr_pcalloc(p, sizeof(sass_dir_config_t));
if (config) {
memset(config, 0, sizeof(sass_dir_config_t));
config->is_compressed = 0;
config->is_output = 0;
config->display_error = 0;
config->include_paths = "";
}
return (void *)config;
}
/* merge dir config */
static void *
sass_merge_dir_config(apr_pool_t *p, void *base_conf, void *override_conf)
{
sass_dir_config_t *config =
(sass_dir_config_t *)apr_pcalloc(p, sizeof(sass_dir_config_t));
sass_dir_config_t *base = (sass_dir_config_t *)base_conf;
sass_dir_config_t *override = (sass_dir_config_t *)override_conf;
if (override->is_compressed != 0) {
config->is_compressed = 1;
} else {
config->is_compressed = base->is_compressed;
}
if (override->is_output != 0) {
config->is_output = 1;
} else {
config->is_output = base->is_output;
}
if (override->display_error != 0) {
config->display_error = 1;
} else {
config->display_error = base->display_error;
}
if (override->include_paths && strlen(override->include_paths) > 0) {
config->include_paths = override->include_paths;
} else {
config->include_paths = base->include_paths;
}
return (void *)config;
}
/* Commands */
static const command_rec sass_cmds[] =
{
AP_INIT_FLAG("SassCompressed", ap_set_flag_slot,
(void *)APR_OFFSETOF(sass_dir_config_t, is_compressed),
RSRC_CONF|ACCESS_CONF, "sass compressed to 'on' or 'off'"),
AP_INIT_FLAG("SassOutput", ap_set_flag_slot,
(void *)APR_OFFSETOF(sass_dir_config_t, is_output),
RSRC_CONF|ACCESS_CONF, "sass output (css) to 'on' or 'off'"),
AP_INIT_FLAG("SassDisplayError", ap_set_flag_slot,
(void *)APR_OFFSETOF(sass_dir_config_t, display_error),
RSRC_CONF|ACCESS_CONF, "sass display error to 'on' or 'off'"),
AP_INIT_TAKE1("SassIncludePaths", ap_set_string_slot,
(void *)APR_OFFSETOF(sass_dir_config_t, include_paths),
RSRC_CONF|ACCESS_CONF, "sass include paths"),
{ NULL }
};
/* Hooks */
static void sass_register_hooks(apr_pool_t *p)
{
ap_hook_handler(sass_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* Module */
module AP_MODULE_DECLARE_DATA sass_module =
{
STANDARD20_MODULE_STUFF,
sass_create_dir_config, /* create per-dir config structures */
sass_merge_dir_config, /* merge per-dir config structures */
NULL, /* create per-server config structures */
NULL, /* merge per-server config structures */
sass_cmds, /* table of config file commands */
sass_register_hooks /* register hooks */
};