Skip to content

Commit

Permalink
perf: Specify no body data is sent
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios committed Sep 1, 2024
1 parent ae9864c commit abbe68f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public static HttpURLConnection getConnectionFromRoute(String apiUrl, Route rout
public static HttpURLConnection getConnectionFromCompiledRoute(String apiUrl, Route.CompiledRoute route) throws IOException {
String url = apiUrl + route.getCompiledRoute();
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
// Request data is in the URL parameters and no body is sent.
// The calling code must set a length if using a request body.
connection.setFixedLengthStreamingMode(0);
connection.setRequestMethod(route.getMethod().name());
String agentString = System.getProperty("http.agent")
+ "; ReVanced/" + Utils.getAppVersionName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,12 @@ private static String confirmRegistration(String userId, String solution) {
applyCommonPostRequestSettings(connection);

String jsonInputString = "{\"solution\": \"" + solution + "\"}";
byte[] body = jsonInputString.getBytes(StandardCharsets.UTF_8);
connection.setFixedLengthStreamingMode(body.length);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
os.write(body);
}

final int responseCode = connection.getResponseCode();
if (checkIfRateLimitWasHit(responseCode)) {
connection.disconnect(); // disconnect, as no more connections will be made for a little while
Expand Down Expand Up @@ -440,9 +442,10 @@ public static boolean sendVote(String videoId, ReturnYouTubeDislike.Vote vote) {
applyCommonPostRequestSettings(connection);

String voteJsonString = "{\"userId\": \"" + userId + "\", \"videoId\": \"" + videoId + "\", \"value\": \"" + vote.value + "\"}";
byte[] body = voteJsonString.getBytes(StandardCharsets.UTF_8);
connection.setFixedLengthStreamingMode(body.length);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = voteJsonString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
os.write(body);
}

final int responseCode = connection.getResponseCode();
Expand Down Expand Up @@ -490,10 +493,12 @@ private static boolean confirmVote(String videoId, String userId, String solutio
applyCommonPostRequestSettings(connection);

String jsonInputString = "{\"userId\": \"" + userId + "\", \"videoId\": \"" + videoId + "\", \"solution\": \"" + solution + "\"}";
byte[] body = jsonInputString.getBytes(StandardCharsets.UTF_8);
connection.setFixedLengthStreamingMode(body.length);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
os.write(body);
}

final int responseCode = connection.getResponseCode();
if (checkIfRateLimitWasHit(responseCode)) {
connection.disconnect(); // disconnect, as no more connections will be made for a little while
Expand Down

0 comments on commit abbe68f

Please sign in to comment.