URL Shortener

Problem this snippet solves:

The Small URL Generator takes a long URL, examines its length, and assigns it a variable length key based on the original URL's length. The key is then stored in a subtable along with the original URL. When a user accesses the small URL (http:/// ), they are then redirected to the original long URL. This Small URL Generator also has the ability to create custom URL keys.

Code :

when RULE_INIT {
  set static::small_url_timeout 86400
  set static::small_url_lifetime 86400
  set static::small_url_response_header "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"><html><head> \
    <title>Small URL Generator</title></head><body><center><h1>Small URL Generator</h1> \
    "
  set static::small_url_response_footer "</center></body></html>"
}

when HTTP_REQUEST {
  if { ([HTTP::uri] starts_with "/create?") and ([HTTP::query] ne "") } {
    set url [URI::decode [string tolower [URI::query [HTTP::uri] url]]]
    set custom_url_key [string tolower [URI::query [HTTP::uri] custom_url_key]]

    if { $custom_url_key ne "" } {
      if { ([table lookup -subtable small_url $custom_url_key] ne "") } {
        HTTP::respond 200 content "$static::small_url_response_header <b><font color=\"ff0000\"> \
          Error: the custom Small URL <a href=\"http://[HTTP::host]/$custom_url_key\"> \
          http://[HTTP::host]/$custom_url_key</a> has already been taken. Please try again. \
          </font></b> $static::small_url_response_footer"
      } else {
        set url_key $custom_url_key
        log local0. "Custom Small URL created for $url with custom key $url_key"      }
    } else {
      switch -glob [string length $url] {
        {[1-9]} { set url_key_length 3 }
        {1[0-9]} { set url_key_length 3 }
        {2[0-9]} { set url_key_length 4 }
        {3[0-9]} { set url_key_length 5 }
        default { set url_key_length 6 }
      }

      set url_key [string tolower [scan [string map {/ "" + ""} [b64encode [md5 $url]]] "%${url_key_length}s"]]
    }

    if { ([table lookup -subtable small_url $url_key] eq "") } {
      table add -subtable small_url $url_key $url $static::small_url_timeout $static::small_url_lifetime
      log local0. "Small URL created for $url with key $url_key"
    } else {
      log local0. "Small URL for $url already exists with key $url_key"
    }

    HTTP::respond 200 content "$static::small_url_response_header The Small URL for \
      <a href=\"$url\">$url</a> is <a href=\"http://[HTTP::host]/$url_key\"> \
      http://[HTTP::host]/$url_key</a> $static::small_url_response_footer"
  } else {
    set url_key [string map {/ ""} [HTTP::path]]
    set url [table lookup -subtable small_url $url_key]

    if { [string length $url] != 0 } {
      log local0. "Found key $url_key, redirecting to $url"
      HTTP::redirect $url
    } else {
      HTTP::respond 200 content "$static::small_url_response_header <form action=\"/create\" \
        method=\"get\"><input type=\"text\" name=\"url\">&nbsp; \
        <input type=\"submit\" value=\"make small!\"><h4>Make it custom! \
        (optional)</h4>http://[HTTP::host]/<input type=\"text\" name=\"custom_url_key\"></form> \
        $static::small_url_response_footer"
    }
  }
}

Tested this on version:

10.2

Updated Jan 26, 2023
Version 2.0

Was this article helpful?

No CommentsBe the first to comment