/* * valac --pkg=glib-2.0 --pkg=libsoup-2.4 redirect.vala * * Find the real URL (it even works with 302 HTTP status code). */ using GLib; using Soup; public class Request : Object { private string url; public Request (string resource) { this.url = resource; } private string get_real_uri (string resource) { Session session; Message msg; string new_url = null; /* Create Soup.* objects */ session = new Session (); msg = new Message ("GET", resource); /* Signal */ msg.got_headers.connect (() => { /* 302 */ if (msg.status_code == Status.FOUND) { new_url = msg.response_headers.get_one ("Location"); /* Finish processing request */ session.cancel_message (msg, Status.CANCELLED); } }); session.send_message (msg); if (new_url == null) { new_url = "%s".printf (resource); } return new_url; } public void send () { /* Find the real URI */ stdout.printf ("%s\n", get_real_uri (this.url)); } public static int main (string[] args) { //string uri = "http://ftp.netbsd.org/pub/NetBSD/security/advisories/NetBSD-SA2016-005.txt.asc"; //string uri = "http://rf.proxycast.org/1175697431602405376/13940-16.06.2016-ITEMA_21011329-0.mp3"; string uri = "http://www.podtrac.com/pts/redirect.mp4/201406.jb-dl.cdn.scaleengine.net/bsdnow/2016/bsd-0146.mp4"; var r = new Request (uri); r.send (); return 0; } }