rewrite welcome jsp to a servlet, add sanity check of inputs

pull/34/head
Zlatin Balevsky 2019-12-15 16:16:11 +00:00
parent 6b1d2bc5ce
commit 6bc5a9075b
6 changed files with 76 additions and 32 deletions

View File

@ -93,11 +93,11 @@ public class ConfigurationServlet extends HttpServlet {
private static File getDirectory(String s) throws Exception {
File f = new File(s);
if (!f.exists())
throw new Exception(Util._t("Bad input")+ Util._t("{0} does not exist",s));
throw new Exception(Util._t("Bad input") + ":" + Util._t("{0} does not exist",s));
if (!f.isDirectory())
throw new Exception(Util._t("Bad input")+ Util._t("{0} is not a directory",s));
throw new Exception(Util._t("Bad input") + ":" + Util._t("{0} is not a directory",s));
if (!f.canWrite())
throw new Exception(Util._t("Bad input")+ Util._t("{0} not writeable",s));
throw new Exception(Util._t("Bad input") + ":" + Util._t("{0} not writeable",s));
return f;
}

View File

@ -0,0 +1,55 @@
package com.muwire.webui;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InitServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String nickname = req.getParameter("nickname");
if (nickname == null || nickname.trim().length() == 0)
throw new Exception("Nickname cannot be blank");
String downloadLocation = req.getParameter("download_location");
if (downloadLocation == null)
throw new Exception("Download location cannot be blank");
File downloadLocationFile = new File(downloadLocation);
if (!downloadLocationFile.exists()) {
if (!downloadLocationFile.mkdirs())
throw new Exception("Couldn't create download location");
} else if (downloadLocationFile.isFile())
throw new Exception("Download location must point to a directory");
else if (!downloadLocationFile.canWrite())
throw new Exception("Download location not writeable");
String incompleteLocation = req.getParameter("incomplete_location");
if (incompleteLocation == null)
throw new Exception("Incomplete files location cannot be blank");
File incompleteLocationFile = new File(incompleteLocation);
if (!incompleteLocationFile.exists()) {
if (!incompleteLocationFile.mkdirs())
throw new Exception("Couldn't create incomplete files location");
} else if (incompleteLocationFile.isFile())
throw new Exception("Incomplete files location must point to a directory");
else if (!incompleteLocationFile.canWrite())
throw new Exception("Incomplete files location not writeable");
MuWireClient client = (MuWireClient) req.getServletContext().getAttribute("mwClient");
client.initMWProps(nickname, downloadLocationFile, incompleteLocationFile);
client.start();
resp.sendRedirect("/MuWire/index");
} catch (Throwable e) {
req.getServletContext().setAttribute("MWInitError", e);
resp.sendRedirect("/MuWire/MuWire");
}
}
}

View File

@ -44,6 +44,7 @@ public class Util {
_x("Download"),
_x("Download Location"),
_x("Download retry frequency (seconds)"),
_x("Download speed smoothing interval (second)"),
_x("Downloader"),
_x("Downloading"),
_x("Enter Reason (Optional)"),

View File

@ -15,6 +15,8 @@
String defaultIncompletesLocation = System.getProperty("user.home") + File.separator+"MuWire Incompletes";
session.setAttribute("defaultDownloadLocation",defaultDownloadLocation);
session.setAttribute("defaultIncompletesLocation",defaultIncompletesLocation);
Throwable error = (Throwable) application.getAttribute("MWInitError");
%>
<noscript>
@ -22,6 +24,11 @@
<center><b><%=Util._t("MuWire requires JavaScript. Please enable JavaScript in your browser.")%></b></center>
</div>
</noscript>
<% if (error != null) { %>
<div class="warning"><%=error.getMessage()%></div>
<% } %>
<p><%=Util._t("Welcome to MuWire! Please select a nickname and download locations")%></p>
<form action="/MuWire/init" method="post">
<%=Util._t("Nickname")%>:

View File

@ -1,24 +0,0 @@
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.io.File" %>
<%@ page import="com.muwire.webui.MuWireClient" %>
<html>
<head>
<%@include file="css.jsi"%>
</head>
<body>
<%
String nickname = request.getParameter("nickname");
String downloadLocation = request.getParameter("download_location");
String incompleteLocation = request.getParameter("incomplete_location");
MuWireClient client = (MuWireClient) application.getAttribute("mwClient");
client.initMWProps(nickname, new File(downloadLocation), new File(incompleteLocation));
client.start();
%>
<c:redirect url="/index"/>
</body>
</html>

View File

@ -16,6 +16,11 @@
</init-param>
</servlet>
<servlet>
<servlet-name>com.muwire.webui.InitServlet</servlet-name>
<servlet-class>com.muwire.webui.InitServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>com.muwire.webui.SearchServlet</servlet-name>
<servlet-class>com.muwire.webui.SearchServlet</servlet-class>
@ -66,6 +71,11 @@
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>com.muwire.webui.InitServlet</servlet-name>
<url-pattern>/init</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>com.muwire.webui.SearchServlet</servlet-name>
<url-pattern>/Search</url-pattern>
@ -154,11 +164,6 @@ Mappings without the .jsp suffix
<url-pattern>/SharedFiles</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>com.muwire.webui.init_jsp</servlet-name>
<url-pattern>/init</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>com.muwire.webui.TrustUsers_jsp</servlet-name>
<url-pattern>/TrustUsers</url-pattern>