*
* @see "Manager.prototype.handleHandshake"
* @param clientData
*/
public void handle(final ClientData clientData) {
final HttpServerRequest request = clientData.getRequest();
final Settings settings = manager.getSettings();
log.trace("In handshake handler");
MultiMap reqHeaders = request.headers();
final MultiMap resHeaders = request.response().headers();
String reqOrigin = reqHeaders.get("Origin");
resHeaders.add("CONTENT_TYPE", "text/plain; charset=UTF-8");
if (!this.verifyOrigin(reqOrigin)) {
manager.writeError(request, 403, "handshake bad origin");
return;
}
final HandshakeData handshakeData = new HandshakeData(clientData);
if (reqOrigin != null) {
// https://developer.mozilla.org/En/HTTP_Access_Control
resHeaders.add("Access-Control-Allow-Origin", reqOrigin);
resHeaders.add("Access-Control-Allow-Credentials", "true");
}
authorize(request, handshakeData, new AuthorizationCallback() {
public void handle(Exception e, boolean isAuthorized) {
if(e != null) {
manager.writeError(request, e);
return;
}
if(isAuthorized) {
String id = UUID.randomUUID().toString();
String result = id + ":"
+ (settings.isHeartbeats() ? settings.getHeartbeatTimeout() : "") + ":"
+ settings.getCloseTimeout() + ":"
+ settings.getTransports();
request.response().setStatusCode(200);
String jsonp = clientData.getParams().get("jsonp");
if(jsonp != null) {
result = "io.j[" + jsonp + "](\"" + result + "\");";
resHeaders.add("Content-Type", "application/javascript");
}
request.response().end(result);
manager.getHandshaken().put(id, handshakeData);
// self.store.publish('handshake', id, newData || handshakeData);
log.info("handshake authorized " + id);
} else {