1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| void Http::readClient()
{
qDebug("Requête en cours....");
// This slot is called when the client sent data to the server. The
// server looks if it was a get request and sends a very simple HTML
// document back.
QTcpSocket* socket = (QTcpSocket*)sender();
if (socket->canReadLine()) {
QStringList tokens = QString(socket->readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
qWarning(tokens.join(",").toStdString().c_str());
if (tokens[0] == "GET") {
qDebug(tokens[1].toStdString().c_str());
QString filePath;
if (tokens[1]=="" || tokens[1]=="/"){
filePath = QDir::currentPath()+"/"+SERVER_WWW+"/index.php";
}else{
filePath = QDir::currentPath()+"/"+SERVER_WWW+tokens[1];
}
qDebug(filePath.toStdString().c_str());
QFile file(filePath);
QTextStream in(&file);
QTextStream os(socket);
os.setAutoDetectUnicode(true);
if(!file.open(QIODevice::ReadOnly)) {
qDebug("ERREUR : Fichier non ouvert");
os << "HTTP/1.0 404 OkHTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html; charset=\"utf-8\"\r\n"
"\r\n<h1>Erreur 404</h1> Page non trouvée\r\n\r\n";
}else{
os << Http::getContentType(filePath);
while(!in.atEnd()) {
os << in.readLine() << "\r\n";
}
file.close();
}
socket->close();
if (socket->state() == QTcpSocket::UnconnectedState) {
delete socket;
}
}
}
} |
Partager