Search in sources :

Example 1 with ByteChunk

use of org.glassfish.grizzly.http.util.ByteChunk in project Payara by payara.

the class Request method convertURI.

// START CR 6309511
/**
 * Character conversion of the URI.
 */
protected void convertURI(MessageBytes uri) throws Exception {
    ByteChunk bc = uri.getByteChunk();
    CharChunk cc = uri.getCharChunk();
    int length = bc.getLength();
    cc.allocate(length, -1);
    String enc = connector.getURIEncoding();
    if (enc != null && !enc.isEmpty() && !Globals.ISO_8859_1_ENCODING.equalsIgnoreCase(enc)) {
        B2CConverter conv = getURIConverter();
        try {
            if (conv == null) {
                conv = new B2CConverter(enc);
                setURIConverter(conv);
            }
        } catch (IOException e) {
            // Ignore
            log.log(Level.SEVERE, LogFacade.INVALID_URI_ENCODING);
            connector.setURIEncoding(null);
        }
        if (conv != null) {
            try {
                conv.convert(bc, cc, cc.getBuffer().length - cc.getEnd());
                uri.setChars(cc.getBuffer(), cc.getStart(), cc.getLength());
                return;
            } catch (IOException e) {
                log.log(Level.SEVERE, LogFacade.INVALID_URI_CHAR_ENCODING);
                cc.recycle();
            }
        }
    }
    // Default encoding: fast conversion
    byte[] bbuf = bc.getBuffer();
    char[] cbuf = cc.getBuffer();
    int start = bc.getStart();
    for (int i = 0; i < length; i++) {
        cbuf[i] = (char) (bbuf[i + start] & 0xff);
    }
    uri.setChars(cbuf, 0, length);
}
Also used : B2CConverter(org.glassfish.grizzly.http.util.B2CConverter) ByteChunk(org.glassfish.grizzly.http.util.ByteChunk) IOException(java.io.IOException) CharChunk(org.glassfish.grizzly.http.util.CharChunk)

Example 2 with ByteChunk

use of org.glassfish.grizzly.http.util.ByteChunk in project Payara by payara.

the class FormAuthenticator method restoreRequest.

/**
 * Restore the original request from information stored in our session.
 * If the original request is no longer present (because the session
 * timed out), return <code>false</code>; otherwise, return
 * <code>true</code>.
 *
 * @param request The request to be restored
 * @param session The session containing the saved information
 */
protected boolean restoreRequest(HttpRequest request, Session session) throws IOException {
    // Retrieve and remove the SavedRequest object from our session
    SavedRequest saved = (SavedRequest) session.getNote(Constants.FORM_REQUEST_NOTE);
    /*
         * PWC 6463046:
         * Do not remove the saved request: It will be needed again in case
         * another j_security_check is sent. The saved request will be
         * purged when the session expires.
        session.removeNote(Constants.FORM_REQUEST_NOTE);
         */
    session.removeNote(Constants.FORM_PRINCIPAL_NOTE);
    if (saved == null)
        return (false);
    // Swallow any request body since we will be replacing it
    // Need to do this before headers are restored as AJP connector uses
    // content length header to determine how much data needs to be read for
    // request body
    byte[] buffer = new byte[4096];
    InputStream is = request.getStream();
    while (is.read(buffer) >= 0) {
    // Ignore request body
    }
    // Modify our current request to reflect the original one
    request.clearCookies();
    Iterator<Cookie> cookies = saved.getCookies();
    while (cookies.hasNext()) {
        request.addCookie(cookies.next());
    }
    String method = saved.getMethod();
    boolean cachable = "GET".equalsIgnoreCase(method) || "HEAD".equalsIgnoreCase(method);
    request.clearHeaders();
    Iterator<String> names = saved.getHeaderNames();
    while (names.hasNext()) {
        String name = names.next();
        // BZ 43687
        if (!("If-Modified-Since".equalsIgnoreCase(name) || (cachable && "If-None-Match".equalsIgnoreCase(name)))) {
            Iterator<String> values = saved.getHeaderValues(name);
            while (values.hasNext()) {
                request.addHeader(name, values.next());
            }
        }
    }
    request.setContentLength(saved.getContentLenght());
    request.clearLocales();
    Iterator<Locale> locales = saved.getLocales();
    while (locales.hasNext()) {
        request.addLocale(locales.next());
    }
    request.clearParameters();
    // setQueryStringEncoding is done inside request.clearParameters
    ByteChunk body = saved.getBody();
    if (body != null) {
        byte[] tempData = body.getBytes();
        // tempData is a buffer with reserved extra space
        // we must keep only the valid data here
        byte[] data = new byte[body.getLength()];
        System.arraycopy(tempData, body.getStart(), data, 0, data.length);
        request.replayPayload(data);
        // If no content type specified, use default for POST
        String savedContentType = saved.getContentType();
        if (savedContentType == null && "POST".equalsIgnoreCase(method)) {
            savedContentType = "application/x-www-form-urlencoded";
        }
        request.setContentType(savedContentType);
    }
    request.setMethod(method);
    request.setQueryString(saved.getQueryString());
    return true;
}
Also used : Cookie(javax.servlet.http.Cookie) ByteChunk(org.glassfish.grizzly.http.util.ByteChunk) InputStream(java.io.InputStream)

Example 3 with ByteChunk

use of org.glassfish.grizzly.http.util.ByteChunk in project Payara by payara.

the class FormAuthenticator method saveRequest.

/**
 * Save the original request information into our session.
 *
 * @param request The request to be saved
 * @param session The session to contain the saved information
 */
// START Apache bug 36136: Refactor the login and error page forward
// private void saveRequest(HttpRequest request, Session session) {
protected void saveRequest(HttpRequest request, Session session) throws IOException {
    // END Apache bug 36136: Refactor the login and error page forward
    // Create and populate a SavedRequest object for this request
    HttpServletRequest hreq = (HttpServletRequest) request.getRequest();
    SavedRequest saved = new SavedRequest();
    Cookie[] cookies = hreq.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) saved.addCookie(cookies[i]);
    }
    Enumeration names = hreq.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        Enumeration values = hreq.getHeaders(name);
        while (values.hasMoreElements()) {
            String value = (String) values.nextElement();
            saved.addHeader(name, value);
        }
    }
    saved.setContentLength(hreq.getContentLength());
    Enumeration locales = hreq.getLocales();
    while (locales.hasMoreElements()) {
        Locale locale = (Locale) locales.nextElement();
        saved.addLocale(locale);
    }
    // May need to acknowledge a 100-continue expectation
    ((HttpResponse) request.getResponse()).sendAcknowledgement();
    ByteChunk body = new ByteChunk();
    body.setLimit(request.getConnector().getMaxSavePostSize());
    byte[] buffer = new byte[4096];
    int bytesRead;
    InputStream is = request.getStream();
    while ((bytesRead = is.read(buffer)) >= 0) {
        body.append(buffer, 0, bytesRead);
    }
    // Only save the request body if there is something to save
    if (body.getLength() > 0) {
        saved.setContentType(hreq.getContentType());
        saved.setBody(body);
    }
    saved.setMethod(hreq.getMethod());
    saved.setQueryString(hreq.getQueryString());
    saved.setRequestURI(hreq.getRequestURI());
    // Stash the SavedRequest in our session for later use
    session.setNote(Constants.FORM_REQUEST_NOTE, saved);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) ByteChunk(org.glassfish.grizzly.http.util.ByteChunk) InputStream(java.io.InputStream) HttpResponse(org.apache.catalina.HttpResponse) SecurityConstraint(org.apache.catalina.deploy.SecurityConstraint)

Example 4 with ByteChunk

use of org.glassfish.grizzly.http.util.ByteChunk in project Payara by payara.

the class CoyoteAdapter method normalizeBytes.

private static boolean normalizeBytes(MessageBytes uriMB) {
    ByteChunk uriBC = uriMB.getByteChunk();
    byte[] b = uriBC.getBytes();
    int start = uriBC.getStart();
    int end = uriBC.getEnd();
    // An empty URL is not acceptable
    if (start == end)
        return false;
    // URL * is acceptable
    if ((end - start == 1) && b[start] == (byte) '*')
        return true;
    int pos = 0;
    int index = 0;
    // Check for null byte
    for (pos = start; pos < end; pos++) {
        if (b[pos] == (byte) '\\') {
            if (ALLOW_BACKSLASH) {
                b[pos] = (byte) '/';
            } else {
                return false;
            }
        }
        if (b[pos] == (byte) 0) {
            return false;
        }
    }
    // The URL must start with '/'
    if (b[start] != (byte) '/') {
        return false;
    }
    // Replace "//" with "/"
    if (COLLAPSE_ADJACENT_SLASHES) {
        for (pos = start; pos < (end - 1); pos++) {
            if (b[pos] == (byte) '/') {
                while ((pos + 1 < end) && (b[pos + 1] == (byte) '/')) {
                    copyBytes(b, pos, pos + 1, end - pos - 1);
                    end--;
                }
            }
        }
    }
    // as the next character is a non-significant WS.
    if (((end - start) > 2) && (b[end - 1] == (byte) '.')) {
        if ((b[end - 2] == (byte) '/') || ((b[end - 2] == (byte) '.') && (b[end - 3] == (byte) '/'))) {
            b[end] = (byte) '/';
            end++;
        }
    }
    uriBC.setEnd(end);
    index = 0;
    // Resolve occurrences of "/./" in the normalized path
    while (true) {
        index = uriBC.indexOf("/./", 0, 3, index);
        if (index < 0)
            break;
        copyBytes(b, start + index, start + index + 2, end - start - index - 2);
        end = end - 2;
        uriBC.setEnd(end);
    }
    index = 0;
    // Resolve occurrences of "/../" in the normalized path
    while (true) {
        index = uriBC.indexOf("/../", 0, 4, index);
        if (index < 0)
            break;
        // Prevent from going outside our context
        if (index == 0)
            return false;
        int index2 = -1;
        for (pos = start + index - 1; (pos >= 0) && (index2 < 0); pos--) {
            if (b[pos] == (byte) '/') {
                index2 = pos;
            }
        }
        copyBytes(b, start + index2, start + index + 3, end - start - index - 3);
        end = end + index2 - index - 3;
        uriBC.setEnd(end);
        index = index2;
    }
    uriBC.setBytes(b, start, end);
    return true;
}
Also used : ByteChunk(org.glassfish.grizzly.http.util.ByteChunk)

Aggregations

ByteChunk (org.glassfish.grizzly.http.util.ByteChunk)4 InputStream (java.io.InputStream)2 Cookie (javax.servlet.http.Cookie)2 IOException (java.io.IOException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpResponse (org.apache.catalina.HttpResponse)1 SecurityConstraint (org.apache.catalina.deploy.SecurityConstraint)1 B2CConverter (org.glassfish.grizzly.http.util.B2CConverter)1 CharChunk (org.glassfish.grizzly.http.util.CharChunk)1