httpperf 1.0rc3__tar.gz → 1.0rc4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. {httpperf-1.0rc3/httpperf.egg-info → httpperf-1.0rc4}/PKG-INFO +2 -2
  2. {httpperf-1.0rc3 → httpperf-1.0rc4}/README.md +1 -1
  3. {httpperf-1.0rc3 → httpperf-1.0rc4}/httpperf/__init__.py +40 -24
  4. httpperf-1.0rc4/httpperf/version.py +1 -0
  5. {httpperf-1.0rc3 → httpperf-1.0rc4/httpperf.egg-info}/PKG-INFO +2 -2
  6. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/ae.c +1 -0
  7. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/httpperf.c +164 -11
  8. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_connections.py +26 -0
  9. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_limiter.py +52 -0
  10. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_response.py +36 -0
  11. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_tls.py +62 -8
  12. httpperf-1.0rc3/httpperf/version.py +0 -1
  13. {httpperf-1.0rc3 → httpperf-1.0rc4}/LICENSE +0 -0
  14. {httpperf-1.0rc3 → httpperf-1.0rc4}/MANIFEST.in +0 -0
  15. {httpperf-1.0rc3 → httpperf-1.0rc4}/NOTICE +0 -0
  16. {httpperf-1.0rc3 → httpperf-1.0rc4}/httpperf.egg-info/SOURCES.txt +0 -0
  17. {httpperf-1.0rc3 → httpperf-1.0rc4}/httpperf.egg-info/dependency_links.txt +0 -0
  18. {httpperf-1.0rc3 → httpperf-1.0rc4}/httpperf.egg-info/top_level.txt +0 -0
  19. {httpperf-1.0rc3 → httpperf-1.0rc4}/pyproject.toml +0 -0
  20. {httpperf-1.0rc3 → httpperf-1.0rc4}/setup.cfg +0 -0
  21. {httpperf-1.0rc3 → httpperf-1.0rc4}/setup.py +0 -0
  22. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/.clang-format +0 -0
  23. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/ae.h +0 -0
  24. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/ae_epoll.c +0 -0
  25. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/ae_evport.c +0 -0
  26. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/ae_kqueue.c +0 -0
  27. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/ae_select.c +0 -0
  28. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/atomicvar.h +0 -0
  29. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/config.h +0 -0
  30. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/zmalloc.c +0 -0
  31. {httpperf-1.0rc3 → httpperf-1.0rc4}/src/ae/zmalloc.h +0 -0
  32. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_api.py +0 -0
  33. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_cancel.py +0 -0
  34. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_cookie.py +0 -0
  35. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_engine.py +0 -0
  36. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_http_versions.py +0 -0
  37. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_httpbin.py +0 -0
  38. {httpperf-1.0rc3 → httpperf-1.0rc4}/tests/test_leaks.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpperf
3
- Version: 1.0rc3
3
+ Version: 1.0rc4
4
4
  Summary: An asyncio HTTP client for load testing, built on libcurl.
5
5
  Author-email: Tony Simpson <agjasimpson@gmail.com>
6
6
  Project-URL: Source, https://github.com/tonysimpson/httpperf
@@ -105,7 +105,7 @@ All arguments are keyword-only. A session can be created anywhere, even outside
105
105
  | `cert` | `None` | `(certfile, keyfile)` paths for a client certificate, used by every request unless the request overrides it. |
106
106
  | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Cancelling the task (or `asyncio.wait_for`) also stops the transfer and frees its connection slot; on HTTP/1.1 that closes the connection, on HTTP/2 and HTTP/3 it resets only that stream. |
107
107
  | `connect_timeout` | `None` | Seconds allowed to connect, TLS included. `None` means curl's default of 300 s. |
108
- | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CA bundle: `SSL_CERT_FILE` if set, otherwise libcurl's own default, or the usual system locations if it has none. A path: check against that CA bundle. Requests can override it. |
108
+ | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CAs, from the first of: `SSL_CERT_FILE` and `SSL_CERT_DIR`, if set and present; libcurl's own default bundle, if present; Python's default bundle (`ssl.get_default_verify_paths()`); the usual system locations. A path: check against that CA bundle. Requests can override it. |
109
109
  | `json_dumps` | `None` | Function that turns a `json=` argument into the request body (`str` or `bytes`). `None` means the engine's. |
110
110
  | `json_loads` | `None` | Function that turns the response body (`bytes`) into the result of `json()`. `None` means the engine's. |
111
111
 
@@ -79,7 +79,7 @@ All arguments are keyword-only. A session can be created anywhere, even outside
79
79
  | `cert` | `None` | `(certfile, keyfile)` paths for a client certificate, used by every request unless the request overrides it. |
80
80
  | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Cancelling the task (or `asyncio.wait_for`) also stops the transfer and frees its connection slot; on HTTP/1.1 that closes the connection, on HTTP/2 and HTTP/3 it resets only that stream. |
81
81
  | `connect_timeout` | `None` | Seconds allowed to connect, TLS included. `None` means curl's default of 300 s. |
82
- | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CA bundle: `SSL_CERT_FILE` if set, otherwise libcurl's own default, or the usual system locations if it has none. A path: check against that CA bundle. Requests can override it. |
82
+ | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CAs, from the first of: `SSL_CERT_FILE` and `SSL_CERT_DIR`, if set and present; libcurl's own default bundle, if present; Python's default bundle (`ssl.get_default_verify_paths()`); the usual system locations. A path: check against that CA bundle. Requests can override it. |
83
83
  | `json_dumps` | `None` | Function that turns a `json=` argument into the request body (`str` or `bytes`). `None` means the engine's. |
84
84
  | `json_loads` | `None` | Function that turns the response body (`bytes`) into the result of `json()`. `None` means the engine's. |
85
85
 
@@ -5,6 +5,7 @@ import email.utils
5
5
  import http
6
6
  import json as _json
7
7
  import os
8
+ import ssl
8
9
  import sys
9
10
  import threading
10
11
  import time
@@ -26,26 +27,33 @@ _CA_FILES = (
26
27
  "/etc/ssl/certs/ca-certificates.crt",
27
28
  "/etc/pki/tls/certs/ca-bundle.crt",
28
29
  "/etc/ssl/ca-bundle.pem",
30
+ "/etc/pki/tls/cacert.pem",
29
31
  "/etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem",
30
32
  "/etc/ssl/cert.pem",
31
33
  )
32
34
 
33
35
 
34
- def _find_ca_file():
35
- env = os.environ.get("SSL_CERT_FILE")
36
- if env:
37
- return env
36
+ def _find_ca():
37
+ paths = ssl.get_default_verify_paths()
38
+ ca_file = os.environ.get(paths.openssl_cafile_env)
39
+ ca_dir = os.environ.get(paths.openssl_capath_env)
40
+ ca_file = ca_file if ca_file and os.path.isfile(ca_file) else None
41
+ ca_dir = ca_dir if ca_dir and os.path.isdir(ca_dir) else None
42
+ if ca_file or ca_dir:
43
+ return ca_file, ca_dir
38
44
  builtin = _httpperf.default_ca_file()
39
45
  if builtin and os.path.exists(builtin):
40
- return None
41
- return next((path for path in _CA_FILES if os.path.exists(path)), None)
46
+ return None, None
47
+ if paths.cafile and os.path.isfile(paths.cafile):
48
+ return paths.cafile, None
49
+ return next((path for path in _CA_FILES if os.path.exists(path)), None), None
42
50
 
43
51
 
44
- _ca_file = _find_ca_file()
52
+ _ca = _find_ca()
45
53
 
46
54
 
47
55
  def _verify(verify):
48
- return _ca_file if verify is True and _ca_file is not None else verify
56
+ return _ca if verify is True and _ca != (None, None) else verify
49
57
 
50
58
 
51
59
  class RequestError(Exception):
@@ -367,6 +375,9 @@ class Headers(collections.abc.Mapping):
367
375
  return repr(dict(self.items()))
368
376
 
369
377
 
378
+ _UNSET = object()
379
+
380
+
370
381
  class Response:
