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