Skip to content
Browse files

Use HTTP_PROXY env. var. for MythBrowser and other QHttp. Refs #9525.

Now also supports Mac OS X and windows, though only tested on the former.
Compared to the original patch, this is horrendously complicated,
but given that Qt's proxy support is hidden deep down without debugging,
I think having sanity checking up front is useful.
  • Loading branch information...
1 parent 7d18db0 commit 01f1c9215ffd54e19716ab35101fc6f41f4ddc81 @NigelPearson NigelPearson committed May 10, 2011
View
118 mythtv/libs/libmythbase/util.cpp
@@ -37,6 +37,8 @@ using namespace std;
#include <QFile>
#include <QDir>
#include <QFileInfo>
+#include <QUrl>
+#include <QNetworkProxy>
// Myth headers
#include "mythcorecontext.h"
@@ -1358,4 +1360,120 @@ bool MythRemoveDirectory(QDir &aDir)
return string;
}
+/**
+ * \brief Get network proxy settings from OS, and use for [Q]Http[Comms]
+ *
+ * The HTTP_PROXY environment var. is parsed for values like; "proxy-host",
+ * "proxy-host:8080", "http://host:8080" and "http"//user:password@host:1080",
+ * and that is used for any Qt-based Http fetches.
+ * We also test connectivity here with ping and telnet, and warn if it fails.
+ *
+ * If there is was no env. var, we use Qt to get proxy settings from the OS,
+ * and search through them for a proxy server we can connect to.
+ */
+ MBASE_PUBLIC void setHttpProxy(void)
+{
+ QString LOC = "setHttpProxy() - ";
+ QNetworkProxy p;
+
+
+ // Set http proxy for the application if specified in environment variable
+ QString var(getenv("http_proxy"));
+ if (var.isEmpty())
+ var = getenv("HTTP_PROXY"); // Sadly, some OS envs are case sensitive
+ if (var.length())
+ {
+ if (!var.startsWith("http://")) // i.e. just a host name
+ var.prepend("http://");
+
+ QUrl url = QUrl(var, QUrl::TolerantMode);
+ QString host = url.host();
+ int port = url.port();
+
+ if (port == -1) // Parsing error
+ {
+ port = 0; // The default when creating a QNetworkProxy
+
+ if (telnet(host, 1080)) // Socks?
+ port = 1080;
+ if (telnet(host, 3128)) // Squid
+ port = 3128;
+ if (telnet(host, 8080)) // MS ISA
+ port = 8080;
+
+ VERBOSE(VB_NETWORK, (LOC + "assuming port %1 on host %2")
+ .arg(port).arg(host));
+ url.setPort(port);
+ }
+ else if (!ping(host, 1))
+ VERBOSE(VB_IMPORTANT,
+ (LOC + "cannot locate host %1").arg(host) +
+ "\n\t\t\tPlease check HTTP_PROXY environment variable!");
+ else if (!telnet(host,port))
+ VERBOSE(VB_IMPORTANT,
+ (LOC + "%1:%2 - cannot connect!").arg(host).arg(port) +
+ "\n\t\t\tPlease check HTTP_PROXY environment variable!");
+
+#if 0
+ VERBOSE(VB_NETWORK, (LOC + "using http://%1:%2@%3:%4")
+ .arg(url.userName()).arg(url.password())
+ .arg(host).arg(port));
+#endif
+ p = QNetworkProxy(QNetworkProxy::HttpProxy,
+ host, port, url.userName(), url.password());
+ QNetworkProxy::setApplicationProxy(p);
+ return;
+ }
+
+
+
+
+ VERBOSE(VB_NETWORK, LOC + "no HTTP_PROXY environment var.");
+
+ // Use Qt to look for user proxy settings stored by the OS or browser:
+
+ QList<QNetworkProxy> proxies;
+ QNetworkProxyQuery query(QUrl("http://www.mythtv.org"));
+
+
+ proxies = QNetworkProxyFactory::systemProxyForQuery(query);
+
+ Q_FOREACH (p, proxies)
+ {
+ QString host = p.hostName();
+ int port = p.port();
+
+ if (p.type() == QNetworkProxy::NoProxy)
+ continue;
+
+ if (!telnet(host, port))
+ {
+ VERBOSE(VB_NETWORK, (LOC + "failed to contact proxy host ") + host);
+ continue;
+ }
+
+ VERBOSE(VB_NETWORK, (LOC + "using proxy host %1:%2")
+ .arg(host).arg(port));
+ QNetworkProxy::setApplicationProxy(p);
+
+ // Allow sub-commands to use this proxy
+ // via myth_system(command), by setting HTTP_PROXY
+ QString url;
+
+ if (p.user().length())
+ url = "http://%1:%2@%3:%4",
+ url = url.arg(p.user()).arg(p.password());
+ else
+ url = "http://%1:%2";
+
+ url = url.arg(p.hostName()).arg(p.port());
+ setenv("HTTP_PROXY", url.toAscii(), 1);
+ setenv("http_proxy", url.toAscii(), 0);
+
+ return;
+ }
+
+ VERBOSE(VB_NETWORK, LOC + "failed to find a network proxy");
+}
+
/* vim: set expandtab tabstop=4 shiftwidth=4: */
View
2 mythtv/libs/libmythbase/util.h
@@ -88,6 +88,8 @@ inline int sq(int a) { return a*a; }
MBASE_PUBLIC bool MythRemoveDirectory(QDir &aDir);
MBASE_PUBLIC QString &ShellEscape(QString &string);
+ MBASE_PUBLIC void setHttpProxy(void);
+
// CPU Tick timing function
#ifdef MMX
#ifdef _WIN32
View
2 mythtv/programs/mythbackend/main.cpp
@@ -154,6 +154,8 @@ int main(int argc, char **argv)
VERBOSE(VB_IMPORTANT, versionStr);
}
+ setHttpProxy();
+
gContext = new MythContext(MYTH_BINARY_VERSION);
if (cmdline.HasBackendCommand())
View
2 mythtv/programs/mythfrontend/main.cpp
@@ -1424,6 +1424,8 @@ int main(int argc, char **argv)
CleanupMyOldInUsePrograms();
+ setHttpProxy();
+
pmanager = new MythPluginManager();
gContext->SetPluginManager(pmanager);

0 comments on commit 01f1c92

Please sign in to comment.
Something went wrong with that request. Please try again.