# Apache JServ Protocol Version 1.0

Obsolete - July 29, 1998

describes the protocol used by JServ to communicate between mod_jserv.c (the module)
and the Java servlet server (JServ). It works over a TCP socket.

### Startup

> The module starts up JServ during its initialization phase. It should run java, with the appropriate classpath to contain the JDK, JSDK and JServ classes. It may also contains servlets, but note that those cannot be reloaded if changed. JServ needs at least one command-line argument: the port it should listen on. The other command-line arguments are optional. The full usage is "<java interpreter> org.jserv.apache.JServ [-p configFile] [-a] port". If JServ is started with the "-a" command-line option, it will deactivate the authentication. This means that anyone on the machine can run servlets, so beware. Normally, it is started automatically by the module, without "-a". The "-p" command-line option can be used to tell JServ to use a configuration file instead of receiving information from System.in (stdin).
> The module then passes, to the java process's stdin, the secret authentication key (a string randomly generated by the server) and a newline. After, for every host that will use servlets, it sends one line contains minimally three pieces of information separated by tab:

> > hostname propertyfile (servlet_repository) +

> The first element is the name of the host that can run servlets. The second argument is the path to the property file for that host. Finally, follows a tab-delimited list of servlet repositories, either directories or zip/jar files. When all the host data has been sent. The module send a line containing "end".

### Request

> The request is made by opening a TCP connection to port JServ is running on. The socket should be opened to the loopback interface (127.0.0.1), request from any other address is refused by JServ.
> The module sends the request metadata to JServ. These are sent as lines, with two parts: a one-character identifier, and the data. This is preceded with a four-digit hex number indicating the length of the following string (with leading 0s), e.g. "000dHDate Friday", where "000d" is the length, "H" is the identifier, and "Date Friday" is the data.

1. Authentication and initialization
   The module sends "A<auth data>" where <auth data> is the secret key generated by the server and passed to JServ earlier. If this is not correct, JServ will reject the response.
2. Server name
   The module sends "S<hostname>". This is the host in which this servlet should be requested. This will determine where JServ will look to service the servlet.
3. Repository and class name
   The module sends "C<rep_name><tab><classname>". The first element is the repository to which the servlet has been map. This is currently unused but reserved for future extension where repository may be assigned a security model. The class name is the fully qualified name of the class in dot notation. i.e. foo.bar.Servlet. The servlet would load from one of the repository defined for that host or from the CLASSPATH.
4. Environment variables
   The module sends "E<var name><tab><var value>". The vars should contain all the standard CGI variables, but should not include the HTTP_ set, since the HTTP headers are sent later. e.g, "EPATH_INFO /foo" The following variables are required. Most are CGI/1.1 variables, but a few are Apache-specific. The following should all be sent (except for those marked with an asterix(*), which should only be sent if appropriate), as well as any additional CGI variables available:

   AUTH_TYPE(*): The type of authentication used
   CONTENT_LENGTH(*): The length of the request entity
   CONTENT_TYPE(*): The media type of the request entity
   DOCUMENT_ROOT: The server's main document root
   PATH_INFO(*): Extra URI path information
   PATH_TRANSLATED(*): The translated path info
   QUERY_STRING(*): The query arguments
   REQUEST_METHOD: The method used for the request
   REMOTE_USER(*): The authenticated username used for the request
   REMOTE_ADDR: The IP address of the requesting host
   REMOTE_HOST: The hostname of the requesting host (if available)
   SCRIPT_NAME: The URI portion that refers to the servlet
   SERVER_NAME: The hostname of the server
   SERVER_PORT: The port number of the server
   SERVER_PROTOCOL: The protocol used for the request
   SERVER_SOFTWARE: The name of the server software

5. Request headers
   The module sends all of the client's request headers, in the form "H<header name><tab><header value>". e.g., "HUser-Agent Mozilla/4.0 (Windows NT)"
6. A blank line
   When all the E and H lines (they may be interpolated), the module sends a blank line. (i.e., 0-length) This indicates that the metadata has been completed.
7. The request entity
   If there is a request entity, it is sent. If not, nothing further is sent.

### Response

> The response consists of two parts, response headers and the response entity. This is sent basically the same way CGI responses are sent:

1. Response headers
   The headers to be set for the response should then be sent, in MIME format: "<header name>: <header value>", terminated with a crlf. Headers set here will be outputted by the server. The server will add the HTTP line, and Server:, Date: and other headers, so only headers that the servlet needs to set should be set. e.g., Content-type, Cookie, whatever. When the last header has been sent, a blank line should be sent.
   A few headers have special meanings:
   - Status: <code> <string>
     This sets the response status to <code>, with a status message of <string>. This is the same as the CGI output header of the same name. e.g., "Status: 404 Not Found"
   - Servlet-Log: <string>
     This sends a message to be logged in the server's log. Servlet-Error: <message>. This indicates that JServHandler had an error occur. The server should send an error response back to the browser, with the status set by a separate Status: header (see above) - 500 should be used if Status: is not present. The <message> strong should be logged to the server's error log, as the reason why the request failed. No response entity will be present for this response.
2. Response entity
   JServHandler outputs the contents of the response to the module through the socket. When it is finished, JServ closes the socket. This will indicate to the module that the request has been completed.

### Signal Handling

> Since Java doesn't provide any facility for signal handling, after JServ is started and has read the host information via a stdin. It launches a thread that waits for signal token on stdin. These are three character strings (not newline-terminated) of the form "S##", where ## is the number (with leading 0, if neccessary) indicating a POSIX signal has been received, that JServ should deal with. Currently, JServ handles both SIGTERM (S15) and SIGHUP (S01) the same way : it calls destroy() on all its servlets and then quit via System.exit(0). If the signal was a SIGHUP, the parent process (Apache or standalonge JServ) will reinstall Java.
> What this achieves is that the calling process can install a signal handler for itself and when receives such a signal sends it on stdin of JServ.

Copyright (c) 1997-98 The Java Apache Project.
$Id: AJPv1.html,v 1.3 1999/06/09 05:21:29 jonbolt Exp $
All rights reserved.