Index | Thread | Search

From:
Rafael Sadowski <rafael@sizeofvoid.org>
Subject:
relayd: refactor split relay_http_parse_startline by directions
To:
tech@openbsd.org
Date:
Wed, 11 Nov 2026 08:52:08 +0100

Download raw body.

Thread
  • Rafael Sadowski:

    relayd: refactor split relay_http_parse_startline by directions

I would like to split relay_http_parse_startline by directions (request
/ response). No functional change. OK?

commit bd0fc9fda2c62fe70113a387b89f3c0a9b610452
Author: Rafael Sadowski <rafael@sizeofvoid.org>
Date:   Mon Oct 5 16:54:04 2026 +0200

    relayd: refactor split relay_http_parse_startline by directions

diff --git a/relay_http.c b/relay_http.c
index ad95fef..56f1087 100644
--- a/relay_http.c
+++ b/relay_http.c
@@ -83,6 +83,10 @@ char		*server_root_strip(char *, int);
 int		 relay_http_parse_startline(struct ctl_relay_event *, char *,
     enum httpmethod *);
 static int	 relay_url_log_match(const struct kv *, const char *);
+int		 relay_http_parse_startline_res(struct ctl_relay_event *,
+    char *, enum httpmethod *);
+int		 relay_http_parse_startline_req(struct ctl_relay_event *,
+    char *, enum httpmethod *);
 
 static struct relayd	*env = NULL;
 
@@ -2118,11 +2122,23 @@ server_root_strip(char *path, int n)
 	return (path);
 }
 
+int
+relay_http_parse_startline(struct ctl_relay_event *cre, char *line,
+    enum httpmethod *method)
+{
+	if (cre->dir == RELAY_DIR_RESPONSE) {
+		return (relay_http_parse_startline_res(cre, line, method));
+	} else if (cre->dir == RELAY_DIR_REQUEST) {
+		return (relay_http_parse_startline_req(cre, line, method));
+	}
+	return (-1);
+}
+
 /*
- * Handle the first line of an HTTP message (Request- or Status-Line).
+ * Handle the first line of an HTTP message (Response- or Status-Line).
  */
 int
-relay_http_parse_startline(struct ctl_relay_event *cre, char *line,
+relay_http_parse_startline_res(struct ctl_relay_event *cre, char *line,
     enum httpmethod *request_method)
 {
 	struct rsession		*con = cre->con;
@@ -2132,7 +2148,8 @@ relay_http_parse_startline(struct ctl_relay_event *cre, char *line,
 	char			*key, *value;
 	const char		*errstr;
 
-	log_debug("%s: session %d http_session %p", __func__, con->se_id, hs);
+	log_debug("%s: session %d http response session %p", __func__,
+	    con->se_id, hs);
 
 	key = line;
 	if ((value = strchr(key, ' ')) == NULL) {
@@ -2141,99 +2158,125 @@ relay_http_parse_startline(struct ctl_relay_event *cre, char *line,
 	}
 	*value++ = '\0';
 
-	if (cre->dir == RELAY_DIR_RESPONSE) {
-		desc->http_method = HTTP_METHOD_RESPONSE;
-		hmn = SIMPLEQ_FIRST(&hs->hs_methods);
+	desc->http_method = HTTP_METHOD_RESPONSE;
+	hmn = SIMPLEQ_FIRST(&hs->hs_methods);
 
-		/*
-		 * There is nothing preventing the relay from
-		 * sending an unbalanced response.  Be prepared.
-		 */
-		if (hmn == NULL) {
-			*request_method = HTTP_METHOD_NONE;
-			log_debug("%s: session %d unbalanced response",
-			    __func__, con->se_id);
-		} else {
-			SIMPLEQ_REMOVE_HEAD(&hs->hs_methods, hmn_entry);
-			*request_method = hmn->hmn_method;
-			log_debug("%s: session %d dequeuing %s",
-			    __func__, con->se_id,
-			    relay_httpmethod_byid(*request_method));
-			free(hmn);
-		}
+	/*
+	 * There is nothing preventing the relay from
+	 * sending an unbalanced response.  Be prepared.
+	 */
+	if (hmn == NULL) {
+		*request_method = HTTP_METHOD_NONE;
+		log_debug("%s: session %d unbalanced response",
+		    __func__, con->se_id);
+	} else {
+		SIMPLEQ_REMOVE_HEAD(&hs->hs_methods, hmn_entry);
+		*request_method = hmn->hmn_method;
+		log_debug("%s: session %d dequeuing %s", __func__, con->se_id,
+		    relay_httpmethod_byid(*request_method));
+		free(hmn);
+	}
 
-		/*
-		 * Decode response path and query
-		 */
-		desc->http_version = strdup(key);
-		if (desc->http_version == NULL)
-			goto fail;
+	/*
+	 * Decode response path and query
+	 */
+	desc->http_version = strdup(key);
+	if (desc->http_version == NULL)
+		goto fail;
 
-		desc->http_rescode = strdup(value);
-		if (desc->http_rescode == NULL)
-			goto fail;
+	desc->http_rescode = strdup(value);
+	if (desc->http_rescode == NULL)
+		goto fail;
 
-		desc->http_resmesg = strchr(desc->http_rescode, ' ');
-		if (desc->http_resmesg == NULL)
-			goto fail;
+	desc->http_resmesg = strchr(desc->http_rescode, ' ');
+	if (desc->http_resmesg == NULL)
+		goto fail;
 
-		*desc->http_resmesg++ = '\0';
-		desc->http_resmesg = strdup(desc->http_resmesg);
-		if (desc->http_resmesg == NULL)
-			goto fail;
+	*desc->http_resmesg++ = '\0';
+	desc->http_resmesg = strdup(desc->http_resmesg);
+	if (desc->http_resmesg == NULL)
+		goto fail;
 
-		desc->http_status = strtonum(desc->http_rescode, 100, 599,
-		    &errstr);
-		if (errstr) {
-			log_debug(
-			    "%s: http_status %s: errno %d, %s",
-			    __func__, desc->http_rescode, errno,
-			    errstr);
-			goto fail;
-		}
-		log_debug("http_version %s http_rescode %s http_resmesg %s",
-		    desc->http_version, desc->http_rescode,
-		    desc->http_resmesg);
-	} else if (cre->dir == RELAY_DIR_REQUEST) {
-		desc->http_method = relay_httpmethod_byname(key);
+	desc->http_status = strtonum(desc->http_rescode, 100, 599, &errstr);
 
-		if (desc->http_method == HTTP_METHOD_NONE)
-			goto fail;
+	if (errstr) {
+		log_debug(
+		    "%s: http_status %s: errno %d, %s",
+		    __func__, desc->http_rescode, errno,
+		    errstr);
+		goto fail;
+	}
+	log_debug("http_version %s http_rescode %s http_resmesg %s",
+	    desc->http_version, desc->http_rescode,
+	    desc->http_resmesg);
+	return (0);
+fail:
+	relay_abort_http(con, 500, strerror(errno), 0);
+	return (-1);
+}
 
-		if ((hmn = calloc(1, sizeof *hmn)) == NULL)
-			goto fail;
-		hmn->hmn_method = desc->http_method;
-		log_debug("%s: session %d enqueuing %s", __func__, con->se_id,
-		    relay_httpmethod_byid(hmn->hmn_method));
-		SIMPLEQ_INSERT_TAIL(&hs->hs_methods, hmn, hmn_entry);
-		/*
-		 * Decode request path and query
-		 */
-		desc->http_path = strdup(value);
-		if (desc->http_path == NULL)
-			goto fail;
+/*
+ * Handle the first line of an HTTP message (Request- or Status-Line).
+ */
+int
+relay_http_parse_startline_req(struct ctl_relay_event *cre, char *line,
+    enum httpmethod *request_method)
+{
+	struct rsession		*con = cre->con;
+	struct http_descriptor	*desc = cre->desc;
+	struct http_session	*hs = con->se_priv;
+	struct http_method_node	*hmn;
+	char			*key, *value;
 
-		desc->http_version = strchr(desc->http_path, ' ');
-		if (desc->http_version == NULL)
-			goto fail;
+	log_debug("%s: session %d http request session %p", __func__,
+	    con->se_id, hs);
 
-		*desc->http_version++ = '\0';
-		desc->http_query = strchr(desc->http_path, '?');
-		if (desc->http_query != NULL)
-			*desc->http_query++ = '\0';
+	key = line;
+	if ((value = strchr(key, ' ')) == NULL) {
+		relay_abort_http(con, 400, "malformed", 0);
+		return (-1);
+	}
+	*value++ = '\0';
 
-		/*
-		 * Have to allocate the strings because they
-		 * could be changed independently by the
-		 * filters later.
-		 */
-		if ((desc->http_version = strdup(desc->http_version)) == NULL)
-			goto fail;
+	desc->http_method = relay_httpmethod_byname(key);
+
+	if (desc->http_method == HTTP_METHOD_NONE)
+		goto fail;
+
+	if ((hmn = calloc(1, sizeof *hmn)) == NULL)
+		goto fail;
+	hmn->hmn_method = desc->http_method;
+	log_debug("%s: session %d enqueuing %s", __func__, con->se_id,
+	    relay_httpmethod_byid(hmn->hmn_method));
+	SIMPLEQ_INSERT_TAIL(&hs->hs_methods, hmn, hmn_entry);
+	/*
+	 * Decode request path and query
+	 */
+	desc->http_path = strdup(value);
+	if (desc->http_path == NULL)
+		goto fail;
+
+	desc->http_version = strchr(desc->http_path, ' ');
+	if (desc->http_version == NULL)
+		goto fail;
+
+	*desc->http_version++ = '\0';
+	desc->http_query = strchr(desc->http_path, '?');
+	if (desc->http_query != NULL)
+		*desc->http_query++ = '\0';
+
+	/*
+	 * Have to allocate the strings because they
+	 * could be changed independently by the
+	 * filters later.
+	 */
+	if ((desc->http_version = strdup(desc->http_version)) == NULL)
+		goto fail;
+
+	if (desc->http_query != NULL &&
+	    (desc->http_query = strdup(desc->http_query)) == NULL)
+		goto fail;
 
-		if (desc->http_query != NULL && (desc->http_query =
-		    strdup(desc->http_query)) == NULL)
-			goto fail;
-	}
 	return (0);
 fail:
 	relay_abort_http(con, 500, strerror(errno), 0);