I solved myself the problem, which is non trivial.
Web server can use different scheme for separate header lines.
You can have
'\n' or '\r\n' or '\n\r'.
Believe me beacause I tested a lot of embedded web server (not the bigger ones).
The (most) important part of the header is the Content-Length.
If you (as me) are planning to change the body, you must carefull re-write the Content-Length value.
Follow the code.
The "line" variable is a line you want to analyze, for example (Content-Length). It is simple to extract it from the header
{
int c = 0;
char ch;
for(int i=0;i<line.size();i++)
{
sc = line.at(i);
if (sc.isDigit())
ch = sc.unicode() - '0';
//qDebug()<<"UTIL getCR byte"<<line.at(i)<<sc.unicode()*1;;
if (c == 13)
{
// We found CR ASCCI '0D'
// Check if there is a LF
if (i == line.size()-1)
{
sc = line.at(i+1);
c = sc.unicode() * 1;
if (c == 10)
return i+1;
}
else
return i;
}
else
// Do the same but first is '0A'
if ( c == 10)
{
// We found CR ASCCI '0D'
// Check if there is a LF
if (i == line.size()-1)
{
sc = line.at(i+1);
c = sc.unicode() * 1;
if (c == 13)
return i+1;
}
else
return i;
}
}
}
int Util::getCR(const QByteArray &line)
{
int c = 0;
char ch;
QChar sc;
for(int i=0;i<line.size();i++)
{
sc = line.at(i);
if (sc.isDigit())
ch = sc.unicode() - '0';
//qDebug()<<"UTIL getCR byte"<<line.at(i)<<sc.unicode()*1;;
if (c == 13)
{
// We found CR ASCCI '0D'
// Check if there is a LF
if (i == line.size()-1)
{
sc = line.at(i+1);
c = sc.unicode() * 1;
if (c == 10)
return i+1;
}
else
return i;
}
else
// Do the same but first is '0A'
if ( c == 10)
{
// We found CR ASCCI '0D'
// Check if there is a LF
if (i == line.size()-1)
{
sc = line.at(i+1);
c = sc.unicode() * 1;
if (c == 13)
return i+1;
}
else
return i;
}
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks