Search in sources :

Example 1 with QuotedCSV

use of org.eclipse.jetty.http.QuotedCSV in project jetty.project by eclipse.

the class ResourceService method passConditionalHeaders.

/* ------------------------------------------------------------ */
/* Check modification date headers.
     */
protected boolean passConditionalHeaders(HttpServletRequest request, HttpServletResponse response, HttpContent content) throws IOException {
    try {
        String ifm = null;
        String ifnm = null;
        String ifms = null;
        long ifums = -1;
        if (request instanceof Request) {
            // Find multiple fields by iteration as an optimization 
            HttpFields fields = ((Request) request).getHttpFields();
            for (int i = fields.size(); i-- > 0; ) {
                HttpField field = fields.getField(i);
                if (field.getHeader() != null) {
                    switch(field.getHeader()) {
                        case IF_MATCH:
                            ifm = field.getValue();
                            break;
                        case IF_NONE_MATCH:
                            ifnm = field.getValue();
                            break;
                        case IF_MODIFIED_SINCE:
                            ifms = field.getValue();
                            break;
                        case IF_UNMODIFIED_SINCE:
                            ifums = DateParser.parseDate(field.getValue());
                            break;
                        default:
                    }
                }
            }
        } else {
            ifm = request.getHeader(HttpHeader.IF_MATCH.asString());
            ifnm = request.getHeader(HttpHeader.IF_NONE_MATCH.asString());
            ifms = request.getHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
            ifums = request.getDateHeader(HttpHeader.IF_UNMODIFIED_SINCE.asString());
        }
        if (!HttpMethod.HEAD.is(request.getMethod())) {
            if (_etags) {
                String etag = content.getETagValue();
                if (ifm != null) {
                    boolean match = false;
                    if (etag != null) {
                        QuotedCSV quoted = new QuotedCSV(true, ifm);
                        for (String tag : quoted) {
                            if (CompressedContentFormat.tagEquals(etag, tag)) {
                                match = true;
                                break;
                            }
                        }
                    }
                    if (!match) {
                        response.setStatus(HttpServletResponse.SC_PRECONDITION_FAILED);
                        return false;
                    }
                }
                if (ifnm != null && etag != null) {
                    // Handle special case of exact match OR gzip exact match
                    if (CompressedContentFormat.tagEquals(etag, ifnm) && ifnm.indexOf(',') < 0) {
                        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                        response.setHeader(HttpHeader.ETAG.asString(), ifnm);
                        return false;
                    }
                    // Handle list of tags
                    QuotedCSV quoted = new QuotedCSV(true, ifnm);
                    for (String tag : quoted) {
                        if (CompressedContentFormat.tagEquals(etag, tag)) {
                            response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                            response.setHeader(HttpHeader.ETAG.asString(), tag);
                            return false;
                        }
                    }
                    // If etag requires content to be served, then do not check if-modified-since
                    return true;
                }
            }
            // Handle if modified since
            if (ifms != null) {
                //Get jetty's Response impl
                String mdlm = content.getLastModifiedValue();
                if (mdlm != null && ifms.equals(mdlm)) {
                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    if (_etags)
                        response.setHeader(HttpHeader.ETAG.asString(), content.getETagValue());
                    response.flushBuffer();
                    return false;
                }
                long ifmsl = request.getDateHeader(HttpHeader.IF_MODIFIED_SINCE.asString());
                if (ifmsl != -1 && content.getResource().lastModified() / 1000 <= ifmsl / 1000) {
                    response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    if (_etags)
                        response.setHeader(HttpHeader.ETAG.asString(), content.getETagValue());
                    response.flushBuffer();
                    return false;
                }
            }
            // Parse the if[un]modified dates and compare to resource
            if (ifums != -1 && content.getResource().lastModified() / 1000 > ifums / 1000) {
                response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
                return false;
            }
        }
    } catch (IllegalArgumentException iae) {
        if (!response.isCommitted())
            response.sendError(400, iae.getMessage());
        throw iae;
    }
    return true;
}
Also used : HttpField(org.eclipse.jetty.http.HttpField) PreEncodedHttpField(org.eclipse.jetty.http.PreEncodedHttpField) HttpFields(org.eclipse.jetty.http.HttpFields) HttpServletRequest(javax.servlet.http.HttpServletRequest) QuotedCSV(org.eclipse.jetty.http.QuotedCSV)

Aggregations

HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpField (org.eclipse.jetty.http.HttpField)1 HttpFields (org.eclipse.jetty.http.HttpFields)1 PreEncodedHttpField (org.eclipse.jetty.http.PreEncodedHttpField)1 QuotedCSV (org.eclipse.jetty.http.QuotedCSV)1