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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/*
** utility.c -- Apache layout module
** $Revision: 1.3 $
**
** Copyright 2007 Brian Aker brian@tangent.org
**
** Please see "LICENSE" file that was included in the distribution.
**
*/
#include "mod_layout.h"
LAYOUT_EXPORT(apr_array_header_t *) layout_array_push_kind(apr_pool_t *p, apr_array_header_t *origin, apr_array_header_t *new, int kind) {
apr_array_header_t *returnable= NULL;
int x= 0;
layout_string **new_layouts;
layout_string **origin_layouts;
new_layouts= (layout_string **) new->elts;
origin_layouts= (layout_string **) origin->elts;
if (!origin && !new)
return NULL;
else if (!origin)
return new;
else if (!new)
return origin;
/* Size of new plus some padding */
returnable= apr_array_make (p, new->nelts + 2 + origin->nelts, sizeof (layout_string *));
/* Copy in the origin pieces */
for (x= 0; x < origin->nelts; x++)
{
if (origin_layouts[x]->kind == kind)
*(layout_string **) apr_array_push (returnable)= (layout_string *) origin_layouts[x];
}
/* Copy in the new pieces */
for (x= 0; x < new->nelts; x++)
{
if (new_layouts[x]->kind == kind)
*(layout_string **) apr_array_push (returnable)= (layout_string *) new_layouts[x];
}
return returnable;
}
LAYOUT_EXPORT(int) check_type(layout_request *request)
{
#ifdef DEBUG
printf("check_type: %s \n", request->type);
#endif
if (request->http == ORIGIN)
return 0;
if (request->type == 0)
return 0;
if (strcmp(request->type, "text/plain") == 0)
return 1;
if (strcmp(request->type, "text/html") == 0)
return 1;
return 0;
}
LAYOUT_EXPORT(layout_request *) create_layout_request(request_rec *r, layout_conf *cfg)
{
layout_request *info= NULL;
const char *content_length= NULL;
info= apr_pcalloc(r->pool, sizeof(layout_request));
info->origin= cfg->display_origin;
info->header= OFF;
info->footer= OFF;
if ((content_length= apr_table_get(r->headers_in, "Content-Length")))
info->length= (content_length ? atoi(content_length) : 0);
info->type= NULL;
info->http= LAYOUT;
#ifdef DEBUG
//printf("IGNORES HTTP %d Header %d Footer %d\n", cfg->uris_ignore_http_header, cfg->uris_ignore_header, cfg->footer_enabled);
#endif
if (isOn(cfg->header_enabled))
{
info->header= ON;
if (cfg->uris_ignore_header)
{
if (table_find(cfg->uris_ignore_header, r->uri))
info->header= OFF;
}
}
if (isOn(cfg->footer_enabled))
{
info->footer= ON;
if (cfg->uris_ignore_footer)
{
if (table_find(cfg->uris_ignore_footer, r->uri))
info->footer= OFF;
}
}
return info;
}
LAYOUT_EXPORT(int) table_search(request_rec *r, const apr_table_t *t, const char *string)
{
const apr_array_header_t *hdrs_arr= NULL;
const apr_table_entry_t *elts= NULL;
int i= 0;
if (string == NULL)
return 0;
if (t == NULL)
return 0;
hdrs_arr= apr_table_elts(t);
elts= (const apr_table_entry_t *) hdrs_arr->elts;
for (i= 0; i < hdrs_arr->nelts; ++i)
{
if (string_search(r, string, elts[i].key, 0, 0) == -1)
return 0;
}
return 1;
}
LAYOUT_EXPORT(void) table_cat(apr_table_t *src, apr_table_t *dest, char *string)
{
const apr_array_header_t *hdrs_arr= NULL;
const apr_table_entry_t *elts= NULL;
int x= 0;
if (src == NULL)
return;
if (dest == NULL)
return;
hdrs_arr= apr_table_elts(src);
elts= (const apr_table_entry_t *) hdrs_arr->elts;
if (string)
{
for (x= 0; x < hdrs_arr->nelts; ++x)
{
if (strcasecmp(string,elts[x].key) == 0)
apr_table_add(dest, elts[x].key, elts[x].val);
}
}
else
{
for (x= 0; x < hdrs_arr->nelts; ++x)
apr_table_add(dest, elts[x].key, elts[x].val);
}
}
void table_list(char *string, const apr_table_t * t)
{
const apr_array_header_t *hdrs_arr= NULL;
const apr_table_entry_t *elts= NULL;
int i= 0;
if (t == NULL)
return;
if (string == NULL)
string= "table_list: ";
hdrs_arr= apr_table_elts(t);
elts= (const apr_table_entry_t *) hdrs_arr->elts;
for (i= 0; i < hdrs_arr->nelts; ++i)
printf("%s:Key %s:%s:\n", string, elts[i].key, elts[i].val);
}
LAYOUT_EXPORT(int) string_search(request_rec *r, const char *string, const char *delim, int init_pos, int flag)
{
char *temp= NULL;
char *sub_temp= NULL;
char *lower= NULL;
char *substring= NULL;
int position= 0;
int end= 0;
int delim_size;
int complete_position= 0;
if (delim == NULL || string == NULL)
return -1;
/* length= strlen(string); */
delim_size= strlen(delim);
temp= (char *)string + init_pos;
complete_position= init_pos;
while ((position= ap_ind(temp, delim[0])) != -1) {
sub_temp= temp + position;
if ((end= ap_ind(sub_temp, delim[delim_size - 1])) != -1) {
substring= apr_pstrndup(r->pool, sub_temp , end + 1);
lower= apr_pstrdup(r->pool, substring);
ap_str_tolower(lower);
if (apr_fnmatch(delim, lower, APR_FNM_CASE_BLIND) == 0)
{
if (flag)
complete_position += position;
else
complete_position += position + end + 1;
return complete_position;
}
}
else
return -1;
temp += end + 1;
complete_position += end + 1;
}
return -1;
}
LAYOUT_EXPORT(int) parser_put(request_rec *r, layout_conf *cfg, layout_request *info, const char *string, int init_pos)
{
char *temp= NULL;
char *sub_temp= NULL;
char *lower= NULL;
char *substring= NULL;
int length= 0;
int end= 0;
int x= 0;
int j= 0;
int match= 0;
int complete_position= 0;
int run= 0;
layout_string **layouts;
if (cfg->layouts != NULL)
layouts= (layout_string **) cfg->layouts->elts;
else
layouts= NULL;
if (string == NULL)
return -1;
length= strlen(string);
temp= (char *)string + init_pos;
complete_position= init_pos;
x= init_pos;
while ( x < length )
{
if (string[x] == '<')
{
sub_temp= (char *)string + x;
if ((end= ap_ind(sub_temp, '>')) != -1 && layouts)
{
substring= apr_pstrndup(r->pool, sub_temp , end + 1);
lower= apr_pstrdup(r->pool, substring);
ap_str_tolower(lower);
#ifdef DEBUG
printf("MATCH: %s (%d)\n", lower, cfg->layouts->nelts);
#endif
for (j= 0; j < cfg->layouts->nelts; j++)
{
#ifdef DEBUG
printf("\tWITH: %s\n", layouts[j]->pattern);
#endif
/* The next few lines should be reduced */
run= 1;
if (layouts[j]->kind == HEADER && !info->header)
run= 0;
if (layouts[j]->kind == FOOTER && !info->footer)
run= 0;
if (run)
{
if (apr_fnmatch(layouts[j]->pattern, lower, APR_FNM_CASE_BLIND) == 0)
{
if (layouts[j]->append == APPEND)
{
ap_fputs(info->f, info->b, substring);
layout_print(r, cfg, info, j);
if (isOn(cfg->notes))
update_info(r->notes, info);
}
else if (layouts[j]->append == PREPEND)
{
layout_print(r, cfg, info, j);
if (isOn(cfg->notes))
update_info(r->notes, info);
ap_fputs(info->f, info->b, substring);
}
else
{ /* We replace! */
layout_print(r, cfg, info, j);
if (isOn(cfg->notes))
update_info(r->notes, info);
}
match++;
}
}
}
if (match == 0)
ap_fputs(info->f, info->b, substring);
match= 0;
x += strlen(substring);
}
else
{
/* If we never match just write out the substring and keep going */
ap_fputc(info->f, info->b, string[x]);
x++;
}
}
else
{
ap_fputc(info->f, info->b, string[x]);
x++;
}
}
return -1;
}
LAYOUT_EXPORT(int) find_headers(request_rec *r, char *body)
{
int position= 0;
char *temp= NULL;
unsigned int end= 0;
temp= body;
if (body == NULL)
return -1;
while ((position= ap_ind(temp, '\n')) != -1)
{
if (temp[position + 1] == '\n')
{
end= end + position + 1;
return (end);
}
if (temp[position + 1] == '\r')
{
end += position + 2;
return (end);
}
temp= temp + position + 1;
end= end + position + 1;
}
return -1;
}
int check_table(const char *a)
{
if (a == NULL)
return 0;
if ('1' == a[0])
return 1;
return 0;
}
/* This method is borrowed from alloc.c in the main apache
distribution. */
LAYOUT_EXPORT(int) table_find(const apr_table_t *t, const char *key)
{
const apr_array_header_t *hdrs_arr= NULL;
const apr_table_entry_t *elts= NULL;
unsigned int x= 0;
if (t == NULL)
return 0;
hdrs_arr= apr_table_elts(t);
elts= (const apr_table_entry_t *) hdrs_arr->elts;
if (key == NULL)
return 0;
for (x= 0; x < hdrs_arr->nelts; ++x)
{
if (!apr_fnmatch(elts[x].key, key, APR_FNM_CASE_BLIND))
if (check_table(elts[x].val))
return 1;
}
return 0;
}
LAYOUT_EXPORT(void) update_info(const apr_table_t *t, layout_request *info)
{
const apr_array_header_t *hdrs_arr= NULL;
const apr_table_entry_t *elts= NULL;
int i= 0;
if (t == NULL)
return;
hdrs_arr= apr_table_elts(t);
elts= (const apr_table_entry_t *) hdrs_arr->elts;
for (i= 0; i < hdrs_arr->nelts; ++i)
{
if (!apr_fnmatch(elts[i].key, "LAYOUT", APR_FNM_CASE_BLIND))
{
if (!apr_fnmatch(elts[i].val, "originoff", APR_FNM_CASE_BLIND))
info->origin= OFF;
else if (!apr_fnmatch(elts[i].val, "originon", APR_FNM_CASE_BLIND))
info->origin= ON;
else if (!apr_fnmatch(elts[i].val, "footeroff", APR_FNM_CASE_BLIND))
info->footer= OFF;
else if (!apr_fnmatch(elts[i].val, "footeron", APR_FNM_CASE_BLIND))
info->footer= ON;
else if (!apr_fnmatch(elts[i].val, "headeroff", APR_FNM_CASE_BLIND))
info->header= OFF;
else if (!apr_fnmatch(elts[i].val, "headeron", APR_FNM_CASE_BLIND))
info->header= ON;
}
}
}
LAYOUT_EXPORT(int) is_ignored(request_rec *r, layout_conf *cfg, layout_request *info, char *body)
{
if (cfg->tag_ignore)
{
if (table_search(r, cfg->tag_ignore, body))
{
info->header= OFF;
info->footer= OFF;
return 1;
}
}
if (cfg->tag_ignore_footer)
{
if (table_search(r, cfg->tag_ignore_footer, body))
info->footer= OFF;
}
if (cfg->tag_ignore_header)
{
if (table_search(r, cfg->tag_ignore_header, body))
info->header= OFF;
}
return 0;
}
LAYOUT_EXPORT(int) call_container(request_rec *r, const char *uri, layout_conf *cfg, layout_request *info, int assbackwards)
{
int status= OK;
request_rec *subr;
const char *temp= NULL;
#ifdef LAYOUT_FILEOWNER_NAME
struct passwd * uidpasswd= NULL;
#endif
subr= (request_rec *) ap_sub_req_lookup_uri(uri, r, info->f);
apr_table_setn(subr->headers_in, "Content-Length", "0");
apr_table_setn(subr->subprocess_env, "LAYOUT_SCRIPT_NAME", r->uri);
apr_table_setn(subr->subprocess_env, "LAYOUT_PATH_INFO", r->path_info);
apr_table_setn(subr->subprocess_env, "LAYOUT_QUERY_STRING", r->args);
apr_table_setn(subr->subprocess_env, "LAYOUT_FILENAME", r->filename);
#ifdef NOT_USED
apr_table_setn(subr->subprocess_env, "LAYOUT_LAST_MODIFIED",
apr_ht_time(r->pool, r->finfo.st_mtime, cfg->time_format, 0));
#endif
#ifdef LAYOUT_FILEOWNER_NAME
uidpasswd=getpwuid(r->finfo.st_uid);
if (uidpasswd)
apr_table_setn(subr->subprocess_env, "LAYOUT_FILEOWNER_NAME", uidpasswd->pw_name);
#endif
subr->args= r->args;
subr->path_info= r->path_info;
subr->assbackwards= assbackwards;
temp= apr_table_get(r->headers_in, "Referer");
if (temp)
apr_table_setn(subr->subprocess_env, "HTTP_REFERER", temp);
status= ap_run_sub_req(subr);
table_cat(subr->notes, r->notes, NULL);
ap_destroy_sub_req(subr);
return status;
}
LAYOUT_EXPORT(char *) layout_add_file(cmd_parms *cmd, const char *file)
{
apr_file_t *file_ptr;
char buf[HUGE_STRING_LEN];
char *content= NULL;
apr_status_t rc;
rc= apr_file_open(&file_ptr, file, APR_READ | APR_BINARY | APR_XTHREAD,
APR_OS_DEFAULT, cmd->pool);
if (rc != APR_SUCCESS)
{
ap_log_error(APLOG_MARK, APLOG_WARNING, rc, cmd->server,
"mod_layout: unable to open file(%s, O_RDONLY), skipping", file);
return NULL;
}
while ((rc= apr_file_gets(buf, HUGE_STRING_LEN, file_ptr)) == APR_SUCCESS)
{
if (content)
content= (char *)apr_pstrcat(cmd->temp_pool, content, buf, NULL);
else
content= (char *)apr_pstrcat(cmd->temp_pool, buf, NULL);
}
apr_file_close(file_ptr);
return content;
}
LAYOUT_EXPORT(void) layout_kind(request_rec *r, layout_conf *cfg, layout_request *info, int kind)
{
unsigned int x= 0;
layout_string **layouts;
layouts= (layout_string **) cfg->layouts->elts;
for(x= 0; x < cfg->layouts->nelts; x++)
{
if (layouts[x]->kind == kind)
layout_print(r, cfg, info, x);
}
}
|