Blame src/lua/quvi/http/cookie.c

Packit 3ff1e7
/* libquvi
Packit 3ff1e7
 * Copyright (C) 2013  Toni Gundogdu <legatvs@gmail.com>
Packit 3ff1e7
 *
Packit 3ff1e7
 * This file is part of libquvi <http://quvi.sourceforge.net/>.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is free software: you can redistribute it and/or
Packit 3ff1e7
 * modify it under the terms of the GNU Affero General Public
Packit 3ff1e7
 * License as published by the Free Software Foundation, either
Packit 3ff1e7
 * version 3 of the License, or (at your option) any later version.
Packit 3ff1e7
 *
Packit 3ff1e7
 * This library is distributed in the hope that it will be useful,
Packit 3ff1e7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff1e7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 3ff1e7
 * GNU Affero General Public License for more details.
Packit 3ff1e7
 *
Packit 3ff1e7
 * You should have received a copy of the GNU Affero General
Packit 3ff1e7
 * Public License along with this library.  If not, see
Packit 3ff1e7
 * <http://www.gnu.org/licenses/>.
Packit 3ff1e7
 */
Packit 3ff1e7
Packit 3ff1e7
#include "config.h"
Packit 3ff1e7
Packit 3ff1e7
#include <string.h>
Packit 3ff1e7
#include <lauxlib.h>
Packit 3ff1e7
#include <glib.h>
Packit 3ff1e7
#include <curl/curl.h>
Packit 3ff1e7
Packit 3ff1e7
#include "quvi.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "_quvi_s.h"
Packit 3ff1e7
/* -- */
Packit 3ff1e7
#include "lua/quvi/opts.h"
Packit 3ff1e7
#include "lua/getfield.h"
Packit 3ff1e7
#include "lua/setfield.h"
Packit 3ff1e7
#include "lua/def.h"
Packit 3ff1e7
Packit 3ff1e7
static gint _ret(lua_State *l, const _quvi_t q)
Packit 3ff1e7
{
Packit 3ff1e7
  lua_newtable(l); /* Return a table of results. */
Packit 3ff1e7
  l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1);
Packit 3ff1e7
  l_setfield_n(l, QO_QUVI_CODE, q->status.rc);
Packit 3ff1e7
  return (1); /* no. of returned values (a table) */
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
struct _cookie_opts_s
Packit 3ff1e7
{
Packit 3ff1e7
  const gchar *s;
Packit 3ff1e7
  gint mode;
Packit 3ff1e7
};
Packit 3ff1e7
Packit 3ff1e7
typedef struct _cookie_opts_s *_cookie_opts_t;
Packit 3ff1e7
Packit 3ff1e7
typedef enum
Packit 3ff1e7
{
Packit 3ff1e7
  COOKIE_MODE_SESSION = 0x01,
Packit 3ff1e7
  COOKIE_MODE_FILE,
Packit 3ff1e7
  COOKIE_MODE_LIST,
Packit 3ff1e7
  COOKIE_MODE_JAR
Packit 3ff1e7
} CookieMode;
Packit 3ff1e7
Packit 3ff1e7
static gint _setopt(lua_State *l, const _quvi_t q, const CURLoption copt,
Packit 3ff1e7
                    const _cookie_opts_t co, const gboolean croak_if_error)
