Skip to content

Commit 5c2a5c3

Browse files
committed
初始提交
0 parents  commit 5c2a5c3

26 files changed

+1404
-0
lines changed

README

Whitespace-only changes.

build.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project name="movie_spider-compile" default="compile1">
2+
<target name="compile1">
3+
<javac srcdir="./src"
4+
destdir="./build"
5+
debug="on"
6+
source="1.7"
7+
target="1.7">
8+
</javac>
9+
<jar destfile="./dist/proxy.jar" basedir="./build">
10+
<manifest>
11+
<attribute name="Main-Class"
12+
value="com.meituan.tools.proxy.DemoStarter"/>
13+
</manifest>
14+
</jar>
15+
</target>
16+
</project>

dist/proxy.jar

34.8 KB
Binary file not shown.

dist/start.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
java -Xmx200M -ea -cp ./proxy.jar com.meituan.tools.proxy.JavaHttpProxy null 58088 ./files

src/com/meituan/service/GarUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.meituan.service;
2+
3+
import java.io.*;
4+
5+
public class GarUtils {
6+
7+
public static int transfer(InputStream input,OutputStream output) throws IOException {
8+
return transfer(input,output,-1);
9+
}
10+
11+
public static int transfer(InputStream input,OutputStream output,long maxLength) throws IOException {
12+
if(maxLength<0) {
13+
maxLength=Long.MAX_VALUE;
14+
}
15+
byte[] buffer=new byte[204800];
16+
int max=buffer.length;
17+
if(max>maxLength) {
18+
max= (int) maxLength;
19+
}
20+
int size;
21+
int total=0;
22+
while((size=input.read(buffer,0,max))>=0) {
23+
output.write(buffer,0,size);
24+
maxLength-=size;
25+
total+=size;
26+
if(maxLength<=0) {
27+
break;
28+
} else {
29+
if(max>maxLength) {
30+
max= (int) maxLength;
31+
}
32+
}
33+
}
34+
return total;
35+
}
36+
37+
}

src/com/meituan/tasks/Executors.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.meituan.tasks;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.concurrent.*;
6+
7+
public class Executors {
8+
9+
private static Executors instance;
10+
11+
public static Executors getInstance() {
12+
if(instance==null) {
13+
synchronized(Executors.class) {
14+
if(instance==null) {
15+
instance=new Executors();
16+
}
17+
}
18+
}
19+
return instance;
20+
}
21+
22+
private ExecutorService executor;
23+
24+
private Executors() {
25+
}
26+
27+
protected synchronized ExecutorService getExecutor() {
28+
if(executor==null) {
29+
final ThreadFactory parent= java.util.concurrent.Executors.defaultThreadFactory();
30+
ThreadFactory factory=new ThreadFactory() {
31+
@Override
32+
public Thread newThread(Runnable runnable) {
33+
Thread thread=parent.newThread(runnable);
34+
thread.setDaemon(true);
35+
return thread;
36+
}
37+
};
38+
executor= java.util.concurrent.Executors.newCachedThreadPool(factory);
39+
}
40+
return executor;
41+
}
42+
43+
public void shutdown() {
44+
if(executor!=null) {
45+
executor.shutdown();
46+
}
47+
}
48+
49+
public <T> Future<T> submitCommon(Callable<T> call) {
50+
return getExecutor().submit(call);
51+
}
52+
53+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.meituan.tools.proxy;
2+
3+
import com.meituan.tasks.Executors;
4+
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
import java.util.concurrent.Callable;
8+
9+
class AcceptThread implements Callable<Object> {
10+
11+
JavaHttpProxy parent;
12+
Logger logger= LoggerFactory.getLogger(this.getClass());
13+
14+
public AcceptThread(JavaHttpProxy parent) {
15+
this.parent=parent;
16+
}
17+
18+
@Override
19+
public Object call() throws Exception {
20+
ServerSocket serverSocket=parent.serverSocket;
21+
while(serverSocket.isBound()) {
22+
Socket socket=serverSocket.accept();
23+
logger.debug("connection from %s",socket.getRemoteSocketAddress());
24+
Executors.getInstance().submitCommon(new ProcessThread(parent,socket));
25+
}
26+
return this;
27+
}
28+
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.meituan.tools.proxy;
2+
3+
public interface Constants {
4+
5+
public static final String Host="Host";
6+
public static final String Server="Server";
7+
public static final String ContentLength="Content-Length";
8+
public static final String ContentType="Content-Type";
9+
public static final String ContentEncoding="Content-Encoding";
10+
public static final String TestHtml="this is <b>test</b> html!";
11+
public static final boolean Debug=false;
12+
public static final String TextHtml="text/html";
13+
public static final int DefaultPort=80;
14+
public static final String Connection="Connection";
15+
public static final String ProxyConnection="Proxy-Connection";
16+
public static final String ProxyServerName="zms-java-proxy";
17+
public static final boolean UseSysout=true;
18+
public static final byte[] LineBreak=new byte[]{'\r','\n'};
19+
20+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.meituan.tools.proxy;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
8+
public class DemoStarter extends JFrame implements ActionListener,Runnable {
9+
10+
public DemoStarter() throws HeadlessException {
11+
super("Java Http Proxy");
12+
this.args=new String[4];
13+
args[0]="null";
14+
args[1]="58088";
15+
args[2]="./files";
16+
args[3]="running";
17+
this.design();
18+
}
19+
20+
JButton button;
21+
String[] args;
22+
23+
protected void design() {
24+
Container panel = this.getContentPane();
25+
panel.setLayout(new BorderLayout());
26+
button=new JButton("已启动/退出");
27+
panel.add(button,BorderLayout.CENTER);
28+
button.addActionListener(this);
29+
Thread thread=new Thread(this);
30+
thread.setDaemon(true);
31+
thread.start();
32+
}
33+
34+
@Override
35+
public void actionPerformed(ActionEvent e) {
36+
args[3]=null;
37+
this.dispose();
38+
}
39+
40+
@Override
41+
public void run() {
42+
try {
43+
JavaHttpProxy.main(args);
44+
} catch (Throwable t) {
45+
t.printStackTrace();
46+
button.setText("错误/退出");
47+
}
48+
}
49+
50+
public static void main(String[] args) throws Exception {
51+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
52+
DemoStarter starter=new DemoStarter();
53+
starter.pack();
54+
starter.setLocation(640,480);
55+
starter.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
56+
starter.setVisible(true);
57+
}
58+
59+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.meituan.tools.proxy;
2+
3+
import java.io.InputStream;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
class HttpResponse implements Constants {
8+
9+
private String versionTok="HTTP/1.1";
10+
private int statucCode;
11+
private String statusMessage;
12+
private List<NameValuePair> headers;
13+
private byte[] body;
14+
public static String HeaderEncoding=ProxyThread.HeaderEncoding;
15+
private String sendEncoding=HeaderEncoding;
16+
private InputStream input;
17+
MyByteArrayOutputStream baos;
18+
19+
private HttpResponse() {
20+
headers=new ArrayList<>();
21+
headers.add(new NameValuePair(Server,ProxyServerName));
22+
}
23+
24+
public void setHeaders(List<NameValuePair> _headers) {
25+
this.headers.clear();
26+
this.headers.addAll(_headers);
27+
String server=ProxyUtils.getHeaderValue(this.headers,Server);
28+
if(server==null || server.length()<1) {
29+
ProxyUtils.setHeader(this.headers,Server,ProxyServerName);
30+
}
31+
}
32+
33+
public HttpResponse(int status,String message) {
34+
this();
35+
this.statucCode=status;
36+
this.statusMessage=message;
37+
}
38+
39+
public HttpResponse(StatusCode status) {
40+
this();
41+
this.statucCode=status.getCode();
42+
this.statusMessage=status.name();
43+
}
44+
45+
public HttpResponse(StatusCode status,byte[] body) {
46+
this(status);
47+
this.setBody(body);
48+
}
49+
50+
public HttpResponse(StatusCode status,byte[] body,String contentType) {
51+
this(status,body);
52+
ProxyUtils.setHeader(this.getHeaders(),ContentType,contentType);
53+
}
54+
55+
public String getVersionTok() {
56+
return versionTok;
57+
}
58+
59+
public void setVersionTok(String versionTok) {
60+
this.versionTok = versionTok;
61+
}
62+
63+
public int getStatucCode() {
64+
return statucCode;
65+
}
66+
67+
public void setStatucCode(int statucCode) {
68+
this.statucCode = statucCode;
69+
}
70+
71+
public String getStatusMessage() {
72+
return statusMessage;
73+
}
74+
75+
public void setStatusMessage(String statusMessage) {
76+
this.statusMessage = statusMessage;
77+
}
78+
79+
public List<NameValuePair> getHeaders() {
80+
return headers;
81+
}
82+
83+
public byte[] getBody() {
84+
return body;
85+
}
86+
87+
public void setBody(byte[] body) {
88+
this.body = body;
89+
}
90+
91+
public String getSendEncoding() {
92+
return sendEncoding;
93+
}
94+
95+
public InputStream getInput() {
96+
return input;
97+
}
98+
99+
public void setInput(InputStream input) {
100+
this.input = input;
101+
}
102+
103+
public void setSendEncoding(String sendEncoding) {
104+
this.sendEncoding = sendEncoding;
105+
}
106+
107+
}

0 commit comments

Comments
 (0)