psf / psf/requests

Session.get_adapter won't work correctly for case-sensitive URLs

Open
#2,585 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Planned
Dominant language
Python
Stars
54.3k
Forks
10.4k
Avg merge
16h 43m
Merged PRs (30d)
3

Description

In its current implementation Session.get_adapter turns url to lowercase in line 637. If it is aiming for case-insensitive compare it should also turn prefix variable to lowercase in the same line or do that at registration in the mount() method. But this whole idea seems incorrect because URL paths can be case sensitive and one might register different adapters for different paths on the same site.

The fix should most likely be spliting the URL into components and using that tuple as a key in the self.adapters dictionary. Changes mount() and get_adapter().

In [48]: urlparse.urlparse('HTTP://lala.mi.do/path')
Out[48]: ParseResult(scheme='http', netloc='lala.mi.do', path='/path', params='', query='', fragment='')

In [49]: d = {}

In [50]: purl = urlparse.urlparse('HTTP://lala.mi.do/path')

In [51]: d[purl] = 'myadapter'

In [52]: purl = urlparse.urlparse('HTTP://lala.mi.do/Path2')

In [53]: purl
Out[53]: ParseResult(scheme='http', netloc='lala.mi.do', path='/Path2', params='', query='', fragment='')

In [54]: d[purl] = 'myadapter2'

In [55]: d
Out[55]:
{ParseResult(scheme='http', netloc='lala.mi.do', path='/Path2', params='', query='', fragment=''): 'myadapter2',
 ParseResult(scheme='http', netloc='lala.mi.do', path='/path', params='', query='', fragment=''): 'myadapter'}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Session.get_adapter around line 637 and the mount() method, then inspect how self.adapters stores prefixes and how urlparse represents URL components. Reproduce the two case-sensitive paths described in the issue and trace adapter selection. Done means scheme and host matching remain appropriate while distinct path casing can select different registered adapters.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.