| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 80a68f9 commit a221d7b
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,6 +28,7 @@ | |||
| 28 | 28 | import gitlab.config | |
| 29 | 29 | from gitlab.const import * # noqa | |
| 30 | 30 | from gitlab.exceptions import * # noqa | |
| 31 | + from gitlab import utils # noqa | ||
| 31 | 32 | ||
| 32 | 33 | __title__ = 'python-gitlab' | |
| 33 | 34 | __version__ = '1.5.1' | |
@@ -39,6 +40,9 @@ | |||
| 39 | 40 | warnings.filterwarnings('default', category=DeprecationWarning, | |
| 40 | 41 | module='^gitlab') | |
| 41 | 42 | ||
| 43 | + REDIRECT_MSG = ('python-gitlab detected an http to https redirection. You ' | ||
| 44 | + 'must update your GitLab URL to use https:// to avoid issues.') | ||
| 45 | + | ||
| 42 | 46 | ||
| 43 | 47 | def _sanitize(value): | |
| 44 | 48 | if isinstance(value, dict): | |
@@ -394,6 +398,26 @@ def _build_url(self, path): | |||
| 394 | 398 | else: | |
| 395 | 399 | return '%s%s' % (self._url, path) | |
| 396 | 400 | ||
| 401 | + def _check_redirects(self, result): | ||
| 402 | + # Check the requests history to detect http to https redirections. | ||
| 403 | + # If the initial verb is POST, the next request will use a GET request, | ||
| 404 | + # leading to an unwanted behaviour. | ||
| 405 | + # If the initial verb is PUT, the data will not be send with the next | ||
| 406 | + # request. | ||
| 407 | + # If we detect a redirection to https with a POST or a PUT request, we | ||
| 408 | + # raise an exception with a useful error message. | ||
| 409 | + if result.history and self._base_url.startswith('http:'): | ||
| 410 | + for item in result.history: | ||
| 411 | + if item.status_code not in (301, 302): | ||
| 412 | + continue | ||
| 413 | + # GET methods can be redirected without issue | ||
| 414 | + if result.request.method == 'GET': | ||
| 415 | + continue | ||
| 416 | + # Did we end-up with an https:// URL? | ||
| 417 | + location = item.headers.get('Location', None) | ||
| 418 | + if location and location.startswith('https://'): | ||
| 419 | + raise RedirectError(REDIRECT_MSG) | ||
| 420 | + | ||
| 397 | 421 | def http_request(self, verb, path, query_data={}, post_data=None, | |
| 398 | 422 | streamed=False, files=None, **kwargs): | |
| 399 | 423 | """Make an HTTP request to the Gitlab server. | |
@@ -417,27 +441,11 @@ def http_request(self, verb, path, query_data={}, post_data=None, | |||
| 417 | 441 | GitlabHttpError: When the return code is not 2xx | |
| 418 | 442 | """ | |
| 419 | 443 | ||
| 420 | - def sanitized_url(url): | ||
| 421 | - parsed = six.moves.urllib.parse.urlparse(url) | ||
| 422 | - new_path = parsed.path.replace('.', '%2E') | ||
| 423 | - return parsed._replace(path=new_path).geturl() | ||
| 424 | - | ||
| 425 | 444 | url = self._build_url(path) | |
| 426 | 445 | ||
| 427 | - def copy_dict(dest, src): | ||
| 428 | - for k, v in src.items(): | ||
| 429 | - if isinstance(v, dict): | ||
| 430 | - # Transform dict values in new attributes. For example: | ||
| 431 | - # custom_attributes: {'foo', 'bar'} => | ||
| 432 | - # custom_attributes['foo']: 'bar' | ||
| 433 | - for dict_k, dict_v in v.items(): | ||
| 434 | - dest['%s[%s]' % (k, dict_k)] = dict_v | ||
| 435 | - else: | ||
| 436 | - dest[k] = v | ||
| 437 | - | ||
| 438 | 446 | params = {} | |
| 439 | - copy_dict(params, query_data) | ||
| 440 | - copy_dict(params, kwargs) | ||
| 447 | + utils.copy_dict(params, query_data) | ||
| 448 | + utils.copy_dict(params, kwargs) | ||
| 441 | 449 | ||
| 442 | 450 | opts = self._get_session_opts(content_type='application/json') | |
| 443 | 451 | ||
@@ -462,7 +470,7 @@ def copy_dict(dest, src): | |||
| 462 | 470 | req = requests.Request(verb, url, json=json, data=data, params=params, | |
| 463 | 471 | files=files, **opts) | |
| 464 | 472 | prepped = self.session.prepare_request(req) | |
| 465 | - prepped.url = sanitized_url(prepped.url) | ||
| 473 | + prepped.url = utils.sanitized_url(prepped.url) | ||
| 466 | 474 | settings = self.session.merge_environment_settings( | |
| 467 | 475 | prepped.url, {}, streamed, verify, None) | |
| 468 | 476 | ||
@@ -472,6 +480,8 @@ def copy_dict(dest, src): | |||
| 472 | 480 | while True: | |
| 473 | 481 | result = self.session.send(prepped, timeout=timeout, **settings) | |
| 474 | 482 | ||
| 483 | + self._check_redirects(result) | ||
| 484 | + | ||
| 475 | 485 | if 200 <= result.status_code < 300: | |
| 476 | 486 | return result | |
| 477 | 487 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,10 @@ class GitlabAuthenticationError(GitlabError): | |||
| 41 | 41 | pass | |
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | + class RedirectError(GitlabError): | ||
| 45 | + pass | ||
| 46 | + | ||
| 47 | + | ||
| 44 | 48 | class GitlabParsingError(GitlabError): | |
| 45 | 49 | pass | |
| 46 | 50 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -15,6 +15,8 @@ | |||
| 15 | 15 | # You should have received a copy of the GNU Lesser General Public License | |
| 16 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | 17 | ||
| 18 | + import six | ||
| 19 | + | ||
| 18 | 20 | ||
| 19 | 21 | class _StdoutStream(object): | |
| 20 | 22 | def __call__(self, chunk): | |
@@ -31,3 +33,21 @@ def response_content(response, streamed, action, chunk_size): | |||
| 31 | 33 | for chunk in response.iter_content(chunk_size=chunk_size): | |
| 32 | 34 | if chunk: | |
| 33 | 35 | action(chunk) | |
| 36 | + | ||
| 37 | + | ||
| 38 | + def copy_dict(dest, src): | ||
| 39 | + for k, v in src.items(): | ||
| 40 | + if isinstance(v, dict): | ||
| 41 | + # Transform dict values to new attributes. For example: | ||
| 42 | + # custom_attributes: {'foo', 'bar'} => | ||
| 43 | + # "custom_attributes['foo']": "bar" | ||
| 44 | + for dict_k, dict_v in v.items(): | ||
| 45 | + dest['%s[%s]' % (k, dict_k)] = dict_v | ||
| 46 | + else: | ||
| 47 | + dest[k] = v | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + def sanitized_url(url): | ||
| 51 | + parsed = six.moves.urllib.parse.urlparse(url) | ||
| 52 | + new_path = parsed.path.replace('.', '%2E') | ||
| 53 | + return parsed._replace(path=new_path).geturl() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments