tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / network / libnetapi / UrlContext.cpp
blob6a9f6e1028788c67d19669f2d50a498e481f6cd4
1 /*
2 * Copyright 2010 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Christophe Huriaux, c.huriaux@gmail.com
7 */
10 #include <UrlContext.h>
12 #include <stdio.h>
14 #include <HashMap.h>
15 #include <HashString.h>
18 BUrlContext::BUrlContext()
20 fCookieJar(),
21 fAuthenticationMap(NULL),
22 fProxyHost(),
23 fProxyPort(0)
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
48 void
49 BUrlContext::SetCookieJar(const BNetworkCookieJar& cookieJar)
51 fCookieJar = cookieJar;
55 void
56 BUrlContext::AddAuthentication(const BUrl& url,
57 const BHttpAuthentication& authentication)
59 BString domain = url.Host();
60 domain += url.Path();
61 BPrivate::HashString hostHash(domain.String(), domain.Length());
63 fAuthenticationMap->Lock();
65 BHttpAuthentication* previous = fAuthenticationMap->Get(hostHash);
67 if (previous)
68 *previous = authentication;
69 else {
70 BHttpAuthentication* copy
71 = new(std::nothrow) BHttpAuthentication(authentication);
72 fAuthenticationMap->Put(hostHash, copy);
75 fAuthenticationMap->Unlock();
79 void
80 BUrlContext::SetProxy(BString host, uint16 port)
82 fProxyHost = host;
83 fProxyPort = port;
87 // #pragma mark Context accessors
90 BNetworkCookieJar&
91 BUrlContext::GetCookieJar()
93 return fCookieJar;
97 BHttpAuthentication&
98 BUrlContext::GetAuthentication(const BUrl& url)
100 BString domain = url.Host();
101 domain += url.Path();
103 BHttpAuthentication* authentication = NULL;
105 do {
106 authentication = fAuthenticationMap->Get( HashString(domain.String(),
107 domain.Length()));
109 domain.Truncate(domain.FindLast('/'));
111 } while (authentication == NULL);
113 return *authentication;
117 bool
118 BUrlContext::UseProxy()
120 return !fProxyHost.IsEmpty();
124 BString
125 BUrlContext::GetProxyHost()
127 return fProxyHost;
131 uint16
132 BUrlContext::GetProxyPort()
134 return fProxyPort;