xref: /curl/tests/http/test_12_reuse.py (revision 433d7303)
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3#***************************************************************************
4#                                  _   _ ____  _
5#  Project                     ___| | | |  _ \| |
6#                             / __| | | | |_) | |
7#                            | (__| |_| |  _ <| |___
8#                             \___|\___/|_| \_\_____|
9#
10# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11#
12# This software is licensed as described in the file COPYING, which
13# you should have received as part of this distribution. The terms
14# are also available at https://curl.se/docs/copyright.html.
15#
16# You may opt to use, copy, modify, merge, publish, distribute and/or sell
17# copies of the Software, and permit persons to whom the Software is
18# furnished to do so, under the terms of the COPYING file.
19#
20# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21# KIND, either express or implied.
22#
23# SPDX-License-Identifier: curl
24#
25###########################################################################
26#
27import difflib
28import filecmp
29import logging
30import os
31from datetime import datetime, timedelta
32import pytest
33
34from testenv import Env, CurlClient
35
36
37log = logging.getLogger(__name__)
38
39
40@pytest.mark.skipif(condition=Env.curl_uses_lib('bearssl'), reason='BearSSL too slow')
41@pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason=f"curl without SSL")
42class TestReuse:
43
44    # check if HTTP/1.1 handles 'Connection: close' correctly
45    @pytest.mark.parametrize("proto", ['http/1.1'])
46    def test_12_01_h1_conn_close(self, env: Env,
47                                 httpd, nghttpx, repeat, proto):
48        httpd.clear_extra_configs()
49        httpd.set_extra_config('base', [
50            f'MaxKeepAliveRequests 1',
51        ])
52        httpd.reload()
53        count = 100
54        curl = CurlClient(env=env)
55        urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
56        r = curl.http_download(urls=[urln], alpn_proto=proto)
57        r.check_response(count=count, http_status=200)
58        # Server sends `Connection: close` on every 2nd request, requiring
59        # a new connection
60        delta = 5
61        assert (count/2 - delta) < r.total_connects < (count/2 + delta)
62
63    @pytest.mark.skipif(condition=Env.httpd_is_at_least('2.5.0'),
64                        reason=f"httpd 2.5+ handles KeepAlives different")
65    @pytest.mark.parametrize("proto", ['http/1.1'])
66    def test_12_02_h1_conn_timeout(self, env: Env,
67                                   httpd, nghttpx, repeat, proto):
68        httpd.clear_extra_configs()
69        httpd.set_extra_config('base', [
70            f'KeepAliveTimeout 1',
71        ])
72        httpd.reload()
73        count = 5
74        curl = CurlClient(env=env)
75        urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]'
76        r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[
77            '--rate', '30/m',
78        ])
79        r.check_response(count=count, http_status=200)
80        # Connections time out on server before we send another request,
81        assert r.total_connects == count
82
83    @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported")
84    def test_12_03_alt_svc_h2h3(self, env: Env, httpd, nghttpx):
85        httpd.clear_extra_configs()
86        httpd.reload()
87        count = 2
88        # write a alt-svc file the advises h3 instead of h2
89        asfile = os.path.join(env.gen_dir, 'alt-svc-12_03.txt')
90        ts = datetime.now() + timedelta(hours=24)
91        expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}'
92        with open(asfile, 'w') as fd:
93            fd.write(f'h2 {env.domain1} {env.https_port} h3 {env.domain1} {env.https_port} "{expires}" 0 0')
94        log.info(f'altscv: {open(asfile).readlines()}')
95        curl = CurlClient(env=env)
96        urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]'
97        r = curl.http_download(urls=[urln], with_stats=True, extra_args=[
98            '--alt-svc', f'{asfile}',
99        ])
100        r.check_response(count=count, http_status=200)
101        # We expect the connection to be reused
102        assert r.total_connects == 1
103        for s in r.stats:
104            assert s['http_version'] == '3', f'{s}'
105
106    def test_12_04_alt_svc_h3h2(self, env: Env, httpd, nghttpx):
107        httpd.clear_extra_configs()
108        httpd.reload()
109        count = 2
110        # write a alt-svc file the advises h2 instead of h3
111        asfile = os.path.join(env.gen_dir, 'alt-svc-12_04.txt')
112        ts = datetime.now() + timedelta(hours=24)
113        expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}'
114        with open(asfile, 'w') as fd:
115            fd.write(f'h3 {env.domain1} {env.https_port} h2 {env.domain1} {env.https_port} "{expires}" 0 0')
116        log.info(f'altscv: {open(asfile).readlines()}')
117        curl = CurlClient(env=env)
118        urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]'
119        r = curl.http_download(urls=[urln], with_stats=True, extra_args=[
120            '--alt-svc', f'{asfile}',
121        ])
122        r.check_response(count=count, http_status=200)
123        # We expect the connection to be reused
124        assert r.total_connects == 1
125        for s in r.stats:
126            assert s['http_version'] == '2', f'{s}'
127
128    def test_12_05_alt_svc_h3h1(self, env: Env, httpd, nghttpx):
129        httpd.clear_extra_configs()
130        httpd.reload()
131        count = 2
132        # write a alt-svc file the advises h1 instead of h3
133        asfile = os.path.join(env.gen_dir, 'alt-svc-12_05.txt')
134        ts = datetime.now() + timedelta(hours=24)
135        expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}'
136        with open(asfile, 'w') as fd:
137            fd.write(f'h3 {env.domain1} {env.https_port} http/1.1 {env.domain1} {env.https_port} "{expires}" 0 0')
138        log.info(f'altscv: {open(asfile).readlines()}')
139        curl = CurlClient(env=env)
140        urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]'
141        r = curl.http_download(urls=[urln], with_stats=True, extra_args=[
142            '--alt-svc', f'{asfile}',
143        ])
144        r.check_response(count=count, http_status=200)
145        # We expect the connection to be reused
146        assert r.total_connects == 1
147        # When using http/1.1 from alt-svc, we ALPN-negotiate 'h2,http/1.1' anyway
148        # which means our server gives us h2
149        for s in r.stats:
150            assert s['http_version'] == '2', f'{s}'
151