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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set ts=4 sw=4 sts=4 et: */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Malcolm Smith <malsmith@cs.rmit.edu.au>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsProtocolProxyService.h"
#include "nsProxyInfo.h"
#include "nsIServiceManager.h"
#include "nsXPIDLString.h"
#include "nsIProxyAutoConfig.h"
#include "nsAutoLock.h"
#include "nsEventQueueUtils.h"
#include "nsIIOService.h"
#include "nsIObserverService.h"
#include "nsIProtocolHandler.h"
#include "nsIProtocolProxyCallback.h"
#include "nsICancelable.h"
#include "nsIDNSService.h"
#include "nsIPrefService.h"
#include "nsIPrefBranch2.h"
#include "nsReadableUtils.h"
#include "nsString.h"
#include "nsNetUtil.h"
#include "nsCRT.h"
#include "prnetdb.h"
#include "nsEventQueueUtils.h"
#include "nsPACMan.h"
//----------------------------------------------------------------------------
#include "prlog.h"
#if defined(PR_LOGGING)
static PRLogModuleInfo *sLog = PR_NewLogModule("proxy");
#endif
#define LOG(args) PR_LOG(sLog, PR_LOG_DEBUG, args)
//----------------------------------------------------------------------------
// This structure is intended to be allocated on the stack
struct nsProtocolInfo {
nsCAutoString scheme;
PRUint32 flags;
PRInt32 defaultPort;
};
//----------------------------------------------------------------------------
class nsAsyncResolveRequest : public PLEvent
, public nsPACManCallback
, public nsICancelable
{
public:
NS_DECL_ISUPPORTS
nsAsyncResolveRequest(nsProtocolProxyService *pps, nsIURI *uri,
nsIProtocolProxyCallback *callback)
: mStatus(NS_OK)
, mDispatched(PR_FALSE)
, mPPS(pps)
, mURI(uri)
, mCallback(callback)
{
NS_ASSERTION(mCallback, "null callback");
PL_InitEvent(this, nsnull, HandleEvent, CleanupEvent);
}
void SetResult(nsresult status, nsIProxyInfo *pi)
{
mStatus = status;
mProxyInfo = pi;
}
NS_IMETHOD Cancel(nsresult reason)
{
NS_ENSURE_ARG(NS_FAILED(reason));
// If we've already called DoCallback then, nothing more to do.
if (!mCallback)
return NS_OK;
SetResult(reason, nsnull);
return DispatchCallback();
}
nsresult DispatchCallback()
{
if (mDispatched) // Only need to dispatch once
return NS_OK;
nsCOMPtr<nsIEventQueue> eventQ;
nsresult rv = NS_GetCurrentEventQ(getter_AddRefs(eventQ));
if (NS_FAILED(rv))
NS_WARNING("could not get current event queue");
else {
NS_ADDREF_THIS();
rv = eventQ->PostEvent(this);
if (NS_FAILED(rv)) {
NS_WARNING("unable to dispatch callback event");
PL_DestroyEvent(this);
}
else {
mDispatched = PR_TRUE;
return NS_OK;
}
}
mCallback = nsnull; // break possible reference cycle
return rv;
}
private:
// Called asynchronously, so we do not need to post another PLEvent
// before calling DoCallback.
void OnQueryComplete(nsresult status, const nsCString &pacString)
{
// If we've already called DoCallback then, nothing more to do.
if (!mCallback)
return;
// Provided we haven't been canceled...
if (mStatus == NS_OK) {
mStatus = status;
mPACString = pacString;
}
// In the cancelation case, we may still have another PLEvent in
// the queue that wants to call DoCallback. No need to wait for
// it, just run the callback now.
DoCallback();
}
void DoCallback()
{
// Generate proxy info from the PAC string if appropriate
if (NS_SUCCEEDED(mStatus) && !mProxyInfo && !mPACString.IsEmpty())
mPPS->ProcessPACString(mPACString, getter_AddRefs(mProxyInfo));
// Now apply proxy filters
if (NS_SUCCEEDED(mStatus)) {
nsProtocolInfo info;
mStatus = mPPS->GetProtocolInfo(mURI, &info);
if (NS_SUCCEEDED(mStatus))
mPPS->ApplyFilters(mURI, info, mProxyInfo);
else
mProxyInfo = nsnull;
}
mCallback->OnProxyAvailable(this, mURI, mProxyInfo, mStatus);
mCallback = nsnull; // in case the callback holds an owning ref to us
}
PR_STATIC_CALLBACK(void *) HandleEvent(PLEvent *ev)
{
nsAsyncResolveRequest *self =
NS_STATIC_CAST(nsAsyncResolveRequest *, ev);
if (self->mCallback)
self->DoCallback();
return nsnull;
}
PR_STATIC_CALLBACK(void) CleanupEvent(PLEvent *ev)
{
nsAsyncResolveRequest *self =
NS_STATIC_CAST(nsAsyncResolveRequest *, ev);
NS_RELEASE(self); // balance AddRef in DispatchCallback
}
private:
nsresult mStatus;
nsCString mPACString;
PRBool mDispatched;
nsRefPtr<nsProtocolProxyService> mPPS;
nsCOMPtr<nsIURI> mURI;
nsCOMPtr<nsIProtocolProxyCallback> mCallback;
nsCOMPtr<nsIProxyInfo> mProxyInfo;
};
NS_IMPL_ISUPPORTS1(nsAsyncResolveRequest, nsICancelable)
//----------------------------------------------------------------------------
#define IS_ASCII_SPACE(_c) ((_c) == ' ' || (_c) == '\t')
//
// apply mask to address (zeros out excluded bits).
//
// NOTE: we do the byte swapping here to minimize overall swapping.
//
static void
proxy_MaskIPv6Addr(PRIPv6Addr &addr, PRUint16 mask_len)
{
if (mask_len == 128)
return;
if (mask_len > 96) {
addr.pr_s6_addr32[3] = PR_htonl(
PR_ntohl(addr.pr_s6_addr32[3]) & (~0L << (128 - mask_len)));
}
else if (mask_len > 64) {
addr.pr_s6_addr32[3] = 0;
addr.pr_s6_addr32[2] = PR_htonl(
PR_ntohl(addr.pr_s6_addr32[2]) & (~0L << (96 - mask_len)));
}
else if (mask_len > 32) {
addr.pr_s6_addr32[3] = 0;
addr.pr_s6_addr32[2] = 0;
addr.pr_s6_addr32[1] = PR_htonl(
PR_ntohl(addr.pr_s6_addr32[1]) & (~0L << (64 - mask_len)));
}
else {
addr.pr_s6_addr32[3] = 0;
addr.pr_s6_addr32[2] = 0;
addr.pr_s6_addr32[1] = 0;
addr.pr_s6_addr32[0] = PR_htonl(
PR_ntohl(addr.pr_s6_addr32[0]) & (~0L << (32 - mask_len)));
}
}
static void
proxy_GetStringPref(nsIPrefBranch *aPrefBranch,
const char *aPref,
nsCString &aResult)
{
nsXPIDLCString temp;
nsresult rv = aPrefBranch->GetCharPref(aPref, getter_Copies(temp));
if (NS_FAILED(rv))
aResult.Truncate();
else {
aResult.Assign(temp);
// all of our string prefs are hostnames, so we should remove any
// whitespace characters that the user might have unknowingly entered.
aResult.StripWhitespace();
}
}
static void
proxy_GetIntPref(nsIPrefBranch *aPrefBranch,
const char *aPref,
PRInt32 &aResult)
{
PRInt32 temp;
nsresult rv = aPrefBranch->GetIntPref(aPref, &temp);
if (NS_FAILED(rv))
aResult = -1;
else
aResult = temp;
}
static void
proxy_GetBoolPref(nsIPrefBranch *aPrefBranch,
const char *aPref,
PRBool &aResult)
{
PRBool temp;
nsresult rv = aPrefBranch->GetBoolPref(aPref, &temp);
if (NS_FAILED(rv))
aResult = PR_FALSE;
else
aResult = temp;
}
//----------------------------------------------------------------------------
NS_IMPL_ISUPPORTS3(nsProtocolProxyService,
nsIProtocolProxyService,
nsPIProtocolProxyService,
nsIObserver)
nsProtocolProxyService::nsProtocolProxyService()
: mFilters(nsnull)
, mProxyConfig(eProxyConfig_Direct)
, mHTTPProxyPort(-1)
, mFTPProxyPort(-1)
, mGopherProxyPort(-1)
, mHTTPSProxyPort(-1)
, mSOCKSProxyPort(-1)
, mSOCKSProxyVersion(4)
, mSOCKSProxyRemoteDNS(PR_FALSE)
, mPACMan(nsnull)
, mSessionStart(PR_Now())
, mFailedProxyTimeout(30 * 60) // 30 minute default
{
}
nsProtocolProxyService::~nsProtocolProxyService()
{
// These should have been cleaned up in our Observe method.
NS_ASSERTION(mHostFiltersArray.Count() == 0 && mFilters == nsnull &&
mPACMan == nsnull, "what happened to xpcom-shutdown?");
}
// nsProtocolProxyService methods
nsresult
nsProtocolProxyService::Init()
{
if (!mFailedProxies.Init())
return NS_ERROR_OUT_OF_MEMORY;
// failure to access prefs is non-fatal
nsCOMPtr<nsIPrefBranch2> prefBranch =
do_GetService(NS_PREFSERVICE_CONTRACTID);
if (prefBranch) {
// monitor proxy prefs
prefBranch->AddObserver("network.proxy", this, PR_FALSE);
// read all prefs
PrefsChanged(prefBranch, nsnull);
}
// register for shutdown notification so we can clean ourselves up properly.
nsCOMPtr<nsIObserverService> obs =
do_GetService("@mozilla.org/observer-service;1");
if (obs)
obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
return NS_OK;
}
NS_IMETHODIMP
nsProtocolProxyService::Observe(nsISupports *aSubject,
const char *aTopic,
const PRUnichar *aData)
{
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
// cleanup
if (mHostFiltersArray.Count() > 0) {
mHostFiltersArray.EnumerateForwards(CleanupFilterArray, nsnull);
mHostFiltersArray.Clear();
}
if (mFilters) {
delete mFilters;
mFilters = nsnull;
}
if (mPACMan) {
mPACMan->Shutdown();
mPACMan = nsnull;
}
}
else {
NS_ASSERTION(strcmp(aTopic, NS_PREFBRANCH_PREFCHANGE_TOPIC_ID) == 0,
"what is this random observer event?");
nsCOMPtr<nsIPrefBranch> prefs = do_QueryInterface(aSubject);
if (prefs)
PrefsChanged(prefs, NS_LossyConvertUTF16toASCII(aData).get());
}
return NS_OK;
}
void
nsProtocolProxyService::PrefsChanged(nsIPrefBranch *prefBranch,
const char *pref)
{
nsresult rv = NS_OK;
PRBool reloadPAC = PR_FALSE;
nsXPIDLCString tempString;
if (!pref || !strcmp(pref, "network.proxy.type")) {
PRInt32 type = -1;
rv = prefBranch->GetIntPref("network.proxy.type",&type);
if (NS_SUCCEEDED(rv)) {
// bug 115720 - for ns4.x backwards compatability
if (type == eProxyConfig_Direct4x) {
type = eProxyConfig_Direct;
// Reset the type so that the dialog looks correct, and we
// don't have to handle this case everywhere else
// I'm paranoid about a loop of some sort - only do this
// if we're enumerating all prefs, and ignore any error
if (!pref)
prefBranch->SetIntPref("network.proxy.type", type);
} else if (type >= eProxyConfig_Last) {
LOG(("unknown proxy type: %lu; assuming direct\n", type));
type = eProxyConfig_Direct;
}
mProxyConfig = NS_STATIC_CAST(ProxyConfig, type);
reloadPAC = PR_TRUE;
}
}
if (!pref || !strcmp(pref, "network.proxy.http"))
proxy_GetStringPref(prefBranch, "network.proxy.http", mHTTPProxyHost);
if (!pref || !strcmp(pref, "network.proxy.http_port"))
proxy_GetIntPref(prefBranch, "network.proxy.http_port", mHTTPProxyPort);
if (!pref || !strcmp(pref, "network.proxy.ssl"))
proxy_GetStringPref(prefBranch, "network.proxy.ssl", mHTTPSProxyHost);
if (!pref || !strcmp(pref, "network.proxy.ssl_port"))
proxy_GetIntPref(prefBranch, "network.proxy.ssl_port", mHTTPSProxyPort);
if (!pref || !strcmp(pref, "network.proxy.ftp"))
proxy_GetStringPref(prefBranch, "network.proxy.ftp", mFTPProxyHost);
if (!pref || !strcmp(pref, "network.proxy.ftp_port"))
proxy_GetIntPref(prefBranch, "network.proxy.ftp_port", mFTPProxyPort);
if (!pref || !strcmp(pref, "network.proxy.gopher"))
proxy_GetStringPref(prefBranch, "network.proxy.gopher", mGopherProxyHost);
if (!pref || !strcmp(pref, "network.proxy.gopher_port"))
proxy_GetIntPref(prefBranch, "network.proxy.gopher_port", mGopherProxyPort);
if (!pref || !strcmp(pref, "network.proxy.socks"))
proxy_GetStringPref(prefBranch, "network.proxy.socks", mSOCKSProxyHost);
if (!pref || !strcmp(pref, "network.proxy.socks_port"))
proxy_GetIntPref(prefBranch, "network.proxy.socks_port", mSOCKSProxyPort);
if (!pref || !strcmp(pref, "network.proxy.socks_version")) {
PRInt32 version;
proxy_GetIntPref(prefBranch, "network.proxy.socks_version", version);
// make sure this preference value remains sane
if (version == 5)
mSOCKSProxyVersion = 5;
else
mSOCKSProxyVersion = 4;
}
if (!pref || !strcmp(pref, "network.proxy.socks_remote_dns"))
proxy_GetBoolPref(prefBranch, "network.proxy.socks_remote_dns", mSOCKSProxyRemoteDNS);
if (!pref || !strcmp(pref, "network.proxy.failover_timeout"))
proxy_GetIntPref(prefBranch, "network.proxy.failover_timeout", mFailedProxyTimeout);
if (!pref || !strcmp(pref, "network.proxy.no_proxies_on")) {
rv = prefBranch->GetCharPref("network.proxy.no_proxies_on",
getter_Copies(tempString));
if (NS_SUCCEEDED(rv))
LoadHostFilters(tempString.get());
}
// We're done if not using PAC or WPAD
if (mProxyConfig != eProxyConfig_PAC && mProxyConfig != eProxyConfig_WPAD)
return;
// OK, we need to reload the PAC file if:
// 1) network.proxy.type changed, or
// 2) network.proxy.autoconfig_url changed and PAC is configured
if (!pref || !strcmp(pref, "network.proxy.autoconfig_url"))
reloadPAC = PR_TRUE;
if (reloadPAC) {
tempString.Truncate();
if (mProxyConfig == eProxyConfig_PAC) {
prefBranch->GetCharPref("network.proxy.autoconfig_url",
getter_Copies(tempString));
}
else if (mProxyConfig == eProxyConfig_WPAD) {
// We diverge from the WPAD spec here in that we don't walk the
// hosts's FQDN, stripping components until we hit a TLD. Doing so
// is dangerous in the face of an incomplete list of TLDs, and TLDs
// get added over time. We could consider doing only a single
// substitution of the first component, if that proves to help
// compatibility.
tempString.AssignLiteral("http://wpad/wpad.dat");
}
ConfigureFromPAC(tempString);
}
}
PRBool
nsProtocolProxyService::CanUseProxy(nsIURI *aURI, PRInt32 defaultPort)
{
if (mHostFiltersArray.Count() == 0)
return PR_TRUE;
PRInt32 port;
nsCAutoString host;
nsresult rv = aURI->GetAsciiHost(host);
if (NS_FAILED(rv) || host.IsEmpty())
return PR_FALSE;
rv = aURI->GetPort(&port);
if (NS_FAILED(rv))
return PR_FALSE;
if (port == -1)
port = defaultPort;
PRNetAddr addr;
PRBool is_ipaddr = (PR_StringToNetAddr(host.get(), &addr) == PR_SUCCESS);
PRIPv6Addr ipv6;
if (is_ipaddr) {
// convert parsed address to IPv6
if (addr.raw.family == PR_AF_INET) {
// convert to IPv4-mapped address
PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &ipv6);
}
else if (addr.raw.family == PR_AF_INET6) {
// copy the address
memcpy(&ipv6, &addr.ipv6.ip, sizeof(PRIPv6Addr));
}
else {
NS_WARNING("unknown address family");
return PR_TRUE; // allow proxying
}
}
PRInt32 index = -1;
while (++index < mHostFiltersArray.Count()) {
HostInfo *hinfo = (HostInfo *) mHostFiltersArray[index];
if (is_ipaddr != hinfo->is_ipaddr)
continue;
if (hinfo->port && hinfo->port != port)
continue;
if (is_ipaddr) {
// generate masked version of target IPv6 address
PRIPv6Addr masked;
memcpy(&masked, &ipv6, sizeof(PRIPv6Addr));
proxy_MaskIPv6Addr(masked, hinfo->ip.mask_len);
// check for a match
if (memcmp(&masked, &hinfo->ip.addr, sizeof(PRIPv6Addr)) == 0)
return PR_FALSE; // proxy disallowed
}
else {
PRUint32 host_len = host.Length();
PRUint32 filter_host_len = hinfo->name.host_len;
if (host_len >= filter_host_len) {
//
// compare last |filter_host_len| bytes of target hostname.
//
const char *host_tail = host.get() + host_len - filter_host_len;
if (!PL_strncasecmp(host_tail, hinfo->name.host, filter_host_len))
return PR_FALSE; // proxy disallowed
}
}
}
return PR_TRUE;
}
static const char kProxyType_HTTP[] = "http";
static const char kProxyType_PROXY[] = "proxy";
static const char kProxyType_SOCKS[] = "socks";
static const char kProxyType_SOCKS4[] = "socks4";
static const char kProxyType_SOCKS5[] = "socks5";
static const char kProxyType_DIRECT[] = "direct";
static const char kProxyType_UNKNOWN[] = "unknown";
const char *
nsProtocolProxyService::ExtractProxyInfo(const char *start, nsProxyInfo **result)
{
*result = nsnull;
PRUint32 flags = 0;
// see BNF in nsIProxyAutoConfig.idl
// find end of proxy info delimiter
const char *end = start;
while (*end && *end != ';') ++end;
// find end of proxy type delimiter
const char *sp = start;
while (sp < end && *sp != ' ' && *sp != '\t') ++sp;
PRUint32 len = sp - start;
const char *type = nsnull;
switch (len) {
case 5:
if (PL_strncasecmp(start, kProxyType_PROXY, 5) == 0)
type = kProxyType_HTTP;
else if (PL_strncasecmp(start, kProxyType_SOCKS, 5) == 0)
type = kProxyType_SOCKS4; // assume v4 for 4x compat
break;
case 6:
if (PL_strncasecmp(start, kProxyType_DIRECT, 6) == 0)
type = kProxyType_DIRECT;
else if (PL_strncasecmp(start, kProxyType_SOCKS4, 6) == 0)
type = kProxyType_SOCKS4;
else if (PL_strncasecmp(start, kProxyType_SOCKS5, 6) == 0)
// map "SOCKS5" to "socks" to match contract-id of registered
// SOCKS-v5 socket provider.
type = kProxyType_SOCKS;
break;
}
if (type) {
const char *host = nsnull, *hostEnd = nsnull;
PRInt32 port = -1;
// If it's a SOCKS5 proxy, do name resolution on the server side.
// We could use this with SOCKS4a servers too, but they might not
// support it.
if (type == kProxyType_SOCKS)
flags |= nsIProxyInfo::TRANSPARENT_PROXY_RESOLVES_HOST;
// extract host:port
start = sp;
while ((*start == ' ' || *start == '\t') && start < end)
start++;
if (start < end) {
host = start;
hostEnd = strchr(host, ':');
if (!hostEnd || hostEnd > end) {
hostEnd = end;
// no port, so assume default
if (type == kProxyType_HTTP)
port = 80;
else
port = 1080;
}
else
port = atoi(hostEnd + 1);
}
nsProxyInfo *pi = new nsProxyInfo;
if (pi) {
pi->mType = type;
pi->mFlags = flags;
pi->mTimeout = mFailedProxyTimeout;
// YES, it is ok to specify a null proxy host.
if (host) {
pi->mHost.Assign(host, hostEnd - host);
pi->mPort = port;
}
NS_ADDREF(*result = pi);
}
}
while (*end == ';' || *end == ' ' || *end == '\t')
++end;
return end;
}
void
nsProtocolProxyService::GetProxyKey(nsProxyInfo *pi, nsCString &key)
{
key.AssignASCII(pi->mType);
if (!pi->mHost.IsEmpty()) {
key.Append(' ');
key.Append(pi->mHost);
key.Append(':');
key.AppendInt(pi->mPort);
}
}
PRUint32
nsProtocolProxyService::SecondsSinceSessionStart()
{
PRTime now = PR_Now();
// get time elapsed since session start
PRInt64 diff;
LL_SUB(diff, now, mSessionStart);
// convert microseconds to seconds
PRTime ups;
LL_I2L(ups, PR_USEC_PER_SEC);
LL_DIV(diff, diff, ups);
// convert to 32 bit value
PRUint32 dsec;
LL_L2UI(dsec, diff);
return dsec;
}
void
nsProtocolProxyService::EnableProxy(nsProxyInfo *pi)
{
nsCAutoString key;
GetProxyKey(pi, key);
mFailedProxies.Remove(key);
}
void
nsProtocolProxyService::DisableProxy(nsProxyInfo *pi)
{
nsCAutoString key;
GetProxyKey(pi, key);
PRUint32 dsec = SecondsSinceSessionStart();
// Add timeout to interval (this is the time when the proxy can
// be tried again).
dsec += pi->mTimeout;
// NOTE: The classic codebase would increase the timeout value
// incrementally each time a subsequent failure occured.
// We could do the same, but it would require that we not
// remove proxy entries in IsProxyDisabled or otherwise
// change the way we are recording disabled proxies.
// Simpler is probably better for now, and at least the
// user can tune the timeout setting via preferences.
LOG(("DisableProxy %s %d\n", key.get(), dsec));
// If this fails, oh well... means we don't have enough memory
// to remember the failed proxy.
mFailedProxies.Put(key, dsec);
}
PRBool
nsProtocolProxyService::IsProxyDisabled(nsProxyInfo *pi)
{
nsCAutoString key;
GetProxyKey(pi, key);
PRUint32 val;
if (!mFailedProxies.Get(key, &val))
return PR_FALSE;
PRUint32 dsec = SecondsSinceSessionStart();
// if time passed has exceeded interval, then try proxy again.
if (dsec > val) {
mFailedProxies.Remove(key);
return PR_FALSE;
}
return PR_TRUE;
}
void
nsProtocolProxyService::ProcessPACString(const nsCString &pacString,
nsIProxyInfo **result)
{
if (pacString.IsEmpty()) {
*result = nsnull;
return;
}
const char *proxies = pacString.get();
nsProxyInfo *pi = nsnull, *first = nsnull, *last = nsnull;
while (*proxies) {
proxies = ExtractProxyInfo(proxies, &pi);
if (pi) {
if (last) {
NS_ASSERTION(last->mNext == nsnull, "leaking nsProxyInfo");
last->mNext = pi;
}
else
first = pi;
last = pi;
}
}
*result = first;
}
// nsIProtocolProxyService
NS_IMETHODIMP
nsProtocolProxyService::Resolve(nsIURI *uri, PRUint32 flags,
nsIProxyInfo **result)
{
nsProtocolInfo info;
nsresult rv = GetProtocolInfo(uri, &info);
if (NS_FAILED(rv))
return rv;
PRBool usePAC;
rv = Resolve_Internal(uri, info, &usePAC, result);
if (NS_FAILED(rv))
return rv;
if (usePAC && mPACMan) {
NS_ASSERTION(*result == nsnull, "we should not have a result yet");
// If the caller didn't want us to invoke PAC, then error out.
if (flags & RESOLVE_NON_BLOCKING)
return NS_BASE_STREAM_WOULD_BLOCK;
// Query the PAC file synchronously.
nsCString pacString;
rv = mPACMan->GetProxyForURI(uri, pacString);
if (NS_SUCCEEDED(rv))
ProcessPACString(pacString, result);
else if (rv == NS_ERROR_IN_PROGRESS) {
// Construct a special UNKNOWN proxy entry that informs the caller
// that the proxy info is yet to be determined.
rv = NewProxyInfo_Internal(kProxyType_UNKNOWN, EmptyCString(), -1,
0, 0, nsnull, result);
if (NS_FAILED(rv))
return rv;
}
else
NS_WARNING("failed querying PAC file; trying DIRECT");
}
ApplyFilters(uri, info, result);
return NS_OK;
}
NS_IMETHODIMP
nsProtocolProxyService::AsyncResolve(nsIURI *uri, PRUint32 flags,
nsIProtocolProxyCallback *callback,
nsICancelable **result)
{
nsRefPtr<nsAsyncResolveRequest> ctx =
new nsAsyncResolveRequest(this, uri, callback);
if (!ctx)
return NS_ERROR_OUT_OF_MEMORY;
nsProtocolInfo info;
nsresult rv = GetProtocolInfo(uri, &info);
if (NS_FAILED(rv))
return rv;
PRBool usePAC;
nsCOMPtr<nsIProxyInfo> pi;
rv = Resolve_Internal(uri, info, &usePAC, getter_AddRefs(pi));
if (NS_FAILED(rv))
return rv;
if (!usePAC || !mPACMan) {
ApplyFilters(uri, info, pi);
ctx->SetResult(NS_OK, pi);
return ctx->DispatchCallback();
}
// else kick off a PAC query
rv = mPACMan->AsyncGetProxyForURI(uri, ctx);
if (NS_SUCCEEDED(rv)) {
*result = ctx;
NS_ADDREF(*result);
}
return rv;
}
NS_IMETHODIMP
nsProtocolProxyService::NewProxyInfo(const nsACString &aType,
const nsACString &aHost,
PRInt32 aPort,
PRUint32 aFlags,
PRUint32 aFailoverTimeout,
nsIProxyInfo *aFailoverProxy,
nsIProxyInfo **aResult)
{
static const char *types[] = {
kProxyType_HTTP,
kProxyType_SOCKS,
kProxyType_SOCKS4,
kProxyType_DIRECT
};
// resolve type; this allows us to avoid copying the type string into each
// proxy info instance. we just reference the string literals directly :)
const char *type = nsnull;
for (PRUint32 i=0; i<NS_ARRAY_LENGTH(types); ++i) {
if (aType.LowerCaseEqualsASCII(types[i])) {
type = types[i];
break;
}
}
NS_ENSURE_TRUE(type, NS_ERROR_INVALID_ARG);
if (aPort <= 0)
aPort = -1;
return NewProxyInfo_Internal(type, aHost, aPort, aFlags, aFailoverTimeout,
aFailoverProxy, aResult);
}
NS_IMETHODIMP
nsProtocolProxyService::ConfigureFromPAC(const nsACString &spec)
{
if (!mPACMan) {
mPACMan = new nsPACMan();
if (!mPACMan)
return NS_ERROR_OUT_OF_MEMORY;
}
mFailedProxies.Clear();
nsCOMPtr<nsIURI> pacURI;
nsresult rv = NS_NewURI(getter_AddRefs(pacURI), spec);
if (NS_FAILED(rv))
return rv;
return mPACMan->LoadPACFromURI(pacURI);
}
NS_IMETHODIMP
nsProtocolProxyService::GetFailoverForProxy(nsIProxyInfo *aProxy,
nsIURI *aURI,
nsresult aStatus,
nsIProxyInfo **aResult)
{
// We only support failover when a PAC file is configured.
if (mProxyConfig != eProxyConfig_PAC && mProxyConfig != eProxyConfig_WPAD)
return NS_ERROR_NOT_AVAILABLE;
// Verify that |aProxy| is one of our nsProxyInfo objects.
nsCOMPtr<nsProxyInfo> pi = do_QueryInterface(aProxy);
NS_ENSURE_ARG(pi);
// OK, the QI checked out. We can proceed.
// Remember that this proxy is down.
DisableProxy(pi);
// NOTE: At this point, we might want to prompt the user if we have
// not already tried going DIRECT. This is something that the
// classic codebase supported; however, IE6 does not prompt.
if (!pi->mNext)
return NS_ERROR_NOT_AVAILABLE;
LOG(("PAC failover from %s %s:%d to %s %s:%d\n",
pi->mType, pi->mHost.get(), pi->mPort,
pi->mNext->mType, pi->mNext->mHost.get(), pi->mNext->mPort));
NS_ADDREF(*aResult = pi->mNext);
return NS_OK;
}
NS_IMETHODIMP
nsProtocolProxyService::RegisterFilter(nsIProtocolProxyFilter *filter,
PRUint32 position)
{
UnregisterFilter(filter); // remove this filter if we already have it
FilterLink *link = new FilterLink(position, filter);
if (!link)
return NS_ERROR_OUT_OF_MEMORY;
if (!mFilters) {
mFilters = link;
return NS_OK;
}
// insert into mFilters in sorted order
FilterLink *last = nsnull;
for (FilterLink *iter = mFilters; iter; iter = iter->next) {
if (position < iter->position) {
if (last) {
link->next = last->next;
last->next = link;
} else {
link->next = mFilters;
mFilters = link;
}
return NS_OK;
}
last = iter;
}
// our position is equal to or greater than the last link in the list
last->next = link;
return NS_OK;
}
NS_IMETHODIMP
nsProtocolProxyService::UnregisterFilter(nsIProtocolProxyFilter *filter)
{
// QI to nsISupports so we can safely test object identity.
nsCOMPtr<nsISupports> givenObject = do_QueryInterface(filter);
FilterLink *last = nsnull;
for (FilterLink *iter = mFilters; iter; iter = iter->next) {
nsCOMPtr<nsISupports> object = do_QueryInterface(iter->filter);
if (object == givenObject) {
if (last)
last->next = iter->next;
else
mFilters = iter->next;
iter->next = nsnull;
delete iter;
return NS_OK;
}
last = iter;
}
// No need to throw an exception in this case.
return NS_OK;
}
PRBool PR_CALLBACK
nsProtocolProxyService::CleanupFilterArray(void *aElement, void *aData)
{
if (aElement)
delete (HostInfo *) aElement;
return PR_TRUE;
}
void
nsProtocolProxyService::LoadHostFilters(const char *filters)
{
// check to see the owners flag? /!?/ TODO
if (mHostFiltersArray.Count() > 0) {
mHostFiltersArray.EnumerateForwards(CleanupFilterArray, nsnull);
mHostFiltersArray.Clear();
}
if (!filters)
return; // fail silently...
//
// filter = ( host | domain | ipaddr ["/" mask] ) [":" port]
// filters = filter *( "," LWS filter)
//
while (*filters) {
// skip over spaces and ,
while (*filters && (*filters == ',' || IS_ASCII_SPACE(*filters)))
filters++;
const char *starthost = filters;
const char *endhost = filters + 1; // at least that...
const char *portLocation = 0;
const char *maskLocation = 0;
//
// XXX this needs to be fixed to support IPv6 address literals,
// which in this context will need to be []-escaped.
//
while (*endhost && (*endhost != ',' && !IS_ASCII_SPACE(*endhost))) {
if (*endhost == ':')
portLocation = endhost;
else if (*endhost == '/')
maskLocation = endhost;
endhost++;
}
filters = endhost; // advance iterator up front
HostInfo *hinfo = new HostInfo();
if (!hinfo)
return; // fail silently
hinfo->port = portLocation ? atoi(portLocation + 1) : 0;
// locate end of host
const char *end = maskLocation ? maskLocation :
portLocation ? portLocation :
endhost;
nsCAutoString str(starthost, end - starthost);
PRNetAddr addr;
if (PR_StringToNetAddr(str.get(), &addr) == PR_SUCCESS) {
hinfo->is_ipaddr = PR_TRUE;
hinfo->ip.family = PR_AF_INET6; // we always store address as IPv6
hinfo->ip.mask_len = maskLocation ? atoi(maskLocation + 1) : 128;
if (hinfo->ip.mask_len == 0) {
NS_WARNING("invalid mask");
goto loser;
}
if (addr.raw.family == PR_AF_INET) {
// convert to IPv4-mapped address
PR_ConvertIPv4AddrToIPv6(addr.inet.ip, &hinfo->ip.addr);
// adjust mask_len accordingly
if (hinfo->ip.mask_len <= 32)
hinfo->ip.mask_len += 96;
}
else if (addr.raw.family == PR_AF_INET6) {
// copy the address
memcpy(&hinfo->ip.addr, &addr.ipv6.ip, sizeof(PRIPv6Addr));
}
else {
NS_WARNING("unknown address family");
goto loser;
}
// apply mask to IPv6 address
proxy_MaskIPv6Addr(hinfo->ip.addr, hinfo->ip.mask_len);
}
else {
PRUint32 startIndex, endIndex;
if (str.First() == '*')
startIndex = 1; // *.domain -> .domain
else
startIndex = 0;
endIndex = (portLocation ? portLocation : endhost) - starthost;
hinfo->is_ipaddr = PR_FALSE;
hinfo->name.host = ToNewCString(Substring(str, startIndex, endIndex));
if (!hinfo->name.host)
goto loser;
hinfo->name.host_len = endIndex - startIndex;
}
//#define DEBUG_DUMP_FILTERS
#ifdef DEBUG_DUMP_FILTERS
printf("loaded filter[%u]:\n", mHostFiltersArray.Count());
printf(" is_ipaddr = %u\n", hinfo->is_ipaddr);
printf(" port = %u\n", hinfo->port);
if (hinfo->is_ipaddr) {
printf(" ip.family = %x\n", hinfo->ip.family);
printf(" ip.mask_len = %u\n", hinfo->ip.mask_len);
PRNetAddr netAddr;
PR_SetNetAddr(PR_IpAddrNull, PR_AF_INET6, 0, &netAddr);
memcpy(&netAddr.ipv6.ip, &hinfo->ip.addr, sizeof(hinfo->ip.addr));
char buf[256];
PR_NetAddrToString(&netAddr, buf, sizeof(buf));
printf(" ip.addr = %s\n", buf);
}
else {
printf(" name.host = %s\n", hinfo->name.host);
}
#endif
mHostFiltersArray.AppendElement(hinfo);
hinfo = nsnull;
loser:
if (hinfo)
delete hinfo;
}
}
nsresult
nsProtocolProxyService::GetProtocolInfo(nsIURI *uri, nsProtocolInfo *info)
{
nsresult rv;
rv = uri->GetScheme(info->scheme);
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIProtocolHandler> handler;
rv = ios->GetProtocolHandler(info->scheme.get(), getter_AddRefs(handler));
if (NS_FAILED(rv))
return rv;
rv = handler->GetProtocolFlags(&info->flags);
if (NS_FAILED(rv))
return rv;
rv = handler->GetDefaultPort(&info->defaultPort);
return rv;
}
nsresult
nsProtocolProxyService::NewProxyInfo_Internal(const char *aType,
const nsACString &aHost,
PRInt32 aPort,
PRUint32 aFlags,
PRUint32 aFailoverTimeout,
nsIProxyInfo *aFailoverProxy,
nsIProxyInfo **aResult)
{
nsCOMPtr<nsProxyInfo> failover;
if (aFailoverProxy) {
failover = do_QueryInterface(aFailoverProxy);
NS_ENSURE_ARG(failover);
}
nsProxyInfo *proxyInfo = new nsProxyInfo();
if (!proxyInfo)
return NS_ERROR_OUT_OF_MEMORY;
proxyInfo->mType = aType;
proxyInfo->mHost = aHost;
proxyInfo->mPort = aPort;
proxyInfo->mFlags = aFlags;
proxyInfo->mTimeout = aFailoverTimeout == PR_UINT32_MAX
? mFailedProxyTimeout : aFailoverTimeout;
failover.swap(proxyInfo->mNext);
NS_ADDREF(*aResult = proxyInfo);
return NS_OK;
}
nsresult
nsProtocolProxyService::Resolve_Internal(nsIURI *uri,
const nsProtocolInfo &info,
PRBool *usePAC,
nsIProxyInfo **result)
{
NS_ENSURE_ARG_POINTER(uri);
*usePAC = PR_FALSE;
*result = nsnull;
if (!(info.flags & nsIProtocolHandler::ALLOWS_PROXY))
return NS_OK; // Can't proxy this (filters may not override)
// if proxies are enabled and this host:port combo is supposed to use a
// proxy, check for a proxy.
if (mProxyConfig == eProxyConfig_Direct ||
(mProxyConfig == eProxyConfig_Manual &&
!CanUseProxy(uri, info.defaultPort)))
return NS_OK;
// Proxy auto config magic...
if (mProxyConfig == eProxyConfig_PAC || mProxyConfig == eProxyConfig_WPAD) {
// Do not query PAC now.
*usePAC = PR_TRUE;
return NS_OK;
}
// proxy info values
const char *type = nsnull;
const nsACString *host = nsnull;
PRInt32 port = -1;
PRUint32 proxyFlags = 0;
if (!mHTTPProxyHost.IsEmpty() && mHTTPProxyPort > 0 &&
info.scheme.EqualsLiteral("http")) {
host = &mHTTPProxyHost;
type = kProxyType_HTTP;
port = mHTTPProxyPort;
}
else if (!mHTTPSProxyHost.IsEmpty() && mHTTPSProxyPort > 0 &&
info.scheme.EqualsLiteral("https")) {
host = &mHTTPSProxyHost;
type = kProxyType_HTTP;
port = mHTTPSProxyPort;
}
else if (!mFTPProxyHost.IsEmpty() && mFTPProxyPort > 0 &&
info.scheme.EqualsLiteral("ftp")) {
host = &mFTPProxyHost;
type = kProxyType_HTTP;
port = mFTPProxyPort;
}
else if (!mGopherProxyHost.IsEmpty() && mGopherProxyPort > 0 &&
info.scheme.EqualsLiteral("gopher")) {
host = &mGopherProxyHost;
type = kProxyType_HTTP;
port = mGopherProxyPort;
}
else if (!mSOCKSProxyHost.IsEmpty() && mSOCKSProxyPort > 0) {
host = &mSOCKSProxyHost;
if (mSOCKSProxyVersion == 4)
type = kProxyType_SOCKS4;
else
type = kProxyType_SOCKS;
port = mSOCKSProxyPort;
if (mSOCKSProxyRemoteDNS)
proxyFlags |= nsIProxyInfo::TRANSPARENT_PROXY_RESOLVES_HOST;
}
if (type) {
nsresult rv = NewProxyInfo_Internal(type, *host, port, proxyFlags,
PR_UINT32_MAX, nsnull, result);
if (NS_FAILED(rv))
return rv;
}
return NS_OK;
}
void
nsProtocolProxyService::ApplyFilters(nsIURI *uri, const nsProtocolInfo &info,
nsIProxyInfo **list)
{
if (!(info.flags & nsIProtocolHandler::ALLOWS_PROXY))
return;
// We prune the proxy list prior to invoking each filter. This may be
// somewhat inefficient, but it seems like a good idea since we want each
// filter to "see" a valid proxy list.
nsresult rv;
nsCOMPtr<nsIProxyInfo> result;
for (FilterLink *iter = mFilters; iter; iter = iter->next) {
PruneProxyInfo(info, list);
rv = iter->filter->ApplyFilter(this, uri, *list,
getter_AddRefs(result));
if (NS_FAILED(rv))
continue;
result.swap(*list);
}
PruneProxyInfo(info, list);
}
void
nsProtocolProxyService::PruneProxyInfo(const nsProtocolInfo &info,
nsIProxyInfo **list)
{
if (!*list)
return;
nsProxyInfo *head = nsnull;
CallQueryInterface(*list, &head);
if (!head) {
NS_NOTREACHED("nsIProxyInfo must QI to nsProxyInfo");
return;
}
NS_RELEASE(*list);
// Pruning of disabled proxies works like this:
// - If all proxies are disabled, return the full list
// - Otherwise, remove the disabled proxies.
//
// Pruning of disallowed proxies works like this:
// - If the protocol handler disallows the proxy, then we disallow it.
// Start by removing all disallowed proxies if required:
if (!(info.flags & nsIProtocolHandler::ALLOWS_PROXY_HTTP)) {
nsProxyInfo *last = nsnull, *iter = head;
while (iter) {
if (iter->Type() == kProxyType_HTTP) {
// reject!
if (last)
last->mNext = iter->mNext;
else
head = iter->mNext;
nsProxyInfo *next = iter->mNext;
iter->mNext = nsnull;
iter->Release();
iter = next;
} else {
last = iter;
iter = iter->mNext;
}
}
if (!head)
return;
}
// Now, scan to see if all remaining proxies are disabled. If so, then
// we'll just bail and return them all. Otherwise, we'll go and prune the
// disabled ones.
PRBool allDisabled = PR_TRUE;
nsProxyInfo *iter;
for (iter = head; iter; iter = iter->mNext) {
if (!IsProxyDisabled(iter)) {
allDisabled = PR_FALSE;
break;
}
}
if (allDisabled)
LOG(("All proxies are disabled, so trying all again"));
else {
// remove any disabled proxies.
nsProxyInfo *last = nsnull;
for (iter = head; iter; ) {
if (IsProxyDisabled(iter)) {
// reject!
nsProxyInfo *reject = iter;
iter = iter->mNext;
if (last)
last->mNext = iter;
else
head = iter;
reject->mNext = nsnull;
NS_RELEASE(reject);
continue;
}
// since we are about to use this proxy, make sure it is not on
// the disabled proxy list. we'll add it back to that list if
// we have to (in GetFailoverForProxy).
//
// XXX(darin): It might be better to do this as a final pass.
//
EnableProxy(iter);
last = iter;
iter = iter->mNext;
}
}
// if only DIRECT was specified then return no proxy info, and we're done.
if (head && !head->mNext && head->mType == kProxyType_DIRECT)
NS_RELEASE(head);
*list = head; // Transfer ownership
}
|