* i2psnark: Redirect after post

Button spacing on config form
This commit is contained in:
zzz
2012-12-29 13:17:27 +00:00
parent db0501f31b
commit c6dd7b4cc5

View File

@ -188,11 +188,15 @@ public class I2PSnarkServlet extends DefaultServlet {
} else { } else {
String base = URIUtil.addPaths(req.getRequestURI(), "/"); String base = URIUtil.addPaths(req.getRequestURI(), "/");
String listing = getListHTML(resource, base, true, method.equals("POST") ? req.getParameterMap() : null); String listing = getListHTML(resource, base, true, method.equals("POST") ? req.getParameterMap() : null);
if (listing != null) if (method.equals("POST")) {
// P-R-G
sendRedirect(req, resp, "");
} else if (listing != null) {
resp.getWriter().write(listing); resp.getWriter().write(listing);
else // shouldn't happen } else { // shouldn't happen
resp.sendError(404); resp.sendError(404);
} }
}
} else { } else {
super.service(req, resp); super.service(req, resp);
} }
@ -209,6 +213,9 @@ public class I2PSnarkServlet extends DefaultServlet {
processRequest(req); processRequest(req);
else // nonce is constant, shouldn't happen else // nonce is constant, shouldn't happen
_manager.addMessage("Please retry form submission (bad nonce)"); _manager.addMessage("Please retry form submission (bad nonce)");
// P-R-G (or G-R-G to hide the params from the address bar)
sendRedirect(req, resp, peerString);
return;
} }
PrintWriter out = resp.getWriter(); PrintWriter out = resp.getWriter();
@ -807,6 +814,22 @@ public class I2PSnarkServlet extends DefaultServlet {
} }
} }
/**
* Redirect a POST to a GET (P-R-G), preserving the peer string
* @since 0.9.5
*/
private void sendRedirect(HttpServletRequest req, HttpServletResponse resp, String p) throws IOException {
String url = req.getRequestURL().toString();
StringBuilder buf = new StringBuilder(128);
if (url.endsWith("_post"))
url = url.substring(0, url.length() - 5);
buf.append(url);
if (p.length() > 0)
buf.append('?').append(p);
resp.setHeader("Location", buf.toString());
resp.sendError(302, "Moved");
}
/** @since 0.9 */ /** @since 0.9 */
private void processTrackerForm(String action, HttpServletRequest req) { private void processTrackerForm(String action, HttpServletRequest req) {
if (action.equals(_("Delete selected")) || action.equals(_("Save tracker configuration"))) { if (action.equals(_("Delete selected")) || action.equals(_("Save tracker configuration"))) {
@ -1768,10 +1791,11 @@ public class I2PSnarkServlet extends DefaultServlet {
out.write(_("I2CP options")); out.write(_("I2CP options"));
out.write(": <td><textarea name=\"i2cpOpts\" cols=\"60\" rows=\"1\" wrap=\"off\" spellcheck=\"false\" >" out.write(": <td><textarea name=\"i2cpOpts\" cols=\"60\" rows=\"1\" wrap=\"off\" spellcheck=\"false\" >"
+ opts.toString() + "</textarea><br>\n" + + opts.toString() + "</textarea><br>\n" +
"<tr><td colspan=\"2\">&nbsp;\n" + // spacer
"<tr><td>&nbsp;<td><input type=\"submit\" class=\"accept\" value=\""); "<tr><td>&nbsp;<td><input type=\"submit\" class=\"accept\" value=\"");
out.write(_("Save configuration")); out.write(_("Save configuration"));
out.write("\" name=\"foo\" >\n" + out.write("\" name=\"foo\" >\n" +
"<tr><td colspan=\"2\">&nbsp;\n" + // spacer
"</table></div></div></form>"); "</table></div></div></form>");
} }
@ -1837,6 +1861,7 @@ public class I2PSnarkServlet extends DefaultServlet {
"<td><input type=\"checkbox\" class=\"optbox\" name=\"_add_open_\"></td>" + "<td><input type=\"checkbox\" class=\"optbox\" name=\"_add_open_\"></td>" +
"<td><input type=\"checkbox\" class=\"optbox\" name=\"_add_private_\"></td>" + "<td><input type=\"checkbox\" class=\"optbox\" name=\"_add_private_\"></td>" +
"<td><input type=\"text\" class=\"trackerannounce\" name=\"taurl\"></td></tr>\n" + "<td><input type=\"text\" class=\"trackerannounce\" name=\"taurl\"></td></tr>\n" +
"<tr><td colspan=\"6\">&nbsp;</td></tr>\n" + // spacer
"<tr><td colspan=\"2\"></td><td colspan=\"4\">\n" + "<tr><td colspan=\"2\"></td><td colspan=\"4\">\n" +
"<input type=\"submit\" name=\"taction\" class=\"default\" value=\"").append(_("Add tracker")).append("\">\n" + "<input type=\"submit\" name=\"taction\" class=\"default\" value=\"").append(_("Add tracker")).append("\">\n" +
"<input type=\"submit\" name=\"taction\" class=\"delete\" value=\"").append(_("Delete selected")).append("\">\n" + "<input type=\"submit\" name=\"taction\" class=\"delete\" value=\"").append(_("Delete selected")).append("\">\n" +
@ -1844,7 +1869,9 @@ public class I2PSnarkServlet extends DefaultServlet {
// "<input type=\"reset\" class=\"cancel\" value=\"").append(_("Cancel")).append("\">\n" + // "<input type=\"reset\" class=\"cancel\" value=\"").append(_("Cancel")).append("\">\n" +
"<input type=\"submit\" name=\"taction\" class=\"reload\" value=\"").append(_("Restore defaults")).append("\">\n" + "<input type=\"submit\" name=\"taction\" class=\"reload\" value=\"").append(_("Restore defaults")).append("\">\n" +
"<input type=\"submit\" name=\"taction\" class=\"add\" value=\"").append(_("Add tracker")).append("\">\n" + "<input type=\"submit\" name=\"taction\" class=\"add\" value=\"").append(_("Add tracker")).append("\">\n" +
"</td></tr></table></div></div></form>\n"); "</td></tr>" +
"<tr><td colspan=\"6\">&nbsp;</td></tr>\n" + // spacer
"</table></div></div></form>\n");
out.write(buf.toString()); out.write(buf.toString());
} }
@ -2001,7 +2028,7 @@ public class I2PSnarkServlet extends DefaultServlet {
* @param base The base URL * @param base The base URL
* @param parent True if the parent directory should be included * @param parent True if the parent directory should be included
* @param postParams map of POST parameters or null if not a POST * @param postParams map of POST parameters or null if not a POST
* @return String of HTML * @return String of HTML or null if postParams != null
* @since 0.7.14 * @since 0.7.14
*/ */
private String getListHTML(Resource r, String base, boolean parent, Map postParams) private String getListHTML(Resource r, String base, boolean parent, Map postParams)
@ -2013,8 +2040,6 @@ public class I2PSnarkServlet extends DefaultServlet {
Arrays.sort(ls, Collator.getInstance()); Arrays.sort(ls, Collator.getInstance());
} // if r is not a directory, we are only showing torrent info section } // if r is not a directory, we are only showing torrent info section
StringBuilder buf=new StringBuilder(4096);
buf.append(DOCTYPE + "<HTML><HEAD><TITLE>");
String title = URIUtil.decodePath(base); String title = URIUtil.decodePath(base);
if (title.startsWith("/i2psnark/")) if (title.startsWith("/i2psnark/"))
title = title.substring("/i2psnark/".length()); title = title.substring("/i2psnark/".length());
@ -2028,9 +2053,14 @@ public class I2PSnarkServlet extends DefaultServlet {
torrentName = title; torrentName = title;
Snark snark = _manager.getTorrentByBaseName(torrentName); Snark snark = _manager.getTorrentByBaseName(torrentName);
if (snark != null && postParams != null) if (snark != null && postParams != null) {
// caller must P-R-G
savePriorities(snark, postParams); savePriorities(snark, postParams);
return null;
}
StringBuilder buf=new StringBuilder(4096);
buf.append(DOCTYPE).append("<HTML><HEAD><TITLE>");
if (title.endsWith("/")) if (title.endsWith("/"))
title = title.substring(0, title.length() - 1); title = title.substring(0, title.length() - 1);
String directory = title; String directory = title;