util_uri.c revision b0f20a4a26bcfa85724b1c2e5ec6a077f12ef44c
/* ====================================================================
* Copyright (c) 1998-1999 The Apache Group. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* 4. The names "Apache Server" and "Apache Group" must not be used to
* endorse or promote products derived from this software without
* prior written permission.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the Apache Group
* for use in the Apache HTTP server project (http://www.apache.org/)."
*
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Group and was originally based
* on public domain software written at the National Center for
* Supercomputing Applications, University of Illinois, Urbana-Champaign.
* For more information on the Apache Group and the Apache HTTP server
* project, please see <http://www.apache.org/>.
*
*/
/*
* util_uri.c: URI related utility things
*
*/
#include "httpd.h"
#include "http_log.h"
#include "util_uri.h"
#include <string.h>
#include <netdb.h>
/* This will become global when the protocol abstraction comes */
/* As the schemes are searched by a linear search, */
/* they are sorted by their expected frequency */
{
{"http", DEFAULT_HTTP_PORT},
{"ftp", DEFAULT_FTP_PORT},
{"https", DEFAULT_HTTPS_PORT},
{"gopher", DEFAULT_GOPHER_PORT},
{"wais", DEFAULT_WAIS_PORT},
{"nntp", DEFAULT_NNTP_PORT},
{"snews", DEFAULT_SNEWS_PORT},
{"prospero", DEFAULT_PROSPERO_PORT},
};
{
return scheme->default_port;
return 0;
}
{
return (r->parsed_uri.scheme)
: 0;
}
/* Create a copy of a "struct hostent" record; it was presumably returned
* from a call to gethostbyname() and lives in static storage.
* By creating a copy we can tuck it away for later use.
*/
{
char **ptrs;
char **aliases;
int i = 0, j = 0;
return NULL;
/* Count number of alias entries */
continue;
/* Count number of in_addr entries */
continue;
/* Allocate hostent structure, alias ptrs, addr ptrs, addrs */
/* Copy Alias Names: */
}
/* Copy address entries */
}
return newent;
}
/* pgethostbyname(): resolve hostname, if successful return an ALLOCATED
* COPY OF the hostent structure, intended to be stored and used later.
* (gethostbyname() uses static storage that would be overwritten on each call)
*/
{
}
/* Unparse a uri_components structure to an URI string.
* Optionally suppress the password for security reasons.
*/
API_EXPORT(char *) ap_unparse_uri_components(ap_context_t *p, const uri_components *uptr, unsigned flags)
{
char *ret = "";
/* If suppressing the site part, omit both user name & scheme://hostname */
if (!(flags & UNP_OMITSITEPART)) {
/* Construct a "user:password@" string, honoring the passed UNP_ flags: */
ret = ap_pstrcat (p,
: "",
"@", NULL);
/* Construct scheme://site string */
int is_default_port;
ret = ap_pstrcat (p,
NULL);
}
}
/* Should we suppress all path info? */
if (!(flags & UNP_OMITPATHINFO)) {
/* Append path, query and fragment strings: */
ret = ap_pstrcat (p,
ret,
NULL);
}
return ret;
}
/* The regex version of parse_uri_components has the advantage that it is
* relatively easy to understand and extend. But it has the disadvantage
* that the regexes are complex enough that regex libraries really
* don't do a great job with them performancewise.
*
* The default is a hand coded scanner that is two orders of magnitude
* faster.
*/
#ifdef UTIL_URI_REGEX
static regex_t re_hostpart;
void ap_util_uri_init(void)
{
int ret;
const char *re_str;
/* This is a modified version of the regex that appeared in
* draft-fielding-uri-syntax-01. It doesnt allow the uri to contain a
* scheme but no hostinfo or vice versa.
*
* draft-fielding-uri-syntax-01.txt, section 4.4 tells us:
*
* Although the BNF defines what is allowed in each component, it is
* ambiguous in terms of differentiating between a site component and
* a path component that begins with two slash characters.
*
* RFC2068 disambiguates this for the Request-URI, which may only ever be
* path /bar. Nowhere in RFC2068 is it possible to have a scheme but no
* hostinfo or a hostinfo but no scheme. (Unless you're proxying a
* protocol other than HTTP, but this parsing engine probably won't work
* for other protocols.)
*
* 12 3 4 5 6 7 8 */
re_str = "^(([^:/?#]+)://([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?$";
/* ^scheme--^ ^site---^ ^path--^ ^query^ ^frag */
char line[1024];
/* Make a readable error message */
"Internal error: regcomp(\"%s\") returned non-zero (%s) - "
"possibly due to broken regex lib! "
"Did you define WANTHSREGEX=yes?",
exit(1);
}
/* This is a sub-RE which will break down the hostinfo part,
* i.e., user, password, hostname and port.
* $ 12 3 4 5 6 7 */
re_str = "^(([^:]*)(:(.*))?@)?([^@:]*)(:([0-9]*))?$";
/* ^^user^ :pw ^host^ ^:[port]^ */
char line[1024];
/* Make a readable error message */
"Internal error: regcomp(\"%s\") returned non-zero (%s) - "
"possibly due to broken regex lib! "
"Did you define WANTHSREGEX=yes?",
exit(1);
}
}
/* parse_uri_components():
* Parse a given URI, fill in all supplied fields of a uri_components
* structure. This eliminates the necessity of extracting host, port,
* path, query info repeatedly in the modules.
* Side effects:
* - fills in fields of uri_components *uptr
* - none on any of the r->* fields
*/
{
int ret;
* as there are braces in the re_strings */
/* Initialize the structure. parse_uri() and parse_uri_components()
* can be called more than once per request.
*/
if (ret != 0) {
"ap_regexec() could not parse uri (\"%s\")",
uri);
return HTTP_BAD_REQUEST;
}
/* empty hostinfo is valid, that's why we test $1 but use $3 */
/* empty query string is valid, that's why we test $5 but use $6 */
/* empty fragment is valid, test $7 use $8 */
/* Parse the hostinfo part to extract user, password, host, and port */
if (ret != 0) {
"ap_regexec() could not parse (\"%s\") as host part",
return HTTP_BAD_REQUEST;
}
/* $ 12 3 4 5 6 7 */
/* "^(([^:]*)(:(.*))?@)?([^@:]*)(:([0-9]*))?$" */
/* ^^user^ :pw ^host^ ^:[port]^ */
/* empty user is valid, that's why we test $1 but use $2 */
/* empty password is valid, test $3 but use $4 */
/* empty hostname is valid, and implied by the existence of hostinfo */
/* Note that the port string can be empty.
* If it is, we use the default port associated with the scheme
*/
char *endstr;
int port;
if (*endstr != '\0') {
/* Invalid characters after ':' found */
return HTTP_BAD_REQUEST;
}
}
else {
}
}
}
if (ret == 0)
return ret;
}
#else
/* Here is the hand-optimized parse_uri_components(). There are some wild
* tricks we could pull in assembly language that we don't pull here... like we
* can do word-at-time scans for delimiter characters using the same technique
* that fast memchr()s use. But that would be way non-portable. -djg
*/
/* We have a ap_table_t that we can index by character and it tells us if the
* character is one of the interesting delimiters. Note that we even get
* compares for NUL for free -- it's just another delimiter.
*/
/* the uri_delims.h file is autogenerated by gen_uri_delims.c */
#include "uri_delims.h"
/* it works like this:
if (uri_delims[ch] & NOTEND_foobar) {
then we're not at a delimiter for foobar
}
*/
/* Note that we optimize the scheme scanning here, we cheat and let the
* compiler know that it doesn't have to do the & masking.
*/
#define NOTEND_SCHEME (0xff)
void ap_util_uri_init(void)
{
/* nothing to do */
}
/* parse_uri_components():
* Parse a given URI, fill in all supplied fields of a uri_components
* structure. This eliminates the necessity of extracting host, port,
* path, query info repeatedly in the modules.
* Side effects:
* - fills in fields of uri_components *uptr
* - none on any of the r->* fields
*/
{
const char *s;
const char *s1;
const char *hostinfo;
char *endstr;
int port;
/* Initialize the structure. parse_uri() and parse_uri_components()
* can be called more than once per request.
*/
/* We assume the processor has a branch predictor like most --
* it assumes forward branches are untaken and backwards are taken. That's
* the reason for the gotos. -djg
*/
if (uri[0] == '/') {
/* we expect uri to point to first character of path ... remember
* that the path could be empty -- http://foobar?query for example
*/
s = uri;
while ((uri_delims[*(unsigned char *)s] & NOTEND_PATH) == 0) {
++s;
}
if (s != uri) {
}
if (*s == 0) {
return HTTP_OK;
}
if (*s == '?') {
++s;
if (s1) {
}
else {
}
return HTTP_OK;
}
/* otherwise it's a fragment */
return HTTP_OK;
}
/* find the scheme: */
s = uri;
while ((uri_delims[*(unsigned char *)s] & NOTEND_SCHEME) == 0) {
++s;
}
/* scheme must be non-empty and followed by :// */
goto deal_with_path; /* backwards predicted taken! */
}
s += 3;
hostinfo = s;
while ((uri_delims[*(unsigned char *)s] & NOTEND_HOSTINFO) == 0) {
++s;
}
uri = s; /* whatever follows hostinfo is start of uri */
/* If there's a username:password@host:port, the @ we want is the last @...
* too bad there's no memrchr()... For the C purists, note that hostinfo
* is definately not the first character of the original uri so therefore
* &hostinfo[-1] < &hostinfo[0] ... and this loop is valid C.
*/
do {
--s;
} while (s >= hostinfo && *s != '@');
if (s < hostinfo) {
/* again we want the common case to be fall through */
/* We expect hostinfo to point to the first character of
* the hostname. If there's a port it is the first colon.
*/
if (s == NULL) {
/* we expect the common case to have no port */
goto deal_with_path;
}
++s;
if (uri != s) {
if (*endstr == '\0') {
goto deal_with_path;
}
/* Invalid characters after ':' found */
return HTTP_BAD_REQUEST;
}
goto deal_with_path;
}
/* first colon delimits username:password */
if (s1) {
++s1;
}
else {
}
hostinfo = s + 1;
goto deal_with_host;
}
/* Special case for CONNECT parsing: it comes with the hostinfo part only */
/* See the INTERNET-DRAFT document "Tunneling SSL Through a WWW Proxy"
* currently at http://www.mcom.com/newsref/std/tunneling_ssl.html
* for the format of the "CONNECT host:port HTTP/1.0" request
*/
API_EXPORT(int) ap_parse_hostinfo_components(ap_context_t *p, const char *hostinfo, uri_components *uptr)
{
const char *s;
char *endstr;
/* Initialize the structure. parse_uri() and parse_uri_components()
* can be called more than once per request.
*/
/* We expect hostinfo to point to the first character of
* the hostname. There must be a port, separated by a colon
*/
if (s == NULL) {
return HTTP_BAD_REQUEST;
}
++s;
if (*s != '\0') {
if (*endstr == '\0') {
return HTTP_OK;
}
/* Invalid characters after ':' found */
}
return HTTP_BAD_REQUEST;
}
#endif