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
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.
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]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
35 #define HTTP_SCHEME "http://"
36 #define HTTPS_SCHEME "https://"
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.
45 * A host port string should be of the form -> host[:port]
47 * Returns: One of the URL parsing error return codes.
50 url_parse_hostport(const char *hpstr
, url_hport_t
*hport
, ushort_t def_port
)
57 lhpstr
= strdup(hpstr
);
59 return (URL_PARSE_NOMEM
);
63 * Find the host/port separator.
67 ptr
= strstr(optr
, ":");
74 * Copy in the hostname and check to see that it was a
77 hlen
= strlcpy(hport
->hostname
, optr
, sizeof (hport
->hostname
));
78 if (hlen
== 0 || hlen
>= sizeof (hport
->hostname
)) {
80 return (URL_PARSE_BAD_HOSTPORT
);
84 * If the hostport string does not contain a port, then use
85 * the default port provided by the caller.
87 if (ptr
== NULL
|| *ptr
== '\0') {
88 hport
->port
= def_port
;
91 while (*ptr
!= '\0') {
94 return (URL_PARSE_BAD_HOSTPORT
);
97 hport
->port
+= (*ptr
- '0');
103 return (URL_PARSE_SUCCESS
);
107 * This routine parses an http or https URL and initializes an url_t
108 * structure with its contents.
110 * A URL string should be of the form -> http[s]://host[:port]/abspath
112 * Returns: One of the URL parsing error return codes.
115 url_parse(const char *urlstr
, url_t
*url
) {
123 lurlstr
= strdup(urlstr
);
124 if (lurlstr
== NULL
) {
125 return (URL_PARSE_NOMEM
);
129 * Determine 'http' or 'https'.
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
);
140 return (URL_PARSE_BAD_SCHEME
);
144 * Find the hostport/abspath separator.
147 ptr
= strstr(optr
, "/");
153 * Parse the hostport entity; supply suitable port defaults.
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
) {
163 * If the URL string does not contain an abspath, then supply "/"
168 plen
= strlcpy(url
->abspath
, ptr
, sizeof (url
->abspath
));
169 if (plen
>= sizeof (url
->abspath
)) {
171 return (URL_PARSE_BAD_ABSPATH
);
174 (void) strlcpy(url
->abspath
, "/", sizeof (url
->abspath
));
178 return (URL_PARSE_SUCCESS
);