import sys import os.path sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "lib")) import re import requests from requests.status_codes import codes try: import http.client as httplib except ImportError: import httplib import commandline import sublime try: from io import StringIO except ImportError: from StringIO import StringIO # Linux version blows up when importing io.StringIO import logging logging.basicConfig(format='%(asctime)s %(message)s') logger = logging.getLogger() class CurlSession(object): ERR_UNKNOWN_CODE = "Curl failed with an unrecognized code" CURL_ERRORS = { 2: "Curl failed initialization.", 5: "Curl could not resolve the proxy specified.", 6: "Curl could not resolve the remote host.\n\nPlease verify that your Internet" " connection works properly." } class FakeSocket(StringIO): def makefile(self, *args, **kw): return self def __init__(self, verify=None): self.verify = verify def _parse_http(self, text): # if the response text starts with a 302, skip to the next non-302 header if re.match(r'^HTTP/.*?\s302 Found', text): m = re.search(r'(HTTP/\d+\.\d+\s(?!302 Found).*$)', text, re.S) if not m: raise Exception("Unrecognized response: %s" % text) else: text = m.group(1) # if the response text starts with a "200 Connection established" but continues with a 201, # skip the 200 header. This happens when using a proxy. # # e.g. HTTP/1.1 200 Connection established # Via: 1.1 proxy # Connection: Keep-Alive # Proxy-Connection: Keep-Alive # # HTTP/1.1 201 Created # Server: GitHub.com # ... # Status: 201 Created # ... if re.match(r'^HTTP/.*?\s200 Connection established', text): m = re.search(r'(HTTP/\d+\.\d+\s(?!200 Connection established).*$)', text, re.S) if not m: raise Exception("Unrecognized response: %s" % text) else: text = m.group(1) # remove Transfer-Encoding: chunked header, as it causes reading the response to fail # first do a quick check for it, so we can avoid doing the expensive negative-lookbehind # regex if we don't need it if "Transfer-Encoding: chunked" in text: # we do the negative-lookbehind to make sure we only strip the Transfer-Encoding # string in the header text = re.sub(r'(?