1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/*
This file is part of mpdscribble,
another audioscrobbler plugin for music player daemon.
Copyright © 2005 Kuno Woudt <kuno@frob.nl>
mpdscribble is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
mpdscribble is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with mpdscribble; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libsoup/soup.h>
#include "conn.h"
#include "misc.h"
#include "as.h"
struct global {
SoupSession *session;
SoupUri *base_uri;
char *base;
int pending;
char *receive;
size_t count;
callback_t *callback;
GMainLoop *mainloop;
};
static struct global g;
void
conn_grow_buffer (size_t new)
{
g.receive = realloc (g.receive, new);
if (!g.receive)
fatal ("out of memory");
}
static void
conn_callback (SoupMessage * msg, gpointer uri)
{
size_t l;
if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
{
l = msg->response.length;
conn_grow_buffer (g.count + l);
memcpy (g.receive + g.count, msg->response.body, l);
g.count += l;
}
soup_uri_free (uri);
if (!--g.pending)
{
g.callback (g.count, g.receive);
free (g.receive);
g.receive = NULL;
g.count = 0;
g_main_loop_quit (g.mainloop);
g_main_loop_unref (g.mainloop);
}
}
void
conn_setup (void)
{
g_type_init ();
g_thread_init (NULL);
g.base_uri = NULL;
g.receive = NULL;
g.pending = 0;
g.count = 0;
}
static int
conn_mainloop_quit (void *data)
{
g_main_loop_quit (g.mainloop);
}
int
conn_initiate (char *url, callback_t *callback, char *post_data,
unsigned int seconds)
{
SoupMessage *msg;
g.callback = callback;
g.base = url;
g.base_uri = soup_uri_new (g.base);
if (!g.base_uri)
fatal ("Could not parse '%s' as a URL", g.base);
g.session = soup_session_async_new ();
if (post_data)
{
msg = soup_message_new (SOUP_METHOD_POST, g.base);
soup_message_set_request
(msg, "application/x-www-form-urlencoded",
SOUP_BUFFER_USER_OWNED, post_data, strlen (post_data));
soup_message_add_header (msg->request_headers, "User-Agent",
AS_CLIENT_ID "/" AS_CLIENT_VERSION);
soup_message_add_header (msg->request_headers, "Pragma", "no-cache");
soup_message_add_header (msg->request_headers, "Accept", "*/*");
}
else
{
msg = soup_message_new (SOUP_METHOD_GET, g.base);
}
soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT);
g.pending++;
soup_session_queue_message (g.session, msg,
conn_callback, soup_uri_new (g.base));
g.mainloop = g_main_loop_new (g_main_context_default (), FALSE);
g_timeout_add (seconds * 1000, &conn_mainloop_quit, NULL);
return CONN_OK;
}
int
conn_pending (void)
{
return g.pending;
}
int
conn_poll (void)
{
g_main_loop_run (g.mainloop);
/*
while (g_main_context_pending (NULL))
g_main_context_iteration(NULL, FALSE);
*/
if (!g.pending && g.base_uri)
{
soup_uri_free (g.base_uri);
g.base_uri = NULL;
}
return g.pending;
}
void
conn_cleanup (void)
{
if (g.receive)
free (g.receive);
}
|