Skip to content
cookie.c 31.2 KiB
Newer Older
Dan Fandrich's avatar
Dan Fandrich committed
int Curl_cookie_output(struct CookieInfo *c, const char *dumphere)
Daniel Stenberg's avatar
Daniel Stenberg committed
  FILE *out;
  bool use_stdout=FALSE;

  if((NULL == c) || (0 == c->numcookies))
Daniel Stenberg's avatar
Daniel Stenberg committed
    /* If there are no known cookies, we don't write or even create any
       destination file */
    return 0;

  if(strequal("-", dumphere)) {
    /* use stdout */
    out = stdout;
    use_stdout=TRUE;
  }
  else {
    out = fopen(dumphere, "w");
    if(!out)
      return 1; /* failure */
  }

Daniel Stenberg's avatar
Daniel Stenberg committed
    fputs("# Netscape HTTP Cookie File\n"
          "# http://curl.haxx.se/rfc/cookie_spec.html\n"
          "# This file was generated by libcurl! Edit at your own risk.\n\n",
Daniel Stenberg's avatar
Daniel Stenberg committed
          out);
Daniel Stenberg's avatar
Daniel Stenberg committed
      if(format_ptr == NULL) {
        fprintf(out, "#\n# Fatal libcurl error\n");
        return 1;
      }
      fprintf(out, "%s\n", format_ptr);
      free(format_ptr);
Daniel Stenberg's avatar
Daniel Stenberg committed

  if(!use_stdout)
    fclose(out);

  return 0;
struct curl_slist *Curl_cookie_list(struct SessionHandle *data)
{
  struct curl_slist *list = NULL;
  struct curl_slist *beg;
  struct Cookie *c;
  char *line;
Daniel Stenberg's avatar
Daniel Stenberg committed
  if((data->cookies == NULL) ||
      (data->cookies->numcookies == 0))
    return NULL;
  c = data->cookies->cookies;
  beg = list;
Daniel Stenberg's avatar
Daniel Stenberg committed
  while(c) {
    /* fill the list with _all_ the cookies we know */
    line = get_netscape_format(c);
Daniel Stenberg's avatar
Daniel Stenberg committed
    if(line == NULL) {
      curl_slist_free_all(beg);
      return NULL;
    }
    list = curl_slist_append(list, line);
    free(line);
Daniel Stenberg's avatar
Daniel Stenberg committed
    if(list == NULL) {
      curl_slist_free_all(beg);
      return NULL;
    }
Daniel Stenberg's avatar
Daniel Stenberg committed
    else if(beg == NULL) {
      beg = list;
    }
    c = c->next;
  }
  return list;
#endif /* CURL_DISABLE_HTTP || CURL_DISABLE_COOKIES */