Ejemplo n.º 1
0
 def test_attfnboth3(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4;"
         " filename*=ISO-8859-1''currency-sign%3d%a4")
     assert 'attachment' == disptype
     assert {'filename*': 'currency-sign=¤',
             'filename*0*': "ISO-8859-15''euro-sign%3d%a4"} == params
Ejemplo n.º 2
0
 def test_attmdate(self):
     disptype, params = parse_content_disposition(
         'attachment; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"')
     assert 'attachment' == disptype
     assert {
         'modification-date': 'Wed, 12 Feb 1997 16:29:51 -0500'
     } == params
Ejemplo n.º 3
0
 def test_attmdate(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"')
     assert "attachment" == disptype
     assert {
         "modification-date": "Wed, 12 Feb 1997 16:29:51 -0500"
     } == params
Ejemplo n.º 4
0
 def test_attfnboth2(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*=UTF-8''foo-%c3%a4.html;"
         ' filename="foo-ae.html"')
     assert 'attachment' == disptype
     assert {'filename': 'foo-ae.html',
             'filename*': 'foo-ä.html'} == params
Ejemplo n.º 5
0
async def download(session, url, *, download_dir=None, params=None, chunk_size=100*1024, overwrite=False):
    async with session.get(url, params=params) as response:
        response.raise_for_status()
        content_disposition = response.headers.get(aiohttp.hdrs.CONTENT_DISPOSITION)
        if content_disposition is None:
            u = response.url
            filename = os.path.basename(u.path)
        else:
            disptype, params = aiohttp.parse_content_disposition(content_disposition)
            filename = params["filename"]
        if download_dir is not None:
            filename = os.path.join(download_dir, filename)

        new_filename = filename
        i = 1
        while not overwrite and os.path.isfile(new_filename):
            path, ext = os.path.splitext(filename)
            new_filename = f"{path}({i}){ext}"
            i += 1
        filename = new_filename

        size = response.headers.get(aiohttp.hdrs.CONTENT_LENGTH)
        size = int(size or chunk_size)
        if size < chunk_size:
            chunk_size = size

        async with aiofiles.open(filename, "wb") as f:
            while True:
                chunk = await response.content.read(chunk_size)
                if not chunk:
                    break
                await f.write(chunk)

        return filename
Ejemplo n.º 6
0
 def test_attfnboth3(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4;"
         " filename*=ISO-8859-1''currency-sign%3d%a4")
     assert 'attachment' == disptype
     assert {
         'filename*': 'currency-sign=¤',
         'filename*0*': "ISO-8859-15''euro-sign%3d%a4"
     } == params
Ejemplo n.º 7
0
 def test_attfnboth3(self) -> None:
     disptype, params = parse_content_disposition(
         "attachment; filename*0*=ISO-8859-15''euro-sign%3d%a4;"
         " filename*=ISO-8859-1''currency-sign%3d%a4")
     assert "attachment" == disptype
     assert {
         "filename*": "currency-sign=¤",
         "filename*0*": "ISO-8859-15''euro-sign%3d%a4",
     } == params
Ejemplo n.º 8
0
 def test_attfncontenc(self):
     disptype, params = parse_content_disposition(
         'attachment; filename*0*=UTF-8'
         'foo-%c3%a4; filename*1=".html"')
     assert 'attachment' == disptype
     assert {
         'filename*0*': 'UTF-8'
         'foo-%c3%a4',
         'filename*1': '.html'
     } == params
Ejemplo n.º 9
0
 def test_attfncontenc(self) -> None:
     disptype, params = parse_content_disposition(
         "attachment; filename*0*=UTF-8"
         'foo-%c3%a4; filename*1=".html"')
     assert "attachment" == disptype
     assert {
         "filename*0*": "UTF-8"
         "foo-%c3%a4",
         "filename*1": ".html"
     } == params
Ejemplo n.º 10
0
 def test_attwithfn2231ws3(self) -> None:
     disptype, params = parse_content_disposition(
         "attachment; filename* =UTF-8''foo-%c3%a4.html")
     assert "attachment" == disptype
     assert {"filename*": "foo-ä.html"} == params
Ejemplo n.º 11
0
 def test_parse_empty(self):
     disptype, params = parse_content_disposition(None)
     assert disptype is None
     assert {} == params
Ejemplo n.º 12
0
 def test_attwithisofn2231iso(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*=iso-8859-1''foo-%E4.html")
     assert 'attachment' == disptype
     assert {'filename*': 'foo-ä.html'} == params
Ejemplo n.º 13
0
 def test_attwithfn2231iso_bad(self):
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*=utf-8''foo-%E4.html")
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 14
0
 def test_attonlyucase(self) -> None:
     disptype, params = parse_content_disposition("ATTACHMENT")
     assert "attachment" == disptype
     assert {} == params
Ejemplo n.º 15
0
 def test_attwithfn2231abspathdisguised(self) -> None:
     disptype, params = parse_content_disposition(
         "attachment; filename*=UTF-8''%5cfoo.html")
     assert "attachment" == disptype
     assert {"filename*": "\\foo.html"} == params
Ejemplo n.º 16
0
 def test_attwithfn2231nbadpct2(self):
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*=UTF-8''f%oo.html")
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 17
0
 def test_attonlyquoted(self):
     with pytest.warns(aiohttp.BadContentDispositionHeader):
         disptype, params = parse_content_disposition('"attachment"')
     assert disptype is None
     assert {} == params
Ejemplo n.º 18
0
 def test_attwithfn2231singleqmissing(self):
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*=UTF-8'foo-%c3%a4.html")
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 19
0
 def test_attwithfn2231quot2(self):
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*=\"foo%20bar.html\"")
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 20
0
 def test_attwithfn2231ws3(self):
     disptype, params = parse_content_disposition(
         "attachment; filename* =UTF-8''foo-%c3%a4.html")
     assert 'attachment' == disptype
     assert {'filename*': 'foo-ä.html'} == params
Ejemplo n.º 21
0
 def test_attonly(self):
     disptype, params = parse_content_disposition('attachment')
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 22
0
 def test_attwithfn2231singleqmissing(self) -> None:
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*=UTF-8'foo-%c3%a4.html")
     assert "attachment" == disptype
     assert {} == params
Ejemplo n.º 23
0
 def test_attwithfn2231nbadpct2(self) -> None:
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*=UTF-8''f%oo.html")
     assert "attachment" == disptype
     assert {} == params
Ejemplo n.º 24
0
 def test_attwithfn2231dpct(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*=UTF-8''A-%2541.html")
     assert 'attachment' == disptype
     assert {'filename*': 'A-%41.html'} == params
Ejemplo n.º 25
0
 def test_attwithfn2231abspathdisguised(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*=UTF-8''%5cfoo.html")
     assert 'attachment' == disptype
     assert {'filename*': '\\foo.html'} == params
Ejemplo n.º 26
0
 def test_attfncontqs(self):
     disptype, params = parse_content_disposition(
         r'attachment; filename*0="foo"; filename*1="\b\a\r.html"')
     assert 'attachment' == disptype
     assert {'filename*0': 'foo',
             'filename*1': 'bar.html'} == params
Ejemplo n.º 27
0
 def test_attfnboth2(self) -> None:
     disptype, params = parse_content_disposition(
         "attachment; filename*=UTF-8''foo-%c3%a4.html;"
         ' filename="foo-ae.html"')
     assert "attachment" == disptype
     assert {"filename": "foo-ae.html", "filename*": "foo-ä.html"} == params
Ejemplo n.º 28
0
 def test_attfncontenc(self):
     disptype, params = parse_content_disposition(
         'attachment; filename*0*=UTF-8''foo-%c3%a4; filename*1=".html"')
     assert 'attachment' == disptype
     assert {'filename*0*': 'UTF-8''foo-%c3%a4',
             'filename*1': '.html'} == params
Ejemplo n.º 29
0
 def test_parse_empty(self) -> None:
     disptype, params = parse_content_disposition(None)
     assert disptype is None
     assert {} == params
Ejemplo n.º 30
0
 def test_attfnconts1(self):
     disptype, params = parse_content_disposition(
         'attachment; filename*0="foo."; filename*2="html"')
     assert 'attachment' == disptype
     assert {'filename*0': 'foo.',
             'filename*2': 'html'} == params
Ejemplo n.º 31
0
 def test_attwithasciifnescapedquote(self):
     disptype, params = parse_content_disposition(
         'attachment; filename="\"quoting\" tested.html"')
     assert 'attachment' == disptype
     assert {'filename': '"quoting" tested.html'} == params
Ejemplo n.º 32
0
 def test_attonlyucase(self):
     disptype, params = parse_content_disposition('ATTACHMENT')
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 33
0
 def test_attwithquotedsemicolon(self):
     disptype, params = parse_content_disposition(
         'attachment; filename="Here\'s a semicolon;.html"')
     assert 'attachment' == disptype
     assert {'filename': 'Here\'s a semicolon;.html'} == params
Ejemplo n.º 34
0
 def test_attfncontord(self):
     disptype, params = parse_content_disposition(
         'attachment; filename*1="bar"; filename*0="foo"')
     assert 'attachment' == disptype
     assert {'filename*0': 'foo',
             'filename*1': 'bar'} == params
Ejemplo n.º 35
0
 def test_attonly(self) -> None:
     disptype, params = parse_content_disposition("attachment")
     assert "attachment" == disptype
     assert {} == params
Ejemplo n.º 36
0
 def test_attwithfn2231utf8comp(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*=UTF-8''foo-a%cc%88.html")
     assert 'attachment' == disptype
     assert {'filename*': 'foo-ä.html'} == params
Ejemplo n.º 37
0
 def test_attwithfn2231quot2(self) -> None:
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             'attachment; filename*="foo%20bar.html"')
     assert "attachment" == disptype
     assert {} == params
Ejemplo n.º 38
0
 def test_attwithfn2231noc(self):
     disptype, params = parse_content_disposition(
         "attachment; filename*=''foo-%c3%a4-%e2%82%ac.html")
     assert 'attachment' == disptype
     assert {'filename*': 'foo-ä-€.html'} == params
Ejemplo n.º 39
0
 def test_attonlyquoted(self) -> None:
     with pytest.warns(aiohttp.BadContentDispositionHeader):
         disptype, params = parse_content_disposition('"attachment"')
     assert disptype is None
     assert {} == params
Ejemplo n.º 40
0
 def test_attnewandfn(self):
     disptype, params = parse_content_disposition(
         'attachment; foobar=x; filename="foo.html"')
     assert 'attachment' == disptype
     assert {'foobar': 'x',
             'filename': 'foo.html'} == params
Ejemplo n.º 41
0
 def test_attwithfn2231dpct(self) -> None:
     disptype, params = parse_content_disposition(
         "attachment; filename*=UTF-8''A-%2541.html")
     assert "attachment" == disptype
     assert {"filename*": "A-%41.html"} == params
Ejemplo n.º 42
0
 def test_attnewandfn(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; foobar=x; filename="foo.html"')
     assert "attachment" == disptype
     assert {"foobar": "x", "filename": "foo.html"} == params
Ejemplo n.º 43
0
 def test_attfncontqs(self) -> None:
     disptype, params = parse_content_disposition(
         r'attachment; filename*0="foo"; filename*1="\b\a\r.html"')
     assert "attachment" == disptype
     assert {"filename*0": "foo", "filename*1": "bar.html"} == params
Ejemplo n.º 44
0
 def test_attrfc2047quoted(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename="=?ISO-8859-1?Q?foo-=E4.html?="')
     assert "attachment" == disptype
     assert {"filename": "=?ISO-8859-1?Q?foo-=E4.html?="} == params
Ejemplo n.º 45
0
 def test_attfnconts1(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename*0="foo."; filename*2="html"')
     assert "attachment" == disptype
     assert {"filename*0": "foo.", "filename*2": "html"} == params
Ejemplo n.º 46
0
 def test_inlwithasciifilenamepdf(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename="foo.pdf"')
     assert "attachment" == disptype
     assert {"filename": "foo.pdf"} == params
Ejemplo n.º 47
0
 def test_attfncontord(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename*1="bar"; filename*0="foo"')
     assert "attachment" == disptype
     assert {"filename*0": "foo", "filename*1": "bar"} == params
Ejemplo n.º 48
0
 def test_attwithasciifnescapedchar(self) -> None:
     disptype, params = parse_content_disposition(
         r'attachment; filename="f\oo.html"')
     assert "attachment" == disptype
     assert {"filename": "foo.html"} == params
Ejemplo n.º 49
0
 def test_attrfc2047token(self):
     with pytest.warns(aiohttp.BadContentDispositionHeader):
         disptype, params = parse_content_disposition(
             'attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=')
     assert disptype is None
     assert {} == params
Ejemplo n.º 50
0
 def test_attwithfn2231utf8_bad(self):
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*=iso-8859-1''foo-%c3%a4-%e2%82%ac.html")
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 51
0
 def test_attrfc2047token(self) -> None:
     with pytest.warns(aiohttp.BadContentDispositionHeader):
         disptype, params = parse_content_disposition(
             "attachment; filename==?ISO-8859-1?Q?foo-=E4.html?=")
     assert disptype is None
     assert {} == params
Ejemplo n.º 52
0
 def test_attrfc2047quoted(self):
     disptype, params = parse_content_disposition(
         'attachment; filename="=?ISO-8859-1?Q?foo-=E4.html?="')
     assert 'attachment' == disptype
     assert {'filename': '=?ISO-8859-1?Q?foo-=E4.html?='} == params
Ejemplo n.º 53
0
 def test_bad_continuous_param(self) -> None:
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             "attachment; filename*0=foo bar")
     assert "attachment" == disptype
     assert {} == params
Ejemplo n.º 54
0
 def test_bad_continuous_param(self):
     with pytest.warns(aiohttp.BadContentDispositionParam):
         disptype, params = parse_content_disposition(
             'attachment; filename*0=foo bar')
     assert 'attachment' == disptype
     assert {} == params
Ejemplo n.º 55
0
 def test_attwithasciifilename35(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename="00000000001111111111222222222233333"')
     assert "attachment" == disptype
     assert {"filename": "00000000001111111111222222222233333"} == params
Ejemplo n.º 56
0
 def test_inlwithasciifilenamepdf(self):
     disptype, params = parse_content_disposition(
         'attachment; filename="foo.pdf"')
     assert 'attachment' == disptype
     assert {'filename': 'foo.pdf'} == params
Ejemplo n.º 57
0
 def test_attwithasciifnescapedquote(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename=""quoting" tested.html"')
     assert "attachment" == disptype
     assert {"filename": '"quoting" tested.html'} == params
Ejemplo n.º 58
0
 def test_attwithasciifilename35(self):
     disptype, params = parse_content_disposition(
         'attachment; filename="00000000001111111111222222222233333"')
     assert 'attachment' == disptype
     assert {'filename': '00000000001111111111222222222233333'} == params
Ejemplo n.º 59
0
 def test_attwithquotedsemicolon(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename="Here\'s a semicolon;.html"')
     assert "attachment" == disptype
     assert {"filename": "Here's a semicolon;.html"} == params
Ejemplo n.º 60
0
 def test_attwithasciifnescapedchar(self):
     disptype, params = parse_content_disposition(
         r'attachment; filename="f\oo.html"')
     assert 'attachment' == disptype
     assert {'filename': 'foo.html'} == params