-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a servlet which can cause slow regular expression parse
- Loading branch information
Showing
7 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/main/java/performance/SlowRegularExpressionServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package performance; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.t246osslab.easybuggy.utils.Closer; | ||
import org.t246osslab.easybuggy.utils.HTTPResponseCreator; | ||
import org.t246osslab.easybuggy.utils.MessageUtils; | ||
|
||
@SuppressWarnings("serial") | ||
@WebServlet(urlPatterns = { "/slowre" }) | ||
public class SlowRegularExpressionServlet extends HttpServlet { | ||
|
||
private static Logger log = LoggerFactory.getLogger(SlowRegularExpressionServlet.class); | ||
|
||
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { | ||
|
||
PrintWriter writer = null; | ||
try { | ||
String word = req.getParameter("word"); | ||
Locale locale = req.getLocale(); | ||
|
||
StringBuilder bodyHtml = new StringBuilder(); | ||
|
||
bodyHtml.append("<form action=\"slowre\" method=\"post\">"); | ||
bodyHtml.append(MessageUtils.getMsg("description.test.regular.expression", locale)); | ||
bodyHtml.append("<br><br>"); | ||
bodyHtml.append("<img src=\"images/regular-expression.png\">"); | ||
bodyHtml.append("<br><br>"); | ||
bodyHtml.append(MessageUtils.getMsg("label.string", locale) + ": "); | ||
bodyHtml.append("<input type=\"text\" name=\"word\" size=\"50\" maxlength=\"50\">"); | ||
bodyHtml.append("<br><br>"); | ||
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.submit", locale) + "\">"); | ||
bodyHtml.append("<br><br>"); | ||
|
||
if (word != null && !word.equals("")) { | ||
Date startDdate = new Date(); | ||
log.info("Start Ddate: " + startDdate.toString()); | ||
Pattern compile = Pattern.compile("^([a-z0-9]+[-]{0,1}){1,100}$"); | ||
Matcher matcher = compile.matcher(word); | ||
boolean matches = matcher.matches(); | ||
Date endDdate = new Date(); | ||
log.info("End Ddate: " + endDdate.toString()); | ||
if (matches) { | ||
bodyHtml.append(MessageUtils.getMsg("msg.match.regular.expression", locale)); | ||
} else { | ||
bodyHtml.append(MessageUtils.getMsg("msg.not.match.regular.expression", locale)); | ||
} | ||
} else { | ||
bodyHtml.append(MessageUtils.getMsg("msg.enter.word", locale)); | ||
} | ||
bodyHtml.append("<br><br>"); | ||
bodyHtml.append(MessageUtils.getMsg("msg.note.slow.regular.expression", locale)); | ||
bodyHtml.append("</form>"); | ||
|
||
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.slow.regular.expression.page", locale), | ||
bodyHtml.toString()); | ||
|
||
} catch (Exception e) { | ||
log.error("Exception occurs: ", e); | ||
} finally { | ||
Closer.close(writer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters