Skip to content

Commit 48ef37e

Browse files
committed
timeout support (rough but working)
1 parent 20ac60e commit 48ef37e

File tree

1 file changed

+68
-14
lines changed

1 file changed

+68
-14
lines changed

www/test.jsp

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<%@ page contentType="text/plain;charset=utf-8"
22
import="java.io.*,
3+
java.text.*,
34
java.util.*,
45
java.util.regex.*,
56
org.json.simple.*,
@@ -30,6 +31,7 @@
3031
options.addAll(Arrays.asList(ArrOption));
3132
}
3233
String[] ArrInput = request.getParameterValues("input");
34+
int timeoutMillis = 10 * 1000;
3335
3436
if (Strings.isNullOrEmpty(regex))
3537
{
@@ -164,47 +166,58 @@
164166
pw.print(StringEscapeUtils.escapeHtml4(test));
165167
pw.println("</td>");
166168
167-
Matcher m = p.matcher(test);
169+
Matcher m = null;
170+
boolean matches, lookingAt;
171+
String replaceFirst, replaceAll;
168172
169-
pw.print("\t\t\t<td>");
170-
pw.print(StringEscapeUtils.escapeHtml4(booleanToString(m.matches(), loc)));
171-
pw.println("</td>");
172-
173-
m.reset();
174-
175-
pw.print("\t\t\t<td>");
176173
try
177174
{
178-
pw.print(StringEscapeUtils.escapeHtml4(m.replaceFirst(replacement)));
175+
m = p.matcher(new TimeoutCharSequence(test, timeoutMillis));
176+
matches = m.matches();
177+
m.reset();
178+
replaceFirst = m.replaceFirst(replacement);
179+
m.reset();
180+
replaceAll = m.replaceAll(replacement);
181+
m.reset();
182+
lookingAt = m.lookingAt();
179183
}
180184
catch (Exception e)
181185
{
182-
pw.print("<i>(ERROR: ");
186+
pw.print("\t\t\t<td class=\"text-error\" colspan=\"6\">");
187+
pw.print("ERROR: ");
183188
pw.print(StringEscapeUtils.escapeHtml4(e.getMessage()));
184-
pw.print(")</i>");
189+
pw.println("</td>");
190+
pw.println("\t\t</tr>");
191+
continue;
185192
}
186193
194+
pw.print("\t\t\t<td>");
195+
pw.print(StringEscapeUtils.escapeHtml4(booleanToString(matches, loc)));
187196
pw.println("</td>");
188197
189-
m.reset();
190198
191199
pw.print("\t\t\t<td>");
192200
try
193201
{
194-
pw.print(StringEscapeUtils.escapeHtml4(m.replaceAll(replacement)));
202+
pw.print(StringEscapeUtils.escapeHtml4(replaceFirst));
195203
}
196204
catch (Exception e)
197205
{
198206
pw.print("<i>(ERROR: ");
199207
pw.print(StringEscapeUtils.escapeHtml4(e.getMessage()));
200208
pw.print(")</i>");
201209
}
210+
202211
pw.println("</td>");
203212
204213
m.reset();
205214
206215
pw.print("\t\t\t<td>");
207-
pw.print(StringEscapeUtils.escapeHtml4(booleanToString(m.lookingAt(), loc)));
216+
pw.print(StringEscapeUtils.escapeHtml4(replaceAll));
217+
pw.println("</td>");
218+
219+
pw.print("\t\t\t<td>");
220+
pw.print(StringEscapeUtils.escapeHtml4(booleanToString(lookingAt, loc)));
208221
pw.println("</td>");
209222
210223
m.reset();
@@ -300,6 +313,47 @@
300313
301314
return false;
302315
}
316+
317+
class TimeoutCharSequence
318+
implements CharSequence
319+
{
320+
private final CharSequence inner;
321+
private final int timeoutMillis;
322+
private final long timeoutTime;
323+
324+
public TimeoutCharSequence(CharSequence inner, int timeoutMillis)
325+
{
326+
super();
327+
this.inner = inner;
328+
this.timeoutMillis = timeoutMillis;
329+
timeoutTime = System.currentTimeMillis() + timeoutMillis;
330+
}
331+
332+
public char charAt(int index)
333+
{
334+
if (System.currentTimeMillis() > timeoutTime)
335+
{
336+
throw new RuntimeException(MessageFormat.format("Interrupted after {0}ms", timeoutMillis));
337+
}
338+
return inner.charAt(index);
339+
}
340+
341+
public int length()
342+
{
343+
return inner.length();
344+
}
345+
346+
public CharSequence subSequence(int start, int end)
347+
{
348+
return new TimeoutCharSequence(inner.subSequence(start, end), timeoutMillis);
349+
}
350+
351+
@Override
352+
public String toString()
353+
{
354+
return inner.toString();
355+
}
356+
}
303357
%>
304358

305359

0 commit comments

Comments
 (0)