Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, April 2008
QSslSocket and HTTP protocol


Message 1 in thread

Consider a class subclassed from QSslSocket and hitting an HTTPS  
server.  (I can't use the HTTP class because I need to setup custom  
headers.)

My class is pointer based, i.e. it is dynamically allocated using  
new.  I am able to connect and read my data.  The problem I was seeing  
is that even though I have received all the data, the remote server is  
not closing the connection.  So, when I see that I have content equal  
to Content-Length, I give a disconnect to the class.  I'm having a  
problem because I am not certain when it is safe to delete the  
instance of my socket class.  I delete when I think it is safe but at  
a later point, an event occurs and the code tries to do something with  
the class.   Is there something I need to do so that the remote will  
disconnect, thereby causing the disconnect signal to fire?  Or is  
there some signal that I can key off of that will let me know that the  
class can be deleted.

I have tried so many permutations and at this point, I would welcome  
any help.

Thanks,
Michael

--
 [ signature omitted ] 

Message 2 in thread

On Tuesday 29 April 2008 17:53:38 Michael Simpson wrote:
> Consider a class subclassed from QSslSocket and hitting an HTTPS
> server.  
> (I can't use the HTTP class because I need to setup custom 
> headers.)

it does support that.

http://doc.trolltech.com/4.3/qhttp.html#request

>
> My class is pointer based, i.e. it is dynamically allocated using
> new.  I am able to connect and read my data.  The problem I was seeing
> is that even though I have received all the data, the remote server is
> not closing the connection.  

see section 8.1.2.1 of the http spec.
the server is allowed to assume you want a persistent connection, if you 
didn't  send a header"Connection: close\r\n"

> So, when I see that I have content equal 
> to Content-Length, I give a disconnect to the class.  I'm having a
> problem because I am not certain when it is safe to delete the
> instance of my socket class.  I delete when I think it is safe but at
> a later point, an event occurs and the code tries to do something with
> the class. 

http://doc.trolltech.com/4.3/qobject.html#deleteLater




-- 
 [ signature omitted ] 

Message 3 in thread

Arvid.  Perfect!  Thank you.

I will add that to my header.

M

On Apr 29, 2008, at 9:08 AM, Arvid Ephraim Picciani wrote:

> On Tuesday 29 April 2008 17:53:38 Michael Simpson wrote:
>> Consider a class subclassed from QSslSocket and hitting an HTTPS
>> server.
>> (I can't use the HTTP class because I need to setup custom
>> headers.)
>
> it does support that.
>
> http://doc.trolltech.com/4.3/qhttp.html#request
>
>>
>> My class is pointer based, i.e. it is dynamically allocated using
>> new.  I am able to connect and read my data.  The problem I was  
>> seeing
>> is that even though I have received all the data, the remote server  
>> is
>> not closing the connection.
>
> see section 8.1.2.1 of the http spec.
> the server is allowed to assume you want a persistent connection, if  
> you
> didn't  send a header"Connection: close\r\n"
>
>> So, when I see that I have content equal
>> to Content-Length, I give a disconnect to the class.  I'm having a
>> problem because I am not certain when it is safe to delete the
>> instance of my socket class.  I delete when I think it is safe but at
>> a later point, an event occurs and the code tries to do something  
>> with
>> the class.
>
> http://doc.trolltech.com/4.3/qobject.html#deleteLater
>
>
>
>
> -- 
> best regards/Mit freundlichen Grüßen
> Arvid Ephraim Picciani
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx  
> with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>

--
 [ signature omitted ] 

Message 4 in thread

On Tuesday 29 April 2008 18:08:10 Arvid Ephraim Picciani wrote:
> On Tuesday 29 April 2008 17:53:38 Michael Simpson wrote:
> > Consider a class subclassed from QSslSocket and hitting an HTTPS
> > server.
> > (I can't use the HTTP class because I need to setup custom
> > headers.)
>
> it does support that.
>
> http://doc.trolltech.com/4.3/qhttp.html#request

And:
http://doc.trolltech.com/4.4/qnetworkrequest.html#setHeader
http://doc.trolltech.com/4.4/qnetworkrequest.html#setRawHeader

> > My class is pointer based, i.e. it is dynamically allocated using
> > new.  I am able to connect and read my data.  The problem I was seeing
> > is that even though I have received all the data, the remote server is
> > not closing the connection.
>
> see section 8.1.2.1 of the http spec.
> the server is allowed to assume you want a persistent connection, if you
> didn't  send a header"Connection: close\r\n"

HTTP/1.0 connections default to "Connection: close". But HTTP/1.1 defaults 
to "Connection: keep-alive".

This means you explicitly sent an HTTP/1.1 request and now you're surprised 
that you got 1.1 behaviour :-)

> > So, when I see that I have content equal
> > to Content-Length, I give a disconnect to the class.  I'm having a
> > problem because I am not certain when it is safe to delete the
> > instance of my socket class.  I delete when I think it is safe but at
> > a later point, an event occurs and the code tries to do something with
> > the class.
>
> http://doc.trolltech.com/4.3/qobject.html#deleteLater

Correct. You're probably deleting the class from a slot connected to a signal 
in that same object. That is generally a very bad idea.

I would recommend the following alternatives, in order of preference:
1) Use Network Access API, new in 4.4, along with the functions I mentioned 
above to set your headers. The new framework is HTTP/1.1 compliant and, as 
far as our tests could reveal, robust.

2) Use QHttp with the function Arvid mentioned. Be careful with QHttp, since 
it is fragile. Try not to do much from inside the slots connected to its 
signals. In special, do not delete it there!

3) If you're only ever going to send one request to the server, 
add "Connection: close" to the headers. But don't delete the class from 
inside its own code.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.