4 * Copyright 2003-2004 Oliver Feiler <kiza@kcore.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/types.h>
36 #include "conversions.h"
39 #include "io-internal.h"
43 void GetHTTPErrorString (char * errorstring, int size, int httpstatus) {
46 snprintf(errorstring, size, "Bad request");
49 snprintf(errorstring, size, "Payment required");
52 snprintf(errorstring, size, "Access denied");
55 snprintf(errorstring, size, "Internal server error");
58 snprintf(errorstring, size, "Not implemented");
62 snprintf(errorstring, size, "Service unavailable");
65 sprintf(errorstring, "HTTP %d!", httpstatus);
69 void PrintUpdateError (int suppressoutput, struct feed * cur_ptr) {
74 err = cur_ptr->netio_error;
76 if (!suppressoutput) {
80 case NET_ERR_URL_INVALID:
81 fprintf(stderr, "%s: Invalid URL!\n", cur_ptr->title);
83 case NET_ERR_SOCK_ERR:
84 fprintf(stderr, "%s: Couldn't create network socket!\n", cur_ptr->title);
86 case NET_ERR_HOST_NOT_FOUND:
87 fprintf(stderr, "%s: Can't resolve host!\n", cur_ptr->title);
89 case NET_ERR_CONN_REFUSED:
90 fprintf(stderr, "%s: Connection refused!\n", cur_ptr->title);
92 case NET_ERR_CONN_FAILED:
93 fprintf(stderr, "%s: Couldn't connect to server: %s\n",
95 (strerror(cur_ptr->connectresult) ? strerror(cur_ptr->connectresult) : "(null)"));
98 fprintf(stderr, "%s: Connection timed out.\n", cur_ptr->title);
100 case NET_ERR_UNKNOWN:
102 case NET_ERR_REDIRECT_COUNT_ERR:
103 fprintf(stderr, "%s: Too many HTTP redirects encountered! Giving up.\n", cur_ptr->title);
105 case NET_ERR_REDIRECT_ERR:
106 fprintf(stderr, "%s: Server sent an invalid redirect!\n", cur_ptr->title);
108 case NET_ERR_HTTP_410:
109 case NET_ERR_HTTP_404:
110 fprintf(stderr, "%s: This feed no longer exists. Please unsubscribe!\n", cur_ptr->title);
112 case NET_ERR_HTTP_NON_200:
113 GetHTTPErrorString(httperrstr, sizeof(httperrstr), cur_ptr->lasthttpstatus);
114 fprintf(stderr, "%s: Could not download feed: %s\n", cur_ptr->title, httperrstr);
116 case NET_ERR_HTTP_PROTO_ERR:
117 fprintf(stderr, "%s: Error in server reply.\n", cur_ptr->title);
119 case NET_ERR_AUTH_FAILED:
120 fprintf(stderr, "%s: Authentication failed!\n", cur_ptr->title);
122 case NET_ERR_AUTH_NO_AUTHINFO:
123 fprintf(stderr, "%s: URL does not contain authentication information!\n", cur_ptr->title);
125 case NET_ERR_AUTH_GEN_AUTH_ERR:
126 fprintf(stderr, "%s: Could not generate authentication information!\n", cur_ptr->title);
128 case NET_ERR_AUTH_UNSUPPORTED:
129 fprintf(stderr, "%s: Unsupported authentication method requested by server!\n", cur_ptr->title);
131 case NET_ERR_GZIP_ERR:
132 fprintf(stderr, "%s: Error decompressing server reply!\n", cur_ptr->title);
137 /* Must be inside if(!suppressoutput) statement! */
142 /* Update given feed from server.
143 * Reload XML document and replace in memory cur_ptr->feed with it.
145 int UpdateFeed (struct feed * cur_ptr) {
149 if (cur_ptr == NULL) {
153 /* Need to work on a copy of ->feedurl, because DownloadFeed() changes the pointer. */
154 tmpname = strdup (cur_ptr->feedurl);
155 freeme = tmpname; /* Need to make a copy, otherwise we cannot free all RAM. */
156 free (cur_ptr->feed);
158 cur_ptr->feed = DownloadFeed (tmpname, cur_ptr, 0);
161 /* Set title and link structure to something.
162 * To the feedurl in this case so the program show something
163 * as placeholder instead of crash. */
164 if (cur_ptr->title == NULL)
165 cur_ptr->title = strdup (cur_ptr->feedurl);
166 if (cur_ptr->link == NULL)
167 cur_ptr->link = strdup (cur_ptr->feedurl);
169 /* If the download function returns a NULL pointer return from here. */
170 if (cur_ptr->feed == NULL) {
171 if (cur_ptr->problem == 1)
172 PrintUpdateError (0, cur_ptr);
176 /* If there is no feed, return. */
177 if (cur_ptr->feed == NULL)
180 if ((DeXML (cur_ptr)) != 0) {
181 fprintf(stderr, "Invalid XML! Cannot parse this feed!\n");
183 /* Activate feed problem flag. */
184 cur_ptr->problem = 1;
188 /* We don't need these anymore. Free the raw XML to save some memory. */
189 free (cur_ptr->feed);
190 cur_ptr->feed = NULL;