PageRenderTime 72ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/proxy/ajp.h

https://bitbucket.org/jonasteuwen/apache2nginx
C Header | 509 lines | 142 code | 60 blank | 307 comment | 0 complexity | 9bb7bb022ddc71dde08c449fd407368a MD5 | raw file
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file ajp.h
  18. * @brief Apache Jserv Protocol
  19. *
  20. * @defgroup AJP_defines AJP definitions
  21. * @ingroup MOD_PROXY
  22. * @{
  23. */
  24. #ifndef AJP_H
  25. #define AJP_H
  26. #include "apr_version.h"
  27. #include "apr.h"
  28. #include "apr_hooks.h"
  29. #include "apr_lib.h"
  30. #include "apr_strings.h"
  31. #include "apr_buckets.h"
  32. #include "apr_md5.h"
  33. #include "apr_network_io.h"
  34. #include "apr_poll.h"
  35. #include "apr_pools.h"
  36. #include "apr_strings.h"
  37. #include "apr_uri.h"
  38. #include "apr_date.h"
  39. #include "apr_fnmatch.h"
  40. #define APR_WANT_STRFUNC
  41. #include "apr_want.h"
  42. #if APR_HAVE_NETINET_IN_H
  43. #include <netinet/in.h>
  44. #endif
  45. #if APR_HAVE_ARPA_INET_H
  46. #include <arpa/inet.h>
  47. #endif
  48. #define AJP13_DEF_HOST "127.0.0.1"
  49. #ifdef NETWARE
  50. #define AJP13_DEF_PORT 9009 /* default to 9009 since 8009 is used by OS */
  51. #else
  52. #define AJP13_DEF_PORT 8009
  53. #endif
  54. /* The following environment variables match mod_ssl! */
  55. #define AJP13_HTTPS_INDICATOR "HTTPS"
  56. #define AJP13_SSL_CLIENT_CERT_INDICATOR "SSL_CLIENT_CERT"
  57. #define AJP13_SSL_CIPHER_INDICATOR "SSL_CIPHER"
  58. #define AJP13_SSL_SESSION_INDICATOR "SSL_SESSION_ID"
  59. #define AJP13_SSL_KEY_SIZE_INDICATOR "SSL_CIPHER_USEKEYSIZE"
  60. #if APR_CHARSET_EBCDIC
  61. #define USE_CHARSET_EBCDIC
  62. #define ajp_xlate_to_ascii(b, l) ap_xlate_proto_to_ascii(b, l)
  63. #define ajp_xlate_from_ascii(b, l) ap_xlate_proto_from_ascii(b, l)
  64. #else /* APR_CHARSET_EBCDIC */
  65. #define ajp_xlate_to_ascii(b, l)
  66. #define ajp_xlate_from_ascii(b, l)
  67. #endif
  68. #ifdef AJP_USE_HTTPD_WRAP
  69. #include "httpd_wrap.h"
  70. #else
  71. #include "httpd.h"
  72. #include "http_config.h"
  73. #include "http_request.h"
  74. #include "http_core.h"
  75. #include "http_protocol.h"
  76. #include "http_main.h"
  77. #include "http_log.h"
  78. #endif
  79. #include "mod_proxy.h"
  80. /** AJP Specific error codes
  81. */
  82. /** Buffer overflow exception */
  83. #define AJP_EOVERFLOW (APR_OS_START_USERERR + 1)
  84. /** Destination Buffer is to small */
  85. #define AJP_ETOSMALL (APR_OS_START_USERERR + 2)
  86. /** Invalid input parameters */
  87. #define AJP_EINVAL (APR_OS_START_USERERR + 3)
  88. /** Bad message signature */
  89. #define AJP_EBAD_SIGNATURE (APR_OS_START_USERERR + 4)
  90. /** Incoming message too bg */
  91. #define AJP_ETOBIG (APR_OS_START_USERERR + 5)
  92. /** Missing message header */
  93. #define AJP_ENO_HEADER (APR_OS_START_USERERR + 6)
  94. /** Bad message header */
  95. #define AJP_EBAD_HEADER (APR_OS_START_USERERR + 7)
  96. /** Bad message */
  97. #define AJP_EBAD_MESSAGE (APR_OS_START_USERERR + 8)
  98. /** Cant log via AJP14 */
  99. #define AJP_ELOGFAIL (APR_OS_START_USERERR + 9)
  100. /** Bad request method */
  101. #define AJP_EBAD_METHOD (APR_OS_START_USERERR + 10)
  102. /** A structure that represents ajp message */
  103. typedef struct ajp_msg ajp_msg_t;
  104. /** A structure that represents ajp message */
  105. struct ajp_msg
  106. {
  107. /** The buffer holding a AJP message */
  108. apr_byte_t *buf;
  109. /** The length of AJP message header (defaults to AJP_HEADER_LEN) */
  110. apr_size_t header_len;
  111. /** The length of AJP message */
  112. apr_size_t len;
  113. /** The current read position */
  114. apr_size_t pos;
  115. /** The size of the buffer */
  116. apr_size_t max_size;
  117. /** Flag indicating the origing of the message */
  118. int server_side;
  119. };
  120. /**
  121. * Signature for the messages sent from Apache to tomcat
  122. */
  123. #define AJP13_WS_HEADER 0x1234
  124. #define AJP_HEADER_LEN 4
  125. #define AJP_HEADER_SZ_LEN 2
  126. #define AJP_HEADER_SZ 6
  127. #define AJP_MSG_BUFFER_SZ 8192
  128. #define AJP_MAX_BUFFER_SZ 65536
  129. #define AJP13_MAX_SEND_BODY_SZ (AJP_MAX_BUFFER_SZ - AJP_HEADER_SZ)
  130. #define AJP_PING_PONG_SZ 128
  131. /** Send a request from web server to container*/
  132. #define CMD_AJP13_FORWARD_REQUEST (unsigned char)2
  133. /** Write a body chunk from the servlet container to the web server */
  134. #define CMD_AJP13_SEND_BODY_CHUNK (unsigned char)3
  135. /** Send response headers from the servlet container to the web server. */
  136. #define CMD_AJP13_SEND_HEADERS (unsigned char)4
  137. /** Marks the end of response. */
  138. #define CMD_AJP13_END_RESPONSE (unsigned char)5
  139. /** Get further data from the web server if it hasn't all been transferred yet. */
  140. #define CMD_AJP13_GET_BODY_CHUNK (unsigned char)6
  141. /** The web server asks the container to shut itself down. */
  142. #define CMD_AJP13_SHUTDOWN (unsigned char)7
  143. /** Webserver ask container to take control (logon phase) */
  144. #define CMD_AJP13_PING (unsigned char)8
  145. /** Container response to cping request */
  146. #define CMD_AJP13_CPONG (unsigned char)9
  147. /** Webserver check if container is alive, since container should respond by cpong */
  148. #define CMD_AJP13_CPING (unsigned char)10
  149. /** @} */
  150. /**
  151. * @defgroup AJP_api AJP API functions
  152. * @ingroup MOD_PROXY
  153. * @{
  154. */
  155. /**
  156. * Check a new AJP Message by looking at signature and return its size
  157. *
  158. * @param msg AJP Message to check
  159. * @param len Pointer to returned len
  160. * @return APR_SUCCESS or error
  161. */
  162. apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len);
  163. /**
  164. * Reset an AJP Message
  165. *
  166. * @param msg AJP Message to reset
  167. * @return APR_SUCCESS or error
  168. */
  169. apr_status_t ajp_msg_reset(ajp_msg_t *msg);
  170. /**
  171. * Reuse an AJP Message
  172. *
  173. * @param msg AJP Message to reuse
  174. * @return APR_SUCCESS or error
  175. */
  176. apr_status_t ajp_msg_reuse(ajp_msg_t *msg);
  177. /**
  178. * Mark the end of an AJP Message
  179. *
  180. * @param msg AJP Message to end
  181. * @return APR_SUCCESS or error
  182. */
  183. apr_status_t ajp_msg_end(ajp_msg_t *msg);
  184. /**
  185. * Add an unsigned 32bits value to AJP Message
  186. *
  187. * @param msg AJP Message to get value from
  188. * @param value value to add to AJP Message
  189. * @return APR_SUCCESS or error
  190. */
  191. apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value);
  192. /**
  193. * Add an unsigned 16bits value to AJP Message
  194. *
  195. * @param msg AJP Message to get value from
  196. * @param value value to add to AJP Message
  197. * @return APR_SUCCESS or error
  198. */
  199. apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value);
  200. /**
  201. * Add an unsigned 8bits value to AJP Message
  202. *
  203. * @param msg AJP Message to get value from
  204. * @param value value to add to AJP Message
  205. * @return APR_SUCCESS or error
  206. */
  207. apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value);
  208. /**
  209. * Add a String in AJP message, and transform the String in ASCII
  210. * if convert is set and we're on an EBCDIC machine
  211. *
  212. * @param msg AJP Message to get value from
  213. * @param value Pointer to String
  214. * @param convert When set told to convert String to ASCII
  215. * @return APR_SUCCESS or error
  216. */
  217. apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
  218. int convert);
  219. /**
  220. * Add a String in AJP message, and transform
  221. * the String in ASCII if we're on an EBCDIC machine
  222. */
  223. #define ajp_msg_append_string(m, v) ajp_msg_append_string_ex(m, v, 1)
  224. /**
  225. * Add a String in AJP message.
  226. */
  227. #define ajp_msg_append_string_ascii(m, v) ajp_msg_append_string_ex(m, v, 0)
  228. /**
  229. * Add a Byte array to AJP Message
  230. *
  231. * @param msg AJP Message to get value from
  232. * @param value Pointer to Byte array
  233. * @param valuelen Byte array len
  234. * @return APR_SUCCESS or error
  235. */
  236. apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
  237. apr_size_t valuelen);
  238. /**
  239. * Get a 32bits unsigned value from AJP Message
  240. *
  241. * @param msg AJP Message to get value from
  242. * @param rvalue Pointer where value will be returned
  243. * @return APR_SUCCESS or error
  244. */
  245. apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue);
  246. /**
  247. * Get a 16bits unsigned value from AJP Message
  248. *
  249. * @param msg AJP Message to get value from
  250. * @param rvalue Pointer where value will be returned
  251. * @return APR_SUCCESS or error
  252. */
  253. apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
  254. /**
  255. * Peek a 16bits unsigned value from AJP Message, position in message
  256. * is not updated
  257. *
  258. * @param msg AJP Message to get value from
  259. * @param rvalue Pointer where value will be returned
  260. * @return APR_SUCCESS or error
  261. */
  262. apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue);
  263. /**
  264. * Get a 8bits unsigned value from AJP Message
  265. *
  266. * @param msg AJP Message to get value from
  267. * @param rvalue Pointer where value will be returned
  268. * @return APR_SUCCESS or error
  269. */
  270. apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
  271. /**
  272. * Peek a 8bits unsigned value from AJP Message, position in message
  273. * is not updated
  274. *
  275. * @param msg AJP Message to get value from
  276. * @param rvalue Pointer where value will be returned
  277. * @return APR_SUCCESS or error
  278. */
  279. apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue);
  280. /**
  281. * Get a String value from AJP Message
  282. *
  283. * @param msg AJP Message to get value from
  284. * @param rvalue Pointer where value will be returned
  285. * @return APR_SUCCESS or error
  286. */
  287. apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue);
  288. /**
  289. * Get a Byte array from AJP Message
  290. *
  291. * @param msg AJP Message to get value from
  292. * @param rvalue Pointer where value will be returned
  293. * @param rvalueLen Pointer where Byte array len will be returned
  294. * @return APR_SUCCESS or error
  295. */
  296. apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
  297. apr_size_t *rvalue_len);
  298. /**
  299. * Create an AJP Message from pool
  300. *
  301. * @param pool memory pool to allocate AJP message from
  302. * @param size size of the buffer to create
  303. * @param rmsg Pointer to newly created AJP message
  304. * @return APR_SUCCESS or error
  305. */
  306. apr_status_t ajp_msg_create(apr_pool_t *pool, apr_size_t size, ajp_msg_t **rmsg);
  307. /**
  308. * Recopy an AJP Message to another
  309. *
  310. * @param smsg source AJP message
  311. * @param dmsg destination AJP message
  312. * @return APR_SUCCESS or error
  313. */
  314. apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg);
  315. /**
  316. * Serialize in an AJP Message a PING command
  317. *
  318. * +-----------------------+
  319. * | PING CMD (1 byte) |
  320. * +-----------------------+
  321. *
  322. * @param smsg AJP message to put serialized message
  323. * @return APR_SUCCESS or error
  324. */
  325. apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg);
  326. /**
  327. * Serialize in an AJP Message a CPING command
  328. *
  329. * +-----------------------+
  330. * | CPING CMD (1 byte) |
  331. * +-----------------------+
  332. *
  333. * @param smsg AJP message to put serialized message
  334. * @return APR_SUCCESS or error
  335. */
  336. apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg);
  337. /**
  338. * Dump up to the first 1024 bytes on an AJP Message
  339. *
  340. * @param pool pool to allocate from
  341. * @param msg AJP Message to dump
  342. * @param err error string to display
  343. * @return dump message
  344. */
  345. char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err);
  346. /**
  347. * Send an AJP message to backend
  348. *
  349. * @param soct backend socket
  350. * @param smsg AJP message to put serialized message
  351. * @return APR_SUCCESS or error
  352. */
  353. apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg);
  354. /**
  355. * Receive an AJP message from backend
  356. *
  357. * @param sock backend socket
  358. * @param smsg AJP message to put serialized message
  359. * @return APR_SUCCESS or error
  360. */
  361. apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg);
  362. /**
  363. * Build the ajp header message and send it
  364. * @param sock backend socket
  365. * @param r current request
  366. * @param buffsize max size of the AJP packet.
  367. * @uri uri requested uri
  368. * @return APR_SUCCESS or error
  369. */
  370. apr_status_t ajp_send_header(apr_socket_t *sock, request_rec *r,
  371. apr_size_t buffsize,
  372. apr_uri_t *uri);
  373. /**
  374. * Read the ajp message and return the type of the message.
  375. * @param sock backend socket
  376. * @param r current request
  377. * @param buffsize size of the buffer.
  378. * @param msg returned AJP message
  379. * @return APR_SUCCESS or error
  380. */
  381. apr_status_t ajp_read_header(apr_socket_t *sock,
  382. request_rec *r,
  383. apr_size_t buffsize,
  384. ajp_msg_t **msg);
  385. /**
  386. * Allocate a msg to send data
  387. * @param pool pool to allocate from
  388. * @param ptr data buffer
  389. * @param len the length of allocated data buffer
  390. * @param msg returned AJP message
  391. * @return APR_SUCCESS or error
  392. */
  393. apr_status_t ajp_alloc_data_msg(apr_pool_t *pool, char **ptr,
  394. apr_size_t *len, ajp_msg_t **msg);
  395. /**
  396. * Send the data message
  397. * @param sock backend socket
  398. * @param msg AJP message to send
  399. * @param len AJP message length
  400. * @return APR_SUCCESS or error
  401. */
  402. apr_status_t ajp_send_data_msg(apr_socket_t *sock,
  403. ajp_msg_t *msg, apr_size_t len);
  404. /**
  405. * Parse the message type
  406. * @param r current request
  407. * @param msg AJP message
  408. * @return AJP message type.
  409. */
  410. int ajp_parse_type(request_rec *r, ajp_msg_t *msg);
  411. /**
  412. * Parse the header message from container
  413. * @param r current request
  414. * @param msg AJP message
  415. * @return APR_SUCCESS or error
  416. */
  417. apr_status_t ajp_parse_header(request_rec *r, proxy_dir_conf *conf,
  418. ajp_msg_t *msg);
  419. /**
  420. * Parse the message body and return data address and length
  421. * @param r current request
  422. * @param msg AJP message
  423. * @param len returned AJP message length
  424. * @param ptr returned data
  425. * @return APR_SUCCESS or error
  426. */
  427. apr_status_t ajp_parse_data(request_rec *r, ajp_msg_t *msg,
  428. apr_uint16_t *len, char **ptr);
  429. /**
  430. * Check the reuse flag in CMD_AJP13_END_RESPONSE
  431. * @param r current request
  432. * @param msg AJP message
  433. * @param reuse returned reuse flag
  434. * @return APR_SUCCESS or error
  435. */
  436. apr_status_t ajp_parse_reuse(request_rec *r, ajp_msg_t *msg,
  437. apr_byte_t *reuse);
  438. /**
  439. * Handle the CPING/CPONG messages
  440. * @param sock backend socket
  441. * @param r current request
  442. * @param timeout time window for receiving cpong reply
  443. * @return APR_SUCCESS or error
  444. */
  445. apr_status_t ajp_handle_cping_cpong(apr_socket_t *sock,
  446. request_rec *r,
  447. apr_interval_time_t timeout);
  448. /** @} */
  449. #endif /* AJP_H */