Extract method to get cookie value
Currently, trying to get a `csrftoken` from an empty cookie string will crash the application. This creates a helper method to extract a cookie value given a cookie name by pattern matching on the cookie string and returning the value if one is found. Fixes: #167
This commit is contained in:
parent
890c4d32d1
commit
dd0f1e397c
@ -12,6 +12,8 @@ import java.net.URI;
|
|||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import awais.instagrabber.BuildConfig;
|
import awais.instagrabber.BuildConfig;
|
||||||
import awaisomereport.LogCollector;
|
import awaisomereport.LogCollector;
|
||||||
@ -56,22 +58,23 @@ public final class CookieUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static String getUserIdFromCookie(final String cookie) {
|
public static String getUserIdFromCookie(final String cookies) {
|
||||||
if (!TextUtils.isEmpty(cookie)) {
|
return getCookieValue(cookies, "ds_user_id");
|
||||||
final int uidIndex = cookie.indexOf("ds_user_id=");
|
|
||||||
if (uidIndex > 0) {
|
|
||||||
String uid = cookie.split("ds_user_id=")[1].split(";")[0];
|
|
||||||
return !TextUtils.isEmpty(uid) ? uid : null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCsrfTokenFromCookie(final String cookie) {
|
@Nullable
|
||||||
if (cookie == null) {
|
public static String getCsrfTokenFromCookie(final String cookies) {
|
||||||
return null;
|
return getCookieValue(cookies, "csrftoken");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static String getCookieValue(final String cookies, final String name) {
|
||||||
|
final Pattern pattern = Pattern.compile(name + "=(.+?);");
|
||||||
|
final Matcher matcher = pattern.matcher(cookies);
|
||||||
|
if (matcher.find()) {
|
||||||
|
return matcher.group(1);
|
||||||
}
|
}
|
||||||
return cookie.split("csrftoken=")[1].split(";")[0];
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
Loading…
Reference in New Issue
Block a user