summaryrefslogtreecommitdiff
path: root/libproxy/modules/ignore_domain.cpp
blob: f39e5459024a199985b24fc8f50ec21a119177b6 (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
/*******************************************************************************
 * 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 <cstdio>

#include "../extension_ignore.hpp"
using namespace libproxy;

class domain_ignore_extension : public ignore_extension {
public:
	virtual bool ignore(url& url, const string &ignorestr) {
		/* Get our URL's hostname and port */
		string host = url.get_host();
		int    port = url.get_port();

		/* Get our ignore pattern's hostname and port */
		string ihost = ignorestr;
		int    iport = 0;
		if (ihost.find(':') != string::npos) {
				if (sscanf(ignorestr.substr(ihost.find(':')+1).c_str(), "%d", &iport) == 1)
						ihost = ihost.substr(0, ihost.find(':'));
				else
						iport = 0;
		}

		/* Hostname match (domain.com or domain.com:80) */
		if (host == ihost)
			return (iport == 0 || port == iport);

		/* Endswith (.domain.com or .domain.com:80) */
		if (ihost[0] == '.' && host.find(ihost) == host.size() - ihost.size() && host.size() >= ihost.size())
			return (iport == 0 || port == iport);

		/* Glob (*.domain.com or *.domain.com:80) */
		if (ihost[0] == '*' && host.find(ihost.substr(1)) == host.size() - ihost.substr(1).size() && host.size() >= ihost.substr(1).size()) 
			return (iport == 0 || port == iport);

		/* No match was found */
		return false;
	}
};

MM_MODULE_INIT_EZ(domain_ignore_extension, true, NULL, NULL);