1 | !636 |
2 | |
3 | import java.util.*; |
4 | import java.io.*; |
5 | import java.net.*; |
6 | |
7 | import java.nio.ByteBuffer; |
8 | import java.nio.channels.FileChannel; |
9 | import java.nio.charset.Charset; |
10 | import java.security.KeyStore; |
11 | import java.text.SimpleDateFormat; |
12 | import java.util.logging.Level; |
13 | import java.util.logging.Logger; |
14 | import java.util.regex.Matcher; |
15 | import java.util.regex.Pattern; |
16 | import java.util.zip.GZIPOutputStream; |
17 | |
18 | import javax.net.ssl.*; |
19 | |
20 | main { |
21 | static class MyHTTPD extends NanoHTTPD { |
22 | |
23 | /** |
24 | * Constructs an HTTP server on given port. |
25 | */ |
26 | public MyHTTPD(int port) throws IOException { |
27 | super(port); |
28 | } |
29 | |
30 | |
31 | @Override |
32 | public Response serve( String uri, Method method, |
33 | Map<String, String> header, Map<String, String> parms, |
34 | Map<String, String> files ) |
35 | { |
36 | System.out.println( method + " '222" + uri + "' " ); |
37 | |
38 | String ip = header.get("remote-addr"); |
39 | if (!ip.equals("127.0.0.1")) |
40 | peers.add(ip); |
41 | |
42 | if (uri.equals("/time")) |
43 | return newFixedLengthResponse(time24()); |
44 | |
45 | String msg = "<html><body><h1>Hello server</h1>\n"; |
46 | if ( parms.get("username") == null ) |
47 | msg += |
48 | "<form action='?' method='get'>\n" + |
49 | " <p>Your name: <input type='text' name='username'></p>\n" + |
50 | "</form>\n"; |
51 | else |
52 | msg += "<p>Hello, " + parms.get("username") + "!</p>"; |
53 | |
54 | msg += "uri: " + uri + "<br>"; |
55 | new List<String> links; |
56 | for (String p : peers) |
57 | links.add("<a href=\"http://" + p + ":8888\">" + p + "</a>"); |
58 | msg += "All known peers: " + join(", ", links); |
59 | msg += "</body></html>\n"; |
60 | return newFixedLengthResponse(msg); |
61 | } |
62 | |
63 | static String join(String glue, Iterable<String> list) { |
64 | new StringBuilder buf; |
65 | boolean first = true; |
66 | for (String s : list) { |
67 | if (first) first = false; |
68 | else buf.append(glue); |
69 | buf.append(s); |
70 | } |
71 | return buf.toString(); |
72 | } |
73 | |
74 | Response serveBinary(String path) { |
75 | System.out.println("Serving " + path); |
76 | long length = new File(path).length(); |
77 | System.out.println("File size: " + length); |
78 | FileInputStream fis = null; |
79 | try { |
80 | fis = new FileInputStream(path); |
81 | } catch (FileNotFoundException e) { |
82 | // TODO Auto-generated catch block |
83 | e.printStackTrace(); |
84 | } |
85 | return new Response(Status.OK, "application/binary", fis, length); |
86 | } |
87 | } |
88 | |
89 | private static MyHTTPD server; |
90 | private static int port = 8888; |
91 | |
92 | static new (Tree)Set<String> peers; |
93 | |
94 | psvm { |
95 | server = new MyHTTPD(port); |
96 | server.start(); |
97 | System.out.println("HTTP server started (listening on port " + port + "!)"); |
98 | printMyIPs(); |
99 | server.join(); // keep main thread alive |
100 | } |
101 | |
102 | static void printMyIPs() { |
103 | String ip; |
104 | try { |
105 | Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); |
106 | while (interfaces.hasMoreElements()) { |
107 | NetworkInterface iface = interfaces.nextElement(); |
108 | // filters out 127.0.0.1 and inactive interfaces |
109 | if (iface.isLoopback() || !iface.isUp()) |
110 | continue; |
111 | |
112 | Enumeration<InetAddress> addresses = iface.getInetAddresses(); |
113 | while(addresses.hasMoreElements()) { |
114 | InetAddress addr = addresses.nextElement(); |
115 | if (addr instanceof Inet4Address) { // we still stick to the old stuff... until the new stuff comes along! |
116 | ip = addr.getHostAddress(); |
117 | System.out.println("\nPlease visit http://" + ip + ":" + port + " to see me!"); |
118 | } |
119 | } |
120 | } |
121 | } catch (Throwable e) { |
122 | e.printStackTrace(); |
123 | } |
124 | } |
125 | |
126 | static String time24() { |
127 | SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.US); |
128 | //dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); |
129 | return dateFormat.format(new Date()); |
130 | } |
131 | } |
132 | |
133 | !include #1000433 // class NanoHTTP |
Began life as a copy of #1000596
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000695 |
Snippet name: | Serve clock time under /time |
Eternal ID of this version: | #1000695/1 |
Text MD5: | f44d9fb33025a64ee39ec83805d77acf |
Author: | stefan |
Category: | javax android |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-08-22 22:42:25 |
Source code size: | 4111 bytes / 133 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 738 / 632 |
Referenced in: | [show references] |