]> git.phdru.name Git - dotfiles.git/blobdiff - bin/HEAD.py
Feat(zip): Save and restore symlinks
[dotfiles.git] / bin / HEAD.py
index c7b376403f5ded4aff11fedcb2d4916fad29d270..6c0e5f7eb0d3bd30dddf2ad7d64dd8b2608fb5ff 100755 (executable)
@@ -1,61 +1,57 @@
-#! /usr/bin/env python
+#! /usr/bin/env python3
 
+from http.client import HTTPConnection, HTTPSConnection
+from urllib.parse import urlsplit, parse_qsl, urlencode, quote, quote_plus
 import sys
-url = sys.argv[1]
-
-import socket
-socket.setdefaulttimeout(30)
+import urllib.request
 
-from cgi import parse_qsl
-import urllib
-from m_lib.defenc import default_encoding
+url = sys.argv[1]
 
-protocol, request = urllib.splittype(url)
-user, password, port = None, None, None
-host, path = urllib.splithost(request)
+split_results = urlsplit(url)
+protocol, netloc, path, query, tag = split_results
+user = split_results.username
+password = split_results.password
+host = split_results.hostname
+port = split_results.port
+qlist = parse_qsl(query)
+
+url = protocol + "://"
+if user:
+    url += quote(user)
+    if password:
+        url += ':' + quote(password)
+    url += '@'
 if host:
-   user, host = urllib.splituser(host)
-   if user:
-      user, password = urllib.splitpasswd(user)
-   host, port = urllib.splitport(host)
-   if port: port = int(port)
-path, tag = urllib.splittag(path)
-path, query = urllib.splitquery(path)
-path = urllib.unquote(path)
-if tag: tag = urllib.unquote_plus(tag)
-
-if query:
-   qlist = []
-   for name, value in parse_qsl(query):
-       qlist.append((name, value))
-
-url = ''
-host = host.decode(default_encoding).encode('idna')
+    host = host.encode('idna').decode('ascii')
+    url += host
+    if port:
+        url += ':%d' % port
 if path:
-   url += urllib.quote(path)
+    if protocol == "file":
+        url += quote(path)
+    else:
+        url += quote(path)
 if query:
-   url += '?' + urllib.urlencode(qlist)
+    url += '?' + urlencode(qlist)
 if tag:
-   url += '#' + urllib.quote_plus(tag)
+    url += '#' + quote_plus(tag)
 
-import httplib
-server = httplib.HTTP(host, port)
+if protocol == "https":
+    server = HTTPSConnection(host, port)
+else:
+    server = HTTPConnection(host, port)
 server.set_debuglevel(1)
-
 server.putrequest("HEAD", path)
-if port:
-    server.putheader("Host", '%s:%d' % (host, port))
-else:
-   server.putheader("Host", host)
 
 # I remember seeing some sites that return broken HTML or even HTTP response
 # without "compatible" user agent; I don't know if such sites are still around,
 # but this header doesn't cause any harm so I'd better continue to use it.
 # UPDATE: I saw a number of sites that forbid "Mozilla compatible"
-client_version = "Python-urllib/%s" % urllib.__version__
+urllib_version = urllib.request.__version__
+client_version = "Python-urllib/%s" % urllib_version
 server.putheader('User-agent', client_version)
 
 server.putheader('Accept-Charset', "koi8-r;q=1.0")
 server.endheaders()
 
-server.getreply()
+server.getresponse()