View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2026 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix.demo.nanohttpd;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.net.InetAddress;
23  import java.net.ServerSocket;
24  import java.net.SocketAddress;
25  
26  import org.newsclub.net.unix.AFSocketAddress;
27  import org.newsclub.net.unix.AFUNIXSocketAddress;
28  import org.newsclub.net.unix.demo.DemoHelper;
29  import org.newsclub.net.unix.demo.okhttp.OkHttpClientDemo;
30  
31  import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
32  
33  import fi.iki.elonen.NanoHTTPD;
34  
35  /**
36   * Creates a {@link NanoHTTPD} server, bound to {@code /tmp/junixsocket-http-server.sock}.
37   *
38   * Http requests on that socket should return "Hello world from <hostname>".
39   *
40   * @author Christian Kohlschütter
41   * @see OkHttpClientDemo
42   */
43  @SuppressFBWarnings("UNENCRYPTED_SERVER_SOCKET")
44  public final class NanoHttpdServerDemo extends NanoHTTPD {
45  
46    public NanoHttpdServerDemo(SocketAddress socketAddress) throws IOException {
47      super(0);
48      setServerSocketFactory(new ServerSocketFactory() {
49  
50        @Override
51        public ServerSocket create() throws IOException {
52          if (socketAddress instanceof AFSocketAddress) {
53            return ((AFSocketAddress) socketAddress).newForceBoundServerSocket();
54          } else {
55            ServerSocket serverSocket = new ServerSocket();
56            serverSocket.bind(socketAddress);
57            return serverSocket;
58          }
59        }
60      });
61      System.out.println("Address: " + socketAddress);
62      if (socketAddress instanceof AFUNIXSocketAddress) {
63        System.out.println("Try: curl --unix-socket " + ((AFUNIXSocketAddress) socketAddress)
64            .getPath() + " http://localhost/");
65      }
66    }
67  
68    public static void main(String[] args) throws IOException {
69      SocketAddress addr = DemoHelper.parseAddress(args, //
70          AFUNIXSocketAddress.of(new File("/tmp/junixsocket-http-server.sock")));
71  
72      new NanoHttpdServerDemo(addr).start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
73    }
74  
75    @Override
76    public Response serve(IHTTPSession session) {
77      return newFixedLengthResponse("Hello world from " + getSystemHostname() + "\n");
78    }
79  
80    private static String getSystemHostname() {
81      try {
82        return InetAddress.getLocalHost().getHostName();
83      } catch (Exception e) {
84        e.printStackTrace();
85        return null;
86      }
87    }
88  }