util_uri.c revision 404e2e1f8ad30c2d996f5fb6b3a9a4a4a14a004b
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* ====================================================================
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * The Apache Software License, Version 1.1
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * Copyright (c) 2000 The Apache Software Foundation. All rights
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * Redistribution and use in source and binary forms, with or without
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * modification, are permitted provided that the following conditions
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * 1. Redistributions of source code must retain the above copyright
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * notice, this list of conditions and the following disclaimer.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * 2. Redistributions in binary form must reproduce the above copyright
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * notice, this list of conditions and the following disclaimer in
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * the documentation and/or other materials provided with the
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * distribution.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * 3. The end-user documentation included with the redistribution,
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * if any, must include the following acknowledgment:
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * "This product includes software developed by the
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * Apache Software Foundation (http://www.apache.org/)."
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * Alternately, this acknowledgment may appear in the software itself,
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * if and wherever such third-party acknowledgments normally appear.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * 4. The names "Apache" and "Apache Software Foundation" must
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * not be used to endorse or promote products derived from this
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * software without prior written permission. For written
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * permission, please contact apache@apache.org.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * 5. Products derived from this software may not be called "Apache",
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * nor may "Apache" appear in their name, without prior written
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * permission of the Apache Software Foundation.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * SUCH DAMAGE.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * ====================================================================
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * This software consists of voluntary contributions made by many
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * individuals on behalf of the Apache Software Foundation. For more
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * information on the Apache Software Foundation, please see
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * Portions of this software are based upon public domain software
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * originally written at the National Center for Supercomputing Applications,
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * University of Illinois, Urbana-Champaign.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * util_uri.c: URI related utility things
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* Some WWW schemes and their default ports; this is basically /etc/services */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* This will become global when the protocol abstraction comes */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* As the schemes are searched by a linear search, */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* they are sorted by their expected frequency */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaAPI_EXPORT(unsigned short) ap_default_port_for_scheme(const char *scheme_str)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa for (scheme = schemes; scheme->name != NULL; ++scheme)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaAPI_EXPORT(unsigned short) ap_default_port_for_request(const request_rec *r)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa ? ap_default_port_for_scheme(r->parsed_uri.scheme)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* Create a copy of a "struct hostent" record; it was presumably returned
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * from a call to gethostbyname() and lives in static storage.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * By creating a copy we can tuck it away for later use.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaAPI_EXPORT(struct hostent *) ap_pduphostent(ap_pool_t *p, const struct hostent *hp)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa int i = 0, j = 0;
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* Count number of alias entries */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* Count number of in_addr entries */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* Allocate hostent structure, alias ptrs, addr ptrs, addrs */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa newent = (struct hostent *) ap_palloc(p, sizeof(*hp));
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa aliases = (char **) ap_palloc(p, (j+1) * sizeof(char*));
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa ptrs = (char **) ap_palloc(p, (i+1) * sizeof(char*));
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa addrs = (struct in_addr *) ap_palloc(p, (i+1) * sizeof(struct in_addr));
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* Copy Alias Names: */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* Copy address entries */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa addrs[i] = *(struct in_addr *) hp->h_addr_list[i];
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* pgethostbyname(): resolve hostname, if successful return an ALLOCATED
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * COPY OF the hostent structure, intended to be stored and used later.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * (gethostbyname() uses static storage that would be overwritten on each call)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaAPI_EXPORT(struct hostent *) ap_pgethostbyname(ap_pool_t *p, const char *hostname)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa return (hp == NULL) ? NULL : ap_pduphostent(p, hp);
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa/* Unparse a uri_components structure to an URI string.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa * Optionally suppress the password for security reasons.
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen KuksaAPI_EXPORT(char *) ap_unparse_uri_components(ap_pool_t *p, const uri_components *uptr, unsigned flags)
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* If suppressing the site part, omit both user name & scheme://hostname */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* Construct a "user:password@" string, honoring the passed UNP_ flags: */
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa (uptr->user && !(flags & UNP_OMITUSER)) ? uptr->user : "",
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa (uptr->password && !(flags & UNP_OMITPASSWORD)) ? ":" : "",
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa ? ((flags & UNP_REVEALPASSWORD) ? uptr->password : "XXXXXXXX")
a389e88e0acb83d8489bdc5e55bc5522b152bbecEugen Kuksa /* Construct scheme://site string */
int is_default_port;
NULL);
ret,
NULL);
return ret;
#ifdef UTIL_URI_REGEX
void ap_util_uri_init(void)
int ret;
const char *re_str;
* draft-fielding-uri-syntax-01.txt, section 4.4 tells us:
int ret;
if (ret != 0) {
uri);
return HTTP_BAD_REQUEST;
if (ret != 0) {
return HTTP_BAD_REQUEST;
char *endstr;
int port;
return HTTP_BAD_REQUEST;
if (ret == 0)
return ret;
/* the uri_delims.h file is autogenerated by gen_uri_delims.c */
#include "uri_delims.h"
void ap_util_uri_init(void)
const char *s1;
const char *hostinfo;
char *endstr;
int port;
* that the path could be empty -- http://foobar?query for example
s = uri;
if (s != uri) {
return HTTP_OK;
if (s1) {
return HTTP_OK;
return HTTP_OK;
s = uri;
hostinfo = s;
if (s < hostinfo) {
if (s == NULL) {
goto deal_with_path;
if (uri != s) {
goto deal_with_path;
return HTTP_BAD_REQUEST;
goto deal_with_path;
if (s1) {
++s1;
goto deal_with_host;
* currently at http://www.mcom.com/newsref/std/tunneling_ssl.html
API_EXPORT(int) ap_parse_hostinfo_components(ap_pool_t *p, const char *hostinfo, uri_components *uptr)
char *endstr;
if (s == NULL) {
return HTTP_BAD_REQUEST;
return HTTP_OK;
return HTTP_BAD_REQUEST;