mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-28 00:02:28 +00:00
Make port optional in X-Forwarded-For
This parser was being overly restrictive which presented integration issues with haproxy which doesn't make it easy to pass the port info
This commit is contained in:
parent
87d7010495
commit
e4ef38403b
3 changed files with 90 additions and 55 deletions
60
test/net/http/parseforwarded_test.c
Normal file
60
test/net/http/parseforwarded_test.c
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "net/http/http.h"
|
||||
|
||||
TEST(ParseForwarded, test) {
|
||||
uint32_t ip = 7;
|
||||
uint16_t port = 7;
|
||||
EXPECT_EQ(-1, ParseForwarded("", -1, &ip, &port));
|
||||
EXPECT_EQ(-1, ParseForwarded("[::1]:123", -1, &ip, &port));
|
||||
EXPECT_EQ(-1, ParseForwarded("203.0.113.0:123123123", -1, &ip, &port));
|
||||
EXPECT_EQ(7, ip);
|
||||
EXPECT_EQ(7, port);
|
||||
EXPECT_EQ(0, ParseForwarded("0.0.0.0", -1, &ip, &port));
|
||||
EXPECT_EQ(0x00000000, ip);
|
||||
EXPECT_EQ(0, port);
|
||||
EXPECT_EQ(0, ParseForwarded("8.8.8.8", -1, &ip, &port));
|
||||
EXPECT_EQ(0x08080808, ip);
|
||||
EXPECT_EQ(0, port);
|
||||
EXPECT_EQ(0, ParseForwarded("0.0.0.1:123", -1, &ip, &port));
|
||||
EXPECT_EQ(0x00000001, ip);
|
||||
EXPECT_EQ(123, port);
|
||||
EXPECT_EQ(0, ParseForwarded("1.2.3.4:123", -1, &ip, &port));
|
||||
EXPECT_EQ(0x01020304, ip);
|
||||
EXPECT_EQ(123, port);
|
||||
EXPECT_EQ(0, ParseForwarded("128.2.3.4:123", -1, &ip, &port));
|
||||
EXPECT_EQ(0x80020304, ip);
|
||||
EXPECT_EQ(123, port);
|
||||
EXPECT_EQ(0, ParseForwarded("255.255.255.255:123", -1, &ip, &port));
|
||||
EXPECT_EQ(0xFFFFFFFF, ip);
|
||||
EXPECT_EQ(123, port);
|
||||
EXPECT_EQ(0, ParseForwarded("203.0.113.0:123", -1, &ip, &port));
|
||||
EXPECT_EQ(0xcb007100, ip);
|
||||
EXPECT_EQ(123, port);
|
||||
EXPECT_EQ(0, ParseForwarded("203.0.113.42:31337", -1, &ip, &port));
|
||||
}
|
||||
|
||||
BENCH(ParseForwarded, bench) {
|
||||
uint32_t ip;
|
||||
uint16_t port;
|
||||
EZBENCH2("ParseForwarded 80", donothing,
|
||||
ParseForwarded("203.0.113.42:31337", 20, &ip, &port));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue