summaryrefslogtreecommitdiff
path: root/helper/GetUrl.c
blob: 25321226be98beca6f9662cce7a81fe3ad0a2688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* $Xorg: GetUrl.c,v 1.4 2001/02/09 02:05:57 xorgcvs Exp $ */
/*

Copyright 1996, 1998 The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABIL-
ITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization from
The Open Group.

*/
/* $XFree86: xc/programs/xrx/helper/GetUrl.c,v 1.4tsi Exp $ */

/*
 * This file is really split into two major parts where GetUrl is implemented
 * in two completely different ways.
 * The first one, which is used when XUSE_WWW is defined uses the standalone
 * program "www" to perform the GET request.
 * The second one, based on the xtrans layer, performs the GET request
 * directly.
 */

#define Free(b) if (b) free(b)

#ifdef XUSE_WWW

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xos.h>		/* for strcmp() etc... */

/*
 * GetUrl first method, using www.
 */

int
GetUrl(char *url, char **reply_ret, int *len_ret)
{
    char buf[BUFSIZ], *reply, *ptr;
    FILE *fp;
    int readbytes, size;

    sprintf(buf, "www -source \"%s\"", url);
    fp = popen(buf, "r");
    if (fp == NULL)
	return 1;

    reply = NULL;
    size = 0;
    do {
	readbytes = fread(buf, sizeof(char), BUFSIZ, fp);
	if (readbytes != 0) {
	    /* malloc enough memory */
	    ptr = (char *) realloc(reply, sizeof(char) * size + readbytes);
	    if (ptr == NULL) {	/* memory allocation failed */
		Free(reply);
		return 1;
	    }
	    reply = ptr;
	    memcpy(reply + size, buf, readbytes);
	    size += readbytes;
	}
    } while (readbytes == BUFSIZ);
    pclose(fp);

    *reply_ret = reply;
    *len_ret = size;
    return 0;
}

#else /* XUSE_WWW */

/*
 * GetUrl second method, using the xtrans layer.
 */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

/* get definitions for the HTTP protocol */
#if !defined(UNIXCPP) || defined(ANSICPP)
#define TRANS(func) _HttpTrans##func
#else
#define TRANS(func) _HttpTrans/**/func
#endif
#define TRANS_CLIENT
#include <X11/Xtrans/Xtrans.h>

#define HTTP_CONNECTION_RETRIES 5

static XtransConnInfo
OpenConnection(char *hostname, int port)
{
    int retry;				/* retry counter */
    XtransConnInfo trans_conn = NULL;	/* transport connection object */
    int connect_stat;
    char address[128];		/* address passed to the transport layer */
    char protocol[] = "tcp";	/* hardcoded transport */

    /* make the address of the form: protocol/host:port */
    sprintf(address,"%s/%s:%d",
	protocol ? protocol : "",
	hostname ? hostname : "",
	port);

    /*
     * Make the connection. Do retries in case server host has hit its
     * backlog (which, unfortunately, isn't distinguishable from there not
     * being a server listening at all, which is why we have to not retry
     * too many times).
     */
    for(retry = HTTP_CONNECTION_RETRIES; retry >= 0; retry--) {
	if ((trans_conn = _HttpTransOpenCOTSClient(address)) == NULL)
	    break;

	if ((connect_stat = _HttpTransConnect(trans_conn, address)) < 0) {
	    _HttpTransClose(trans_conn);
	    trans_conn = NULL;

	    if (connect_stat == TRANS_TRY_CONNECT_AGAIN) {
		sleep(1);
		continue;
	    } else
		break;
	}
    }

    return trans_conn;
}

static void
CloseConnection(XtransConnInfo trans_conn)
{
    _HttpTransDisconnect(trans_conn);
    _HttpTransClose(trans_conn);
}

static void
SendGetRequest(XtransConnInfo trans_conn, char *filename)
{
    char *request;
    char request_format[] =
	"GET %s HTTP/1.0\r\nUser-Agent: xrx\r\nAccept: */*\r\n\r\n";
    int request_len = strlen(filename) + sizeof(request_format) + 1;

    /* build request */
    request = (char *)malloc(request_len);
    sprintf(request, request_format, filename);
    /* send it */
    _HttpTransWrite(trans_conn, request, request_len);

    Free(request);
}

static int
ReadGetReply(XtransConnInfo trans_conn, char **reply_ret)
{
    char *reply = NULL;
    char buf[BUFSIZ];
    int len, nbytes;

    len = 0;
    do {
	nbytes = _HttpTransRead(trans_conn, buf, BUFSIZ);
	if (nbytes > 0) {
	    reply = (char *)realloc(reply, len + nbytes);
	    if (reply == NULL)
		break;
	    memcpy(reply + len, buf, nbytes);
	    len += nbytes;
	}
    } while (nbytes != 0);

    *reply_ret = reply;
    return len;
}

/* per HTTP 1.0 spec */
#define HTTP_DEFAULT_PORT 80

/* Parse given http url and return hostname, port number and path.
 * expected syntax: "http:" "//" host [ ":" port ] [ abs_path ]
 * default port: 80
 */
static int
ParseHttpUrl(char *url, char **hostname_ret, int *port_ret, char **path_ret)
{
    char HTTP[] = "http://";
    char *hostname, *path;
    int port;
    char *ptr, *bos;
    int status = 0;
    int bracketed = 0;

    /* check if it's an http url */
    if (strncmp(HTTP, url, sizeof(HTTP) - 1))
	return 1;		/* this is not an http url */

    /* parse hostname */
    bos = ptr = url + sizeof(HTTP) - 1;
    /* Check for RFC 2732 bracketed IPv6 numeric address */
    if (*ptr == '[') {
	bos++;
	while (*ptr && (*ptr != ']')) {
	    ptr++;
	}
	bracketed = 1;
    } else {
	while (*ptr && *ptr != ':' && *ptr != '/')
	    ptr++;
    }
    if (bos == ptr)
	return 1;		/* doesn't have any hostname */

    hostname = (char *)malloc(ptr - bos + 1);
    if (hostname == NULL)
	return 1;		/* not enouch memory */
    memcpy(hostname, bos, ptr - bos);
    hostname[ptr - bos] = '\0';

    /* make sure path is initialized in case of error */
    path = NULL;

    if (bracketed)
	ptr++;

    /* parse port */
    if (*ptr != ':' || ! ptr[1])
	port = HTTP_DEFAULT_PORT;
    else {
	++ptr;			/* skip ':' */
	port = 0;
	while (*ptr && *ptr != '/') {
	    if (isdigit((int) *ptr)) {
		port *= 10;
		port += *ptr - '0';
	    } else {
		status = 1;	/* bad port specification */
		goto error;
	    }
	    ptr++;
	}
    }

    /* the rest of the url is the path */
    path = (char *)malloc(strlen(ptr) + 1);
    if (path == NULL) {
	status = 1;		/* not enouch memory */
	goto error;
    }
    strcpy(path, ptr);

    /* set returned value */
    *hostname_ret = hostname;
    *port_ret = port;
    *path_ret = path;
    return 0;

error:
    Free(hostname);
    Free(path);
    return status;
}

int
GetUrl(char *url, char **reply_ret, int *len_ret)
{
    char *hostname, *path;
    int port;
    XtransConnInfo trans;
    int status = 0;

    if (ParseHttpUrl(url, &hostname, &port, &path) != 0)
	return 1;		/* invalid URL */

    trans = OpenConnection(hostname, port);
    if (trans == NULL) {
	status = 1;		/* connection failed */
	goto end;
    }

    SendGetRequest(trans, path);

    *len_ret = ReadGetReply(trans, reply_ret);

    CloseConnection(trans);

end:
    Free(hostname);
    Free(path);
    return status;
}


#endif /* XUSE_WWW */