-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1262 from Theta-Dev/fix/ios-client-vdata
[YouTube] update iOS client, add visitor data to requests
- Loading branch information
Showing
3 changed files
with
126 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
extractor/src/main/java/org/schabi/newpipe/extractor/utils/ProtoBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.schabi.newpipe.extractor.utils; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
|
||
public class ProtoBuilder { | ||
ByteArrayOutputStream byteBuffer; | ||
|
||
public ProtoBuilder() { | ||
this.byteBuffer = new ByteArrayOutputStream(); | ||
} | ||
|
||
public byte[] toBytes() { | ||
return byteBuffer.toByteArray(); | ||
} | ||
|
||
public String toUrlencodedBase64() { | ||
final String b64 = Base64.getUrlEncoder().encodeToString(toBytes()); | ||
return URLEncoder.encode(b64, StandardCharsets.UTF_8); | ||
} | ||
|
||
private void writeVarint(final long val) { | ||
try { | ||
if (val == 0) { | ||
byteBuffer.write(new byte[]{(byte) 0}); | ||
} else { | ||
long v = val; | ||
while (v != 0) { | ||
byte b = (byte) (v & 0x7f); | ||
v >>= 7; | ||
|
||
if (v != 0) { | ||
b |= (byte) 0x80; | ||
} | ||
byteBuffer.write(new byte[]{b}); | ||
} | ||
} | ||
} catch (final IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private void field(final int field, final byte wire) { | ||
final long fbits = ((long) field) << 3; | ||
final long wbits = ((long) wire) & 0x07; | ||
final long val = fbits | wbits; | ||
writeVarint(val); | ||
} | ||
|
||
public void varint(final int field, final long val) { | ||
field(field, (byte) 0); | ||
writeVarint(val); | ||
} | ||
|
||
public void string(final int field, final String string) { | ||
final byte[] strBts = string.getBytes(StandardCharsets.UTF_8); | ||
bytes(field, strBts); | ||
} | ||
|
||
public void bytes(final int field, final byte[] bytes) { | ||
field(field, (byte) 2); | ||
writeVarint(bytes.length); | ||
try { | ||
byteBuffer.write(bytes); | ||
} catch (final IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
extractor/src/test/java/org/schabi/newpipe/extractor/utils/ProtoBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.schabi.newpipe.extractor.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class ProtoBuilderTest { | ||
@Test | ||
public void testProtoBuilder() { | ||
final ProtoBuilder pb = new ProtoBuilder(); | ||
pb.varint(1, 128); | ||
pb.varint(2, 1234567890); | ||
pb.varint(3, 1234567890123456789L); | ||
pb.string(4, "Hello"); | ||
pb.bytes(5, new byte[]{1, 2, 3}); | ||
assertEquals("CIABENKF2MwEGJWCpu_HnoSRESIFSGVsbG8qAwECAw%3D%3D", pb.toUrlencodedBase64()); | ||
} | ||
} |