开发者

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.

P.S. 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 wher开发者_Python百科e 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