Determine URL of client request from server side.

2019-09-11 02:05发布

问题:

Ref: Socket.io Client Request Origin URL

Ref: Socket.io - How to get client URL request on server side?

How do you determine the URL of client request from server side? Client request may come from multiple domains.

回答1:

socket.io stores the request object from the original request that initiated the socket.io connection in socket.request.

On this request object is:

request.url
request.headers

The .url property will be the path of the URL (everything after the protocol, hostname and port).

The .headers property will contain any headers on the original request. In a cooperating browser, if the request is a cross origin request, then there will be an origin header that tells you what the domain was of the web page that the request was initiated from (it may also be present on a same-origin request too). This "origin" header will be accurate when coming from a trusted browser such as Chrome, IE, Safari, Firefox, etc..., but can easily be spoofed if coming from any other sort of agent (such as another server or script).