1/*	$NetBSD: url.h,v 1.4 2024/02/21 22:52:31 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0 and MIT
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16/*
17 * Copyright Joyent, Inc. and other Node contributors. All rights reserved.
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a copy
20 * of this software and associated documentation files (the "Software"), to
21 * deal in the Software without restriction, including without limitation the
22 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
23 * sell copies of the Software, and to permit persons to whom the Software is
24 * furnished to do so, subject to the following conditions:
25 *
26 * The above copyright notice and this permission notice shall be included in
27 * all copies or substantial portions of the Software.
28 *
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
35 * IN THE SOFTWARE.
36 */
37
38#pragma once
39
40#include <stdbool.h>
41#include <stddef.h>
42#include <stdint.h>
43
44#include <isc/result.h>
45
46/*
47 * Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
48 * faster
49 */
50#ifndef HTTP_PARSER_STRICT
51#define HTTP_PARSER_STRICT 1
52#endif
53
54typedef enum {
55	ISC_UF_SCHEMA = 0,
56	ISC_UF_HOST = 1,
57	ISC_UF_PORT = 2,
58	ISC_UF_PATH = 3,
59	ISC_UF_QUERY = 4,
60	ISC_UF_FRAGMENT = 5,
61	ISC_UF_USERINFO = 6,
62	ISC_UF_MAX = 7
63} isc_url_field_t;
64
65/* Result structure for isc_url_parse().
66 *
67 * Callers should index into field_data[] with UF_* values iff field_set
68 * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
69 * because we probably have padding left over), we convert any port to
70 * a uint16_t.
71 */
72typedef struct {
73	uint16_t field_set; /* Bitmask of (1 << UF_*) values */
74	uint16_t port;	    /* Converted UF_PORT string */
75
76	struct {
77		uint16_t off; /* Offset into buffer in which field starts */
78		uint16_t len; /* Length of run in buffer */
79	} field_data[ISC_UF_MAX];
80} isc_url_parser_t;
81
82isc_result_t
83isc_url_parse(const char *buf, size_t buflen, bool is_connect,
84	      isc_url_parser_t *up);
85/*%<
86 * Parse a URL; return nonzero on failure
87 */
88