@Nullable private Request convertRequest(ServiceRequestContext ctx, AggregatedHttpMessage req) throws Throwable { final String mappedPath = ctx.mappedPath(); final Request coyoteReq = new Request(); coyoteReq.scheme().setString(req.scheme()); coyoteReq.remoteAddr().setString(remoteAddr.getAddress().getHostAddress()); coyoteReq.remoteHost().setString(remoteAddr.getHostString()); coyoteReq.setRemotePort(remoteAddr.getPort()); coyoteReq.localAddr().setString(localAddr.getAddress().getHostAddress()); coyoteReq.localName().setString(hostName()); coyoteReq.setLocalPort(localAddr.getPort()); final int colonPos = hostHeader.indexOf(':'); if (colonPos < 0) { coyoteReq.serverName().setString(hostHeader); } else { coyoteReq.serverName().setString(hostHeader.substring(0, colonPos)); try { final int port = Integer.parseInt(hostHeader.substring(colonPos + 1)); coyoteReq.method().setString(method.name()); coyoteReq.requestURI().setBytes(uriBytes, 0, uriBytes.length); coyoteReq.queryString().setString(ctx.query());
@Nullable private static String toHeaderValue(MessageBytes value) { switch (value.getType()) { case MessageBytes.T_BYTES: { final ByteChunk chunk = value.getByteChunk(); return new String(chunk.getBuffer(), chunk.getOffset(), chunk.getLength(), StandardCharsets.US_ASCII); } case MessageBytes.T_CHARS: { final CharChunk chunk = value.getCharChunk(); return new String(chunk.getBuffer(), chunk.getOffset(), chunk.getLength()); } case MessageBytes.T_STR: { return value.getString(); } } return null; } }
private static HttpHeaders convertResponse(Response coyoteRes) { final HttpHeaders headers = HttpHeaders.of(HttpStatus.valueOf(coyoteRes.getStatus())); final String contentType = coyoteRes.getContentType(); if (contentType != null && !contentType.isEmpty()) { headers.set(HttpHeaderNames.CONTENT_TYPE, contentType); } final long contentLength = coyoteRes.getBytesWritten(true); // 'true' will trigger flush. final String method = coyoteRes.getRequest().method().toString(); if (!"HEAD".equals(method)) { headers.setLong(HttpHeaderNames.CONTENT_LENGTH, contentLength); } final MimeHeaders cHeaders = coyoteRes.getMimeHeaders(); final int numHeaders = cHeaders.size(); for (int i = 0; i < numHeaders; i++) { final AsciiString name = toHeaderName(cHeaders.getName(i)); if (name == null) { continue; } final String value = toHeaderValue(cHeaders.getValue(i)); if (value == null) { continue; } headers.add(name.toLowerCase(), value); } return headers; }
@Override public boolean containsValue(Object value) { if (value instanceof String) { MessageBytes needle = MessageBytes.newInstance(); needle.setString((String) value); for (int i = 0; i < this.headers.size(); i++) { if (this.headers.getValue(i).equals(needle)) { return true; } } } return false; }
request.getCoyoteRequest().getMimeHeaders() .getValue("authorization"); authorization.toBytes(); ByteChunk authorizationBC = authorization.getByteChunk(); if (authorizationBC.startsWithIgnoreCase("basic ", 0)) { authorizationBC.setOffset(authorizationBC.getOffset() + 6); CharChunk authorizationCC = authorization.getCharChunk(); Base64.decode(authorizationBC, authorizationCC); try { MessageBytes authenticate = response.getCoyoteResponse().getMimeHeaders() .addValue(AUTHENTICATE_BYTES, 0, AUTHENTICATE_BYTES.length); CharChunk authenticateCC = authenticate.getCharChunk(); authenticateCC.append("Basic realm=\""); authenticateCC.append((realmName == null) ? "<unspecified>" : realmName); authenticateCC.append('\"'); authenticate.toChars(); response.sendError(HttpServletResponse.SC_UNAUTHORIZED); return new AuthResult(TomcatAuthStatus.SEND_CONTINUE, null);
private void doConnectorAuthenticationAuthorization(org.apache.coyote.Request req, Request request) { String username = req.getRemoteUser().toString(); if (username != null) { if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.authenticate", username)); if (req.getRemoteUserNeedsAuthorization()) { Authenticator authenticator = request.getContext().getAuthenticator(); if (!(authenticator instanceof AuthenticatorBase)) { if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.authorize", username)); request.setUserPrincipal( request.getContext().getRealm().authenticate(username)); String authtype = req.getAuthType().toString(); if (authtype != null) { request.setAuthType(authtype);
public void processParameters( MessageBytes data, String encoding ) { if( data==null || data.isNull() || data.getLength() <= 0 ) return; if( data.getType() != MessageBytes.T_BYTES ) { data.toBytes(); } ByteChunk bc=data.getByteChunk(); processParameters( bc.getBytes(), bc.getOffset(), bc.getLength(), encoding); }
rframe.associated = 0; MimeHeaders headers = response.getMimeHeaders(); for (int i = 0; i < headers.size(); i++) { MessageBytes mb = headers.getName(i); mb.toBytes(); ByteChunk bc = mb.getByteChunk(); byte[] bb = bc.getBuffer(); for (int j = bc.getStart(); j < bc.getEnd(); j++) { bb[j] = (byte) Ascii.toLower(bb[j]); rframe.headerName(bc.getBuffer(), bc.getStart(), bc.getLength()); mb = headers.getValue(i); mb.toBytes(); bc = mb.getByteChunk(); rframe.headerValue(bc.getBuffer(), bc.getStart(), bc.getLength()); if (response.getStatus() == 0) { rframe.addHeader(SpdyFrame.STATUS, SpdyFrame.OK200); } else { && HttpMessages.isSafeInHttpHeader(response.getMessage())) { message = response.getMessage();
request.getCoyoteRequest().getMimeHeaders() .getValue("authorization"); authorization.toBytes(); ByteChunk authorizationBC = authorization.getByteChunk(); BasicCredentials credentials = null; try { if (log.isDebugEnabled()) { log.debug("Invalid Authorization" + iae.getMessage());
@Override public void parseCookieHeader(MimeHeaders headers, ServerCookies serverCookies) { if (headers == null) { // nothing to process return; } // process each "cookie" header int pos = headers.findHeader("Cookie", 0); while (pos >= 0) { MessageBytes cookieValue = headers.getValue(pos); if (cookieValue != null && !cookieValue.isNull() ) { if (cookieValue.getType() != MessageBytes.T_BYTES ) { Exception e = new Exception(); // TODO: Review this in light of HTTP/2 log.debug("Cookies: Parsing cookie as String. Expected bytes.", e); cookieValue.toBytes(); } if (log.isDebugEnabled()) { log.debug("Cookies: Parsing b[]: " + cookieValue.toString()); } ByteChunk bc = cookieValue.getByteChunk(); processCookieHeader(bc.getBytes(), bc.getOffset(), bc.getLength(), serverCookies); } // search from the next position pos = headers.findHeader("Cookie", ++pos); } }
protected void parseHost(MessageBytes valueMB) { if (valueMB == null || valueMB.isNull()) { populateHost(); return; ByteChunk valueBC = valueMB.getByteChunk(); byte[] valueB = valueBC.getBytes(); int valueL = valueBC.getLength(); int valueS = valueBC.getStart(); if (hostNameC.length < valueL) { hostNameC = new char[valueL]; request.setServerPort(port); hostNameC[i] = (char) valueB[i + valueS]; request.serverName().setChars(hostNameC, 0, valueL); String message = sm.getString("abstractProcessor.hostInvalid", valueMB.toString()); switch (logMode) { case INFO_THEN_DEBUG: getLog().info(message, e); break; case DEBUG: getLog().debug(message, e); response.setStatus(400); setErrorState(ErrorState.CLOSE_CLEAN, e);
Response res=req.getResponse(); if( log.isDebugEnabled() ) log.debug( "Invoke " + req + " " + res + " " + req.requestURI().toString()); res.setNote( epNote, ep ); ep.setStatus( MsgContext.JK_STATUS_HEAD ); RequestInfo rp = req.getRequestProcessor(); rp.setStage(Constants.STAGE_SERVICE); try { adapter.service( req, res ); } catch( Exception ex ) { log.info("Error servicing request " + req,ex); res.finish(); req.updateCounters(); req.recycle(); res.recycle(); ep.recycle(); if( ep.getStatus() == MsgContext.JK_STATUS_ERROR ) {
final void push(Request request) throws IOException { // Can only push when supported and from a peer initiated stream if (!isPushSupported() || getIdentifier().intValue() % 2 == 0) { return; } // Set the special HTTP/2 headers request.getMimeHeaders().addValue(":method").duplicate(request.method()); request.getMimeHeaders().addValue(":scheme").duplicate(request.scheme()); StringBuilder path = new StringBuilder(request.requestURI().toString()); if (!request.queryString().isNull()) { path.append('?'); path.append(request.queryString().toString()); } request.getMimeHeaders().addValue(":path").setString(path.toString()); // Authority needs to include the port only if a non-standard port is // being used. if (!(request.scheme().equals("http") && request.getServerPort() == 80) && !(request.scheme().equals("https") && request.getServerPort() == 443)) { request.getMimeHeaders().addValue(":authority").setString( request.serverName().getString() + ":" + request.getServerPort()); } else { request.getMimeHeaders().addValue(":authority").duplicate(request.serverName()); } push(handler, request, this); }
/** * Reject the request that was denied by this valve. * <p>If <code>invalidAuthenticationWhenDeny</code> is true * and the context has <code>preemptiveAuthentication</code> * set, set an invalid authorization header to trigger basic auth. * * @param request The servlet request to be processed * @param response The servlet response to be processed * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ protected void denyRequest(Request request, Response response) throws IOException, ServletException { if (invalidAuthenticationWhenDeny) { Context context = request.getContext(); if (context != null && context.getPreemptiveAuthentication()) { if (request.getCoyoteRequest().getMimeHeaders().getValue("authorization") == null) { request.getCoyoteRequest().getMimeHeaders().addValue("authorization").setString("invalid"); } getNext().invoke(request, response); return; } } response.sendError(denyStatus); }
/** * Read bytes into the specified chunk. */ public int doRead(ByteChunk chunk, Request req ) throws IOException { if (endOfStream) { return -1; } if (first && req.getContentLength() > 0) { // Handle special first-body-chunk if (!receive()) { return 0; } } else if (empty) { if (!refillReadBuffer()) { return -1; } } ByteChunk bc = bodyBytes.getByteChunk(); chunk.setBytes(bc.getBuffer(), bc.getStart(), bc.getLength()); empty = true; return chunk.getLength(); }
/* */ public String getRemoteHost() /* */ { /* 1181 */ if (this.remoteHost == null) { /* 1182 */ if (!this.connector.getEnableLookups()) { /* 1183 */ this.remoteHost = getRemoteAddr(); /* */ } else { /* 1185 */ this.coyoteRequest.action(ActionCode.ACTION_REQ_HOST_ATTRIBUTE, this.coyoteRequest); /* */ /* 1187 */ this.remoteHost = this.coyoteRequest.remoteHost().toString(); /* */ } /* */ } /* 1190 */ return this.remoteHost; /* */ } /* */
/** * Read bytes into the specified chunk. */ public int doRead(ByteChunk chunk, Request req ) throws IOException { if (endOfStream) { return -1; } if (first && req.getContentLengthLong() > 0) { // Handle special first-body-chunk if (!receive()) { return 0; } } else if (empty) { if (!refillReadBuffer()) { return -1; } } ByteChunk bc = bodyBytes.getByteChunk(); chunk.setBytes(bc.getBuffer(), bc.getStart(), bc.getLength()); empty = true; return chunk.getLength(); }
/** Called by the processor before recycling the request. It'll collect * statistic information. */ void updateCounters() { bytesReceived+=req.getBytesRead(); bytesSent+=req.getResponse().getContentWritten(); requestCount++; if( req.getResponse().getStatus() >=400 ) errorCount++; long t0=req.getStartTime(); long t1=System.currentTimeMillis(); long time=t1-t0; this.lastRequestProcessingTime = time; processingTime+=time; if( maxTime < time ) { maxTime=time; maxRequestUri=req.requestURI().toString(); } }
private void findNext() { next=null; for(; pos< size; pos++ ) { MessageBytes n1=headers.getName( pos ); if( n1.equalsIgnoreCase( name )) { next=headers.getValue( pos ); break; } } pos++; }
/* */ public String[] getHeaderNames() /* */ { /* 859 */ MimeHeaders headers = this.coyoteResponse.getMimeHeaders(); /* 860 */ int n = headers.size(); /* 861 */ String[] result = new String[n]; /* 862 */ for (int i = 0; i < n; i++) { /* 863 */ result[i] = headers.getName(i).toString(); /* */ } /* 865 */ return result; /* */ } /* */