~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/http/_urllib2_wrappers.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2007-07-03 07:35:46 UTC
  • mfrom: (2574.1.1 ianc-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20070703073546-9dv8bocak8u3ou6m
(Vincent Ladeuil) Fix #115209 - Unable to handle http code 400: Bad Request When issuing too many ranges

Show diffs side-by-side

added added

removed removed

Lines of Context:
153
153
 
154
154
    def __init__(self, method, url, data=None, headers={},
155
155
                 origin_req_host=None, unverifiable=False,
156
 
                 connection=None, parent=None,):
 
156
                 connection=None, parent=None,
 
157
                 accepted_errors=None):
157
158
        urllib2.Request.__init__(self, url, data, headers,
158
159
                                 origin_req_host, unverifiable)
159
160
        self.method = method
160
161
        self.connection = connection
 
162
        self.accepted_errors = accepted_errors
161
163
        # To handle redirections
162
164
        self.parent = parent
163
165
        self.redirected_to = None
1138
1140
    instead, we leave our Transport handle them.
1139
1141
    """
1140
1142
 
 
1143
    accepted_errors = [200, # Ok
 
1144
                       206, # Partial content
 
1145
                       404, # Not found
 
1146
                       ]
 
1147
    """The error codes the caller will handle.
 
1148
 
 
1149
    This can be specialized in the request on a case-by case basis, but the
 
1150
    common cases are covered here.
 
1151
    """
 
1152
 
1141
1153
    def http_response(self, request, response):
1142
1154
        code, msg, hdrs = response.code, response.msg, response.info()
1143
1155
 
1144
 
        if code not in (200, # Ok
1145
 
                        206, # Partial content
1146
 
                        404, # Not found
1147
 
                        ):
 
1156
        accepted_errors = request.accepted_errors
 
1157
        if accepted_errors is None:
 
1158
            accepted_errors = self.accepted_errors
 
1159
 
 
1160
        if code not in accepted_errors:
1148
1161
            response = self.parent.error('http', request, response,
1149
1162
                                         code, msg, hdrs)
1150
1163
        return response
1156
1169
    """Translate common errors into bzr Exceptions"""
1157
1170
 
1158
1171
    def http_error_default(self, req, fp, code, msg, hdrs):
1159
 
        if code == 404:
1160
 
            raise errors.NoSuchFile(req.get_selector(),
1161
 
                                    extra=HTTPError(req.get_full_url(),
1162
 
                                                    code, msg,
1163
 
                                                    hdrs, fp))
1164
 
        elif code == 403:
 
1172
        if code == 403:
1165
1173
            raise errors.TransportError('Server refuses to fullfil the request')
1166
1174
        elif code == 416:
1167
1175
            # We don't know which, but one of the ranges we
1173
1181
                                             'Unable to handle http code %d: %s'
1174
1182
                                             % (code, msg))
1175
1183
 
 
1184
 
1176
1185
class Opener(object):
1177
1186
    """A wrapper around urllib2.build_opener
1178
1187
 
1196
1205
            HTTPSHandler,
1197
1206
            HTTPDefaultErrorHandler,
1198
1207
            )
 
1208
 
1199
1209
        self.open = self._opener.open
1200
1210
        if DEBUG >= 2:
1201
1211
            # When dealing with handler order, it's easy to mess