A code replacing Nametrans redirect?
Hi,
I use following line in obj.conf file
NameTrans fn="redirect" from="/testfolder" url="/test.jsp"
This enables all visitors trying to enter "/testfolder" and all subfolders to redirecting to "/test.jsp" file.
I need is not "redirecting" but "forwarding" of the request entering "/testfolder".
ie. The browser shall be displaying "http://aaa.bbb.ccc/testfolder/test2/" instead of redirected "http://aaa.bbb.ccc/test.jsp"
Is it possible? If possible can I be still reading the original request URL from test.jsp? Or somehow include that info in the process?
# 1
Here's some code I wrote years ago (back in the NES 3.6 or 4.1 days, though it still works fine in SJWS 6.1) to allow seamlessly remapping a URI (1:1 mapping of requested URI to handling URI) without issuing a redirect. Usage in obj.conf looks like
NameTrans fn="PW-internal-redirect" apparentPath="/" cgiPath="/apps/home.pl"
Don't worry about "cgi" in the "cgiPath" argument name -- it should work for remapping to any sort of URI. You'll need to replace strcmp() with something like shexp_cmp() in order to have a N:1 "/testfolder/*" to "/test.jsp" mapping as you discuss.
Note that by default the web server log records the first request line (method + URI + protocol/version) in the access log, and this NSAPI does not change that value. So if you remapped /testfolder/ to /test.jsp and fielded a request for /testfolder/, your access log would show something like "GET /testfolder/ HTTP/1.1" even though /test.jsp serviced the request.
To pass the original URI to /test.jsp, you'd need to add something like this to the SAF C code
param_free(pblock_remove("X-INITIAL-URI", rq->headers));
pblock_nvinsert("X-INITIAL-URI", pblock_findval("uri", rq->reqpb), rq->headers);
before resetting ppath and then you could use something like request.getHeader("x-initial-uri") in the JSP to retrieve the original URI.
See the NSAPI programmer's guide for more info if you haven't built a custom SAF before.
/* Netscape NSAPI header files */
#include "nsapi.h"
NSAPI_PUBLIC int PW_internal_redirect(pblock *pb, Session *sn, Request *rq) {
char *cgiPath;
char *apparentPath;
/* check parameters and ppath value (you could obviously change
from strcmp() to a regexp compile/comparison if needed) */
if ( ((cgiPath=pblock_findval("cgiPath",pb)) == NULL)
|| ((apparentPath=pblock_findval("apparentPath",pb)) == NULL)
|| (strcmp(apparentPath, pblock_findval("ppath", rq->vars)) != 0) )
return REQ_NOACTION;
/* looks like a match, clear the current ppath */
if ( param_free(pblock_remove("ppath", rq->vars)) ) {
/* make the httpd act as if they requested something else */
pblock_nvinsert("ppath", cgiPath, rq->vars);
}
/* and we return from the function */
return REQ_NOACTION;
}
The Makefile (Solaris, Sun Workshop compiler) looks like this:
# Defines for example NSAPI programs running under SOLARIS
CC_CMD=cc -DNET_SSL -DSOLARIS -D_REENTRANT -xchip=ultra2
LD_SHAREDCMD=ld -G -s
all:
prepare:
INCLUDEDIR=/path/to/sjwsdir/plugins/include
OBJS = PW_internal_redirect.o
INCLUDE_FLAGS=-I$(INCLUDEDIR) -I$(INCLUDEDIR)/base -I$(INCLUDEDIR)/frame
COMMON_DEFS=-DMCC_HTTPD -DXP_UNIX -DSPAPI20
all: PW_internal_redirect.so
PW_internal_redirect.so: $(OBJS)
$(LD_SHAREDCMD) $(OBJS) -o PW_internal_redirect.so $(EXTRA_LDDEFINES)
.c.o:
$(CC_CMD) $(COMMON_DEFS) $(INCLUDE_FLAGS) -c $<
clean:
rm $(OBJS) PW_internal_redirect.so $(EXTRA_CLEAN)