Show Contents Previous Page Next Page Chapter 11 - C API Reference Guide, Part II In this section...
The Apache API provides an extensive set of functions for parsing and manipulating strings and URIs. Some of these routines are functionally identical to standard C library functions but provide either a performance boost or enhanced safety. Other routines provide completely new functionality. Show Contents Go to Top Previous Page Next PageWhile Apache's library for parsing and manipulating character strings is not nearly as rich as Perl's text processing abilities, it is a vast improvement over what's available in the impoverished standard C library. Most of the string parsing routines belong to the ap_getword* family, which together provide functionality similar to the Perl split() function. Each member of this family is able to extract a word from a string, splitting the text on delimiters such as whitespace or commas. Unlike Perl split(), in which the entire string is split at once and the pieces are returned in a list, the ap_getword* functions operate on one word at a time. The function returns the next word each time it's called and keeps track of where it's been by bumping up a pointer.
All of the ap_getword* routines are declared in httpd.h. The original declarations in httpd.h refer to the second argument as char *ap_getword (pool *p, const char **string, char stop) ap_getword() is the most frequently used member of this family. It takes a pointer to a
Here is an example of using ap_getword() to split a URL query string into its component
key/value pairs. ap_getword() is called in two different contexts. First it's called repeatedly
to split the query string into words delimited by the while(*data && (val = ap_getword(r->pool, &data, '&'))) {
key = ap_getword(r->pool, &val, '=');
ap_unescape_url((char *)key); ap_unescape_url((char *)val); ap_table_merge(tab, key, val); } This API also makes parsing HTTP cookies a breeze. In the following code fragment,
util_parse_cookie() fetches the incoming HTTP cookies and parses them
into a table. The incoming HTTP Cookie field, if present, contains
one or more cookies separated by semicolons. Each cookie has the format The code begins by retrieving the value of Cookie. It then splits it into individual name=value pairs using the ap_get_word() function. After trimming whitespace, ap_ getword() is called once more to split each cookie into its name and value parts and again a third time to split out the individual values. The values are unescaped with ap_ unescape_url(), and the parsed name and values are then added to a growing table: table *util_parse_cookie(request_rec *r)
{
const char *data = ap_table_get(r->headers_in, "Cookie");
table *cookies;
const char *pair;
if(!data) return NULL;
cookies = ap_make_table(r->pool, 4);
while(*data && (pair = ap_getword(r->pool, &data, ';'))) {
const char *name, *value;
if(*data == ' ') ++data;
name = ap_getword(r->pool, &pair, '=');
while(*pair && (value = ap_getword(r->pool, &pair, '&'))) {
ap_unescape_url((char *)value);
ap_table_add(cookies, name, value);
}
}
return cookies; } char *ap_getword_nc (pool *p, char **string, char stop)
char *ap_getword_nulls (pool *p, const char **string, char stop)
char *ap_getword_nulls_nc (pool *p, char **string, char stop)
char *ap_getword_white (pool *p, const char **string)
while(*data && (val = ap_getword_white(r->pool, &data))) {
...
}
char * ap_getword_white_nc (pool *p, char **string)
char *ap_getword_conf (pool *p, const char **string)
char *ap_getword_conf_nc (pool *p, char **string)
char *ap_get_token (pool *p, const char **string, int accept_white)
while(*data && (val = ap_get_token(r->pool, &data, 0))) {
...
}
int ap_find_token (pool *p, const char *string, const char *tok)
separators = "(" | ")" | "<" | ">" | "@"
| "," | ";" | ":" | "\" | <">
| "/" | "[" | "]" | "?" | "="
| "{" | "}" | SP | HT
ap_find_token() will return true if any token in the specified string matches the third argument,
if(ap_find_token(p, ap_table_get(r->headers_in, "Accept-encoding"), "gzip")) {
/* we could do some on-the-fly compression */
}
Show Contents Go to Top Previous Page Next PageCopyright © 1999 by O'Reilly & Associates, Inc. |
HIVE: All information for read only. Please respect copyright! |