[Libwebsockets] 答复: a question libwebsockets client for 302 redirection

Andy Green andy at warmcat.com
Sun Nov 1 09:35:46 CET 2020



On 10/31/20 11:41 AM, huangkaicheng wrote:
> Sorry, maybe I doesn’t describe my question clearly just now.
> 
> Server address is 121.37.223.76. ws client connect to 
> server(121.37.223.76) when http handshaking. At the moment, Server 

OK...

> response with 302 reason code to make ws client connect another 
> address(58.222.56.63) with 

OK...

> “v1/rtc/3423?appid=123213&userid=huangkaicheng&origindomain=mytest.com”. 

So handling client redirects is a bit tricky, we already committed to 
using a specific wsi for the connection and told the user code what it 
is while it starts to do the DNS lookup and the tcp / tls connection, 
and we return from the client connection creation api.

If in fact we completed an http transaction on the wsi, and what we 
learned when we did it is that it wants us to do another one (which is 
completely different than a generic http transaction flow), this makes a 
problem for using a single wsi.  The relocation may be to a different 
server or a different path.

We could start a new wsi to do the new connection, but the user code 
doesn't expect that, he has been told it's coming on the old wsi, and he 
may have configured quite a lot of user code around that (C++ wrappers etc).

To square the circle, we have an internal api that "resets" the original 
wsi and fills it back up with info that was parsed out of the relocation 
  Location: header, something similar happens with parsing data out of 
the client connection info struct and into "fake ah headers".  That 
covers stuff like the dns address string but also the new path part.

In both cases we have the information in an ephermeral form, either from 
the client connection info struct which may be on the stack, or from the 
Location: header, and copy them into ah entries, so they have a place to 
live while the wsi connection proceeds asynchronously.

> ws can still redirect well with redirect url. But It seems that there is 
> no way to get this redirect url(path) for caller when server response a 
> redirection. In this situation, How can get the redirect url from server 
> ? Is exist api or callback to get redirection info?

For the relocated path, we put it in _WSI_TOKEN_CLIENT_URI, it looks 
like you should be able to get it out of there using the normal lws_hdr apis

https://libwebsockets.org/git/libwebsockets/tree/lib/roles/http/client/client-http.c#n1643

We also stash it in _WSI_TOKEN_CLIENT_URI from the client connection 
info, I didn't try it but it should be OK to use that header and get the 
correct path part for both nonrelocated and relocated transactions.

-Andy

> redirect ads='58.222.56.63', port=443, 
> path='v1/rtc/3423?appid=123213&userid=huangkaicheng&origindomain=mytest.com', 
> ssl = 1, pifds 0
> 
> *发件人:*huangkaicheng
> *发送时间:*2020年10月31日17:38
> *收件人:*'libwebsockets' <libwebsockets at ml.libwebsockets.org>
> *抄送:*Chenyake <chenyake at huawei.com>
> *主题:*a question libwebsockets client for(302 redirect)
> 
> Hi,
> 
> Is there a way to get the path of redirect when ws client handshake,we 
> meet  as follow ?  If not ,Could we add a callback to get this info. 
> Thanks to anwser my question.
> 
> redirect ads='58.222.56.63', port=443, 
> path='v1/rtc/3423?appid=123213&userid=huangkaicheng&origindomain=mytest.com', 
> ssl = 1, pifds 0
> 
> imap://andy@warmcat.com:993/fetch%3EUID%3E.INBOX%3E129515?header=quotebody&part=1.1.2&filename=image001.png
> 
> 
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets
> 


More information about the Libwebsockets mailing list