371
382
  __slots__ = (
372
383
  "_req",
@@ -391,6 +402,15 @@ class Response:
391
402
  self._resp = resp
392
403
  self._start_time = start_time
393
404
  self._end_time = end_time
405
+ self._redirect_url = _UNSET
406
+ self._prev = None
407
+ self._body = None
408
+ self._text = None
409
+ self._header = None
410
+ self._headers_tuple = None
411
+ self._headers = None
412
+ self._encoding = None
413
+ self._json = _UNSET
394
414
 
395
415
  @property
396
416
  def request(self):
@@ -432,7 +452,7 @@ class Response:
432
452
 
433
453
  @property
434
454
  def redirect_url(self):
435
- if not hasattr(self, "_redirect_url"):
455
+ if self._redirect_url is _UNSET:
436
456
  self._redirect_url = self._resp.get_redirect_url()
437
457
  return self._redirect_url
438
458
 
@@ -509,55 +529,51 @@ class Response:
509
529
  @property
510
530
  def history(self):
511
531
  result = []
512
- cur = getattr(self, "_prev", None)
532
+ cur = self._prev
513
533
  while cur is not None:
514
534
  result.append(cur)
515
- cur = getattr(cur, "_prev", None)
535
+ cur = cur._prev
516
536
  result.reverse()
517
537
  return result
518
538
 
519
539
  @property
520
540
  def body(self):
521
- if not hasattr(self, "_body"):
541
+ if self._body is None:
522
542
  self._body = self._resp.get_body()
523
543
  return self._body
524
544
 
525
545
  @property
526
546
  def encoding(self):
527
- if not hasattr(self, "_encoding"):
547
+ if self._encoding is None:
528
548
  self._encoding = _encoding_from_content_type(self.headers.get("content-type"))
529
549
  return self._encoding
530
550
 
531
551
  @encoding.setter
532
552
  def encoding(self, encoding):
533
553
  self._encoding = encoding
534
- if hasattr(self, "_text"):
535
- del self._text
554
+ self._text = None
536
555
 
537
556
  @property
538
557
  def text(self):
539
- if not hasattr(self, "_text"):
558
+ if self._text is None:
540
559
  self._text = self.body.decode(self.encoding)
541
560
  return self._text
542
561
 
543
562
  def json(self):
544
- if not hasattr(self, "_json"):
563
+ if self._json is _UNSET:
545
564
  self._json = self._json_loads(self.body)
546
565
  return self._json
547
566
 
548
567
  @property
549
568
  def headers(self):
550
- if not hasattr(self, "_headers"):
569
+ if self._headers is None:
551
570
  self._headers = Headers(self.headers_tuple)
552
571
  return self._headers
553
572
 
554
573
  @property
555
574
  def headers_tuple(self):
556
- if not hasattr(self, "_headers_tuple"):
557
- block = self._final_header_block()
558
- self._headers_tuple = tuple(
559
- [(name, value.strip()) for name, _, value in [line.partition(":") for line in block.split("\r\n")[1:]]]
560
- )
575
+ if self._headers_tuple is None:
576
+ self._headers_tuple = _httpperf.parse_headers(self._resp.get_header())
561
577
  return self._headers_tuple
562
578
 
563
579
  def _final_header_block(self):
@@ -566,7 +582,7 @@ class Response:
566
582
 
567
583
  @property
568
584
  def header(self):
569
- if not hasattr(self, "_header"):
585
+ if self._header is None:
570
586
  self._header = self._resp.get_header().decode("latin-1")
571
587
  return self._header
572
588
 
@@ -0,0 +1 @@
1
+ __version__ = "1.0rc4"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpperf
3
- Version: 1.0rc3
3
+ Version: 1.0rc4
4
4
  Summary: An asyncio HTTP client for load testing, built on libcurl.
5
5
  Author-email: Tony Simpson <agjasimpson@gmail.com>
6
6
  Project-URL: Source, https://github.com/tonysimpson/httpperf
@@ -105,7 +105,7 @@ All arguments are keyword-only. A session can be created anywhere, even outside
105
105
  | `cert` | `None` | `(certfile, keyfile)` paths for a client certificate, used by every request unless the request overrides it. |
106
106
  | `timeout` | `None` | Seconds allowed for each request, from the start of the transfer to the last byte (each redirect hop separately). `None` or 0 means no limit. Cancelling the task (or `asyncio.wait_for`) also stops the transfer and frees its connection slot; on HTTP/1.1 that closes the connection, on HTTP/2 and HTTP/3 it resets only that stream. |
107
107
  | `connect_timeout` | `None` | Seconds allowed to connect, TLS included. `None` means curl's default of 300 s. |
108
- | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CA bundle: `SSL_CERT_FILE` if set, otherwise libcurl's own default, or the usual system locations if it has none. A path: check against that CA bundle. Requests can override it. |
108
+ | `verify` | `False` | Server certificate checking. `False`: none, as most load tests want. `True`: check against the system CAs, from the first of: `SSL_CERT_FILE` and `SSL_CERT_DIR`, if set and present; libcurl's own default bundle, if present; Python's default bundle (`ssl.get_default_verify_paths()`); the usual system locations. A path: check against that CA bundle. Requests can override it. |
109
109
  | `json_dumps` | `None` | Function that turns a `json=` argument into the request body (`str` or `bytes`). `None` means the engine's. |
110
110
  | `json_loads` | `None` | Function that turns the response body (`bytes`) into the result of `json()`. `None` means the engine's. |
111
111
 
@@ -426,6 +426,7 @@ int aeProcessEvents(aeEventLoop *eventLoop, int flags)
426
426
  DEBUG_PRINT("fd=%d fe->mask=%d", fd, fe->mask);
427
427
  rfired = 1;
428
428
  fe->rfileProc(eventLoop,fd,fe->clientData,mask);
429
+ fe = &eventLoop->events[fd];
429
430
  }
