mirror of
https://github.com/SinTan1729/chhoto-url
synced 2024-12-26 23:58:35 -06:00
Added a basic input validation for shortUrl
This commit is contained in:
parent
8cd399d2e9
commit
1322569cf6
3 changed files with 23 additions and 6 deletions
|
@ -1,5 +1,6 @@
|
||||||
package tk.draganczuk.url;
|
package tk.draganczuk.url;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.http.HttpStatus;
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
import spark.Response;
|
import spark.Response;
|
||||||
|
|
||||||
|
@ -29,8 +30,14 @@ public class Routes {
|
||||||
shortUrl = Utils.randomString();
|
shortUrl = Utils.randomString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Utils.validate(shortUrl)) {
|
||||||
return urlFile.addUrl(longUrl, shortUrl);
|
return urlFile.addUrl(longUrl, shortUrl);
|
||||||
|
} else {
|
||||||
|
res.status(HttpStatus.BAD_REQUEST_400);
|
||||||
|
return "shortUrl not valid ([a-z0-9]+)";
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String goToLongUrl(Request req, Response res) {
|
public static String goToLongUrl(Request req, Response res) {
|
||||||
String shortUrl = req.params("shortUrl");
|
String shortUrl = req.params("shortUrl");
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
package tk.draganczuk.url;
|
package tk.draganczuk.url;
|
||||||
|
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
private static final Random random = new Random(System.currentTimeMillis());
|
private static final Random random = new Random(System.currentTimeMillis());
|
||||||
|
|
||||||
|
private static final String SHORT_URL_PATTERN = "[a-z0-9]+";
|
||||||
|
private static final Pattern PATTERN = Pattern.compile(SHORT_URL_PATTERN);
|
||||||
|
|
||||||
public static String randomString() {
|
public static String randomString() {
|
||||||
int leftLimit = 48; // numeral '0'
|
int leftLimit = 48; // numeral '0'
|
||||||
int rightLimit = 122; // letter 'z'
|
int rightLimit = 122; // letter 'z'
|
||||||
|
@ -18,4 +22,9 @@ public class Utils {
|
||||||
StringBuilder::append)
|
StringBuilder::append)
|
||||||
.toString();
|
.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean validate(String shortUrl) {
|
||||||
|
return PATTERN.matcher(shortUrl)
|
||||||
|
.matches();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,11 +30,12 @@
|
||||||
<legend>Add new URL</legend>
|
<legend>Add new URL</legend>
|
||||||
<div class="pure-control-group">
|
<div class="pure-control-group">
|
||||||
<label for="longUrl">Long URL</label>
|
<label for="longUrl">Long URL</label>
|
||||||
<input type="text" name="longUrl" id="longUrl" placeholder="Long URL" required/>
|
<input type="url" name="longUrl" id="longUrl" placeholder="Long URL" required/>
|
||||||
</div>
|
</div>
|
||||||
<div class="pure-control-group">
|
<div class="pure-control-group">
|
||||||
<label for="shortUrl">Short URL (Optional)</label>
|
<label for="shortUrl">Short URL (Optional)</label>
|
||||||
<input type="text" name="shortUrl" id="shortUrl" placeholder="Short URL (optional)"/>
|
<input type="text" name="shortUrl" id="shortUrl" placeholder="Short URL (optional)"
|
||||||
|
pattern="[a-z0-9]+"/>
|
||||||
</div>
|
</div>
|
||||||
<div class="pure-controls">
|
<div class="pure-controls">
|
||||||
<button class="pure-button pure-button-primary">Submit</button>
|
<button class="pure-button pure-button-primary">Submit</button>
|
||||||
|
|
Loading…
Reference in a new issue