Example #1
0
static void
multipart_handling_cb (GObject *source, GAsyncResult *res, gpointer data)
{
    GMainLoop *loop = (GMainLoop*)data;
    SoupRequest *request = SOUP_REQUEST (source);
    GError *error = NULL;
    GInputStream *in;
    SoupMessage *message;

    in = soup_request_send_finish (request, res, &error);
    message = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
    multipart = soup_multipart_input_stream_new (message, in);
    g_object_unref (message);
    g_object_unref (in);

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

        g_main_loop_quit (loop);
        return;
    }

    if (g_object_get_data (source, "multipart-small-reads"))
        g_object_set_data (G_OBJECT (multipart), "multipart-small-reads", GINT_TO_POINTER(1));

    soup_multipart_input_stream_next_part_async (multipart, G_PRIORITY_DEFAULT, NULL,
            multipart_next_part_cb, data);
}
Example #2
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");
}
Example #3
0
static void
multipart_read_cb (GObject *source, GAsyncResult *asyncResult, gpointer data)
{
    GMainLoop *loop = (GMainLoop*)data;
    GInputStream *in = G_INPUT_STREAM (source);
    GError *error = NULL;
    static gssize bytes_read_for_part = 0;
    gssize bytes_read;

    bytes_read = g_input_stream_read_finish (in, asyncResult, &error);

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

        g_input_stream_close_async (in, G_PRIORITY_DEFAULT, NULL,
                                    multipart_close_part_cb, NULL);
        g_object_unref (in);

        g_main_loop_quit (loop);
        return;
    }

    /* Read 0 bytes - try to start reading another part. */
    if (!bytes_read) {
        check_read (bytes_read_for_part, passes);
        bytes_read_for_part = 0;
        passes++;

        g_input_stream_close_async (in, G_PRIORITY_DEFAULT, NULL,
                                    multipart_close_part_cb, NULL);
        g_object_unref (in);

        soup_multipart_input_stream_next_part_async (multipart, G_PRIORITY_DEFAULT, NULL,
                multipart_next_part_cb, data);
        return;
    }

    bytes_read_for_part += bytes_read;
    g_input_stream_read_async (in, buffer, READ_BUFFER_SIZE,
                               G_PRIORITY_DEFAULT, NULL,
                               multipart_read_cb, data);
}
Example #4
0
/*
 * our multipart handler callback
 * If we make an invalid request (like trying to cancel when no recipe is running)
 * then status_code will not be 200 and we will exit out after propagating the error
 * to our own GError
 */
static void
request_sent_cb (GObject *source, GAsyncResult *async_result, gpointer user_data)
{

    //g_print ("request_sent_cb\n");
    MultiPartData *multipart_data = (MultiPartData *) user_data;

    SoupRequest *request = SOUP_REQUEST (source);
    GInputStream *in = soup_request_send_finish (request,
                                                 async_result,
                                                 &multipart_data->error);
    if (multipart_data->error) {
        g_object_unref(request);
        multipart_destroy (multipart_data);
        return;
    }

    SoupMessage *message = soup_request_http_get_message (SOUP_REQUEST_HTTP (request));
    g_object_unref(request);

    if (message->status_code != SOUP_STATUS_OK) {
        g_set_error_literal(&multipart_data->error,
                            RESTRAINT_ERROR,
                            message->status_code,
                            message->reason_phrase);
        multipart_destroy (multipart_data);
        return;
    }

    multipart_data->multipart = soup_multipart_input_stream_new (message,
                                                                 in);
    g_object_unref (message);
    g_object_unref (in);

    soup_multipart_input_stream_next_part_async (multipart_data->multipart,
                                                 G_PRIORITY_DEFAULT,
                                                 multipart_data->cancellable,
                                                 next_part_cb,
                                                 user_data);
}