| 133 | } |
| 134 | |
| 135 | void FileServer::onRequestNeedsReply(QtHttpRequest* request, QtHttpReply* reply) |
| 136 | { |
| 137 | QString command = request->getCommand(); |
| 138 | if (command == QStringLiteral("GET")) |
| 139 | { |
| 140 | QString path = request->getUrl().path(); |
| 141 | QStringList uri_parts = path.split('/', Qt::SkipEmptyParts); |
| 142 | // special uri handling for server commands |
| 143 | if (!uri_parts.empty()) |
| 144 | { |
| 145 | if (uri_parts.at(0) == "hyperhdr_heart_beat") |
| 146 | { |
| 147 | reply->addHeader("Content-Type", "text/plain"); |
| 148 | reply->appendRawData("alive"); |
| 149 | return; |
| 150 | } |
| 151 | else if (uri_parts.at(0) == "description.xml" && !_ssdpXmlDesc.isEmpty()) |
| 152 | { |
| 153 | reply->addHeader("Content-Type", "text/xml"); |
| 154 | reply->appendRawData(_ssdpXmlDesc.toLocal8Bit()); |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | QFileInfo info(_baseUrl % "/" % path); |
| 160 | if (path == "/" || path.isEmpty()) |
| 161 | { |
| 162 | path = "index.html"; |
| 163 | } |
| 164 | else if (info.isDir() && path.endsWith("/")) |
| 165 | { |
| 166 | path += "index.html"; |
| 167 | } |
| 168 | else if (info.isDir() && !path.endsWith("/")) |
| 169 | { |
| 170 | path += "/index.html"; |
| 171 | } |
| 172 | |
| 173 | // get static files |
| 174 | QFile file(_baseUrl % "/" % path); |
| 175 | if (file.exists()) |
| 176 | { |
| 177 | QString mimeName = getMimeName(file.fileName()); |
| 178 | if (file.open(QFile::ReadOnly)) { |
| 179 | QByteArray data = file.readAll(); |
| 180 | reply->addHeader("Content-Type", mimeName.toLocal8Bit()); |
| 181 | reply->addHeader(QtHttpHeader::AccessControlAllow, "*"); |
| 182 | reply->appendRawData(data); |
| 183 | file.close(); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | printErrorToReply(reply, QtHttpReply::Forbidden, "Requested file: " % path); |
| 188 | } |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | printErrorToReply(reply, QtHttpReply::NotFound, "Requested file: " % path); |
no test coverage detected