xref: /haiku/src/kits/network/libnetservices/UrlContext.cpp (revision 9918c8295480d70f55fafb98008e23109b598871)
1 /*
2  * Copyright 2010-2015 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Christophe Huriaux, c.huriaux@gmail.com
7  *		Adrien Destugues, pulkomandy@pulkomandy.tk
8  */
9 
10 
11 #include <UrlContext.h>
12 
13 #include <stdio.h>
14 
15 #include <HashMap.h>
16 #include <HashString.h>
17 
18 
19 #ifndef LIBNETAPI_DEPRECATED
20 using namespace BPrivate::Network;
21 #endif
22 
23 class BUrlContext::BHttpAuthenticationMap : public
24 	SynchronizedHashMap<BPrivate::HashString, BHttpAuthentication*> {};
25 
26 
27 BUrlContext::BUrlContext()
28 	:
29 	fCookieJar(),
30 	fAuthenticationMap(NULL),
31 	fCertificates(20, true),
32 	fProxyHost(),
33 	fProxyPort(0)
34 {
35 	fAuthenticationMap = new(std::nothrow) BHttpAuthenticationMap();
36 
37 	// This is the default authentication, used when nothing else is found.
38 	// The empty string used as a key will match all the domain strings, once
39 	// we have removed all components.
40 	fAuthenticationMap->Put(HashString("", 0), new BHttpAuthentication());
41 }
42 
43 
44 BUrlContext::~BUrlContext()
45 {
46 	BHttpAuthenticationMap::Iterator iterator
47 		= fAuthenticationMap->GetIterator();
48 	while (iterator.HasNext())
49 		delete iterator.Next().value;
50 
51 	delete fAuthenticationMap;
52 }
53 
54 
55 // #pragma mark Context modifiers
56 
57 
58 void
59 BUrlContext::SetCookieJar(const BNetworkCookieJar& cookieJar)
60 {
61 	fCookieJar = cookieJar;
62 }
63 
64 
65 void
66 BUrlContext::AddAuthentication(const BUrl& url,
67 	const BHttpAuthentication& authentication)
68 {
69 	BString domain = url.Host();
70 	domain += url.Path();
71 	BPrivate::HashString hostHash(domain.String(), domain.Length());
72 
73 	fAuthenticationMap->Lock();
74 
75 	BHttpAuthentication* previous = fAuthenticationMap->Get(hostHash);
76 
77 	if (previous)
78 		*previous = authentication;
79 	else {
80 		BHttpAuthentication* copy
81 			= new(std::nothrow) BHttpAuthentication(authentication);
82 		fAuthenticationMap->Put(hostHash, copy);
83 	}
84 
85 	fAuthenticationMap->Unlock();
86 }
87 
88 
89 void
90 BUrlContext::SetProxy(BString host, uint16 port)
91 {
92 	fProxyHost = host;
93 	fProxyPort = port;
94 }
95 
96 
97 void
98 BUrlContext::AddCertificateException(const BCertificate& certificate)
99 {
100 	BCertificate* copy = new(std::nothrow) BCertificate(certificate);
101 	if (copy != NULL) {
102 		fCertificates.AddItem(copy);
103 	}
104 }
105 
106 
107 // #pragma mark Context accessors
108 
109 
110 BNetworkCookieJar&
111 BUrlContext::GetCookieJar()
112 {
113 	return fCookieJar;
114 }
115 
116 
117 BHttpAuthentication&
118 BUrlContext::GetAuthentication(const BUrl& url)
119 {
120 	BString domain = url.Host();
121 	domain += url.Path();
122 
123 	BHttpAuthentication* authentication = NULL;
124 
125 	do {
126 		authentication = fAuthenticationMap->Get( HashString(domain.String(),
127 			domain.Length()));
128 
129 		domain.Truncate(domain.FindLast('/'));
130 
131 	} while (authentication == NULL);
132 
133 	return *authentication;
134 }
135 
136 
137 bool
138 BUrlContext::UseProxy()
139 {
140 	return !fProxyHost.IsEmpty();
141 }
142 
143 
144 BString
145 BUrlContext::GetProxyHost()
146 {
147 	return fProxyHost;
148 }
149 
150 
151 uint16
152 BUrlContext::GetProxyPort()
153 {
154 	return fProxyPort;
155 }
156 
157 
158 bool
159 BUrlContext::HasCertificateException(const BCertificate& certificate)
160 {
161 	struct Equals: public UnaryPredicate<const BCertificate> {
162 		Equals(const BCertificate& itemToMatch)
163 			:
164 			fItemToMatch(itemToMatch)
165 		{
166 		}
167 
168 		int operator()(const BCertificate* item) const
169 		{
170 			/* Must return 0 if there is a match! */
171 			return !(*item == fItemToMatch);
172 		}
173 
174 		const BCertificate& fItemToMatch;
175 	} comparator(certificate);
176 
177 	return fCertificates.FindIf(comparator) != NULL;
178 }
179