diff options
author | Rob Bradford <rob@o-hand.com> | 2008-08-18 17:34:25 +0100 |
---|---|---|
committer | Rob Bradford <rob@o-hand.com> | 2008-08-18 17:34:25 +0100 |
commit | daacff4204496756124af8492a85a3a36ce591cc (patch) | |
tree | a42fbb708d50d4ec4241254cc7273e73a3a19653 | |
download | librest-daacff4204496756124af8492a85a3a36ce591cc.tar.gz |
Initial commit
-rw-r--r-- | AUTHORS | 0 | ||||
-rw-r--r-- | COPYING | 0 | ||||
-rw-r--r-- | ChangeLog | 0 | ||||
-rw-r--r-- | INSTALL | 237 | ||||
-rw-r--r-- | Makefile.am | 1 | ||||
-rw-r--r-- | NEWS | 0 | ||||
-rw-r--r-- | README | 0 | ||||
-rwxr-xr-x | autogen.sh | 3 | ||||
-rw-r--r-- | configure.ac | 36 | ||||
-rw-r--r-- | examples/Makefile.am | 5 | ||||
-rw-r--r-- | examples/test-raw.c | 38 | ||||
-rw-r--r-- | rest.pc.in | 12 | ||||
-rw-r--r-- | rest/Makefile.am | 7 | ||||
-rw-r--r-- | rest/rest-proxy.c | 302 | ||||
-rw-r--r-- | rest/rest-proxy.h | 60 |
15 files changed, 701 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/ChangeLog @@ -0,0 +1,237 @@ +Installation Instructions +************************* + +Copyright (C) 1994, 1995, 1996, 1999, 2000, 2001, 2002, 2004, 2005, +2006, 2007 Free Software Foundation, Inc. + +This file is free documentation; the Free Software Foundation gives +unlimited permission to copy, distribute and modify it. + +Basic Installation +================== + +Briefly, the shell commands `./configure; make; make install' should +configure, build, and install this package. The following +more-detailed instructions are generic; see the `README' file for +instructions specific to this package. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, and a +file `config.log' containing compiler output (useful mainly for +debugging `configure'). + + It can also use an optional file (typically called `config.cache' +and enabled with `--cache-file=config.cache' or simply `-C') that saves +the results of its tests to speed up reconfiguring. Caching is +disabled by default to prevent problems with accidental use of stale +cache files. + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If you are using the cache, and at +some point `config.cache' contains results you don't want to keep, you +may remove or edit it. + + The file `configure.ac' (or `configure.in') is used to create +`configure' by a program called `autoconf'. You need `configure.ac' if +you want to change it or regenerate `configure' using a newer version +of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. + + Running `configure' might take a while. While running, it prints + some messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + + 6. Often, you can also type `make uninstall' to remove the installed + files again. + +Compilers and Options +===================== + +Some systems require unusual options for compilation or linking that the +`configure' script does not know about. Run `./configure --help' for +details on some of the pertinent environment variables. + + You can give `configure' initial values for configuration parameters +by setting variables in the command line or in the environment. Here +is an example: + + ./configure CC=c99 CFLAGS=-g LIBS=-lposix + + *Note Defining Variables::, for more details. + +Compiling For Multiple Architectures +==================================== + +You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you can use GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + With a non-GNU `make', it is safer to compile the package for one +architecture at a time in the source code directory. After you have +installed the package for one architecture, use `make distclean' before +reconfiguring for another architecture. + +Installation Names +================== + +By default, `make install' installs the package's commands under +`/usr/local/bin', include files under `/usr/local/include', etc. You +can specify an installation prefix other than `/usr/local' by giving +`configure' the option `--prefix=PREFIX'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +pass the option `--exec-prefix=PREFIX' to `configure', the package uses +PREFIX as the prefix for installing programs and libraries. +Documentation and other data files still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=DIR' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + +Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + +There may be some features `configure' cannot figure out automatically, +but needs to determine by the type of machine the package will run on. +Usually, assuming the package is built to be run on the _same_ +architectures, `configure' can figure that out, but if it prints a +message saying it cannot guess the machine type, give it the +`--build=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name which has the form: + + CPU-COMPANY-SYSTEM + +where SYSTEM can have one of these forms: + + OS KERNEL-OS + + See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the machine type. + + If you are _building_ compiler tools for cross-compiling, you should +use the option `--target=TYPE' to select the type of system they will +produce code for. + + If you want to _use_ a cross compiler, that generates code for a +platform different from the build platform, you should specify the +"host" platform (i.e., that on which the generated programs will +eventually be run) with `--host=TYPE'. + +Sharing Defaults +================ + +If you want to set default values for `configure' scripts to share, you +can create a site shell script called `config.site' that gives default +values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Defining Variables +================== + +Variables not defined in a site shell script can be set in the +environment passed to `configure'. However, some packages may run +configure again during the build, and the customized values of these +variables may be lost. In order to avoid this problem, you should set +them in the `configure' command line, using `VAR=value'. For example: + + ./configure CC=/usr/local2/bin/gcc + +causes the specified `gcc' to be used as the C compiler (unless it is +overridden in the site shell script). + +Unfortunately, this technique does not work for `CONFIG_SHELL' due to +an Autoconf bug. Until the bug is fixed you can use this workaround: + + CONFIG_SHELL=/bin/bash /bin/bash ./configure CONFIG_SHELL=/bin/bash + +`configure' Invocation +====================== + +`configure' recognizes the following options to control how it operates. + +`--help' +`-h' + Print a summary of the options to `configure', and exit. + +`--version' +`-V' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`--cache-file=FILE' + Enable the cache: use and save the results of the tests in FILE, + traditionally `config.cache'. FILE defaults to `/dev/null' to + disable caching. + +`--config-cache' +`-C' + Alias for `--cache-file=config.cache'. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`configure' also accepts some other, not widely useful, options. Run +`configure --help' for more details. + diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..8d65a55 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = rest examples diff --git a/autogen.sh b/autogen.sh new file mode 100755 index 0000000..2217502 --- /dev/null +++ b/autogen.sh @@ -0,0 +1,3 @@ +#!/bin/sh +gtkdocize || exit 1 +autoreconf -v -i && ./configure $@ diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..62c3d4d --- /dev/null +++ b/configure.ac @@ -0,0 +1,36 @@ +AC_PREREQ(2.53) +AC_INIT(rest, 0.1) +AM_INIT_AUTOMAKE(AC_PACKAGE_NAME, AC_PACKAGE_VERSION) +AM_CONFIG_HEADER(config.h) + +AC_CANONICAL_HOST + +AC_PROG_CC +AC_PROG_CC_STDC +AC_PROG_INSTALL +AC_PROG_LIBTOOL +AC_ISC_POSIX +AC_HEADER_STDC +AM_PROG_CC_C_O + +PKG_CHECK_MODULES(GLIB, glib-2.0) +PKG_CHECK_MODULES(SOUP, libsoup-2.4) + +AC_SUBST(GLIB_CFLAGS) +AC_SUBST(GLIB_LIBS) + +AC_SUBST(SOUP_CFLAGS) +AC_SUBST(SOUP_LIBS) + +GTK_DOC_CHECK(1.4) + +localedir=${datadir}/locale +AC_SUBST(localedir) + +AC_OUTPUT([ + Makefile + rest/Makefile + examples/Makefile + rest.pc +]) + diff --git a/examples/Makefile.am b/examples/Makefile.am new file mode 100644 index 0000000..3ac2af5 --- /dev/null +++ b/examples/Makefile.am @@ -0,0 +1,5 @@ +noinst_PROGRAMS = test-raw + +test_raw_SOURCES = test-raw.c +test_raw_LDADD = $(GLIB_LIBS) $(SOUP_LIBS) ../rest/librest.la +test_raw_CFLAGS = $(GLIB_CFLAGS) $(SOUP_CFLAGS) -I.. diff --git a/examples/test-raw.c b/examples/test-raw.c new file mode 100644 index 0000000..eddba43 --- /dev/null +++ b/examples/test-raw.c @@ -0,0 +1,38 @@ +#include <rest/rest-proxy.h> + +static void +proxy_call_raw_async_cb (RestProxy *proxy, + guint status_code, + const gchar *response_message, + GHashTable *headers, + const gchar *payload, + gssize len, + GObject *weak_object, + gpointer userdata) +{ + write (1, payload, len); +} + +gint +main (gint argc, gchar **argv) +{ + RestProxy *proxy; + GMainLoop *loop; + + g_type_init (); + g_thread_init (NULL); + + loop = g_main_loop_new (NULL, FALSE); + + proxy = rest_proxy_new ("http://www.o-hand.com/", FALSE); + rest_proxy_call_raw_async (proxy, + NULL, + "GET", + proxy_call_raw_async_cb, + NULL, + NULL, + NULL, + NULL); + + g_main_loop_run (loop); +} diff --git a/rest.pc.in b/rest.pc.in new file mode 100644 index 0000000..8f94ab1 --- /dev/null +++ b/rest.pc.in @@ -0,0 +1,12 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: rest +Description: RESTful web api query library +Version: @VERSION@ +Libs: -L${libdir} -lrest +Cflags: -I${includedir} +Requires: glib-2.0 libsoup-2.4 + diff --git a/rest/Makefile.am b/rest/Makefile.am new file mode 100644 index 0000000..f46207c --- /dev/null +++ b/rest/Makefile.am @@ -0,0 +1,7 @@ +lib_LTLIBRARIES = librest.la + +librest_la_CFLAGS = $(GLIB_CFLAGS) $(SOUP_CFLAGS) +librest_la_LIBADD = $(GLIB_LIBS) $(SOUP_LIBS) +librest_la_SOURCES = rest-proxy.c +librest_la_HEADERS = rest-proxy.h +librest_ladir = $(includedir)/rest/rest diff --git a/rest/rest-proxy.c b/rest/rest-proxy.c new file mode 100644 index 0000000..64b0239 --- /dev/null +++ b/rest/rest-proxy.c @@ -0,0 +1,302 @@ +#include <libsoup/soup.h> + +#include "rest-proxy.h" + +G_DEFINE_TYPE (RestProxy, rest_proxy, G_TYPE_OBJECT) + +#define GET_PRIVATE(o) \ + (G_TYPE_INSTANCE_GET_PRIVATE ((o), REST_TYPE_PROXY, RestProxyPrivate)) + +typedef struct _RestProxyPrivate RestProxyPrivate; + +struct _RestProxyPrivate { + gchar *url_format; + gchar *url; + gboolean binding_required; + SoupSession *session; +}; + +enum +{ + PROP0 = 0, + PROP_URL_FORMAT, + PROP_BINDING_REQUIRED +}; + +static void +rest_proxy_get_property (GObject *object, guint property_id, + GValue *value, GParamSpec *pspec) +{ + RestProxyPrivate *priv = GET_PRIVATE (object); + + switch (property_id) { + case PROP_URL_FORMAT: + g_value_set_string (value, priv->url_format); + break; + case PROP_BINDING_REQUIRED: + g_value_set_boolean (value, priv->binding_required); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + } +} + +static void +rest_proxy_set_property (GObject *object, guint property_id, + const GValue *value, GParamSpec *pspec) +{ + RestProxyPrivate *priv = GET_PRIVATE (object); + + switch (property_id) { + case PROP_URL_FORMAT: + g_free (priv->url_format); + priv->url_format = g_value_dup_string (value); + + /* Clear the cached url */ + g_free (priv->url); + priv->url = NULL; + break; + case PROP_BINDING_REQUIRED: + priv->binding_required = g_value_get_boolean (value); + + /* Clear cached url */ + g_free (priv->url); + priv->url; + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + } +} + +static void +rest_proxy_dispose (GObject *object) +{ + if (G_OBJECT_CLASS (rest_proxy_parent_class)->dispose) + G_OBJECT_CLASS (rest_proxy_parent_class)->dispose (object); +} + +static void +rest_proxy_finalize (GObject *object) +{ + if (G_OBJECT_CLASS (rest_proxy_parent_class)->finalize) + G_OBJECT_CLASS (rest_proxy_parent_class)->finalize (object); +} + +static void +rest_proxy_class_init (RestProxyClass *klass) +{ + GParamSpec *pspec; + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + g_type_class_add_private (klass, sizeof (RestProxyPrivate)); + + object_class->get_property = rest_proxy_get_property; + object_class->set_property = rest_proxy_set_property; + object_class->dispose = rest_proxy_dispose; + object_class->finalize = rest_proxy_finalize; + + pspec = g_param_spec_string ("url-format", + "url-format", + "Format string for the RESTful url for this proxy", + NULL, + G_PARAM_READWRITE); + g_object_class_install_property (object_class, + PROP_URL_FORMAT, + pspec); + + pspec = g_param_spec_boolean ("binding-required", + "binding-required", + "Whether the URL format string requires binding", + FALSE, + G_PARAM_READWRITE); + g_object_class_install_property (object_class, + PROP_BINDING_REQUIRED, + pspec); +} + +static void +rest_proxy_init (RestProxy *self) +{ + RestProxyPrivate *priv = GET_PRIVATE (self); + + /* TODO: Use proxy settings, etc etc... */ + priv->session = soup_session_async_new (); +} + +RestProxy * +rest_proxy_new (const gchar *url_format, + gboolean binding_required) +{ + return g_object_new (REST_TYPE_PROXY, + "url-format", url_format, + "binding-required", binding_required, + NULL); +} + +gboolean +rest_proxy_bind (RestProxy *proxy, ...) +{ + RestProxyPrivate *priv = GET_PRIVATE (proxy); + va_list params; + + g_return_val_if_fail (proxy != NULL, FALSE); + g_return_val_if_fail (priv->url_format != NULL, FALSE); + g_return_val_if_fail (priv->binding_required != TRUE, FALSE); + + g_free (priv->url); + priv->url = g_strdup_vprintf (priv->url_format, params); +} + +typedef struct { + RestProxy *proxy; + RestProxyCallRawCallback callback; + GObject *weak_object; + gpointer userdata; + SoupMessage *message; +} RestProxyCallRawAsyncClosure; + +static void _call_raw_async_weak_notify_cb (gpointer *data, + GObject *dead_object); +static void _call_raw_async_finished_cb (SoupMessage *message, + gpointer userdata); + +static GHashTable * +_populate_headers_hash_table (const gchar *name, + const gchar *value, + gpointer userdata) +{ + GHashTable *headers = (GHashTable *)userdata; + + g_hash_table_insert (headers, g_strdup (name), g_strdup (value)); +} + +static void +_call_raw_async_finished_cb (SoupMessage *message, + gpointer userdata) +{ + RestProxyCallRawAsyncClosure *closure; + GHashTable *headers; + + closure = (RestProxyCallRawAsyncClosure *)userdata; + + headers = g_hash_table_new_full (g_str_hash, + g_str_equal, + g_free, + g_free); + + soup_message_headers_foreach (message->response_headers, + (SoupMessageHeadersForeachFunc)_populate_headers_hash_table, + headers); + + closure->callback (closure->proxy, + message->status_code, + message->reason_phrase, + headers, + message->response_body->data, + message->response_body->length, + closure->weak_object, + closure->userdata); + + if (closure->weak_object) + { + g_object_weak_unref (closure->weak_object, + (GWeakNotify)_call_raw_async_weak_notify_cb, + closure); + } + + g_object_unref (closure->proxy); + g_free (closure); +} + +static void +_call_raw_async_weak_notify_cb (gpointer *data, + GObject *dead_object) +{ + RestProxyCallRawAsyncClosure *closure; + + closure = (RestProxyCallRawAsyncClosure *)data; + + g_signal_handlers_disconnect_by_func (closure->message, + _call_raw_async_finished_cb, + closure); + g_object_unref (closure->proxy); + g_free (closure); +} + +gboolean +rest_proxy_call_raw_async (RestProxy *proxy, + const gchar *function, + const gchar *method, + RestProxyCallRawCallback callback, + GObject *weak_object, + gpointer userdata, + GError *error, + const gchar *first_field_name, + ...) +{ + RestProxyPrivate *priv = GET_PRIVATE (proxy); + va_list params; + SoupMessage *message = NULL; + gchar *url = NULL; + RestProxyCallRawAsyncClosure *closure; + + if (priv->binding_required && !priv->url) + { + g_critical (G_STRLOC ": URL requires binding and is unbound"); + /* FIXME: Return a GError */ + return FALSE; + } + + if (!priv->binding_required) + { + priv->url = g_strdup (priv->url_format); + } + + if (function) + { + if (g_str_has_suffix (priv->url, "/")) + { + url = g_strdup_printf ("%s%s", priv->url, function); + } else { + url = g_strdup_printf ("%s/%s", priv->url, function); + } + } else { + url = g_strdup (priv->url); + } + + if (first_field_name) + { + message = soup_form_request_new (method, + url, + first_field_name, + params); + } else { + message = soup_message_new (method, url); + } + + closure = g_new0 (RestProxyCallRawAsyncClosure, 1); + closure->proxy = g_object_ref (proxy); + closure->callback = callback; + closure->weak_object = weak_object; + closure->message = message; + + if (closure->weak_object) + { + g_object_weak_ref (closure->weak_object, + (GWeakNotify)_call_raw_async_weak_notify_cb, + closure); + } + + g_signal_connect (message, + "finished", + (GCallback)_call_raw_async_finished_cb, + closure); + + /* TODO: Should we do something in this callback ? */ + soup_session_queue_message (priv->session, + message, + NULL, + NULL); + + return TRUE; +} diff --git a/rest/rest-proxy.h b/rest/rest-proxy.h new file mode 100644 index 0000000..10122ad --- /dev/null +++ b/rest/rest-proxy.h @@ -0,0 +1,60 @@ +#ifndef _REST_PROXY +#define _REST_PROXY + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define REST_TYPE_PROXY rest_proxy_get_type() + +#define REST_PROXY(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST ((obj), REST_TYPE_PROXY, RestProxy)) + +#define REST_PROXY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST ((klass), REST_TYPE_PROXY, RestProxyClass)) + +#define REST_IS_PROXY(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE ((obj), REST_TYPE_PROXY)) + +#define REST_IS_PROXY_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE ((klass), REST_TYPE_PROXY)) + +#define REST_PROXY_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS ((obj), REST_TYPE_PROXY, RestProxyClass)) + +typedef struct { + GObject parent; +} RestProxy; + +typedef struct { + GObjectClass parent_class; +} RestProxyClass; + +typedef void (*RestProxyCallRawCallback)(RestProxy *proxy, + guint status_code, + const gchar *response_message, + GHashTable *headers, + const gchar *payload, + gssize len, + GObject *weak_object, + gpointer userdata); + +GType rest_proxy_get_type (void); + +RestProxy *rest_proxy_new (const gchar *url_format, + gboolean binding_required); + +gboolean rest_proxy_call_raw_async (RestProxy *proxy, + const gchar *function, + const gchar *method, + RestProxyCallRawCallback callback, + GObject *weak_object, + gpointer userdata, + GError *error, + const gchar *first_field_name, + ...); + +G_END_DECLS + +#endif /* _REST_PROXY */ + |