2 * Copyright (C) 2010 Nokia Corporation.
4 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
5 * <zeeshan.ali@nokia.com>
7 * This file is part of Rygel.
9 * Rygel is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * Rygel is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27 public errordomain Rygel.TestError {
32 public class Rygel.HTTPPostTest : GLib.Object {
33 protected HTTPServer server;
34 protected HTTPClient client;
36 private bool server_done;
37 private bool client_done;
39 private MainLoop main_loop;
43 public static int main (string[] args) {
45 var test = new HTTPPostTest ();
48 } catch (TestError.SKIP error) {
50 } catch (Error error) {
51 critical ("%s", error.message);
59 public HTTPPostTest () throws Error {
60 this.server = new HTTPServer ();
61 this.client = new HTTPClient (this.server.context,
63 this.main_loop = new MainLoop (null, false);
66 public virtual void run () throws Error {
68 var file = File.new_for_uri (MediaItem.URI);
69 FileUtils.remove (file.get_path ());
70 Timeout.add_seconds (3, this.on_timeout);
71 this.server.message_received.connect (this.on_message_received);
72 this.client.completed.connect (this.on_client_completed);
74 this.client.run.begin ();
76 this.main_loop.run ();
78 if (this.error != null) {
83 private HTTPRequest create_request (Soup.Message msg) throws Error {
84 return new HTTPPost (this.server,
85 this.server.context.server,
89 private void on_client_completed (StateMachine client) {
90 this.client_done = true;
91 this.check_and_exit.begin ();
94 private void on_message_received (HTTPServer server,
96 this.handle_client_message.begin (msg);
99 private async void handle_client_message (Soup.Message msg) {
101 var request = this.create_request (msg);
103 yield request.run ();
105 assert ((request as HTTPPost).item != null);
107 this.server_done = true;
108 this.check_and_exit.begin ();
109 } catch (Error error) {
111 this.main_loop.quit ();
117 private bool on_timeout () {
118 this.error = new TestError.TIMEOUT ("Timeout");
119 this.main_loop.quit ();
124 private async void check_and_exit () {
125 if (!(this.server_done && this.client_done)) {
130 var file = this.server.root_container.item.file;
131 var stream = yield file.read_async (Priority.HIGH, null);
132 var buffer = new uint8[HTTPClient.LENGTH];
134 yield stream.read_async (buffer, Priority.HIGH, null);
136 for (var i = 0; i < HTTPClient.LENGTH; i++) {
137 assert (buffer[i] == this.client.content[i]);
139 } catch (Error error) {
143 this.main_loop.quit ();
147 public class Rygel.HTTPServer : GLib.Object {
148 private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
149 public string path_root {
155 public MediaContainer root_container;
156 public GUPnP.Context context;
160 var item_uri = new HTTPItemURI (this.root_container.ITEM_ID,
163 return item_uri.to_string ();
167 public signal void message_received (Soup.Message message);
169 public HTTPServer () throws TestError {
171 this.context = new GUPnP.Context (null, "lo", 0);
172 } catch (Error error) {
173 throw new TestError.SKIP ("Network context not available");
176 assert (this.context != null);
177 assert (this.context.host_ip != null);
178 assert (this.context.port > 0);
180 context.server.request_started.connect (this.on_request_started);
182 this.root_container = new MediaContainer ();
185 private void on_request_started (Soup.Server server,
187 Soup.ClientContext client) {
188 msg.got_headers.connect (this.on_got_headers);
191 private void on_got_headers (Soup.Message msg) {
192 this.message_received (msg);
196 public class Rygel.HTTPClient : GLib.Object, StateMachine {
197 public const size_t LENGTH = 1024;
199 public uint8[] content;
201 public GUPnP.Context context;
202 public Soup.Message msg;
204 public Cancellable cancellable { get; set; }
206 public HTTPClient (GUPnP.Context context,
208 this.context = context;
209 this.content = new uint8[1024];
211 this.msg = new Soup.Message ("POST", uri);
212 assert (this.msg != null);
215 public async void run () {
216 SourceFunc run_continue = run.callback;
218 this.msg.request_body.append (MemoryUse.COPY, content);
220 this.context.session.queue_message (this.msg, (session, msg) => {
230 public class Rygel.MediaContainer : Rygel.MediaObject {
231 public const string ITEM_ID = "TestItem";
233 public signal void container_updated (MediaContainer container);
235 public string id = "TesContainer";
236 public MediaItem item;
238 private FileMonitor monitor;
240 public MediaContainer () {
241 this.item = new MediaItem (ITEM_ID, this);
243 this.monitor = this.item.file.monitor_file (FileMonitorFlags.NONE);
244 this.monitor.changed.connect (this.on_file_changed);
247 public async MediaObject? find_object (string item_id,
248 Cancellable? cancellable)
250 SourceFunc find_object_continue = find_object.callback;
252 find_object_continue ();
259 if (item_id == ITEM_ID) {
266 public void on_file_changed (FileMonitor monitor,
269 FileMonitorEvent event_type) {
270 this.container_updated (this);
274 public class Rygel.MediaItem : Rygel.MediaObject {
275 public const string URI = "file:///tmp/rygel-upload-test.wav";
277 public weak MediaContainer parent;
280 public long size = 1024;
281 public long duration = 1024;
283 public bool place_holder = true;
287 public MediaItem (string id, MediaContainer parent) {
289 this.parent = parent;
291 this.file = File.new_for_uri (URI);
296 this.file.delete (null);
297 } catch (GLib.Error error) {
298 assert_not_reached ();
302 public async File? get_writable (Cancellable? cancellable) throws Error {
303 SourceFunc get_writable_continue = get_writable.callback;
306 get_writable_continue ();
317 internal class Rygel.HTTPResponse : Rygel.StateMachine, GLib.Object {
318 public Cancellable cancellable { get; set; }
320 private Soup.Message msg;
321 private Soup.Server server;
323 public HTTPResponse (HTTPPost get_request) {
324 this.msg = get_request.msg;
325 this.server = get_request.server;
328 public async void run () {
329 SourceFunc run_continue = run.callback;
339 this.msg.set_status (Soup.KnownStatusCode.OK);
340 this.server.unpause_message (msg);
346 public class Rygel.ItemRemovalQueue: GLib.Object {
347 public static ItemRemovalQueue get_default () {
348 return new ItemRemovalQueue ();
351 public bool dequeue (MediaItem item) {
355 public async void remove_now (MediaItem item, Cancellable? cancellable) {
356 Idle.add (remove_now.callback);
362 public class Rygel.MediaObject : GLib.Object {}
364 public errordomain Rygel.ContentDirectoryError {