From bf8be95f4a07ce4d1a6d9f60025d621b01193e6c Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Fri, 10 Dec 2021 20:19:42 -0500 Subject: [PATCH] It's Friday night, Wesley is out, Sammi is not hungry, and Miss Melissa is asleep on the couch. Instead of making dinner I added URL parameter parsing to the web server. I didn't think we'd need it but I want to be able to transmit some parameters to ENT0 and this seems like a reasonable way to do it. --- webcit-ng/http.c | 26 ++++++++++++++++++++++++++ webcit-ng/webcit.h | 1 + 2 files changed, 27 insertions(+) diff --git a/webcit-ng/http.c b/webcit-ng/http.c index 1df8b8efc..29a1fba6d 100644 --- a/webcit-ng/http.c +++ b/webcit-ng/http.c @@ -114,6 +114,7 @@ void perform_one_http_transaction(struct client_handle *ch) { memset(&h, 0, sizeof h); h.request_headers = array_new(sizeof(struct keyval)); + h.request_parms = array_new(sizeof(struct keyval)); h.response_headers = array_new(sizeof(struct keyval)); while (len = client_readline(ch, buf, sizeof buf), (len > 0)) { @@ -158,6 +159,24 @@ void perform_one_http_transaction(struct client_handle *ch) { } + // If the URL had any query parameters in it, parse them out now. + char *p = strchr(h.url, '?'); + if (p) { + *p++ = 0; // insert a null to remove parameters from the URL + char *tok, *saveptr = NULL; + for (tok = strtok_r(p, "&", &saveptr); tok!=NULL; tok = strtok_r(NULL, "&", &saveptr)) { + char *eq = strchr(tok, '='); + if (eq) { + *eq++ = 0; + unescape_input(eq); + struct keyval kv; + kv.key = strdup(tok); + kv.val = strdup(eq); + array_append(h.request_parms, &kv); + } + } + } + // build up the site prefix, such as https://foo.bar.com:4343 h.site_prefix = malloc(256); strcpy(h.site_prefix, (is_https ? "https://" : "http://")); @@ -236,6 +255,13 @@ void perform_one_http_transaction(struct client_handle *ch) { array_delete_element_at(h.request_headers, 0); } array_free(h.request_headers); + while (array_len(h.request_parms) > 0) { + struct keyval *kv = array_get_element_at(h.request_parms, 0); + if (kv->key) free(kv->key); + if (kv->val) free(kv->val); + array_delete_element_at(h.request_parms, 0); + } + array_free(h.request_parms); while (array_len(h.response_headers) > 0) { struct keyval *kv = array_get_element_at(h.response_headers, 0); if (kv->key) free(kv->key); diff --git a/webcit-ng/webcit.h b/webcit-ng/webcit.h index 0e5321354..06269da81 100644 --- a/webcit-ng/webcit.h +++ b/webcit-ng/webcit.h @@ -65,6 +65,7 @@ struct http_transaction { // The lifetime of an HTTP request goes through this Array *request_headers; char *request_body; long request_body_length; + Array *request_parms; // anything after the "?" in the URL int response_code; char *response_string; Array *response_headers; -- 2.39.2