From 54643c4c4cbaa47cd99245c6826f4a78b47229e3 Mon Sep 17 00:00:00 2001 From: Roland Stigge Date: Sat, 20 Jan 2018 17:02:32 +0100 Subject: Multiple file uploads at once --- src/webbox.cpp | 66 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 30 deletions(-) (limited to 'src/webbox.cpp') diff --git a/src/webbox.cpp b/src/webbox.cpp index 995e688..1bc0664 100644 --- a/src/webbox.cpp +++ b/src/webbox.cpp @@ -13,7 +13,7 @@ #include #include -#define PROGRAMVERSION "1.2" +#define PROGRAMVERSION "1.3" #define BUFSIZE 1000000 // XML special characters: @@ -617,52 +617,58 @@ class UploadCommand: public PostCommand { } else { QByteArray boundary = QByteArray("--") + contentType.split(separator)[1].toUtf8(); int boundaryCount = m_content.count(boundary); - if (boundaryCount != 2) { + if (boundaryCount < 2) { FCGX_PutS(QString("Bad boundary number found: %1").arg(boundaryCount).toUtf8().data(), p.request.out); } else { - int start = m_content.indexOf(boundary) + boundary.size(); - int end = m_content.indexOf(QByteArray("\r\n") + boundary, start); + while (true) { + int start = m_content.indexOf(boundary) + boundary.size(); + int end = m_content.indexOf(QByteArray("\r\n") + boundary, start); - m_content = m_content.mid(start, end - start); + if (end == -1) { // no further boundary found: all handled. + break; + } - // Read filename - start = m_content.indexOf("filename=\""); - if (start == -1) { - FCGX_PutS(QString("Error reading filename / start").toUtf8().data(), p.request.out); - } else { - start += QByteArray("filename=\"").size(); + QByteArray filecontent = m_content.mid(start, end - start); + int nextBoundaryIndex = end; - end = m_content.indexOf(QByteArray("\""), start); - if (end == -1) { - FCGX_PutS(QString("Error reading filename / end").toUtf8().data(), p.request.out); + // Read filename + start = filecontent.indexOf("filename=\""); + if (start == -1) { + FCGX_PutS(QString("Error reading filename / start").toUtf8().data(), p.request.out); } else { - QString filename = QString::fromUtf8(m_content.mid(start, end - start)); + start += QByteArray("filename=\"").size(); - if (filename.size() < 1) { - FCGX_PutS(QString("Bad filename").toUtf8().data(), p.request.out); + end = filecontent.indexOf(QByteArray("\""), start); + if (end == -1) { + FCGX_PutS(QString("Error reading filename / end").toUtf8().data(), p.request.out); } else { - // Remove header - start = m_content.indexOf(QByteArray("\r\n\r\n")); - if (start == -1) { - FCGX_PutS(QString("Error removing upload header").toUtf8().data(), p.request.out); + QString filename = QString::fromUtf8(filecontent.mid(start, end - start)); + + if (filename.size() < 1) { + FCGX_PutS(QString("Bad filename").toUtf8().data(), p.request.out); } else { + // Remove header + start = filecontent.indexOf(QByteArray("\r\n\r\n")); + if (start == -1) { + FCGX_PutS(QString("Error removing upload header").toUtf8().data(), p.request.out); + } else { - m_content = m_content.mid(start + QString("\r\n\r\n").toUtf8().size()); + filecontent = filecontent.mid(start + QString("\r\n\r\n").toUtf8().size()); - QFile file(m_path + "/" + filename); - if (!file.open(QIODevice::WriteOnly)) { - FCGX_PutS(QString("Error opening file").toUtf8().data(), p.request.out); - } else { - qint64 written = file.write(m_content); - if (written != m_content.size()) { - FCGX_PutS(QString("Error writing file").toUtf8().data(), p.request.out); + QFile file(m_path + "/" + filename); + if (!file.open(QIODevice::WriteOnly)) { + FCGX_PutS(QString("Error opening file").toUtf8().data(), p.request.out); } else { - FCGX_PutS("OK", p.request.out); + qint64 written = file.write(filecontent); + if (written != filecontent.size()) { + FCGX_PutS(QString("Error writing file").toUtf8().data(), p.request.out); + } } } } } } + m_content.remove(0, nextBoundaryIndex); } } } -- cgit v1.2.3