Add option to specify HTTP server listen address/interface.

This commit is contained in:
craig.p.drummond@gmail.com
2012-06-15 11:40:53 +00:00
committed by craig.p.drummond@gmail.com
parent dcc47d1b57
commit fc7f4ab98e
10 changed files with 120 additions and 20 deletions

View File

@@ -47,6 +47,7 @@
29. Workaround tab-widget issues with Gtk style. Only draw highlight for
selected item, and item curretly under mouse - no fade in/out.
30. Fix/workaround issues with fetching lyrics from letras.mus.br
31. Add option to specify HTTP server listen address/interface.
0.7.1
-----

View File

@@ -35,6 +35,7 @@ void HttpServerSettings::load()
enableHttp->setChecked(Settings::self()->enableHttp());
alwaysUseHttp->setChecked(Settings::self()->alwaysUseHttp());
httpPort->setValue(Settings::self()->httpPort());
httpAddress->setText(Settings::self()->httpAddress());
}
void HttpServerSettings::save()
@@ -42,4 +43,5 @@ void HttpServerSettings::save()
Settings::self()->saveEnableHttp(enableHttp->isChecked());
Settings::self()->saveAlwaysUseHttp(alwaysUseHttp->isChecked());
Settings::self()->saveHttpPort(httpPort->value());
Settings::self()->saveHttpAddress(httpAddress->text());
}

View File

@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>384</width>
<height>254</height>
<width>416</width>
<height>345</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
@@ -50,7 +50,7 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="BuddyLabel" name="label_3">
<property name="text">
<string>Port:</string>
@@ -60,7 +60,7 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QSpinBox" name="httpPort">
<property name="minimum">
<number>1</number>
@@ -70,6 +70,19 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="BuddyLabel" name="label_5">
<property name="text">
<string>Address/interface:</string>
</property>
<property name="buddy">
<cstring>httpAddress</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="LineEdit" name="httpAddress"/>
</item>
</layout>
</item>
<item>
@@ -91,7 +104,8 @@
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>&lt;i&gt;&lt;b&gt;NOTE:&lt;/b&gt; MPD usually only plays songs that are stored within its folders. If you have connected via a local socket, then MPD can also play files located on your filesystem. If you are using a non local socket (e.g. you have entered a hostname or IP address in the &quot;Host&quot; field of the &quot;Server&quot; settings page), then Cantata contains a minimal HTTP server that can be used to server files to MPD. This, however, will only work whilst Cantata is running. If you enable &quot;Always use server&quot;, then Cantata will always pass server URLs to MPD for files, even when you have connected via a local socket.&lt;/i&gt;</string>
<string>&lt;b&gt;NOTE:&lt;/b&gt; MPD usually only plays songs that are stored within its folders. If you have connected via a local socket, then MPD can also play files located on your filesystem. If you are using a non local socket (e.g. you have entered a hostname or IP address in the &quot;Host&quot; field of the &quot;Server&quot; settings page), then Cantata contains a minimal HTTP server that can be used to server files to MPD. This, however, will only work whilst Cantata is running. If you enable &quot;Always use server&quot;, then Cantata will always pass server URLs to MPD for files, even when you have connected via a local socket.&lt;br/&gt;&lt;br/&gt;
&lt;b&gt;NOTE:&lt;/b&gt;&quot;Address/interface&quot; may contian either the IP address, or interface (e.g. eth0), that you wish to listen for connections on. Leave blank to use the local loopback address (127.0.0.1).&lt;/i&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
@@ -114,12 +128,23 @@
</layout>
</widget>
<customwidgets>
<customwidget>
<class>LineEdit</class>
<extends>QLineEdit</extends>
<header>lineedit.h</header>
</customwidget>
<customwidget>
<class>BuddyLabel</class>
<extends>QLabel</extends>
<header>buddylabel.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>enableHttp</tabstop>
<tabstop>alwaysUseHttp</tabstop>
<tabstop>httpAddress</tabstop>
<tabstop>httpPort</tabstop>
</tabstops>
<resources/>
<connections>
<connection>

View File

@@ -1546,7 +1546,11 @@ void MainWindow::readSettings()
{
checkMpdDir();
Covers::self()->setSaveInMpdDir(Settings::self()->storeCoversInMpdDir());
HttpServer::self()->setPort(Settings::self()->enableHttp() ? Settings::self()->httpPort() : 0);
if (Settings::self()->enableHttp()) {
HttpServer::self()->setDetails(Settings::self()->httpAddress(), Settings::self()->httpPort());
} else {
HttpServer::self()->setDetails(QString(), 0);
}
#ifdef ENABLE_DEVICES_SUPPORT
deleteSongsAction->setVisible(Settings::self()->showDeleteAction());
#endif

View File

@@ -512,6 +512,11 @@ int Settings::httpPort()
return GET_INT("httpPort", 9001);
}
QString Settings::httpAddress()
{
return GET_STRING("httpAddress", QString());
}
bool Settings::enableHttp()
{
return GET_BOOL("enableHttp", false);
@@ -814,6 +819,11 @@ void Settings::saveHttpPort(int v)
SET_VALUE("httpPort", v);
}
void Settings::saveHttpAddress(const QString &v)
{
SET_VALUE("httpAddress", v);
}
void Settings::saveEnableHttp(bool v)
{
SET_VALUE("enableHttp", v);

View File

@@ -102,6 +102,7 @@ public:
int version();
int stopFadeDuration();
int httpPort();
QString httpAddress();
bool enableHttp();
bool alwaysUseHttp();
bool playQueueGrouped();
@@ -159,6 +160,7 @@ public:
#endif
void saveStopFadeDuration(int v);
void saveHttpPort(int v);
void saveHttpAddress(const QString &v);
void saveEnableHttp(bool v);
void saveAlwaysUseHttp(bool v);
void savePlayQueueGrouped(bool v);

View File

@@ -60,9 +60,9 @@ void HttpServer::stop()
}
}
bool HttpServer::setPort(quint16 port)
bool HttpServer::setDetails(const QString &addr, quint16 port)
{
if (socket && port==socket->port()) {
if (socket && port==socket->serverPort() && addr==socket->configuredAddress()) {
return true;
}
@@ -78,7 +78,7 @@ bool HttpServer::setPort(quint16 port)
if (0!=port) {
thread=new QThread(0);
socket=new HttpSocket(port);
socket=new HttpSocket(addr, port);
socket->moveToThread(thread);
thread->start();
return socket->isListening();
@@ -94,7 +94,8 @@ bool HttpServer::isAlive() const
QString HttpServer::address() const
{
return QLatin1String("http://127.0.0.1:")+QString::number(socket ? socket->port() : Settings::self()->httpPort());
return socket ? QLatin1String("http://")+socket->address()+QChar(':')+QString::number(socket->serverPort())
: QLatin1String("http://127.0.0.1:")+QString::number(Settings::self()->httpPort());
}
bool HttpServer::isOurs(const QString &url) const
@@ -109,8 +110,8 @@ QByteArray HttpServer::encodeUrl(const Song &s) const
}
QUrl url;
url.setScheme("http");
url.setHost("127.0.0.1");
url.setPort(socket->port());
url.setHost(socket->address());
url.setPort(socket->serverPort());
url.setPath(s.file);
if (!s.album.isEmpty()) {
url.addQueryItem("album", s.album);

View File

@@ -46,7 +46,7 @@ public:
void stop();
bool isAlive() const;
bool setPort(quint16 port);
bool setDetails(const QString &addr, quint16 port);
QString address() const;
bool isOurs(const QString &url) const;
QByteArray encodeUrl(const Song &s) const;

View File

@@ -23,17 +23,67 @@
#include "httpsocket.h"
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QNetworkInterface>
#include <QtCore/QStringList>
#include <QtCore/QTextStream>
#include <QtCore/QFile>
#include <QtCore/QUrl>
#include <QtCore/QDebug>
HttpSocket::HttpSocket(quint16 p)
// static int level(const QString &s)
// {
// return QLatin1String("Link-local")==s
// ? 1
// : QLatin1String("Site-local")==s
// ? 2
// : QLatin1String("Global")==s
// ? 3
// : 0;
// }
HttpSocket::HttpSocket(const QString &addr, quint16 p)
: QTcpServer(0)
, portNumber(p)
, cfgAddr(addr)
, terminated(false)
{
listen(QHostAddress::LocalHost, portNumber);
QHostAddress a;
if (!addr.isEmpty()) {
a=QHostAddress(addr);
if (a.isNull()) {
QString ifaceName=addr;
// bool ipV4=true;
//
// if (ifaceName.endsWith("::6")) {
// ifaceName=ifaceName.left(ifaceName.length()-3);
// ipV4=false;
// }
QNetworkInterface iface=QNetworkInterface::interfaceFromName(ifaceName);
if (iface.isValid()) {
QList<QNetworkAddressEntry> addresses=iface.addressEntries();
// int ip6Scope=-1;
foreach (const QNetworkAddressEntry &addr, addresses) {
QHostAddress ha=addr.ip();
if (QAbstractSocket::IPv4Protocol==ha.protocol()) {
// if ((ipV4 && QAbstractSocket::IPv4Protocol==ha.protocol()) || (!ipV4 && QAbstractSocket::IPv6Protocol==ha.protocol())) {
// if (ipV4) {
a=ha;
break;
// } else {
// int scope=level(a.scopeId());
// if (scope>ip6Scope) {
// ip6Scope=scope;
// a=ha;
// }
// }
}
}
}
}
}
listen(a.isNull() ? QHostAddress::LocalHost : a, p);
qWarning() << address();
}
void HttpSocket::terminate()

View File

@@ -31,15 +31,20 @@ class HttpSocket : public QTcpServer
Q_OBJECT
public:
HttpSocket(quint16 p);
HttpSocket(const QString &addr, quint16 p);
virtual ~HttpSocket() {
}
void terminate();
void incomingConnection(int socket);
quint16 port() const {
return portNumber;
QString address() const {
return serverAddress().toString();
}
QString configuredAddress() {
return cfgAddr;
}
private Q_SLOTS:
@@ -47,7 +52,7 @@ private Q_SLOTS:
void discardClient();
private:
quint16 portNumber;
QString cfgAddr;
bool terminated;
};