Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[#500] Fix getRemoteAddress for IpV6

  • Loading branch information...
commit 547d67e022270c5f375944fe87de372a9b411298 1 parent 8ba83e1
Guillaume Bort authored December 21, 2010

Showing 1 changed file with 18 additions and 21 deletions. Show diff stats Hide diff stats

  1. 39  framework/src/play/server/PlayHandler.java
39  framework/src/play/server/PlayHandler.java
@@ -49,9 +49,7 @@
49 49
 public class PlayHandler extends SimpleChannelUpstreamHandler {
50 50
 
51 51
     final ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
52  
-
53 52
     private final static String signature = "Play! Framework;" + Play.version + ";" + Play.mode.name().toLowerCase();
54  
-
55 53
     /**
56 54
      * If true (the default), Play will send the HTTP header "Server: Play! Framework; ....".
57 55
      * This could be a security problem (old versions having publicly known security bugs), so you can
@@ -59,8 +57,7 @@
59 57
      */
60 58
     private final static boolean exposePlayServer;
61 59
 
62  
-    static
63  
-    {
  60
+    static {
64 61
         exposePlayServer = !"false".equals(Play.configuration.getProperty("http.exposePlayServer"));
65 62
     }
66 63
 
@@ -102,7 +99,6 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Ex
102 99
         }
103 100
         Logger.trace("messageReceived: end");
104 101
     }
105  
-
106 102
     private static final Map<String, RenderStatic> staticPathsCache = new HashMap<String, RenderStatic>();
107 103
 
108 104
     public class NettyInvocation extends Invoker.Invocation {
@@ -395,7 +391,7 @@ public void copyResponse(ChannelHandlerContext ctx, Request request, Response re
395 391
             if (!keepAlive) {
396 392
                 writeFuture.addListener(ChannelFutureListener.CLOSE);
397 393
             }
398  
-        } else if(stream != null) {
  394
+        } else if (stream != null) {
399 395
             ChannelFuture writeFuture = ctx.getChannel().write(nettyResponse);
400 396
             if (!nettyRequest.getMethod().equals(HttpMethod.HEAD) && !nettyResponse.getStatus().equals(HttpResponseStatus.NOT_MODIFIED)) {
401 397
                 final ChunkedInput originalStream = stream;
@@ -408,14 +404,14 @@ public boolean hasNextChunk() throws Exception {
408 404
 
409 405
                     public Object nextChunk() throws Exception {
410 406
                         Object nextChunk = originalStream.nextChunk();
411  
-                        if(nextChunk == null) {
  407
+                        if (nextChunk == null) {
412 408
                             return null;
413 409
                         }
414  
-                        if(nextChunk instanceof String) {
415  
-                            return wrappedBuffer(((String)nextChunk).getBytes());
  410
+                        if (nextChunk instanceof String) {
  411
+                            return wrappedBuffer(((String) nextChunk).getBytes());
416 412
                         }
417  
-                        if(nextChunk instanceof byte[]) {
418  
-                            return wrappedBuffer(((byte[])nextChunk));
  413
+                        if (nextChunk instanceof byte[]) {
  414
+                            return wrappedBuffer(((byte[]) nextChunk));
419 415
                         }
420 416
                         return nextChunk;
421 417
                     }
@@ -427,9 +423,9 @@ public boolean isEndOfInput() throws Exception {
427 423
                     public void close() throws Exception {
428 424
                         originalStream.close();
429 425
                     }
430  
-
431 426
                 };
432 427
                 originalStream.addListener(new ChunkedInput.ChunkedInputListener() {
  428
+
433 429
                     public void onNewChunks() {
434 430
                         chunkedWriteHandler.resumeTransfer();
435 431
                     }
@@ -450,13 +446,11 @@ public void onNewChunks() {
450 446
 
451 447
     static String getRemoteIPAddress(ChannelHandlerContext ctx) {
452 448
         String fullAddress = ((InetSocketAddress) ctx.getChannel().getRemoteAddress()).getAddress().getHostAddress();
453  
-        // Address resolves to /x.x.x.x:zzzz we only want x.x.x.x
454  
-        if (fullAddress.startsWith("/")) {
  449
+        if (fullAddress.matches("/[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+[:][0-9]+")) {
455 450
             fullAddress = fullAddress.substring(1);
456  
-        }
457  
-        int i = fullAddress.indexOf(":");
458  
-        if (i != -1) {
459  
-            fullAddress = fullAddress.substring(0, i);
  451
+            fullAddress = fullAddress.substring(0, fullAddress.indexOf(":"));
  452
+        } else if (fullAddress.matches(".*[%].*")) {
  453
+            fullAddress = fullAddress.substring(0, fullAddress.indexOf("%"));
460 454
         }
461 455
         return fullAddress;
462 456
     }
@@ -466,14 +460,14 @@ public static Request parseRequest(ChannelHandlerContext ctx, HttpRequest nettyR
466 460
         Logger.trace("parseRequest: URI = " + nettyRequest.getUri());
467 461
         int index = nettyRequest.getUri().indexOf("?");
468 462
         String querystring = "";
469  
-        
  463
+
470 464
         String uri = nettyRequest.getUri();
471 465
         // Remove domain and port from URI if it's present.
472 466
         if (uri.startsWith("http://") || uri.startsWith("https://")) {
473 467
             // Begins searching / after 9th character (last / of https://)
474 468
             uri = uri.substring(uri.indexOf("/", 9));
475 469
         }
476  
-        
  470
+
477 471
         String path = URLDecoder.decode(uri, "UTF-8");
478 472
         if (index != -1) {
479 473
             path = URLDecoder.decode(uri.substring(0, index), "UTF-8");
@@ -592,7 +586,10 @@ protected static void addToRequest(HttpRequest nettyRequest, Request request) {
592 586
 
593 587
     @Override
594 588
     public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
595  
-        try { e.getChannel().close(); } catch(Exception ex) { } 
  589
+        try {
  590
+            e.getChannel().close();
  591
+        } catch (Exception ex) {
  592
+        }
596 593
     }
597 594
 
598 595
     public static void serve404(NotFound e, ChannelHandlerContext ctx, Request request, HttpRequest nettyRequest) {

0 notes on commit 547d67e

Please sign in to comment.
Something went wrong with that request. Please try again.