MEDIUM: h2: add a function to emit an HTTP/1 request from a headers list

The current H2 to H1 protocol conversion presents some issues which will
require to perform some processing on certain headers before writing them
so it's not possible to convert HPACK to H1 on the fly.

Here we introduce a function which performs half of what hpack_decode_header()
used to do, which is to take a list of headers on input and emit the
corresponding request in HTTP/1.1 format. The code is the same and functions
were renamed to be prefixed with "h2" instead of "hpack", though it ends
up being simpler as the various HPACK-specific cases could be fused into
a single one (ie: add header).

Moving this part here makes a lot of sense as now this code is specific to
what is documented in HTTP/2 RFC 7540 and will be able to deal with special
cases related to H2 to H1 conversion enumerated in section 8.1.

Various error codes which were previously assigned to HPACK were never
used (aside being negative) and were all replaced by -1 with a comment
indicating what error was detected. The code could be further factored
thanks to this but this commit focuses on compatibility first.

This code is not yet used but builds fine.
diff --git a/include/common/h2.h b/include/common/h2.h
index 0521b11..65c5ab1 100644
--- a/include/common/h2.h
+++ b/include/common/h2.h
@@ -5,27 +5,67 @@
  * Copyright (C) 2000-2017 Willy Tarreau - w@1wt.eu
  * Copyright (C) 2017 HAProxy Technologies
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation, version 2.1
- * exclusively.
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
  *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 #ifndef _COMMON_H2_H
 #define _COMMON_H2_H
 
 #include <common/config.h>
+#include <common/http-hdr.h>
+#include <common/ist.h>
 
 
+/* indexes of most important pseudo headers can be simplified to an almost
+ * linear array by dividing the index by 2 for all values from 1 to 9, and
+ * caping to 4 for values up to 14 ; thus it fits in a single 24-bit array
+ * shifted by 3 times the index value/2, or a 32-bit array shifted by 4x.
+ * Don't change these values, they are assumed by hpack_idx_to_phdr(). There
+ * is an entry for the Host header field which is not a pseudo-header but
+ * needs to be tracked as we should only use :authority if it's absent.
+ */
+enum {
+	H2_PHDR_IDX_NONE = 0,
+	H2_PHDR_IDX_AUTH = 1, /* :authority = 1     */
+	H2_PHDR_IDX_METH = 2, /* :method    = 2..3  */
+	H2_PHDR_IDX_PATH = 3, /* :path      = 4..5  */
+	H2_PHDR_IDX_SCHM = 4, /* :scheme    = 6..7  */
+	H2_PHDR_IDX_STAT = 5, /* :status    = 8..14 */
+	H2_PHDR_IDX_HOST = 6, /* Host, never returned, just a place-holder */
+	H2_PHDR_NUM_ENTRIES   /* must be last */
+};
+
+/* bit fields indicating the pseudo-headers found. It also covers the HOST
+ * header field as well as any non-pseudo-header field (NONE).
+ */
+enum {
+	H2_PHDR_FND_NONE = 1 << H2_PHDR_IDX_NONE, /* found a regular header */
+	H2_PHDR_FND_AUTH = 1 << H2_PHDR_IDX_AUTH,
+	H2_PHDR_FND_METH = 1 << H2_PHDR_IDX_METH,
+	H2_PHDR_FND_PATH = 1 << H2_PHDR_IDX_PATH,
+	H2_PHDR_FND_SCHM = 1 << H2_PHDR_IDX_SCHM,
+	H2_PHDR_FND_STAT = 1 << H2_PHDR_IDX_STAT,
+	H2_PHDR_FND_HOST = 1 << H2_PHDR_IDX_HOST,
+};
+
 /* frame types, from the standard */
 enum h2_ft {
 	H2_FT_DATA            = 0x00,     // RFC7540 #6.1
@@ -104,6 +144,11 @@
 	"\x54\x50\x2f\x32\x2e\x30\x0d\x0a"  \
 	"\x0d\x0a\x53\x4d\x0d\x0a\x0d\x0a"
 
+
+/* various protocol processing functions */
+
+int h2_make_h1_request(struct http_hdr *list, char *out, int osize);
+
 /*
  * Some helpful debugging functions.
  */
@@ -125,6 +170,27 @@
 	}
 }
 
+/* returns the pseudo-header <str> corresponds to among H2_PHDR_IDX_*, 0 if not a
+ * pseudo-header, or -1 if not a valid pseudo-header.
+ */
+static inline int h2_str_to_phdr(const struct ist str)
+{
+	if (*str.ptr == ':') {
+		if (isteq(str, ist(":path")))           return H2_PHDR_IDX_PATH;
+		else if (isteq(str, ist(":method")))    return H2_PHDR_IDX_METH;
+		else if (isteq(str, ist(":scheme")))    return H2_PHDR_IDX_SCHM;
+		else if (isteq(str, ist(":status")))    return H2_PHDR_IDX_STAT;
+		else if (isteq(str, ist(":authority"))) return H2_PHDR_IDX_AUTH;
+
+		/* all other names starting with ':' */
+		return -1;
+	}
+
+	/* not a pseudo header */
+	return 0;
+}
+
+
 #endif /* _COMMON_H2_H */
 
 /*