Lines 561-594
Link Here
|
561 |
HttpServletResponse response, |
561 |
HttpServletResponse response, |
562 |
ResourceAttributes resourceAttributes) |
562 |
ResourceAttributes resourceAttributes) |
563 |
throws IOException { |
563 |
throws IOException { |
|
|
564 |
|
565 |
// the logic here is that a function that detects that it can send |
566 |
// a reply will do so and return false. So it is safe to drop |
567 |
// checkIfMatch and checkIfNoneMatch from the following expression |
568 |
// to eliminate ETag handling |
564 |
|
569 |
|
565 |
return checkIfMatch(request, response, resourceAttributes) |
570 |
return checkIfModifiedSince(request, response, resourceAttributes) |
566 |
&& checkIfModifiedSince(request, response, resourceAttributes) |
|
|
567 |
&& checkIfNoneMatch(request, response, resourceAttributes) |
568 |
&& checkIfUnmodifiedSince(request, response, resourceAttributes); |
571 |
&& checkIfUnmodifiedSince(request, response, resourceAttributes); |
569 |
|
572 |
|
570 |
} |
573 |
} |
571 |
|
574 |
|
572 |
|
575 |
|
573 |
/** |
576 |
/** |
574 |
* Get the ETag associated with a file. |
|
|
575 |
* |
576 |
* @param resourceAttributes The resource information |
577 |
*/ |
578 |
protected String getETag(ResourceAttributes resourceAttributes) { |
579 |
String result = null; |
580 |
if ((result = resourceAttributes.getETag(true)) != null) { |
581 |
return result; |
582 |
} else if ((result = resourceAttributes.getETag()) != null) { |
583 |
return result; |
584 |
} else { |
585 |
return "W/\"" + resourceAttributes.getContentLength() + "-" |
586 |
+ resourceAttributes.getLastModified() + "\""; |
587 |
} |
588 |
} |
589 |
|
590 |
|
591 |
/** |
592 |
* URL rewriter. |
577 |
* URL rewriter. |
593 |
* |
578 |
* |
594 |
* @param path Path which has to be rewiten |
579 |
* @param path Path which has to be rewiten |
Lines 723-729
Link Here
|
723 |
ranges = parseRange(request, response, cacheEntry.attributes); |
708 |
ranges = parseRange(request, response, cacheEntry.attributes); |
724 |
|
709 |
|
725 |
// ETag header |
710 |
// ETag header |
726 |
response.setHeader("ETag", getETag(cacheEntry.attributes)); |
711 |
// response.setHeader("ETag", getETag(cacheEntry.attributes)); |
727 |
|
712 |
|
728 |
// Last-Modified header |
713 |
// Last-Modified header |
729 |
response.setHeader("Last-Modified", |
714 |
response.setHeader("Last-Modified", |
Lines 969-982
Link Here
|
969 |
; |
954 |
; |
970 |
} |
955 |
} |
971 |
|
956 |
|
972 |
String eTag = getETag(resourceAttributes); |
957 |
// eTag = getETag(resourceAttributes); |
973 |
long lastModified = resourceAttributes.getLastModified(); |
958 |
long lastModified = resourceAttributes.getLastModified(); |
974 |
|
959 |
|
975 |
if (headerValueTime == (-1L)) { |
960 |
if (headerValueTime == (-1L)) { |
976 |
|
961 |
|
977 |
// If the ETag the client gave does not match the entity |
962 |
// If the ETag the client gave does not match the entity |
978 |
// etag, then the entire entity is returned. |
963 |
// etag, then the entire entity is returned. |
979 |
if (!eTag.equals(headerValue.trim())) |
964 |
// if (!eTag.equals(headerValue.trim())) |
980 |
return FULL; |
965 |
return FULL; |
981 |
|
966 |
|
982 |
} else { |
967 |
} else { |
Lines 1505-1554
Link Here
|
1505 |
} |
1490 |
} |
1506 |
} |
1491 |
} |
1507 |
|
1492 |
|
1508 |
|
|
|
1509 |
/** |
1510 |
* Check if the if-match condition is satisfied. |
1511 |
* |
1512 |
* @param request The servlet request we are processing |
1513 |
* @param response The servlet response we are creating |
1514 |
* @param resourceInfo File object |
1515 |
* @return boolean true if the resource meets the specified condition, |
1516 |
* and false if the condition is not satisfied, in which case request |
1517 |
* processing is stopped |
1518 |
*/ |
1519 |
protected boolean checkIfMatch(HttpServletRequest request, |
1520 |
HttpServletResponse response, |
1521 |
ResourceAttributes resourceAttributes) |
1522 |
throws IOException { |
1523 |
|
1524 |
String eTag = getETag(resourceAttributes); |
1525 |
String headerValue = request.getHeader("If-Match"); |
1526 |
if (headerValue != null) { |
1527 |
if (headerValue.indexOf('*') == -1) { |
1528 |
|
1529 |
StringTokenizer commaTokenizer = new StringTokenizer |
1530 |
(headerValue, ","); |
1531 |
boolean conditionSatisfied = false; |
1532 |
|
1533 |
while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) { |
1534 |
String currentToken = commaTokenizer.nextToken(); |
1535 |
if (currentToken.trim().equals(eTag)) |
1536 |
conditionSatisfied = true; |
1537 |
} |
1538 |
|
1539 |
// If none of the given ETags match, 412 Precodition failed is |
1540 |
// sent back |
1541 |
if (!conditionSatisfied) { |
1542 |
response.sendError |
1543 |
(HttpServletResponse.SC_PRECONDITION_FAILED); |
1544 |
return false; |
1545 |
} |
1546 |
|
1547 |
} |
1548 |
} |
1549 |
return true; |
1550 |
|
1551 |
} |
1552 |
|
1493 |
|
1553 |
|
1494 |
|
1554 |
/** |
1495 |
/** |
Lines 1577-1583
Link Here
|
1577 |
// The entity has not been modified since the date |
1518 |
// The entity has not been modified since the date |
1578 |
// specified by the client. This is not an error case. |
1519 |
// specified by the client. This is not an error case. |
1579 |
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); |
1520 |
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); |
1580 |
response.setHeader("ETag", getETag(resourceAttributes)); |
1521 |
// response.setHeader("ETag", getETag(resourceAttributes)); |
1581 |
|
1522 |
|
1582 |
return false; |
1523 |
return false; |
1583 |
} |
1524 |
} |
Lines 1591-1656
Link Here
|
1591 |
|
1532 |
|
1592 |
|
1533 |
|
1593 |
/** |
1534 |
/** |
1594 |
* Check if the if-none-match condition is satisfied. |
|
|
1595 |
* |
1596 |
* @param request The servlet request we are processing |
1597 |
* @param response The servlet response we are creating |
1598 |
* @param resourceInfo File object |
1599 |
* @return boolean true if the resource meets the specified condition, |
1600 |
* and false if the condition is not satisfied, in which case request |
1601 |
* processing is stopped |
1602 |
*/ |
1603 |
protected boolean checkIfNoneMatch(HttpServletRequest request, |
1604 |
HttpServletResponse response, |
1605 |
ResourceAttributes resourceAttributes) |
1606 |
throws IOException { |
1607 |
|
1608 |
String eTag = getETag(resourceAttributes); |
1609 |
String headerValue = request.getHeader("If-None-Match"); |
1610 |
if (headerValue != null) { |
1611 |
|
1612 |
boolean conditionSatisfied = false; |
1613 |
|
1614 |
if (!headerValue.equals("*")) { |
1615 |
|
1616 |
StringTokenizer commaTokenizer = |
1617 |
new StringTokenizer(headerValue, ","); |
1618 |
|
1619 |
while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) { |
1620 |
String currentToken = commaTokenizer.nextToken(); |
1621 |
if (currentToken.trim().equals(eTag)) |
1622 |
conditionSatisfied = true; |
1623 |
} |
1624 |
|
1625 |
} else { |
1626 |
conditionSatisfied = true; |
1627 |
} |
1628 |
|
1629 |
if (conditionSatisfied) { |
1630 |
|
1631 |
// For GET and HEAD, we should respond with |
1632 |
// 304 Not Modified. |
1633 |
// For every other method, 412 Precondition Failed is sent |
1634 |
// back. |
1635 |
if ( ("GET".equals(request.getMethod())) |
1636 |
|| ("HEAD".equals(request.getMethod())) ) { |
1637 |
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); |
1638 |
response.setHeader("ETag", getETag(resourceAttributes)); |
1639 |
|
1640 |
return false; |
1641 |
} else { |
1642 |
response.sendError |
1643 |
(HttpServletResponse.SC_PRECONDITION_FAILED); |
1644 |
return false; |
1645 |
} |
1646 |
} |
1647 |
} |
1648 |
return true; |
1649 |
|
1650 |
} |
1651 |
|
1652 |
|
1653 |
/** |
1654 |
* Check if the if-unmodified-since condition is satisfied. |
1535 |
* Check if the if-unmodified-since condition is satisfied. |
1655 |
* |
1536 |
* |
1656 |
* @param request The servlet request we are processing |
1537 |
* @param request The servlet request we are processing |