source: protocols/yahoo/yahoo_httplib.c @ be1efa3

Last change on this file since be1efa3 was 6b13103, checked in by dequis <dx@…>, at 2015-01-16T19:50:23Z

Replace isdigit/isalpha/.../tolower/toupper with glib variants

This fixes warnings about passing signed chars to them (apparently they
are implemented as macros that do array lookups without checks in some
platforms, yay)

Specifically:

functions=isalnum|isalpha|isdigit|isspace|isxdigit|tolower|toupper
sed -ir "s/$functions/g_ascii_&/g" /*.c

  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * libyahoo2: yahoo_httplib.c
3 *
4 * Copyright (C) 2002-2004, Philip S Tellis <philip.tellis AT gmx.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19 *
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24
25#if STDC_HEADERS
26# include <string.h>
27#else
28# if !HAVE_STRCHR
29#  define strchr index
30#  define strrchr rindex
31# endif
32char *strchr(), *strrchr();
33# if !HAVE_MEMCPY
34#  define memcpy(d, s, n) bcopy ((s), (d), (n))
35#  define memmove(d, s, n) bcopy ((s), (d), (n))
36# endif
37#endif
38
39#include <errno.h>
40#include <unistd.h>
41#include <ctype.h>
42#include "yahoo2.h"
43#include "yahoo2_callbacks.h"
44#include "yahoo_httplib.h"
45#include "yahoo_util.h"
46
47#include "yahoo_debug.h"
48#ifdef __MINGW32__
49# include <winsock2.h>
50# define snprintf _snprintf
51#endif
52
53#ifdef USE_STRUCT_CALLBACKS
54extern struct yahoo_callbacks *yc;
55#define YAHOO_CALLBACK(x)       yc->x
56#else
57#define YAHOO_CALLBACK(x)       x
58#endif
59
60extern enum yahoo_log_level log_level;
61
62#if 0
63int yahoo_tcp_readline(char *ptr, int maxlen, void *fd)
64{
65        int n, rc;
66        char c;
67
68        for (n = 1; n < maxlen; n++) {
69
70                do {
71                        rc = YAHOO_CALLBACK(ext_yahoo_read) (fd, &c, 1);
72                } while (rc == -1 && (errno == EINTR || errno == EAGAIN));      /* this is bad - it should be done asynchronously */
73
74                if (rc == 1) {
75                        if (c == '\r')  /* get rid of \r */
76                                continue;
77                        *ptr = c;
78                        if (c == '\n')
79                                break;
80                        ptr++;
81                } else if (rc == 0) {
82                        if (n == 1)
83                                return (0);     /* EOF, no data */
84                        else
85                                break;  /* EOF, w/ data */
86                } else {
87                        return -1;
88                }
89        }
90
91        *ptr = 0;
92        return (n);
93}
94#endif
95
96static int url_to_host_port_path(const char *url,
97        char *host, int *port, char *path, int *ssl)
98{
99        char *urlcopy = NULL;
100        char *slash = NULL;
101        char *colon = NULL;
102
103        /*
104         * http://hostname
105         * http://hostname/
106         * http://hostname/path
107         * http://hostname/path:foo
108         * http://hostname:port
109         * http://hostname:port/
110         * http://hostname:port/path
111         * http://hostname:port/path:foo
112         * and https:// variants of the above
113         */
114
115        if (strstr(url, "http://") == url) {
116                urlcopy = strdup(url + 7);
117        } else if (strstr(url, "https://") == url) {
118                urlcopy = strdup(url + 8);
119                *ssl = 1;
120        } else {
121                WARNING(("Weird url - unknown protocol: %s", url));
122                return 0;
123        }
124
125        slash = strchr(urlcopy, '/');
126        colon = strchr(urlcopy, ':');
127
128        if (!colon || (slash && slash < colon)) {
129                if (*ssl)
130                        *port = 443;
131                else
132                        *port = 80;
133        } else {
134                *colon = 0;
135                *port = atoi(colon + 1);
136        }
137
138        if (!slash) {
139                strcpy(path, "/");
140        } else {
141                strcpy(path, slash);
142                *slash = 0;
143        }
144
145        strcpy(host, urlcopy);
146
147        FREE(urlcopy);
148
149        return 1;
150}
151
152static int isurlchar(unsigned char c)
153{
154        return (g_ascii_isalnum(c));
155}
156
157char *yahoo_urlencode(const char *instr)
158{
159        int ipos = 0, bpos = 0;
160        char *str = NULL;
161        int len = strlen(instr);
162
163        if (!(str = y_new(char, 3 *len + 1)))
164                 return "";
165
166        while (instr[ipos]) {
167                while (isurlchar(instr[ipos]))
168                        str[bpos++] = instr[ipos++];
169                if (!instr[ipos])
170                        break;
171
172                snprintf(&str[bpos], 4, "%%%02x", instr[ipos] & 0xff);
173                bpos += 3;
174                ipos++;
175        }
176        str[bpos] = '\0';
177
178        /* free extra alloc'ed mem. */
179        len = strlen(str);
180        str = y_renew(char, str, len + 1);
181
182        return (str);
183}
184
185#if 0
186char *yahoo_urldecode(const char *instr)
187{
188        int ipos = 0, bpos = 0;
189        char *str = NULL;
190        char entity[3] = { 0, 0, 0 };
191        unsigned dec;
192        int len = strlen(instr);
193
194        if (!(str = y_new(char, len + 1)))
195                 return "";
196
197        while (instr[ipos]) {
198                while (instr[ipos] && instr[ipos] != '%')
199                        if (instr[ipos] == '+') {
200                                str[bpos++] = ' ';
201                                ipos++;
202                        } else
203                                str[bpos++] = instr[ipos++];
204                if (!instr[ipos])
205                        break;
206
207                if (instr[ipos + 1] && instr[ipos + 2]) {
208                        ipos++;
209                        entity[0] = instr[ipos++];
210                        entity[1] = instr[ipos++];
211                        sscanf(entity, "%2x", &dec);
212                        str[bpos++] = (char)dec;
213                } else {
214                        str[bpos++] = instr[ipos++];
215                }
216        }
217        str[bpos] = '\0';
218
219        /* free extra alloc'ed mem. */
220        len = strlen(str);
221        str = y_renew(char, str, len + 1);
222
223        return (str);
224}
225
226char *yahoo_xmldecode(const char *instr)
227{
228        int ipos = 0, bpos = 0, epos = 0;
229        char *str = NULL;
230        char entity[4] = { 0, 0, 0, 0 };
231        char *entitymap[5][2] = {
232                {"amp;", "&"},
233                {"quot;", "\""},
234                {"lt;", "<"},
235                {"gt;", "<"},
236                {"nbsp;", " "}
237        };
238        unsigned dec;
239        int len = strlen(instr);
240
241        if (!(str = y_new(char, len + 1)))
242                 return "";
243
244        while (instr[ipos]) {
245                while (instr[ipos] && instr[ipos] != '&')
246                        if (instr[ipos] == '+') {
247                                str[bpos++] = ' ';
248                                ipos++;
249                        } else
250                                str[bpos++] = instr[ipos++];
251                if (!instr[ipos] || !instr[ipos + 1])
252                        break;
253                ipos++;
254
255                if (instr[ipos] == '#') {
256                        ipos++;
257                        epos = 0;
258                        while (instr[ipos] != ';')
259                                entity[epos++] = instr[ipos++];
260                        sscanf(entity, "%u", &dec);
261                        str[bpos++] = (char)dec;
262                        ipos++;
263                } else {
264                        int i;
265                        for (i = 0; i < 5; i++)
266                                if (!strncmp(instr + ipos, entitymap[i][0],
267                                                strlen(entitymap[i][0]))) {
268                                        str[bpos++] = entitymap[i][1][0];
269                                        ipos += strlen(entitymap[i][0]);
270                                        break;
271                                }
272                }
273        }
274        str[bpos] = '\0';
275
276        /* free extra alloc'ed mem. */
277        len = strlen(str);
278        str = y_renew(char, str, len + 1);
279
280        return (str);
281}
282#endif
283
284typedef void (*http_connected) (int id, void *fd, int error);
285
286struct callback_data {
287        int id;
288        yahoo_get_fd_callback callback;
289        char *request;
290        void *user_data;
291};
292
293static void connect_complete(void *fd, int error, void *data)
294{
295        struct callback_data *ccd = data;
296        if (error == 0)
297                YAHOO_CALLBACK(ext_yahoo_write) (fd, ccd->request,
298                        strlen(ccd->request));
299        free(ccd->request);
300        ccd->callback(ccd->id, fd, error, ccd->user_data);
301        FREE(ccd);
302}
303
304static void yahoo_send_http_request(int id, char *host, int port, char *request,
305        yahoo_get_fd_callback callback, void *data, int use_ssl)
306{
307        struct callback_data *ccd = y_new0(struct callback_data, 1);
308        ccd->callback = callback;
309        ccd->id = id;
310        ccd->request = strdup(request);
311        ccd->user_data = data;
312
313        YAHOO_CALLBACK(ext_yahoo_connect_async) (id, host, port,
314                connect_complete, ccd, use_ssl);
315}
316
317void yahoo_http_post(int id, const char *url, const char *cookies,
318        long content_length, yahoo_get_fd_callback callback, void *data)
319{
320        char host[255];
321        int port = 80;
322        char path[255];
323        char buff[1024];
324        int ssl = 0;
325
326        if (!url_to_host_port_path(url, host, &port, path, &ssl))
327                return;
328
329        /* thanks to kopete dumpcap */
330        snprintf(buff, sizeof(buff),
331                "POST %s HTTP/1.1\r\n"
332                "Cookie: %s\r\n"
333                "User-Agent: Mozilla/5.0\r\n"
334                "Host: %s\r\n"
335                "Content-Length: %ld\r\n"
336                "Cache-Control: no-cache\r\n"
337                "\r\n", path, cookies, host, content_length);
338
339        yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
340}
341
342void yahoo_http_get(int id, const char *url, const char *cookies, int http11,
343        int keepalive, yahoo_get_fd_callback callback, void *data)
344{
345        char host[255];
346        int port = 80;
347        char path[255];
348        char buff[2048];
349        char cookiebuff[1024];
350        int ssl = 0;
351
352        if (!url_to_host_port_path(url, host, &port, path, &ssl))
353                return;
354
355        /* Allow cases when we don't need to send a cookie */
356        if (cookies)
357                snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n",
358                        cookies);
359        else
360                cookiebuff[0] = '\0';
361
362        snprintf(buff, sizeof(buff),
363                "GET %s HTTP/1.%s\r\n"
364                "%sHost: %s\r\n"
365                "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n"
366                "Accept: */*\r\n"
367                "%s" "\r\n", path, http11?"1":"0", cookiebuff, host,
368                keepalive? "Connection: Keep-Alive\r\n":"Connection: close\r\n");
369
370        yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
371}
372
373void yahoo_http_head(int id, const char *url, const char *cookies, int len,
374        char *payload, yahoo_get_fd_callback callback, void *data)
375{
376        char host[255];
377        int port = 80;
378        char path[255];
379        char buff[2048];
380        char cookiebuff[1024];
381        int ssl = 0;
382
383        if (!url_to_host_port_path(url, host, &port, path, &ssl))
384                return;
385
386        /* Allow cases when we don't need to send a cookie */
387        if (cookies)
388                snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n",
389                        cookies);
390        else
391                cookiebuff[0] = '\0';
392
393        snprintf(buff, sizeof(buff),
394                "HEAD %s HTTP/1.0\r\n"
395                "Accept: */*\r\n"
396                "Host: %s:%d\r\n"
397                "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n"
398                "%s"
399                "Content-Length: %d\r\n"
400                "Cache-Control: no-cache\r\n"
401                "\r\n%s", path, host, port, cookiebuff, len,
402                payload?payload:"");
403
404        yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
405}
406
Note: See TracBrowser for help on using the repository browser.