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