Skip to content

Commit

Permalink
added proxy servlet which caches image files for the front-end
Browse files Browse the repository at this point in the history
This is the back-end implementation to support
fossasia/loklak_webclient#59
  • Loading branch information
Orbiter committed Jun 12, 2015
1 parent 1e335ae commit e8790e8
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/org/loklak/LoklakServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.loklak.api.server.DumpDownloadServlet;
import org.loklak.api.server.HelloServlet;
import org.loklak.api.server.PeersServlet;
import org.loklak.api.server.ProxyServlet;
import org.loklak.api.server.PushServlet;
import org.loklak.api.server.SearchServlet;
import org.loklak.api.server.StatusServlet;
Expand Down Expand Up @@ -122,6 +123,9 @@ public static void main(String[] args) throws Exception {
servletHandler.addServlet(SuggestServlet.class, "/api/suggest.json");
servletHandler.addServlet(AccountServlet.class, "/api/account.json");
servletHandler.addServlet(CampaignServlet.class, "/api/campaign.json");
servletHandler.addServlet(ProxyServlet.class, "/api/proxy.gif");
servletHandler.addServlet(ProxyServlet.class, "/api/proxy.png");
servletHandler.addServlet(ProxyServlet.class, "/api/proxy.jpg");
servletHandler.addServlet(MarkdownServlet.class, "/vis/markdown.gif");
servletHandler.addServlet(MarkdownServlet.class, "/vis/markdown.png");
servletHandler.addServlet(MarkdownServlet.class, "/vis/markdown.jpg");
Expand Down
95 changes: 95 additions & 0 deletions src/org/loklak/api/server/ProxyServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* ProxyServlet
* Copyright 12.06.2015 by Michael Peter Christen, @0rb1t3r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>.
*/

package org.loklak.api.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.loklak.api.client.ClientConnection;
import org.loklak.api.server.RemoteAccess;
import org.loklak.tools.Cache;

public class ProxyServlet extends HttpServlet {

private static final long serialVersionUID = -9112326722297824443L;

private final static Cache<String, byte[]> cache = new Cache<>(1000);

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RemoteAccess.Post post = RemoteAccess.evaluate(request);
if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;} // DoS protection
process(request, response, post);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RemoteAccess.Post post = RemoteAccess.evaluate(request);
if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;} // DoS protection
post.initPOST(RemoteAccess.getPostMap(request));
process(request, response, post);
}

// http://localhost:9000/api/proxy.png?screen_name=loklak_app&url=https://pbs.twimg.com/profile_images/577512240640733184/fizL4YIn_bigger.png

protected void process(HttpServletRequest request, HttpServletResponse response, RemoteAccess.Post post) throws ServletException, IOException {
// manage DoS
if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;}
if (!post.isLocalhostAccess()) {response.sendError(503, "access only allowed from localhost"); return;}

// parse arguments
String url = post.get("url", "");
String screen_name = post.get("screen_name", "");

if (url.length() == 0 || screen_name.length() == 0) {response.sendError(503, "attributes url and screen_name must be submitted"); return;}

byte[] buffer = cache.get(url);
if (buffer == null) {
try {
ClientConnection connection = new ClientConnection(url);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffer = new byte[2048];
int count;
try {
while ((count = connection.inputStream.read(buffer)) > 0) baos.write(buffer, 0, count);
} catch (IOException e) {}
connection.close();
buffer = baos.toByteArray();
cache.put(url, buffer);
} catch (IOException e) {
response.sendError(503, "resource not available"); return;
}
}

if (url.endsWith(".png")) post.setResponse(response, "image/png");
else if (url.endsWith(".gif")) post.setResponse(response, "image/gif");
else if (url.endsWith(".jpg")) post.setResponse(response, "image/jpeg");
else post.setResponse(response, "application/octet-stream");

ServletOutputStream sos = response.getOutputStream();
sos.write(buffer);
}
}

0 comments on commit e8790e8

Please sign in to comment.