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

nette / http / 26462513624

26 May 2026 04:53PM UTC coverage: 83.183% (-0.2%) from 83.409%
26462513624

push

github

dg
added CLAUDE.md

920 of 1106 relevant lines covered (83.18%)

0.83 hits per line

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

94.12
/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
                if (!($ipBin = inet_pton($ip)) || !($maskBin = inet_pton($mask))) {
1✔
46
                        return false;
×
47
                }
48

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

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

60

61
        /**
62
         * Sends the strict same-site cookie used to detect same-site requests.
63
         */
64
        public static function initCookie(IRequest $request, IResponse $response): void
1✔
65
        {
66
                $response->setCookie(self::StrictCookieName, '1', 0, '/', sameSite: IResponse::SameSiteStrict);
1✔
67
        }
1✔
68
}
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