| 150 | } |
| 151 | |
| 152 | void HttpRequest::readBody(QTcpSocket* socket) |
| 153 | { |
| 154 | Q_ASSERT(expectedBodySize!=0); |
| 155 | if (boundary.isEmpty()) |
| 156 | { |
| 157 | // normal body, no multipart |
| 158 | #ifdef SUPERVERBOSE |
| 159 | Logger::logger()->log(Logger::LANServer, "HttpRequest: receive body"); |
| 160 | #endif |
| 161 | int toRead=expectedBodySize-bodyData.size(); |
| 162 | QByteArray newData=socket->read(toRead); |
| 163 | currentSize+=newData.size(); |
| 164 | bodyData.append(newData); |
| 165 | if (bodyData.size()>=expectedBodySize) |
| 166 | { |
| 167 | status=complete; |
| 168 | } |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | // multipart body, store into temp file |
| 173 | #ifdef SUPERVERBOSE |
| 174 | Logger::logger()->log(Logger::LANServer, "HttpRequest: receiving multipart body"); |
| 175 | #endif |
| 176 | // Create an object for the temporary file, if not already present |
| 177 | if (tempFile == nullptr) |
| 178 | { |
| 179 | tempFile = new QTemporaryFile; |
| 180 | } |
| 181 | if (!tempFile->isOpen()) |
| 182 | { |
| 183 | tempFile->open(); |
| 184 | } |
| 185 | // Transfer data in 64kb blocks |
| 186 | qint64 fileSize=tempFile->size(); |
| 187 | qint64 toRead=expectedBodySize-fileSize; |
| 188 | if (toRead>65536) |
| 189 | { |
| 190 | toRead=65536; |
| 191 | } |
| 192 | fileSize+=tempFile->write(socket->read(toRead)); |
| 193 | if (fileSize>=maxMultiPartSize) |
| 194 | { |
| 195 | Logger::logger()->log(Logger::LANServer, "HttpRequest: received too many multipart bytes"); |
| 196 | status=abort; |
| 197 | } |
| 198 | else if (fileSize>=expectedBodySize) |
| 199 | { |
| 200 | #ifdef SUPERVERBOSE |
| 201 | Logger::logger()->log(Logger::LANServer, "HttpRequest: received whole multipart body"); |
| 202 | #endif |
| 203 | tempFile->flush(); |
| 204 | if (tempFile->error()) |
| 205 | { |
| 206 | Logger::logger()->log(Logger::LANServer, "HttpRequest: Error writing temp file for multipart body"); |
| 207 | } |
| 208 | parseMultiPartFile(); |
| 209 | tempFile->close(); |