1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.impl.cache;
28
29 import java.net.URI;
30 import java.util.Objects;
31
32 import org.apache.hc.client5.http.utils.URIUtils;
33 import org.apache.hc.core5.annotation.Internal;
34 import org.apache.hc.core5.http.Header;
35 import org.apache.hc.core5.http.MessageHeaders;
36 import org.apache.hc.core5.util.TextUtils;
37 import org.apache.hc.core5.util.TimeValue;
38
39
40
41
42
43
44 @Internal
45 public final class CacheSupport {
46
47 public static URI getLocationURI(final URI requestUri, final MessageHeaders response, final String headerName) {
48 final Header h = response.getFirstHeader(headerName);
49 if (h == null) {
50 return null;
51 }
52 final URI locationUri = CacheKeyGenerator.normalize(h.getValue());
53 if (locationUri == null) {
54 return requestUri;
55 }
56 if (locationUri.isAbsolute()) {
57 return locationUri;
58 } else {
59 return URIUtils.resolve(requestUri, locationUri);
60 }
61 }
62
63 public static boolean isSameOrigin(final URI requestURI, final URI targetURI) {
64 return targetURI.isAbsolute() && Objects.equals(requestURI.getAuthority(), targetURI.getAuthority());
65 }
66
67 public static final TimeValue MAX_AGE = TimeValue.ofSeconds(Integer.MAX_VALUE + 1L);
68
69 public static long deltaSeconds(final String s) {
70 if (TextUtils.isEmpty(s)) {
71 return -1;
72 }
73 try {
74 long ageValue = Long.parseLong(s);
75 if (ageValue < 0) {
76 ageValue = -1;
77 } else if (ageValue > Integer.MAX_VALUE) {
78 ageValue = MAX_AGE.toSeconds();
79 }
80 return ageValue;
81 } catch (final NumberFormatException ignore) {
82 }
83 return 0;
84 }
85
86 }