简体   繁体   中英

Getting binary HTTP GET parameter in Play Framework

I need to get binary data in GET request by Play Framework. This is used to get info_hash from BitTorrent clients.

I get it like this:

byte[] infoHash = params.get("info_hash").getBytes("ISO-8859-1")

Unfortunately all non-ascii symbols replaced by 0x3f.

PS I can get url encoded parameters from Http.Request.current().querystring, but this is a bad idea.

Update: I override play.data.parsing.UrlEncodedParser.parse(InputStream is) method with my variant where used ISO-8859-1 in parameter instead of hardcoded UTF-8 as in original and all is working as it should. But i still looking for a better way, because i don't want to edit framework sources.

According to http://wiki.theory.org/BitTorrent_Tracker_Protocol :

info_hash

The 20 byte sha1 hash of the bencoded form of the info value from the metainfo file.

A SHA1 sum looks like this: 92a11182a8405cbd8d25cd3cc3334fc6155bec06

Each successive pair of bytes in the representation of a byte. Although this representation itself might be encoded, it's not a URL-encoding of the bytes for the info_hash.

So you need to convert each pair of chars from the String into a byte. If you find a library that does it instead, stick to it. If not, feel free to use this code:

byte[] decode(String enc) {
    if (enc.length() % 2 != 0) throw new NumberFormatException();

    byte arr[] = new byte[enc.length() / 2];
    int c = 0;
    for (int i = 0; i < enc.length(); i += 2) {
        arr[c++] = Integer.valueOf(enc.substring(i, i + 2), 16).byteValue();
    }
    return arr;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM