comparison src/c/urweb.c @ 1465:2f5fd248588d

getHeader and setHeader
author Adam Chlipala <adam@chlipala.net>
date Sun, 29 May 2011 14:29:26 -0400
parents fb88d64abec8
children 8fce85939259
comparison
equal deleted inserted replaced
1464:969b90b1f2f9 1465:2f5fd248588d
3344 return s; 3344 return s;
3345 else 3345 else
3346 return NULL; 3346 return NULL;
3347 } 3347 }
3348 3348
3349 int mime_format(const char *s) { 3349 static int mime_format(const char *s) {
3350 for (; *s; ++s) 3350 for (; *s; ++s)
3351 if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.') 3351 if (!isalnum((int)*s) && *s != '/' && *s != '-' && *s != '.')
3352 return 0; 3352 return 0;
3353 3353
3354 return 1; 3354 return 1;
3370 3370
3371 if (ctx->app->check_mime(s)) 3371 if (ctx->app->check_mime(s))
3372 return s; 3372 return s;
3373 else 3373 else
3374 return NULL; 3374 return NULL;
3375 }
3376
3377 uw_Basis_string uw_Basis_blessRequestHeader(uw_context ctx, uw_Basis_string s) {
3378 if (!mime_format(s))
3379 uw_error(ctx, FATAL, "Request header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
3380
3381 if (ctx->app->check_requestHeader(s))
3382 return s;
3383 else
3384 uw_error(ctx, FATAL, "Disallowed request header %s", uw_Basis_htmlifyString(ctx, s));
3385 }
3386
3387 uw_Basis_string uw_Basis_checkRequestHeader(uw_context ctx, uw_Basis_string s) {
3388 if (!mime_format(s))
3389 return NULL;
3390
3391 if (ctx->app->check_requestHeader(s))
3392 return s;
3393 else
3394 return NULL;
3395 }
3396
3397 uw_Basis_string uw_Basis_blessResponseHeader(uw_context ctx, uw_Basis_string s) {
3398 if (!mime_format(s))
3399 uw_error(ctx, FATAL, "Response header \"%s\" contains invalid character", uw_Basis_htmlifyString(ctx, s));
3400
3401 if (ctx->app->check_responseHeader(s))
3402 return s;
3403 else
3404 uw_error(ctx, FATAL, "Disallowed response header %s", uw_Basis_htmlifyString(ctx, s));
3405 }
3406
3407 uw_Basis_string uw_Basis_checkResponseHeader(uw_context ctx, uw_Basis_string s) {
3408 if (!mime_format(s))
3409 return NULL;
3410
3411 if (ctx->app->check_responseHeader(s))
3412 return s;
3413 else
3414 return NULL;
3415 }
3416
3417 uw_Basis_string uw_Basis_getHeader(uw_context ctx, uw_Basis_string name) {
3418 return uw_Basis_requestHeader(ctx, name);
3419 }
3420
3421 static int mime_value_format(const char *s) {
3422 for (; *s; ++s)
3423 if (*s == '\r' || *s == '\n')
3424 return 0;
3425
3426 return 1;
3427 }
3428
3429 uw_unit uw_Basis_setHeader(uw_context ctx, uw_Basis_string name, uw_Basis_string value) {
3430 if (!mime_value_format(value))
3431 uw_error(ctx, FATAL, "Invalid value for HTTP response header");
3432
3433 uw_write_header(ctx, name);
3434 uw_write_header(ctx, ": ");
3435 uw_write_header(ctx, value);
3436 uw_write_header(ctx, "\r\n");
3437
3438 return uw_unit_v;
3375 } 3439 }
3376 3440
3377 uw_Basis_string uw_unnull(uw_Basis_string s) { 3441 uw_Basis_string uw_unnull(uw_Basis_string s) {
3378 return s ? s : ""; 3442 return s ? s : "";
3379 } 3443 }