annotate src/c/feed.c @ 7:05a28a77f6fe

'show' instance for 'document'
author Adam Chlipala <adam@chlipala.net>
date Thu, 10 Mar 2011 18:36:50 -0500
parents ad85b8813e8a
children
rev   line source
adam@0 1 #include <ctype.h>
adam@0 2 #include <string.h>
adam@0 3 #include <stdlib.h>
adam@0 4
adam@0 5 #include <curl/curl.h>
adam@0 6
adam@0 7 #include <feed.h>
adam@0 8
adam@0 9 #define BUF_INIT 1024
adam@0 10 #define BUF_MAX 1073741824
adam@0 11
adam@0 12 uw_unit uw_FeedFfi_init(uw_context ctx) {
adam@0 13 curl_global_init(CURL_GLOBAL_ALL);
adam@0 14
adam@0 15 return uw_unit_v;
adam@0 16 }
adam@0 17
adam@0 18 static CURL *curl(uw_context ctx) {
adam@0 19 CURL *r;
adam@0 20
adam@0 21 if (!(r = uw_get_global(ctx, "curl"))) {
adam@0 22 r = curl_easy_init();
adam@0 23 if (r)
adam@0 24 uw_set_global(ctx, "curl", r, curl_easy_cleanup);
adam@0 25 }
adam@0 26
adam@0 27 return r;
adam@0 28 }
adam@0 29
adam@0 30 static void free_buffer(void *data) {
adam@0 31 uw_buffer *buf = data;
adam@0 32
adam@0 33 uw_buffer_free(buf);
adam@0 34 free(buf);
adam@0 35 }
adam@0 36
adam@0 37 static uw_buffer *curl_buffer(uw_context ctx) {
adam@0 38 uw_buffer *r;
adam@0 39
adam@0 40 if (!(r = uw_get_global(ctx, "curl_buffer"))) {
adam@0 41 r = malloc(sizeof(uw_buffer));
adam@0 42 if (r) {
adam@0 43 uw_buffer_init(BUF_MAX, r, BUF_INIT);
adam@0 44 uw_set_global(ctx, "curl_buffer", r, free_buffer);
adam@0 45 }
adam@0 46 }
adam@0 47
adam@0 48 return r;
adam@0 49 }
adam@0 50
adam@0 51 static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
adam@0 52 uw_buffer *out = userp;
adam@0 53
adam@0 54 uw_buffer_append(out, buffer, size * nmemb);
adam@0 55
adam@0 56 return size * nmemb;
adam@0 57 }
adam@0 58
adam@0 59 uw_Basis_string uw_FeedFfi_fetch(uw_context ctx, uw_Basis_string url) {
adam@0 60 CURL *c = curl(ctx);
adam@0 61 uw_buffer *b = curl_buffer(ctx);
adam@0 62 CURLcode code;
adam@0 63
adam@0 64 uw_buffer_reset(b);
adam@0 65
adam@0 66 curl_easy_setopt(c, CURLOPT_URL, url);
adam@0 67 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_data);
adam@0 68 curl_easy_setopt(c, CURLOPT_WRITEDATA, b);
adam@0 69
adam@0 70 code = curl_easy_perform(c);
adam@0 71
adam@0 72 if (code)
adam@0 73 uw_error(ctx, FATAL, "Error fetching URL %s", url);
adam@0 74 else {
adam@0 75 uw_buffer_append(b, "", 1);
adam@0 76 return uw_strdup(ctx, b->start);
adam@0 77 }
adam@0 78 }