Before writing your Caching Proxy plug-in programs, you need to understand how the proxy server works. The behavior of the proxy server can be divided into several distinct processing steps. For each of these steps, you can supply your own customized functions using the API. For example, do you want to do something after a client request is read but before performing any other processing? Or maybe you want to perform special routines during authentication and then again after the requested file is sent.
A library of predefined functions is provided with the API. Your plug-in programs can call the predefined API functions in order to interact with the proxy server process (for example, to manipulate requests, to read or write request headers, or to write to the proxy server's logs). These functions should not be confused with the plug-in functions that you write, which are called by the proxy server. The predefined functions are described in Predefined functions and macros.
You instruct the proxy server to call your plug-in functions at the appropriate steps by using the corresponding Caching Proxy API directives in your server configuration file. These directives are described in Caching Proxy configuration directives for API steps.
This document includes the following:
You can use these components and procedures to write your own Caching Proxy plug-in programs.
The basic operation of the proxy server can be broken up into steps based on the type of processing that the server performs during that phase. Each step includes a juncture at which a specified part of your program can run. By adding API directives to your Caching Proxy configuration file (ibmproxy.conf), you indicate which of your plug-in functions you want to be called during a particular step. You can call several plug-in functions during a particular process step by including more than one directive for that step.
Some steps are part of the server request process. In other words, the proxy server executes these steps each time it processes a request. Other steps are performed independently of request processing; that is, the server executes these steps regardless of whether a request is being processed.
Your compiled program resides in a shared object, for example, a DLL or .so file, depending on your operating system. As the server proceeds through its request process steps, it calls the plug-in functions associated with each step until one of the functions indicates that it has handled the request. If you specify more than one plug-in function for a particular step, the functions are called in the order in which their directives appear in the configuration file.
If the request is not handled by a plug-in function (either you did not include a Caching Proxy API directive for that step, or your plug-in function for that step returned HTTP_NOACTION), the server performs its default action for that step.
Note: This is true for all steps except the Service step; the Service step does not have a default action.
Figure 1 depicts the steps of the proxy server process and defines the processing order for the steps that are related to request processing.
Four of the steps on the diagram are executed independently from the processing of any client request. These steps are related to the running and maintenance of the proxy server. They include the following:
The following list explains the purpose of each step pictured in Figure 1. Note that not all steps are guaranteed to be called for a particular request.
Performs processing after a request is read but before anything else is done.
If this step returns an indication that the request was processed (HTTP_OK), the server bypasses the other steps in the request process and performs only the Transmogrifier, Log, and PostExit steps.
Uses stored security tokens to check the physical path for protections, ACLs, and other access controls, and generates the WWW-Authenticate headers required for basic authentication. If you write your own plug-in function to replace this step, you must generate these headers yourself.
See Authentication and authorization for more information.
Decodes, verifies, and stores security tokens.
See Authentication and authorization for more information.
Performs processing after authorization and object location but before the request is satisfied.
If this step returns an indication that the request was processed (HTTP_OK), the server bypasses the other steps in the request process and performs only the Transmogrifier, Log, and PostExit steps.
On AIX® systems, you need an export file (for example, libmyapp.exp) that lists your plug-in functions, and you must link with the Caching Proxy API import file, libhttpdapi.exp.
On Linux, HP-UX, and Solaris systems, you must link with the libhttpdapi and libc libraries.
On Windows systems, you need a module definition file (.def) that lists your plug-in functions, and you must link with HTTPDAPI.LIB.
Be sure to include HTAPI.h and to use the HTTPD_LINKAGE macro in your function definitions. This macro ensures that all the functions use the same calling conventions.
Use the following compile and link commands as a guideline.
cc_r -c -qdbxextra -qcpluscmt foo.c
cc_r -bM:SRE -bnoentry -o libfoo.so foo.o -bI:libhttpdapi.exp
-bE:foo.exp
(This command is shown on two lines for readability only.)
cc -Ae -c +Z +DAportable
aCC +Z -mt -c +DAportable
gcc -c foo.c
ld -G -Bsymbolic -o libfoo.so foo.o -lhttpdapi -lc
cc -mt -Bsymbolic -c foo.c
cc -mt -Bsymbolic -G -o libfoo.so foo.o -lhttpdapi -lc
cl /c /MD /DWIN32 foo.c
link httpdapi.lib foo.obj /def:foo.def /out:foo.dll /dll
To specify exports, use one of these methods:
Follow the syntax presented in Plug-in function prototypes to write your own program functions for the defined request processing steps.
Each of your functions must fill in the return code parameter with a value that indicates what action was taken:
The function prototypes for each Caching Proxy step show the format to use and explain the type of processing they can perform. Note that the function names are not predefined. You must give your functions unique names, and you can choose your own naming conventions. For ease of association, this document uses names that relate to the server's processing steps.
In each of these plug-in functions, certain predefined API functions are valid. Some predefined functions are not valid for all steps. The following predefined API functions are valid when called from all of these plug-in functions:
Additional valid or invalid API functions are noted in the function prototype descriptions.
The value of the handle parameter sent to your functions can be passed as the first argument to the predefined functions. Predefined API functions are described in Predefined functions and macros.
void HTTPD_LINKAGE ServerInitFunction (
unsigned char *handle,
unsigned long *major_version,
unsigned long *minor_version,
long *return_code
)
A function defined for this step is called once when your module is loaded during server initialization. It is your opportunity to perform initialization before any requests have been accepted.
Although all server initialization functions are called, a error return code from a function in this step causes the server to ignore all other functions configured in the same module as the function that returned the error code. (That is, any other functions contained in the same shared object as the function that returned the error are not called.)
The version parameters contain the proxy server's version number; these are supplied by the Caching Proxy.
void HTTPD_LINKAGE PreExitFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request after the request has been read but before any processing has occurred. A plug-in at this step can be used to access the client's request before it is processed by the Caching Proxy.
Valid return codes for the preExit function are the following:
Other return codes must not be used.
If this function returns HTTP_OK, the proxy server assumes that the request has been handled. All subsequent request processing steps are bypassed, and only the response steps (Transmogrifier, Log, and PostExit) are performed.
All predefined API functions are valid during this step.
void HTTPD_LINKAGE MidnightFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step runs daily at midnight and contains no request context. For example, it can be used to invoke a child process to analyze logs. (Note that extensive processing during this step can interfere with logging.)
void HTTPD_LINKAGE AuthenticationFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request based on the request's authentication scheme. This function can be used to customize verification of the security tokens that are sent with a request.
void HTTPD_LINKAGE NameTransFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request. A URL template can be specified in the configuration file directive if you want the plug-in function to be called only for requests that match the template. The Name Translation step occurs before the request is processed and provides a mechanism for mapping URLs to objects such as file names.
void HTTPD_LINKAGE AuthorizationFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request. A URL template can be specified in the configuration file directive if you want the plug-in function to be called only for requests that match the template. The Authorization step occurs before the request is processed and can be used to verify that the identified object can be returned to the client. If you are doing basic authentication, you must generate the required WWW-Authenticate headers.
void HTTPD_LINKAGE ObjTypeFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request. A URL template can be specified in the configuration file directive if you want the plug-in function to be called only for requests that match the template. The Object Type step occurs before the request is processed and can be used to check whether the object exists, and to perform object typing.
void HTTPD_LINKAGE PostAuthFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called after the request has been authorized but before any processing has occurred. If this function returns HTTP_OK, the proxy server assumes that the request has been handled. All subsequent request steps are bypassed, and only the response steps (Transmogrifier, Log, and PostExit) are performed.
All server predefined functions are valid during this step.
void HTTPD_LINKAGE ServiceFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request. A URL template can be specified in the configuration file directive if you want the plug-in function to be called only for requests that match the template. The Service step satisfies the request, if it was not satisfied in the PreExit or PostAuthorization steps.
All server predefined functions are valid during this step.
Refer to the Enable directive in the WebSphere Application Server Caching Proxy Administration Guide for information on configuring your Service function to be executed based on the HTTP method rather than on the URL.
For this step, you must implement the following four functions. (Your function names do not need to match these names.)
void * HTTPD_LINKAGE openFunction (
unsigned char *handle,
long *return_code
)
The open function performs any initialization (such as buffer allocation) required to process the data for this stream. Any return code other than HTTP_OK causes this filter to abort (the write and close functions are not called). Your function can return a void pointer so that you can allocate space for a structure and have the pointer passed back to you in the correlator parameter of the subsequent functions.
void HTTPD_LINKAGE writeFunction (
unsigned char *handle,
unsigned char *data, /* response data sent by the
origin server */
unsigned long *length, /* length of response data */
void *correlator, /* pointer returned by the
'open' function */
long *return_code
)
The write function processes the data and can call the server's predefined HTTPD_write() function with the new or changed data. The plug-in must not attempt to free the buffer passed to it or expect the server to free the buffer it receives.
If you decide not to change the data during the scope of your write function, you still must call the HTTPD_write() function during the scope of either your open, write, or close function in order to pass the data for the response to the client. The correlator argument is the pointer to the data buffer that was returned in your open routine.
void HTTPD_LINKAGE closeFunction (
unsigned char *handle,
void *correlator,
long *return_code
)
The close function performs any clean-up actions (such as flushing and freeing the correlator buffer) required to complete processing the data for this stream. The correlator argument is the pointer to the data buffer that was returned in your open routine.
void HTTPD_LINKAGE errorFunction (
unsigned char *handle,
void *correlator,
long *return_code
)
The error function enables performance of clean-up actions, such as flushing or freeing the buffered data (or both) before an error page is sent. At this point, your open, write, and close functions are called to process the error page. The correlator argument is the pointer to the data buffer that was returned in your open routine.
Notes:
void HTTPD_LINKAGE GCAdvisorFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each file in the cache during garbage collection. This function enables you to influence which files are kept and which files are discarded. For more information, see the GC_* variables.
void HTTPD_LINKAGE ProxyAdvisorFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is invoked during service of each proxy request. For example, it can be used to set the USE_PROXY variable.
void HTTPD_LINKAGE LogFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request after the request has been processed and the communication to the client has been closed. A URL template can be specified in the configuration file directive if you want the plug-in function to be called only for requests that match the template. This function is called regardless of the success or failure of the request processing. If you do not want your log plug-in to override the default log mechanism, set your return code to HTTP_NOACTION instead of HTTP_OK.
void HTTPD_LINKAGE ErrorFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request that fails. A URL template can be specified in the configuration file directive if you want the plug-in function to be called only for failed requests that match the template. The Error step provides an opportunity for you to customize the error response.
void HTTPD_LINKAGE PostExitFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called for each request, regardless of the success or failure of the request. This step enables you to do clean-up tasks for any resources allocated by your plug-in to process the request.
void HTTPD_LINKAGE ServerTermFunction (
unsigned char *handle,
long *return_code
)
A function defined for this step is called when an orderly shutdown of the server occurs. It enables you to clean up resources allocated during the Server Initialization step. Do not call any HTTP_* functions in this step (the results are unpredictable). If you have more than one Caching Proxy API directive in your configuration file for Server Termination, they will all be called.
These return codes follow the HTTP 1.1 specification, RFC 2616, published by the World Wide Web Consortium (www.w3.org/pub/WWW/Protocols/). Your plug-in functions must return one of these values.
Value | Return code |
0 | HTTP_NOACTION |
100 | HTTP_CONTINUE |
101 | HTTP_SWITCHING_PROTOCOLS |
200 | HTTP_OK |
201 | HTTP_CREATED |
202 | HTTP_ACCEPTED |
203 | HTTP_NON_AUTHORITATIVE |
204 | HTTP_NO_CONTENT |
205 | HTTP_RESET_CONTENT |
206 | HTTP_PARTIAL_CONTENT |
300 | HTTP_MULTIPLE_CHOICES |
301 | HTTP_MOVED_PERMANENTLY |
302 | HTTP_MOVED_TEMPORARILY |
302 | HTTP_FOUND |
303 | HTTP_SEE_OTHER |
304 | HTTP_NOT_MODIFIED |
305 | HTTP_USE_PROXY |
307 | HTTP_TEMPORARY_REDIRECT |
400 | HTTP_BAD_REQUEST |
401 | HTTP_UNAUTHORIZED |
403 | HTTP_FORBIDDEN |
404 | HTTP_NOT_FOUND |
405 | HTTP_METHOD_NOT_ALLOWED |
406 | HTTP_NOT_ACCEPTABLE |
407 | HTTP_PROXY_UNAUTHORIZED |
408 | HTTP_REQUEST_TIMEOUT |
409 | HTTP_CONFLICT |
410 | HTTP_GONE |
411 | HTTP_LENGTH_REQUIRED |
412 | HTTP_PRECONDITION_FAILED |
413 | HTTP_ENTITY_TOO_LARGE |
414 | HTTP_URI_TOO_LONG |
415 | HTTP_BAD_MEDIA_TYPE |
416 | HTTP_BAD_RANGE |
417 | HTTP_EXPECTATION_FAILED |
500 | HTTP_SERVER_ERROR |
501 | HTTP_NOT_IMPLEMENTED |
502 | HTTP_BAD_GATEWAY |
503 | HTTP_SERVICE_UNAVAILABLE |
504 | HTTP_GATEWAY_TIMEOUT |
505 | HTTP_BAD_VERSION |
You can call the server's predefined functions and macros from your own plug-in functions. You must use their predefined names and follow the format described below. In the parameter descriptions, the letter i indicates an input parameter, the letter o indicates an output parameter, and i/o indicates that a parameter is used for both input and output.
Each of these functions returns one of the HTTPD return codes, depending on the success of the request. These codes are described in Return codes from predefined functions and macros.
Use the handle provided to your plug-in as the first parameter when calling these functions. Otherwise, the function returns an HTTPD_PARAMETER_ERROR error code. NULL is not accepted as a valid handle.
void HTTPD_LINKAGE HTTPD_authenticate (
unsigned char *handle, /* i; handle */
long *return_code /* o; return code */
)
void HTTPD_LINKAGE HTTPD_cacheable_url (
unsigned char *handle, /* i; handle */
unsigned char *url, /* i; URL to check */
unsigned char *req_method, /* i; request method for the URL */
long *retval /* o; return code */
)
The return value HTTPD_SUCCESS indicates that the URL content is cacheable; HTTPD_FAILURE indicates the content is not cacheable. HTTPD_INTERNAL_ERROR also is a possible return code for this function.
void HTTPD_LINKAGE HTTPD_close (
unsigned char *handle, /* i; handle */
long *return_code /* o; return code */
)
void HTTPD_LINKAGE HTTPD_exec (
unsigned char *handle, /* i; handle */
unsigned char *name, /* i; name of script to run */
unsigned long *name_length, /* i; length of the name */
long *return_code /* o; return code */
)
void HTTPD_LINKAGE HTTPD_extract (
unsigned char *handle, /* i; handle */
unsigned char *name, /* i; name of variable to extract */
unsigned long *name_length, /* i; length of the name */
unsigned char *value, /* o; buffer in which to put
the value */
unsigned long *value_length, /* i/o; buffer size */
long *return_code /* o; return code */
)
If this function returns the code HTTPD_BUFFER_TOO_SMALL, the buffer size you requested was not big enough for the extracted value. In this case, the function does not use the buffer but updates the value_length parameter with the buffer size that you need in order to successfully extract this value. Retry the extraction with a buffer that is at least as big as the returned value_length.
void HTTPD_LINKAGE HTTPD_file (
unsigned char *handle, /* i; handle */
unsigned char *name, /* i; name of file to send */
unsigned long *name_length, /* i; length of the name */
long *return_code /* o; return code */
)
const unsigned char * /* o; value of variable */
HTTPD_LINKAGE
httpd_getvar(
unsigned char *handle, /* i; handle */
unsigned char *name, /* i; variable name */
unsigned long *n /* i; index number for the array
containing the header */
)
The index for the array containing the header begins with 0. To obtain the first item in the array, use the value 0 for n; to obtain the fifth item, use the value 4 for n.
void HTTPD_LINKAGE HTTPD_log_access (
unsigned char *handle, /* i; handle */
unsigned char *value, /* i; data to write */
unsigned long *value_length, /* i; length of the data */
long *return_code /* o; return code */
)
Note that escape symbols are not required when writing the percent symbol (%) in server access logs.
void HTTPD_LINKAGE HTTPD_log_error (
unsigned char *handle, /* i; handle */
unsigned char *value, /* i; data to write */
unsigned long *value_length, /* i; length of the data */
long *return_code /* o; return code */
)
Note that escape symbols are not required when writing the percent symbol (%) in server error logs.
void HTTPD_LINKAGE HTTPD_log_event (
unsigned char *handle, /* i; handle */
unsigned char *value, /* i; data to write */
unsigned long *value_length, /* i; length of the data */
long *return_code /* o; return code */
)
Note that escape symbols are not required when writing the percent symbol (%) in server event logs.
void HTTPD_LINKAGE HTTPD_log_trace (
unsigned char *handle, /* i; handle */
unsigned char *value, /* i; data to write */
unsigned long *value_length, /* i; length of the data */
long *return_code /* o; return code */
)
Note that escape symbols are not required when writing the percent symbol (%) in server trace logs.
void HTTPD_LINKAGE HTTPD_open (
unsigned char *handle, /* i; handle */
long *return_code /* o; return code */
)
void HTTPD_LINKAGE HTTPD_proxy (
unsigned char *handle, /* i; handle */
unsigned char *url_name, /* i; URL for the
proxy request */
unsigned long *name_length, /* i; length of URL */
void *request_body, /* i; body of request */
unsigned long *body_length, /* i; length of body */
long *return_code /* o; return code */
)
void HTTPD_LINKAGE HTTPD_read (
unsigned char *handle, /* i; handle */
unsigned char *value, /* i; buffer for data */
unsigned long *value_length, /* i/o; buffer size
(data length) */
long *return_code /* o; return code */
)
void HTTPD_LINKAGE HTTPD_restart (
long *return_code /* o; return code */
)
Note that you can also create variables with this function. Variables that you create are subject to the conventions for HTTP_ and PROXY_ prefixes, which are described in Variables. If you create a variable that begins with HTTP_, it is sent as a header in the response to the client, without the HTTP_ prefix. For example, to set a Location header, use HTTPD_set() with the variable name HTTP_LOCATION. Variables created with a PROXY_ prefix are sent as headers in the request to the content server. Variables created with a CGI_ prefix are passed to CGI programs.
This function is valid in all steps; however, not all variables are valid in all steps.
void HTTPD_LINKAGE HTTPD_set (
unsigned char *handle, /* i; handle */
unsigned char *name, /* i; name of value to set */
unsigned long *name_length, /* i; length of the name */
unsigned char *value, /* i; buffer with value */
unsigned long *value_length, /* i; length of value */
long *return_code /* o; return code */
)
long /* o; return code */
HTTPD_LINKAGE httpd_setvar (
unsigned char *handle, /* i; handle */
unsigned char *name, /* i; variable name */
unsigned char *value, /* i; new value */
unsigned long *addHdr /* i; add header or replace it */
)
The addHdr parameter has four possible values:
These values are defined in HTAPI.h.
void HTTPD_LINKAGE httpd_variant_insert (
unsigned char *handle, /* i; handle */
unsigned char *URI, /* i; URI of this object */
unsigned char *dimension, /* i; dimension of variation */
unsigned char *variant, /* i; value of the variant */
unsigned char *filename, /* i; file containing the object */
long *return_code /* o; return code */
)
Notes:
Mozilla 4.0 (compatible; BatBrowser 94.1.2; Bat OS)
void HTTPD_LINKAGE httpd_variant_lookup (
unsigned char *handle, /* i; handle */
unsigned char *URI, /* URI of this object */
unsigned char *dimension, /* i; dimension of variation */
unsigned char *variant, /* i; value of the variant */
long *return_code); /* o; return code */
If you do not set the content type before calling this function for the first time, the server assumes that you are sending a CGI data stream.
void HTTPD_LINKAGE HTTPD_write (
unsigned char *handle, /* i; handle */
unsigned char *value, /* i; data to send */
unsigned char *value_length, /* i; length of the data */
long *return_code); /* o; return code */
The server will set the return code parameter to one of these values, depending on the success of the request:
Value | Status code | Explanation |
---|---|---|
-1 | HTTPD_UNSUPPORTED | The function is not supported. |
0 | HTTPD_SUCCESS | The function succeeded, and the output fields are valid. |
1 | HTTPD_FAILURE | The function failed. |
2 | HTTPD_INTERNAL_ERROR | An internal error was encountered and processing for this request cannot continue. |
3 | HTTPD_PARAMETER_ERROR | One or more invalid parameters was passed. |
4 | HTTPD_STATE_CHECK | The function is not valid in this process step. |
5 | HTTPD_READ_ONLY | (Returned only by HTTPD_set and httpd_setvar.) The variable is read-only and cannot be set by the plug-in. |
6 | HTTPD_BUFFER_TOO_SMALL | (Returned by HTTPD_set, httpd_setvar, and HTTPD_read.) The buffer provided was too small. |
7 | HTTPD_AUTHENTICATE_FAILED | (Returned only by HTTPD_authenticate.) The authentication failed. Examine the HTTP_RESPONSE and HTTP_REASON variables for more information. |
8 | HTTPD_EOF | (Returned only by HTTPD_read.) Indicates the end of the request body. |
9 | HTTPD_ABORT_REQUEST | The request was aborted because the client provided an entity tag that did not match the condition specified by the request. |
10 | HTTPD_REQUEST_SERVICED | (Returned by HTTPD_proxy.) The function that was called completed the response for this request. |
11 | HTTPD_RESPONSE_ALREADY_COMPLETED | The function failed because the response for that request has already been completed. |
12 | HTTPD_WRITE_ONLY | The variable is write-only and cannot be read by the plug-in. |
Each step in the request process has a configuration directive that you use to indicate which of your plug-in functions you want to call and execute during that step. You can add these directives to your server's configuration file (ibmproxy.conf) by manually editing and updating it, or by using the API Request Processing form in the Caching Proxy Configuration and Administration forms.
This means that the server processes the Service, NameTrans, Exec, Fail, Map, Pass, Proxy, ProxyWAS, and Redirect directives in their sequence within the configuration file. When the server successfully maps a URL to a file, it does not read or process any other of these directives. (The Map directive is an exception. Refer to the WebSphere Application Server Caching Proxy Administration Guide for complete information about proxy server mapping rules.)
These configuration file directives must appear in the ibmproxy.conf file as one line, with no spaces other than those explicitly specified here. Although line breaks appear for readability in some of the syntax examples, there must be no spaces at those points in the actual directive.
The variables in these directives have the following meanings:
The Service directive requires an asterisk (*) after the function name if you want to have access to path information.
For additional information, including syntax, for these directives, see the WebSphere Application Server Caching Proxy Administration Guide.