Created
December 28, 2009 08:37
-
-
Save youz/264591 to your computer and use it in GitHub Desktop.
expand-short-url #xyzzy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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