summaryrefslogtreecommitdiff
path: root/tests/rygel-http-response-test.vala
blob: c6d9792516edcf1a31d836345f2e8acbe51d60b2 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
 * Copyright (C) 2010 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.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;

public errordomain Rygel.TestError {
    SKIP = 77,
    TIMEOUT
}

public errordomain Rygel.HTTPRequestError {
    NOT_FOUND = Soup.Status.NOT_FOUND
}

public class Rygel.HTTPResponseTest : GLib.Object {
    public const long MAX_BYTES = 102400;

    protected HTTPServer server;
    protected HTTPClient client;

    private bool server_done;
    private bool client_done;

    private MediaItem item;

    private MainLoop main_loop;

    protected Cancellable cancellable;
    private Error error;

    public static int main (string[] args) {
        try {
            var test = new HTTPResponseTest.complete ();
            test.run ();

            test = new HTTPResponseTest.abort ();
            test.run ();
        } catch (TestError.SKIP error) {
            return error.code;
        } catch (Error error) {
            critical ("%s", error.message);

            return -1;
        }

        return 0;
    }

    public HTTPResponseTest (Cancellable? cancellable = null) throws Error {
        this.cancellable = cancellable;

        this.server = new HTTPServer ();
        this.client = new HTTPClient (this.server.context,
                                      this.server.uri,
                                      MAX_BYTES,
                                      cancellable != null);
        this.main_loop = new MainLoop (null, false);
    }

    public HTTPResponseTest.complete () throws Error {
        this ();

        this.item = new MediaItem.fixed_size ();
    }

    public HTTPResponseTest.abort () throws Error {
        this (new Cancellable ());

        this.item = new MediaItem ();
    }

    public virtual void run () throws Error {
        Timeout.add_seconds (3, this.on_timeout);
        this.server.message_received.connect (this.on_message_received);
        this.server.message_aborted.connect (this.on_message_aborted);
        if (this.cancellable == null) {
            this.client.completed.connect (this.on_client_completed);
        } else {
            this.client_done = true;
        }

        this.client.run.begin ();

        this.main_loop.run ();

        if (this.error != null) {
            throw this.error;
        }
    }

    private HTTPResponse create_response (Soup.Message msg) throws Error {
        var seek = null as HTTPSeek;

        if (!this.item.is_live_stream ()) {
            seek = new HTTPByteSeek (0, MAX_BYTES - 1, this.item.size);
            msg.response_headers.set_content_length (seek.length);
        }

        var request = new HTTPGet (this.server.context.server,
                                   msg,
                                   this.item,
                                   seek,
                                   this.cancellable);
        var handler = new HTTPGetHandler (this.cancellable);
        var src = this.item.create_stream_source ();

        return new HTTPResponse (request, handler, src);
    }

    private void on_client_completed (StateMachine client) {
        if (this.server_done) {
            this.main_loop.quit ();
        }

        this.client_done = true;
    }

    private void on_response_completed (StateMachine response) {
        if (this.client_done) {
            this.main_loop.quit ();
        }

        this.server_done = true;
    }

    private void on_message_received (HTTPServer   server,
                                      Soup.Message msg) {
        try {
            var response = this.create_response (msg);

            response.run.begin ();

            response.completed.connect (this.on_response_completed);
        } catch (Error error) {
            this.error = error;
            this.main_loop.quit ();

            return;
        }
    }

    private void on_message_aborted (HTTPServer   server,
                                     Soup.Message msg) {
        this.cancellable.cancel ();
    }

    private bool on_timeout () {
        this.error = new TestError.TIMEOUT ("Timeout");
        this.main_loop.quit ();

        return false;
    }
}

public class Rygel.HTTPServer : GLib.Object {
    private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";

    public GUPnP.Context context;

    public string uri {
        owned get { return "http://" +
                           this.context.host_ip + ":" +
                           this.context.port.to_string () +
                           SERVER_PATH;
        }
    }

    public signal void message_received (Soup.Message message);
    public signal void message_aborted (Soup.Message message);

    public HTTPServer () throws TestError {
        try {
            this.context = new GUPnP.Context (null, "lo", 0);
        } catch (Error error) {
            throw new TestError.SKIP ("Network context not available");
        }

        assert (this.context != null);
        assert (this.context.host_ip != null);
        assert (this.context.port > 0);

        this.context.server.add_handler (SERVER_PATH, this.server_cb);
        this.context.server.request_aborted.connect (this.on_request_aborted);
    }

    private void server_cb (Server        server,
                            Soup.Message  msg,
                            string        path,
                            HashTable?    query,
                            ClientContext client) {
        this.context.server.pause_message (msg);
        this.message_received (msg);
    }

    private void on_request_aborted (Soup.Server        server,
                                     Soup.Message       message,
                                     Soup.ClientContext client) {
        this.message_aborted (message);
    }
}

public class Rygel.HTTPClient : GLib.Object, StateMachine {
    public GUPnP.Context context;
    public Soup.Message msg;
    public size_t total_bytes;

    public Cancellable cancellable { get; set; }

    public HTTPClient (GUPnP.Context context,
                       string        uri,
                       size_t        total_bytes,
                       bool          active) {
        this.context = context;
        this.total_bytes = total_bytes;

        this.msg = new Soup.Message ("HTTP",  uri);
        assert (this.msg != null);
        this.msg.response_body.set_accumulate (false);

        if (active) {
            this.cancellable = new Cancellable ();
            this.cancellable.cancelled.connect (this.on_cancelled);
        }
    }

    public async void run () {
        SourceFunc run_continue = run.callback;
        size_t bytes_received = 0;

        this.msg.got_chunk.connect ((msg, chunk) => {
            bytes_received += chunk.length;

            if (bytes_received >= this.total_bytes &&
                this.cancellable != null) {

                this.cancellable.cancel ();
            }
        });

        this.context.session.queue_message (this.msg, (session, msg) => {
            assert (cancellable != null || bytes_received == this.total_bytes);

            run_continue ();
        });

        yield;

        this.completed ();
    }

    private void on_cancelled (Cancellable cancellable) {
        this.context.session.cancel_message (this.msg,
                                             Status.CANCELLED);
        this.completed ();
    }
}

public class Rygel.HTTPSeek : GLib.Object {
    public int64 start { get; private set; }
    public int64 stop { get; private set; }
    public int64 length { get; private set; }
    public int64 total_length { get; private set; }

    public HTTPSeek (int64 start, int64 stop, int64 total_length) {
        this.start = start;
        this.stop = stop;
        this.total_length = total_length;

        this.length = stop - start + 1;
    }
}

public class Rygel.HTTPByteSeek : Rygel.HTTPSeek {
    public HTTPByteSeek (int64 start, int64 stop, int64 total_length) {
        base (start, stop, total_length);
    }
}

public class Rygel.HTTPTimeSeek : Rygel.HTTPSeek {
    public HTTPTimeSeek (int64 start, int64 stop, int64 total_length) {
        base (start, stop, total_length);
    }
}

public class Rygel.HTTPGet : GLib.Object {
    public Soup.Server server;
    public Soup.Message msg;

    public Cancellable cancellable;

    public MediaItem item;

    internal HTTPSeek seek;

    public HTTPGet (Soup.Server  server,
                    Soup.Message msg,
                    MediaItem    item,
                    HTTPSeek?    seek,
                    Cancellable? cancellable) {
        this.server = server;
        this.msg = msg;
        this.item = item;
        this.seek = seek;
        this.cancellable = cancellable;
        this.msg.response_headers.set_encoding (Soup.Encoding.EOF);
        this.msg.set_status (Soup.Status.OK);
    }
}

public class Rygel.HTTPGetHandler : GLib.Object {
    public Cancellable cancellable;

    public HTTPGetHandler (Cancellable? cancellable) {
        this.cancellable = cancellable;
    }
}

internal class Rygel.TestDataSource : Rygel.DataSource, Object {
    private long block_size;
    private long buffers;
    private uint64 data_sent;
    private bool frozen;

    public TestDataSource (long block_size, long buffers) {
        this.block_size = block_size;
        this.buffers = buffers;
        this.data_sent = 0;
    }

    public void start (HTTPSeek? seek) throws Error {
        Idle.add ( () => {
            if (frozen) {
                return false;
            }

            var data = new uint8[block_size];
            this.data_sent += block_size;
            if (this.data_sent > HTTPResponseTest.MAX_BYTES) {
                this.done ();

                return false;
            }

            this.data_available (data);

            return true;
        });
    }

    public void freeze () {
        this.frozen = true;
    }

    public void thaw () {
        if (!this.frozen) {
            return;
        }

        this.frozen = false;

        try {
            this.start (null);
        } catch (GLib.Error error) {
            assert_not_reached ();
        }
    }

    public void stop () {
        this.freeze ();
    }
}

public class Rygel.MediaItem {
    private const long BLOCK_SIZE = HTTPResponseTest.MAX_BYTES / 16;
    private const long MAX_BUFFERS = 25;

    public int64 size {
        get {
            return MAX_BUFFERS * BLOCK_SIZE;
        }
    }

    private DataSource src;
    bool is_live = false;

    public MediaItem () {
        this.src = new TestDataSource (BLOCK_SIZE, MAX_BUFFERS);
        this.is_live = true;
    }

    public MediaItem.fixed_size () {
        this ();
    }

    public DataSource? create_stream_source () {
        return this.src;
    }

    public bool is_live_stream () {
        return this.is_live;
    }
}