-
Notifications
You must be signed in to change notification settings - Fork 1
/
JServer.java
97 lines (86 loc) · 3.27 KB
/
JServer.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
87
88
89
90
91
92
93
94
95
96
97
package amap.faas.demo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.Thread;
import java.net.ServerSocket;
import java.net.Socket;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpResponse;
import org.apache.commons.codec.binary.Base64;
public class JServer {
private ServerSocket s;
public JServer(int port) {
try {
s = new ServerSocket(port);
} catch(IOException ioe) {}
while (true) {
Socket c = null;
try {
c = s.accept();
} catch(IOException ioe) {}
new Handler(c).start();
}
}
private class Handler extends Thread {
private Socket sock;
public Handler (Socket c) {
sock = c;
}
public void run () {
try {
InputStream is = sock.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
OutputStream os = sock.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String command[] = br.readLine().split("\\s+");
if (3 == command.length && "GET".equals(command[0])) {
System.out.printf("GET %s\n", command[1]);
String path = command[1];
String strResult = "succ";
String type = "text/plain";
bw.write("HTTP/1.0 200 Ok");
bw.newLine();
bw.write(String.format("Content-Type: %s", type));
bw.newLine();
bw.write(String.format("Content-Length: %d", strResult.length()));
bw.newLine();
bw.write(String.format("Access-Control-Allow-Origin: *"));
bw.newLine();
bw.newLine();
bw.write(strResult);
bw.flush();
} else {
bw.write("HTTP/1.1 400 Bad Request");
bw.newLine();
bw.write(String.format("Access-Control-Allow-Origin: *"));
bw.newLine();
bw.newLine();
bw.write("arg num is not right");
bw.flush();
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
try {
sock.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
}
public static void main (String args[]) {
System.out.println("JServer started!");
new JServer(80);
}
}