Fix ParseHttpMessage failing to store >2 repeatable headers (#657)

This commit is contained in:
Malcolm Seyd 2022-10-10 20:17:17 -07:00 committed by GitHub
parent e557058ac8
commit 84b9b8ed87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View file

@ -236,8 +236,12 @@ int ParseHttpMessage(struct HttpMessage *r, const char *p, size_t n) {
unsigned c2;
struct HttpHeader *p1, *p2;
p1 = r->xheaders.p;
c2 = r->xheaders.c + 2;
c2 = c2 >> 1;
c2 = r->xheaders.c;
if (c2 == 0) {
c2 = 1;
} else {
c2 = c2 * 2;
}
if ((p2 = realloc(p1, c2 * sizeof(*p1)))) {
r->xheaders.p = p2;
r->xheaders.c = c2;

View file

@ -16,11 +16,11 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/bits.h"
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/log/check.h"
#include "libc/mem/mem.h"
#include "libc/mem/gc.internal.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/testlib/ezbench.h"
@ -245,6 +245,29 @@ Content-Type: text/plain\r\n\
ASSERT_EQ(0, req->xheaders.n);
}
TEST(ParseHttpMessage, testCommaSeparatedOnMultipleLines_manyLines) {
static const char m[] = "\
GET / HTTP/1.1\r\n\
Accept: text/html\r\n\
Accept: text/plain\r\n\
Accept: text/csv\r\n\
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_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)));
EXPECT_STREQ("text/plain", gc(slice(m, req->xheaders.p[0].v)));
EXPECT_STREQ("Accept", gc(slice(m, req->xheaders.p[1].k)));
EXPECT_STREQ("text/csv", gc(slice(m, req->xheaders.p[1].v)));
EXPECT_STREQ("Accept", gc(slice(m, req->xheaders.p[2].k)));
EXPECT_STREQ("text/xml", gc(slice(m, req->xheaders.p[2].v)));
EXPECT_STREQ("Accept", gc(slice(m, req->xheaders.p[3].k)));
EXPECT_STREQ("text/css", gc(slice(m, req->xheaders.p[3].v)));
}
TEST(ParseHttpMessage, testCommaSeparatedOnMultipleLines_becomesLinear) {
static const char m[] = "\
GET / HTTP/1.1\r\n\