HttpHeaderParser.java 源代码


package com.x8zs.plugin.volley.toolbox;

import com.mbridge.msdk.foundation.download.Command;
import com.x8zs.plugin.apache.http.protocol.HTTP;
import com.x8zs.plugin.volley.Cache;
import com.x8zs.plugin.volley.Header;
import com.x8zs.plugin.volley.NetworkResponse;
import com.x8zs.plugin.volley.VolleyLog;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;

public class HttpHeaderParser {
    private static final String DEFAULT_CONTENT_CHARSET = "ISO-8859-1";
    static final String HEADER_CONTENT_TYPE = "Content-Type";
    private static final String RFC1123_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";

    public static String formatEpochAsRfc1123(long j6) {
        return newRfc1123Formatter().format(new Date(j6));
    }

    private static SimpleDateFormat newRfc1123Formatter() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        return simpleDateFormat;
    }

    public static Cache.Entry parseCacheHeaders(NetworkResponse networkResponse) {
        long j6;
        boolean z6;
        long j7;
        long j8;
        long j9;
        long j10;
        long j11;
        long j12;
        long j13;
        long currentTimeMillis = System.currentTimeMillis();
        Map<String, String> map = networkResponse.headers;
        String str = map.get(HTTP.DATE_HEADER);
        if (str != null) {
            j6 = parseDateAsEpoch(str);
        } else {
            j6 = 0;
        }
        String str2 = map.get("Cache-Control");
        int i6 = 0;
        if (str2 != null) {
            String[] split = str2.split(",", 0);
            z6 = false;
            j7 = 0;
            j8 = 0;
            while (i6 < split.length) {
                String trim = split[i6].trim();
                if (!trim.equals("no-cache") && !trim.equals("no-store")) {
                    if (trim.startsWith("max-age=")) {
                        try {
                            j7 = Long.parseLong(trim.substring(8));
                        } catch (Exception unused) {
                        }
                    } else if (trim.startsWith("stale-while-revalidate=")) {
                        j8 = Long.parseLong(trim.substring(23));
                    } else if (trim.equals("must-revalidate") || trim.equals("proxy-revalidate")) {
                        z6 = true;
                    }
                    i6++;
                } else {
                    return null;
                }
            }
            i6 = 1;
        } else {
            z6 = false;
            j7 = 0;
            j8 = 0;
        }
        String str3 = map.get("Expires");
        if (str3 != null) {
            j9 = parseDateAsEpoch(str3);
        } else {
            j9 = 0;
        }
        String str4 = map.get("Last-Modified");
        if (str4 != null) {
            j10 = parseDateAsEpoch(str4);
        } else {
            j10 = 0;
        }
        String str5 = map.get(Command.HTTP_HEADER_ETAG);
        if (i6 != 0) {
            j12 = currentTimeMillis + (j7 * 1000);
            if (z6) {
                j13 = j12;
            } else {
                Long.signum(j8);
                j13 = (j8 * 1000) + j12;
            }
            j11 = j13;
        } else {
            j11 = 0;
            if (j6 > 0 && j9 >= j6) {
                j12 = currentTimeMillis + (j9 - j6);
                j11 = j12;
            } else {
                j12 = 0;
            }
        }
        Cache.Entry entry = new Cache.Entry();
        entry.data = networkResponse.data;
        entry.etag = str5;
        entry.softTtl = j12;
        entry.ttl = j11;
        entry.serverDate = j6;
        entry.lastModified = j10;
        entry.responseHeaders = map;
        entry.allResponseHeaders = networkResponse.allHeaders;
        return entry;
    }

    public static String parseCharset(Map<String, String> map, String str) {
        String str2 = map.get("Content-Type");
        if (str2 != null) {
            String[] split = str2.split(";", 0);
            for (int i6 = 1; i6 < split.length; i6++) {
                String[] split2 = split[i6].trim().split("=", 0);
                if (split2.length == 2 && split2[0].equals("charset")) {
                    return split2[1];
                }
            }
        }
        return str;
    }

    public static long parseDateAsEpoch(String str) {
        try {
            return newRfc1123Formatter().parse(str).getTime();
        } catch (ParseException e6) {
            VolleyLog.e(e6, "Unable to parse dateStr: %s, falling back to 0", str);
            return 0L;
        }
    }

    public static List<Header> toAllHeaderList(Map<String, String> map) {
        ArrayList arrayList = new ArrayList(map.size());
        for (Map.Entry<String, String> entry : map.entrySet()) {
            arrayList.add(new Header(entry.getKey(), entry.getValue()));
        }
        return arrayList;
    }

    public static Map<String, String> toHeaderMap(List<Header> list) {
        TreeMap treeMap = new TreeMap(String.CASE_INSENSITIVE_ORDER);
        for (Header header : list) {
            treeMap.put(header.getName(), header.getValue());
        }
        return treeMap;
    }

    public static String parseCharset(Map<String, String> map) {
        return parseCharset(map, "ISO-8859-1");
    }
}