xref: /titanic_44/usr/src/common/net/wanboot/parseURL.c (revision d2ec54f7875f7e05edd56195adbeb593c947763f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <ctype.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 
33 #include <parseURL.h>
34 
35 #define	HTTP_SCHEME	"http://"
36 #define	HTTPS_SCHEME	"https://"
37 
38 /*
39  * This routine parses a hostport string and initializes an url_hport_t
40  * structure with its contents. Technically, a hostport string does not
41  * require a port component. In the case, where there is no port component
42  * in the hostport string, this routine will initialize the url_hport_t
43  * structure with the default port supplied by the caller.
44  *
45  * A host port string should be of the form -> host[:port]
46  *
47  * Returns: One of the URL parsing error return codes.
48  */
49 int
50 url_parse_hostport(const char *hpstr, url_hport_t *hport, ushort_t def_port)
51 {
52 	char *lhpstr;
53 	char *ptr;
54 	char *optr;
55 	size_t hlen;
56 
57 	lhpstr = strdup(hpstr);
58 	if (lhpstr == NULL) {
59 		return (URL_PARSE_NOMEM);
60 	}
61 
62 	/*
63 	 * Find the host/port separator.
64 	 */
65 	ptr = lhpstr;
66 	optr = ptr;
67 	ptr = strstr(optr, ":");
68 	if (ptr != NULL) {
69 		*ptr = '\0';
70 		ptr++;
71 	}
72 
73 	/*
74 	 * Copy in the hostname and check to see that it was a
75 	 * a valid size.
76 	 */
77 	hlen = strlcpy(hport->hostname, optr, sizeof (hport->hostname));
78 	if (hlen == 0 || hlen >= sizeof (hport->hostname)) {
79 		free(lhpstr);
80 		return (URL_PARSE_BAD_HOSTPORT);
81 	}
82 
83 	/*
84 	 * If the hostport string does not contain a port, then use
85 	 * the default port provided by the caller.
86 	 */
87 	if (ptr == NULL || *ptr == '\0') {
88 		hport->port = def_port;
89 	} else {
90 		hport->port = 0;
91 		while (*ptr != '\0') {
92 			if (!isdigit(*ptr)) {
93 				free(lhpstr);
94 				return (URL_PARSE_BAD_HOSTPORT);
95 			}
96 			hport->port *= 10;
97 			hport->port += (*ptr - '0');
98 			ptr++;
99 		}
100 	}
101 
102 	free(lhpstr);
103 	return (URL_PARSE_SUCCESS);
104 }
105 
106 /*
107  * This routine parses an http or https URL and initializes an url_t
108  * structure with its contents.
109  *
110  * A URL string should be of the form -> http[s]://host[:port]/abspath
111  *
112  * Returns: One of the URL parsing error return codes.
113  */
114 int
115 url_parse(const char *urlstr, url_t *url) {
116 
117 	char *lurlstr;
118 	char *ptr;
119 	char *optr;
120 	size_t plen;
121 	int ret;
122 
123 	lurlstr = strdup(urlstr);
124 	if (lurlstr == NULL) {
125 		return (URL_PARSE_NOMEM);
126 	}
127 
128 	/*
129 	 * Determine 'http' or 'https'.
130 	 */
131 	ptr = lurlstr;
132 	if (strncmp(ptr, HTTP_SCHEME, strlen(HTTP_SCHEME)) == 0) {
133 		ptr += strlen(HTTP_SCHEME);
134 		url->https = B_FALSE;
135 	} else if (strncmp(ptr, HTTPS_SCHEME, strlen(HTTPS_SCHEME)) == 0) {
136 		ptr += strlen(HTTPS_SCHEME);
137 		url->https = B_TRUE;
138 	} else {
139 		free(lurlstr);
140 		return (URL_PARSE_BAD_SCHEME);
141 	}
142 
143 	/*
144 	 * Find the hostport/abspath separator.
145 	 */
146 	optr = ptr;
147 	ptr = strstr(optr, "/");
148 	if (ptr != NULL) {
149 		*ptr = '\0';
150 	}
151 
152 	/*
153 	 * Parse the hostport entity; supply suitable port defaults.
154 	 */
155 	ret = url_parse_hostport(optr, &url->hport, url->https ?
156 	    URL_DFLT_HTTPS_SRVR_PORT : URL_DFLT_SRVR_PORT);
157 	if (ret != URL_PARSE_SUCCESS) {
158 		free(lurlstr);
159 		return (ret);
160 	}
161 
162 	/*
163 	 * If the URL string does not contain an abspath, then supply "/"
164 	 * by default.
165 	 */
166 	if (ptr != NULL) {
167 		*ptr = '/';
168 		plen = strlcpy(url->abspath, ptr, sizeof (url->abspath));
169 		if (plen >= sizeof (url->abspath)) {
170 			free(lurlstr);
171 			return (URL_PARSE_BAD_ABSPATH);
172 		}
173 	} else {
174 		(void) strlcpy(url->abspath, "/", sizeof (url->abspath));
175 	}
176 
177 	free(lurlstr);
178 	return (URL_PARSE_SUCCESS);
179 }
180