View Javadoc
1   /*
2    * Copyright 2015 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty5.example.http2.tiles;
18  
19  import java.util.Random;
20  
21  import static io.netty5.util.CharsetUtil.UTF_8;
22  
23  /**
24   * Static and dynamically generated HTML for the example.
25   */
26  public final class Html {
27  
28      public static final String IP = System.getProperty("ip", "127.0.0.1");
29  
30      public static final byte[] FOOTER = "</body></html>".getBytes(UTF_8);
31  
32      public static final byte[] HEADER = ("<!DOCTYPE html><html><head lang=\"en\"><title>Netty HTTP/2 Example</title>"
33              + "<style>body {background:#DDD;} div#netty { line-height:0;}</style>"
34              + "<link rel=\"shortcut icon\" href=\"about:blank\">"
35              + "<meta charset=\"UTF-8\"></head><body>A grid of 200 tiled images is shown below. Compare:"
36              + "<p>[<a href='https://" + url(Http2Server.PORT) + "?latency=0'>HTTP/2, 0 latency</a>] [<a href='http://"
37              + url(HttpServer.PORT) + "?latency=0'>HTTP/1, 0 latency</a>]<br/>" + "[<a href='https://"
38              + url(Http2Server.PORT) + "?latency=30'>HTTP/2, 30ms latency</a>] [<a href='http://" + url(HttpServer.PORT)
39              + "?latency=30'>HTTP/1, 30ms latency</a>]<br/>" + "[<a href='https://" + url(Http2Server.PORT)
40              + "?latency=200'>HTTP/2, 200ms latency</a>] [<a href='http://" + url(HttpServer.PORT)
41              + "?latency=200'>HTTP/1, 200ms latency</a>]<br/>" + "[<a href='https://" + url(Http2Server.PORT)
42              + "?latency=1000'>HTTP/2, 1s latency</a>] [<a href='http://" + url(HttpServer.PORT)
43              + "?latency=1000'>HTTP/1, " + "1s latency</a>]<br/>").getBytes(UTF_8);
44  
45      private static final int IMAGES_X_AXIS = 20;
46  
47      private static final int IMAGES_Y_AXIS = 10;
48  
49      private Html() {
50      }
51  
52      private static String url(int port) {
53          return IP + ":" + port + "/http2";
54      }
55  
56      public static byte[] body(int latency) {
57          int r = Math.abs(new Random().nextInt());
58          // The string to be built contains 13192 fixed characters plus the variable latency and random cache-bust.
59          int numberOfCharacters = 13192 + stringLength(latency) + stringLength(r);
60          StringBuilder sb = new StringBuilder(numberOfCharacters).append("<div id=\"netty\">");
61          for (int y = 0; y < IMAGES_Y_AXIS; y++) {
62              for (int x = 0; x < IMAGES_X_AXIS; x++) {
63                  sb.append("<img width=30 height=29 src='/http2?x=")
64                  .append(x)
65                  .append("&y=").append(y)
66                  .append("&cachebust=").append(r)
67                  .append("&latency=").append(latency)
68                  .append("'>");
69              }
70              sb.append("<br/>\r\n");
71          }
72          sb.append("</div>");
73          return sb.toString().getBytes(UTF_8);
74      }
75  
76      private static int stringLength(int value) {
77          return Integer.toString(value).length() * IMAGES_X_AXIS * IMAGES_Y_AXIS;
78      }
79  }