Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #11
0
 def test_parse_empty(self):
     disptype, params = parse_content_disposition(None)
     assert disptype is None
     assert {} == params
Пример #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
Пример #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
Пример #14
0
 def test_attonlyucase(self) -> None:
     disptype, params = parse_content_disposition("ATTACHMENT")
     assert "attachment" == disptype
     assert {} == params
Пример #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
Пример #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
Пример #17
0
 def test_attonlyquoted(self):
     with pytest.warns(aiohttp.BadContentDispositionHeader):
         disptype, params = parse_content_disposition('"attachment"')
     assert disptype is None
     assert {} == params
Пример #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
Пример #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
Пример #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
Пример #21
0
 def test_attonly(self):
     disptype, params = parse_content_disposition('attachment')
     assert 'attachment' == disptype
     assert {} == params
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #29
0
 def test_parse_empty(self) -> None:
     disptype, params = parse_content_disposition(None)
     assert disptype is None
     assert {} == params
Пример #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
Пример #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
Пример #32
0
 def test_attonlyucase(self):
     disptype, params = parse_content_disposition('ATTACHMENT')
     assert 'attachment' == disptype
     assert {} == params
Пример #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
Пример #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
Пример #35
0
 def test_attonly(self) -> None:
     disptype, params = parse_content_disposition("attachment")
     assert "attachment" == disptype
     assert {} == params
Пример #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
Пример #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
Пример #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
Пример #39
0
 def test_attonlyquoted(self) -> None:
     with pytest.warns(aiohttp.BadContentDispositionHeader):
         disptype, params = parse_content_disposition('"attachment"')
     assert disptype is None
     assert {} == params
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #46
0
 def test_inlwithasciifilenamepdf(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename="foo.pdf"')
     assert "attachment" == disptype
     assert {"filename": "foo.pdf"} == params
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #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
Пример #55
0
 def test_attwithasciifilename35(self) -> None:
     disptype, params = parse_content_disposition(
         'attachment; filename="00000000001111111111222222222233333"')
     assert "attachment" == disptype
     assert {"filename": "00000000001111111111222222222233333"} == params
Пример #56
0
 def test_inlwithasciifilenamepdf(self):
     disptype, params = parse_content_disposition(
         'attachment; filename="foo.pdf"')
     assert 'attachment' == disptype
     assert {'filename': 'foo.pdf'} == params
Пример #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
Пример #58
0
 def test_attwithasciifilename35(self):
     disptype, params = parse_content_disposition(
         'attachment; filename="00000000001111111111222222222233333"')
     assert 'attachment' == disptype
     assert {'filename': '00000000001111111111222222222233333'} == params
Пример #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
Пример #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