1 /* mRss - Copyright (C) 2005-2007 bakunin - Andrea Marchesini
2  *                                    <bakunin@autistici.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #else
22 # error Use configure; make; make install
23 #endif
24 
25 #include "mrss.h"
26 #include "mrss_internal.h"
27 
28 char *
mrss_strerror(mrss_error_t err)29 mrss_strerror (mrss_error_t err)
30 {
31   switch (err)
32     {
33     case MRSS_OK:
34       return "Success";
35 
36     case MRSS_ERR_PARSER:
37       return "Parser error";
38 
39     case MRSS_ERR_DOWNLOAD:
40       return "Download error";
41 
42     case MRSS_ERR_VERSION:
43       return "Version error";
44 
45     case MRSS_ERR_DATA:
46       return "No correct paramenter in the function";
47 
48     default:
49       return strerror (errno);
50     }
51 }
52 
53 char *
mrss_curl_strerror(CURLcode err)54 mrss_curl_strerror (CURLcode err)
55 {
56   return (char *) curl_easy_strerror (err);
57 }
58 
59 mrss_error_t
mrss_element(mrss_generic_t element,mrss_element_t * ret)60 mrss_element (mrss_generic_t element, mrss_element_t * ret)
61 {
62   mrss_t *tmp;
63 
64   if (!element || !ret)
65     return MRSS_ERR_DATA;
66 
67   tmp = (mrss_t *) element;
68   *ret = tmp->element;
69   return MRSS_OK;
70 }
71 
72 static size_t
__mrss_get_last_modified_header(void * ptr,size_t size,size_t nmemb,time_t * timing)73 __mrss_get_last_modified_header (void *ptr, size_t size, size_t nmemb,
74 				 time_t * timing)
75 {
76   char *header = (char *) ptr;
77 
78   if (!strncmp ("Last-Modified:", header, 14))
79     *timing = curl_getdate (header + 14, NULL);
80 
81   return size * nmemb;
82 }
83 
84 mrss_error_t
mrss_get_last_modified(char * urlstring,time_t * lastmodified)85 mrss_get_last_modified (char *urlstring, time_t * lastmodified)
86 {
87   return mrss_get_last_modified_with_options (urlstring, lastmodified, NULL);
88 }
89 
90 mrss_error_t
mrss_get_last_modified_with_options(char * urlstring,time_t * lastmodified,mrss_options_t * options)91 mrss_get_last_modified_with_options (char *urlstring, time_t * lastmodified,
92 				     mrss_options_t * options)
93 {
94   CURL *curl;
95 
96   if (!urlstring || !lastmodified)
97     return MRSS_ERR_DATA;
98 
99   *lastmodified = 0;
100 
101   curl_global_init (CURL_GLOBAL_DEFAULT);
102   if (!(curl = curl_easy_init ()))
103     return MRSS_ERR_POSIX;
104 
105   curl_easy_setopt (curl, CURLOPT_URL, urlstring);
106   curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION,
107 		    __mrss_get_last_modified_header);
108   curl_easy_setopt (curl, CURLOPT_HEADERDATA, lastmodified);
109   curl_easy_setopt (curl, CURLOPT_NOBODY, 1);
110   curl_easy_setopt (curl, CURLOPT_FOLLOWLOCATION, 1);
111   curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1);
112 
113   if (options)
114     {
115       if (options->timeout > 0)
116 	curl_easy_setopt (curl, CURLOPT_TIMEOUT, options->timeout);
117       else if (options->timeout < 0)
118 	curl_easy_setopt (curl, CURLOPT_TIMEOUT, 10);
119 
120       if (options->certfile)
121 	curl_easy_setopt (curl, CURLOPT_SSLCERT, options->certfile);
122 
123       if (options->password)
124 	curl_easy_setopt (curl, CURLOPT_SSLCERTPASSWD, options->password);
125 
126       if (options->cacert)
127 	curl_easy_setopt (curl, CURLOPT_CAINFO, options->cacert);
128 
129       if (options->proxy)
130 	{
131 	  curl_easy_setopt (curl, CURLOPT_PROXY, options->proxy);
132 
133 	  if (options->proxy_authentication)
134 	    curl_easy_setopt (curl, CURLOPT_PROXYUSERPWD,
135 			      options->proxy_authentication);
136 	}
137 
138       curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, options->verifypeer);
139     }
140 
141   if (curl_easy_perform (curl))
142     {
143       curl_easy_cleanup (curl);
144       return MRSS_ERR_POSIX;
145     }
146 
147   curl_easy_cleanup (curl);
148 
149   return MRSS_OK;
150 }
151 
152 /* EOF */
153