TTKMusicPlayer  4.1.3.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 &url = "http://%1:11111/music/";
62  const QString &value = prefix.left(prefix.lastIndexOf(TTK_DOT));
63 
64  for(const QHostAddress &address : QNetworkInterface::allAddresses())
65  {
66  if(address.toString().contains(value))
67  {
68  return url.arg(address.toString());
69  }
70  }
71  return url.arg("0.0.0.0");
72 }
73 
75 {
77  if(d->m_prefix.isEmpty())
78  {
79  response->writeHead(500);
80  response->end("Music path prefix is empty");
81  return;
82  }
83 
84  const QRegExp regx("^/music/(.*)$");
85  if(regx.indexIn(request->path()) != -1)
86  {
87  response->setHeader("Content-Type", "audio/mp3");
88 
89  const QString &name = regx.cap(1);
90  QFile file(d->m_prefix + TTK_SEPARATOR + name);
91  if(file.open(QIODevice::ReadOnly))
92  {
93  response->writeHead(200);
94  response->end(file.readAll());
95  file.close();
96  }
97  else
98  {
99  response->writeHead(404);
100  response->end("Resource not found");
101  }
102  }
103  else
104  {
105  response->writeHead(403);
106  response->end("You aren't allowed here");
107  }
108 }
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:196
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