Skip to content

Commit 509f42d

Browse files
committed
[Bandcamp] Send Content-Type header in POST requests
This header was not sent before and was added and guessed by OkHttp. This can create issues when using other HTTP clients than OkHttp, such as Cronet. Also make use of StandardCharsets.UTF_8 when getting bytes of bodies instead of the platform default's charset, to make sure to prevent some encoding issues on some JVMs.
1 parent 399fe26 commit 509f42d

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampExtractorHelper.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@
66
import com.grack.nanojson.JsonParser;
77
import com.grack.nanojson.JsonParserException;
88
import com.grack.nanojson.JsonWriter;
9-
109
import org.jsoup.Jsoup;
1110
import org.jsoup.nodes.Element;
1211
import org.schabi.newpipe.extractor.NewPipe;
1312
import org.schabi.newpipe.extractor.exceptions.ParsingException;
1413
import org.schabi.newpipe.extractor.exceptions.ReCaptchaException;
1514
import org.schabi.newpipe.extractor.localization.DateWrapper;
15+
import org.schabi.newpipe.extractor.utils.Utils;
1616

17+
import javax.annotation.Nullable;
1718
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
1820
import java.time.DateTimeException;
1921
import java.time.ZonedDateTime;
2022
import java.time.format.DateTimeFormatter;
23+
import java.util.Collections;
2124
import java.util.Locale;
2225

23-
import javax.annotation.Nullable;
24-
2526
public final class BandcampExtractorHelper {
2627

2728
public static final String BASE_URL = "https://bandcamp.com";
@@ -43,8 +44,8 @@ public static String getStreamUrlFromIds(final long bandId,
4344
+ "&tralbum_id=" + itemId + "&tralbum_type=" + itemType.charAt(0))
4445
.responseBody();
4546

46-
return JsonParser.object().from(jsonString)
47-
.getString("bandcamp_url").replace("http://", "https://");
47+
return Utils.replaceHttpWithHttps(JsonParser.object().from(jsonString)
48+
.getString("bandcamp_url"));
4849

4950
} catch (final JsonParserException | ReCaptchaException | IOException e) {
5051
throw new ParsingException("Ids could not be translated to URL", e);
@@ -60,19 +61,16 @@ public static String getStreamUrlFromIds(final long bandId,
6061
*/
6162
public static JsonObject getArtistDetails(final String id) throws ParsingException {
6263
try {
63-
return
64-
JsonParser.object().from(
65-
NewPipe.getDownloader().post(
66-
BASE_API_URL + "/mobile/22/band_details",
67-
null,
68-
JsonWriter.string()
69-
.object()
70-
.value("band_id", id)
71-
.end()
72-
.done()
73-
.getBytes()
74-
).responseBody()
75-
);
64+
return JsonParser.object().from(NewPipe.getDownloader().post(
65+
BASE_API_URL + "/mobile/22/band_details",
66+
Collections.singletonMap("Content-Type",
67+
Collections.singletonList("application/json")),
68+
JsonWriter.string()
69+
.object()
70+
.value("band_id", id)
71+
.end()
72+
.done()
73+
.getBytes(StandardCharsets.UTF_8)).responseBody());
7674
} catch (final IOException | ReCaptchaException | JsonParserException e) {
7775
throw new ParsingException("Could not download band details", e);
7876
}
@@ -123,7 +121,7 @@ public static boolean isSupportedDomain(final String url) throws ParsingExceptio
123121
/**
124122
* Whether the URL points to a radio kiosk.
125123
* @param url the URL to check
126-
* @return true if the URL matches <code>https://bandcamp.com/?show=SHOW_ID</code>
124+
* @return true if the URL matches {@code https://bandcamp.com/?show=SHOW_ID}
127125
*/
128126
public static boolean isRadioUrl(final String url) {
129127
return url.toLowerCase().matches("https?://bandcamp\\.com/\\?show=\\d+");

extractor/src/main/java/org/schabi/newpipe/extractor/services/bandcamp/extractors/BandcampFeaturedExtractor.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import javax.annotation.Nonnull;
2020
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
22+
import java.util.Collections;
2123

2224
import static org.schabi.newpipe.extractor.services.bandcamp.extractors.BandcampExtractorHelper.BASE_API_URL;
2325

@@ -40,11 +42,12 @@ public BandcampFeaturedExtractor(final StreamingService streamingService,
4042
public void onFetchPage(@Nonnull final Downloader downloader)
4143
throws IOException, ExtractionException {
4244
try {
43-
json = JsonParser.object().from(
44-
getDownloader().post(
45-
FEATURED_API_URL, null, "{\"platform\":\"\",\"version\":0}".getBytes()
46-
).responseBody()
47-
);
45+
json = JsonParser.object().from(getDownloader().post(
46+
FEATURED_API_URL,
47+
Collections.singletonMap("Content-Type",
48+
Collections.singletonList("application/json")),
49+
"{\"platform\":\"\",\"version\":0}".getBytes(StandardCharsets.UTF_8))
50+
.responseBody());
4851
} catch (final JsonParserException e) {
4952
throw new ParsingException("Could not parse Bandcamp featured API response", e);
5053
}

0 commit comments

Comments
 (0)