• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / http / 22837209177

09 Mar 2026 03:32AM UTC coverage: 83.513% (-0.1%) from 83.62%
22837209177

push

github

dg
added CLAUDE.md

932 of 1116 relevant lines covered (83.51%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.74
/src/Http/Helpers.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Nette\Http;
9

10
use Nette;
11
use Nette\Utils\DateTime;
12
use function array_map, explode, implode, inet_pton, sprintf, strlen, strncmp, unpack;
13

14

15
/**
16
 * Rendering helpers for HTTP.
17
 */
18
final class Helpers
19
{
20
        use Nette\StaticClass;
21

22
        /** @internal */
23
        public const StrictCookieName = '_nss';
24

25
        /** @deprecated */
26
        public const STRICT_COOKIE_NAME = self::StrictCookieName;
27

28

29
        /**
30
         * Formats a date and time in the HTTP date format (RFC 7231), e.g. 'Mon, 23 Jan 1978 10:00:00 GMT'.
31
         */
32
        public static function formatDate(string|int|\DateTimeInterface $time): string
1✔
33
        {
34
                $time = DateTime::from($time)->setTimezone(new \DateTimeZone('GMT'));
1✔
35
                return $time->format('D, d M Y H:i:s \G\M\T');
1✔
36
        }
37

38

39
        /**
40
         * Checks whether an IP address falls within a CIDR block (e.g. '192.168.1.0/24').
41
         */
42
        public static function ipMatch(string $ip, string $mask): bool
1✔
43
        {
44
                [$mask, $size] = explode('/', $mask . '/');
1✔
45
                $ipBin = inet_pton($ip);
1✔
46
                $maskBin = inet_pton($mask);
1✔
47
                if ($ipBin === false || $maskBin === false) {
1✔
48
                        return false;
×
49
                }
50

51
                $tmp = fn(int $n): string => sprintf('%032b', $n);
1✔
52
                $ip = implode('', array_map($tmp, unpack('N*', $ipBin) ?: []));
1✔
53
                $mask = implode('', array_map($tmp, unpack('N*', $maskBin) ?: []));
1✔
54
                $max = strlen($ip);
1✔
55
                if (!$max || $max !== strlen($mask) || (int) $size < 0 || (int) $size > $max) {
1✔
56
                        return false;
1✔
57
                }
58

59
                return strncmp($ip, $mask, $size === '' ? $max : (int) $size) === 0;
1✔
60
        }
61

62

63
        /**
64
         * Sends the strict same-site cookie used to detect same-site requests.
65
         */
66
        public static function initCookie(IRequest $request, IResponse $response): void
1✔
67
        {
68
                $response->setCookie(self::StrictCookieName, '1', 0, '/', sameSite: IResponse::SameSiteStrict);
1✔
69
        }
1✔
70
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc