5.9 Limax Http


  • 5.9.4 HttpServer class

    • 1. The create method of HttpServer is used to create an Http server on a host port. An Https server can be created by calling the method containing the SSLContext parameter. For applications, HttpExchange and WebSocketExchange can call the getSSLSession method to know whether it is running in http or https context.

    • 2. The createHost method of HttpServer uses a domain name to create a virtual host. HttpServer itself is the default virtual host. The second createHost on the same domain name is equivalent to getHost.

    • 3. The virtual host's createContext method installs an HttpHandler or WebSocketHandler on a path, and the removeContext removes the corresponding Handler on the path.

    • 4. The get and set methods of HttpServer are used to access the configuration parameters defined by HttpServer.Parameter. There are too many configuration parameters and the type is also more complicated. Usually the default parameters should be enough, please refer to the source code to modify.

    • 5. The start and stop methods of HttpServer are used to start and stop the server. Repeated start and stop will cause an UnsupportedOperationException exception.

    • 6. The register method of HttpServer can register a Closeable, which is called when the server is stopped.

    • 7. The createFileSystemHandler method of HttpServer is used to create a static file server handler, and the effect is basically equivalent to a common static file server. There are many parameters: htdocs specifies the file server root directory; textCharset specifies the text file character set; mmapThreshold specifies the size threshold of the file mapping, the file data smaller than the threshold is directly read into the memory, otherwise the file mapping is used; compressThreshold specifies the file data compression threshold, compression size divided by the original size is less than the threshold means that the compression effectively uses the compressed version. Otherwise, the uncompressed version is used. Whether the file is allowed to be compressed is determined by the enumeration type of limax.http.ContentType. Generally, the text content is allowed to be compressed, and the image is not compressed; the indexes array specify the default index file by default, usually should be configured as index.html and index.htm; if encountering directory access, there is no default file specified by indexes in the directory, browseDir determines whether to allow browsing the directory. If it is not allowed to respond to 403FORBIDDEN; browseDirExceptions specifies those directories as exceptions.


  • 5.9.5 Example

    • 1. In the source directory, demo/httpserver provides an example that can be adapted to the specific environment.

    • 2. If wanting to test https, first install the root certificate ca.p7b, and then give the correct path to localhost.p12 in the code. After running the server, the browser can access it through https://localhost/. (Also refer to the Appendix PKIX support to establish own certificate system).

    • 3. The example provides a static http server that can be experimented with creating static websites. Access /upload.html and /upload2.html can test file uploading and learn about the features of FormData. Access /websocket.html can experiment with websocket. Access /sse.html can test Server-Sent Events. Access /exception can test the case where the handle throws an exception.Access /async can test asynchronous response.

    • 4. The server supports HTTP/1.1 upgrade to HTTP/2, but currently there seems to be no client support for this capability.

    • 5. If want to test HTTP/2 without encryption, can only search and install a command line program nghttp. Currently, no browser supports HTTP/2 without encryption.

    • 6. If the https configuration is complete, running the server with JDK8 will not support HTTP/2, because the JDK8 SSLEngine does not support ALPN.

    • 7. With JDK above 8 run server and can support HTTP/2, but the current SSLEngine of JDK11 has a bug. If use Firefox to access and then refresh frequently, the SSL handshake method will fall into an infinite loop. JDK13 can pass the test.


  • 5.9.6 Advanced topic

    • Server model

      • 1. The server adopts a complete asynchronous model (a protocol such as HTTP/2 can make a large number of concurrent requests on a connection. If a request corresponds to one thread executing service, a small number of user connections consume a large amount of server threads), and the hash value of the request is calculated to select the thread executing service. Service handles should not be blocked as much as possible. If there is a database IO, use cache as much as possible. In heavy load, should set a larger service thread pool size.

      • 2. In order to be unified with other services of the limax framework, use Engine.open to start the network engine. The server parameter NETSERVER_WORKMODE is true to select the asynchronous network model, and false to select the poll network model.

      • 3. The nProcessors parameter of Engine.open determines the size of the network thread pool. The network thread pool is responsible for parsing all request data and delivering the closing message. The scheduler configured with the parameter protocolSchedulers is only used by the HTTP/2 timer. In the case where only the HTTP service is required, the configuration is usually not required to be too large. The parameter applicationExecutors determines the size of the service thread pool. The service thread pool is used for handle requests and performs flow control.


    • Data transmission

      • 1. HTTP/1.1 and HTTP/2 support flow control. FLOWCONTROL_WINDOW_SIZE determines the size of the flow control window.

      • 2. Sending a large amount of data on WebSocket, event.type()==SENDREADY is used to determine the subsequent transmission to ensure correct flow control and prevent data accumulation. Interactive applications usually do not strictly follow the flow controlled.

      • 3. Similar to WebSocket, Server-Sent Events uses onSendReady for flow control.

      • 4. Within CONGESTION_TIMEOUT no consumption of the send buffer is considered as serious congestion, following close of the connection.


    • Data upload

      • 1. The HTTP protocol performs uploading through the POST method, but the design of the POST itself is too simple. If the POST is handled accidentally in the actual application, the malicious client can easily consume the server resources by using the POST. The biggest problem is the data size of POST.

      • 2. POST, which does not involve file uploading, is usually used to send some large-sized queries. In this case, the maximum size is appointed. The postLimit method of FormData can be used to simply limit it.

      • 3. POST involving file uploading is much more complicated. The file may be a few K, M, and G, or it may upload multiple files at a time. The postLimit is difficult to determine. (To avoid excessive memory overhead, the useTempFile method of FormData is still necessary for normal file uploads)

      • 4. In the example /upload2.html, the first input box should fill in an estimated upload size (the size of the uploaded file plus the size of the MIME header information added by the browser). If the size is too small, the upload fails.

      • 5. In actual applications, the upload details, such as the number of files and the file size, should be negotiated before the specific file is uploaded to estimate the total size. The result of the negotiation is shared between the negotiation service and the upload service. The client exchanges the time-sensitive key of the negotiation result to ensure that the upload service is correctly configured according to the negotiation result.

      • 6. The censor(HttpExchange exchange) method of HttpHandler is executed by the network thread, and the connection can be terminated as long as an exception is thrown. If necessary, more FormData reviews can be completed (Partial information that has been parsed can be access through FormData without full parsing). FormData also provides methods such as getBytesCount() and getCreateTime(), which can be used to accomplish tasks such as upload rate management, for example, to directly close a connection that is too slow. (If the upload is paused, the configuration parameters like HTTP11_REQUEST_TIMEOUT, HTTP2_IDLE_TIMEOUT still take effect)


Prev Next