Commit 9f01a465 authored by Robert Szulist's avatar Robert Szulist
Browse files

Make code compatibile with python 2 and 3

Add chcecks for version for importing and decoding bytestring.
Inspired by PR #66
parent 08fb44b6
Loading
Loading
Loading
Loading
+16 −4
Original line number Diff line number Diff line
@@ -4,8 +4,15 @@

# Copyright (C) 2014-2015 Nginx, Inc.

import sys, os, signal, base64, ldap, Cookie, argparse
import sys, os, signal, base64, ldap, argparse
if sys.version_info.major == 2:
    from Cookie import BaseCookie
    from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
elif sys.version_info.major == 3:
    from http.cookies import BaseCookie
    from http.server import HTTPServer, BaseHTTPRequestHandler

if not hasattr(__builtins__, "basestring"): basestring = (str, bytes)

#Listen = ('localhost', 8888)
#Listen = "/tmp/auth.sock"    # Also uncomment lines in 'Requests are
@@ -16,7 +23,11 @@ from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
# -----------------------------------------------------------------------------
# Requests are processed in separate thread
import threading
if sys.version_info.major == 2:
    from SocketServer import ThreadingMixIn
elif sys.version_info.major == 3:
    from socketserver import ThreadingMixIn

class AuthHTTPServer(ThreadingMixIn, HTTPServer):
    pass
# -----------------------------------------------------------------------------
@@ -71,6 +82,7 @@ class AuthHandler(BaseHTTPRequestHandler):

        try:
            auth_decoded = base64.b64decode(auth_header[6:])
            if sys.version_info.major == 3: auth_decoded = auth_decoded.decode("utf-8")
            user, passwd = auth_decoded.split(':', 1)

        except:
@@ -86,7 +98,7 @@ class AuthHandler(BaseHTTPRequestHandler):
    def get_cookie(self, name):
        cookies = self.headers.get('Cookie')
        if cookies:
            authcookie = Cookie.BaseCookie(cookies).get(name)
            authcookie = BaseCookie(cookies).get(name)
            if authcookie:
                return authcookie.value
            else: