This document is a proposal of evolution of the current Apache JServ Protocol version 1.3, also known as ajp13. I'll not cover here the full protocol but only the add-on from ajp13. This nth pass include comments from the tomcat-dev list and misses discovered during developpment.
Missing features in AJP13
ajp13 is a good protocol to link a servlet engine like tomcat to a web server like Apache:
- use persistent connections to avoid reconnect time at each request
- encode many http commands to reduce stream size
- send to servlet engine much info from web server (like SSL certs)
But ajp13 lacks support for:
- security between web server and servlet engine. Anybody can connect to an ajp13 port (no login mechanism used) You could connect, for example with telnet, and keep the remote thread up by not sending any data (no timeout in connection)
- context information passed from servlet engine to web server. Part of the configuration of JK, the web server connector, is to indicate to the web server which URI to handle. The mod_jk JkMount directive, told to web server which URI must be forwarded to servlet engine. A servlet engine already knows which URI it handle and TC 3.3 is allready capable to generate a config file for JK from the list of available contexts.
- state update of contexts from servlet engine to web server. Big site with farm of Tomcat, like ISP and virtuals hosters, may need to stop a context for admin purposes. In that case the front web server must know that the context is currently down, to eventually relay the request to another Tomcat
- verify state of connection before sending request. Actually JK send the request to the servlet engine and next wait for the answer. But one of the beauty of the socket API, is you that you could write() to a closed connection without any error reporting, but a read() to a closed connection return you the error code.
Proposed add-ons to AJP13
Let's describe here the features and add-on that could be added to AJP13. Since this document is a proposal, a reasonable level of chaos must be expected at first. Be sure that discussion on tomcat list will help clarify points, add features but the current list seems to be a 'minimun vital'
- Advanced login features at connect time
- Basic authorisation system, where a shared secret key is present in web server and servlet engine.
- Basic protocol negociation, just to be sure that if functionnalities are added to AJP13 in the future, current implementations will still works.
- Clean handling of 'Unknown packets'
- Extended env vars passed from web-server to servlet engine.
- Add extra SSL informations needed by Servlet 2.3 API (like SSL_KEY_SIZE)
Advanced login
- WEB-SERVER send LOGIN INIT CMD + NEGOCIATION DATA + WEB SERVER INFO
- TOMCAT respond with LOGIN SEED CMD + RANDOM DATA
- WEB-SERVER calculted the MD5 of RANDOM DATA+SECRET DATA
- WEB-SERVER send LOGIN COMP CMD + MD5 (SECRET DATA + RANDOM DATA)
- TOMCAT respond with LOGIN STATUS CMD + NEGOCIED DATA + SERVLET ENGINE INFO
Messages Stream
+----------------+------------------+-----------------+
| LOGIN INIT CMD | NEGOCIATION DATA | WEB SERVER INFO |
+----------------+------------------+-----------------+
+----------------+----------------+
| LOGIN SEED CMD | MD5 of entropy |
+----------------+----------------+
+----------------+----------------------------+
| LOGIN COMP CMD | MD5 of RANDOM + SECRET KEY |
+----------------+----------------------------+
+-----------+---------------+---------------------+
| LOGOK CMD | NEGOCIED DATA | SERVLET ENGINE INFO |
+-----------+---------------+---------------------+
+------------+--------------+
| LOGNOK CMD | FAILURE CODE |
+------------+--------------+
- LOGIN INIT CMD, LOGIN SEED CMD, LOGIN COMP CMD, LOGOK CMD, LOGNOK CMD are 1 byte long.
- MD5, MD5 of RANDOM + SECRET KEY are 32 chars long.
- NEGOCIATION DATA, NEGOCIED DATA, FAILURE CODE are 32 bits long.
- WEB SERVER INFO, SERVLET ENGINE INFO are CString.
worker.ajp13.port=8009
worker.ajp13.host=localhost
worker.ajp13.type=ajp13
worker.ajp13.secretkey=myverysecretkey
Shutdown feature
AJP13 miss a functionality of AJP12, which is shutdown command. A logout will tell servlet engine to shutdown itself.
+--------------+----------------------------+
| SHUTDOWN CMD | MD5 of RANDOM + SECRET KEY |
+--------------+----------------------------+
+------------+
| SHUTOK CMD |
+------------+
+-------------+--------------+
| SHUTNOK CMD | FAILURE CODE |
+-------------+--------------+
- SHUTDOWN CMD, SHUTOK CMD, SHUTNOK CMD are 1 byte long.
- MD5 of RANDOM + SECRET KEY are 32 chars long.
- FAILURE CODE is 32 bits long.
Extended Env Vars feature
NOTA: While working on AJP13 in JK, I really discovered "JkEnvVar". The following "Extended Env Vars feature" description may not be implemented in extended AJP13 since allready available in original implementation. DESC: Many users will want to see some of their web-server env vars passed to their servlet engine. To reduce the network traffic, the web-servlet will send a table to describing the external vars in a shorter fashion. We'll use there a functionality allready present in AJP13, attributes list: In the AJP13, we've got:
AJP13_FORWARD_REQUEST :=
prefix_code 2
method (byte)
protocol (string)
req_uri (string)
remote_addr (string)
remote_host (string)
server_name (string)
server_port (integer)
is_ssl (boolean)
num_headers (integer)
request_headers *(req_header_name req_header_value)
?context (byte string)
?servlet_path (byte string)
?remote_user (byte string)
?auth_type (byte string)
?query_string (byte string)
?route (byte string)
?ssl_cert (byte string)
?ssl_cipher (byte string)
?ssl_session (byte string)
?attributes *(attribute_name attribute_value)
request_terminator (byte)
+-------------------+---------------------------+-------------------------------+----+
| EXTENDED VARS CMD | WEB SERVER ATTRIBUTE NAME | SERVLET ENGINE ATTRIBUTE NAME | ES |
+-------------------+---------------------------+-------------------------------+----+
JkExtVars S1 SSL_CLIENT_V_START javax.servlet.request.ssl_start_cert_date
JkExtVars S2 SSL_CLIENT_V_END javax.servlet.request.ssl_end_cert_date
JkExtVars S3 SSL_SESSION_ID javax.servlet.request.ssl_session_id
+-------------------+----+-------------------------------------------+
| EXTENDED VARS CMD | S1 | javax.servlet.request.ssl_start_cert_date |
+-------------------+----+-------------------------------------------+
+----+-----------------------------------------+
| S2 | javax.servlet.request.ssl_end_cert_date |
+----+-----------------------------------------+
+----+-----------------------------------------+
| S3 | javax.servlet.request.ssl_end_cert_date |
+----+-----------------------------------------+
- EXTENDED VARS CMD is 1 byte long.
- WEB SERVER ATTRIBUTE NAME, SERVLET ENGINE ATTRIBUTE NAME are CString.
- ES is an empty CString.
Context informations forwarding for Servlet engine to web server
Just after the LOGON PHASE, the web server will ask for the list of contexts and URLs/URIs handled by the servlet engine. It will ease installation in many sites, reduce questions about configuration on tomcat-user list, and be ready for servlet API 2.3. This mode will be activated by a new directive JkAutoMount ie: JkAutoMount examples myworker1 /examples/ If we want to get ALL the contexts handled by the servlet engine, willcard could be used: ie: JkAutoMount * myworker1 * A servlet engine could have many contexts, /examples, /admin, /test. We may want to use only some contexts for a given worker. It was done previously, in Apache HTTP Server for example, by setting by hand the JkMount accordingly in each [virtual] area of Apache. If you web-server support virtual hosting, we'll forward also that information to servlet engine which will only return contexts for that virtual host. In that case the servlet engine will only return the URL/URI matching these particular virtual server (defined in server.xml). This feature will help ISP and big sites which mutualize large farm of Tomcat in load balancing configuration.
+-----------------+-------------------+----------+----------+----+
| CONTEXT QRY CMD | VIRTUAL HOST NAME | CONTEXTA | CONTEXTB | ES |
+-----------------+-------------------+----------+----------+----+
+------------------+-------------------+----------+-------------------+----------+---------------+----+
| CONTEXT INFO CMD | VIRTUAL HOST NAME | CONTEXTA | URL1 URL2 URL3 ES | CONTEXTB | URL1 URL2 ... | ES |
+------------------+-------------------+----------+-------------------+----------+---------------+----+
- CONTEXT QRY CMD and CONTEXT INFO CMD are 1 byte long.
- VIRTUAL HOST NAME is a CString, ie an array of chars terminated by a null byte (/0).
- An empty string is just a null byte (/0).
- ES is an empty CString. Indicate end of URI/URLs or end of CONTEXTs.
When VirtualMode is not to be used, the VIRTUAL HOST NAME is '*'. In that case the servlet engine will send all contexts handled.
Context informations updates from Servlet engine to web server
Context update are messages caming from the servlet engine each time a context is desactivated/reactivated. The update will be in use when the directive JkUpdateMount. This directive will set the AJP13_CONTEXT_UPDATE_NEG flag. ie: JkUpdateMount myworker1
+--------------------+-------------------+----------+--------+----------+--------+----+
| CONTEXT UPDATE CMD | VIRTUAL HOST NAME | CONTEXTA | STATUS | CONTEXTB | STATUS | ES |
+--------------------+-------------------+----------+--------+----------+--------+----+
- CONTEXT UPDATE CMD, STATUS are 1 byte long.
- VIRTUAL HOST NAME, CONTEXTS are CString.
- ES is an empty CString. Indicate end of CONTEXTs.
When VirtualMode is not in use, the VIRTUAL HOST NAME is '*'. STATUS is one byte indicating if context is UP/DOWN/INVALID
Context status query to Servlet engine
This query will be used by the web-server to determine if a given contexts are UP, DOWN or INVALID (and should be removed).
+-------------------+--------------------+----------+----------+----+
| CONTEXT STATE CMD | VIRTUAL HOST NAME | CONTEXTA | CONTEXTB | ES |
+-------------------+--------------------+----------+----------+----+
+-------------------------+-------------------+----------+--------+----------+--------+----+
| CONTEXT STATE REPLY CMD | VIRTUAL HOST NAME | CONTEXTA | STATUS | CONTEXTB | STATUS | ES |
+-------------------------+-------------------+----------+-------------------+--------+----+
- CONTEXT STATE CMD, CONTEXT STATE REPLY CMD, STATUS are 1 byte long.
- VIRTUAL HOST NAME, CONTEXTs are CString
- ES is an empty CString
When VirtualMode is not in use, the VIRTUAL HOST NAME is an empty string.
Handling of unknown packets
Sometimes even with a well negocied protocol, we may be in a situation where one end (web server or servlet engine), will receive a message it couldn't understand. In that case the receiver will send an 'UNKNOW PACKET CMD' with attached the unhandled message.
+--------------------+------------------------+-------------------+
| UNKNOWN PACKET CMD | UNHANDLED MESSAGE SIZE | UNHANDLED MESSAGE |
+--------------------+------------------------+-------------------+
- UNKNOWN PACKET CMD is 1 byte long.
- UNHANDLED MESSAGE SIZE is 16bits long.
- UNHANDLED MESSAGE is an array of byte (length is contained in UNHANDLED MESSAGE SIZE)
added UNHANDLED MESSAGE SIZE (development)
Verification of connection before sending request
NOTA: This fonctionality may never be used, since it may slow up the normal process since requiring on the web-server side an extra IO (read) before forwarding the request..... One of the beauty of socket APIs, is that you could write on a half closed socket. When servlet engine close the socket, the web server will discover it only at the next read() to the socket. Basically, in the AJP13 protocol, the web server send the HTTP HEADER and HTTP BODY (POST by chunk of 8K) to the servlet engine and then try to receive the reply. If the connection was broken the web server will learn it only at receive time. We could use a buffering scheme but what happen when you use the servlet engine for upload operations with more than 8ko of datas ? The hack in the AJP13 protocol is to add some bytes to read after the end of the service:
EXAMPLE OF DISCUSSION BETWEEN WEB SERVER AND SERVLET ENGINE
AJP HTTP-HEADER (+ HTTP-POST) (WEB->SERVLET)
AJP HTTP-REPLY (SERVLET->WEB)
AJP END OF DISCUSSION (SERVLET->WEB)
---> AJP STATUS (SERVLET->WEB AJP13)
+------------+-------------+
| STATUS CMD | STATUS DATA |
+------------+-------------+
- STATUS CMD and STATUS DATA are one byte long.