Refactor and improve CTL and other code

This commit is contained in:
Justine Tunney 2024-06-04 05:41:53 -07:00
parent 1d8f37a2f0
commit 9906f299bb
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
25 changed files with 5768 additions and 5350 deletions

View file

@ -1,19 +1,20 @@
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8
//
// Copyright 2024 Mozilla Foundation
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 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.
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 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 "ctl/optional.h"
@ -21,12 +22,16 @@
#include "ctl/string.h"
// #include <optional>
// #include <string>
// #define ctl std
int
main()
{
{
Optional<int> x;
ctl::optional<int> x;
if (x)
return 1;
if (x.has_value())
@ -34,7 +39,7 @@ main()
}
{
Optional<int> x(42);
ctl::optional<int> x(42);
if (!x)
return 3;
if (!x.has_value())
@ -44,8 +49,8 @@ main()
}
{
Optional<String> x("hello");
Optional<String> y(x);
ctl::optional<ctl::string> x("hello");
ctl::optional<ctl::string> y(x);
if (!y)
return 6;
if (!y.has_value())
@ -55,8 +60,8 @@ main()
}
{
Optional<String> x("world");
Optional<String> y(std::move(x));
ctl::optional<ctl::string> x("world");
ctl::optional<ctl::string> y(std::move(x));
if (!y)
return 9;
if (!y.has_value())
@ -66,8 +71,8 @@ main()
}
{
Optional<int> x(42);
Optional<int> y;
ctl::optional<int> x(42);
ctl::optional<int> y;
y = x;
if (!y)
return 13;
@ -78,8 +83,8 @@ main()
}
{
Optional<String> x("hello");
Optional<String> y;
ctl::optional<ctl::string> x("hello");
ctl::optional<ctl::string> y;
y = std::move(x);
if (!y)
return 16;
@ -90,7 +95,7 @@ main()
}
{
Optional<int> x(42);
ctl::optional<int> x(42);
x.reset();
if (x)
return 20;
@ -99,7 +104,7 @@ main()
}
{
Optional<String> x;
ctl::optional<ctl::string> x;
x.emplace("hello");
if (!x)
return 22;

View file

@ -24,14 +24,14 @@
#include "libc/str/str.h"
// #include <string>
// #define String std::string
// #define ctl std
int
main(int argc, char* argv[])
main()
{
{
String s;
ctl::string s;
s += 'h';
s += 'i';
if (s != "hi")
@ -39,7 +39,7 @@ main(int argc, char* argv[])
}
{
String s;
ctl::string s;
if (!s.empty())
return 6;
s.reserve(32);
@ -55,7 +55,7 @@ main(int argc, char* argv[])
}
{
String s;
ctl::string s;
s += "hello world how are you";
s.reserve(3);
if (s != "hello world how are you")
@ -63,7 +63,7 @@ main(int argc, char* argv[])
}
{
String s(4, 'x');
ctl::string s(4, 'x');
if (s != "xxxx")
return 12;
s.resize(3);
@ -75,42 +75,42 @@ main(int argc, char* argv[])
}
{
String a = "a";
String b = "a";
ctl::string a = "a";
ctl::string b = "a";
if (a.compare(b) != 0)
return 17;
}
{
String a = "a";
String b = "b";
ctl::string a = "a";
ctl::string b = "b";
if (a.compare(b) >= 0)
return 18;
}
{
String a = "a";
String b = "ab";
ctl::string a = "a";
ctl::string b = "ab";
if (a.compare(b) >= 0)
return 19;
}
{
String a = "ab";
String b = "a";
ctl::string a = "ab";
ctl::string b = "a";
if (a.compare(b) <= 0)
return 20;
}
{
String a = "";
String b = "";
ctl::string a = "";
ctl::string b = "";
if (a.compare(b) != 0)
return 21;
}
{
String a = "fooBARbaz";
ctl::string a = "fooBARbaz";
if (a.substr(3, 3) != "BAR")
return 22;
if (a.replace(3, 3, "MOO") != "fooMOObaz")
@ -118,7 +118,7 @@ main(int argc, char* argv[])
}
{
String a = "fooBAR";
ctl::string a = "fooBAR";
if (a.substr(3, 3) != "BAR")
return 24;
if (a.replace(3, 3, "MOO") != "fooMOO")
@ -126,7 +126,7 @@ main(int argc, char* argv[])
}
{
String a = "fooBAR";
ctl::string a = "fooBAR";
if (a.substr(1, 0) != "")
return 26;
if (a.replace(1, 0, "MOO") != "fMOOooBAR")
@ -142,15 +142,15 @@ main(int argc, char* argv[])
}
{
String s1 = "hello";
String s2 = "world";
String s3 = s1 + " " + s2;
ctl::string s1 = "hello";
ctl::string s2 = "world";
ctl::string s3 = s1 + " " + s2;
if (s3 != "hello world")
return 32;
}
{
String s = "hello";
ctl::string s = "hello";
if (s.size() != 5)
return 33;
if (s.length() != 5)
@ -160,7 +160,7 @@ main(int argc, char* argv[])
}
{
String s = "hello";
ctl::string s = "hello";
if (s[0] != 'h' || s[1] != 'e' || s[2] != 'l' || s[3] != 'l' ||
s[4] != 'o')
return 36;
@ -170,17 +170,17 @@ main(int argc, char* argv[])
}
{
String s = "hello";
ctl::string s = "hello";
if (s.find('e') != 1)
return 38;
if (s.find('l') != 2)
return 39;
if (s.find('x') != String::npos)
if (s.find('x') != ctl::string::npos)
return 40;
}
{
String s = "hello";
ctl::string s = "hello";
if (!s.ends_with("lo"))
return 41;
if (s.ends_with("el"))
@ -188,8 +188,8 @@ main(int argc, char* argv[])
}
{
String s = "hello";
String sub = s.substr(1, 3);
ctl::string s = "hello";
ctl::string sub = s.substr(1, 3);
if (sub != "ell")
return 43;
sub = s.substr(2);
@ -198,8 +198,8 @@ main(int argc, char* argv[])
}
{
String s = "hello";
String s2 = s;
ctl::string s = "hello";
ctl::string s2 = s;
if (s != s2)
return 45;
s2[0] = 'H';
@ -208,8 +208,8 @@ main(int argc, char* argv[])
}
{
String s = "hello";
String s2 = std::move(s);
ctl::string s = "hello";
ctl::string s2 = std::move(s);
if (s2 != "hello")
return 47;
if (!s.empty())
@ -217,14 +217,14 @@ main(int argc, char* argv[])
}
{
String s = "hello";
ctl::string s = "hello";
const char* cstr = s.c_str();
if (strcmp(cstr, "hello") != 0)
return 49;
}
// {
// String s = "hello";
// ctl::string s = "hello";
// char buffer[10];
// s.copy(buffer, sizeof(buffer));
// if (strcmp(buffer, "hello") != 0)
@ -232,7 +232,7 @@ main(int argc, char* argv[])
// }
{
String s = "hello";
ctl::string s = "hello";
s.resize(3);
if (s != "hel")
return 51;
@ -242,14 +242,14 @@ main(int argc, char* argv[])
}
{
String s = "hello";
ctl::string s = "hello";
s.clear();
if (!s.empty())
return 53;
}
{
String s = "hello";
ctl::string s = "hello";
auto it = s.begin();
if (*it != 'h')
return 54;
@ -259,21 +259,21 @@ main(int argc, char* argv[])
}
// {
// String s = "hello";
// String s2 = "world";
// ctl::string s = "hello";
// ctl::string s2 = "world";
// s.swap(s2);
// if (s != "world" || s2 != "hello")
// return 56;
// }
{
String s = "hello";
ctl::string s = "hello";
if (s.front() != 'h' || s.back() != 'o')
return 57;
}
{
String s = "hello";
ctl::string s = "hello";
s.push_back('!');
if (s != "hello!")
return 58;
@ -283,42 +283,42 @@ main(int argc, char* argv[])
}
{
String s = "hello";
ctl::string s = "hello";
s.insert(2, "XYZ");
if (s != "heXYZllo")
return 60;
}
{
String s = "hello";
ctl::string s = "hello";
s.erase(1, 2);
if (s != "hlo")
return 61;
}
{
String s = "hello";
ctl::string s = "hello";
s.replace(1, 2, "XYZ");
if (s != "hXYZlo")
return 62;
}
{
String s = "hello";
ctl::string s = "hello";
s.append(" world");
if (s != "hello world")
return 63;
}
// {
// String s = "hello";
// ctl::string s = "hello";
// s.assign("world");
// if (s != "world")
// return 64;
// }
{
String s = "hello";
ctl::string s = "hello";
if (s.compare("world") >= 0)
return 65;
if (s.compare("hello") != 0)
@ -328,7 +328,7 @@ main(int argc, char* argv[])
}
{
String s = "hello";
ctl::string s = "hello";
if (s == "world")
return 68;
if (s != "hello")
@ -339,26 +339,6 @@ main(int argc, char* argv[])
return 71;
}
{
StringView s = "hello";
if (s.find('e') != 1)
return 72;
if (s.find('l') != 2)
return 73;
if (s.find('x') != String::npos)
return 74;
}
{
StringView s = "hello there";
s.remove_prefix(6);
if (s != "there")
return 75;
s.remove_suffix(1);
if (s != "ther")
return 76;
}
{
if ("hello"s != "hello")
return 77;

View file

@ -0,0 +1,180 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8
//
// Copyright 2024 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 "ctl/string_view.h"
#include <__utility/move.h>
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
// #include <string_view>
// #define ctl std
int
main(int argc, char* argv[])
{
{
ctl::string_view s = "hello there";
s.remove_prefix(6);
if (s != "there")
return 1;
s.remove_suffix(1);
if (s != "ther")
return 2;
}
{
ctl::string_view s = "hello";
if (s.front() != 'h' || s.back() != 'o')
return 3;
}
{
ctl::string_view s = "hello";
if (s.compare("world") >= 0)
return 4;
if (s.compare("hello") != 0)
return 5;
if (s.compare("hallo") <= 0)
return 6;
}
{
ctl::string_view s = "hello";
if (s.find('e') != 1)
return 7;
if (s.find('l') != 2)
return 8;
if (s.find('x') != ctl::string_view::npos)
return 9;
}
{
ctl::string_view s = "hello";
if (s == "world")
return 10;
if (s != "hello")
return 11;
if (s < "hallo")
return 12;
if (s > "world")
return 13;
}
{
ctl::string_view s = "hello";
auto it = s.begin();
if (*it != 'h')
return 14;
++it;
if (*it != 'e')
return 15;
}
{
ctl::string_view s = "hello";
ctl::string_view s2 = std::move(s);
if (s2 != "hello")
return 16;
if (s.empty())
return 17;
}
{
ctl::string_view s = "hello";
ctl::string_view sub = s.substr(1, 3);
if (sub != "ell")
return 18;
sub = s.substr(2);
if (sub != "llo")
return 19;
}
{
ctl::string_view s = "hello";
if (!s.ends_with("lo"))
return 20;
if (s.ends_with("el"))
return 21;
}
{
ctl::string_view s = "hello";
if (s.find('e') != 1)
return 22;
if (s.find('l') != 2)
return 23;
if (s.find('x') != ctl::string_view::npos)
return 24;
}
{
ctl::string_view s = "hello";
if (s[0] != 'h' || s[1] != 'e' || s[2] != 'l' || s[3] != 'l' ||
s[4] != 'o')
return 25;
}
{
ctl::string_view s = "hello";
if (s.size() != 5)
return 26;
if (s.length() != 5)
return 27;
}
{
ctl::string_view a = "a";
ctl::string_view b = "a";
if (a.compare(b) != 0)
return 28;
}
{
ctl::string_view a = "a";
ctl::string_view b = "b";
if (a.compare(b) >= 0)
return 29;
}
{
ctl::string_view a = "a";
ctl::string_view b = "ab";
if (a.compare(b) >= 0)
return 30;
}
{
ctl::string_view a = "ab";
ctl::string_view b = "a";
if (a.compare(b) <= 0)
return 31;
}
{
ctl::string_view a = "";
ctl::string_view b = "";
if (a.compare(b) != 0)
return 32;
}
CheckForMemoryLeaks();
return 0;
}

View file

@ -1,19 +1,20 @@
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8
//
// Copyright 2024 Mozilla Foundation
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 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.
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 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 "ctl/vector.h"
@ -24,16 +25,15 @@
// #include <string>
// #include <vector>
// #define String std::string
// #define Vector std::vector
// #define ctl std
int
main(int argc, char* argv[])
main()
{
{
int x = 3;
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(x);
@ -48,8 +48,8 @@ main(int argc, char* argv[])
}
{
String yo = "foo";
Vector<String> A;
ctl::string yo = "foo";
ctl::vector<ctl::string> A;
A.push_back("fun");
A.push_back(std::move(yo));
if (yo != "")
@ -66,7 +66,7 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
if (!A.empty())
return 11;
A.push_back(5);
@ -79,11 +79,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(A);
ctl::vector<int> B(A);
if (B.size() != 3)
return 15;
if (B[0] != 1 || B[1] != 2 || B[2] != 3)
@ -91,11 +91,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(std::move(A));
ctl::vector<int> B(std::move(A));
if (A.size() != 0)
return 17;
if (B.size() != 3)
@ -105,11 +105,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
ctl::vector<int> B;
B = A;
if (B.size() != 3)
return 20;
@ -118,11 +118,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
ctl::vector<int> B;
B = std::move(A);
if (A.size() != 0)
return 22;
@ -133,7 +133,7 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
@ -145,7 +145,7 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.resize(5);
if (A.size() != 5)
return 27;
@ -155,11 +155,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
ctl::vector<int> B;
B.push_back(4);
B.push_back(5);
A.swap(B);
@ -174,7 +174,7 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
@ -186,11 +186,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int>::iterator it = A.begin();
ctl::vector<int>::iterator it = A.begin();
if (*it != 1)
return 35;
++it;
@ -205,11 +205,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int>::const_iterator cit = A.cbegin();
ctl::vector<int>::const_iterator cit = A.cbegin();
if (*cit != 1)
return 39;
++cit;
@ -224,7 +224,7 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
for (int i = 0; i < 100; ++i) {
A.push_back(i);
}
@ -237,11 +237,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B(A);
ctl::vector<int> B(A);
if (B.size() != 3)
return 53;
B.push_back(4);
@ -252,7 +252,7 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.reserve(100);
if (A.size() != 0)
return 56;
@ -266,11 +266,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
ctl::vector<int> B;
B = A;
if (B.size() != 3)
return 60;
@ -282,11 +282,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
ctl::vector<int> B;
B = std::move(A);
if (A.size() != 0)
return 63;
@ -297,11 +297,11 @@ main(int argc, char* argv[])
}
{
Vector<int> A;
ctl::vector<int> A;
A.push_back(1);
A.push_back(2);
A.push_back(3);
Vector<int> B;
ctl::vector<int> B;
B.push_back(4);
B.push_back(5);
A.swap(B);

View file

@ -62,18 +62,23 @@ TEST(ParseHttpMessage, soLittleState) {
TEST(ParseHttpMessage, testEmpty_tooShort) {
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(0, ParseHttpMessage(req, "", 0));
EXPECT_EQ(0, ParseHttpMessage(req, "", 0, 1));
}
TEST(ParseHttpMessage, testTooShort) {
TEST(ParseHttpMessage, testShort) {
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(0, ParseHttpMessage(req, "\r\n", 2));
EXPECT_EQ(0, ParseHttpMessage(req, "HT", 2, 32768));
}
TEST(ParseHttpMessage, testBusted) {
InitHttpMessage(req, kHttpRequest);
EXPECT_SYS(EBADMSG, -1, ParseHttpMessage(req, "\r\n", 2, 2));
}
TEST(ParseHttpMessage, testNoHeaders) {
static const char m[] = "GET /foo HTTP/1.0\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("GET", method());
EXPECT_STREQ("/foo", gc(slice(m, req->uri)));
EXPECT_EQ(10, req->version);
@ -86,7 +91,7 @@ Host: foo.example\r\n\
Content-Length: 0\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("POST", method());
EXPECT_STREQ("/foo?bar%20hi", gc(slice(m, req->uri)));
EXPECT_EQ(10, req->version);
@ -98,7 +103,7 @@ Content-Length: 0\r\n\
TEST(ParseHttpMessage, testHttp101) {
static const char m[] = "GET / HTTP/1.1\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("GET", method());
EXPECT_STREQ("/", gc(slice(m, req->uri)));
EXPECT_EQ(11, req->version);
@ -107,7 +112,7 @@ TEST(ParseHttpMessage, testHttp101) {
TEST(ParseHttpMessage, testHttp100) {
static const char m[] = "GET / HTTP/1.0\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("GET", method());
EXPECT_STREQ("/", gc(slice(m, req->uri)));
EXPECT_EQ(10, req->version);
@ -116,40 +121,40 @@ TEST(ParseHttpMessage, testHttp100) {
TEST(ParseHttpMessage, testUnknownMethod_canBeUsedIfYouWant) {
static const char m[] = "#%*+_^ / HTTP/1.0\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("#%*+_^", method());
}
TEST(ParseHttpMessage, testIllegalMethod) {
static const char m[] = "ehd@oruc / HTTP/1.0\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m), strlen(m)));
}
TEST(ParseHttpMessage, testIllegalMethodCasing_weUpperCaseIt) {
static const char m[] = "get / HTTP/1.0\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("GET", method());
}
TEST(ParseHttpMessage, testEmptyMethod_isntAllowed) {
static const char m[] = " / HTTP/1.0\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m), strlen(m)));
}
TEST(ParseHttpMessage, testEmptyUri_isntAllowed) {
static const char m[] = "GET HTTP/1.0\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("GET", method());
}
TEST(ParseHttpMessage, testHttp09) {
static const char m[] = "GET /\r\n\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("GET", method());
EXPECT_STREQ("/", gc(slice(m, req->uri)));
EXPECT_EQ(9, req->version);
@ -158,7 +163,7 @@ TEST(ParseHttpMessage, testHttp09) {
TEST(ParseHttpMessage, testTinyResponse) {
static const char m[] = "HTTP/1.1 429 \r\n\r\n";
InitHttpMessage(req, kHttpResponse);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(429, req->status);
EXPECT_STREQ("", gc(slice(m, req->message)));
}
@ -170,7 +175,7 @@ GET /foo?bar%20hi HTTP/1.0\r\n\
User-Agent: hi\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("/foo?bar%20hi", gc(slice(m, req->uri)));
}
@ -181,7 +186,7 @@ User-Agent: hi\r\n\
there\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(EBADMSG, errno);
}
@ -192,7 +197,7 @@ User-Agent: hi\r\n\
: hi\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(EBADMSG, errno);
}
@ -204,7 +209,7 @@ Content-Length: 0\n\
\n\
\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m) - 1, ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m) - 1, ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("POST", method());
EXPECT_STREQ("/foo?bar%20hi", gc(slice(m, req->uri)));
EXPECT_EQ(10, req->version);
@ -226,7 +231,7 @@ Accept-Encoding: gzip, deflate\r\n\
Accept-Language: en-US,en;q=0.9\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("GET", method());
EXPECT_STREQ("/tool/net/redbean.png", gc(slice(m, req->uri)));
EXPECT_EQ(11, req->version);
@ -243,7 +248,7 @@ GET /foo?bar%20hi HTTP/1.0\r\n\
X-User-Agent: hi\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
ASSERT_EQ(1, req->xheaders.n);
EXPECT_STREQ("X-User-Agent", gc(slice(m, req->xheaders.p[0].k)));
EXPECT_STREQ("hi", gc(slice(m, req->xheaders.p[0].v)));
@ -256,7 +261,7 @@ Content-Type: text/html\r\n\
Content-Type: text/plain\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("text/plain", gc(slice(m, req->headers[kHttpContentType])));
ASSERT_EQ(0, req->xheaders.n);
}
@ -271,7 +276,7 @@ Accept: text/xml\r\n\
Accept: text/css\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("text/html", gc(slice(m, req->headers[kHttpAccept])));
ASSERT_EQ(4, req->xheaders.n);
EXPECT_STREQ("Accept", gc(slice(m, req->xheaders.p[0].k)));
@ -291,7 +296,7 @@ Accept: text/html\r\n\
Accept: text/plain\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("text/html", gc(slice(m, req->headers[kHttpAccept])));
ASSERT_EQ(1, req->xheaders.n);
EXPECT_STREQ("Accept", gc(slice(m, req->xheaders.p[0].k)));
@ -306,7 +311,7 @@ ACCEPT-ENCODING: gzip\r\n\
ACCEPT-encoding: bzip2\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_TRUE(HeaderHas(req, m, kHttpAcceptEncoding, "gzip", -1));
EXPECT_TRUE(HeaderHas(req, m, kHttpAcceptEncoding, "deflate", -1));
EXPECT_FALSE(HeaderHas(req, m, kHttpAcceptEncoding, "funzip", -1));
@ -318,7 +323,7 @@ GET / HTTP/1.1\r\n\
: boop\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(-1, ParseHttpMessage(req, m, strlen(m), strlen(m)));
}
TEST(HeaderHas, testHeaderOnSameLIne) {
@ -327,7 +332,7 @@ GET / HTTP/1.1\r\n\
Accept-Encoding: deflate, gzip, bzip2\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_TRUE(HeaderHas(req, m, kHttpAcceptEncoding, "gzip", -1));
EXPECT_TRUE(HeaderHas(req, m, kHttpAcceptEncoding, "deflate", -1));
EXPECT_FALSE(HeaderHas(req, m, kHttpAcceptEncoding, "funzip", -1));
@ -339,7 +344,7 @@ OPTIONS * HTTP/1.0\r\n\
User-Agent: \t hi there \t \r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_STREQ("hi there", gc(slice(m, req->headers[kHttpUserAgent])));
EXPECT_STREQ("*", gc(slice(m, req->uri)));
}
@ -349,7 +354,7 @@ TEST(ParseHttpMessage, testAbsentHost_setsSliceToZero) {
GET / HTTP/1.1\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(0, req->headers[kHttpHost].a);
EXPECT_EQ(0, req->headers[kHttpHost].b);
}
@ -360,7 +365,7 @@ GET / HTTP/1.1\r\n\
Host:\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_NE(0, req->headers[kHttpHost].a);
EXPECT_EQ(req->headers[kHttpHost].a, req->headers[kHttpHost].b);
}
@ -371,25 +376,25 @@ GET / HTTP/1.1\r\n\
Host: \r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_NE(0, req->headers[kHttpHost].a);
EXPECT_EQ(req->headers[kHttpHost].a, req->headers[kHttpHost].b);
}
TEST(ParseHttpResponse, testEmpty_tooShort) {
InitHttpMessage(req, kHttpResponse);
EXPECT_EQ(0, ParseHttpMessage(req, "", 0));
EXPECT_EQ(0, ParseHttpMessage(req, "", 0, 32768));
}
TEST(ParseHttpResponse, testTooShort) {
InitHttpMessage(req, kHttpResponse);
EXPECT_EQ(0, ParseHttpMessage(req, "\r\n", 2));
EXPECT_EQ(0, ParseHttpMessage(req, "HT", 2, 32768));
}
TEST(ParseHttpResponse, testNoHeaders) {
static const char m[] = "HTTP/1.0 200 OK\r\n\r\n";
InitHttpMessage(req, kHttpResponse);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(200, req->status);
EXPECT_STREQ("OK", gc(slice(m, req->message)));
EXPECT_EQ(10, req->version);
@ -402,7 +407,7 @@ Host: foo.example\r\n\
Content-Length: 0\r\n\
\r\n";
InitHttpMessage(req, kHttpResponse);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(200, req->status);
EXPECT_STREQ("OK", gc(slice(m, req->message)));
EXPECT_EQ(10, req->version);
@ -414,7 +419,7 @@ Content-Length: 0\r\n\
TEST(ParseHttpResponse, testHttp101) {
static const char m[] = "HTTP/1.1 300 OMG\r\n\r\n";
InitHttpMessage(req, kHttpResponse);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(300, req->status);
EXPECT_STREQ("OMG", gc(slice(m, req->message)));
EXPECT_EQ(11, req->version);
@ -423,7 +428,7 @@ TEST(ParseHttpResponse, testHttp101) {
TEST(ParseHttpResponse, testHttp100) {
static const char m[] = "HTTP/1.0 404 Not Found\r\n\r\n";
InitHttpMessage(req, kHttpResponse);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EXPECT_EQ(404, req->status);
EXPECT_STREQ("Not Found", gc(slice(m, req->message)));
EXPECT_EQ(10, req->version);
@ -433,9 +438,8 @@ void DoTiniestHttpRequest(void) {
static const char m[] = "\
GET /\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
ParseHttpMessage(req, m, sizeof(m));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpRequest);
ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m));
}
void DoTinyHttpRequest(void) {
@ -443,9 +447,8 @@ void DoTinyHttpRequest(void) {
GET /\r\n\
Accept-Encoding: gzip\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
ParseHttpMessage(req, m, sizeof(m));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpRequest);
ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m));
}
void DoStandardChromeRequest(void) {
@ -460,9 +463,8 @@ Referer: http://10.10.10.124:8080/\r\n\
Accept-Encoding: gzip, deflate\r\n\
Accept-Language: en-US,en;q=0.9\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m)));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpRequest);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m)));
}
void DoUnstandardChromeRequest(void) {
@ -477,18 +479,16 @@ X-Referer: http://10.10.10.124:8080/\r\n\
X-Accept-Encoding: gzip, deflate\r\n\
X-Accept-Language: en-US,en;q=0.9\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m)));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpRequest);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m)));
}
void DoTiniestHttpResponse(void) {
static const char m[] = "\
HTTP/1.0 200\r\n\
\r\n";
InitHttpMessage(req, kHttpResponse);
ParseHttpMessage(req, m, sizeof(m));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpResponse);
ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m));
}
void DoTinyHttpResponse(void) {
@ -496,9 +496,8 @@ void DoTinyHttpResponse(void) {
HTTP/1.0 200\r\n\
Accept-Encoding: gzip\r\n\
\r\n";
InitHttpMessage(req, kHttpResponse);
ParseHttpMessage(req, m, sizeof(m));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpResponse);
ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m));
}
void DoStandardHttpResponse(void) {
@ -518,9 +517,8 @@ Referrer-Policy: origin\r\n\
Strict-Transport-Security: max-age=31556900\r\n\
Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/ https://cdnjs.cloudflare.com/; frame-src 'self' https://www.google.com/recaptcha/; style-src 'self' 'unsafe-inline'\r\n\
\r\n";
InitHttpMessage(req, kHttpResponse);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m)));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpResponse);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m)));
}
void DoUnstandardHttpResponse(void) {
@ -545,12 +543,12 @@ x-slack-shared-secret-outcome: shared-secret\r\n\
via: envoy-www-iad-qd3r\r\n\
transfer-encoding: chunked\r\n\
\r\n";
InitHttpMessage(req, kHttpResponse);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m)));
DestroyHttpMessage(req);
ResetHttpMessage(req, kHttpResponse);
CHECK_EQ(sizeof(m) - 1, ParseHttpMessage(req, m, sizeof(m) - 1, sizeof(m)));
}
BENCH(ParseHttpMessage, bench) {
InitHttpMessage(req, kHttpRequest);
EZBENCH2("DoTiniestHttpReque", donothing, DoTiniestHttpRequest());
EZBENCH2("DoTinyHttpRequest", donothing, DoTinyHttpRequest());
EZBENCH2("DoStandardChromeRe", donothing, DoStandardChromeRequest());
@ -559,6 +557,7 @@ BENCH(ParseHttpMessage, bench) {
EZBENCH2("DoTinyHttpResponse", donothing, DoTinyHttpResponse());
EZBENCH2("DoStandardHttpResp", donothing, DoStandardHttpResponse());
EZBENCH2("DoUnstandardHttpRe", donothing, DoUnstandardHttpResponse());
DestroyHttpMessage(req);
}
BENCH(HeaderHas, bench) {
@ -572,7 +571,7 @@ ACCEPT-ENCODING: gzip\r\n\
ACCEPT-encoding: bzip2\r\n\
\r\n";
InitHttpMessage(req, kHttpRequest);
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m)));
EXPECT_EQ(strlen(m), ParseHttpMessage(req, m, strlen(m), strlen(m)));
EZBENCH2("HeaderHas txt/pln", donothing,
HeaderHas(req, m, kHttpAccept, "text/plain", 7));
EZBENCH2("HeaderHas deflate", donothing,