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 }
59 return URIUtils.resolve(requestUri, locationUri);
60 }
61
62 public static boolean isSameOrigin(final URI requestURI, final URI targetURI) {
63 return targetURI.isAbsolute() && Objects.equals(requestURI.getAuthority(), targetURI.getAuthority());
64 }
65
66 public static final TimeValue MAX_AGE = TimeValue.ofSeconds(Integer.MAX_VALUE + 1L);
67
68 public static long deltaSeconds(final String s) {
69 if (TextUtils.isEmpty(s)) {
70 return -1;
71 }
72 try {
73 long ageValue = Long.parseLong(s);
74 if (ageValue < 0) {
75 ageValue = -1;
76 } else if (ageValue > Integer.MAX_VALUE) {
77 ageValue = MAX_AGE.toSeconds();
78 }
79 return ageValue;
80 } catch (final NumberFormatException ignore) {
81 }
82 return 0;
83 }
84
85 }