Skip to content

Commit

Permalink
Cleaned up imports in pants.http, and added __all__ where appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
stendec committed Oct 25, 2012
1 parent a2cde8a commit 45790ad
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 32 deletions.
2 changes: 1 addition & 1 deletion docs/web/http_client.rst
Expand Up @@ -9,7 +9,7 @@ Exceptions

.. autoclass:: CertificateError

.. autoclass:: HttpException
.. autoclass:: HTTPClientException
.. autoclass:: MalformedResponse
.. autoclass:: RequestClosed
.. autoclass:: RequestTimedOut
Expand Down
2 changes: 1 addition & 1 deletion pants/http/auth.py
Expand Up @@ -20,7 +20,7 @@
# Imports
###############################################################################

from pants.http.utils import *
import base64

###############################################################################
# BaseAuth Class
Expand Down
51 changes: 42 additions & 9 deletions pants/http/client.py
Expand Up @@ -21,14 +21,21 @@
###############################################################################

import Cookie
import os
import ssl
import tempfile
import urllib
import urlparse
import zlib

from pants.http.utils import *
from pants.http.auth import AuthBase, BasicAuth
from datetime import datetime

from pants.stream import Stream
from pants.engine import Engine

from pants.http.auth import BasicAuth
from pants.http.utils import CRLF, date, DOUBLE_CRLF, encode_multipart, log, \
read_headers, USER_AGENT

try:
from backports.ssl_match_hostname import match_hostname, CertificateError
Expand All @@ -37,39 +44,57 @@
class CertificateError(Exception):
pass

###############################################################################
# Exports
###############################################################################

__all__ = (
# Exceptions
"HTTPClientException", "RequestTimedOut", "MalformedResponse",
"RequestClosed",

# Core Classes
"HTTPClient", "Session", "HTTPResponse",
)

###############################################################################
# Constants
###############################################################################

CHUNK_SIZE = 2 ** 16
MAX_MEMORY_SIZE = 2 ** 20


###############################################################################
# Exceptions
###############################################################################

class HttpException(Exception):
class HTTPClientException(Exception):
"""
The base exception for all the exceptions used by the HTTP client, aside
from :class:`CertificateError`.
"""
pass

class RequestTimedOut(HttpException):

class RequestTimedOut(HTTPClientException):
""" The exception returned when a connection times out. """
pass

class MalformedResponse(HttpException):

class MalformedResponse(HTTPClientException):
""" The exception returned when the response is malformed in some way. """
pass

class RequestClosed(HttpException):

class RequestClosed(HTTPClientException):
"""
The exception returned when the connection closes before the entire
request has been downloaded.
"""
pass


###############################################################################
# Content Encoding
###############################################################################
Expand All @@ -80,10 +105,12 @@ def encoding_gzip():
return zlib.decompressobj(16 + zlib.MAX_WBITS)
CONTENT_ENCODING['gzip'] = encoding_gzip


def encoding_deflate():
return zlib.decompressobj(-zlib.MAX_WBITS)
CONTENT_ENCODING['deflate'] = encoding_deflate


###############################################################################
# Cookie Loading
###############################################################################
Expand All @@ -100,6 +127,7 @@ def _get_cookies(request):
_load_cookies(cookies, request.session.parent)
return cookies


def _load_cookies(cookies, session):
if session.cookies:
for key in session.cookies:
Expand All @@ -108,6 +136,7 @@ def _load_cookies(cookies, session):
if session.parent:
_load_cookies(cookies, session.parent)


###############################################################################
# Getting Hostname and Port on Python <2.7
###############################################################################
Expand All @@ -124,6 +153,7 @@ def _hostname(parts):
else:
return netloc.lower()


def _port(parts):
# This code is borrowed from Python 2.7's argparse.
netloc = parts.netloc.split('@')[-1].split(']')[-1]
Expand All @@ -133,6 +163,7 @@ def _port(parts):
else:
return None


###############################################################################
# _HTTPStream Class
###############################################################################
Expand Down Expand Up @@ -203,6 +234,7 @@ def on_read_error(self, err):
def on_overflow_error(self, err):
self.client._do_error(err)


###############################################################################
# HTTPClient Class
###############################################################################
Expand Down Expand Up @@ -254,8 +286,7 @@ def __init__(self, *args, **kwargs):

# Figure out our engine.
if 'engine' in kwargs:
self.engine = Engine.instance()
del kwargs['engine']
self.engine = kwargs.pop("engine")
else:
self.engine = Engine.instance()

Expand Down Expand Up @@ -738,7 +769,7 @@ def _read_headers(self, data):
response.remaining = response.total

# If there's no length, immediately we've got a response.
if response.remaining == 0:
if not response.remaining:
self._on_response()
return

Expand Down Expand Up @@ -974,6 +1005,7 @@ def _read_chunk_body(self, data):
self._stream.on_read = self._read_chunk_head
self._stream.read_delimiter = CRLF


###############################################################################
# Session Class
###############################################################################
Expand Down Expand Up @@ -1328,6 +1360,7 @@ def __repr__(self):
id(self)
)


###############################################################################
# HTTPResponse Class
###############################################################################
Expand Down
20 changes: 19 additions & 1 deletion pants/http/server.py
Expand Up @@ -20,12 +20,29 @@
# Imports
###############################################################################

import base64
import Cookie
import os
import pprint

from datetime import datetime
from time import time
from urlparse import parse_qsl

from pants.http.utils import *
from pants.stream import Stream
from pants.server import Server

from pants.http.utils import BadRequest, CRLF, date, DOUBLE_CRLF, \
generate_signature, HTTP, log, parse_multipart, read_headers, SERVER, \
WHITESPACE

###############################################################################
# Exports
###############################################################################

__all__ = (
"HTTPConnection", "HTTPRequest", "HTTPServer"
)

###############################################################################
# HTTPConnection Class
Expand Down Expand Up @@ -197,6 +214,7 @@ def _read_header(self, data):
self.write('HTTP/1.1 500 Internal Server Error%s' % CRLF)
self.write('Content-Length: 0%s' % DOUBLE_CRLF)
self.close()
return

try:
# Call the request handler.
Expand Down
11 changes: 0 additions & 11 deletions pants/http/utils.py
Expand Up @@ -20,24 +20,13 @@
# Imports
###############################################################################

import base64
import hashlib
import hmac
import logging
import mimetypes
import os
import re

from datetime import datetime

if os.name == 'nt':
from time import clock as time
else:
from time import time

from pants import Stream, Server
from pants import __version__ as pants_version
from pants.engine import Engine

###############################################################################
# Logging
Expand Down
11 changes: 3 additions & 8 deletions pants/http/websocket.py
Expand Up @@ -25,10 +25,12 @@
import logging
import re
import struct
from pants.stream import StreamBufferOverflow

from pants.stream import StreamBufferOverflow
from pants.util.struct_delimiter import struct_delimiter

from pants.http.utils import log


###############################################################################
# Constants
Expand Down Expand Up @@ -70,13 +72,6 @@
RegexType = type(re.compile(""))


###############################################################################
# Logging
###############################################################################

log = logging.getLogger(__name__)


###############################################################################
# WebSocket Class
###############################################################################
Expand Down
2 changes: 1 addition & 1 deletion pants/web/utils.py
Expand Up @@ -26,7 +26,7 @@
import string
import sys

from pants.http import HTTP, SERVER, SERVER_URL
from pants.http.utils import HTTP, SERVER, SERVER_URL


try:
Expand Down

0 comments on commit 45790ad

Please sign in to comment.