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