summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-v1-hacks.vala
blob: 09a220a36889d6e4da229add5064c33d1be4d04a (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2011 Nokia Corporation.
 * Copyright (C) 2012 Jens Georg.
 *
 * Author: Jens Georg <jensg@openismus.com>
 *         Jens Georg <mail@jensge.org>
 *
 * 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.1 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using Soup;
using GUPnP;

/**
 * Various devices that need a downgrade to MediaServer:1 and
 * ContentDirectory:1 because they ignore that higher versions are
 * required to be backwards-compatible.
 */
// FIXME: internal
public class Rygel.V1Hacks : Object {
    private const string[] AGENTS = { "Allegro-Software-WebClient",
                                      "SEC HHP",
                                      "SEC_HHP",
                                      "Mediabolic-IMHTTP/1.",
                                      "TwoPlayer",
                                      "Reciva",
                                      "FDSSDP",
                                      "Portable SDK for UPnP devices",
                                      "Darwin"};

    private string _device_type;
    public string device_type {
        construct set {
            this._device_type = value;
            this.device_type_v1 = value + ":1";
        }
        get { return this._device_type; }
    }
    private string device_type_v1;

    public string[] service_types { construct; get; }

    private const string MATCHING_PATTERN = ".*%s.*";
    private const string SERVICE_TYPE_PATTERN = ":[0-9]+$";

    private static string agent_pattern;

    public string description_path;

    private Regex agent_regex;
    private Regex service_type_regex;

    /**
     * Read the user-agent snippets from the config file and generate the
     * regular expression string for matching.
     *
     * Returns: A regular expression pattern matching any of the configured
     *          user-agents.
     */
    private static string generate_agent_pattern () {
        if (agent_pattern != null) {
            return agent_pattern;
        }

        var config = MetaConfig.get_default ();
        var raw_agents = AGENTS;
        try {
            raw_agents = config.get_string_list ("general",
                                                 "force-downgrade-for").
                                                 to_array ();
        } catch (Error error) {}

        var agents = new string[0];
        foreach (var agent in raw_agents) {
            agents += MATCHING_PATTERN.printf
                                    (Regex.escape_string (agent));
        }

        if (agents.length > 0) {
            agent_pattern = string.joinv ("|", agents);
        } else {
            agent_pattern = "";
        }

        debug ("V1 downgrade will be applied for devices matching %s",
               agent_pattern);

        return agent_pattern;
    }

    public V1Hacks (string device_type,
                    string[] service_types) {
        Object (device_type : device_type,
                service_types : service_types);
    }

    public override void constructed () {
        base.constructed ();

        try {
            this.agent_regex = new Regex (generate_agent_pattern ());
            this.service_type_regex = new Regex (SERVICE_TYPE_PATTERN);
        } catch (Error error) { assert_not_reached (); }
    }

    public void apply_on_device (RootDevice device,
                                 string?    template_path) throws Error {
        if (!device.get_device_type ().has_prefix (device_type)) {
            return;
        }

        if (template_path == null) {
            return;
        }

        var description_file = new DescriptionFile (template_path);
        description_file.set_device_type (device_type_v1);

        foreach (var service_type in service_types) {
            var service_type_v1 = this.service_type_regex.replace_literal
                                        (service_type, -1, 0, ":1");
            description_file.modify_service_type (service_type, service_type_v1);
        }

        this.description_path = template_path.replace (".xml", "-v1.xml");
        description_file.save (this.description_path);

        var server_path = "/" + device.get_description_document_name ();
        if (this.agent_regex.get_pattern () != "") {
            device.context.host_path_for_agent (this.description_path,
                                                server_path,
                                                this.agent_regex);
        }
    }
}