Search in sources :

Example 71 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat by apache.

the class ApplicationHttpRequest method mergeParameters.

// ------------------------------------------------------ Private Methods
/**
 * Merge the parameters from the saved query parameter string (if any), and
 * the parameters already present on this request (if any), such that the
 * parameter values from the query string show up first if there are
 * duplicate parameter names.
 */
private void mergeParameters() {
    if ((queryParamString == null) || (queryParamString.length() < 1)) {
        return;
    }
    // Parse the query string from the dispatch target
    Parameters paramParser = new Parameters();
    MessageBytes queryMB = MessageBytes.newInstance();
    queryMB.setString(queryParamString);
    // TODO
    // - Should only use body encoding if useBodyEncodingForURI is true
    // - Otherwise, should use URIEncoding
    // - The problem is that the connector is not available...
    // - To add to the fun, the URI default changed in Servlet 4.0 to UTF-8
    String encoding = getCharacterEncoding();
    Charset charset = null;
    if (encoding != null) {
        try {
            charset = B2CConverter.getCharset(encoding);
            queryMB.setCharset(charset);
        } catch (UnsupportedEncodingException e) {
            // Fall-back to default (ISO-8859-1)
            charset = StandardCharsets.ISO_8859_1;
        }
    }
    paramParser.setQuery(queryMB);
    paramParser.setQueryStringCharset(charset);
    paramParser.handleQueryParameters();
    // Insert the additional parameters from the dispatch target
    Enumeration<String> dispParamNames = paramParser.getParameterNames();
    while (dispParamNames.hasMoreElements()) {
        String dispParamName = dispParamNames.nextElement();
        String[] dispParamValues = paramParser.getParameterValues(dispParamName);
        String[] originalValues = parameters.get(dispParamName);
        if (originalValues == null) {
            parameters.put(dispParamName, dispParamValues);
            continue;
        }
        parameters.put(dispParamName, mergeValues(dispParamValues, originalValues));
    }
}
Also used : Parameters(org.apache.tomcat.util.http.Parameters) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) Charset(java.nio.charset.Charset) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 72 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat by apache.

the class ApplicationPushBuilder method push.

@Override
public void push() {
    if (path == null) {
        throw new IllegalStateException(sm.getString("pushBuilder.noPath"));
    }
    org.apache.coyote.Request pushTarget = new org.apache.coyote.Request();
    pushTarget.method().setString(method);
    // The next three are implied by the Javadoc getPath()
    pushTarget.serverName().setString(baseRequest.getServerName());
    pushTarget.setServerPort(baseRequest.getServerPort());
    pushTarget.scheme().setString(baseRequest.getScheme());
    // Copy headers
    for (Map.Entry<String, List<String>> header : headers.entrySet()) {
        for (String value : header.getValue()) {
            pushTarget.getMimeHeaders().addValue(header.getKey()).setString(value);
        }
    }
    // Path and query string
    int queryIndex = path.indexOf('?');
    String pushPath;
    String pushQueryString = null;
    if (queryIndex > -1) {
        pushPath = path.substring(0, queryIndex);
        if (queryIndex + 1 < path.length()) {
            pushQueryString = path.substring(queryIndex + 1);
        }
    } else {
        pushPath = path;
    }
    // Session ID (do this before setting the path since it may change it)
    if (sessionId != null) {
        if (addSessionPathParameter) {
            pushPath = pushPath + ";" + sessionPathParameterName + "=" + sessionId;
            pushTarget.addPathParameter(sessionPathParameterName, sessionId);
        }
        if (addSessionCookie) {
            String sessionCookieHeader = sessionCookieName + "=" + sessionId;
            MessageBytes mb = pushTarget.getMimeHeaders().getValue("cookie");
            if (mb == null) {
                mb = pushTarget.getMimeHeaders().addValue("cookie");
                mb.setString(sessionCookieHeader);
            } else {
                mb.setString(mb.getString() + ";" + sessionCookieHeader);
            }
        }
    }
    // Undecoded path - just %nn encoded
    pushTarget.requestURI().setString(pushPath);
    pushTarget.decodedURI().setString(decode(pushPath, catalinaRequest.getConnector().getURICharset()));
    // Query string
    if (pushQueryString == null && queryString != null) {
        pushTarget.queryString().setString(queryString);
    } else if (pushQueryString != null && queryString == null) {
        pushTarget.queryString().setString(pushQueryString);
    } else if (pushQueryString != null && queryString != null) {
        pushTarget.queryString().setString(pushQueryString + "&" + queryString);
    }
    // Authorization
    if (userName != null) {
        pushTarget.getRemoteUser().setString(userName);
        pushTarget.setRemoteUserNeedsAuthorization(true);
    }
    coyoteRequest.action(ActionCode.PUSH_REQUEST, pushTarget);
    // Reset for next call to this method
    path = null;
    headers.remove("if-none-match");
    headers.remove("if-modified-since");
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Request(org.apache.catalina.connector.Request) MessageBytes(org.apache.tomcat.util.buf.MessageBytes) ArrayList(java.util.ArrayList) List(java.util.List) CaseInsensitiveKeyMap(org.apache.tomcat.util.collections.CaseInsensitiveKeyMap) Map(java.util.Map)

Example 73 with MessageBytes

use of org.apache.tomcat.util.buf.MessageBytes in project tomcat by apache.

the class HealthCheckValve method invoke.

@Override
public void invoke(Request request, Response response) throws IOException, ServletException {
    MessageBytes urlMB = context ? request.getRequestPathMB() : request.getDecodedRequestURIMB();
    if (urlMB.equals(path)) {
        response.setContentType("application/json");
        if (!checkContainersAvailable || isAvailable(getContainer())) {
            response.getOutputStream().print(UP);
        } else {
            response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            response.getOutputStream().print(DOWN);
        }
    } else {
        getNext().invoke(request, response);
    }
}
Also used : MessageBytes(org.apache.tomcat.util.buf.MessageBytes)

Aggregations

MessageBytes (org.apache.tomcat.util.buf.MessageBytes)73 MimeHeaders (org.apache.tomcat.util.http.MimeHeaders)15 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)14 IOException (java.io.IOException)11 Test (org.junit.Test)11 Context (org.apache.catalina.Context)10 LoggingBaseTest (org.apache.catalina.startup.LoggingBaseTest)8 Pattern (java.util.regex.Pattern)6 AbstractEndpoint (org.apache.tomcat.util.net.AbstractEndpoint)6 Principal (java.security.Principal)5 Wrapper (org.apache.catalina.Wrapper)5 CharChunk (org.apache.tomcat.util.buf.CharChunk)5 Host (org.apache.catalina.Host)4 ServletException (jakarta.servlet.ServletException)3 Cookie (jakarta.servlet.http.Cookie)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashSet (java.util.HashSet)3 ServletException (javax.servlet.ServletException)3 Cookie (javax.servlet.http.Cookie)3 Container (org.apache.catalina.Container)3