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 {
67 Timeout.add_seconds (3, this.on_timeout);
68 this.server.message_received.connect (this.on_message_received);
69 this.client.completed.connect (this.on_client_completed);
71 this.client.run.begin ();
73 this.main_loop.run ();
75 if (this.error != null) {
80 private HTTPRequest create_request (Soup.Message msg) throws Error {
81 return new HTTPPost (this.server,
82 this.server.context.server,
86 private void on_client_completed (StateMachine client) {
87 this.client_done = true;
88 this.check_and_exit.begin ();
91 private void on_message_received (HTTPServer server,
93 this.handle_client_message.begin (msg);
96 private async void handle_client_message (Soup.Message msg) {
98 var request = this.create_request (msg);
100 yield request.run ();
102 assert ((request as HTTPPost).item != null);
104 this.server_done = true;
105 this.check_and_exit.begin ();
106 } catch (Error error) {
108 this.main_loop.quit ();
114 private bool on_timeout () {
115 this.error = new TestError.TIMEOUT ("Timeout");
116 this.main_loop.quit ();
121 private async void check_and_exit () {
122 if (!(this.server_done && this.client_done)) {
127 var file = this.server.root_container.item.file;
128 var stream = yield file.read_async (Priority.HIGH, null);
129 var buffer = new uint8[HTTPClient.LENGTH];
131 yield stream.read_async (buffer, Priority.HIGH, null);
133 for (var i = 0; i < HTTPClient.LENGTH; i++) {
134 assert (buffer[i] == this.client.content[i]);
136 } catch (Error error) {
140 this.main_loop.quit ();
144 public class Rygel.HTTPServer : GLib.Object {
145 private const string SERVER_PATH = "/RygelHTTPServer/Rygel/Test";
146 public string path_root {
152 public MediaContainer root_container;
153 public GUPnP.Context context;
157 var item_uri = new HTTPItemURI (this.root_container.ITEM_ID,
160 return item_uri.to_string ();
164 public signal void message_received (Soup.Message message);
166 public HTTPServer () throws TestError {
168 this.context = new GUPnP.Context (null, "lo", 0);
169 } catch (Error error) {
170 throw new TestError.SKIP ("Network context not available");
173 assert (this.context != null);
174 assert (this.context.host_ip != null);
175 assert (this.context.port > 0);
177 context.server.request_started.connect (this.on_request_started);
179 this.root_container = new MediaContainer ();
182 private void on_request_started (Soup.Server server,
184 Soup.ClientContext client) {
185 msg.got_headers.connect (this.on_got_headers);
188 private void on_got_headers (Soup.Message msg) {
189 this.message_received (msg);
193 public class Rygel.HTTPClient : GLib.Object, StateMachine {
194 public const size_t LENGTH = 1024;
196 public uint8[] content;
198 public GUPnP.Context context;
199 public Soup.Message msg;
201 public Cancellable cancellable { get; set; }
203 public HTTPClient (GUPnP.Context context,
205 this.context = context;
206 this.content = new uint8[1024];
208 this.msg = new Soup.Message ("POST", uri);
209 assert (this.msg != null);
212 public async void run () {
213 SourceFunc run_continue = run.callback;
215 this.msg.request_body.append (MemoryUse.COPY, content);
217 this.context.session.queue_message (this.msg, (session, msg) => {
227 public class Rygel.MediaContainer : Rygel.MediaObject {
228 public const string ITEM_ID = "TestItem";
230 public signal void container_updated (MediaContainer container);
232 public string id = "TesContainer";
233 public MediaItem item;
235 private FileMonitor monitor;
237 public MediaContainer () {
238 this.item = new MediaItem (ITEM_ID, this);
240 this.monitor = this.item.file.monitor_file (FileMonitorFlags.NONE);
241 this.monitor.changed.connect (this.on_file_changed);
244 public async MediaObject? find_object (string item_id,
245 Cancellable? cancellable)
247 SourceFunc find_object_continue = find_object.callback;
249 find_object_continue ();
256 if (item_id == ITEM_ID) {
263 public void on_file_changed (FileMonitor monitor,
266 FileMonitorEvent event_type) {
267 this.container_updated (this);
271 public class Rygel.MediaItem : Rygel.MediaObject {
272 public const string URI = "file:///tmp/rygel-upload-test.wav";
274 public weak MediaContainer parent;
277 public long size = 1024;
278 public long duration = 1024;
280 public bool place_holder = true;
284 public MediaItem (string id, MediaContainer parent) {
286 this.parent = parent;
288 this.file = File.new_for_uri (URI);
293 this.file.delete (null);
294 } catch (GLib.Error error) {
295 assert_not_reached ();
299 public async File? get_writable (Cancellable? cancellable) throws Error {
300 SourceFunc get_writable_continue = get_writable.callback;
303 get_writable_continue ();
314 internal class Rygel.HTTPResponse : Rygel.StateMachine, GLib.Object {
315 public Cancellable cancellable { get; set; }
317 private Soup.Message msg;
318 private Soup.Server server;
320 public HTTPResponse (HTTPPost get_request) {
321 this.msg = get_request.msg;
322 this.server = get_request.server;
325 public async void run () {
326 SourceFunc run_continue = run.callback;
336 this.msg.set_status (Soup.KnownStatusCode.OK);
337 this.server.unpause_message (msg);
343 public class Rygel.ItemRemovalQueue: GLib.Object {
344 public static ItemRemovalQueue get_default () {
345 return new ItemRemovalQueue ();
348 public bool dequeue (MediaItem item) {
352 public async void remove_now (MediaItem item, Cancellable? cancellable) {
353 Idle.add (remove_now.callback);
359 public class Rygel.MediaObject : GLib.Object {}
361 public errordomain Rygel.ContentDirectoryError {