Skip to content

Instantly share code, notes, and snippets.

@youz
Created December 28, 2009 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youz/264591 to your computer and use it in GitHub Desktop.
Save youz/264591 to your computer and use it in GitHub Desktop.
expand-short-url #xyzzy
(require 'cmu_loop)
(defun expand-short-url (url)
(if (not (string-match "http://\\([^/]+\\)/\\(.+\\)" url))
url
(let ((host (match-string 1))
(path (match-string 2)))
(with-open-stream (cn (connect host 80))
(format cn "GET /~A HTTP/1.1\nHost: ~A\n\n" path host)
(let* ((res (read-line cn nil))
(m (string-match "^HTTP/[0-9.]+ \\([0-9]+\\) .+$" res))
(status (match-string 1)))
(if (or (not m) (string/= status "301"))
(values url res)
(loop
for buf = (read-line cn nil)
when (string-match "^Location: \\(.+\\)$" buf)
do (return (match-string 1))
while buf
finally (values url "bad response"))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment