#include <stdlib.h>
#include <stdio.h>
#include <glib.h>
#include <libsoup/soup.h>

static void die(GError *error)
{
    printf("%s\n", error->message);
    exit(1);
}

int main(int argc, char **argv)
{
    unsigned char buf[4096];
    ssize_t n;
    GError *error = NULL;
    GInputStream *stream;
    SoupLogger *logger;

    SoupSession *s = soup_session_new();
    logger = soup_logger_new (SOUP_LOGGER_LOG_BODY, 1024);
    soup_session_add_feature (s, SOUP_SESSION_FEATURE (logger));

    SoupRequest *req = soup_session_request(s, argv[1], NULL);
    SoupMessage *msg = soup_request_http_get_message (SOUP_REQUEST_HTTP (req));
    soup_message_headers_replace (msg->request_headers, "Range", "100000-");
    stream = soup_session_send(s, msg, NULL, &error);
    if (!stream)
        die(error);
    g_input_stream_close(stream, NULL, NULL);

    soup_message_headers_replace (msg->request_headers, "Range", "1-");
    stream = soup_session_send(s, msg, NULL, &error);
    if (!stream)
        die(error);
    while ((n = g_input_stream_read(stream, buf, 4096, NULL, NULL)) > 0) {
        fwrite(buf, 1, n, stdout);
    }

    return 0;
}
