File: mrss_generic.c

package info (click to toggle)
feedreader 2.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,868 kB
  • sloc: ansic: 11,829; xml: 879; php: 170; sh: 32; makefile: 16; python: 14
file content (146 lines) | stat: -rw-r--r-- 3,687 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* mRss - Copyright (C) 2005-2007 bakunin - Andrea Marchesini 
 *                                    <bakunin@autistici.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "mrss.h"
#include "mrss_internal.h"

char *
mrss_strerror (mrss_error_t err)
{
  switch (err)
    {
    case MRSS_OK:
      return "Success";

    case MRSS_ERR_PARSER:
      return "Parser error";

    case MRSS_ERR_DOWNLOAD:
      return "Download error";

    case MRSS_ERR_VERSION:
      return "Version error";

    case MRSS_ERR_DATA:
      return "No correct paramenter in the function";

    default:
      return strerror (errno);
    }
}

char *
mrss_curl_strerror (CURLcode err)
{
  return (char *) curl_easy_strerror (err);
}

mrss_error_t
mrss_element (mrss_generic_t element, mrss_element_t * ret)
{
  mrss_t *tmp;

  if (!element || !ret)
    return MRSS_ERR_DATA;

  tmp = (mrss_t *) element;
  *ret = tmp->element;
  return MRSS_OK;
}

static size_t
__mrss_get_last_modified_header (void *ptr, size_t size, size_t nmemb,
				 time_t * timing)
{
  char *header = (char *) ptr;

  if (!strncmp ("Last-Modified:", header, 14))
    *timing = curl_getdate (header + 14, NULL);

  return size * nmemb;
}

mrss_error_t
mrss_get_last_modified (char *urlstring, time_t * lastmodified)
{
  return mrss_get_last_modified_with_options (urlstring, lastmodified, NULL);
}

mrss_error_t
mrss_get_last_modified_with_options (char *urlstring, time_t * lastmodified,
				     mrss_options_t * options)
{
  CURL *curl;

  if (!urlstring || !lastmodified)
    return MRSS_ERR_DATA;

  *lastmodified = 0;

  curl_global_init (CURL_GLOBAL_DEFAULT);
  if (!(curl = curl_easy_init ()))
    return MRSS_ERR_POSIX;

  curl_easy_setopt (curl, CURLOPT_URL, urlstring);
  curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION,
		    __mrss_get_last_modified_header);
  curl_easy_setopt (curl, CURLOPT_HEADERDATA, lastmodified);
  curl_easy_setopt (curl, CURLOPT_NOBODY, 1);
  curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
  curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);

  if (options)
    {
      if (options->timeout > 0)
	curl_easy_setopt (curl, CURLOPT_TIMEOUT, options->timeout);
      else if (options->timeout < 0)
	curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10);

      if (options->certfile)
	curl_easy_setopt (curl, CURLOPT_SSLCERT, options->certfile);

      if (options->password)
	curl_easy_setopt (curl, CURLOPT_SSLCERTPASSWD, options->password);

      if (options->cacert)
	curl_easy_setopt (curl, CURLOPT_CAINFO, options->cacert);

      if (options->proxy)
	{
	  curl_easy_setopt (curl, CURLOPT_PROXY, options->proxy);

	  if (options->proxy_authentication)
	    curl_easy_setopt (curl, CURLOPT_PROXYUSERPWD,
			      options->proxy_authentication);
	}

      curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, options->verifypeer);
    }

  if (curl_easy_perform (curl))
    {
      curl_easy_cleanup (curl);
      return MRSS_ERR_POSIX;
    }

  curl_easy_cleanup (curl);

  return MRSS_OK;
}

/* EOF */