Check if fast C parser is available and should be used. Returns False if: - http_parser='python' is explicitly set - gunicorn_h1c is not installed (in 'auto' mode) - gunicorn_h1c < 0.4.1 (in 'auto' mode) - Incompatible compatibility flags are enabled (in 'auto' mode) Raises
(cfg)
| 33 | |
| 34 | |
| 35 | def _check_fast_parser(cfg): |
| 36 | """Check if fast C parser is available and should be used. |
| 37 | |
| 38 | Returns False if: |
| 39 | - http_parser='python' is explicitly set |
| 40 | - gunicorn_h1c is not installed (in 'auto' mode) |
| 41 | - gunicorn_h1c < 0.4.1 (in 'auto' mode) |
| 42 | - Incompatible compatibility flags are enabled (in 'auto' mode) |
| 43 | |
| 44 | Raises RuntimeError if: |
| 45 | - http_parser='fast' but gunicorn_h1c is not installed |
| 46 | - http_parser='fast' but gunicorn_h1c < 0.4.1 |
| 47 | - http_parser='fast' but incompatible flags are enabled |
| 48 | """ |
| 49 | global _fast_parser_available, _fast_parser_module # pylint: disable=global-statement |
| 50 | |
| 51 | parser_setting = getattr(cfg, 'http_parser', 'auto') |
| 52 | if parser_setting == 'python': |
| 53 | return False |
| 54 | |
| 55 | if _fast_parser_available is None: |
| 56 | try: |
| 57 | import gunicorn_h1c |
| 58 | _fast_parser_available = True |
| 59 | _fast_parser_module = gunicorn_h1c |
| 60 | except ImportError: |
| 61 | _fast_parser_available = False |
| 62 | |
| 63 | if not _fast_parser_available and parser_setting == 'fast': |
| 64 | raise RuntimeError("gunicorn_h1c not installed but http_parser='fast'") |
| 65 | |
| 66 | if not _fast_parser_available: |
| 67 | return False |
| 68 | |
| 69 | # Require >= 0.4.1 for limit enforcement |
| 70 | if not hasattr(_fast_parser_module, 'LimitRequestLine'): |
| 71 | if parser_setting == 'fast': |
| 72 | raise RuntimeError( |
| 73 | "gunicorn_h1c >= 0.4.1 required for http_parser='fast'. " |
| 74 | "Please upgrade: pip install --upgrade gunicorn_h1c" |
| 75 | ) |
| 76 | # In 'auto' mode, fall back to Python parser |
| 77 | return False |
| 78 | |
| 79 | # Check for incompatible compatibility flags |
| 80 | incompatible = [] |
| 81 | for flag in _FAST_PARSER_INCOMPATIBLE_FLAGS: |
| 82 | if getattr(cfg, flag, False): |
| 83 | incompatible.append(flag) |
| 84 | |
| 85 | if incompatible: |
| 86 | if parser_setting == 'fast': |
| 87 | raise RuntimeError( |
| 88 | "http_parser='fast' is incompatible with compatibility flags: %s. " |
| 89 | "Use http_parser='python' or disable these flags." |
| 90 | % ', '.join(incompatible) |
| 91 | ) |
| 92 | # In 'auto' mode, fall back to Python parser |