source: protocols/yahoo/yahoo_httplib.c @ d80822c

Last change on this file since d80822c was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

  • Property mode set to 100644
File size: 9.2 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                        }
78                        *ptr = c;
79                        if (c == '\n') {
80                                break;
81                        }
82                        ptr++;
83                } else if (rc == 0) {
84                        if (n == 1) {
85                                return (0);     /* EOF, no data */
86                        } else {
87                                break;  /* EOF, w/ data */
88                        }
89                } else {
90                        return -1;
91                }
92        }
93
94        *ptr = 0;
95        return (n);
96}
97#endif
98
99static int url_to_host_port_path(const char *url,
100                                 char *host, int *port, char *path, int *ssl)
101{
102        char *urlcopy = NULL;
103        char *slash = NULL;
104        char *colon = NULL;
105
106        /*
107         * http://hostname
108         * http://hostname/
109         * http://hostname/path
110         * http://hostname/path:foo
111         * http://hostname:port
112         * http://hostname:port/
113         * http://hostname:port/path
114         * http://hostname:port/path:foo
115         * and https:// variants of the above
116         */
117
118        if (strstr(url, "http://") == url) {
119                urlcopy = strdup(url + 7);
120        } else if (strstr(url, "https://") == url) {
121                urlcopy = strdup(url + 8);
122                *ssl = 1;
123        } else {
124                WARNING(("Weird url - unknown protocol: %s", url));
125                return 0;
126        }
127
128        slash = strchr(urlcopy, '/');
129        colon = strchr(urlcopy, ':');
130
131        if (!colon || (slash && slash < colon)) {
132                if (*ssl) {
133                        *port = 443;
134                } else {
135                        *port = 80;
136                }
137        } else {
138                *colon = 0;
139                *port = atoi(colon + 1);
140        }
141
142        if (!slash) {
143                strcpy(path, "/");
144        } else {
145                strcpy(path, slash);
146                *slash = 0;
147        }
148
149        strcpy(host, urlcopy);
150
151        FREE(urlcopy);
152
153        return 1;
154}
155
156static int isurlchar(unsigned char c)
157{
158        return (g_ascii_isalnum(c));
159}
160
161char *yahoo_urlencode(const char *instr)
162{
163        int ipos = 0, bpos = 0;
164        char *str = NULL;
165        int len = strlen(instr);
166
167        if (!(str = y_new(char, 3 * len + 1))) {
168                return "";
169        }
170
171        while (instr[ipos]) {
172                while (isurlchar(instr[ipos])) {
173                        str[bpos++] = instr[ipos++];
174                }
175                if (!instr[ipos]) {
176                        break;
177                }
178
179                snprintf(&str[bpos], 4, "%%%02x", instr[ipos] & 0xff);
180                bpos += 3;
181                ipos++;
182        }
183        str[bpos] = '\0';
184
185        /* free extra alloc'ed mem. */
186        len = strlen(str);
187        str = y_renew(char, str, len + 1);
188
189        return (str);
190}
191
192#if 0
193char *yahoo_urldecode(const char *instr)
194{
195        int ipos = 0, bpos = 0;
196        char *str = NULL;
197        char entity[3] = { 0, 0, 0 };
198        unsigned dec;
199        int len = strlen(instr);
200
201        if (!(str = y_new(char, len + 1))) {
202                return "";
203        }
204
205        while (instr[ipos]) {
206                while (instr[ipos] && instr[ipos] != '%') {
207                        if (instr[ipos] == '+') {
208                                str[bpos++] = ' ';
209                                ipos++;
210                        } else {
211                                str[bpos++] = instr[ipos++];
212                        }
213                }
214                if (!instr[ipos]) {
215                        break;
216                }
217
218                if (instr[ipos + 1] && instr[ipos + 2]) {
219                        ipos++;
220                        entity[0] = instr[ipos++];
221                        entity[1] = instr[ipos++];
222                        sscanf(entity, "%2x", &dec);
223                        str[bpos++] = (char) dec;
224                } else {
225                        str[bpos++] = instr[ipos++];
226                }
227        }
228        str[bpos] = '\0';
229
230        /* free extra alloc'ed mem. */
231        len = strlen(str);
232        str = y_renew(char, str, len + 1);
233
234        return (str);
235}
236
237char *yahoo_xmldecode(const char *instr)
238{
239        int ipos = 0, bpos = 0, epos = 0;
240        char *str = NULL;
241        char entity[4] = { 0, 0, 0, 0 };
242        char *entitymap[5][2] = {
243                { "amp;", "&" },
244                { "quot;", "\"" },
245                { "lt;", "<" },
246                { "gt;", "<" },
247                { "nbsp;", " " }
248        };
249        unsigned dec;
250        int len = strlen(instr);
251
252        if (!(str = y_new(char, len + 1))) {
253                return "";
254        }
255
256        while (instr[ipos]) {
257                while (instr[ipos] && instr[ipos] != '&') {
258                        if (instr[ipos] == '+') {
259                                str[bpos++] = ' ';
260                                ipos++;
261                        } else {
262                                str[bpos++] = instr[ipos++];
263                        }
264                }
265                if (!instr[ipos] || !instr[ipos + 1]) {
266                        break;
267                }
268                ipos++;
269
270                if (instr[ipos] == '#') {
271                        ipos++;
272                        epos = 0;
273                        while (instr[ipos] != ';') {
274                                entity[epos++] = instr[ipos++];
275                        }
276                        sscanf(entity, "%u", &dec);
277                        str[bpos++] = (char) dec;
278                        ipos++;
279                } else {
280                        int i;
281                        for (i = 0; i < 5; i++) {
282                                if (!strncmp(instr + ipos, entitymap[i][0],
283                                             strlen(entitymap[i][0]))) {
284                                        str[bpos++] = entitymap[i][1][0];
285                                        ipos += strlen(entitymap[i][0]);
286                                        break;
287                                }
288                        }
289                }
290        }
291        str[bpos] = '\0';
292
293        /* free extra alloc'ed mem. */
294        len = strlen(str);
295        str = y_renew(char, str, len + 1);
296
297        return (str);
298}
299#endif
300
301typedef void (*http_connected) (int id, void *fd, int error);
302
303struct callback_data {
304        int id;
305        yahoo_get_fd_callback callback;
306        char *request;
307        void *user_data;
308};
309
310static void connect_complete(void *fd, int error, void *data)
311{
312        struct callback_data *ccd = data;
313
314        if (error == 0) {
315                YAHOO_CALLBACK (ext_yahoo_write) (fd, ccd->request,
316                                                  strlen(ccd->request));
317        }
318        free(ccd->request);
319        ccd->callback(ccd->id, fd, error, ccd->user_data);
320        FREE(ccd);
321}
322
323static void yahoo_send_http_request(int id, char *host, int port, char *request,
324                                    yahoo_get_fd_callback callback, void *data, int use_ssl)
325{
326        struct callback_data *ccd = y_new0(struct callback_data, 1);
327
328        ccd->callback = callback;
329        ccd->id = id;
330        ccd->request = strdup(request);
331        ccd->user_data = data;
332
333        YAHOO_CALLBACK (ext_yahoo_connect_async) (id, host, port,
334                                                  connect_complete, ccd, use_ssl);
335}
336
337void yahoo_http_post(int id, const char *url, const char *cookies,
338                     long content_length, yahoo_get_fd_callback callback, void *data)
339{
340        char host[255];
341        int port = 80;
342        char path[255];
343        char buff[1024];
344        int ssl = 0;
345
346        if (!url_to_host_port_path(url, host, &port, path, &ssl)) {
347                return;
348        }
349
350        /* thanks to kopete dumpcap */
351        snprintf(buff, sizeof(buff),
352                 "POST %s HTTP/1.1\r\n"
353                 "Cookie: %s\r\n"
354                 "User-Agent: Mozilla/5.0\r\n"
355                 "Host: %s\r\n"
356                 "Content-Length: %ld\r\n"
357                 "Cache-Control: no-cache\r\n"
358                 "\r\n", path, cookies, host, content_length);
359
360        yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
361}
362
363void yahoo_http_get(int id, const char *url, const char *cookies, int http11,
364                    int keepalive, yahoo_get_fd_callback callback, void *data)
365{
366        char host[255];
367        int port = 80;
368        char path[255];
369        char buff[2048];
370        char cookiebuff[1024];
371        int ssl = 0;
372
373        if (!url_to_host_port_path(url, host, &port, path, &ssl)) {
374                return;
375        }
376
377        /* Allow cases when we don't need to send a cookie */
378        if (cookies) {
379                snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n",
380                         cookies);
381        } else {
382                cookiebuff[0] = '\0';
383        }
384
385        snprintf(buff, sizeof(buff),
386                 "GET %s HTTP/1.%s\r\n"
387                 "%sHost: %s\r\n"
388                 "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n"
389                 "Accept: */*\r\n"
390                 "%s" "\r\n", path, http11 ? "1" : "0", cookiebuff, host,
391                 keepalive ? "Connection: Keep-Alive\r\n" : "Connection: close\r\n");
392
393        yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
394}
395
396void yahoo_http_head(int id, const char *url, const char *cookies, int len,
397                     char *payload, yahoo_get_fd_callback callback, void *data)
398{
399        char host[255];
400        int port = 80;
401        char path[255];
402        char buff[2048];
403        char cookiebuff[1024];
404        int ssl = 0;
405
406        if (!url_to_host_port_path(url, host, &port, path, &ssl)) {
407                return;
408        }
409
410        /* Allow cases when we don't need to send a cookie */
411        if (cookies) {
412                snprintf(cookiebuff, sizeof(cookiebuff), "Cookie: %s\r\n",
413                         cookies);
414        } else {
415                cookiebuff[0] = '\0';
416        }
417
418        snprintf(buff, sizeof(buff),
419                 "HEAD %s HTTP/1.0\r\n"
420                 "Accept: */*\r\n"
421                 "Host: %s:%d\r\n"
422                 "User-Agent: Mozilla/4.5 [en] (" PACKAGE "/" VERSION ")\r\n"
423                 "%s"
424                 "Content-Length: %d\r\n"
425                 "Cache-Control: no-cache\r\n"
426                 "\r\n%s", path, host, port, cookiebuff, len,
427                 payload ? payload : "");
428
429        yahoo_send_http_request(id, host, port, buff, callback, data, ssl);
430}
431
Note: See TracBrowser for help on using the repository browser.