The Apache Tomcat Connectors

来源:互联网 发布:煲机软件 编辑:程序博客网 时间:2024/05/16 15:13

简介

  最初的文档是Dan Milstein,danmil@shore.NET于2000年12月份写的,当前文档由一个xml文件生成,可以更加简单地与Tomcat文档集成。

文档描述了Apache JServ协议1.3版本(现在的ajp13),当前没有文档讲述这个协议是如何工作。这个文档尝试着填补这个空缺,方便JK的维护者和任何想使用这个协议到其他地方的人(例如jakarta

作者

  我不是这个协议的设计者,我认为Gal Shachor是最初的设计者.这个文档的任何内容都是基于tomcat3.x代码实现。我希望这个文档是有意义的,但是不能保证绝对的准确性,同时对于一些特定的设计决策我也不知道是如何定的。对一些特定特定选择我能够且提供了些可能的判断,但也只是我的猜测。总体上来说,Shachor写的C代码非常的整洁和易理解(几乎不需要文档说明).我清理了Java代码,使得更易阅读。

设计目标

   根据Gal Shachor发给jakarta-dev邮件列表的email.JK(也就是ajp13)的原本目的是继承mod_jserv和ajp12(我只仅仅列出了和web Server与servlet容器通信相关的目标):

  • 提升性能(特别是速度)
  • 添加对SSL的支持,以便isSecure()和getScheme()在servlet容器功能正常。客户端证书和密码组件作为请求属性


协议概览

  ajp13协议面向数据包,据推测,出于性能考虑,选择了字节格式而非可读性更高的文本。Web Server通过TCP连接和servlet容器通信。为了减少昂贵的socket创建性能开销,Web Server会尝试维护与servlet容器的TCP长连接,对多个请求/相应循环重用连接。

  一旦一个连接被分配给一个特定的请求,请求处理周期结束前连接不可被其他请求所使用。也就是说,请求不能多路复用连接。这使得连接结束处的代码更加简单,尽管导致立马打开更多连接。

  一旦web server打开一个与servlet容器的连接,连接可能状态如下:

  • 空闲 这个连接未处理任何请求
  • 被分配 连接正处理一个特定请求

  一旦一个连接被分配去处理一个特定请求,基础的请求信息(如Http头等)以高压缩的形式(公共字符串编码为数值)通过连接发送。格式的细节如下以请求包结构讲解。假如请求有body(content-length>0),body信息会紧接着以独立数据包方式发送。

  这时候,servlet容器假设已经准备处理请求,处理完毕,容器会发送如下信息返回给web server:

  • SEND_HEADERS发送回一组头部信息给浏览器
  • SEND_BODY_CHUNK发送回一块body数据给浏览器
  • GET_BODY_CHUNK从请求获得更多的数据,假如还有数据未传递完毕。这是必要的因为包大小有固定的最大值,请求的body可能包含任意大小的数据(如上传文件)(注意:这与HTTP块转移没有任何关系)
  • END_RESPONSE结束请求处理周期

每条信息伴随着一条不同包格式的数据,更多信息参照以下Response包结构。

基本包结构

  这个协议有点继承了XDR外部数据表示(External Data Representation),但是也有许多不同的地方(如无4字节对齐)

AJP13为所有数据类型使用网络字节顺序。

协议有四种数据类型:bytes, booleans, integers strings.

Byte

单个字节

Boolean

单个字节,1=true,0=false。一些地方使用非0代表true可行,其他不行。

Integer

一个0到2^16(32768)范围的数字,2字节存储,高位字节在前

String

一个可变长度的字符串(长度范围为2^16),以字符串长度值打包成两字节开始,跟着字符串内容(包括终结符'\0')。注意编码长度不包括最终的'\0',跟strlen一样,***

我认为这样做的原因是当读取servlet容器返回的字符串-以\0字符结尾,C代码更高效,C代码可以分发引用至单个缓冲区,不需要拷贝。假如\0丢失了,C代码为了获得字符串概念需要拷贝出字符串。注意大小为-1(65535)代表空字符串且在这种情况下长度后面不跟任何数据。

包大小

根据许多代码,最大的包大小为8 * 1024 bytes (8K)。实际包长度被编码进头信息。

包头部

从server发送至容器的包以0X1234开头,从容器发送至server的包以AB(ASCII编码A跟着ASCII编码B)开头.这两个字节后,有一个代表负载长度的数值(如上编码)。尽管这可能表明最大负载值可最大至2^16,实际上,编码设置这个值最大为8K。

Packet Format (Server->Container)Byte01234...(n+3)Contents0x120x34Data Length (n)DataPacket Format (Container->Server)Byte01234...(n+3)ContentsABData Length (n)Data


对于大部分包,


待续


Intro

The original document was written by Dan Milstein, danmil@shore.net on December 2000. The present document is generated out of an xml file to allow a more easy integration in the Tomcat documentation.

This describes the Apache JServ Protocol version 1.3 (hereafter ajp13). There is, apparently, no current documentation of how the protocol works. This document is an attempt to remedy that, in order to make life easier for maintainers of JK, and for anyone who wants to port the protocol somewhere (into jakarta 4.x, for example).

author

I am not one of the designers of this protocol -- I believe that Gal Shachor was the original designer. Everything in this document is derived from the actual implementation I found in the tomcat 3.x code. I hope it is useful, but I can't make any grand claims to perfect accuracy. I also don't know why certain design decisions were made. Where I was able, I've offered some possible justifications for certain choices, but those are only my guesses. In general, the C code which Shachor wrote is very clean and comprehensible (if almost totally undocumented). I've cleaned up the Java code, and I think it's reasonably readable.

Design Goals

According to email from Gal Shachor to the jakarta-dev mailing list, the original goals of JK (and thusajp13) were to extend mod_jserv and ajp12 by (I am only including the goals which relate to communication between the web server and the servlet Container):

  • Increasing performance (speed, specifically).
  • Adding support for SSL, so that isSecure() and getScheme() will function correctly within the servlet container. The client certificates and cipher suite will be available to servlets as request attributes.
Overview of the protocol

The ajp13 protocol is packet-oriented. A binary format was presumably chosen over the more readable plain text for reasons of performance. The web server communicates with the servlet container over TCP connections. To cut down on the expensive process of socket creation, the web server will attempt to maintain persistent TCP connections to the servlet container, and to reuse a connection for multiple request/response cycles.

Once a connection is assigned to a particular request, it will not be used for any others until the request-handling cycle has terminated. In other words, requests are not multiplexed over connections. This makes for much simpler code at either end of the connection, although it does cause more connections to be open at once.

Once the web server has opened a connection to the servlet container, the connection can be in one of the following states:

  • Idle 
    No request is being handled over this connection.
  • Assigned 
    The connecton is handling a specific request.

Once a connection is assigned to handle a particular request, the basic request informaton (e.g. HTTP headers, etc) is sent over the connection in a highly condensed form (e.g. common strings are encoded as integers). Details of that format are below in Request Packet Structure. If there is a body to the request (content-length > 0), that is sent in a separate packet immediately after.

At this point, the servlet container is presumably ready to start processing the request. As it does so, it can send the following messages back to the web server:

  • SEND_HEADERS 
    Send a set of headers back to the browser.
  • SEND_BODY_CHUNK 
    Send a chunk of body data back to the browser.
  • GET_BODY_CHUNK 
    Get further data from the request if it hasn't all been transferred yet. This is necessary because the packets have a fixed maximum size and arbitrary amounts of data can be included the body of a request (for uploaded files, for example). (Note: this is unrelated to HTTP chunked tranfer).
  • END_RESPONSE 
    Finish the request-handling cycle.

Each message is accompanied by a differently formatted packet of data. See Response Packet Structures below for details.

Basic Packet Structure

There is a bit of an XDR heritage to this protocol, but it differs in lots of ways (no 4 byte alignment, for example).

AJP13 uses network byte order for all data types.

There are four data types in the protocol: bytes, booleans, integers and strings.

Byte
A single byte.
Boolean
A single byte, 1 = true, 0 = false. Using other non-zero values as true (i.e. C-style) may work in some places, but it won't in others.
Integer
A number in the range of 0 to 2^16 (32768). Stored in 2 bytes with the high-order byte first.
String
A variable-sized string (length bounded by 2^16). Encoded with the length packed into two bytes first, followed by the string (including the terminating '\0'). Note that the encoded length does not include the trailing '\0' -- it is like strlen. This is a touch confusing on the Java side, which is littered with odd autoincrement statements to skip over these terminators. I believe the reason this was done was to allow the C code to be extra efficient when reading strings which the servlet container is sending back -- with the terminating \0 character, the C code can pass around references into a single buffer, without copying. If the \0 was missing, the C code would have to copy things out in order to get its notion of a string. Note a size of -1 (65535) indicates a null string and no data follow the length in this case.
Packet Size

According to much of the code, the max packet size is 8 * 1024 bytes (8K). The actual length of the packet is encoded in the header.

Packet Headers

Packets sent from the server to the container begin with 0x1234. Packets sent from the container to the server begin with AB (that's the ASCII code for A followed by the ASCII code for B). After those first two bytes, there is an integer (encoded as above) with the length of the payload. Although this might suggest that the maximum payload could be as large as 2^16, in fact, the code sets the maximum to be 8K.

Packet Format (Server->Container)Byte01234...(n+3)Contents0x120x34Data Length (n)DataPacket Format (Container->Server)Byte01234...(n+3)ContentsABData Length (n)Data

For most packets, the first byte of the payload encodes the type of message. The exception is for request body packets sent from the server to the container -- they are sent with a standard packet header (0x1234 and then length of the packet), but without any prefix code after that (this seems like a mistake to me).

The web server can send the following messages to the servlet container:

CodeType of PacketMeaning2Forward RequestBegin the request-processing cycle with the following data7ShutdownThe web server asks the container to shut itself down.8PingThe web server asks the container to take control (secure login phase).10CPingThe web server asks the container to respond quickly with a CPong.noneDataSize (2 bytes) and corresponding body data.

To ensure some basic security, the container will only actually do the Shutdown if the request comes from the same machine on which it's hosted.

The first Data packet is send immediatly after the Forward Request by the web server.

The servlet container can send the following types of messages to the web server:

CodeType of PacketMeaning3Send Body ChunkSend a chunk of the body from the servlet container to the web server (and presumably, onto the browser).4Send HeadersSend the response headers from the servlet container to the web server (and presumably, onto the browser).5End ResponseMarks the end of the response (and thus the request-handling cycle).6Get Body ChunkGet further data from the request if it hasn't all been transferred yet.9CPong ReplyThe reply to a CPing request

Each of the above messages has a different internal structure, detailed below.

Request Packet Structure

For messages from the server to the container of type "Forward Request":

AJP13_FORWARD_REQUEST :=    prefix_code      (byte) 0x02 = JK_AJP13_FORWARD_REQUEST    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)    attributes      *(attribut_name attribute_value)    request_terminator (byte) OxFF

The request_headers have the following structure:

req_header_name :=     sc_req_header_name | (string)  [see below for how this is parsed]sc_req_header_name := 0xA0xx (integer)req_header_value := (string)

The attributes are optional and have the following structure:

attribute_name := sc_a_name | (sc_a_req_attribute string)attribute_value := (string)

Not that the all-important header is "content-length', because it determines whether or not the container looks for another packet immediately.

Detailed description of the elements of Forward Request.

request_prefix

For all requests, this will be 2. See above for details on other prefix codes.

method

The HTTP method, encoded as a single byte:

Command NameCodeOPTIONS1GET2HEAD3POST4PUT5DELETE6TRACE7PROPFIND8PROPPATCH9MKCOL10COPY11MOVE12LOCK13UNLOCK14ACL15REPORT16VERSION-CONTROL17CHECKIN18CHECKOUT19UNCHECKOUT20SEARCH21MKWORKSPACE22UPDATE23LABEL24MERGE25BASELINE_CONTROL26MKACTIVITY27

Later version of ajp13, when used with mod_jk2, will transport additional methods, even if they are not in this list.

protocol, req_uri, remote_addr, remote_host, server_name, server_port, is_ssl

These are all fairly self-explanatory. Each of these is required, and will be sent for every request.

Headers

The structure of request_headers is the following: First, the number of headers num_headers is encoded. Then, a series of header name req_header_name / value req_header_value pairs follows. Common header names are encoded as integers, to save space. If the header name is not in the list of basic headers, it is encoded normally (as a string, with prefixed length). The list of common headers sc_req_header_nameand their codes is as follows (all are case-sensitive):

NameCode valueCode nameaccept0xA001SC_REQ_ACCEPTaccept-charset0xA002SC_REQ_ACCEPT_CHARSETaccept-encoding0xA003SC_REQ_ACCEPT_ENCODINGaccept-language0xA004SC_REQ_ACCEPT_LANGUAGEauthorization0xA005SC_REQ_AUTHORIZATIONconnection0xA006SC_REQ_CONNECTIONcontent-type0xA007SC_REQ_CONTENT_TYPEcontent-length0xA008SC_REQ_CONTENT_LENGTHcookie0xA009SC_REQ_COOKIEcookie20xA00ASC_REQ_COOKIE2host0xA00BSC_REQ_HOSTpragma0xA00CSC_REQ_PRAGMAreferer0xA00DSC_REQ_REFERERuser-agent0xA00ESC_REQ_USER_AGENT

The java code that reads this grabs the first two-byte integer and if it sees an '0xA0' in the most significant byte, it uses the integer in the second byte as an index into an array of header names. If the first byte is not '0xA0', it assumes that the two-byte integer is the length of a string, which is then read in.

This works on the assumption that no header names will have length greater than 0x9FFF (==0xA000 - 1), which is perfectly reasonable, though somewhat arbitrary. (If you, like me, started to think about the cookie spec here, and about how long headers can get, fear not -- this limit is on header names not header values. It seems unlikely that unmanageably huge header names will be showing up in the HTTP spec any time soon).

Note: The content-length header is extremely important. If it is present and non-zero, the container assumes that the request has a body (a POST request, for example), and immediately reads a separate packet off the input stream to get that body.

Attributes

The attributes prefixed with a ? (e.g. ?context) are all optional. For each, there is a single byte code to indicate the type of attribute, and then a string to give its value. They can be sent in any order (though the C code always sends them in the order listed below). A special terminating code is sent to signal the end of the list of optional attributes. The list of byte codes is:

InformationCode ValueNote?context0x01Not currently implemented?servlet_path0x02Not currently implemented?remote_user0x03 ?auth_type0x04 ?query_string0x05 ?route0x06 ?ssl_cert0x07 ?ssl_cipher0x08 ?ssl_session0x09 ?req_attribute0x0AName (the name of the attribut follows)?ssl_key_size0x0B ?secret0x0C ?stored_method0x0D are_done0xFFrequest_terminator

The context and servlet_path are not currently set by the C code, and most of the Java code completely ignores whatever is sent over for those fields (and some of it will actually break if a string is sent along after one of those codes). I don't know if this is a bug or an unimplemented feature or just vestigial code, but it's missing from both sides of the connection.

The remote_user and auth_type presumably refer to HTTP-level authentication, and communicate the remote user's username and the type of authentication used to establish their identity (e.g. Basic, Digest). I'm not clear on why the password isn't also sent, but I don't know HTTP authentication inside and out.

The query_stringssl_certssl_cipher, and ssl_session refer to the corresponding pieces of HTTP and HTTPS.

The route, as I understand it, is used to support sticky sessions -- associating a user's session with a particular Tomcat instance in the presence of multiple, load-balancing servers. I don't know the details.

Beyond this list of basic attributes, any number of other attributes can be sent via thereq_attribute code (0x0A). A pair of strings to represent the attribute name and value are sent immediately after each instance of that code. Environment values are passed in via this method.

Finally, after all the attributes have been sent, the attribute terminator, 0xFF, is sent. This signals both the end of the list of attributes and also then end of the Request Packet.

Response Packet Structure

For messages which the container can send back to the server.

AJP13_SEND_BODY_CHUNK :=   prefix_code   3  chunk_length  (integer)  chunk        *(byte)AJP13_SEND_HEADERS :=  prefix_code       4  http_status_code  (integer)  http_status_msg   (string)  num_headers       (integer)  response_headers *(res_header_name header_value)res_header_name :=     sc_res_header_name | (string)   [see below for how this is parsed]sc_res_header_name := 0xA0 (byte)header_value := (string)AJP13_END_RESPONSE :=  prefix_code       5  reuse             (boolean)AJP13_GET_BODY_CHUNK :=  prefix_code       6  requested_length  (integer)

Details:

Send Body Chunk

The chunk is basically binary data, and is sent directly back to the browser.

Send Headers

The status code and message are the usual HTTP things (e.g. "200" and "OK"). The response header names are encoded the same way the request header names are. See above for details about how the the codes are distinguished from the strings. The codes for common headers are:

NameCode valueContent-Type0xA001Content-Language0xA002Content-Length0xA003Date0xA004Last-Modified0xA005Location0xA006Set-Cookie0xA007Set-Cookie20xA008Servlet-Engine0xA009Status0xA00AWWW-Authenticate0xA00B

After the code or the string header name, the header value is immediately encoded.

End Response

Signals the end of this request-handling cycle. If the reuse flag is true (anything other than 0 in the actual C code), this TCP connection can now be used to handle new incoming requests. Ifreuse is false (==0), the connection should be closed.

Get Body Chunk

The container asks for more data from the request (If the body was too large to fit in the first packet sent over or when the request is chuncked). The server will send a body packet back with an amount of data which is the minimum of the request_length, the maximum send body size (8186 (8 Kbytes - 6)), and the number of bytes actually left to send from the request body. 
If there is no more data in the body (i.e. the servlet container is trying to read past the end of the body), the server will send back an "empty" packet, which is a body packet with a payload length of 0. (0x12,0x34,0x00,0x00)

Questions I Have

What happens if the request headers > max packet size? There is no provision to send a second packet of request headers in case there are more than 8K (I think this is correctly handled for response headers, though I'm not certain). I don't know if there is a way to get more than 8K worth of data into that initial set of request headers, but I'll bet there is (combine long cookies with long ssl information and a lot of environment variables, and you should hit 8K easily). I think the connector would just fail before trying to send any headers in this case, but I'm not certain.

What about authentication? There doesn't seem to be any authentication of the connection between the web server and the container. This strikes me as potentially dangerous.

原创粉丝点击