/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libsoup/soup.h>

SoupSession *session;

void got_headers(SoupMessage *msg, GMainLoop *loop)
{
  const gchar *header = soup_message_headers_get(msg->response_headers, "Location");
  if (header)
    g_warning("Location: %s", header);
  else
    {
      g_warning("Status: %d", msg->status_code);
      g_main_loop_quit(loop);
    }
}

void restarted(SoupMessage *msg, gpointer data)
{
  gchar *uri = soup_uri_to_string(soup_message_get_uri(msg), FALSE);
  g_warning("restarted... uri: %s", uri);
  g_free(uri);
}

int main (int argc, char **argv)
{
  SoupMessage *msg;
  GMainLoop *loop;

  g_thread_init(NULL);
  g_type_init();

  loop = g_main_loop_new(NULL, TRUE);

  session = soup_session_async_new();

  msg = soup_message_new("GET", "http://thepiratebay.org/s/?q=foo+bar&page=0&orderby=99");

  g_signal_connect(msg, "got-headers", G_CALLBACK(got_headers), loop);
  g_signal_connect(msg, "restarted", G_CALLBACK(restarted), NULL);

  soup_session_queue_message(session, msg, NULL, NULL);

  g_main_loop_run(loop);

  return 0;
}
