Results 1 to 3 of 3

Thread: QSslSocket cannot read transmitted line(s)

  1. #1
    Join Date
    Sep 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QSslSocket cannot read transmitted line(s)

    Hi everybody,
    I've got the probvlem that if I want to read the to my QSslSocket transmitted https-data, the call of QSslSocket->CanReadLine seems to return false. I don't haver any idea whrer this comes from, but it worked how it should and soddenly it didn't without changing anything and changing anything now brings no success.
    Here is my code (CSSLConnection inherits QThread and is created from a QTCPServer in its' incommingConnection-function):
    Qt Code:
    1. // Verbindung verschlüsseln
    2. MSResult CMSSSLConnection::startSocketencryption()
    3. {
    4. m_pSSLSocket->setLocalCertificate("/home/niklas/Dokumente/MySchool/Debug_Ubuntu_x86_Desktop/MSHTTPSServer/myschool.crt");
    5. m_pSSLSocket->setPrivateKey("/home/niklas/Dokumente/MySchool/Debug_Ubuntu_x86_Desktop/MSHTTPSServer/myschool.key.insecure");
    6.  
    7. m_pSSLSocket->startServerEncryption();
    8. if(m_pSSLSocket->waitForEncrypted())
    9. {
    10. m_pSSLSocket->write("HTTP/1.1 200 OK\r\n"
    11. "Content-type: text/plain\r\n"
    12. "Content-length: 12\r\n"
    13. "\r\n"
    14. "Hello World!");
    15. return MSR_OK;
    16. }
    17. else
    18. {
    19. Log->Error(QString("Cannot encrypt connection to %1: %2").arg(m_pSSLSocket->peerAddress().toString(),
    20. m_pSSLSocket->errorString()));
    21. m_pSSLSocket->disconnectFromHost();
    22. return MSR_ENCRYPTION_ERROR;
    23. }
    24. }
    25.  
    26. // Neue Verbindung wurde aufgebaut
    27. void CMSSSLConnection::acceptClient()
    28. {
    29. }
    30.  
    31. // Daten verarbeiten
    32. void CMSSSLConnection::readData()
    33. {
    34. if(m_pSSLSocket->canReadLine())
    35. {
    36. // Anfrage zerlegen
    37. QStringList request = QString(m_pSSLSocket->readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
    38.  
    39. // Um welchen Anfragetypen handelt es sich?
    40. if(request[0] == "GET")
    41. {
    42. // Einfache GET-ANfrage
    43. QTextStream out(m_pSSLSocket);
    44. out.setAutoDetectUnicode(true);
    45. out << "HTTP/1.0 200 Ok\r\n"
    46. "Content-Type: text/html;\r\n"
    47. "\r\n"
    48. "<h1>Nothing to see here</h1>\n";
    49. }
    50. }
    51. else
    52. {
    53. Log->Error(QString("Cannot read line from %1").arg(m_pSSLSocket->peerAddress().toString()));
    54. return;
    55. }
    56. }
    To copy to clipboard, switch view to plain text mode 

    So in fact, if I connect via firefox (https://localhost) the first message (Hello World) is transmitted, but the second one isn't. Instead of this, in my logfile it says "cannot read line form 127.0.0.1", but there is no exact error given.

    The certificates are created like here: http://quickmediasolutions.com/blog/...ations-with-qt, but I used only a length of 2048 for the key and the program should run on linux (ubuntu).
    I hope you can help me.

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

    Default Re: QSslSocket cannot read transmitted line(s)

    Could you tell me what is the sense of this line?

    Qt Code:
    1. QStringList request = QString(m_pSSLSocket->readLine()).split(QRegExp("[ \r\n][ \r\n]*"));
    To copy to clipboard, switch view to plain text mode 

    As in my language it says "read one line and split it on the 'new line' character". If you read one line how can it have any newline characters?

    Analyze your code. Think whether it may happen that you receive the request in different chunks that you sent it, e.g.:

    chunk 1:
    GE

    chunk 2:
    T /index.html HTTP/1.

    chunk 3:
    1\r\nHost: mywonder

    chunk 4:
    fulhost.com\r\n

    chunk 5:
    \r\n


    If you come to a conclusion that it might happen (I'll give you a hint: yes, it might) then look at your code again and see if it is able to handle that situation.

    I can see this is some kind of school project so we're not allowed to give you complete solutions but such hint should be useful too.
    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.


  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,626
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSslSocket cannot read transmitted line(s)

    As in my language it says "read one line and split it on the 'new line' character". If you read one line how can it have any newline characters?
    There is a space in the character class so the regular expression would have the effect of matching on one or more spaces.

Similar Threads

  1. Read gzip file line-by-line
    By The_Fallen in forum Qt Programming
    Replies: 4
    Last Post: 6th September 2011, 13:41
  2. read xml line by line
    By Dilshad in forum Newbie
    Replies: 17
    Last Post: 28th December 2010, 11:34
  3. How to read and write line by line?
    By Alex Snet in forum Qt Programming
    Replies: 3
    Last Post: 28th November 2010, 15:49
  4. Replies: 3
    Last Post: 13th August 2010, 11:50
  5. read QTextBrowser line by line
    By navid in forum Newbie
    Replies: 1
    Last Post: 1st March 2010, 15:05

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.