1: when RULE_INIT {
2: set static::small_url_timeout 600
3: set static::small_url_lifetime 600
4: }
5:
6: when HTTP_REQUEST {
7: if { ([HTTP::uri] starts_with "/create?") and ([HTTP::query] ne "") } {
8: set url [URI::decode [string tolower [URI::query [HTTP::uri] url]]]
9: set url_key [string tolower [scan [string map {/ "" + ""} [b64encode [md5 $url]]] {%6s}]]
10:
11: if { ([table lookup -subtable small_url $url_key] eq "") } {
12: table add -subtable small_url $url_key $url $static::small_url_timeout $static::small_url_lifetime
13: log local0. "Small URL created for $url with key $url_key"
14: } else {
15: log local0. "Small URL for $url already exists with key $url_key"
16: }
17:
18: HTTP::respond 200 content "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head> \
19: <title>Small URL Generator</title></head><body><center><h1>Small URL Generator</h1> \
20: </center><center>The small URL for <a href=\"$url\">$url</a> is \
21: <a href=\"http://[HTTP::host]/$url_key\">http://[HTTP::host]/$url_key</a></center> \
22: </body></html>"
23: } else {
24: set url_key [string map {/ ""} [HTTP::path]]
25: set url [table lookup -subtable small_url $url_key]
26:
27: if { [string length $url] != 0 } {
28: log local0. "Found key $url_key, redirecting to $url"
29: HTTP::redirect $url
30: } else {
31: HTTP::respond 200 content "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head> \
32: <title>Small URL Generator</title> </head><body> <center><h1>Small URL Generator</h1> \
33: </center><form action=\"/create\" method=\"get\"><center> \
34: <input type=\"text\" name=\"url\"> <input type=\"submit\" value=\"make small!\"> \
35: </center></form></body></html>"
36: }
37: }
38: }