2008-10-23 22:09:12 +00:00
|
|
|
/*
|
2009-03-16 22:54:45 +00:00
|
|
|
Copyright (C) 2008-2009 Christian Dywan <christian@twotoasts.de>
|
2008-10-23 22:09:12 +00:00
|
|
|
|
|
|
|
This library 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.
|
|
|
|
|
|
|
|
See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2009-12-19 09:39:24 +00:00
|
|
|
#if HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2008-10-23 22:09:12 +00:00
|
|
|
#include "katze-net.h"
|
2011-10-28 20:47:17 +00:00
|
|
|
#include "midori-core.h"
|
2008-10-23 22:09:12 +00:00
|
|
|
|
2009-12-19 09:39:24 +00:00
|
|
|
#include <glib/gstdio.h>
|
2009-03-16 22:54:45 +00:00
|
|
|
#include <libsoup/soup.h>
|
2008-10-23 22:09:12 +00:00
|
|
|
|
|
|
|
struct _KatzeNet
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _KatzeNetClass
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (KatzeNet, katze_net, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_net_finalize (GObject* object);
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_net_class_init (KatzeNetClass* class)
|
|
|
|
{
|
|
|
|
GObjectClass* gobject_class;
|
|
|
|
|
|
|
|
gobject_class = G_OBJECT_CLASS (class);
|
|
|
|
gobject_class->finalize = katze_net_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_net_init (KatzeNet* net)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_net_finalize (GObject* object)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (katze_net_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
KatzeNetStatusCb status_cb;
|
|
|
|
KatzeNetTransferCb transfer_cb;
|
|
|
|
gpointer user_data;
|
|
|
|
KatzeNetRequest* request;
|
|
|
|
} KatzeNetPriv;
|
|
|
|
|
2015-09-12 00:47:06 +00:00
|
|
|
#ifndef HAVE_WEBKIT2
|
2008-10-23 22:09:12 +00:00
|
|
|
static void
|
|
|
|
katze_net_priv_free (KatzeNetPriv* priv)
|
|
|
|
{
|
2010-10-21 19:05:36 +00:00
|
|
|
KatzeNetRequest* request = priv->request;
|
2008-10-23 22:09:12 +00:00
|
|
|
g_free (request->uri);
|
|
|
|
g_free (request->mime_type);
|
|
|
|
g_free (request->data);
|
2010-10-21 19:05:36 +00:00
|
|
|
g_slice_free (KatzeNetRequest, request);
|
|
|
|
g_slice_free (KatzeNetPriv, priv);
|
2008-10-23 22:09:12 +00:00
|
|
|
}
|
|
|
|
|
2008-10-27 02:03:20 +00:00
|
|
|
static void
|
|
|
|
katze_net_got_body_cb (SoupMessage* msg,
|
|
|
|
KatzeNetPriv* priv);
|
|
|
|
|
2008-10-23 22:09:12 +00:00
|
|
|
static void
|
|
|
|
katze_net_got_headers_cb (SoupMessage* msg,
|
|
|
|
KatzeNetPriv* priv)
|
|
|
|
{
|
2010-10-04 21:58:19 +00:00
|
|
|
KatzeNetRequest* request = priv->request;
|
2008-10-23 22:09:12 +00:00
|
|
|
|
|
|
|
switch (msg->status_code)
|
|
|
|
{
|
|
|
|
case 200:
|
|
|
|
request->status = KATZE_NET_VERIFIED;
|
|
|
|
break;
|
|
|
|
case 301:
|
|
|
|
request->status = KATZE_NET_MOVED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
request->status = KATZE_NET_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!priv->status_cb (request, priv->user_data))
|
2008-10-24 17:58:58 +00:00
|
|
|
{
|
|
|
|
g_signal_handlers_disconnect_by_func (msg, katze_net_got_headers_cb, priv);
|
2008-10-27 02:03:20 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (msg, katze_net_got_body_cb, priv);
|
2010-10-04 21:58:19 +00:00
|
|
|
soup_session_cancel_message (webkit_get_default_session (), msg, 1);
|
2008-10-24 17:58:58 +00:00
|
|
|
}
|
2008-10-23 22:09:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_net_got_body_cb (SoupMessage* msg,
|
|
|
|
KatzeNetPriv* priv)
|
|
|
|
{
|
2010-10-04 21:58:19 +00:00
|
|
|
KatzeNetRequest* request = priv->request;
|
2008-10-23 22:09:12 +00:00
|
|
|
|
|
|
|
if (msg->response_body->length > 0)
|
|
|
|
{
|
|
|
|
request->data = g_memdup (msg->response_body->data,
|
|
|
|
msg->response_body->length);
|
|
|
|
request->length = msg->response_body->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->transfer_cb (request, priv->user_data);
|
2008-10-24 17:58:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
katze_net_finished_cb (SoupMessage* msg,
|
|
|
|
KatzeNetPriv* priv)
|
|
|
|
{
|
2008-10-23 22:09:12 +00:00
|
|
|
katze_net_priv_free (priv);
|
|
|
|
}
|
|
|
|
|
2009-01-09 21:54:40 +00:00
|
|
|
static gboolean
|
2008-10-23 22:09:12 +00:00
|
|
|
katze_net_default_cb (KatzeNetPriv* priv)
|
|
|
|
{
|
|
|
|
KatzeNetRequest* request;
|
|
|
|
|
|
|
|
request = priv->request;
|
|
|
|
request->status = KATZE_NET_NOT_FOUND;
|
|
|
|
if (priv->status_cb)
|
|
|
|
priv->status_cb (request, priv->user_data);
|
|
|
|
katze_net_priv_free (priv);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2015-09-12 00:47:06 +00:00
|
|
|
#endif
|
2008-10-23 22:09:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* katze_net_load_uri:
|
2010-05-25 16:50:16 +00:00
|
|
|
* @net: a #KatzeNet, or %NULL
|
2008-10-23 22:09:12 +00:00
|
|
|
* @uri: an URI string
|
|
|
|
* @status_cb: function to call for status information
|
|
|
|
* @transfer_cb: function to call upon transfer
|
|
|
|
* @user_data: data to pass to the callback
|
|
|
|
*
|
|
|
|
* Requests a transfer of @uri.
|
|
|
|
*
|
|
|
|
* @status_cb will be called when the status of @uri
|
|
|
|
* is verified. The specified callback may be called
|
|
|
|
* multiple times unless cancelled.
|
|
|
|
*
|
|
|
|
* @transfer_cb will be called when the data @uri is
|
|
|
|
* pointing to was transferred. Note that even a failed
|
|
|
|
* transfer may transfer data.
|
|
|
|
*
|
|
|
|
* @status_cb will always to be called at least once.
|
|
|
|
**/
|
|
|
|
void
|
|
|
|
katze_net_load_uri (KatzeNet* net,
|
|
|
|
const gchar* uri,
|
|
|
|
KatzeNetStatusCb status_cb,
|
|
|
|
KatzeNetTransferCb transfer_cb,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2013-10-24 03:45:02 +00:00
|
|
|
#ifndef HAVE_WEBKIT2
|
2008-10-23 22:09:12 +00:00
|
|
|
KatzeNetRequest* request;
|
|
|
|
KatzeNetPriv* priv;
|
|
|
|
SoupMessage* msg;
|
|
|
|
|
|
|
|
g_return_if_fail (uri != NULL);
|
|
|
|
|
|
|
|
if (!status_cb && !transfer_cb)
|
|
|
|
return;
|
|
|
|
|
2010-10-21 19:05:36 +00:00
|
|
|
request = g_slice_new (KatzeNetRequest);
|
2008-10-23 22:09:12 +00:00
|
|
|
request->uri = g_strdup (uri);
|
|
|
|
request->mime_type = NULL;
|
|
|
|
request->data = NULL;
|
|
|
|
|
2010-10-21 19:05:36 +00:00
|
|
|
priv = g_slice_new (KatzeNetPriv);
|
2008-10-23 22:09:12 +00:00
|
|
|
priv->status_cb = status_cb;
|
|
|
|
priv->transfer_cb = transfer_cb;
|
|
|
|
priv->user_data = user_data;
|
|
|
|
priv->request = request;
|
|
|
|
|
2011-10-28 20:47:17 +00:00
|
|
|
if (midori_uri_is_http (uri))
|
2008-10-23 22:09:12 +00:00
|
|
|
{
|
|
|
|
msg = soup_message_new ("GET", uri);
|
|
|
|
if (status_cb)
|
|
|
|
g_signal_connect (msg, "got-headers",
|
|
|
|
G_CALLBACK (katze_net_got_headers_cb), priv);
|
|
|
|
if (transfer_cb)
|
|
|
|
g_signal_connect (msg, "got-body",
|
|
|
|
G_CALLBACK (katze_net_got_body_cb), priv);
|
2008-10-24 17:58:58 +00:00
|
|
|
g_signal_connect (msg, "finished",
|
|
|
|
G_CALLBACK (katze_net_finished_cb), priv);
|
2010-10-04 21:58:19 +00:00
|
|
|
soup_session_queue_message (webkit_get_default_session (), msg, NULL, NULL);
|
2008-10-23 22:09:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-12 00:47:06 +00:00
|
|
|
g_idle_add ((GSourceFunc)katze_net_default_cb, priv);
|
2013-10-24 03:45:02 +00:00
|
|
|
#endif
|
2008-10-23 22:09:12 +00:00
|
|
|
}
|
|
|
|
|