Packit 3ff1e7
{
Packit 3ff1e7
  CURLcode r;
Packit 3ff1e7
Packit 3ff1e7
  if (co->mode == COOKIE_MODE_SESSION)
Packit 3ff1e7
    r = curl_easy_setopt(q->handle.curl, copt, (glong) g_strtod(co->s, NULL));
Packit 3ff1e7
  else
Packit 3ff1e7
    r = curl_easy_setopt(q->handle.curl, copt, co->s);
Packit 3ff1e7
Packit 3ff1e7
  if (r != CURLE_OK)
Packit 3ff1e7
    {
Packit 3ff1e7
      g_string_printf(q->status.errmsg, "%s", curl_easy_strerror(r));
Packit 3ff1e7
      q->status.rc = QUVI_ERROR_CALLBACK;
Packit 3ff1e7
Packit 3ff1e7
      if (croak_if_error == TRUE)
Packit 3ff1e7
        luaL_error(l, "%s", q->status.errmsg->str);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (_ret(l, q));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
static void _chk_cookie_opts(lua_State *l, GSList *opts, _cookie_opts_t co)
Packit 3ff1e7
{
Packit 3ff1e7
  GSList *p;
Packit 3ff1e7
Packit 3ff1e7
  l_quvi_object_opts_chk_given(l, opts, "cookie");
Packit 3ff1e7
Packit 3ff1e7
  /* required */
Packit 3ff1e7
Packit 3ff1e7
  l_quvi_object_opts_chk_req_s(QUVI_OBJECT_OPTION_HTTP_COOKIE_MODE,
Packit 3ff1e7
                               HRE_COOKIE_MODE, co->mode, n);
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
gint l_quvi_http_cookie(lua_State *l)
Packit 3ff1e7
{
Packit 3ff1e7
  struct _cookie_opts_s co;
Packit 3ff1e7
  gboolean croak_if_error;
Packit 3ff1e7
  CURLoption copt;
Packit 3ff1e7
  GSList *opts;
Packit 3ff1e7
  _quvi_t q;
Packit 3ff1e7
Packit 3ff1e7
  /* quvi handle */
Packit 3ff1e7
Packit 3ff1e7
  q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
Packit 3ff1e7
  g_assert(q != NULL);
Packit 3ff1e7
Packit 3ff1e7
  if (q->opt.allow_cookies == QUVI_FALSE)
Packit 3ff1e7
    return (_ret(l, q)); /* Do nothing if cookies have been disabled. */
Packit 3ff1e7
Packit 3ff1e7
  /* arg1 */
Packit 3ff1e7
Packit 3ff1e7
  memset(&co, 0, sizeof(struct _cookie_opts_s));
Packit 3ff1e7
  co.s = luaL_checkstring(l, 1);
Packit 3ff1e7
  lua_pop(l, 1);
Packit 3ff1e7
Packit 3ff1e7
  /* options */
Packit 3ff1e7
Packit 3ff1e7
  opts = l_quvi_object_opts_new(l, 2);
Packit 3ff1e7
  croak_if_error = l_quvi_object_opts_croak_if_error(l, opts);
Packit 3ff1e7
Packit 3ff1e7
  _chk_cookie_opts(l, opts, &co);
Packit 3ff1e7
  l_quvi_object_opts_free(opts);
Packit 3ff1e7
Packit 3ff1e7
  /* mode */
Packit 3ff1e7
Packit 3ff1e7
  switch (co.mode)
Packit 3ff1e7
    {
Packit 3ff1e7
    case COOKIE_MODE_SESSION:
Packit 3ff1e7
      copt = CURLOPT_COOKIESESSION;
Packit 3ff1e7
      break;
Packit 3ff1e7
Packit 3ff1e7
    case COOKIE_MODE_FILE:
Packit 3ff1e7
      copt = CURLOPT_COOKIEFILE;
Packit 3ff1e7
      break;
Packit 3ff1e7
Packit 3ff1e7
    case COOKIE_MODE_LIST:
Packit 3ff1e7
      copt = CURLOPT_COOKIELIST;
Packit 3ff1e7
      break;
Packit 3ff1e7
Packit 3ff1e7
    case COOKIE_MODE_JAR:
Packit 3ff1e7
      copt = CURLOPT_COOKIEJAR;
Packit 3ff1e7
      break;
Packit 3ff1e7
Packit 3ff1e7
    default:
Packit 3ff1e7
      g_string_printf(q->status.errmsg,
Packit 3ff1e7
                      "[%s] invalid cookie function `0x%02x'",
Packit 3ff1e7
                      __func__, co.mode);
Packit 3ff1e7
Packit 3ff1e7
      q->status.rc = QUVI_ERROR_CALLBACK;
Packit 3ff1e7
      copt = CURLOPT_COOKIESESSION;
Packit 3ff1e7
Packit 3ff1e7
      g_warning("%s", q->status.errmsg->str);
Packit 3ff1e7
    }
Packit 3ff1e7
  return (_setopt(l, q, copt, &co, croak_if_error));
Packit 3ff1e7
}
Packit 3ff1e7
Packit 3ff1e7
/* vim: set ts=2 sw=2 tw=72 expandtab: */