18 Q_ASSERT(settings != 0);
22 maxSize=settings->value(
"maxRequestSize",
"16000").toInt();
30 Q_ASSERT(settings != 0);
43 qDebug(
"HttpRequest::readRequest: read request");
51 qDebug(
"HttpRequest::readRequest: collecting more parts until line break");
57 if (!newData.isEmpty())
59 QList<QByteArray> list=newData.split(
' ');
60 if (list.count()!=3 || !list.at(2).contains(
"HTTP"))
62 qWarning(
"HttpRequest::readRequest: received broken HTTP request, invalid first line");
66 method=list.at(0).trimmed();
78 qDebug(
"HttpRequest::readHeader");
86 qDebug(
"HttpRequest::readHeader: collecting more parts until line break");
92 int colon=newData.indexOf(
':');
97 QByteArray value=newData.mid(colon+1).trimmed();
100 qDebug(
"HttpRequest::readHeader: received header %s: %s",
currentHeader.data(),value.data());
103 else if (!newData.isEmpty())
107 qDebug(
"HttpRequest::readHeader: read additional line of header");
118 qDebug(
"HttpRequest::readHeader: headers completed");
122 QByteArray contentType=
headers.value(
"content-type");
123 if (contentType.startsWith(
"multipart/form-data"))
125 int posi=contentType.indexOf(
"boundary=");
134 QByteArray contentLength=
headers.value(
"content-length");
135 if (!contentLength.isEmpty())
142 qDebug(
"HttpRequest::readHeader: expect no body");
148 qWarning(
"HttpRequest::readHeader: expected body is too large");
153 qWarning(
"HttpRequest::readHeader: expected multipart body is too large");
172 qDebug(
"HttpRequest::readBody: receive body");
175 QByteArray newData=socket->read(toRead);
187 qDebug(
"HttpRequest::readBody: receiving multipart body");
205 fileSize+=
tempFile->write(socket->read(toRead));
208 qWarning(
"HttpRequest::readBody: received too many multipart bytes");
214 qDebug(
"HttpRequest::readBody: received whole multipart body");
219 qCritical(
"HttpRequest::readBody: Error writing temp file for multipart body");
231 qDebug(
"HttpRequest::decodeRequestParams: extract and decode request parameters");
234 QByteArray rawParameters;
235 int questionMark=
path.indexOf(
'?');
238 rawParameters=
path.mid(questionMark+1);
242 QByteArray contentType=
headers.value(
"content-type");
243 if (!
bodyData.isEmpty() && (contentType.isEmpty() || contentType.startsWith(
"application/x-www-form-urlencoded")))
245 if (!rawParameters.isEmpty())
247 rawParameters.append(
'&');
256 QList<QByteArray> list=rawParameters.split(
'&');
257 foreach (QByteArray part, list)
259 int equalsChar=part.indexOf(
'=');
262 QByteArray name=part.left(equalsChar).trimmed();
263 QByteArray value=part.mid(equalsChar+1).trimmed();
266 else if (!part.isEmpty())
277 qDebug(
"HttpRequest::extractCookies");
279 foreach(QByteArray cookieStr,
headers.values(
"cookie"))
282 foreach(QByteArray part, list)
285 qDebug(
"HttpRequest::extractCookies: found cookie %s",part.data());
286 #endif // Split the part into name and value 289 int posi=part.indexOf(
'=');
292 name=part.left(posi).trimmed();
293 value=part.mid(posi+1).trimmed();
323 qWarning(
"HttpRequest::readFromSocket: received too many bytes");
368 return headers.value(name.toLower());
373 return headers.values(name.toLower());
403 QByteArray buffer(source);
404 buffer.replace(
'+',
' ');
405 int percentChar=buffer.indexOf(
'%');
406 while (percentChar>=0)
409 char byte=buffer.mid(percentChar+1,2).toInt(&ok,16);
412 buffer.replace(percentChar,3,(
char*)&byte,1);
414 percentChar=buffer.indexOf(
'%',percentChar+1);
422 qDebug(
"HttpRequest::parseMultiPartFile: parsing multipart temp file");
428 qDebug(
"HttpRequest::parseMultiPartFile: reading multpart headers");
430 QByteArray fieldName;
434 QByteArray line=
tempFile->readLine(65536).trimmed();
435 if (line.startsWith(
"Content-Disposition:"))
437 if (line.contains(
"form-data"))
439 int start=line.indexOf(
" name=\"");
440 int end=line.indexOf(
"\"",start+7);
441 if (start>=0 && end>=start)
443 fieldName=line.mid(start+7,end-start-7);
445 start=line.indexOf(
" filename=\"");
446 end=line.indexOf(
"\"",start+11);
447 if (start>=0 && end>=start)
449 fileName=line.mid(start+11,end-start-11);
452 qDebug(
"HttpRequest::parseMultiPartFile: multipart field=%s, filename=%s",fieldName.data(),fileName.data());
457 qDebug(
"HttpRequest::parseMultiPartFile: ignoring unsupported content part %s",line.data());
460 else if (line.isEmpty())
467 qDebug(
"HttpRequest::parseMultiPartFile: reading multpart data");
469 QTemporaryFile* uploadedFile=0;
470 QByteArray fieldValue;
473 QByteArray line=
tempFile->readLine(65536);
478 if (fileName.isEmpty() && !fieldName.isEmpty())
481 fieldValue.remove(fieldValue.size()-2,2);
483 qDebug(
"HttpRequest: set parameter %s=%s",fieldName.data(),fieldValue.data());
485 else if (!fileName.isEmpty() && !fieldName.isEmpty())
489 qDebug(
"HttpRequest::parseMultiPartFile: finishing writing to uploaded file");
494 uploadedFile->resize(uploadedFile->size()-2);
495 uploadedFile->flush();
496 uploadedFile->seek(0);
497 qDebug(
"HttpRequest::parseMultiPartFile: set parameter %s=%s",fieldName.data(),fileName.data());
499 qDebug(
"HttpRequest::parseMultiPartFile: uploaded file size is %i",(
int) uploadedFile->size());
510 if (fileName.isEmpty() && !fieldName.isEmpty())
514 fieldValue.append(line);
516 else if (!fileName.isEmpty() && !fieldName.isEmpty())
521 uploadedFile=
new QTemporaryFile();
522 uploadedFile->open();
524 uploadedFile->write(line);
525 if (uploadedFile->error())
527 qCritical(
"HttpRequest::parseMultiPartFile: error writing temp file, %s",qPrintable(uploadedFile->errorString()));
535 qCritical(
"HttpRequest::parseMultiPartFile: cannot read temp file, %s",qPrintable(
tempFile->errorString()));
538 qDebug(
"HttpRequest::parseMultiPartFile: finished parsing multipart temp file");
QByteArray getBody() const
QTemporaryFile * tempFile
RequestStatus getStatus() const
QMultiMap< QByteArray, QByteArray > parameters
QMultiMap< QByteArray, QByteArray > getHeaderMap() const
HttpRequest(QSettings *settings)
void readHeader(QTcpSocket *socket)
QByteArray getParameter(const QByteArray &name) const
void readBody(QTcpSocket *socket)
QByteArray getPath() const
QList< QByteArray > getParameters(const QByteArray &name) const
void decodeRequestParams()
QMap< QByteArray, QByteArray > & getCookieMap()
QByteArray getVersion() const
static QByteArray urlDecode(const QByteArray source)
void parseMultiPartFile()
QByteArray getHeader(const QByteArray &name) const
QMultiMap< QByteArray, QByteArray > headers
void readFromSocket(QTcpSocket *socket)
static QList< QByteArray > splitCSV(const QByteArray source)
QByteArray getCookie(const QByteArray &name) const
QHostAddress getPeerAddress() const
QTemporaryFile * getUploadedFile(const QByteArray fieldName) const
const QByteArray & getRawPath() const
QList< QByteArray > getHeaders(const QByteArray &name) const
QMap< QByteArray, QByteArray > cookies
QMap< QByteArray, QTemporaryFile * > uploadedFiles
void readRequest(QTcpSocket *socket)
QMultiMap< QByteArray, QByteArray > getParameterMap() const
QByteArray getMethod() const