@@ -272,17 +272,16 @@ public boolean isWebSocketEnabled() {
272
272
}
273
273
274
274
/**
275
- * Configure allowed {@code Origin} header values. This check is mostly designed for
276
- * browser clients. There is nothing preventing other types of client to modify the
277
- * {@code Origin} header value.
278
- *
279
- * <p>When SockJS is enabled and origins are restricted, transport types that do not
280
- * allow to check request origin (JSONP and Iframe based transports) are disabled.
281
- * As a consequence, IE 6 to 9 are not supported when origins are restricted.
282
- *
283
- * <p>Each provided allowed origin must start by "http://", "https://" or be "*"
284
- * (means that all origins are allowed).
285
- *
275
+ * Configure allowed {@code Origin} header values. This check is mostly
276
+ * designed for browsers. There is nothing preventing other types of client
277
+ * to modify the {@code Origin} header value.
278
+ * <p>When SockJS is enabled and origins are restricted, transport types
279
+ * that do not allow to check request origin (JSONP and Iframe based
280
+ * transports) are disabled. As a consequence, IE 6 to 9 are not supported
281
+ * when origins are restricted.
282
+ * <p>Each provided allowed origin must have a scheme, and optionally a port
283
+ * (e.g. "http://example.org", "http://example.org:9090"). An allowed origin
284
+ * string may also be "*" in which case all origins are allowed.
286
285
* @since 4.1.2
287
286
* @see <a href="https://tools.ietf.org/html/rfc6454">RFC 6454: The Web Origin Concept</a>
288
287
* @see <a href="https://github.com/sockjs/sockjs-client#supported-transports-by-browser-html-served-from-http-or-https">SockJS supported transports by browser</a>
@@ -319,6 +318,7 @@ public boolean shouldSuppressCors() {
319
318
return this .suppressCors ;
320
319
}
321
320
321
+
322
322
/**
323
323
* This method determines the SockJS path and handles SockJS static URLs.
324
324
* Session URLs and raw WebSocket requests are delegated to abstract methods.
@@ -342,68 +342,89 @@ public final void handleRequest(ServerHttpRequest request, ServerHttpResponse re
342
342
// As per SockJS protocol content-type can be ignored (it's always json)
343
343
}
344
344
345
- String requestInfo = logger .isDebugEnabled () ? request .getMethod () + " " + request .getURI () : "" ;
345
+ String requestInfo = (logger .isDebugEnabled () ? request .getMethod () + " " + request .getURI () : null );
346
+
346
347
try {
347
348
if (sockJsPath .equals ("" ) || sockJsPath .equals ("/" )) {
348
- logger .debug (requestInfo );
349
+ if (requestInfo != null ) {
350
+ logger .debug ("Processing transport request: " + requestInfo );
351
+ }
349
352
response .getHeaders ().setContentType (new MediaType ("text" , "plain" , UTF8_CHARSET ));
350
353
response .getBody ().write ("Welcome to SockJS!\n " .getBytes (UTF8_CHARSET ));
351
354
}
355
+
352
356
else if (sockJsPath .equals ("/info" )) {
353
- logger .debug (requestInfo );
357
+ if (requestInfo != null ) {
358
+ logger .debug ("Processing transport request: " + requestInfo );
359
+ }
354
360
this .infoHandler .handle (request , response );
355
361
}
362
+
356
363
else if (sockJsPath .matches ("/iframe[0-9-.a-z_]*.html" )) {
357
364
if (!this .allowedOrigins .isEmpty () && !this .allowedOrigins .contains ("*" )) {
358
- if (logger . isDebugEnabled () ) {
359
- logger .debug ("Iframe support is disabled when an origin check is required, ignoring " +
360
- requestInfo );
365
+ if (requestInfo != null ) {
366
+ logger .debug ("Iframe support is disabled when an origin check is required. " +
367
+ "Ignoring transport request: " + requestInfo );
361
368
}
362
369
response .setStatusCode (HttpStatus .NOT_FOUND );
363
370
return ;
364
371
}
365
372
if (this .allowedOrigins .isEmpty ()) {
366
373
response .getHeaders ().add (XFRAME_OPTIONS_HEADER , "SAMEORIGIN" );
367
374
}
368
- logger .debug (requestInfo );
375
+ if (requestInfo != null ) {
376
+ logger .debug ("Processing transport request: " + requestInfo );
377
+ }
369
378
this .iframeHandler .handle (request , response );
370
379
}
380
+
371
381
else if (sockJsPath .equals ("/websocket" )) {
372
382
if (isWebSocketEnabled ()) {
373
- logger .debug (requestInfo );
383
+ if (requestInfo != null ) {
384
+ logger .debug ("Processing transport request: " + requestInfo );
385
+ }
374
386
handleRawWebSocketRequest (request , response , wsHandler );
375
387
}
376
- else if (logger . isDebugEnabled () ) {
377
- logger .debug ("WebSocket disabled, ignoring " + requestInfo );
388
+ else if (requestInfo != null ) {
389
+ logger .debug ("WebSocket disabled. Ignoring transport request: " + requestInfo );
378
390
}
379
391
}
392
+
380
393
else {
381
394
String [] pathSegments = StringUtils .tokenizeToStringArray (sockJsPath .substring (1 ), "/" );
382
395
if (pathSegments .length != 3 ) {
383
396
if (logger .isWarnEnabled ()) {
384
- logger .warn ("Ignoring invalid transport request " + requestInfo );
397
+ logger .warn ("Invalid SockJS path '" + sockJsPath + "' - required to have 3 path segments" );
398
+ }
399
+ if (requestInfo != null ) {
400
+ logger .debug ("Ignoring transport request: " + requestInfo );
385
401
}
386
402
response .setStatusCode (HttpStatus .NOT_FOUND );
387
403
return ;
388
404
}
405
+
389
406
String serverId = pathSegments [0 ];
390
407
String sessionId = pathSegments [1 ];
391
408
String transport = pathSegments [2 ];
392
409
393
410
if (!isWebSocketEnabled () && transport .equals ("websocket" )) {
394
- if (logger . isDebugEnabled () ) {
395
- logger .debug ("WebSocket transport is disabled, ignoring " + requestInfo );
411
+ if (requestInfo != null ) {
412
+ logger .debug ("WebSocket disabled. Ignoring transport request: " + requestInfo );
396
413
}
397
414
response .setStatusCode (HttpStatus .NOT_FOUND );
398
415
return ;
399
416
}
400
417
else if (!validateRequest (serverId , sessionId , transport )) {
401
- if (logger . isWarnEnabled () ) {
402
- logger .warn ("Ignoring transport request " + requestInfo );
418
+ if (requestInfo != null ) {
419
+ logger .debug ("Ignoring transport request: " + requestInfo );
403
420
}
404
421
response .setStatusCode (HttpStatus .NOT_FOUND );
405
422
return ;
406
423
}
424
+
425
+ if (requestInfo != null ) {
426
+ logger .debug ("Processing transport request: " + requestInfo );
427
+ }
407
428
handleTransportRequest (request , response , wsHandler , sessionId , transport );
408
429
}
409
430
response .close ();
@@ -415,14 +436,16 @@ else if (!validateRequest(serverId, sessionId, transport)) {
415
436
416
437
protected boolean validateRequest (String serverId , String sessionId , String transport ) {
417
438
if (!StringUtils .hasText (serverId ) || !StringUtils .hasText (sessionId ) || !StringUtils .hasText (transport )) {
418
- logger .warn ("No server, session, or transport path segment" );
439
+ logger .warn ("No server, session, or transport path segment in SockJS request. " );
419
440
return false ;
420
441
}
442
+
421
443
// Server and session id's must not contain "."
422
444
if (serverId .contains ("." ) || sessionId .contains ("." )) {
423
445
logger .warn ("Either server or session contains a \" .\" which is not allowed by SockJS protocol." );
424
446
return false ;
425
447
}
448
+
426
449
return true ;
427
450
}
428
451
@@ -455,7 +478,9 @@ protected boolean checkAndAddCorsHeaders(ServerHttpRequest request, ServerHttpRe
455
478
}
456
479
457
480
if (!WebUtils .isValidOrigin (request , this .allowedOrigins )) {
458
- logger .debug ("Request rejected, Origin header value " + origin + " not allowed" );
481
+ if (logger .isWarnEnabled ()) {
482
+ logger .warn ("Origin header value '" + origin + "' not allowed." );
483
+ }
459
484
response .setStatusCode (HttpStatus .FORBIDDEN );
460
485
return false ;
461
486
}
@@ -535,8 +560,7 @@ public void handle(ServerHttpRequest request, ServerHttpResponse response) throw
535
560
}
536
561
}
537
562
else if (HttpMethod .OPTIONS .equals (request .getMethod ())) {
538
- if (checkAndAddCorsHeaders (request , response , HttpMethod .OPTIONS ,
539
- HttpMethod .GET )) {
563
+ if (checkAndAddCorsHeaders (request , response , HttpMethod .OPTIONS , HttpMethod .GET )) {
540
564
addCacheHeaders (response );
541
565
response .setStatusCode (HttpStatus .NO_CONTENT );
542
566
}
0 commit comments