#include "nametrans.h"
#include "../daemon/log.h"
#include "../daemon/request.h"
#include "../util/pblock.h"
#include "../util/util.h"
int assign_name(pblock *pb, Session *sn, Request *rq) {
char *name = pblock_findkeyval(pb_key_name, pb);
char *from = pblock_findkeyval(pb_key_from, pb);
if(!name) {
log_ereport(
LOG_MISCONFIG,
"assign-name: missing name parameter");
protocol_status(sn, rq,
500,
NULL);
return REQ_ABORTED;
}
if(from) {
char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
char c;
int i =
0;
while((c = from[i]) !=
0) {
if(c != uri[i]) {
return REQ_NOACTION;
}
i++;
}
}
pblock_kvinsert(pb_key_name, name, strlen(name), rq->vars);
return REQ_NOACTION;
}
int document_root(pblock *pb, Session *sn, Request *rq) {
char *root = pblock_findkeyval(pb_key_root, pb);
if(!root) {
log_ereport(
LOG_MISCONFIG,
"document-root: missing root parameter");
protocol_status(sn, rq,
500,
NULL);
return REQ_ABORTED;
}
sstr_t root_str = sstr(root);
sstr_t uri_str = sstr(pblock_findkeyval(pb_key_uri, rq->reqpb));
request_set_path(root_str, uri_str, rq->vars);
return REQ_PROCEED;
}
int pfx2dir(pblock *pb, Session *sn, Request *rq) {
char *from = pblock_findkeyval(pb_key_from, pb);
char *dir = pblock_findkeyval(pb_key_dir, pb);
char *name = pblock_findkeyval(pb_key_name, pb);
if(!from || !dir) {
if(!from && dir) {
log_ereport(
LOG_MISCONFIG,
"pfx2dir: missing from parameter");
}
else if(!dir && from) {
log_ereport(
LOG_MISCONFIG,
"pfx2dir: missing dir parameter");
}
else {
log_ereport(
LOG_MISCONFIG,
"pfx2dir: missing from and dir parameter");
}
protocol_status(sn, rq,
500,
NULL);
return REQ_ABORTED;
}
char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
char fc;
char uc;
int i =
0;
while((fc = from[i]) !=
0) {
uc = uri[i];
if(fc != uc) {
return REQ_NOACTION;
}
i++;
}
uri = uri + i;
if(uri[
0] ==
'/') {
uri++;
}
request_set_path(sstr(dir), sstr(uri), rq->vars);
if(name) {
pblock_kvinsert(pb_key_name, name, strlen(name), rq->vars);
}
return REQ_PROCEED;
}
int redirect(pblock *pb, Session *sn, Request *rq) {
char *from = pblock_findval(
"from", pb);
char *url = pblock_findval(
"url", pb);
if(!from || !url) {
log_ereport(
LOG_MISCONFIG,
"redirect: missing parameter (from, url)");
return REQ_ABORTED;
}
char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
if(!strcmp(uri, from)) {
pblock_nvinsert(
"location", url, rq->srvhdrs);
protocol_status(sn, rq,
302,
NULL);
return REQ_ABORTED;
}
return REQ_NOACTION;
}
int simple_rewrite(pblock *pb, Session *sn, Request *rq) {
char *from = pblock_findval(
"from", pb);
char *root = pblock_findval(
"root", pb);
char *path = pblock_findval(
"path", pb);
char *name = pblock_findval(
"name", pb);
if(!from || !path || !root) {
log_ereport(
LOG_MISCONFIG,
"simple-rewrite: missing parameter (from, root, path)");
return REQ_ABORTED;
}
char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb);
sstr_t u = sstr(uri);
sstr_t f = sstr(from);
if(sstrprefix(u, f)) {
sstr_t suf = sstrsubs(u, f.length);
sstr_t ppath = sstrcat(
2, sstr(path), suf);
request_set_path(sstr(root), ppath, rq->vars);
free(ppath.ptr);
if(name) {
pblock_kvinsert(pb_key_name, name, strlen(name), rq->vars);
}
}
return REQ_NOACTION;
}