summaryrefslogtreecommitdiff
path: root/src/lib/url.c
blob: 3eb0e358f952282e8ab2dc8515a2d638315c1607 (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*******************************************************************************
 * libproxy - A library for proxy configuration
 * Copyright (C) 2006 Nathaniel McCallum <nathaniel@natemccallum.com>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 ******************************************************************************/

#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include "misc.h"
#include "url.h"

/**
 * pxURL object. All fields are private.
 */
struct _pxURL {
	char             *url;
	char             *scheme;
	char             *host;
	int               port;
	char             *path;
	struct sockaddr **ips;
};

/**
 * @return Frees the pxURL
 */
void
px_url_free(pxURL *self)
{
	if (!self) return;
	px_free(self->url);
	px_free(self->scheme);
	px_free(self->host);
	px_free(self->path);
	if (self->ips)
	{
		for (int i=0 ; self->ips[i] ; i++)
			px_free(self->ips[i]);
		px_free(self->ips);
	}
	px_free(self);
}

/**
 * Tells whether or not a pxURL string has valid syntax
 * @return true if the pxURL is valid, otherwise false
 */
bool
px_url_is_valid(const char *url)
{
	pxURL *tmp = px_url_new(url);
	if (!tmp) return false;
	px_url_free(tmp);
	return true;
}

/**
 * Sends a get request for the pxURL.
 * @headers A list of headers to be included in the request.
 * @return Socket to read the response on.
 */
int
px_url_get(pxURL *self, const char **headers)
{
	char *request = NULL;
	char *joined_headers = NULL;
	int sock = -1;
	
	// DNS lookup of host
	if (!px_url_get_ips(self)) goto error;
	
	// Iterate through each pxIP trying to make a connection
	for (int i = 0 ;  self->ips && self->ips[i] && sock < 0 ; i++)
	{
		sock = socket(self->ips[i]->sa_family, SOCK_STREAM, 0);
		if (sock < 0) continue;
		
		if (self->ips[i]->sa_family == AF_INET && 
			!connect(sock, self->ips[i], sizeof(struct sockaddr_in)))
			break;
		else if (self->ips[i]->sa_family == AF_INET6 && 
			!connect(sock, self->ips[i], sizeof(struct sockaddr_in6)))
			break;

		close(sock);
		sock = -1;
	}
	if (sock < 0) goto error;
	
	// Merge optional headers
	if (headers)
	{
		joined_headers = px_strjoin(headers, "\r\n");
		if (!joined_headers) goto error;
	}
	else
		joined_headers = px_strdup("");

	// Create request header
	request = px_strcat("GET ", px_url_get_path(self), 
						" HTTP/1.1\r\nHost: ", px_url_get_host(self),
						"\r\n", joined_headers, "\r\n\r\n", NULL);
	px_free(joined_headers);
			
	// Send HTTP request
	if (send(sock, request, strlen(request), 0) != strlen(request)) goto error;
	px_free(request); request = NULL;
	
	// Return the socket, which is ready for reading the response
	return sock;
	
	error:
		if (sock >= 0) close(sock);
		px_free(request);
		return -1;
}

/**
 * @return The default port for this type of pxURL
 */
static int
px_url_get_default_port(pxURL *self)
{
	struct servent *serv;
	if ((serv = getservbyname(self->scheme, NULL))) return ntohs(serv->s_port);
	return 0;
}

/**
 * @return Host portion of the pxURL
 */
const char *
px_url_get_host(pxURL *self)
{
	if (!self) return NULL;
	return self->host;
}

/**
 * Get the IP address of the hostname in this pxURL without using DNS.
 * @return IP address of the host in the pxURL. 
 */
const struct sockaddr *
px_url_get_ip_no_dns(pxURL *self)
{
	if (!self) return NULL;
	
	// Check the cache
	if (self->ips && self->ips[0])
		return (const struct sockaddr *) self->ips[0];
	px_free(self->ips);
	
	// Try for IPv4 first
	struct sockaddr *ip = px_malloc0(sizeof(struct sockaddr_in));
	if (inet_pton(AF_INET, px_url_get_host(self), &((struct sockaddr_in *) ip)->sin_addr) > 0)
	{
		self->ips = px_malloc0(sizeof(struct sockaddr *) * 2);
		self->ips[0] = ip;
		self->ips[0]->sa_family = AF_INET;
		return (const struct sockaddr *) self->ips[0];
	}
	px_free(ip);
	
	// Try for IPv6 next
	ip = px_malloc0(sizeof(struct sockaddr_in6));
	if (inet_pton(AF_INET6, px_url_get_host(self), &((struct sockaddr_in6 *) ip)->sin6_addr) > 0)
	{
		self->ips = px_malloc0(sizeof(struct sockaddr *) * 2);
		self->ips[0] = ip;
		self->ips[0]->sa_family = AF_INET6;
		return (const struct sockaddr *) self->ips[0];
	}
	px_free(ip);
	
	// The hostname was not an IP address
	return NULL;
}

/**
 * Get the IP addresses of the hostname in this pxURL.  Use DNS if necessary.
 * @return IP addresses of the host in the pxURL. 
 */
const struct sockaddr **
px_url_get_ips(pxURL *self)
{
	if (!self) return NULL;
	
	// Check the cache
	if (self->ips) return (const struct sockaddr **) self->ips;
	
	// Check without DNS first
	if (px_url_get_ip_no_dns(self)) return (const struct sockaddr **) self->ips;
	
	// Check DNS for IPs
	struct addrinfo *info;
	if (!getaddrinfo(px_url_get_host(self), NULL, NULL, &info))
	{
		struct addrinfo *first = info;
		int count;
		
		// Count how many IPs we got back
		for (count=0 ; info ; info = info->ai_next)
			count++;
		
		// Copy the sockaddr's into self->ips
		info = first;
		self->ips = px_malloc0(sizeof(struct sockaddr *) * ++count);
		for (int i=0 ; info ; info = info->ai_next)
		{
			if (info->ai_addr->sa_family == AF_INET)
			{
				self->ips[i] = px_malloc0(sizeof(struct sockaddr_in));
				memcpy(self->ips[i], info->ai_addr, sizeof(struct sockaddr_in));
				((struct sockaddr_in *) self->ips[i++])->sin_port = htons(self->port);
			}
			else if (info->ai_addr->sa_family == AF_INET6)
			{
				self->ips[i] = px_malloc0(sizeof(struct sockaddr_in6));
				memcpy(self->ips[i], info->ai_addr, sizeof(struct sockaddr_in6));
				((struct sockaddr_in6 *) self->ips[i++])->sin6_port = htons(self->port);
			}
		}
		
		freeaddrinfo(first);
		return (const struct sockaddr **) self->ips;
	}
	
	// No addresses found
	return NULL;
}

/**
 * @return Path portion of the pxURL
 */
const char *
px_url_get_path(pxURL *self)
{
	if (!self) return NULL;
	return self->path;
}

/**
 * @return Port portion of the pxURL
 */
int
px_url_get_port(pxURL *self)
{
	if (!self) return 0;
	return self->port;
}

/**
 * @return Scheme portion of the pxURL
 */
const char *
px_url_get_scheme(pxURL *self)
{
	if (!self) return NULL;
	return self->scheme;
}

/**
 * @url String used to create the new pxURL object
 * @return New pxURL object
 */
pxURL *
px_url_new(const char *url)
{
	// Allocate pxURL
	pxURL *self  = px_malloc0(sizeof(pxURL));
	
	// Get scheme
	if (!strstr(url, "://")) goto error;
	self->scheme = px_strndup(url, strstr(url, "://") - url);
	
	// Get host
	self->host   = px_strdup(strstr(url, "://") + strlen("://"));
	
	// Get path
	self->path   = px_strdup(strchr(self->host, '/'));
	if (self->path)
		self->host[strlen(self->host) - strlen(self->path)] = 0;
	else
		self->path = px_strdup("");
		
	// Get the port
	bool port_specified = false;
	if (strchr(self->host, ':')) {
		if (!atoi(strchr(self->host, ':')+1)) goto error;
		self->port = atoi(strchr(self->host, ':')+1);
		*(strchr(self->host, ':')) = 0;
		port_specified = true;
	}
	else
		self->port = px_url_get_default_port(self);
		
	// Make sure we have a real host
	if (!strcmp(self->host, "")) goto error;
	
	// Verify by re-assembly
	self->url = px_malloc0(strlen(url) + 1);
	if (!port_specified)
		snprintf(self->url, strlen(url) + 1, "%s://%s%s", self->scheme, self->host, self->path);
	else
		snprintf(self->url, strlen(url) + 1, "%s://%s:%d%s", self->scheme, self->host, self->port, self->path);
	if (strcmp(self->url, url)) goto error;
	
	return self;
	
	error:
		px_url_free(self);
		return NULL;
}

/**
 * @return String representation of the pxURL
 */
const char *
px_url_to_string(pxURL *self)
{
	if (!self) return NULL;
	return self->url;
}

/**
 * @return true if the URLs are the same, else false
 */
bool
px_url_equals(pxURL *self, const pxURL *other)
{
	return !strcmp(px_url_to_string(self), px_url_to_string((pxURL*) other));
}