summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-http-byte-seek.vala
blob: 556ef7542245e0c28c7d59a0399649e0cac9517a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2009 Nokia Corporation.
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Rygel is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

internal class Rygel.HTTPByteSeek : Rygel.HTTPSeek {
    public HTTPByteSeek (HTTPGet request) throws HTTPSeekError {
        Soup.Range[] ranges;
        int64 start = 0, total_length;
        unowned string range = request.msg.request_headers.get_one ("Range");

        if (request.thumbnail != null) {
            total_length = request.thumbnail.size;
        } else if (request.subtitle != null) {
            total_length = request.subtitle.size;
        } else {
            total_length = request.item.size;
        }
        var stop = total_length - 1;

        if (range != null) {
            if (request.msg.request_headers.get_ranges (total_length,
                                                        out ranges)) {
                // TODO: Somehow deal with multipart/byterange properly
                start = ranges[0].start;
                stop = ranges[0].end;
            } else {
                // Range header was present but invalid
                throw new HTTPSeekError.INVALID_RANGE (_("Invalid Range '%s'"),
                                                       range);
            }

            if (start > stop) {
                throw new HTTPSeekError.INVALID_RANGE (_("Invalid Range '%s'"),
                                                       range);
            }
        }

        base (request.msg, start, stop, 1, total_length);
    }

    public static bool needed (HTTPGet request) {
        return (request.item.size > 0 &&
                request.handler is HTTPIdentityHandler) ||
               (request.thumbnail != null &&
                request.thumbnail.size > 0) ||
               (request.subtitle != null && request.subtitle.size > 0) ||
               request.msg.request_headers.get_one ("User-Agent") ==
               "PLAYSTATION 3";
    }

    public static bool requested (HTTPGet request) {
        return request.msg.request_headers.get_one ("Range") != null;
    }

    public override void add_response_headers () {
        // Content-Range: bytes START_BYTE-STOP_BYTE/TOTAL_LENGTH
        var range = "bytes ";
        unowned Soup.MessageHeaders headers = this.msg.response_headers;

        if (this.msg.request_headers.get_one ("Range") != null) {
            headers.append ("Accept-Ranges", "bytes");

            range += this.start.to_string () + "-" +
                     this.stop.to_string () + "/" +
                     this.total_length.to_string ();
            headers.append ("Content-Range", range);
        }

        headers.set_content_length (this.length);
    }
}