Moved apache code into a folder to help prepare for packaging where we dont want...
[httpd-crcsyncproxy.git] / apache / docs / manual / mod / mod_proxy_ajp.xml
blob57ccc079ab57fcfa023dfd43d5fd62eaee829607
1 <?xml version="1.0"?>
2 <!DOCTYPE modulesynopsis SYSTEM "../style/modulesynopsis.dtd">
3 <?xml-stylesheet type="text/xsl" href="../style/manual.en.xsl"?>
4 <!-- $LastChangedRevision$ -->
6 <!--
7  Licensed to the Apache Software Foundation (ASF) under one or more
8  contributor license agreements.  See the NOTICE file distributed with
9  this work for additional information regarding copyright ownership.
10  The ASF licenses this file to You under the Apache License, Version 2.0
11  (the "License"); you may not use this file except in compliance with
12  the License.  You may obtain a copy of the License at
14      http://www.apache.org/licenses/LICENSE-2.0
16  Unless required by applicable law or agreed to in writing, software
17  distributed under the License is distributed on an "AS IS" BASIS,
18  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  See the License for the specific language governing permissions and
20  limitations under the License.
21 -->
23 <modulesynopsis metafile="mod_proxy_ajp.xml.meta">
25 <name>mod_proxy_ajp</name>
26 <description>AJP support module for
27 <module>mod_proxy</module></description>
28 <status>Extension</status>
29 <sourcefile>mod_proxy_ajp.c</sourcefile>
30 <identifier>proxy_ajp_module</identifier>
31 <compatibility>Available in version 2.1 and later</compatibility>
33 <summary>
34     <p>This module <em>requires</em> the service of <module
35     >mod_proxy</module>. It provides support for the 
36     <code>Apache JServ Protocol version 1.3</code> (hereafter
37     <em>AJP13</em>).</p>
39     <p>Thus, in order to get the ability of handling <code>AJP13</code>
40     protocol, <module>mod_proxy</module> and
41     <module>mod_proxy_ajp</module> have to be present in the server.</p>
43     <note type="warning"><title>Warning</title>
44       <p>Do not enable proxying until you have <a
45       href="mod_proxy.html#access">secured your server</a>. Open proxy
46       servers are dangerous both to your network and to the Internet at
47       large.</p>
48     </note>
49 </summary>
51 <seealso><module>mod_proxy</module></seealso>
53 <section id="overviewprotocol"><title>Overview of the protocol</title>
54     <p>The <code>AJP13</code> protocol is packet-oriented.  A binary format
55     was presumably chosen over the more readable plain text for reasons of
56     performance.  The web server communicates with the servlet container over
57     TCP connections.  To cut down on the expensive process of socket creation,
58     the web server will attempt to maintain persistent TCP connections to the
59     servlet container, and to reuse a connection for multiple request/response
60     cycles.</p>
61     <p>Once a connection is assigned to a particular request, it will not be
62     used for any others until the request-handling cycle has terminated.  In
63     other words, requests are not multiplexed over connections.  This makes
64     for much simpler code at either end of the connection, although it does
65     cause more connections to be open at once.</p>
66     <p>Once the web server has opened a connection to the servlet container,
67     the connection can be in one of the following states:</p>
68     <ul>
69     <li> Idle <br/> No request is being handled over this connection. </li>
70     <li> Assigned <br/> The connecton is handling a specific request.</li>
71     </ul>
72     <p>Once a connection is assigned to handle a particular request, the basic
73     request informaton (e.g. HTTP headers, etc) is sent over the connection in
74     a highly condensed form (e.g. common strings are encoded as integers).
75     Details of that format are below in Request Packet Structure. If there is a
76     body to the request <code>(content-length > 0)</code>, that is sent in a
77     separate packet immediately after.</p>
78     <p>At this point, the servlet container is presumably ready to start
79     processing the request.  As it does so, it can send the
80     following messages back to the web server:</p>
81     <ul>
82     <li>SEND_HEADERS <br/>Send a set of headers back to the browser.</li>
83     <li>SEND_BODY_CHUNK <br/>Send a chunk of body data back to the browser.
84     </li>
85     <li>GET_BODY_CHUNK <br/>Get further data from the request if it hasn't all
86     been transferred yet.  This is necessary because the packets have a fixed
87     maximum size and arbitrary amounts of data can be included the body of a
88     request (for uploaded files, for example).  (Note: this is unrelated to
89     HTTP chunked tranfer).</li>
90     <li>END_RESPONSE <br/> Finish the request-handling cycle.</li>
91     </ul>
92     <p>Each message is accompanied by a differently formatted packet of data.
93     See Response Packet Structures below for details.</p>
94 </section>
96 <section id="basppacketstruct"><title>Basic Packet Structure</title>
97     <p>There is a bit of an XDR heritage to this protocol, but it differs
98     in lots of ways (no 4 byte alignment, for example).</p>
99     <p>Byte order: I am not clear about the endian-ness of the individual
100     bytes.  I'm guessing the bytes are little-endian, because that's what
101     XDR specifies, and I'm guessing that sys/socket library is magically
102     making that so (on the C side).  If anyone with a better knowledge of
103     socket calls can step in, that would be great.</p>
104     <p>There are four data types in the protocol: bytes, booleans,
105     integers and strings.</p>
106     <dl>
107     <dt><strong>Byte</strong></dt><dd>A single byte.</dd>
108     <dt><strong>Boolean</strong></dt>
109       <dd>A single byte, <code>1 = true</code>, <code>0 = false</code>.
110       Using other non-zero values as true (i.e. C-style) may work in some places,
111       but it won't in others.</dd>
112     <dt><strong>Integer</strong></dt>
113       <dd>A number in the range of <code>0 to 2^16 (32768)</code>.  Stored in
114       2 bytes with the high-order byte first.</dd>
115     <dt><strong>String</strong></dt>
116       <dd>A variable-sized string (length bounded by 2^16). Encoded with
117       the length packed into two bytes first, followed by the string
118       (including the terminating '\0').  Note that the encoded length does
119       <strong>not</strong> include the trailing '\0' -- it is like
120       <code>strlen</code>.  This is a touch confusing on the Java side, which
121       is littered with odd autoincrement statements to skip over these
122       terminators.  I believe the reason this was done was to allow the C
123       code to be extra efficient when reading strings which the servlet
124       container is sending back -- with the terminating \0 character, the
125       C code can pass around references into a single buffer, without copying.
126       if the \0 was missing, the C code would have to copy things out in order
127       to get its notion of a string.</dd>
128     </dl>
130   <section><title>Packet Size</title>
131     <p>According to much of the code, the max packet size is <code>
132     8 * 1024 bytes (8K)</code>.  The actual length of the packet is encoded in
133     the header.</p>
134   </section>
135   <section><title>Packet Headers</title>
136     <p>Packets sent from the server to the container begin with
137     <code>0x1234</code>.  Packets sent from the container to the server
138     begin with <code>AB</code> (that's the ASCII code for A followed by the
139     ASCII code for B).  After those first two bytes, there is an integer
140     (encoded as above) with the length of the payload.  Although this might
141     suggest that the maximum payload could be as large as 2^16, in fact, the
142     code sets the maximum to be 8K.</p>
143     <table>
144       <tr>
145         <td colspan="6"><em>Packet Format (Server->Container)</em></td>
146       </tr>
147       <tr>
148         <td>Byte</td>
149         <td>0</td>
150         <td>1</td>
151         <td>2</td>
152         <td>3</td>
153         <td>4...(n+3)</td>
154       </tr>
155       <tr>
156         <td>Contents</td>
157         <td>0x12</td>
158         <td>0x34</td>
159         <td colspan="2">Data Length (n)</td>
160         <td>Data</td>
161       </tr>
162     </table>
163     <table>
164       <tr>
165         <td colspan="6"><em>Packet Format (Container->Server)</em></td>
166       </tr>
167       <tr>
168         <td>Byte</td>
169         <td>0</td>
170         <td>1</td>
171         <td>2</td>
172         <td>3</td>
173         <td>4...(n+3)</td>
174       </tr>
175       <tr>
176         <td>Contents</td>
177         <td>A</td>
178         <td>B</td>
179         <td colspan="2">Data Length (n)</td>
180         <td>Data</td>
181       </tr>
182     </table>
183     <p>For most packets, the first byte of the payload encodes the type of
184      message.  The exception is for request body packets sent from the server to
185      the container -- they are sent with a standard packet header (<code>
186      0x1234</code> and then length of the packet), but without any prefix code
187      after that.</p>
188      <p>The web server can send the following messages to the servlet
189      container:</p>
190     <table>
191       <tr>
192         <td>Code</td>
193         <td>Type of Packet</td>
194         <td>Meaning</td>
195       </tr>
196       <tr>
197         <td>2</td>
198         <td>Forward Request</td>
199         <td>Begin the request-processing cycle with the following data</td>
200       </tr>
201       <tr>
202         <td>7</td>
203         <td>Shutdown</td>
204         <td>The web server asks the container to shut itself down.</td>
205       </tr>
206       <tr>
207         <td>8</td>
208         <td>Ping</td>
209         <td>The web server asks the container to take control
210         (secure login phase).</td>
211       </tr>
212       <tr>
213         <td>10</td>
214         <td>CPing</td>
215         <td>The web server asks the container to respond quickly with a CPong.
216         </td>
217       </tr>
218       <tr>
219         <td>none</td>
220         <td>Data</td>
221         <td>Size (2 bytes) and corresponding body data.</td>
222       </tr>
223     </table>
224     <p>To ensure some basic security, the container will only actually do the
225     <code>Shutdown</code> if the request comes from the same machine on which
226     it's hosted.</p>
227     <p>The first <code>Data</code> packet is send immediatly after the
228     <code>Forward Request</code> by the web server.</p>
229     <p>The servlet container can send the following types of messages to the
230     webserver:</p>
231     <table>
232       <tr>
233         <td>Code</td>
234         <td>Type of Packet</td>
235         <td>Meaning</td>
236       </tr>
237       <tr>
238         <td>3</td>
239         <td>Send Body Chunk</td>
240         <td>Send a chunk of the body from the servlet container to the web
241         server (and presumably, onto the browser). </td>
242       </tr>
243       <tr>
244         <td>4</td>
245         <td>Send Headers</td>
246         <td>Send the response headers from the servlet container to the web
247         server (and presumably, onto the browser).</td>
248       </tr>
249       <tr>
250         <td>5</td>
251         <td>End Response</td>
252         <td>Marks the end of the response (and thus the request-handling cycle).
253         </td>
254       </tr>
255       <tr>
256         <td>6</td>
257         <td>Get Body Chunk</td>
258         <td>Get further data from the request if it hasn't all been
259         transferred yet.</td>
260       </tr>
261       <tr>
262         <td>9</td>
263         <td>CPong Reply</td>
264         <td>The reply to a CPing request</td>
265       </tr>
266     </table>
267     <p>Each of the above messages has a different internal structure, detailed
268     below.</p>
269   </section>
270 </section>
271 <section id="rpacetstruct"><title>Request Packet Structure</title>
272     <p>For messages from the server to the container of type
273     <em>Forward Request</em>:</p>
274     <example><pre>
275 AJP13_FORWARD_REQUEST :=
276     prefix_code      (byte) 0x02 = JK_AJP13_FORWARD_REQUEST
277     method           (byte)
278     protocol         (string)
279     req_uri          (string)
280     remote_addr      (string)
281     remote_host      (string)
282     server_name      (string)
283     server_port      (integer)
284     is_ssl           (boolean)
285     num_headers      (integer)
286     request_headers *(req_header_name req_header_value)
287     attributes      *(attribut_name attribute_value)
288     request_terminator (byte) OxFF
289     </pre></example>
290     <p>The <code>request_headers</code> have the following structure:
291     </p><example><pre>
292 req_header_name := 
293     sc_req_header_name | (string)  [see below for how this is parsed]
295 sc_req_header_name := 0xA0xx (integer)
297 req_header_value := (string)
298 </pre></example>
299     <p>The <code>attributes</code> are optional and have the following
300     structure:</p>
301     <example><pre>
302 attribute_name := sc_a_name | (sc_a_req_attribute string)
304 attribute_value := (string)
306     </pre></example>
307     <p>Not that the all-important header is <code>content-length</code>,
308     because it determines whether or not the container looks for another
309     packet immediately.</p>
310   <section><title>Detailed description of the elements of Forward Request
311   </title></section>
312   <section><title>Request prefix</title>
313     <p>For all requests, this will be 2. See above for details on other Prefix
314     codes.</p>
315   </section>
316   <section><title>Method</title>
317     <p>The HTTP method, encoded as a single byte:</p>
318     <table>
319       <tr><td>Command Name</td><td>Code</td></tr>
320       <tr><td>OPTIONS</td><td>1</td></tr>
321       <tr><td>GET</td><td>2</td></tr>
322       <tr><td>HEAD</td><td>3</td></tr>
323       <tr><td>POST</td><td>4</td></tr>
324       <tr><td>PUT</td><td>5</td></tr>
325       <tr><td>DELETE</td><td>6</td></tr>
326       <tr><td>TRACE</td><td>7</td></tr>
327       <tr><td>PROPFIND</td><td>8</td></tr>
328       <tr><td>PROPPATCH</td><td>9</td></tr>
329       <tr><td>MKCOL</td><td>10</td></tr>
330       <tr><td>COPY</td><td>11</td></tr>
331       <tr><td>MOVE</td><td>12</td></tr>
332       <tr><td>LOCK</td><td>13</td></tr>
333       <tr><td>UNLOCK</td><td>14</td></tr>
334       <tr><td>ACL</td><td>15</td></tr>
335       <tr><td>REPORT</td><td>16</td></tr>
336       <tr><td>VERSION-CONTROL</td><td>17</td></tr>
337       <tr><td>CHECKIN</td><td>18</td></tr>
338       <tr><td>CHECKOUT</td><td>19</td></tr>
339       <tr><td>UNCHECKOUT</td><td>20</td></tr>
340       <tr><td>SEARCH</td><td>21</td></tr>
341       <tr><td>MKWORKSPACE</td><td>22</td></tr>
342       <tr><td>UPDATE</td><td>23</td></tr>
343       <tr><td>LABEL</td><td>24</td></tr>
344       <tr><td>MERGE</td><td>25</td></tr>
345       <tr><td>BASELINE_CONTROL</td><td>26</td></tr>
346       <tr><td>MKACTIVITY</td><td>27</td></tr>
347     </table>
348     <p>Later version of ajp13, will transport 
349     additional methods, even if they are not in this list.</p>
350   </section>
351   <section><title>protocol, req_uri, remote_addr, remote_host, server_name,
352   server_port, is_ssl</title>
353     <p>These are all fairly self-explanatory.  Each of these is required, and
354     will be sent for every request.</p>
355   </section>
356   <section><title>Headers</title>
357     <p>The structure of <code>request_headers</code> is the following:
358     First, the number of headers <code>num_headers</code> is encoded.
359     Then, a series of header name <code>req_header_name</code> / value
360     <code>req_header_value</code> pairs follows.
361     Common header names are encoded as integers,
362     to save space.  If the header name is not in the list of basic headers,
363     it is encoded normally (as a string, with prefixed length).  The list of
364     common headers <code>sc_req_header_name</code>and their codes
365     is as follows (all are case-sensitive):</p>
366     <table>
367       <tr><td>Name</td><td>Code value</td><td>Code name</td></tr>
368       <tr><td>accept</td><td>0xA001</td><td>SC_REQ_ACCEPT</td></tr>
369       <tr><td>accept-charset</td><td>0xA002</td><td>SC_REQ_ACCEPT_CHARSET
370       </td></tr>
371       <tr><td>accept-encoding</td><td>0xA003</td><td>SC_REQ_ACCEPT_ENCODING
372       </td></tr>
373       <tr><td>accept-language</td><td>0xA004</td><td>SC_REQ_ACCEPT_LANGUAGE
374       </td></tr>
375       <tr><td>authorization</td><td>0xA005</td><td>SC_REQ_AUTHORIZATION</td>
376       </tr>
377       <tr><td>connection</td><td>0xA006</td><td>SC_REQ_CONNECTION</td></tr>
378       <tr><td>content-type</td><td>0xA007</td><td>SC_REQ_CONTENT_TYPE</td>
379       </tr>
380       <tr><td>content-length</td><td>0xA008</td><td>SC_REQ_CONTENT_LENGTH</td>
381       </tr>
382       <tr><td>cookie</td><td>0xA009</td><td>SC_REQ_COOKIE</td></tr>
383       <tr><td>cookie2</td><td>0xA00A</td><td>SC_REQ_COOKIE2</td></tr>
384       <tr><td>host</td><td>0xA00B</td><td>SC_REQ_HOST</td></tr>
385       <tr><td>pragma</td><td>0xA00C</td><td>SC_REQ_PRAGMA</td></tr>
386       <tr><td>referer</td><td>0xA00D</td><td>SC_REQ_REFERER</td></tr>
387       <tr><td>user-agent</td><td>0xA00E</td><td>SC_REQ_USER_AGENT</td></tr>
388     </table>
389     <p>The Java code that reads this grabs the first two-byte integer and if
390     it sees an <code>'0xA0'</code> in the most significant
391     byte, it uses the integer in the second byte as an index into an array of
392     header names.  If the first byte is not <code>0xA0</code>, it assumes that
393     the two-byte integer is the length of a string, which is then read in.</p>
394     <p>This works on the assumption that no header names will have length
395     greater than <code>0x9999 (==0xA000 - 1)</code>, which is perfectly
396     reasonable, though somewhat arbitrary.</p>
397     <note><title>Note:</title>
398     The <code>content-length</code> header is extremely
399     important.  If it is present and non-zero, the container assumes that
400     the request has a body (a POST request, for example), and immediately
401     reads a separate packet off the input stream to get that body.
402     </note>
403   </section>
404   <section><title>Attributes</title>
405     <p>The attributes prefixed with a <code>?</code>
406     (e.g. <code>?context</code>) are all optional.  For each, there is a
407     single byte code to indicate the type of attribute, and then its value
408     (string or integer).  They can be sent in any order (though the C code
409     always sends them in the order listed below).  A special terminating code
410     is sent to signal the end of the list of optional attributes. The list of
411     byte codes is:</p>
412     <table>
413       <tr><td>Information</td><td>Code Value</td><td>Type Of Value</td><td>Note</td></tr>
414       <tr><td>?context</td><td>0x01</td><td>-</td><td>Not currently implemented
415       </td></tr>
416       <tr><td>?servlet_path</td><td>0x02</td><td>-</td><td>Not currently implemented
417       </td></tr>
418       <tr><td>?remote_user</td><td>0x03</td><td>String</td><td></td></tr>
419       <tr><td>?auth_type</td><td>0x04</td><td>String</td><td></td></tr>
420       <tr><td>?query_string</td><td>0x05</td><td>String</td><td></td></tr>
421       <tr><td>?jvm_route</td><td>0x06</td><td>String</td><td></td></tr>
422       <tr><td>?ssl_cert</td><td>0x07</td><td>String</td><td></td></tr>
423       <tr><td>?ssl_cipher</td><td>0x08</td><td>String</td><td></td></tr>
424       <tr><td>?ssl_session</td><td>0x09</td><td>String</td><td></td></tr>
425       <tr><td>?req_attribute</td><td>0x0A</td><td>String</td><td>Name (the name of the
426       attribute follows)</td></tr>
427       <tr><td>?ssl_key_size</td><td>0x0B</td><td>Integer</td><td></td></tr>
428       <tr><td>are_done</td><td>0xFF</td><td>-</td><td>request_terminator</td></tr>
429     </table>
430     <p>The <code>context</code> and <code>servlet_path</code> are not
431     currently set by the C code, and most of the Java code completely ignores
432     whatever is sent over for those fields (and some of it will actually break
433     if a string is sent along after one of those codes).  I don't know if this
434     is a bug or an unimplemented feature or just vestigial code, but it's
435     missing from both sides of the connection.</p>
436     <p>The <code>remote_user</code> and <code>auth_type</code> presumably
437     refer to HTTP-level authentication, and communicate the remote user's
438     username and the type of authentication used to establish their identity
439     (e.g. Basic, Digest).</p>
440     <p>The <code>query_string</code>, <code>ssl_cert</code>,
441     <code>ssl_cipher</code>, and <code>ssl_session</code> refer to the
442     corresponding pieces of HTTP and HTTPS.</p>
443     <p>The <code>jvm_route</code>, is used to support sticky
444     sessions -- associating a user's sesson with a particular Tomcat instance
445     in the presence of multiple, load-balancing servers.</p>
446     <p>Beyond this list of basic attributes, any number of other attributes
447     can be sent via the <code>req_attribute</code> code <code>0x0A</code>.
448     A pair of strings to represent the attribute name and value are sent
449     immediately after each instance of that code.  Environment values are passed
450     in via this method.</p>
451     <p>Finally, after all the attributes have been sent, the attribute
452     terminator, <code>0xFF</code>, is sent.  This signals both the end of the
453     list of attributes and also then end of the Request Packet.</p>
454   </section>
455 </section>
457 <section id="resppacketstruct"><title>Response Packet Structure</title>
458     <p>for messages which the container can send back to the server.</p>
459     <example><pre>
460 AJP13_SEND_BODY_CHUNK :=
461   prefix_code   3
462   chunk_length  (integer)
463   chunk        *(byte)
464   chunk_terminator (byte) Ox00
467 AJP13_SEND_HEADERS :=
468   prefix_code       4
469   http_status_code  (integer)
470   http_status_msg   (string)
471   num_headers       (integer)
472   response_headers *(res_header_name header_value)
474 res_header_name :=
475     sc_res_header_name | (string)   [see below for how this is parsed]
477 sc_res_header_name := 0xA0 (byte)
479 header_value := (string)
481 AJP13_END_RESPONSE :=
482   prefix_code       5
483   reuse             (boolean)
486 AJP13_GET_BODY_CHUNK :=
487   prefix_code       6
488   requested_length  (integer)
489     </pre></example>
490   <section><title>Details:</title></section>
491   <section><title>Send Body Chunk</title>
492     <p>The chunk is basically binary data, and is sent directly back to the
493     browser.</p>
494   </section>
495   <section><title>Send Headers</title>
496     <p>The status code and message are the usual HTTP things
497     (e.g. <code>200</code> and <code>OK</code>). The response header names are
498     encoded the same way the request header names are. See header_encoding above
499     for details about how the codes are distinguished from the strings.<br />
500     The codes for common headers are:</p>
501     <table>
502       <tr><td>Name</td><td>Code value</td></tr>
503       <tr><td>Content-Type</td><td>0xA001</td></tr>
504       <tr><td>Content-Language</td><td>0xA002</td></tr>
505       <tr><td>Content-Length</td><td>0xA003</td></tr>
506       <tr><td>Date</td><td>0xA004</td></tr>
507       <tr><td>Last-Modified</td><td>0xA005</td></tr>
508       <tr><td>Location</td><td>0xA006</td></tr>
509       <tr><td>Set-Cookie</td><td>0xA007</td></tr>
510       <tr><td>Set-Cookie2</td><td>0xA008</td></tr>
511       <tr><td>Servlet-Engine</td><td>0xA009</td></tr>
512       <tr><td>Status</td><td>0xA00A</td></tr>
513       <tr><td>WWW-Authenticate</td><td>0xA00B</td></tr>
514     </table>
515     <p> After the code or the string header name, the header value is
516     immediately encoded.</p>
517   </section>
518   <section><title>End Response</title>
519     <p>Signals the end of this request-handling cycle.  If the
520     <code>reuse</code> flag is true <code>(==1)</code>, this TCP connection can
521     now be used to handle new incoming requests.  If <code>reuse</code> is false
522     (anything other than 1 in the actual C code), the connection should
523     be closed.</p>
524   </section>
525   <section><title>Get Body Chunk</title>
526     <p>The container asks for more data from the request (If the body was
527     too large to fit in the first packet sent over or when the request is
528     chuncked). The server will send a body packet back with an amount of data
529     which is the minimum of the <code>request_length</code>, the maximum send
530     body size <code>(8186 (8 Kbytes - 6))</code>, and the number of bytes
531     actually left to send from the request body.<br/>
532     If there is no more data in the body (i.e. the servlet container is
533     trying to read past the end of the body), the server will send back an
534     <em>empty</em> packet, which is a body packet with a payload length of 0.
535     <code>(0x12,0x34,0x00,0x00)</code></p>
536   </section>
537 </section>
540 </modulesynopsis>