Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: CRLF in HTTP response

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default CRLF in HTTP response

    I am writing a class to parse the HTTP headers.
    I need to pick up one particulr line .
    The problem is that some servers encode the CRLF with "\r\n" others with "\n".
    How to detect or handle different encoding?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    From what you posted, all serves will use '\n', sometimes with or without '\r'
    So look for '\n' and you are on the safe side.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    Read the headers through a QTextStream and use QTextStream::readLine().

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CRLF in HTTP response

    Quote Originally Posted by giusepped View Post
    I am writing a class to parse the HTTP headers.
    I need to pick up one particulr line .
    The problem is that some servers encode the CRLF with "\r\n" others with "\n".
    How to detect or handle different encoding?
    What's wrong with QHttpRequestHeader?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    What's wrong with QHttpRequestHeader?
    It is obsolete.

    Besides, the OP apparently needs to parse the header sent by a server, in which case the equally obsolete QHttpResponseHeader seems better suited.

    I suppose the modern approach would be to use QNetworkReply::header(), or QNetworkReply::rawHeader(), etc.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CRLF in HTTP response

    Quote Originally Posted by yeye_olive View Post
    It is obsolete.
    So? It's obsolete because of a reason that doesn't apply here.

    I suppose the modern approach would be to use QNetworkReply::header(), or QNetworkReply::rawHeader(), etc.
    QNetworkReply may not be obsolete but it is perfectly useless if one doesn't use QNetworkAccessManager. And if he does, there is no need to parse http headers, is there?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    So? It's obsolete because of a reason that doesn't apply here.
    I admit I do not know that reason, but I would be interested in learning it. In any case the docs state:
    This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
    Since I can see no clear indication of whether giusepped is developing new code or building on existing code already using QHttp* classes, I find that state of affairs worth mentioning.
    QNetworkReply may not be obsolete but it is perfectly useless if one doesn't use QNetworkAccessManager. And if he does, there is no need to parse http headers, is there?
    I cannot think of any reason to parse the headers manually either, at least to the point that the end-of-line encoding matters, regardless of whether the QNetworkAccessManager or QHttp* approach is used, but I have no idea what giusepped is trying to achieve.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CRLF in HTTP response

    Quote Originally Posted by yeye_olive View Post
    I admit I do not know that reason, but I would be interested in learning it.
    It's obsolete because it was meant to be used with QHttp and since QHttp is obsolete now, this class is also marked as such. But it doesn't mean that it's obsolete for every other usecase (like when you're developing your own http server or proxy).

    Since I can see no clear indication of whether giusepped is developing new code or building on existing code already using QHttp* classes, I find that state of affairs worth mentioning.
    Do you see any indication that the OP wants to download anything via http?

    I cannot think of any reason to parse the headers manually either
    I see lots of reasons to do that but only if your goal is not to issue a simple get or post request to a remote http server.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    I cannot use QNetworkAccesManager because I am building an http proxy. Maybe it can be done also by using it, but I think it is more difficult.
    Using simple QTcpSocket is faster, but at that point I have to parse the headers.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CRLF in HTTP response

    Have a look here: http://www.qtcentre.org/threads/34082-NetworqDebugger. It's not a HTTP proxy but maybe it will give you some insights. For parsing HTTP headers I suggest to use QHttpRequestHeader and QHttpResponseHeader.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    It seems that the file you attached in the post cited is not valid. Could you post it her?
    However the problem is that everything from remote host can come in chunk, also headers.
    G

  12. #12
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    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
    Qt Code:
    1. int Util::getCR(const QByteArray &line)
    2. {
    3. int c = 0;
    4. char ch;
    5. QChar sc;
    6. for(int i=0;i<line.size();i++)
    7. {
    8. sc = line.at(i);
    9. if (sc.isDigit())
    10. ch = sc.unicode() - '0';
    11. //qDebug()<<"UTIL getCR byte"<<line.at(i)<<sc.unicode()*1;;
    12.  
    13.  
    14. if (c == 13)
    15.  
    16. {
    17. // We found CR ASCCI '0D'
    18. // Check if there is a LF
    19.  
    20. if (i == line.size()-1)
    21. {
    22. sc = line.at(i+1);
    23. c = sc.unicode() * 1;
    24. if (c == 10)
    25. return i+1;
    26. }
    27. else
    28. return i;
    29.  
    30. }
    31. else
    32. // Do the same but first is '0A'
    33. if ( c == 10)
    34. {
    35. // We found CR ASCCI '0D'
    36. // Check if there is a LF
    37.  
    38. if (i == line.size()-1)
    39. {
    40. sc = line.at(i+1);
    41. c = sc.unicode() * 1;
    42. if (c == 13)
    43. return i+1;
    44. }
    45. else
    46. return i;
    47. }
    48. }
    49.  
    50.  
    51.  
    52. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CRLF in HTTP response

    Quote Originally Posted by giusepped View Post
    It seems that the file you attached in the post cited is not valid.
    It is valid.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #14
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    I tried to unzip it many times. Alway the zipper complains that the archive is invalid.
    Regards

  15. #15
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: CRLF in HTTP response

    I can confirm the file is valid as I've just downloaded it.

    Confirm your software is working OK.

  16. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: CRLF in HTTP response

    Quote Originally Posted by giusepped View Post
    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).
    You should report any embedded web server that uses something other "\r\n" to the creator of the server software. The HTTP RFC is clear:
    "HTTP/1.1 defines the sequence CR LF as the end-of-line marker for all protocol elements except the entity-body"
    http://www.w3.org/Protocols/rfc2616/...c2.html#sec2.2 (Was the same in HTTP/1).

    The RFC advises tolerance (http://www.w3.org/Protocols/rfc2616/...9.html#sec19.3) but even it does not mention LFCR as a possibility. You still have to accept the rubbish data from the server, but if you don't tell them they cannot fix it.

  17. #17
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    Quote Originally Posted by ChrisW67 View Post
    You should report any embedded web server that uses something other "\r\n" to the creator of the server software. The HTTP RFC is clear:
    I agree with you. But also TCP has an RFC and there out there a lot of TCP flavors totally out of the standard (think of download accelerators).
    Anyway, what you suggest is very difficult when the producer of the embedded video server is an unknown China based maker and you have to provide your client with a solution. On the other hand, we are engineer after all, aren't we?

  18. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CRLF in HTTP response

    Quote Originally Posted by giusepped View Post
    I agree with you. But also TCP has an RFC and there out there a lot of TCP flavors totally out of the standard (think of download accelerators).
    Not really. There are a lot of extensions to TCP defined and they have separate RFC documents. It is true that some implementations (mostly Windows) do not conform in 100% to TCP specs but this is mostly related to unused or reserved fields.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #19
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CRLF in HTTP response

    However, your class is of little help and I ma stucked with this problem.
    I am looking for someone can help me.
    Could you take a look at my code?
    It works for most web servers except one.
    Regards

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: CRLF in HTTP response

    Did you try using QHttpRequestHeader and QHttpResponseHeader as I suggested?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 9
    Last Post: 10th October 2012, 22:55
  2. How to parse xml from a http xml response?
    By dineshkumar in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2011, 07:28
  3. Reqest and Response in XML
    By srohit24 in forum Qt Programming
    Replies: 28
    Last Post: 4th July 2009, 07:57
  4. QHTTP does not see tunneled TCP HTTP OK authentication response
    By SailingDreams in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2009, 09:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.