Thursday Jul 10, 2008
Guy asked about how to add Expires header with "access plus 1 day" in my post yesterday, see more at
http://blogs.sun.com/walter/entry/how_to_add_expires_header#comments
I pointed him to
http://forum.java.sun.com/thread.jspa?threadID=5019803&messageID=9052343#9052343
and I tested it work ok, so like to share with all of you.
You can download the ready to use expire.so and its source at
http://blogs.sun.com/walter/resource/code/expire.c
(source code)
and ready to go .so at
http://blogs.sun.com/walter/resource/code/expire.so
(Solaris NSAPI plugin .so)
You will also see the steps to build a NSAPI plugin below.
1. I copied the source code at
http://forum.java.sun.com/thread.jspa?threadID=5019803&messageID=9052343#9052343
into my web 6.1SP9 example NSAPI plugin directory,
apple:/export/home/iws6.1sp9/plugins/nsapi/examples>
-rw-rw-rw- 1 root other 2862 Jul 10 13:53 expire.c
2. I made some changes to the code,
( you can see the complete code at
http://blogs.sun.com/walter/resource/code/expire.c )
a. add the needed header,
apple:/export/home/iws6.1sp9/plugins/nsapi/examples> cat expire.c
#ifdef XP_WIN32
#define NSAPI_PUBLIC __declspec(dllexport)
#else /* !XP_WIN32 */
#define NSAPI_PUBLIC
#endif /* !XP_WIN32 */
#include "nsapi.h"
NSAPI_PUBLIC int expire(pblock *pb, Session *sn, Request *rq)
{
.......
b. you can also see I change the function name above from
kpn_set_cacheable
to
expire
because at first, I got an error earlier when I used this function name,
Service fn="kpn-set-cacheable" max-age="15724800"
because it should be underscore as below, instead of hypens above.
int kpn_set_cacheable(pblock *pb, Session *sn, Request *rq)
So, when I tried this earlier, I got this error ,
[10/Jul/2008:13:55:14] config (11680): for host 129.150.154.110 trying to
GET /images/, func_exec reports: HTTP2122: cannot find function named kpn-set-cacheable
So, I changed the function name to expire to avoid any such above.
3. then I change the Makefile, there,
You can see my Makefile at
http://blogs.sun.com/walter/resource/code/Makefile
4. then
apple:/export/home/iws6.1sp9/plugins/nsapi/examples> touch expire.c
apple:/export/home/iws6.1sp9/plugins/nsapi/examples> make
cc -DNET_SSL -DSOLARIS -D_REENTRANT -DMCC_HTTPD -DXP_UNIX -DSPAPI20 -I../../include -I../../include/base -I../../include/frame -I../../include/nspr -I/usr/include/mps -c expire.c
make prepare
ld -G expire.o -o expire.so
apple:/export/home/iws6.1sp9/plugins/nsapi/examples> ls -lrt
-rw-rw-rw- 1 root other 2862 Jul 10 14:43 expire.c
-rwxrwxrwx 1 root other 5748 Jul 10 14:43 expire.so
-rw-rw-rw- 1 root other 4536 Jul 10 14:43 expire.o
5. then add this into the end of magnus.conf,
Init fn="load-modules" shlib="/export/home/iws6.1sp9/plugins/nsapi/examples/expire.so"
funcs="expire"
(all in 1 line above)
6. added this into the end of obj.conf,
<Object ppath="/export/home/iws6.1sp9/docs/images/*">
Service fn="expire" max-age="86400"
Service method="(GET|HEAD)" type="~magnus-internal/" fn="send-file" nocache=""
</Object>
(note - 1 day = 24 hr * 60 min * 60 sec = 86400 )
7. then restart,
pple:/export/home/iws6.1sp9/https-pblock/config> ../stop; ../start
8. test 1: a file inside /images dir,
apple:/export/home/iws6.1sp9/https-pblock/config> telnet apple.asia 61903
Trying 129.158.175.16...
Connected to apple.asia.sun.com.
Escape character is '^]'.
GET /images/blank.gif HTTP/1.0
HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Thu, 10 Jul 2008 06:50:15 GMT *** access time
Content-length: 43
Content-type: image/gif
Cache-control: public
Expires: Fri, 11 Jul 2008 06:50:15 GMT *** +1 day , see this below !
Last-modified: Thu, 10 Jul 2008 04:27:34 GMT
Accept-ranges: bytes
Connection: close
GIF89a€ÿÿÿ!ù
,L;Connection closed by foreign host.
So, we added the needed access +1 day timestamp into the Expires header above, Expires: Fri, 11 Jul 2008 06:50:15 GMT
logs:
129.158.175.16 - - [10/Jul/2008:14:50:15 +0800] "GET /images/blank.gif HTTP/1.0" 200 43
9. test2 : a file outside the /images dir,
apple:/export/home/iws6.1sp9/https-pblock/config> telnet apple.asia 61903
Trying 129.158.175.16...
Connected to apple.asia.sun.com.
Escape character is '^]'.
GET /blank.gif HTTP/1.0
HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Thu, 10 Jul 2008 06:55:36 GMT
Content-length: 43
Content-type: image/gif
Last-modified: Thu, 27 Mar 2008 00:22:13 GMT
Accept-ranges: bytes
Connection: close
GIF89a€ÿÿÿ!ù
,L;Connection closed by foreign host.
(no such Expires header added as in test 1)
However, this is custom coding and used at your own risk.
(Credits should go to henkfictorie who posted this source code at
http://forum.java.sun.com/thread.jspa?threadID=5019803&messageID=9052343#9052343 )
Tuesday Jul 08, 2008
Sometimes, you would like to add Expires header to all contents in a particular directory, so no cache is allowed in outside browsers.
First of all, it is always better to use web 7.0 because of its better regex matching and httpdate function.
(For Sun internal people, you can look up Sriram's nice write-up on this topic in mailfinder,
Subject : Web Server 7 configuration settings for expires/cache-control/etag
Date: 2008-05-25 16:10
From : Sriram.Natarajan
)
But if you really need this in 6.1, then you can try below <Client..>...</Client> in 6.1:
e.g. change obj.conf below and add <Client> tags:
apple:/export/home/iws6.1sp9/https-jsp/config> cat obj.conf
# You can edit this file, but comments and formatting changes
# might be lost when the admin server makes changes.
<Object name="default">
AuthTrans fn="match-browser" browser="*MSIE*" ssl-unclean-shutdown="true"
......
Service method="(GET|HEAD|POST)" type="*~magnus-internal/*" fn="send-file"
Service method="TRACE" fn="service-trace"
<Client uri="*/nokeep/*.*">
Output fn="set-variable" insert-srvhdrs="Expires: Thu, 01 Dec 1994 16:00:00 GMT"
</Client>
Error fn="error-j2ee"
AddLog fn="flex-log" name="access"
</Object>
I specified a directory called , /nokeep/ above to test in my env.
I want no cache outside in files inside this /nokeep/ directory.
You can change the above directory name per your requirement.
Also, I just added an old date in my expire header above.
Actually , it can be as simple as -1 because RFC
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
said,
" The format is an absolute date and time as defined by HTTP-date in section 3.3.1; it MUST be in RFC 1123 date format:
Expires = "Expires" ":" HTTP-date
An example of its use is
Expires: Thu, 01 Dec 1994 16:00:00 GMT
Note: if a response includes a Cache-Control field with the max-
age directive (see section 14.9.3), that directive overrides the
Expires field.
HTTP/1.1 clients and caches MUST treat other invalid date formats, especially including the value "0", as in the past ( i.e., "already expired" ). "
so, any invalid date format, e.g. -1 , will be considered as in the past as above.
tests:
after the above obj.conf change, then restart,
apple:/export/home/iws6.1sp9/https-jsp/config> ../stop; ../start
a. test the /nokeep folder to see the new Expires header:
apple:/export/home/iws6.1sp9/https-jsp/config> telnet apple.asia 61907
Trying 129.158.175.16...
Connected to apple.asia.sun.com.
Escape character is '^]'.
GET /nokeep/test.html HTTP/1.1
Host: apple.asia.sun.com:61907
HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Tue, 08 Jul 2008 06:51:25 GMT
Content-length: 5
Content-type: text/html
Last-modified: Tue, 08 Jul 2008 05:30:03 GMT
Etag: "5-4872fb5b"
Accept-ranges: bytes
Expires: Thu, 01 Dec 1994 16:00:00 GMT ***
b. then test another folder /keep to see NO such new Expires header:
apple:/export/home/iws6.1sp9/https-jsp/config> telnet apple.asia 61907
Trying 129.158.175.16...
Connected to apple.asia.sun.com.
Escape character is '^]'.
GET /keep/test.html HTTP/1.1
Host: apple.asia.sun.com:61907
HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Tue, 08 Jul 2008 06:52:04 GMT
Content-length: 5
Content-type: text/html
Last-modified: Tue, 08 Jul 2008 05:30:30 GMT
Etag: "5-4872fb76"
Accept-ranges: bytes
However, you can also try other cache-control header in 6.1,
see
http://docs.sun.com/source/817-1831-10/agcontnt.html#wp1003146
However, note the RFC above,
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
"
Note: if a response includes a Cache-Control field with the max-
age directive (see section 14.9.3), that directive overrides the
Expires field.
"
So, if you are using both max-age and Expires above, then max-age will override the Expires , per RFC.
So, another possibility is just to set max-age=0
e.g. I just added
No Store and max-age=0 in /nokeep dir from Admin GUI,
then obj.conf looks like below:
<Object name="default">
.......
Error fn="error-j2ee"
AddLog fn="flex-log" name="access"
<Client uri="*/nokeep/*.*">
Output fn="set-variable" insert-srvhdrs="Expires: Thu, 01 Dec 1994 16:00:00 GMT"
</Client>
</Object>
.......
<Object ppath="/export/home/iws6.1sp9/docs/nokeep/*">
PathCheck fn="set-cache-control" control="no-store,max-age=0"
</Object>
then a telnet test will show:
apple:/export/home/iws6.1sp9/https-jsp/config> telnet apple.asia 61907
Trying 129.158.175.16...
Connected to apple.asia.sun.com.
Escape character is '^]'.
GET /nokeep/test.html HTTP/1.1
Host: apple.asia.sun.com:61907
HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Tue, 08 Jul 2008 07:22:29 GMT
Content-length: 5
Content-type: text/html
Cache-control: no-store,max-age=0 ***
Last-modified: Tue, 08 Jul 2008 05:30:03 GMT
Etag: "5-4872fb5b"
Accept-ranges: bytes
Expires: Thu, 01 Dec 1994 16:00:00 GMT ***
( note the new "Cache-control: no-store,max-age=0" and Expires header,
and per RFC, max-age will override the Expires field.
)
and a hit to /keep dir will not have any new headers,
e.g.
apple:/export/home/iws6.1sp9/https-jsp/config> telnet apple.asia 61907
Trying 129.158.175.16...
Connected to apple.asia.sun.com.
Escape character is '^]'.
GET /keep/ HTTP/1.1
Host: apple.asia.sun.com:61907
HTTP/1.1 200 OK
Server: Sun-ONE-Web-Server/6.1
Date: Tue, 08 Jul 2008 07:24:20 GMT
Content-length: 447
Content-type: text/html
Last-modified: Tue, 08 Jul 2008 05:30:30 GMT
Etag: "1bf-4872fb76"
Accept-ranges: bytes