8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / common / net / wanboot / parseURL.c
blobc27c7844331f02db223638b061b58a80b3b14b16
1 /*
2 * CDDL HEADER START
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.
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]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <ctype.h>
30 #include <strings.h>
31 #include <stdlib.h>
33 #include <parseURL.h>
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.
49 int
50 url_parse_hostport(const char *hpstr, url_hport_t *hport, ushort_t def_port)
52 char *lhpstr;
53 char *ptr;
54 char *optr;
55 size_t hlen;
57 lhpstr = strdup(hpstr);
58 if (lhpstr == NULL) {
59 return (URL_PARSE_NOMEM);
63 * Find the host/port separator.
65 ptr = lhpstr;
66 optr = ptr;
67 ptr = strstr(optr, ":");
68 if (ptr != NULL) {
69 *ptr = '\0';
70 ptr++;
74 * Copy in the hostname and check to see that it was a
75 * a valid size.
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);
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;
89 } else {
90 hport->port = 0;
91 while (*ptr != '\0') {
92 if (!isdigit(*ptr)) {
93 free(lhpstr);
94 return (URL_PARSE_BAD_HOSTPORT);
96 hport->port *= 10;
97 hport->port += (*ptr - '0');
98 ptr++;
102 free(lhpstr);
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) {
117 char *lurlstr;
118 char *ptr;
119 char *optr;
120 size_t plen;
121 int ret;
123 lurlstr = strdup(urlstr);
124 if (lurlstr == NULL) {
125 return (URL_PARSE_NOMEM);
129 * Determine 'http' or 'https'.
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);
144 * Find the hostport/abspath separator.
146 optr = ptr;
147 ptr = strstr(optr, "/");
148 if (ptr != NULL) {
149 *ptr = '\0';
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) {
158 free(lurlstr);
159 return (ret);
163 * If the URL string does not contain an abspath, then supply "/"
164 * by default.
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);
173 } else {
174 (void) strlcpy(url->abspath, "/", sizeof (url->abspath));
177 free(lurlstr);
178 return (URL_PARSE_SUCCESS);