To request a particular version of NSAPI, define the USE_NSAPI_VERSION macro before including the nsapi.h header file. The requested NSAPI version is encoded by multiplying the major version number by 100 and then adding this to the minor version number. For example, the following code requests NSAPI 3.2 features:
#define USE_NSAPI_VERSION 302 /* We want NSAPI 3.2 (Web Server 6.1) */ #include "nsapi.h"
To develop a plug-in that is compatible across multiple server versions, define USE_NSAPI_VERSION to the highest NSAPI version supported by all of the target server versions.
The following table lists server versions and the highest NSAPI version supported by each:
Table 4–2 NSAPI Versions Supported by Different Servers
Server Version |
NSAPI Version |
---|---|
iPlanet Web Server 4.1 |
3.0 |
iPlanet Web Server 6.0 |
3.1 |
Netscape Enterprise Server 6.0 |
3.1 |
Netscape Enterprise Server 6.1 |
3.1 |
Sun ONE Application Server 7.0 |
3.1 |
Sun Java System Web Server 6.1 |
3.2 |
Sun Java System Web Proxy Server 4 |
3.3 |
Do not request a version of NSAPI higher than the highest version supported by the nsapi.h header that the plug-in is being compiled against. Additionally, to use USE_NSAPI_VERSION, you must compile against an nsapi.h header file that supports NSAPI 3.3 or higher.
int USE_NSAPI_VERSION
The following code can be used when building a plug-in designed to work with Sun Java System Web Proxy Server 4:
#define USE_NSAPI_VERSION 303 /* We want NSAPI 3.3 (Proxy Server 4) */ #include "nsapi.h"
NSAPI_RUNTIME_VERSION, NSAPI_VERSION
The util_can_exec function checks that a specified file can be executed, returning either a 1 (executable) or a 0. The function checks whether the file can be executed by the user with the given user and group ID.
Use this function before executing a program using the exec system call.
int util_can_exec(struct stat *finfo, uid_t uid, gid_t gid);
1 if the file is executable, or 0 if the file is not executable.
stat *finfo is the stat structure associated with a file.
uid_t uid is the UNIX user ID.
gid_t gid is the UNIX group ID. Together with uid, this value determines the permissions of the UNIX user.
util_env_create, util_getline, util_hostname
The util_chdir2path function changes the current directory to a specified directory where you will access a file.
When running under Windows, use a critical section to ensure that more than one thread does not call this function at the same time.
Using util_chdir2path makes file access a little quicker because this function does not require a full path.
int util_chdir2path(char *path);
0 if the directory was changed, or -1 if the directory could not be changed.
char *path is the name of a directory.
The parameter must be a writable string because the string is not permanently modified.
The util_cookie_find function finds a specific cookie in a cookie string and returns its value.
char *util_cookie_find(char *cookie, char *name);
If successful, returns a pointer to the NULL-terminated value of the cookie. Otherwise, returns NULL. This function modifies the cookie string parameter by NULL-terminating the name and value.
char *cookie is the value of the Cookie: request header.
char *name is the name of the cookie whose value is to be retrieved.
The util_does_process_exist function verifies that a given process ID is that of an executing process.
#include <libproxy/util.h> int util_does_process_exist (int pid)
nonzero if the pid represents an executing process
0 if the pid does not represent an executing process
int pid is the process ID to be tested.
util_url_fix_host name, util_uri_check
The util_env_create function creates and allocates the environment specified by env, returning a pointer to the environment. If the parameter env is NULL, the function allocates a new environment. Use util_env_create to create an environment when executing a new program.
#include <base/util.h> char **util_env_create(char **env, int n, int *pos);
A pointer to an environment.
char **env is the existing environment or NULL.
int n is the maximum number of environment entries that you want in the environment.
int *pos is an integer that keeps track of the number of entries used in the environment.
util_env_replace, util_env_str , util_env_free , util_env_find
The util_env_find function locates the string denoted by a name in a specified environment and returns the associated value. Use this function to find an entry in an environment.
char *util_env_find(char **env, char *name);
The value of the environment variable if it is found, or NULL if the string was not found.
char **env is the environment.
char *name is the name of an environment variable in env.
util_env_replace, util_env_str , util_env_free , util_env_create
The util_env_free function frees a specified environment. Use this function to deallocate an environment that you created using the function util_env_create.
void util_env_free(char **env);
void
char **env is the environment to be freed.
util_env_replace, util_env_str , util_env_create , util_env_find
The util_env_replace function replaces the occurrence of the variable denoted by a name in a specified environment with a specified value. Use this function to change the value of a setting in an environment.
void util_env_replace(char **env, char *name, char *value);
void
char **env is the environment.
char *name is the name of a name-value pair.
char *value is the new value to be stored.
util_env_str, util_env_free , util_env_find, util_env_create
The util_env_str function creates an environment entry and returns it. This function does not check for nonalphanumeric symbols in the name such as the equal sign “=”. You can use this function to create a new environment entry.
char *util_env_str(char *name, char *value);
A newly allocated string containing the name-value pair.
char *name is the name of a name-value pair.
char *value is the new value to be stored.
util_env_replace, util_env_free , util_env_create , util_env_find
The util_get_current_gmt function obtains the current time, represented in terms of GMT (Greenwich Mean Time).
#include <libproxy/util.h> time_t util_get_current_gmt(void);
the current GMT
No parameter is required.
The util_get_int_from_aux_file function is used to get a single line from a specified file and return it in the form of an integer. This ifunction enables you to store single numbers in a file.
#include <libproxy/cutil.h> int util_get_int_from_file(char *root, char *name);
An integer from the file.
char*root is the name of the directory containing the file to be read.
char*name is the name of the file to be read.
util_get_long_from_aux_file, util_get_string_from_aux_file, util_get_int_from_file, util_get_long_from_file, util_get_string_from_file, util_put_int_to_file, util_put_long_to_file, util_put_string_to_aux_file, util_put_string_to_file
The util_get_int_from_file function is used to get a single line from a specified file and return it in the form of an integer. This function enables you to store single numbers in a file.
#include <libproxy/cutil.h> int util_get_int_from_file(char *filename);
An integer from the file.
-1 if no value was obtained from the file.
char *filename is the name of the file to be read.
util_get_long_from_file, util_get_string_from_file, util_put_int_to_file, util_put_long_to_file, util_put_string_to_file
The util_get_long_from_file function is used to get a single line from a specified file and return it in the form of a long number. This function enables you to store single long numbers in a file.
#include <libproxy/cutil.h> long util_get_long_from_file(char *root,char *name);
A long integer from the file.
char *root is the name of the directory containing the file to be read.
char *name is the name of the file to be read.
util_get_int_from_aux_file, util_get_string_from_aux_file, util_get_int_from_file, util_get_long_from_file, util_get_string_from_file, util_put_int_to_file, util_put_long_to_file, util_put_string_to_aux_file, util_put_string_to_file
The util_get_long_from_file function is used to get a single line from a specified file and return it in the form of a long number. This function enables you to store single long numbers in a file.
#include <libproxy/cutil.h> long util_get_long_from_file(char *filename);
A long integer from the file.
-1 if no value was obtained from the file.
char *file is the name of the file to be read.
util_get_int_from_file, util_get_string_from_file, util_put_int_to_file, util_put_long_to_file, util_put_string_to_file
The util_get_string_from_aux_file function is used to get a single line from a specified file and return it in the form of a word. This function enables you to store single words in a file.
#include <libproxy/cutil.h> char *util_get_string_from_file(char *root, char *name, char *buf, int maxsize);
A string containing the next line from the file.
char *root is the name of the directory containing the file to be read.
char *name is the name of the file to be read.
char *buf is the string to use as the file buffer.
int maxsize is the maximum size for the file buffer.
util_get_int_from_aux_file, util_get_long_from_aux_file, util_get_int_from_file, util_get_long_from_file, util_get_string_from_file, util_put_int_to_file, util_put_long_to_file, util_put_string_to_aux_file, util_put_string_to_file
The util_get_string_from_file function is used to get a single line from a specified file and return it in the form of a word. This function enables you to store single words in a file.
#include <libproxy/cutil.h> char *util_get_string_from_file(char *filename, char *buf, int maxsize);
A string containing the next line from the file.
NULL if no string was obtained.
char *file is the name of the file to be read.
char *buf is the string to use as the file buffer.
int maxsize is the maximum size for the file buffer.
util_get_int_from_file, util_get_long_from_file, util_put_int_to_file, util_put_long_to_file, util_put_string_to_file
The util_getline function scans the specified file buffer to find a line feed or carriage return/line feed terminated string. The string is copied into the specified buffer, and NULL-terminates it. The function returns a value that indicates whether the operation stored a string in the buffer, encountered an error, or reached the end of the file.
Use this function to scan lines out of a text file, such as a configuration file.
int util_getline(filebuf *buf, int lineno, int maxlen, char *line);
0 if successful line contains the string.
1 if the end of file was reached line contains the string.
-1 if an error occurred line contains a description of the error.
filebuf *buf is the file buffer to be scanned.
int lineno is used to include the line number in the error message when an error occurs. The caller is responsible for making sure that the line number is accurate.
int maxlen is the maximum number of characters that can be written into l.
char *l is the buffer in which to store the string. The user is responsible for allocating and deallocating line.
util_can_exec, util_env_create, util_hostname
The util_hostname function retrieves the local host name and returns it as a string. If the function cannot find a fully qualified domain name, it returns NULL. You may reallocate or free this string. Use this function to determine the name of the system you are on.
char *util_hostname(void);
If a fully qualified domain name was found, returns a string containing that name. Otherwise, the function returns NULL if the fully qualified domain name was not found.
none
The util_is_mozilla function checks whether a specified user-agent header string is a Netscape browser of at least a specified revision level. The function uses strings to specify the revision level to avoid ambiguities such as 1.56 > 1.5.
int util_is_mozilla(char *ua, char *major, char *minor);
1 if the user-agent is a Netscape browser, or 0 if the user-agent is not a Netscape browser.
char *ua is the user-agent string from the request headers.
char *major is the major release number to the left of the decimal point.
char *minor is the minor release number to the right of the decimal point.
The util_is_url function checks whether a string is a URL. The string is a URL if it begins with alphabetic characters followed by a colon (:).
int util_is_url(char *url);
1 if the string specified by url is a URL, or 0 if the string specified by url is not a URL.
char *url is the string to be examined.
util_is_mozilla, util_later_than
The util_itoa function converts a specified integer to a string, and returns the length of the string. Use this function to create a textual representation of a number.
int util_itoa(int i, char *a);
The length of the string created.
int i is the integer to be converted.
char *a is the ASCII string that represents the value. The user is responsible for the allocation and deallocation of a. The string should be at least 32 bytes long.
The util_later_than function compares the date specified in a time structure against a date specified in a string. If the date in the string is later than or equal to the one in the time structure, the function returns 1. Use this function to handle RFC 822, RFC 850, and ctime formats.
int util_later_than(struct tm *lms, char *ims);
1 if the date represented by ims is the same as or later than that represented by the lms, or 0 if the date represented by ims is earlier than that represented by the lms.
tm *lms is the time structure containing a date.
char *ims is the string containing a date.
The util_make_filename function concatenates a directory name and a file name into a newly created string. This function is useful when you are dealing with a number of files that all go to the same directory.
#include <libproxy/cutil.h> char *util_make_filename(char *root, char *name);
A new string containing the directory name concatenated with the file name.
char *root is a string containing the directory name.
char *name is a string containing the file name.
The util_make_gmt function converts a given local time to GMT (Greenwich Mean Time), or obtains the current GMT.
#include <libproxy/util.h> time_t util_make_gmt(time_t t);
The GMT equivalent to the local time t, if t is not 0
The current GMT if t is 0
time_t t is a time.
The util_make_local function converts a given GMT to local time.
#include <libproxy/util.h> time_t util_make_local(time_t t);
The local equivalent to the GMT t.
time_t t is a time.
The util_move_dir function moves a directory, preserving permissions, creation times, and last-access times. If renaming fails, for example, if the source and destination are on two different file systems, the function copies the directory.
#include <libproxy/util.h> int util_move_dir (char *src, char *dst);
0 if the move failed.
nonzero if the move succeeded.
char *src is the fully qualified name of the source directory.
char *dst is the fully qualified name of the destination directory.
The util_move_dir function moves a file, preserving permissions, creation time, and last-access time. If renaming fails, for example, if the source and destination are on two different file systems, the function copies the file.
#include <libproxy/util.h> int util_move_file (char *src, char *dst);
0 if the move failed.
nonzero if the move succeeded.
char *src is the fully qualified name of the source file.
char *dst is the fully qualified name of the destination file.
The util_parse_http_time function converts a given HTTP time string to time_t format.
#include <libproxy/util.h> time_t util_parse_http_time(char *date_string);
The time_t equivalent to the GMT t.
Parameters
time_t t is a time.
The util_put_int_to_file function writes a single line containing an integer to a specified file.
#include <libproxy/cutil.h> int util_put_int_to_file(char *filename, int i);
nonzero if the operation succeeded
0 if the operation failed
char *file is the name of the file to be written.
int i is the integer to write.
util_get_int_from_file, util_get_long_from_file, util_put_long_to_file, util_put_string_to_file
The util_put_long_to_file function writes a single line containing a long integer to a specified file.
#include <libproxy/cutil.h> ing util_put_long_to_file(char *filename, long l);
nonzero if the operation succeeded
0 if the operation failed
char *file is the name of the file to be written.
long l is the long integer to write.
util_get_int_from_file, util_get_long_from_file, util_put_int_to_file, util_put_string_to_file
The util_put_string_to_aux_file function writes a single line containing a string to a file specified by directory name and file name.
#include <libproxy/cutil.h> int util_put_string_to_aux_file(char *root, char *name, char *str);
non-zero if the operation succeeded.
0 if the operation failed.
char *root is the name of the directory where the file is to be written.
char *name is the name of the file is to be written.
char *str is the string to write.
util_get_int_from_file, util_get_long_from_file, util_put_int_to_file, util_put_long_to_file, util_put_string_to_file
The util_put_string_to_file function writes a single line containing a string to a specified file.
#include <libproxy/cutil.h> int util_put_string_to_file(char *filename, char *str);
nonzero if the operation succeeded.
0 if the operation failed.
char *file is the name of the file to be read.
char *str is the string to write.
util_get_int_from_file, util_get_long_from_file, util_put_int_to_file, util_put_long_to_file
The util_sect_id function creates a section ID from the section dim and an index.
#include <libproxy/cutil.h> void util_sect_id(int dim, int idx, char *buf);
nonzero if the operation succeeded.
0 if the operation failed.
int dim is the section dim.
int idx is the index.
char *buf is the buffer to receive the section ID.
The util_sh_escape function parses a specified string and places a backslash (\\) in front of any shell-special characters, returning the resultant string. Use this function to ensure that strings from clients won’t cause a shell to behave unexpectedly.
The shell-special characters are the space plus the following characters:
&;\Q’"|*?~<>^()[]{}$\\#!
char *util_sh_escape(char *s);
A newly allocated string.
char *s is the string to be parsed.
The util_snprintf function formats a specified string, using a specified format, into a specified buffer using the printf-style syntax and performs bounds checking. The function returns the number of characters in the formatted buffer.
For more information, see the documentation on the printf function for the runtime library of your compiler.
int util_snprintf(char *s, int n, char *fmt, ...);
The number of characters formatted into the buffer.
char *s is the buffer to receive the formatted string.
int n is the maximum number of bytes allowed to be copied.
char *fmt is the format string. The function handles only %d and %s strings. It does not handle any width or precision strings.
... represents a sequence of parameters for the printf function.
util_sprintf, util_vsnprintf, util_vsprintf
The util_sprintf function formats a specified string, using a specified format, into a specified buffer, using the printf-style syntax without bounds checking. The function returns the number of characters in the formatted buffer.
Because util_sprintf doesn’t perform bounds checking, use this function only if you are certain that the string fits the buffer. Otherwise, use the function util_snprintf. For more information, see the documentation on the printf function for the runtime library of your compiler.
int util_sprintf(char *s, char *fmt, ...);
The number of characters formatted into the buffer.
char *s is the buffer to receive the formatted string.
char *fmt is the format string. The function handles only %d and %s strings. It does not handle any width or precision strings.
... represents a sequence of parameters for the printf function.
char *logmsg;int len;logmsg = (char *) MALLOC(256); len = util_sprintf(logmsg, "%s %s %s\\n", ip, method, uri);
util_snprintf, util_vsnprintf, util_vsprintf
The util_strcasecmp function performs a comparison of two alphanumeric strings and returns a -1, 0, or 1 to signal which string is larger or that the strings are identical.
The comparison is not case sensitive.
int util_strcasecmp(const char *s1, const char *s2);
1 if s1 is greater than s2.
0 if s1 is equal to s2.
-1 if s1 is less than s2.
char *s1 is the first string.
char *s2 is the second string.
The util_strftime function translates a tm structure, which is a structure describing a system time, into a textual representation. This function is a thread-safe version of the standard strftime function
int util_strftime(char *s, const char *format, const struct tm *t);
The number of characters placed into s, not counting the terminating NULL character.
char *s is the string buffer to put the text into. The function does not check bounds, so you must make sure that your buffer is large enough for the text of the date.
const char *format is a format string resembling a printf string in that it consists of text with certain %x substrings. You may use the constant HTTP_DATE_FMT to create date strings in the standard Internet format. For more information, see the documentation on the printf function for the runtime library of your compiler. Refer to Chapter 7, Time Formats for details on time formats.
const struct tm *t is a pointer to a calendar time (tm) structure, usually created by the function system_localtime or system_gmtime.
system_localtime, system_gmtime
The util_strncasecmp function performs a comparison of the first n characters in the alphanumeric strings and returns a -1, 0, or 1 to signal which string is larger or that the strings are identical.
The function’s comparison is not case sensitive.
int util_strncasecmp(const char *s1, const char *s2, int n);
1 if s1 is greater than s2.
0 if s1 is equal to s2.
-1 if s1 is less than s2.
char *s1 is the first string.
char *s2 is the second string.
int n is the number of initial characters to compare.
The util_uri_check function checks whether a URI has a format conforming to the standard.
At present, the only URI checked for is a URL. The standard format for a URL is
protocol://user:password@host:port/url-path
where user:password, :password. :port, or /url-path can be omitted.
#include <libproxy/util.h> int util_uri_check (char *uri);
0 if the URI does not have the proper form.
nonzero if the URI has the proper form.
char *uri is the URI to be tested.
The util_uri_escape function converts any special characters in the URI into the URI format, %XX, where XX is the hexadecimal equivalent of the ASCII character, and returns the escaped string. The special characters are %?#:+&*"<>, space, carriage return, and line feed.
Use util_uri_escape before sending a URI back to the client.
char *util_uri_escape(char *d, char *s);
The string, possibly newly allocated with escaped characters replaced.
char *d is a string. If d is not NULL, the function copies the formatted string into d and returns it. If d is NULL, the function allocates a properly sized string and copies the formatted special characters into the new string, then returns it.
The util_uri_escape function does not check bounds for the parameter d. Therefore, if d is not NULL, it should be at least three times as large as the string s.
char *s is the string containing the original unescaped URI.
util_uri_is_evil, util_uri_parse, util_uri_unescape
The util_uri_is_evil function checks a specified URI for insecure path characters. Insecure path characters include //, /./, /../ and/., /.. (and for Windows./) at the end of the URI. Use this function to see whether a URI requested by the client is insecure.
int util_uri_is_evil(char *t);
1 if the URI is insecure, or 0 if the URI is OK.
char *t is the URI to be checked.
util_uri_escape, util_uri_parse
The util_uri_parse function converts //, /./, and /*/../ into / in the specified URI where * is any character other than /. You can use this function to convert a URI’s unacceptable sequences into valid ones. First use the function util_uri_is_evil to determine whether the function has an incorrect sequence.
void util_uri_parse(char *uri);
void
char *uri is the URI to be converted.
util_uri_is_evil, util_uri_unescape
The util_uri_unescape function converts the encoded characters of a URI into their ASCII equivalents. Encoded characters appear as %XX, where XX is a hexadecimal equivalent of the character.
You cannot use an embedded null in a string, because NSAPI functions assume that a null is the end of the string. Therefore, passing Unicode-encoded content through an NSAPI plug-in doesn’t work.
void util_uri_unescape(char *uri);
void
char *uri is the URI to be converted.
util_uri_escape, util_uri_is_evil, util_uri_parse
The util_url_cmp function compares two URLs. This function is analogous to the strcmp( )()library function of C.
#include <libproxy/util.h> int util_url_cmp (char *s1, char *s2);
-1 if the first URL, s1, is less than the second, s2
0 if they are identical
1 if the first URL, s1, is greater than the second, s2
char *s1 is the first URL to be tested.
char *s2 is the second URL to be tested.
util_url_fix_host name, util_uri_check
The util_url_fix_host name function converts the host name in a URL to lowercase and removes redundant port numbers.
#include <libproxy/util.h> void util_url_fix_host name(char *url);
void but changes the value of its parameter string
The protocol specifier and the host name in the parameter string are changed to lowercase. The function also removes redundant port numbers, such as 80 for HTTP, 70 for gopher, and 21 for FTP.
char *url is the URL to be converted.
The util_url_has_FQDN function returns a value to indicate whether a specified URL references a fully qualified domain name.
#include <libproxy/util.h> int util_url_has_FQDN(char *url);
1 if the URL has a fully qualified domain name.
0 if the URL does not have a fully qualified domain name.
char *url is the URL to be examined.
The util_vsnprintf function formats a specified string, using a specified format, into a specified buffer using the vprintf-style syntax and performs bounds checking. The function returns the number of characters in the formatted buffer.
For more information, see the documentation on the printf function for the runtime library of your compiler.
int util_vsnprintf(char *s, int n, register char *fmt, va_list args);
The number of characters formatted into the buffer.
char *s is the buffer to receive the formatted string.
int n is the maximum number of bytes allowed to be copied.
register char *fmt is the format string. The function handles only %d and %s strings. It does not handle any width or precision strings.
va_list args is an STD argument variable obtained from a previous call to va_start.
The util_vsprintf function formats a specified string, using a specified format, into a specified buffer using the vprintf-style syntax without bounds checking. The function returns the number of characters in the formatted buffer.
For more information, see the documentation on the printf function for the runtime library of your compiler.
int util_vsprintf(char *s, register char *fmt, va_list args);
The number of characters formatted into the buffer.
char *s is the buffer to receive the formatted string.
register char *fmt is the format string. The function handles only %d and %s strings. It does not handle any width or precision strings.
va_list args is an STD argument variable obtained from a previous call to va_start.