2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Christophe Huriaux, c.huriaux@gmail.com
10 #include <UrlContext.h>
15 #include <HashString.h>
18 BUrlContext::BUrlContext()
21 fAuthenticationMap(NULL
),
25 fAuthenticationMap
= new(std::nothrow
) BHttpAuthenticationMap();
27 // This is the default authentication, used when nothing else is found.
28 // The empty string used as a key will match all the domain strings, once
29 // we have removed all components.
30 fAuthenticationMap
->Put(HashString("", 0), new BHttpAuthentication());
34 BUrlContext::~BUrlContext()
36 BHttpAuthenticationMap::Iterator iterator
37 = fAuthenticationMap
->GetIterator();
38 while (iterator
.HasNext())
39 delete *iterator
.NextValue();
41 delete fAuthenticationMap
;
45 // #pragma mark Context modifiers
49 BUrlContext::SetCookieJar(const BNetworkCookieJar
& cookieJar
)
51 fCookieJar
= cookieJar
;
56 BUrlContext::AddAuthentication(const BUrl
& url
,
57 const BHttpAuthentication
& authentication
)
59 BString domain
= url
.Host();
61 BPrivate::HashString
hostHash(domain
.String(), domain
.Length());
63 fAuthenticationMap
->Lock();
65 BHttpAuthentication
* previous
= fAuthenticationMap
->Get(hostHash
);
68 *previous
= authentication
;
70 BHttpAuthentication
* copy
71 = new(std::nothrow
) BHttpAuthentication(authentication
);
72 fAuthenticationMap
->Put(hostHash
, copy
);
75 fAuthenticationMap
->Unlock();
80 BUrlContext::SetProxy(BString host
, uint16 port
)
87 // #pragma mark Context accessors
91 BUrlContext::GetCookieJar()
98 BUrlContext::GetAuthentication(const BUrl
& url
)
100 BString domain
= url
.Host();
101 domain
+= url
.Path();
103 BHttpAuthentication
* authentication
= NULL
;
106 authentication
= fAuthenticationMap
->Get( HashString(domain
.String(),
109 domain
.Truncate(domain
.FindLast('/'));
111 } while (authentication
== NULL
);
113 return *authentication
;
118 BUrlContext::UseProxy()
120 return !fProxyHost
.IsEmpty();
125 BUrlContext::GetProxyHost()
132 BUrlContext::GetProxyPort()