Пример #1
0
gint l_quvi_http_header(lua_State *l)
{
  gboolean croak_if_error;
  const gchar *s;
  GSList *opts;
  CURLcode cc;
  _quvi_t q;

  /* quvi handle */

  q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
  g_assert(q != NULL);

  /* arg1 */

  s = luaL_checkstring(l, 1);
  lua_pop(l, 1);

  /* options */

  opts = l_quvi_object_opts_new(l, 2);
  croak_if_error = l_quvi_object_opts_croak_if_error(l, opts);
  l_quvi_object_opts_free(opts);

  /* apply */

  if (strlen(s) >0)
    {
      CURL *c = q->handle.curl;
      q->http.headers = curl_slist_append(q->http.headers, s);
      cc = curl_easy_setopt(c, CURLOPT_HTTPHEADER, q->http.headers);
    }
  else
    cc = c_reset_headers(q);

  if (cc != CURLE_OK)
    {
      g_string_printf(q->status.errmsg, "%s", curl_easy_strerror(cc));
      q->status.rc = QUVI_ERROR_CALLBACK;

      if (croak_if_error == TRUE)
        luaL_error(l, "%s", q->status.errmsg->str);
    }

  /* Return a table of results. */

  lua_newtable(l);
  l_setfield_s(l, QO_ERROR_MESSAGE, q->status.errmsg->str, -1);
  l_setfield_n(l, QO_QUVI_CODE, q->status.rc);

  return (1); /* no. of returned values (a table) */
}
Пример #2
0
gint l_quvi_http_cookie(lua_State *l)
{
  struct _cookie_opts_s co;
  gboolean croak_if_error;
  CURLoption copt;
  GSList *opts;
  _quvi_t q;

  /* quvi handle */

  q = (_quvi_t) l_get_reg_userdata(l, USERDATA_QUVI_T);
  g_assert(q != NULL);

  if (q->opt.allow_cookies == QUVI_FALSE)
    return (_ret(l, q)); /* Do nothing if cookies have been disabled. */

  /* arg1 */

  memset(&co, 0, sizeof(struct _cookie_opts_s));
  co.s = luaL_checkstring(l, 1);
  lua_pop(l, 1);

  /* options */

  opts = l_quvi_object_opts_new(l, 2);
  croak_if_error = l_quvi_object_opts_croak_if_error(l, opts);

  _chk_cookie_opts(l, opts, &co);
  l_quvi_object_opts_free(opts);

  /* mode */

  switch (co.mode)
    {
    case COOKIE_MODE_SESSION:
      copt = CURLOPT_COOKIESESSION;
      break;

    case COOKIE_MODE_FILE:
      copt = CURLOPT_COOKIEFILE;
      break;

    case COOKIE_MODE_LIST:
      copt = CURLOPT_COOKIELIST;
      break;

    case COOKIE_MODE_JAR:
      copt = CURLOPT_COOKIEJAR;
      break;

    default:
      g_string_printf(q->status.errmsg,
                      "[%s] invalid cookie function `0x%02x'",
                      __func__, co.mode);

      q->status.rc = QUVI_ERROR_CALLBACK;
      copt = CURLOPT_COOKIESESSION;

      g_warning("%s", q->status.errmsg->str);
    }
  return (_setopt(l, q, copt, &co, croak_if_error));
}