📄 handlerrequest.java
字号:
ex.printStackTrace(); } } // -------------------- Incoming message -------------------- String requiredSecret=null; int bodyNote; int tmpBufNote; int secretNote; boolean decoded=true; boolean tomcatAuthentication=true; public int invoke(Msg msg, MsgContext ep ) throws IOException { int type=msg.getByte(); MessageBytes tmpMB=(MessageBytes)ep.getNote( tmpBufNote ); if( tmpMB==null ) { tmpMB=new MessageBytes(); ep.setNote( tmpBufNote, tmpMB); } if( log.isDebugEnabled() ) log.debug( "Handling " + type ); switch( type ) { case JK_AJP13_FORWARD_REQUEST: try { decodeRequest( msg, ep, tmpMB ); } catch( Exception ex ) { log.error( "Error decoding request ", ex ); msg.dump( "Incomming message"); return ERROR; } if( requiredSecret != null ) { String epSecret=(String)ep.getNote( secretNote ); if( epSecret==null || ! requiredSecret.equals( epSecret ) ) return ERROR; } /* XXX it should be computed from request, by workerEnv */ if(log.isDebugEnabled() ) log.debug("Calling next " + next.getName() + " " + next.getClass().getName()); int err= next.invoke( msg, ep ); if( log.isDebugEnabled() ) log.debug( "Invoke returned " + err ); return err; case JK_AJP13_SHUTDOWN: String epSecret=null; if( msg.getLen() > 3 ) { // we have a secret msg.getBytes( tmpMB ); epSecret=tmpMB.toString(); } if( requiredSecret != null && requiredSecret.equals( epSecret ) ) { if( log.isDebugEnabled() ) log.debug("Received wrong secret, no shutdown "); return ERROR; } // XXX add isSameAddress check JkHandler ch=ep.getSource(); if( ch instanceof ChannelSocket ) { if( ! ((ChannelSocket)ch).isSameAddress(ep) ) { log.error("Shutdown request not from 'same address' "); return ERROR; } } // forward to the default handler - it'll do the shutdown next.invoke( msg, ep ); log.info("Exiting"); System.exit(0); return OK; default: System.err.println("Unknown message " + type ); msg.dump("Unknown message" ); } return OK; } private int decodeRequest( Msg msg, MsgContext ep, MessageBytes tmpMB ) throws IOException { // FORWARD_REQUEST handler Request req=(Request)ep.getRequest(); if( req==null ) { req=new Request(); Response res=new Response(); req.setResponse(res); ep.setRequest( req ); } JkInputStream jkBody=(JkInputStream)ep.getNote( bodyNote ); if( jkBody==null ) { jkBody=new JkInputStream(); jkBody.setMsgContext( ep ); ep.setNote( bodyNote, jkBody ); } jkBody.recycle(); // Translate the HTTP method code to a String. byte methodCode = msg.getByte(); String mName=methodTransArray[(int)methodCode - 1]; req.method().setString(mName); msg.getBytes(req.protocol()); msg.getBytes(req.requestURI()); msg.getBytes(req.remoteAddr()); msg.getBytes(req.remoteHost()); msg.getBytes(req.serverName()); req.setServerPort(msg.getInt()); boolean isSSL = msg.getByte() != 0; if( isSSL ) { // XXX req.setSecure( true ); req.scheme().setString("https"); } decodeHeaders( ep, msg, req, tmpMB ); decodeAttributes( ep, msg, req, tmpMB );// if(req.getSecure() ) {// req.setScheme(req.SCHEME_HTTPS);// } // set cookies on request now that we have all headers req.getCookies().setHeaders(req.getMimeHeaders()); // Check to see if there should be a body packet coming along // immediately after int cl=req.getContentLength(); if(cl > 0) { jkBody.setContentLength( cl ); jkBody.receive(); } if (log.isTraceEnabled()) { log.trace(req.toString()); } return OK; } private int decodeAttributes( MsgContext ep, Msg msg, Request req, MessageBytes tmpMB) { boolean moreAttr=true; while( moreAttr ) { byte attributeCode=msg.getByte(); if( attributeCode == SC_A_ARE_DONE ) return 200; /* Special case ( XXX in future API make it separate type !) */ if( attributeCode == SC_A_SSL_KEY_SIZE ) { // Bug 1326: it's an Integer. req.setAttribute(SSLSupport.KEY_SIZE_KEY, new Integer( msg.getInt())); //Integer.toString(msg.getInt())); } if( attributeCode == SC_A_REQ_ATTRIBUTE ) { // 2 strings ???... msg.getBytes( tmpMB ); String n=tmpMB.toString(); msg.getBytes( tmpMB ); String v=tmpMB.toString(); req.setAttribute(n, v ); } // 1 string attributes switch(attributeCode) { case SC_A_CONTEXT : msg.getBytes( tmpMB ); // nothing break; case SC_A_SERVLET_PATH : msg.getBytes( tmpMB ); // nothing break; case SC_A_REMOTE_USER : if( tomcatAuthentication ) { // ignore server msg.getBytes( tmpMB ); } else { msg.getBytes(req.getRemoteUser()); } break; case SC_A_AUTH_TYPE : msg.getBytes(req.getAuthType()); break; case SC_A_QUERY_STRING : msg.getBytes(req.queryString()); break; case SC_A_JVM_ROUTE : msg.getBytes(req.instanceId()); break; case SC_A_SSL_CERT : req.scheme().setString( "https" ); // Transform the string into certificate. MessageBytes tmpMB2 = new MessageBytes(); msg.getBytes(tmpMB2); // SSL certificate extraction is costy, moved to JkCoyoteHandler req.setNote(WorkerEnv.SSL_CERT_NOTE, tmpMB2); break; case SC_A_SSL_CIPHER : req.scheme().setString( "https" ); msg.getBytes(tmpMB); req.setAttribute(SSLSupport.CIPHER_SUITE_KEY, tmpMB.toString()); break; case SC_A_SSL_SESSION : req.scheme().setString( "https" ); msg.getBytes(tmpMB); req.setAttribute(SSLSupport.SESSION_ID_KEY, tmpMB.toString()); break; case SC_A_SECRET : msg.getBytes(tmpMB); String secret=tmpMB.toString(); log.info("Secret: " + secret ); // endpoint note ep.setNote( secretNote, secret ); break; default: break; // ignore, we don't know about it - backward compat } } return 200; } private void decodeHeaders( MsgContext ep, Msg msg, Request req, MessageBytes tmpMB ) { // Decode headers MimeHeaders headers = req.getMimeHeaders(); int hCount = msg.getInt(); for(int i = 0 ; i < hCount ; i++) { String hName = null; // Header names are encoded as either an integer code starting // with 0xA0, or as a normal string (in which case the first // two bytes are the length). int isc = msg.peekInt(); int hId = isc & 0xFF; MessageBytes vMB=null; isc &= 0xFF00; if(0xA000 == isc) { msg.getInt(); // To advance the read position hName = headerTransArray[hId - 1]; vMB=headers.addValue( hName ); } else { // reset hId -- if the header currently being read // happens to be 7 or 8 bytes long, the code below // will think it's the content-type header or the // content-length header - SC_REQ_CONTENT_TYPE=7, // SC_REQ_CONTENT_LENGTH=8 - leading to unexpected // behaviour. see bug 5861 for more information. hId = -1; msg.getBytes( tmpMB ); ByteChunk bc=tmpMB.getByteChunk(); //hName=tmpMB.toString(); // vMB=headers.addValue( hName ); vMB=headers.addValue( bc.getBuffer(), bc.getStart(), bc.getLength() ); } msg.getBytes(vMB); if (hId == SC_REQ_CONTENT_LENGTH || tmpMB.equalsIgnoreCase("Content-Length")) { // just read the content-length header, so set it int contentLength = (vMB == null) ? -1 : vMB.getInt(); req.setContentLength(contentLength); } else if (hId == SC_REQ_CONTENT_TYPE || tmpMB.equalsIgnoreCase("Content-Type")) { // just read the content-type header, so set it ByteChunk bchunk = vMB.getByteChunk(); req.contentType().setBytes(bchunk.getBytes(), bchunk.getOffset(), bchunk.getLength()); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -