[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] dynamic-modules ada5c5e 13/22: new module: curl. basic URL
From: |
Noah Friedman |
Subject: |
[Emacs-diffs] dynamic-modules ada5c5e 13/22: new module: curl. basic URL fetcher working. |
Date: |
Mon, 8 May 2017 19:46:14 -0400 (EDT) |
branch: dynamic-modules
commit ada5c5e86b8f642031656525ebb9f07cb84c7ecf
Author: Aurélien Aptel <address@hidden>
Commit: Ted Zlatanov <address@hidden>
new module: curl. basic URL fetcher working.
---
modules/curl/Makefile | 15 +++++++
modules/curl/curl.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 128 insertions(+)
diff --git a/modules/curl/Makefile b/modules/curl/Makefile
new file mode 100644
index 0000000..2e7fda0
--- /dev/null
+++ b/modules/curl/Makefile
@@ -0,0 +1,15 @@
+ROOT = ../..
+
+CFLAGS = `pkg-config libcurl --cflags`
+LDFLAGS = `pkg-config libcurl --libs`
+
+all: curl.so curl.doc
+
+%.so: %.o
+ gcc -shared $(LDFLAGS) -o $@ $<
+
+%.o: %.c
+ gcc -ggdb3 -Wall -I$(ROOT)/src -I$(ROOT)/lib $(CFLAGS) -fPIC -c $<
+
+%.doc: %.c
+ $(ROOT)/lib-src/make-docfile $< > $@
diff --git a/modules/curl/curl.c b/modules/curl/curl.c
new file mode 100644
index 0000000..f305a01
--- /dev/null
+++ b/modules/curl/curl.c
@@ -0,0 +1,113 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <curl/curl.h>
+
+#include <config.h>
+#include <lisp.h>
+
+int plugin_is_GPL_compatible;
+
+struct buffer
+{
+ char *p;
+ size_t size, capacity;
+};
+
+struct Lisp_CURL
+{
+ struct buffer buf;
+ CURL *curl;
+};
+
+#define XCURL(x) ((struct Lisp_CURL*)XSAVE_POINTER (x, 0))
+
+/* curl write callback */
+static size_t
+write_cb (void *src, size_t size, size_t nb, void *userp)
+{
+ struct buffer *buf = userp;
+ size_t total = size*nb;
+
+ if (buf->size + total > buf->capacity)
+ {
+ buf->capacity = 2 * (buf->size + total);
+ buf->p = realloc (buf->p, buf->capacity);
+ }
+
+ memcpy (buf->p + buf->size, src, total);
+ buf->size += total;
+ buf->p[buf->size] = 0;
+
+ return total;
+}
+
+
+EXFUN (Fcurl_make, 0);
+DEFUN ("curl-make", Fcurl_make, Scurl_make, 0, 0, 0,
+ doc: "Return a new CURL handle.")
+ (void)
+{
+ struct Lisp_CURL *p = calloc (sizeof (*p), 1);
+ p->buf.p = calloc (1, 1); /* so that realloc always work */
+ p->buf.capacity = 0;
+ p->curl = curl_easy_init ();
+ return make_save_ptr ((void*)p);
+}
+
+
+EXFUN (Fcurl_fetch_url, 2);
+DEFUN ("curl-fetch-url", Fcurl_fetch_url, Scurl_fetch_url, 2, 2, 0,
+ doc: "Fetch and store the content of URL using HANDLE.\n\n"
+ "Return t if successful otherwise return an error string.")
+ (Lisp_Object handle, Lisp_Object url)
+{
+ CURLcode res;
+ struct Lisp_CURL *c = XCURL (handle);
+
+ curl_easy_setopt (c->curl, CURLOPT_URL, "http://www.example.com/");
+ curl_easy_setopt (c->curl, CURLOPT_WRITEFUNCTION, write_cb);
+ curl_easy_setopt (c->curl, CURLOPT_WRITEDATA, (void*)&c->buf);
+ curl_easy_setopt (c->curl, CURLOPT_USERAGENT, "curl-in-emacs/1.0");
+ res = curl_easy_perform (c->curl);
+
+ if (res != CURLE_OK)
+ {
+ const char* error = curl_easy_strerror (res);
+ return make_string (error, strlen (error));
+ }
+
+ return Qt;
+}
+
+EXFUN (Fcurl_content, 1);
+DEFUN ("curl-content", Fcurl_content, Scurl_content, 1, 1, 0,
+ doc: "Return the content of a successful fetch made in HANDLE.")
+ (Lisp_Object handle)
+{
+ struct Lisp_CURL *c = XCURL (handle);
+ return make_string (c->buf.p, c->buf.size);
+}
+
+EXFUN (Fcurl_free, 1);
+DEFUN ("curl-free", Fcurl_free, Scurl_free, 1, 1, 0,
+ doc: "Free curl HANDLE.")
+ (Lisp_Object handle)
+{
+ struct Lisp_CURL *c = XCURL (handle);
+ free (c->buf.p);
+ curl_easy_cleanup (c->curl);
+
+ return Qt;
+}
+
+void init ()
+{
+ curl_global_init (CURL_GLOBAL_ALL);
+ /* when unloading: curl_global_cleanup(); */
+
+ defsubr (&Scurl_make);
+ defsubr (&Scurl_fetch_url);
+ defsubr (&Scurl_content);
+ defsubr (&Scurl_free);
+}
- [Emacs-diffs] dynamic-modules 334564f 15/22: fix docstring., (continued)
- [Emacs-diffs] dynamic-modules 334564f 15/22: fix docstring., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 18920eb 19/22: new yaml parsing module using libyaml, Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 3f762bd 02/22: add `load-module' function to emacs lisp., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 1be7b68 10/22: new module: opaque, Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 4efd6a2 21/22: yaml module: rename `yaml-parse' to `yaml-parse-buffer'., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 6334cf7 18/22: use `load-module-suffixes' as a source of module suffixes., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 106c15b 14/22: curl: use url arg., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 9313182 22/22: configure.ac: dynamic modules off by default., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules e374b97 11/22: opaque: add comments., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules 399d8ec 16/22: make `load' look for and handle dynamic modules., Noah Friedman, 2017/05/08
- [Emacs-diffs] dynamic-modules ada5c5e 13/22: new module: curl. basic URL fetcher working.,
Noah Friedman <=