1 23 24 package com.sun.enterprise.web.connector.grizzly.handlers; 25 import com.sun.enterprise.web.connector.grizzly.Constants; 26 import com.sun.enterprise.web.connector.grizzly.FileCacheFactory; 27 import com.sun.enterprise.web.connector.grizzly.Handler; 28 import com.sun.enterprise.web.connector.grizzly.FileCache; 29 import com.sun.enterprise.web.connector.grizzly.SelectorThread; 30 import java.io.IOException ; 31 import java.nio.channels.SocketChannel ; 32 33 import org.apache.coyote.Request; 34 import org.apache.coyote.Response; 35 import org.apache.coyote.tomcat5.CoyoteConnector; 36 import org.apache.coyote.tomcat5.CoyoteRequest; 37 import org.apache.coyote.tomcat5.CoyoteAdapter; 38 import org.apache.tomcat.util.buf.Ascii; 39 import org.apache.tomcat.util.buf.ByteChunk; 40 import org.apache.tomcat.util.buf.MessageBytes; 41 import org.apache.tomcat.util.http.MimeHeaders; 42 47 public class NoParsingHandler implements Handler<Request> { 48 49 52 private SocketChannel socketChannel; 53 54 55 58 protected FileCache fileCache; 59 60 61 63 64 public NoParsingHandler(int port){ 65 fileCache = 66 FileCacheFactory.getFactory(port).getFileCache(); 67 } 68 69 70 73 public void attachChannel(SocketChannel socketChannel){ 74 this.socketChannel = socketChannel; 75 } 76 77 78 82 public int handle(Request request, int handlerCode) throws IOException { 83 if ( socketChannel == null || !FileCacheFactory.isEnabled()) 84 return Handler.CONTINUE; 85 86 if ( fileCache == null && handlerCode != Handler.RESPONSE_PROCEEDED){ 88 return Handler.CONTINUE; 89 } 90 91 if ( handlerCode == Handler.RESPONSE_PROCEEDED ){ 92 CoyoteRequest cr = 93 (CoyoteRequest)request.getNote(CoyoteAdapter.ADAPTER_NOTES); 94 if ( cr != null && cr.getWrapper() != null){ 97 98 String mappedServlet = cr.getWrapper().getName(); 99 100 if ( !mappedServlet.equals(FileCache.DEFAULT_SERVLET_NAME) ) 101 return Handler.CONTINUE; 102 103 if ( cr.getContext().findConstraints().length == 0 104 && cr.getContext().findFilterDefs().length == 0 ){ 105 106 if (!fileCache.isEnabled()) return Handler.CONTINUE; 107 108 String docroot; 109 if ( cr.getContextPath().equals("") ){ 110 docroot = cr.getContext().getDocBase(); 111 } else { 112 docroot = SelectorThread.getWebAppRootPath(); 113 } 114 String requestURI = cr.getRequestURI(); 115 Response response = cr.getCoyoteRequest().getResponse(); 116 MimeHeaders headers = response.getMimeHeaders(); 117 boolean xPoweredBy = ( 118 (CoyoteConnector)cr.getConnector()).isXpoweredBy(); 119 120 fileCache.add(mappedServlet,docroot,requestURI,headers, 121 xPoweredBy); 122 } 123 } 124 } else if ( handlerCode == Handler.REQUEST_LINE_PARSED ) { 125 ByteChunk bc = request.requestURI().getByteChunk(); 126 127 if ( fileCache.sendCache(bc.getBytes(),bc.getStart(), 128 bc.getLength(),socketChannel,keepAlive(request)) ){ 129 return Handler.BREAK; 130 } 131 } 132 return Handler.CONTINUE; 133 } 134 135 136 139 private boolean keepAlive(Request request){ 140 MimeHeaders headers = request.getMimeHeaders(); 141 142 MessageBytes connectionValueMB = headers.getValue("connection"); 144 if (connectionValueMB != null) { 145 ByteChunk connectionValueBC = connectionValueMB.getByteChunk(); 146 if (findBytes(connectionValueBC, Constants.CLOSE_BYTES) != -1) { 147 return false; 148 } else if (findBytes(connectionValueBC, 149 Constants.KEEPALIVE_BYTES) != -1) { 150 return true; 151 } 152 } 153 return true; 154 } 155 156 157 161 protected int findBytes(ByteChunk bc, byte[] b) { 162 163 byte first = b[0]; 164 byte[] buff = bc.getBuffer(); 165 int start = bc.getStart(); 166 int end = bc.getEnd(); 167 168 int srcEnd = b.length; 170 171 for (int i = start; i <= (end - srcEnd); i++) { 172 if (Ascii.toLower(buff[i]) != first) continue; 173 int myPos = i+1; 175 for (int srcPos = 1; srcPos < srcEnd; ) { 176 if (Ascii.toLower(buff[myPos++]) != b[srcPos++]) 177 break; 178 if (srcPos == srcEnd) return i - start; } 180 } 181 return -1; 182 } 183 } 184 | Popular Tags |