diff --git a/analyzer/python/ikos/http.py b/analyzer/python/ikos/http.py index 16c51173..abf2f2b3 100644 --- a/analyzer/python/ikos/http.py +++ b/analyzer/python/ikos/http.py @@ -42,16 +42,20 @@ try: # Python 3 from http.server import HTTPServer, BaseHTTPRequestHandler + from errno import EAFNOSUPPORT from urllib.parse import parse_qs, urlencode from urllib.request import urlopen from socket import AF_INET6 + import os # OSError except ImportError: # Python 2 from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler + from errno import EAFNOSUPPORT from urlparse import parse_qs from urllib import urlencode from urllib2 import urlopen from socket import AF_INET6 + import os # OSError class HTTPServerIPv6(HTTPServer): @@ -59,6 +63,26 @@ class HTTPServerIPv6(HTTPServer): HTTP server that listens in IPv6. In systems with dual stack, this also listens in IPv4. + + This method tries to listed on IPv6 first (and IPv4 if available). If that + is not supported, then it tries IPv4 only. ''' address_family = AF_INET6 + + def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): + try: + # Initially try to open the server with IPv6. This will also select + # IPv4 on systems with dual stack. + HTTPServer(server_address, RequestHandlerClass, bind_and_activate) + + except OSError as e: + if e.errno == errno.EAFNOSUPPORT: + # If the exception is due to IPv6 not being supported, we + # select IPv4 only and try to re-open the server again. + address_family = AF_INET + HTTPServer(server_address, RequestHandlerClass, bind_and_activate) + else: + # If the exception is for any other reason other than IPv6 not + # being supported, then we cannot do anything about it. + raise e