TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qdlnafileserver.cpp
Go to the documentation of this file.
1 #include "qdlnafileserver.h"
2 
6 
7 #include <QFile>
8 #include <QRegExp>
9 #include <QNetworkInterface>
10 
14 class QDlnaFileServerPrivate : public TTKPrivate<QDlnaFileServer>
15 {
16 public:
19 
20  QString m_prefix;
22 
23 };
24 
26  : m_server(new QHttpServer)
27 {
28 
29 }
30 
32 {
33  m_server->close();
34  delete m_server;
35 }
36 
37 
38 
40  : QObject(parent)
41 {
44  connect(d->m_server, SIGNAL(newRequest(QHttpRequest*, QHttpResponse*)), SLOT(handleRequest(QHttpRequest*, QHttpResponse*)));
45 }
46 
48 {
50  d->m_server->listen(QHostAddress::Any, 11111);
51 }
52 
53 void QDlnaFileServer::setPrefixPath(const QString &path)
54 {
56  d->m_prefix = path;
57 }
58 
59 QString QDlnaFileServer::localAddress(const QString &prefix) const
60 {
61  const QString &value = prefix.left(prefix.lastIndexOf(TTK_DOT));
62  for(const QHostAddress &address : QNetworkInterface::allAddresses())
63  {
64  if(address.toString().contains(value))
65  {
66  return QString("http://%1:11111/").arg(address.toString());
67  }
68  }
69  return "http://0.0.0.0:11111/";
70 }
71 
73 {
75  const QRegExp regx("^/music/(.*)$");
76  if(regx.indexIn(request->path()) != -1)
77  {
78  response->setHeader("Content-Type", "audio/mp3");
79 
80  const QString &name = regx.cap(1);
81  QFile file(d->m_prefix + TTK_SEPARATOR + name);
82  if(file.open(QIODevice::ReadOnly))
83  {
84  response->writeHead(200);
85  response->end(file.readAll());
86  file.close();
87  }
88  else
89  {
90  response->writeHead(404);
91  response->end("Resource not found");
92  }
93  }
94  else
95  {
96  response->writeHead(403);
97  response->end("You aren't allowed here");
98  }
99 }
The class of the http response.
Definition: qhttpresponse.h:29
QString localAddress(const QString &prefix) const
void close()
Stop the server and listening for new connections.
void end(const QByteArray &data="")
End/finish the response.
The class of the http request.
Definition: qhttprequest.h:32
#define TTK_DOT
Definition: ttkglobal.h:193
void setPrefixPath(const QString &path)
const char * name
Definition: http_parser.c:458
The class of the http server.
Definition: qhttpserver.h:37
The class of the dlna file server private.
#define TTK_SEPARATOR
Definition: ttkglobal.h:195
The class of the dlna file server.
QDlnaFileServer(QObject *parent=nullptr)
void handleRequest(QHttpRequest *request, QHttpResponse *response)
void setHeader(const QString &field, const QString &value)
Sets a response header field to value.
#define TTK_INIT_PRIVATE(Class)
Definition: ttkprivate.h:33
The class of the ttk private base.
Definition: ttkprivate.h:48
void writeHead(int statusCode)
Writes the header section of the response using status as the response status code.
QString path
Definition: qhttprequest.h:40
#define TTK_D(Class)
Definition: ttkprivate.h:41