430
431
  if (fe->mask & mask & AE_WRITABLE) {
431
432
  if (!rfired || fe->wfileProc != fe->rfileProc)
@@ -84,6 +84,8 @@ typedef struct {
84
84
 
85
85
  typedef struct HostSlot {
86
86
  char *key;
87
+ char *prefix;
88
+ size_t prefix_len;
87
89
  long active;
88
90
  long waiting;
89
91
  bool multiplexed;
@@ -170,6 +172,7 @@ static void share_decref(Share *share) {
170
172
  while (share->hosts != NULL) {
171
173
  HostSlot *next = share->hosts->next;
172
174
  free(share->hosts->key);
175
+ free(share->hosts->prefix);
173
176
  free(share->hosts);
174
177
  share->hosts = next;
175
178
  }
@@ -274,6 +277,7 @@ typedef struct AcRequestData {
274
277
  char *key_file;
275
278
  bool verify;
276
279
  char *ca_file;
280
+ char *ca_path;
277
281
  long timeout_ms;
278
282
  long connect_timeout_ms;
279
283
  char error[CURL_ERROR_SIZE];
@@ -586,17 +590,37 @@ static void host_prune(Share *share) {
586
590
  }
587
591
  host_unlink(share, h);
588
592
  free(h->key);
593
+ free(h->prefix);
589
594
  free(h);
590
595
  }
591
596
  }
592
597
 
598
+ static size_t url_prefix(const char *url) {
599
+ size_t n = strspn(url, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.");
600
+ if (n == 0 || strncmp(url + n, "://", 3) != 0) {
601
+ return 0;
602
+ }
603
+ return n + 3 + strcspn(url + n + 3, "/?#");
604
+ }
605
+
593
606
  static HostSlot *host_slot(Share *share, const char *url) {
594
607
  host_prune(share);
608
+ size_t len = url_prefix(url);
609
+ HostSlot *h = share->hosts;
610
+ if (len > 0) {
611
+ while (h != NULL && (h->prefix_len != len || memcmp(h->prefix, url, len) != 0)) {
612
+ h = h->next;
613
+ }
614
+ if (h != NULL) {
615
+ host_touch(share, h);
616
+ return h;
617
+ }
618
+ h = share->hosts;
619
+ }
595
620
  char *key = host_key(url);
596
621
  if (key == NULL) {
597
622
  return NULL;
598
623
  }
599
- HostSlot *h = share->hosts;
600
624
  while (h != NULL && strcmp(h->key, key) != 0) {
601
625
  h = h->next;
602
626
  }
@@ -610,6 +634,13 @@ static HostSlot *host_slot(Share *share, const char *url) {
610
634
  }
611
635
  h->key = key;
612
636
  }
637
+ char *prefix = len > 0 ? malloc(len) : NULL;
638
+ if (prefix != NULL) {
639
+ memcpy(prefix, url, len);
640
+ free(h->prefix);
641
+ h->prefix = prefix;
642
+ h->prefix_len = len;
643
+ }
613
644
  host_touch(share, h);
614
645
  return h;
615
646
  }
@@ -948,6 +979,9 @@ void start_request(struct aeEventLoop *eventLoop, int fd, void *clientData, int
948
979
  if (rd->ca_file != NULL) {
949
980
  curl_easy_setopt(rd->curl, CURLOPT_CAINFO, rd->ca_file);
950
981
  }
982
+ if (rd->ca_path != NULL) {
983
+ curl_easy_setopt(rd->curl, CURLOPT_CAPATH, rd->ca_path);
984
+ }
951
985
  } else {
952
986
  curl_easy_setopt(rd->curl, CURLOPT_SSL_VERIFYPEER, 0L);
953
987
  curl_easy_setopt(rd->curl, CURLOPT_SSL_VERIFYHOST, 0L);
@@ -986,6 +1020,8 @@ void start_request(struct aeEventLoop *eventLoop, int fd, void *clientData, int
986
1020
  rd->key_file = NULL;
987
1021
  free(rd->ca_file);
988
1022
  rd->ca_file = NULL;
1023
+ free(rd->ca_path);
1024
+ rd->ca_path = NULL;
989
1025
  free(rd->cookies_str);
990
1026
  rd->cookies_str = NULL;
991
1027
  if (rd->slot != NULL && !slot_free(rd->limiter, rd->slot)) {
@@ -1287,6 +1323,7 @@ static PyObject *EventLoop_abandon(EventLoop *self, PyObject *args) {
1287
1323
  free(rd->cert_file);
1288
1324
  free(rd->key_file);
1289
1325
  free(rd->ca_file);
1326
+ free(rd->ca_path);
1290
1327
  free(rd->cookies_str);
1291
1328
  Py_XDECREF(rd->cookies);
1292
1329
  free(rd->req_data_buf);
@@ -1678,19 +1715,37 @@ static PyObject *Session_request(Session *self, PyObject *args, PyObject *kwds)
1678
1715
  CHECK_ALLOC(rd->cert_file);
1679
1716
  CHECK_ALLOC(rd->key_file);
1680
1717
  }
1681
- if (verify == Py_True) {
1682
- rd->verify = true;
1683
- } else if (PyUnicode_CheckExact(verify)) {
1684
- const char *ca_file = PyUnicode_AsUTF8(verify);
1685
- if (ca_file == NULL) {
1718
+ PyObject *ca_file = Py_None;
1719
+ PyObject *ca_path = Py_None;
1720
+ if (PyUnicode_CheckExact(verify)) {
1721
+ ca_file = verify;
1722
+ } else if (PyTuple_CheckExact(verify) && PyTuple_GET_SIZE(verify) == 2) {
1723
+ ca_file = PyTuple_GET_ITEM(verify, 0);
1724
+ ca_path = PyTuple_GET_ITEM(verify, 1);
1725
+ }
1726
+ if ((verify != Py_True && verify != Py_False && ca_file == Py_None && ca_path == Py_None) ||
1727
+ (ca_file != Py_None && !PyUnicode_CheckExact(ca_file)) ||
1728
+ (ca_path != Py_None && !PyUnicode_CheckExact(ca_path))) {
1729
+ PyErr_SetString(PyExc_ValueError,
1730
+ "verify should be True, False, a CA bundle path or a (CA file, CA directory) tuple");
1731
+ goto error_cleanup;
1732
+ }
1733
+ rd->verify = verify != Py_False;
1734
+ if (ca_file != Py_None) {
1735
+ const char *path = PyUnicode_AsUTF8(ca_file);
1736
+ if (path == NULL) {
1686
1737
  goto error_cleanup;
1687
1738
  }
1688
- rd->verify = true;
1689
- rd->ca_file = strdup(ca_file);
1739
+ rd->ca_file = strdup(path);
1690
1740
  CHECK_ALLOC(rd->ca_file);
1691
- } else if (verify != Py_False) {
1692
- PyErr_SetString(PyExc_ValueError, "verify should be True, False or a CA bundle path");
1693
- goto error_cleanup;
1741
+ }
1742
+ if (ca_path != Py_None) {
1743
+ const char *path = PyUnicode_AsUTF8(ca_path);
1744
+ if (path == NULL) {
1745
+ goto error_cleanup;
1746
+ }
1747
+ rd->ca_path = strdup(path);
1748
+ CHECK_ALLOC(rd->ca_path);
1694
1749
  }
1695
1750
  if (cookies != Py_None) {
1696
1751
  Py_INCREF(cookies);
@@ -1759,6 +1814,7 @@ error_cleanup:
1759
1814
  free(rd->cert_file);
1760
1815
  free(rd->key_file);
1761
1816
  free(rd->ca_file);
1817
+ free(rd->ca_path);
1762
1818
  if (rd->cookies) {
1763
1819
  Py_DECREF(rd->cookies);
1764
1820
  free(rd->cookies_str);
@@ -1902,6 +1958,102 @@ static PyTypeObject SessionType = {
1902
1958
 
1903
1959
  const static char MODULE_NAME[] = "_httpperf";
1904
1960
 
1961
+ static const char *find_crlf(const char *p, const char *end) {
1962
+ while (p < end && (p = memchr(p, '\r', end - p)) != NULL && p + 1 < end) {
1963
+ if (p[1] == '\n') {
1964
+ return p;
1965
+ }
1966
+ p++;
1967
+ }
1968
+ return NULL;
1969
+ }
1970
+
1971
+ static const char *find_blank_line(const char *p, const char *end) {
1972
+ while ((p = find_crlf(p, end)) != NULL) {
1973
+ if (end - p >= 4 && p[2] == '\r' && p[3] == '\n') {
1974
+ return p;
1975
+ }
1976
+ p++;
1977
+ }
1978
+ return NULL;
1979
+ }
1980
+
1981
+ static bool is_latin1_space(unsigned char c) {
1982
+ return (c >= 0x09 && c <= 0x0d) || (c >= 0x1c && c <= 0x20) || c == 0x85 || c == 0xa0;
1983
+ }
1984
+
1985
+ static PyObject *header_pair(const char *line, const char *end) {
1986
+ const char *colon = memchr(line, ':', end - line);
1987
+ const char *value = colon != NULL ? colon + 1 : end;
1988
+ const char *value_end = end;
1989
+ while (value < value_end && is_latin1_space(*value)) {
1990
+ value++;
1991
+ }
1992
+ while (value_end > value && is_latin1_space(value_end[-1])) {
1993
+ value_end--;
1994
+ }
1995
+ PyObject *pair = PyTuple_New(2);
1996
+ if (pair == NULL) {
1997
+ return NULL;
1998
+ }
1999
+ PyObject *name_str = PyUnicode_DecodeLatin1(line, (colon != NULL ? colon : end) - line, NULL);
2000
+ if (name_str == NULL) {
2001
+ Py_DECREF(pair);
2002
+ return NULL;
2003
+ }
2004
+ PyTuple_SET_ITEM(pair, 0, name_str);
2005
+ PyObject *value_str = PyUnicode_DecodeLatin1(value, value_end - value, NULL);
2006
+ if (value_str == NULL) {
2007
+ Py_DECREF(pair);
2008
+ return NULL;
2009
+ }
2010
+ PyTuple_SET_ITEM(pair, 1, value_str);
2011
+ return pair;
2012
+ }
2013
+
2014
+ static PyObject *module_parse_headers(PyObject *self, PyObject *arg) {
2015
+ if (!PyBytes_Check(arg)) {
2016
+ PyErr_SetString(PyExc_TypeError, "parse_headers expects bytes");
2017
+ return NULL;
2018
+ }
2019
+ const char *start = PyBytes_AS_STRING(arg);
2020
+ const char *end = start + PyBytes_GET_SIZE(arg);
2021
+ const char *block = NULL;
2022
+ const char *block_end = NULL;
2023
+ for (;;) {
2024
+ const char *blank = find_blank_line(start, end);
2025
+ const char *stop = blank != NULL ? blank : end;
2026
+ if (stop > start) {
2027
+ block = start;
2028
+ block_end = stop;
2029
+ }
2030
+ if (blank == NULL) {
2031
+ break;
2032
+ }
2033
+ start = blank + 4;
2034
+ }
2035
+ PyObject *pairs = PyList_New(0);
2036
+ if (pairs == NULL) {
2037
+ return NULL;
2038
+ }
2039
+ const char *line = block != NULL ? find_crlf(block, block_end) : NULL;
2040
+ while (line != NULL) {
2041
+ line += 2;
2042
+ const char *next = find_crlf(line, block_end);
2043
+ PyObject *pair = header_pair(line, next != NULL ? next : block_end);
2044
+ if (pair == NULL || PyList_Append(pairs, pair) == -1) {
2045
+ Py_XDECREF(pair);
2046
+ Py_DECREF(pairs);
2047
+ return NULL;
2048
+ }
2049
+ Py_DECREF(pair);
2050
+ line = next;
2051
+ }
2052
+ PyObject *result = PyList_AsTuple(pairs);
2053
+ Py_DECREF(pairs);
2054
+ return result;
2055
+ }
2056
+
1905
2057
  static PyObject *module_default_ca_file(PyObject *self, PyObject *args) {
1906
2058
  #if LIBCURL_VERSION_NUM >= 0x075400
1907
2059
  CURL *curl = curl_easy_init();
@@ -1933,6 +2085,7 @@ static PyObject *module_set_host_idle_seconds(PyObject *self, PyObject *arg) {
1933
2085
  static PyMethodDef module_methods[] = {
1934
2086
  {"_set_host_idle_seconds", module_set_host_idle_seconds, METH_O, "How long idle host records are kept, for tests"},
1935
2087
  {"default_ca_file", module_default_ca_file, METH_NOARGS, "libcurl's built-in CA bundle path, or None"},
2088
+ {"parse_headers", module_parse_headers, METH_O, "The (name, value) pairs of the final header block"},
1936
2089
  {NULL, NULL, 0, NULL}};
1937
2090
 
1938
2091
  static struct PyModuleDef _httpperf_module = {PyModuleDef_HEAD_INIT, MODULE_NAME, NULL, -1, module_methods};
@@ -276,3 +276,29 @@ def test_proxy_environment_is_read_per_session(url, monkeypatch):
276
276
  assert get(uppercase) == 200
277
277
  el.close()
278
278
  loop.close()
279
+
280
+
281
+ def test_many_new_connections_while_responses_arrive(server):
282
+ async def run(engine):
283
+ busy = httpperf.Session(engine=engine)
284
+ done = False
285
+
286
+ async def traffic():
287
+ while not done:
288
+ assert (await busy.get(server.url + "/")).status_code == 200
289
+
290
+ workers = [asyncio.create_task(traffic()) for _ in range(20)]
291
+ sessions = [httpperf.Session(engine=engine, connections="session") for _ in range(350)]
292
+ responses = await asyncio.gather(*(s.get(server.url + "/") for s in sessions))
293
+ for s in sessions:
294
+ s.close()
295
+ assert [r.status_code for r in responses] == [200] * 350
296
+ done = True
297
+ await asyncio.gather(*workers)
298
+
299
+ async def main():
300
+ for _ in range(4):
301
+ async with httpperf.Engine() as engine:
302
+ await run(engine)
303
+
304
+ asyncio.run(main())
@@ -185,3 +185,55 @@ def test_busy_host_records_are_kept(server, short_host_idle):
185
185
  return busy
186
186
 
187
187
  assert asyncio.run(main()) == {"active": 1, "waiting": 1, "multiplexed": False}
188
+
189
+
190
+ def test_spellings_of_a_host_share_its_slots(server):
191
+ port = server.url.rsplit(":", 1)[1]
192
+ spellings = [
193
+ server.url + "/slow",
194
+ "HTTP://127.0.0.1:%s/slow?a=1" % port,
195
+ "http://user:pw@127.0.0.1:%s/slow" % port,
196
+ "http://127.0.0.1:%s?b=2#c" % port,
197
+ server.url + "/slow",
198
+ server.url + "/slow",
199
+ ]
200
+
201
+ async def main():
202
+ async with httpperf.Session(http_version="HTTP/1.1", connections="session", max_host_connections=1) as s:
203
+ state = s._session._limiter_state
204
+ tasks = [asyncio.create_task(s.get(url)) for url in spellings]
205
+ busy = await wait_until(state, lambda st: st["hosts"] and st["hosts"][key(server.url)]["waiting"] == 5)
206
+ server.release.set()
207
+ await asyncio.gather(*tasks)
208
+ return busy, check(state())
209
+
210
+ busy, idle = asyncio.run(main())
211
+ assert list(busy["hosts"]) == [key(server.url)]
212
+ assert busy["hosts"][key(server.url)]["active"] == 1
213
+ assert idle["hosts"] == {key(server.url): {"active": 0, "waiting": 0, "multiplexed": False}}
214
+
215
+
216
+ def test_host_keys_of_unusual_urls():
217
+ urls = [
218
+ "http://[::1]:9/",
219
+ "http://127.0.0.1:9?q=1",
220
+ "http://127.0.0.1:9#f",
221
+ "http://u@127.0.0.2:9/",
222
+ "http://127.0.0.1:90/",
223
+ ]
224
+
225
+ async def main():
226
+ async with httpperf.Session(connections="session", max_host_connections=1) as s:
227
+ for url in urls + urls:
228
+ with pytest.raises(httpperf.RequestError):
229
+ await s.get(url)
230
+ return check(s._session._limiter_state())
231
+
232
+ state = asyncio.run(main())
233
+ assert sorted(state["hosts"]) == [
234
+ "http://127.0.0.1:9",
235
+ "http://127.0.0.1:90",
236
+ "http://127.0.0.2:9",
237
+ "http://[::1]:9",
238
+ ]
239
+ assert all(h["active"] == h["waiting"] == 0 for h in state["hosts"].values())
@@ -1,3 +1,5 @@
1
+ import random
2
+
1
3
  import pytest
2
4
 
3
5
  import httpperf
@@ -73,3 +75,37 @@ def test_headers():
73
75
  assert list(r.headers) == ["Content-Type", "X-Name", "X-Dup", "X-Tight"]
74
76
  assert r.headers.get("missing", "default") == "default"
75
77
  assert dict(r.headers) == {"Content-Type": "text/plain", "X-Name": "café", "X-Dup": "a, b", "X-Tight": "v"}
78
+
79
+
80
+ def python_parse_headers(header):
81
+ blocks = [b for b in header.decode("latin-1").split("\r\n\r\n") if b]
82
+ block = blocks[-1] if blocks else ""
83
+ return tuple((name, value.strip()) for name, _, value in (line.partition(":") for line in block.split("\r\n")[1:]))
84
+
85
+
86
+ @pytest.mark.parametrize(
87
+ "header",
88
+ [
89
+ b"",
90
+ b"\r\n\r\n",
91
+ b"HTTP/1.1 200 OK",
92
+ b"HTTP/1.1 200 OK\r\n",
93
+ b"HTTP/1.1 200 OK\r\n\r\n",
94
+ b"HTTP/1.1 200 OK\r\nA: b\r\n\r\n\r\n",
95
+ b"HTTP/1.1 200 OK\r\nA: b\r\n\r\n\r\n\r\nHTTP/1.1 204 No Content\r\nC:d\r\n\r\n",
96
+ b"HTTP/1.1 200 OK\r\nNo colon\r\n:\r\nA:\r\n x: folded\r\n\r\n",
97
+ b"HTTP/1.1 200 OK\r\nA: \t\x0b\x0c\x1c\x1f\x85\xa0 v \xa0\x85\r\n\r\n",
98
+ b"HTTP/1.1 200 OK\r\nA: b\rc\nd\r\n\r\n",
99
+ b"HTTP/1.1 200 OK\r\nA: b:c\r\n\r",
100
+ ],
101
+ )
102
+ def test_parse_headers_matches_python(header):
103
+ assert httpperf._httpperf.parse_headers(header) == python_parse_headers(header)
104
+
105
+
106
+ def test_parse_headers_matches_python_on_random_input():
107
+ rng = random.Random(1)
108
+ alphabet = b"\r\n\r\n:: \t\x0b\x1c\x85\xa0aZ"
109
+ for _ in range(20000):
110
+ header = bytes(rng.choice(alphabet) for _ in range(rng.randrange(40)))
111
+ assert httpperf._httpperf.parse_headers(header) == python_parse_headers(header), header
@@ -139,14 +139,68 @@ def test_verify(tls_url):
139
139
  loop.close()
140
140
 
141
141
 
142
- def test_ca_file_lookup(monkeypatch, tmp_path):
143
- monkeypatch.setenv("SSL_CERT_FILE", "/custom/ca.pem")
144
- assert httpperf._find_ca_file() == "/custom/ca.pem"
145
- monkeypatch.delenv("SSL_CERT_FILE")
146
- monkeypatch.setattr(httpperf._httpperf, "default_ca_file", lambda: None)
142
+ @pytest.fixture
143
+ def ca_lookup(monkeypatch, tmp_path):
144
+ for name in ("SSL_CERT_FILE", "SSL_CERT_DIR"):
145
+ monkeypatch.delenv(name, raising=False)
147
146
  bundle = tmp_path / "ca.pem"
148
147
  bundle.write_text("")
148
+ python_bundle = tmp_path / "python.pem"
149
+ python_bundle.write_text("")
150
+ paths = ssl.DefaultVerifyPaths(
151
+ str(python_bundle), str(tmp_path), "SSL_CERT_FILE", str(python_bundle), "SSL_CERT_DIR", str(tmp_path)
152
+ )
153
+ monkeypatch.setattr(ssl, "get_default_verify_paths", lambda: paths)
154
+ monkeypatch.setattr(httpperf._httpperf, "default_ca_file", lambda: None)
149
155
  monkeypatch.setattr(httpperf, "_CA_FILES", ("/missing/ca.pem", str(bundle)))
150
- assert httpperf._find_ca_file() == str(bundle)
151
- monkeypatch.setattr(httpperf._httpperf, "default_ca_file", lambda: str(bundle))
152
- assert httpperf._find_ca_file() is None
156
+ return tmp_path
157
+
158
+
159
+ def test_ca_lookup_uses_the_environment_first(ca_lookup, monkeypatch):
160
+ monkeypatch.setattr(httpperf._httpperf, "default_ca_file", lambda: str(ca_lookup / "ca.pem"))
161
+ monkeypatch.setenv("SSL_CERT_FILE", str(ca_lookup / "ca.pem"))
162
+ assert httpperf._find_ca() == (str(ca_lookup / "ca.pem"), None)
163
+ monkeypatch.setenv("SSL_CERT_DIR", str(ca_lookup))
164
+ assert httpperf._find_ca() == (str(ca_lookup / "ca.pem"), str(ca_lookup))
165
+ monkeypatch.delenv("SSL_CERT_FILE")
166
+ assert httpperf._find_ca() == (None, str(ca_lookup))
167
+
168
+
169
+ def test_ca_lookup_ignores_missing_environment_paths(ca_lookup, monkeypatch):
170
+ monkeypatch.setenv("SSL_CERT_FILE", "/missing/ca.pem")
171
+ monkeypatch.setenv("SSL_CERT_DIR", "/missing/certs")
172
+ assert httpperf._find_ca() == (str(ca_lookup / "python.pem"), None)
173
+
174
+
175
+ def test_ca_lookup_leaves_libcurls_own_bundle_alone(ca_lookup, monkeypatch):
176
+ monkeypatch.setattr(httpperf._httpperf, "default_ca_file", lambda: str(ca_lookup / "ca.pem"))
177
+ assert httpperf._find_ca() == (None, None)
178
+ monkeypatch.setattr(httpperf._httpperf, "default_ca_file", lambda: "/missing/ca.pem")
179
+ assert httpperf._find_ca() == (str(ca_lookup / "python.pem"), None)
180
+
181
+
182
+ def test_ca_lookup_uses_pythons_bundle_without_its_directory(ca_lookup):
183
+ assert httpperf._find_ca() == (str(ca_lookup / "python.pem"), None)
184
+
185
+
186
+ def test_ca_lookup_falls_back_to_known_bundles(ca_lookup, monkeypatch):
187
+ (ca_lookup / "python.pem").unlink()
188
+ assert httpperf._find_ca() == (str(ca_lookup / "ca.pem"), None)
189
+ monkeypatch.setattr(httpperf, "_CA_FILES", ("/missing/ca.pem",))
190
+ assert httpperf._find_ca() == (None, None)
191
+
192
+
193
+ def test_verify_with_ssl_cert_dir(tls_url, monkeypatch, tmp_path):
194
+ with open(pytest_httpbin.certs.where()) as f:
195
+ (tmp_path / "ca.pem").write_text(f.read())
196
+ subprocess.run(["openssl", "rehash", str(tmp_path)], check=True, capture_output=True)
197
+ monkeypatch.delenv("SSL_CERT_FILE", raising=False)
198
+ monkeypatch.setenv("SSL_CERT_DIR", str(tmp_path))
199
+ monkeypatch.setattr(httpperf, "_ca", httpperf._find_ca())
200
+ assert httpperf._ca == (None, str(tmp_path))
201
+ loop = asyncio.new_event_loop()
202
+ el = httpperf.Engine()
203
+ response = loop.run_until_complete(httpperf.Session(engine=el, verify=True).get(tls_url))
204
+ assert response.status_code == 200
205
+ el.close()
206
+ loop.close()
@@ -1 +0,0 @@
1
- __version__ = "1.0rc3"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes