Skip to content

Commit

Permalink
Start of web pass-through proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
daneren2005 committed Mar 10, 2014
1 parent 22361c6 commit 264e853
Showing 1 changed file with 149 additions and 0 deletions.
149 changes: 149 additions & 0 deletions src/github/daneren2005/serverproxy/WebProxy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
This file is part of ServerProxy.
SocketProxy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Subsonic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Subsonic. If not, see <http://www.gnu.org/licenses/>.
Copyright 2014 (C) Scott Jackson
*/

package github.daneren2005.serverproxy;

import android.content.Context;
import android.util.Log;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicHttpRequest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class WebProxy extends ServerProxy {
private static String TAG = WebProxy.class.getSimpleName();

public WebProxy(Context context) {
super(context);
}

@Override
ProxyTask getTask(Socket client) {
return new StreamSiteTask(client);
}

protected class StreamSiteTask extends ProxyTask {
private List<Header> headers;

public StreamSiteTask(Socket client) {
super(client);
}

public List<Header> getHeaders(List<Header> headers) {
// Strip common problems
Iterator<Header> it = headers.iterator();
while(it.hasNext()) {
Header header = it.next();
if("Server".equals(header.getName())) {
it.remove();
}
}

return headers;
}
private String getHeaderString(int response, List<Header> headers) {
StringBuilder sb = new StringBuilder();

sb.append("HTTP/1.0 ");
sb.append(response);
sb.append(" OK\r\n");

for(Header header: headers) {
sb.append(header.getName());
sb.append(": ");
sb.append(header.getValue());
sb.append("\r\n");
}
sb.append("\r\n");

return sb.toString();
}

@Override
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;

try {
RequestWrapper newRequest = new RequestWrapper(request);
newRequest.setURI(new URI(path));
newRequest.removeHeaders("Host");
newRequest.removeHeaders("Accept-Encoding");
newRequest.removeHeaders("Referer");
response = httpclient.execute(newRequest);
StatusLine status = response.getStatusLine();
if(status.getStatusCode() == HttpStatus.SC_OK) {
headers = new ArrayList<Header>();
headers.addAll(Arrays.asList(response.getAllHeaders()));
headers = getHeaders(headers);

OutputStream output = null;
InputStream input = null;
try {
output = new BufferedOutputStream(client.getOutputStream(), 64*1024);
output.write(getHeaderString(response.getStatusLine().getStatusCode(), headers).getBytes());

input = response.getEntity().getContent();

byte[] buffer = new byte[1024 * 32];
int count = 0, n= 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
output.flush();
} finally {
if(output != null) {
output.close();
}
if(input != null) {
input.close();
}
}
} else {
response.getEntity().getContent().close();
throw new IOException(status.getReasonPhrase());
}
} catch (IOException e) {
Log.e(TAG, "Failed to get data from url: " + path, e);
} catch(Exception e) {
Log.e(TAG, "Exception thrown from web proxy task", e);
}
}
}
}

0 comments on commit 264e853

Please sign in to comment.