Skip to content

Commit

Permalink
[YouTube] Send Content-Type header in all POST requests
Browse files Browse the repository at this point in the history
This header was not sent partially before and was added and guessed by OkHttp. This can create issues when using other HTTP clients than OkHttp, such as Cronet.

Some code in the modified classes has been improved and / or deduplicated, and usages of the UTF_8 constant of the Utils class has been replaced by StandardCharsets.UTF_8 where possible.

Note that this header has been not added in except in YoutubeDashManifestCreatorsUtils, as an empty body is sent in the POST requests made by this class.
  • Loading branch information
AudricV committed Jul 11, 2022
1 parent 509f42d commit 6017730
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,14 @@ public static boolean areHardcodedClientVersionAndKeyValid()
.end()
.value("fetchLiveState", true)
.end()
.end().done().getBytes(UTF_8);
.end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on

final Map<String, List<String>> headers = new HashMap<>();
headers.put("X-YouTube-Client-Name", Collections.singletonList("1"));
headers.put("X-YouTube-Client-Version",
Collections.singletonList(HARDCODED_CLIENT_VERSION));
headers.put("Content-Type", Collections.singletonList("application/json"));

// This endpoint is fetched by the YouTube website to get the items of its main menu and is
// pretty lightweight (around 30kB)
Expand Down Expand Up @@ -817,16 +818,16 @@ public static boolean isHardcodedYoutubeMusicKeyValid() throws IOException,
.end()
.end()
.value("input", "")
.end().done().getBytes(UTF_8);
.end().done().getBytes(StandardCharsets.UTF_8);
// @formatter:on

final Map<String, List<String>> headers = new HashMap<>();
headers.put("X-YouTube-Client-Name", Collections.singletonList(
HARDCODED_YOUTUBE_MUSIC_KEY[1]));
headers.put("X-YouTube-Client-Version", Collections.singletonList(
HARDCODED_YOUTUBE_MUSIC_KEY[2]));
headers.put("X-YouTube-Client-Name",
Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[1]));
headers.put("X-YouTube-Client-Version",
Collections.singletonList(HARDCODED_YOUTUBE_MUSIC_KEY[2]));
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
headers.put("Referer", Collections.singletonList("music.youtube.com"));
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
headers.put("Content-Type", Collections.singletonList("application/json"));

final Response response = getDownloader().post(url, headers, json);
Expand Down Expand Up @@ -1065,13 +1066,12 @@ public static JsonObject getJsonPostResponse(final String endpoint,
final Localization localization)
throws IOException, ExtractionException {
final Map<String, List<String>> headers = new HashMap<>();
addClientInfoHeaders(headers);
addYouTubeHeaders(headers);
headers.put("Content-Type", Collections.singletonList("application/json"));

final Response response = getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key="
+ getKey() + DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization);

return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
return JsonUtils.toJsonObject(getValidJsonResponseBody(
getDownloader().post(YOUTUBEI_V1_URL + endpoint + "?key=" + getKey()
+ DISABLE_PRETTY_PRINT_PARAMETER, headers, body, localization)));
}

public static JsonObject getJsonAndroidPostResponse(
Expand Down Expand Up @@ -1100,17 +1100,18 @@ private static JsonObject getMobilePostResponse(
@Nonnull final String innerTubeApiKey,
@Nullable final String endPartOfUrlRequest) throws IOException, ExtractionException {
final Map<String, List<String>> headers = new HashMap<>();
headers.put("Content-Type", Collections.singletonList("application/json"));
headers.put("User-Agent", Collections.singletonList(userAgent));
headers.put("X-Goog-Api-Format-Version", Collections.singletonList("2"));
headers.put("Content-Type", Collections.singletonList("application/json"));

final String baseEndpointUrl = YOUTUBEI_V1_GAPIS_URL + endpoint + "?key=" + innerTubeApiKey
+ DISABLE_PRETTY_PRINT_PARAMETER;

final Response response = getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
? baseEndpointUrl : baseEndpointUrl + endPartOfUrlRequest,
headers, body, localization);
return JsonUtils.toJsonObject(getValidJsonResponseBody(response));
return JsonUtils.toJsonObject(getValidJsonResponseBody(
getDownloader().post(isNullOrEmpty(endPartOfUrlRequest)
? baseEndpointUrl
: baseEndpointUrl + endPartOfUrlRequest,
headers, body, localization)));
}

@Nonnull
Expand Down Expand Up @@ -1288,6 +1289,17 @@ public static String getIosUserAgent(@Nullable final Localization localization)
+ ")";
}

@Nonnull
public static Map<String, List<String>> getYoutubeMusicHeaders() {
final Map<String, List<String>> headers = new HashMap<>();
headers.put("X-YouTube-Client-Name", Collections.singletonList(youtubeMusicKey[1]));
headers.put("X-YouTube-Client-Version", Collections.singletonList(youtubeMusicKey[2]));
headers.put("Origin", Collections.singletonList("https://music.youtube.com"));
headers.put("Referer", Collections.singletonList("https://music.youtube.com"));
headers.put("Content-Type", Collections.singletonList("application/json"));
return headers;
}

/**
* Add required headers and cookies to an existing headers Map.
* @see #addClientInfoHeaders(Map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Map;
import java.util.Objects;

import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.addClientInfoHeaders;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getAndroidUserAgent;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.getIosUserAgent;
import static org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper.isAndroidStreamingUrl;
Expand Down Expand Up @@ -708,7 +707,8 @@ private static Response getStreamingWebUrlWithoutRedirects(
throws CreationException {
try {
final Map<String, List<String>> headers = new HashMap<>();
addClientInfoHeaders(headers);
headers.put("Origin", Collections.singletonList("https://www.youtube.com"));
headers.put("Referer", Collections.singletonList("https://www.youtube.com"));

String responseMimeType = "";

Expand Down
Loading

0 comments on commit 6017730

Please sign in to comment.