Exemple #1
0
/*
 *  * Read from g_input_stream until we get 0 bytes read.  Then process
 *   * using the value of stream_type.  Finally try and read another multipart.
 *    */
static void
read_cb (GObject *source, GAsyncResult *async_result, gpointer user_data)
{
    //g_print ("read_cb: Enter\n");
    GInputStream *in = G_INPUT_STREAM (source);
    MultiPartData *multipart_data = (MultiPartData *) user_data;
    SoupMultipartInputStream *multipart = SOUP_MULTIPART_INPUT_STREAM (multipart_data->multipart);
    gssize bytes_read;

    bytes_read = g_input_stream_read_finish (in, async_result, &multipart_data->error);

    /* Read 0 bytes - try to start reading another part. */
    if (bytes_read <= 0) {
        g_input_stream_close_async (in,
                                    G_PRIORITY_DEFAULT,
                                    multipart_data->cancellable,
                                    close_cb,
                                    user_data);
        if (multipart_data->callback) {
            SoupBuffer *soup_buffer;
            //g_print ("callback\n");
            soup_buffer = soup_buffer_new(SOUP_MEMORY_TEMPORARY,
                                (guchar *) multipart_data->buffer->str,
                                multipart_data->buffer->len);
            multipart_data->callback (multipart_data->method,
                                      multipart_data->path,
                                      multipart_data->cancellable,
                                      multipart_data->error,
                                      multipart_data->headers,
                                      soup_buffer,
                                      multipart_data->user_data);
            soup_buffer_free(soup_buffer);
        }
        g_string_free (multipart_data->buffer, TRUE);
        if (multipart_data->error) {
            g_input_stream_close_async (G_INPUT_STREAM (multipart),
                                        G_PRIORITY_DEFAULT,
                                        multipart_data->cancellable,
                                        close_base_cb,
                                        user_data);
            return;
        }
        soup_multipart_input_stream_next_part_async (multipart_data->multipart,
                                                     G_PRIORITY_DEFAULT,
                                                     multipart_data->cancellable,
                                                     next_part_cb,
                                                     user_data);
        return;
    }
    multipart_data->buffer = g_string_append_len (multipart_data->buffer, buffer, bytes_read);
    g_input_stream_read_async (in,
                               buffer,
                               READ_BUFFER_SIZE,
                               G_PRIORITY_DEFAULT,
                               multipart_data->cancellable,
                               read_cb,
                               user_data);
    //g_print ("read_cb: Exit\n");
}
Exemple #2
0
/*
 * Try and read another multipart message. if in is NULL then there are no more
 * messages to read.
 */
static void
next_part_cb (GObject *source, GAsyncResult *async_result, gpointer user_data)
{
    //g_print ("next_part_cb: Enter\n");
    SoupMultipartInputStream *multipart = SOUP_MULTIPART_INPUT_STREAM (source);

    MultiPartData *multipart_data = (MultiPartData *) user_data;

    g_assert (SOUP_MULTIPART_INPUT_STREAM (source) == multipart_data->multipart);

    GInputStream *in = soup_multipart_input_stream_next_part_finish (multipart,
                                                       async_result,
                                                       &multipart_data->error);
    if (!in) {
        g_input_stream_close_async (G_INPUT_STREAM (multipart),
                                    G_PRIORITY_DEFAULT,
                                    multipart_data->cancellable,
                                    close_base_cb,
                                    user_data);
        return;
    }

    // Read the headers here.
    multipart_read_headers (multipart_data);

    multipart_data->buffer = g_string_sized_new(READ_BUFFER_SIZE);

    g_input_stream_read_async (in,
                               buffer,
                               READ_BUFFER_SIZE,
                               G_PRIORITY_DEFAULT,
                               multipart_data->cancellable,
                               read_cb,
                               user_data);
    //g_print ("next_part_cb: Exit\n");
}
Exemple #3
0
static void
multipart_next_part_cb (GObject *source, GAsyncResult *res, gpointer data)
{
    GMainLoop *loop = (GMainLoop*)data;
    GError *error = NULL;
    GInputStream *in;
    gsize read_size = READ_BUFFER_SIZE;

    g_assert (SOUP_MULTIPART_INPUT_STREAM (source) == multipart);

    in = soup_multipart_input_stream_next_part_finish (multipart, res, &error);

    if (error) {
        debug_printf (1, "  failed next part: %s\n", error->message);
        g_clear_error (&error);
        errors++;

        g_object_unref (multipart);
        g_main_loop_quit (loop);
        return;
    }

    if (!in) {
        if (passes != 4) {
            debug_printf (1, "  expected 4 parts, got %u\n", passes);
            errors++;
        }

        g_object_unref (multipart);
        g_main_loop_quit (loop);
        return;
    }

    check_headers (multipart, passes);

    if (g_object_get_data (G_OBJECT (multipart), "multipart-small-reads"))
        read_size = 4;

    g_input_stream_read_async (in, buffer, read_size,
                               G_PRIORITY_DEFAULT, NULL,
                               multipart_read_cb, data);
}