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