-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServer.java
executable file
·88 lines (71 loc) · 2.45 KB
/
WebServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
/**
* Created by lingduokong on 1/24/15.
*/
public class WebServer {
/*
get the port number from command argument
author: Junchi Liang
*/
public static void main(String[] args) throws IOException {
int i, http_port = -1, ssl_port = -1;
String http_port_pattern_str = "--serverPort=[0-9]+";
String ssl_port_pattern_str = "--sslServerPort=[0-9]+";
Pattern http_port_pattern = Pattern.compile(http_port_pattern_str);
Pattern ssl_port_pattern = Pattern.compile(ssl_port_pattern_str);
for (i = 0; i < args.length; i++)
{
Matcher port_matcher = http_port_pattern.matcher(args[i]);
if (port_matcher.find())
{
try
{
http_port = Integer.parseInt(args[i].substring(13));
} catch (NumberFormatException e)
{
break;
}
}
}
for (i = 0; i < args.length; i++)
{
Matcher port_matcher = ssl_port_pattern.matcher(args[i]);
if (port_matcher.find())
{
try
{
ssl_port = Integer.parseInt(args[i].substring(16));
} catch (NumberFormatException e)
{
break;
}
}
}
System.out.println("http port:" + http_port + " SSL port:" + ssl_port);
if (http_port < 0 && ssl_port < 0) {
System.out.println("please use --serverPort option or --sslServerPort option (or both)");
return;
}
if (http_port >= 0) {
MySingleServer http_server = new MySingleServer(http_port, "http");
System.out.println("run a HTTP server");
http_server.start();
}
if (ssl_port >= 0) {
MySingleServer ssl_server = new MySingleServer(ssl_port, "ssl");
System.out.println("run a HTTPS server");
ssl_server.start();
}
while (true) {
String user_input;
Scanner scanner = new Scanner(System.in);
user_input = scanner.next();
if (user_input.compareTo("exit") == 0)
break;
}
System.out.println("server end");
}
}