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 logging 28import os 29from datetime import datetime, timedelta 30import pytest 31 32from testenv import Env, CurlClient 33 34 35log = logging.getLogger(__name__) 36 37 38@pytest.mark.skipif(condition=not Env.have_ssl_curl(), reason="curl without SSL") 39class TestReuse: 40 41 # check if HTTP/1.1 handles 'Connection: close' correctly 42 @pytest.mark.parametrize("proto", ['http/1.1']) 43 def test_12_01_h1_conn_close(self, env: Env, httpd, nghttpx, proto): 44 httpd.clear_extra_configs() 45 httpd.set_extra_config('base', [ 46 'MaxKeepAliveRequests 1', 47 ]) 48 httpd.reload() 49 count = 100 50 curl = CurlClient(env=env) 51 urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]' 52 r = curl.http_download(urls=[urln], alpn_proto=proto) 53 r.check_response(count=count, http_status=200) 54 # Server sends `Connection: close` on every 2nd request, requiring 55 # a new connection 56 delta = 5 57 assert (count/2 - delta) < r.total_connects < (count/2 + delta) 58 59 @pytest.mark.skipif(condition=Env.httpd_is_at_least('2.5.0'), 60 reason="httpd 2.5+ handles KeepAlives different") 61 @pytest.mark.parametrize("proto", ['http/1.1']) 62 def test_12_02_h1_conn_timeout(self, env: Env, httpd, nghttpx, proto): 63 httpd.clear_extra_configs() 64 httpd.set_extra_config('base', [ 65 'KeepAliveTimeout 1', 66 ]) 67 httpd.reload() 68 count = 5 69 curl = CurlClient(env=env) 70 urln = f'https://{env.authority_for(env.domain1, proto)}/data.json?[0-{count-1}]' 71 r = curl.http_download(urls=[urln], alpn_proto=proto, extra_args=[ 72 '--rate', '30/m', 73 ]) 74 r.check_response(count=count, http_status=200) 75 # Connections time out on server before we send another request, 76 assert r.total_connects == count 77 78 @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported") 79 def test_12_03_alt_svc_h2h3(self, env: Env, httpd, nghttpx): 80 httpd.clear_extra_configs() 81 httpd.reload() 82 count = 2 83 # write a alt-svc file the advises h3 instead of h2 84 asfile = os.path.join(env.gen_dir, 'alt-svc-12_03.txt') 85 ts = datetime.now() + timedelta(hours=24) 86 expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}' 87 with open(asfile, 'w') as fd: 88 fd.write(f'h2 {env.domain1} {env.https_port} h3 {env.domain1} {env.https_port} "{expires}" 0 0') 89 log.info(f'altscv: {open(asfile).readlines()}') 90 curl = CurlClient(env=env) 91 urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]' 92 r = curl.http_download(urls=[urln], with_stats=True, extra_args=[ 93 '--alt-svc', f'{asfile}', 94 ]) 95 r.check_response(count=count, http_status=200) 96 # We expect the connection to be reused 97 assert r.total_connects == 1 98 for s in r.stats: 99 assert s['http_version'] == '3', f'{s}' 100 101 @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported") 102 def test_12_04_alt_svc_h3h2(self, env: Env, httpd, nghttpx): 103 httpd.clear_extra_configs() 104 httpd.reload() 105 count = 2 106 # write a alt-svc file the advises h2 instead of h3 107 asfile = os.path.join(env.gen_dir, 'alt-svc-12_04.txt') 108 ts = datetime.now() + timedelta(hours=24) 109 expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}' 110 with open(asfile, 'w') as fd: 111 fd.write(f'h3 {env.domain1} {env.https_port} h2 {env.domain1} {env.https_port} "{expires}" 0 0') 112 log.info(f'altscv: {open(asfile).readlines()}') 113 curl = CurlClient(env=env) 114 urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]' 115 r = curl.http_download(urls=[urln], with_stats=True, extra_args=[ 116 '--alt-svc', f'{asfile}', 117 ]) 118 r.check_response(count=count, http_status=200) 119 # We expect the connection to be reused and use HTTP/2 120 assert r.total_connects == 1 121 for s in r.stats: 122 assert s['http_version'] == '2', f'{s}' 123 124 @pytest.mark.skipif(condition=not Env.have_h3(), reason="h3 not supported") 125 def test_12_05_alt_svc_h3h1(self, env: Env, httpd, nghttpx): 126 httpd.clear_extra_configs() 127 httpd.reload() 128 count = 2 129 # write a alt-svc file the advises h1 instead of h3 130 asfile = os.path.join(env.gen_dir, 'alt-svc-12_05.txt') 131 ts = datetime.now() + timedelta(hours=24) 132 expires = f'{ts.year:04}{ts.month:02}{ts.day:02} {ts.hour:02}:{ts.minute:02}:{ts.second:02}' 133 with open(asfile, 'w') as fd: 134 fd.write(f'h3 {env.domain1} {env.https_port} http/1.1 {env.domain1} {env.https_port} "{expires}" 0 0') 135 log.info(f'altscv: {open(asfile).readlines()}') 136 curl = CurlClient(env=env) 137 urln = f'https://{env.authority_for(env.domain1, "h2")}/data.json?[0-{count-1}]' 138 r = curl.http_download(urls=[urln], with_stats=True, extra_args=[ 139 '--alt-svc', f'{asfile}', 140 ]) 141 r.check_response(count=count, http_status=200) 142 # We expect the connection to be reused and use HTTP/1.1 143 assert r.total_connects == 1 144 for s in r.stats: 145 assert s['http_version'] == '1.1', f'{s}' 146