Skip to content

Commit

Permalink
Handle application/x-www-form-urlencoded parms.
Browse files Browse the repository at this point in the history
This conforms to RFC 9110 section 8.3.1, Media Type, which stipulates
that the content type can have trailing parameters.  These are now
discarded: prior to this, the request would have been erroneously
passed to the body parser.

Closes #109
  • Loading branch information
kristapsdz committed Sep 15, 2024
1 parent 3424af0 commit fe974ef
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ REGRESS = regress/test-abort-validator \
regress/test-ping \
regress/test-ping-double \
regress/test-post \
regress/test-post-charset \
regress/test-post-charset2 \
regress/test-returncode \
regress/test-template \
regress/test-upload \
Expand Down
8 changes: 8 additions & 0 deletions child.c
Original file line number Diff line number Diff line change
Expand Up @@ -1468,8 +1468,16 @@ kworker_child_body(struct env *env, int fd, size_t envsz,
}

if (cp != NULL) {
/*
* The application/x-www-form-urlencoded can take
* arbitrary parameters (usually "charset", but really
* anything) after the type and subtype. XXX: should
* these be passed into the front-end in any way?
*/
if (strcasecmp(cp, "application/x-www-form-urlencoded") == 0)
parse_pairs_urlenc(pp, b);
else if (strncasecmp(cp, "application/x-www-form-urlencoded;", 34) == 0)
parse_pairs_urlenc(pp, b);
else if (strncasecmp(cp, "multipart/form-data", 19) == 0)
parse_multi(pp, cp + 19, b, bsz);
else if (meth == KMETHOD_POST && strcasecmp(cp, "text/plain") == 0)
Expand Down
71 changes: 71 additions & 0 deletions regress/test-post-charset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and 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 "../config.h"

#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <curl/curl.h>

#include "../kcgi.h"
#include "regress.h"

static int
parent(CURL *curl)
{
const char *data = "foo=bar";
struct curl_slist *list = NULL;
int c;

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
list = curl_slist_append(list, "Content-Type: "
"application/x-www-form-urlencoded;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:17123/");
c = curl_easy_perform(curl);
curl_slist_free_all(list);
return c == CURLE_OK;
}

static int
child(void)
{
struct kreq r;
const char *page = "index";

if (khttp_parse(&r, NULL, 0, &page, 1, 0) != KCGI_OK)
return 0;
if (r.fieldsz != 1 ||
strcmp(r.fields[0].key, "foo") != 0 ||
strcmp(r.fields[0].val, "bar") != 0)
return 0;
khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s",
kmimetypes[KMIME_TEXT_HTML]);
khttp_body(&r);
khttp_free(&r);
return 1;
}

int
main(int argc, char *argv[])
{

return regress_cgi(parent, child) ? 0 : 1;
}
71 changes: 71 additions & 0 deletions regress/test-post-charset2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and 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 "../config.h"

#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <curl/curl.h>

#include "../kcgi.h"
#include "regress.h"

static int
parent(CURL *curl)
{
const char *data = "foo=bar";
struct curl_slist *list = NULL;
int c;

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);
list = curl_slist_append(list, "Content-Type: "
"application/x-www-form-urlencoded;");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:17123/");
c = curl_easy_perform(curl);
curl_slist_free_all(list);
return c == CURLE_OK;
}

static int
child(void)
{
struct kreq r;
const char *page = "index";

if (khttp_parse(&r, NULL, 0, &page, 1, 0) != KCGI_OK)
return 0;
if (r.fieldsz != 1 ||
strcmp(r.fields[0].key, "foo") != 0 ||
strcmp(r.fields[0].val, "bar") != 0)
return 0;
khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
khttp_head(&r, kresps[KRESP_CONTENT_TYPE], "%s",
kmimetypes[KMIME_TEXT_HTML]);
khttp_body(&r);
khttp_free(&r);
return 1;
}

int
main(int argc, char *argv[])
{

return regress_cgi(parent, child) ? 0 : 1;
}
15 changes: 9 additions & 6 deletions regress/test-post.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* $Id$ */
/*
* Copyright (c) 2016 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand Down Expand Up @@ -44,20 +43,24 @@ child(void)
struct kreq r;
const char *page = "index";

if (KCGI_OK != khttp_parse(&r, NULL, 0, &page, 1, 0))
return(0);
if (khttp_parse(&r, NULL, 0, &page, 1, 0) != KCGI_OK)
return 0;
if (r.fieldsz != 1 ||
strcmp(r.fields[0].key, "foo") != 0 ||
strcmp(r.fields[0].val, "bar") != 0)
return 0;
khttp_head(&r, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_200]);
khttp_head(&r, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[KMIME_TEXT_HTML]);
khttp_body(&r);
khttp_free(&r);
return(1);
return 1;
}

int
main(int argc, char *argv[])
{

return(regress_cgi(parent, child) ? EXIT_SUCCESS : EXIT_FAILURE);
return regress_cgi(parent, child) ? 0 : 1;
}

0 comments on commit fe974ef

Please sign in to comment.