/** * app.c * * Copyright (C) 2003 - 2008 Bojan Smojver, Rexursive * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 2.1 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * */ #include #ifdef HAVE_RXV_SPIN_H #include #endif #ifdef HAVE_HTTP_CORE_H #include #endif int rxv_spin_prepare(rxv_spin_ctx_t *ctx){ char *auth_URL,*user=NULL; request_rec *r=rxv_spin_r(ctx); apr_pool_t *pool=rxv_spin_pool(ctx); /* if there is authentication configuration, use it */ if((auth_URL=rxv_spin_app_sget(ctx,"auth_URL"))){ if(!(user=rxv_spin_ses_sget(ctx,"auth_user"))){ char *full_URL,*this_URL; /* full URL is auth_URL + return URL */ this_URL=ap_construct_url(pool,r->parsed_uri.path,r); full_URL=apr_pstrcat(pool,auth_URL,"?return=",this_URL,NULL); apr_table_add(r->err_headers_out,"Location",full_URL); return HTTP_SEE_OTHER; } } return OK; } int rxv_spin_service(rxv_spin_ctx_t *ctx){ #ifndef RXV_SPIN_HASPREPARE int rc; #endif apr_pool_t *pool=rxv_spin_pool(ctx); rxv_spin_data_t *user,*hehe,*hoho,*e,*abc,*def,*ghi,*w,*x,*y,*z,*xyz,*nbsp3, *first=NULL,*last=NULL,*odd=NULL,*shrunk,*stretched, *t1=NULL,*t2=NULL,*t3=NULL,*t4=NULL,*t5=NULL,*s,*upper,*f, *dbs,*cns,*src; rxv_spin_curs_t *fc=NULL,*lc=NULL,*oc=NULL,*dc=NULL,*cc=NULL; rxv_spin_db_t *db; rxv_spin_data_t *result=NULL; apr_size_t r,sz,i; #ifndef RXV_SPIN_HASPREPARE if((rc=rxv_spin_prepare(ctx))!=OK) return rc; #endif /* if we have a valid user, put it in context */ if((user=rxv_spin_ses_get(ctx,"auth_user"))) rxv_spin_set(ctx,"user",user); /* funny data :-) */ hehe=rxv_spin_datum(pool,"hehe",NULL); hoho=rxv_spin_datum(pool,"hehe",NULL); /* empty data */ e=apr_pcalloc(pool,100); /* three non-breaking spaces */ nbsp3=rxv_spin_datum(pool,"   ",NULL); /* plain string array data */ def=rxv_spin_column(pool,"def",NULL, rxv_spin_datum(pool,"1",t1), rxv_spin_datum(pool,"2",t2), rxv_spin_datum(pool,"3",t3), NULL); ghi=rxv_spin_column(pool,"ghi",NULL, rxv_spin_datum(pool,"a",t1), rxv_spin_datum(pool,"b",t2), rxv_spin_datum(pool,"c",t3), NULL); w=rxv_spin_column(pool,"w",NULL, rxv_spin_datum(pool,"1",t1), rxv_spin_datum(pool,"22",t2), rxv_spin_datum(pool,"333",t3), rxv_spin_datum(pool,"4444",t4), rxv_spin_datum(pool,"55555",t5), NULL); x=rxv_spin_column(pool,"x",NULL, rxv_spin_datum(pool,"x1",t1), rxv_spin_datum(pool,"x2",t2), rxv_spin_datum(pool,"x3",t3), rxv_spin_datum(pool,"x4",t4), rxv_spin_datum(pool,"x5",t5), NULL); f=rxv_spin_column(pool,"x",NULL, rxv_spin_datum(pool,"f1",t1), rxv_spin_datum(pool,"f2",t2), rxv_spin_datum(pool,"f3",t3), rxv_spin_datum(pool,"f4",t4), rxv_spin_datum(pool,"f5",t5), NULL); y=rxv_spin_column(pool,"y",NULL, rxv_spin_datum(pool,"y1",t1), rxv_spin_datum(pool,"y2",t2), rxv_spin_datum(pool,"y3",t3), rxv_spin_datum(pool,"y4",t4), rxv_spin_datum(pool,"y5",t5), NULL); z=rxv_spin_column(pool,"z",NULL, rxv_spin_datum(pool,"z1",t1), rxv_spin_datum(pool,"z2",t2), rxv_spin_datum(pool,"z3",t3), rxv_spin_datum(pool,"z4",t4), rxv_spin_datum(pool,"z5",t5), NULL); /* rows data */ xyz=rxv_spin_column(pool,"xyz",NULL, rxv_spin_rows(pool,NULL,w,f,y,z,NULL), rxv_spin_rows(pool,NULL,x,y,z,w,NULL), rxv_spin_rows(pool,NULL,y,z,w,f,NULL), rxv_spin_rows(pool,NULL,z,w,f,y,NULL), NULL); abc=rxv_spin_rows(pool,NULL,def,ghi,xyz,NULL); dbs=rxv_spin_parse(pool,"dbs", apr_pstrdup(pool,rxv_spin_app_sget(ctx,"dbs")),",",NULL); cns=rxv_spin_parse(pool,"cns", apr_pstrdup(pool,rxv_spin_app_sget(ctx,"cns")),"^",NULL); src=rxv_spin_rows(pool,NULL,dbs,cns,NULL); /* database access test */ for(i=rxv_spin_first(pool,src,"dbs",&dc,"cns",&cc,NULL); i; i=rxv_spin_next(dc)){ db=rxv_spin_db_open(ctx,rxv_spin_string(rxv_spin_this(cc))); if(db){ result=rxv_spin_db_pselect(pool,db,"select * from spintest",NULL); if(result){ sz=rxv_spin_size(result); first=rxv_spin_null(pool,"first",sz,NULL); last=rxv_spin_null(pool,"last",sz,NULL); odd=rxv_spin_null(pool,"odd",sz,NULL); result=rxv_spin_rows(pool,NULL,result,first,last,odd,NULL); for(r=rxv_spin_first(pool,result,"first",&fc,"last",&lc,"odd",&oc,NULL); r; r=rxv_spin_next(fc)){ if(r==1){ s=rxv_spin_this(fc); rxv_spin_datum(pool,"first",s); } else if(r==sz){ s=rxv_spin_this(lc); rxv_spin_datum(pool,"last",s); } if(r%2 != 0){ s=rxv_spin_this(oc); rxv_spin_datum(pool,"odd",s); } } rxv_spin_set(ctx,apr_pstrcat(pool, rxv_spin_string(rxv_spin_this(dc)), "result",NULL), result); /* let's manipulate the result a bit and put into context again */ upper=rxv_spin_copy(pool,result,NULL); for(r=rxv_spin_first(pool,upper,"firstfield",&fc,NULL); r; r=rxv_spin_next(fc)){ rxv_spin_upper(rxv_spin_this(fc)); } rxv_spin_set(ctx,apr_pstrcat(pool, rxv_spin_string(rxv_spin_this(dc)), "upperresult",NULL), upper); } } } /* a bit of resizing */ if(result){ shrunk=rxv_spin_resize(rxv_spin_copy(pool,result,NULL),5); rxv_spin_set(ctx,"shrunk",shrunk); stretched=rxv_spin_resize(rxv_spin_copy(pool,result,NULL),10); for(r=rxv_spin_first(pool,stretched,"firstfield",&fc,"odd",&oc,NULL); r; r=rxv_spin_next(fc)){ s=rxv_spin_this(fc); rxv_spin_datum(pool,apr_psprintf(pool,"New value=%" APR_SIZE_T_FMT,r),s); if(r%2 != 0){ s=rxv_spin_this(oc); rxv_spin_datum(pool,"odd",s); } } rxv_spin_set(ctx,"stretched",stretched); } /* use trim */ rxv_spin_set(ctx,"trimmed", rxv_spin_trim2(rxv_spin_datum(pool, apr_pstrdup(pool," trimmed "), NULL))); rxv_spin_sset(ctx,"trimmedstring", rxv_spin_strim2(apr_pstrdup(pool," trimmed string "))); /* counted strings */ rxv_spin_set(ctx,"counted1",rxv_spin_mdatum(pool,"1234567890",10,NULL)); rxv_spin_set(ctx,"counted2", rxv_spin_mdatum(pool,"12345678901234567890",20,NULL)); /* application/session data set/get */ rxv_spin_app_sset(ctx,"application key 1","application value 1"); rxv_spin_set(ctx,"application-key-1", rxv_spin_app_get(ctx,"application key 1")); rxv_spin_app_sset(ctx,"application key 2","application value 2"); rxv_spin_set(ctx,"application-key-2", rxv_spin_app_get(ctx,"application key 2")); rxv_spin_ses_sset(ctx,"session key 1","session value 1"); rxv_spin_set(ctx,"session-key-1",rxv_spin_ses_get(ctx,"session key 1")); rxv_spin_ses_sset(ctx,"session key 2","session value 2"); rxv_spin_set(ctx,"session-key-2",rxv_spin_ses_get(ctx,"session key 2")); rxv_spin_set(ctx,"hehe",hehe); rxv_spin_set(ctx,"hoho",hoho); rxv_spin_set(ctx,"empty",e); rxv_spin_set(ctx,"abc",abc); rxv_spin_set(ctx,"nbsp3",nbsp3); /* application configuration parameters */ rxv_spin_set(ctx,"spinparameter1", rxv_spin_app_get(ctx,"spinparameter1")); rxv_spin_set(ctx,"spinparameter2", rxv_spin_app_get(ctx,"spinparameter2")); /* session id and validity */ rxv_spin_sset(ctx,"sessionid",rxv_spin_ses_id(ctx)); rxv_spin_sset(ctx,"sessionvalid",rxv_spin_ses_valid(ctx)?"yes":"no"); return OK; }