source: protocols/yahoo/yahoo_httplib.c @ 6b13103

Last change on this file since 6b13103 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
RevLine 
[b7d3cc34]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
[6f10697]18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
[b7d3cc34]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
[9034ba0]32char *strchr(), *strrchr();
[b7d3cc34]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
[3cd37d7]62#if 0
[9034ba0]63int yahoo_tcp_readline(char *ptr, int maxlen, void *fd)
[b7d3cc34]64{
65        int n, rc;
66        char c;
67
68        for (n = 1; n < maxlen; n++) {
69
70                do {
[9034ba0]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 */
[b7d3cc34]73
74                if (rc == 1) {
[9034ba0]75                        if (c == '\r')  /* get rid of \r */
[b7d3cc34]76                                continue;
77                        *ptr = c;
78                        if (c == '\n')
79                                break;
80                        ptr++;
81                } else if (rc == 0) {
82                        if (n == 1)
[9034ba0]83                                return (0);     /* EOF, no data */
[b7d3cc34]84                        else
[9034ba0]85                                break;  /* EOF, w/ data */
[b7d3cc34]86                } else {
87                        return -1;
88                }
89        }
90
91        *ptr = 0;
92        return (n);
93}
[3cd37d7]94#endif
[b7d3cc34]95
96static int url_to_host_port_path(const char *url,
[9034ba0]97        char *host, int *port, char *path, int *ssl)
[b7d3cc34]98{
[9034ba0]99        char *urlcopy = NULL;
100        char *slash = NULL;
101        char *colon = NULL;
102
[b7d3cc34]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
[9034ba0]112         * and https:// variants of the above
[b7d3cc34]113         */
114
[9034ba0]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;
[b7d3cc34]120        } else {
121                WARNING(("Weird url - unknown protocol: %s", url));
122                return 0;
123        }
124
125        slash = strchr(urlcopy, '/');
126        colon = strchr(urlcopy, ':');
127
[9034ba0]128        if (!colon || (slash && slash < colon)) {
129                if (*ssl)
130                        *port = 443;
131                else
132                        *port = 80;
[b7d3cc34]133        } else {
134                *colon = 0;
[9034ba0]135                *port = atoi(colon + 1);
[b7d3cc34]136        }
137
[9034ba0]138        if (!slash) {
[b7d3cc34]139                strcpy(path, "/");
140        } else {
141                strcpy(path, slash);
142                *slash = 0;
143        }
144
145        strcpy(host, urlcopy);
[9034ba0]146
[b7d3cc34]147        FREE(urlcopy);
148
149        return 1;
150}
151
152static int isurlchar(unsigned char c)
153{
[6b13103]154        return (g_ascii_isalnum(c));
[b7d3cc34]155}
156
157char *yahoo_urlencode(const char *instr)
158{
[9034ba0]159        int ipos = 0, bpos = 0;
[b7d3cc34]160        char *str = NULL;
161        int len = strlen(instr);
162
[9034ba0]163        if (!(str = y_new(char, 3 *len + 1)))
164                 return "";
[b7d3cc34]165
[9034ba0]166        while (instr[ipos]) {
167                while (isurlchar(instr[ipos]))
[b7d3cc34]168                        str[bpos++] = instr[ipos++];
[9034ba0]169                if (!instr[ipos])
[b7d3cc34]170                        break;
[9034ba0]171
172                snprintf(&str[bpos], 4, "%%%02x", instr[ipos] & 0xff);
173                bpos += 3;
[b7d3cc34]174                ipos++;
175        }
[9034ba0]176        str[bpos] = '\0';
[b7d3cc34]177
178        /* free extra alloc'ed mem. */
179        len = strlen(str);
[9034ba0]180        str = y_renew(char, str, len + 1);
[b7d3cc34]181
182        return (str);
183}
184
[3cd37d7]185#if 0
[b7d3cc34]186char *yahoo_urldecode(const char *instr)
187{
[9034ba0]188        int ipos = 0, bpos = 0;
[b7d3cc34]189        char *str = NULL;
[9034ba0]190        char entity[3] = { 0, 0, 0 };
[b7d3cc34]191        unsigned dec;
192        int len = strlen(instr);
193
[9034ba0]194        if (!(str = y_new(char, len + 1)))
195                 return "";
[b7d3cc34]196
[9034ba0]197        while (instr[ipos]) {
198                while (instr[ipos] && instr[ipos] != '%')
199                        if (instr[ipos] == '+') {
200                                str[bpos++] = ' ';
[b7d3cc34]201                                ipos++;
202                        } else
203                                str[bpos++] = instr[ipos++];
[9034ba0]204                if (!instr[ipos])
[b7d3cc34]205                        break;
[9034ba0]206
207                if (instr[ipos + 1] && instr[ipos + 2]) {
[b7d3cc34]208                        ipos++;
[9034ba0]209                        entity[0] = instr[ipos++];
210                        entity[1] = instr[ipos++];
[b7d3cc34]211                        sscanf(entity, "%2x", &dec);
212                        str[bpos++] = (char)dec;
213                } else {
214                        str[bpos++] = instr[ipos++];
215                }
216        }
[9034ba0]217        str[bpos] = '\0';
[b7d3cc34]218
219        /* free extra alloc'ed mem. */
220        len = strlen(str);
[9034ba0]221        str = y_renew(char, str, len + 1);
[b7d3cc34]222
223        return (str);
224}
225
226char *yahoo_xmldecode(const char *instr)
227{
[9034ba0]228        int ipos = 0, bpos = 0, epos = 0;
[b7d3cc34]229        char *str = NULL;
[9034ba0]230        char entity[4] = { 0, 0, 0, 0 };
231        char *entitymap[5][2] = {
232                {"amp;", "&"},
[b7d3cc34]233                {"quot;", "\""},
[9034ba0]234                {"lt;", "<"},
235                {"gt;", "<"},
[b7d3cc34]236                {"nbsp;", " "}
237        };
238        unsigned dec;
239        int len = strlen(instr);
240
[9034ba0]241        if (!(str = y_new(char, len + 1)))
242                 return "";
[b7d3cc34]243
[9034ba0]244        while (instr[ipos]) {
245                while (instr[ipos] && instr[ipos] != '&')
246                        if (instr[ipos] == '+') {
247                                str[bpos++] = ' ';
[b7d3cc34]248                                ipos++;
249                        } else
250                                str[bpos++] = instr[ipos++];
[9034ba0]251                if (!instr[ipos] || !instr[ipos + 1])
[b7d3cc34]252                        break;
253                ipos++;
254
[9034ba0]255                if (instr[ipos] == '#') {
[b7d3cc34]256                        ipos++;
[9034ba0]257                        epos = 0;
258                        while (instr[ipos] != ';')
259                                entity[epos++] = instr[ipos++];
[b7d3cc34]260                        sscanf(entity, "%u", &dec);
261                        str[bpos++] = (char)dec;
262                        ipos++;
263                } else {
264                        int i;
[9034ba0]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];
[b7d3cc34]269                                        ipos += strlen(entitymap[i][0]);
270                                        break;
271                                }
272                }
273        }
[9034ba0]274        str[bpos] = '\0';
[b7d3cc34]275
276        /* free extra alloc'ed mem. */
277        len = strlen(str);
[9034ba0]278        str = y_renew(char, str, len + 1);
[b7d3cc34]279
280        return (str);
281}
[3cd37d7]282#endif
[b7d3cc34]283
[9034ba0]284typedef void (*http_connected) (int id, void *fd, int error);
[b7d3cc34]285
286struct callback_data {
287        int id;
288        yahoo_get_fd_callback callback;
289        char *request;
290        void *user_data;
291};
292
[9034ba0]293static void connect_complete(void *fd, int error, void *data)
[b7d3cc34]294{
295        struct callback_data *ccd = data;
[9034ba0]296        if (error == 0)
297                YAHOO_CALLBACK(ext_yahoo_write) (fd, ccd->request,
298                        strlen(ccd->request));
299        free(ccd->request);
[b7d3cc34]300        ccd->callback(ccd->id, fd, error, ccd->user_data);
301        FREE(ccd);
302}
303
[9034ba0]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)
[b7d3cc34]306{
[9034ba0]307        struct callback_data *ccd = y_new0(struct callback_data, 1);
[b7d3cc34]308        ccd->callback = callback;
309        ccd->id = id;
310        ccd->request = strdup(request);
311        ccd->user_data = data;
[9034ba0]312
313        YAHOO_CALLBACK(ext_yahoo_connect_async) (id, host, port,
314                connect_complete, ccd, use_ssl);
[b7d3cc34]315}
316
[9034ba0]317void yahoo_http_post(int id, const char *url, const char *cookies,
318        long content_length, yahoo_get_fd_callback callback, void *data)
[b7d3cc34]319{
320        char host[255];
321        int port = 80;
322        char path[255];
323        char buff[1024];
[9034ba0]324        int ssl = 0;
[b7d3cc34]325
[9034ba0]326        if (!url_to_host_port_path(url, host, &port, path, &ssl))
327                return;
[b7d3cc34]328
[9034ba0]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);
[b7d3cc34]340}
341
[9034ba0]342void yahoo_http_get(int id, const char *url, const char *cookies, int http11,
343        int keepalive, yahoo_get_fd_callback callback, void *data)
[b7d3cc34]344{
345        char host[255];
346        int port = 80;
347        char path[255];
[9034ba0]348        char buff[2048];
349        char cookiebuff[1024];
350        int ssl = 0;
[b7d3cc34]351
[9034ba0]352        if (!url_to_host_port_path(url, host, &port, path, &ssl))
353                return;
[b7d3cc34]354
[9034ba0]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);
[b7d3cc34]371}
372
[9034ba0]373void yahoo_http_head(int id, const char *url, const char *cookies, int len,
374        char *payload, yahoo_get_fd_callback callback, void *data)
[b7d3cc34]375{
[9034ba0]376        char host[255];
377        int port = 80;
378        char path[255];
379        char buff[2048];
380        char cookiebuff[1024];
381        int ssl = 0;
[b7d3cc34]382
[9034ba0]383        if (!url_to_host_port_path(url, host, &port, path, &ssl))
[b7d3cc34]384                return;
385
[9034ba0]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);
[b7d3cc34]405}
406
Note: See TracBrowser for help on using the repository browser.