]> granicus.if.org Git - apache/blobdiff - modules/proxy/ajp_msg.c
replace recent AJP direct comparisons to APR_TIMEUP with APR_STATUS_IS_TIMEUP.
[apache] / modules / proxy / ajp_msg.c
index 2b5b4b80393fbe9465bd51e3fa3e6bc27566e041..b56c2d3f3847ac7bcf3e5cde4e79bc89eab6b59b 100644 (file)
@@ -1,8 +1,9 @@
-/* Copyright 1999-2004 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
 
 #include "ajp.h"
 
+APLOG_USE_MODULE(proxy_ajp);
 
 static char *hex_table = "0123456789ABCDEF";
 
 /**
  * Dump up to the first 1024 bytes on an AJP Message
  *
+ * @param pool      pool to allocate from
  * @param msg       AJP Message to dump
  * @param err       error string to display
- * @return          APR_SUCCESS or error
+ * @return          dump message
  */
-apr_status_t ajp_msg_dump(ajp_msg_t *msg, char *err)
+char * ajp_msg_dump(apr_pool_t *pool, ajp_msg_t *msg, char *err)
 {
     apr_size_t  i, j;
     char        line[80];
     char        *current;
+    char        *rv, *p;
+    apr_size_t  bl = 8192;
     apr_byte_t  x;
     apr_size_t  len = msg->len;
 
     /* Display only first 1024 bytes */
     if (len > 1024)
         len = 1024;
-
-    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
-                 "ajp_msg_dump(): %s pos=%" APR_SIZE_T_FMT 
-                 " len=%" APR_SIZE_T_FMT " max=%d",
-                 err, msg->pos, msg->len, AJP_MSG_BUFFER_SZ);
-
+    rv = apr_palloc(pool, bl);
+    apr_snprintf(rv, bl,
+                 "ajp_msg_dump(): %s pos=%" APR_SIZE_T_FMT
+                 " len=%" APR_SIZE_T_FMT " max=%" APR_SIZE_T_FMT "\n",
+                 err, msg->pos, msg->len, msg->max_size);
+    bl -= strlen(rv);
+    p = rv + strlen(rv);
     for (i = 0; i < len; i += 16) {
         current = line;
 
@@ -67,13 +73,15 @@ apr_status_t ajp_msg_dump(ajp_msg_t *msg, char *err)
         }
 
         *current++ = '\0';
+        apr_snprintf(p, bl,
+                     "ajp_msg_dump(): %.4lx    %s\n",
+                     (unsigned long)i, line);
+        bl -= strlen(rv);
+        p = rv + strlen(rv);
 
-        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL,
-                      "ajp_msg_dump(): %.4lx    %s",
-                      i, line);
     }
-    
-    return APR_SUCCESS;
+
+    return rv;
 }
 
 
@@ -93,7 +101,7 @@ apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len)
           (head[0] == 0x12 && head[1] == 0x34))) {
 
         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
-                      "ajp_check_msg_header() got bad signature %x%x",
+                      "ajp_msg_check_header() got bad signature %02x%02x",
                       head[0], head[1]);
 
         return AJP_EBAD_SIGNATURE;
@@ -102,18 +110,18 @@ apr_status_t ajp_msg_check_header(ajp_msg_t *msg, apr_size_t *len)
     msglen  = ((head[2] & 0xff) << 8);
     msglen += (head[3] & 0xFF);
 
-    if (msglen > AJP_MSG_BUFFER_SZ) {
+    if (msglen > msg->max_size) {
         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
-                     "ajp_check_msg_header() incoming message is "
-                     "too big %" APR_SIZE_T_FMT ", max is %d",
-                     msglen, AJP_MSG_BUFFER_SZ);
+                     "ajp_msg_check_header() incoming message is "
+                     "too big %" APR_SIZE_T_FMT ", max is %" APR_SIZE_T_FMT,
+                     msglen, msg->max_size);
         return AJP_ETOBIG;
     }
 
     msg->len = msglen + AJP_HEADER_LEN;
     msg->pos = AJP_HEADER_LEN;
     *len     = msglen;
-    
+
     return APR_SUCCESS;
 }
 
@@ -127,7 +135,28 @@ apr_status_t ajp_msg_reset(ajp_msg_t *msg)
 {
     msg->len = AJP_HEADER_LEN;
     msg->pos = AJP_HEADER_LEN;
-    
+
+    return APR_SUCCESS;
+}
+
+/**
+ * Reuse an AJP Message
+ *
+ * @param msg       AJP Message to reuse
+ * @return          APR_SUCCESS or error
+ */
+apr_status_t ajp_msg_reuse(ajp_msg_t *msg)
+{
+    apr_byte_t *buf;
+    apr_size_t max_size;
+
+    buf = msg->buf;
+    max_size = msg->max_size;
+    memset(msg, 0, sizeof(ajp_msg_t));
+    msg->buf = buf;
+    msg->max_size = max_size;
+    msg->header_len = AJP_HEADER_LEN;
+    ajp_msg_reset(msg);
     return APR_SUCCESS;
 }
 
@@ -152,14 +181,14 @@ apr_status_t ajp_msg_end(ajp_msg_t *msg)
 
     msg->buf[2] = (apr_byte_t)((len >> 8) & 0xFF);
     msg->buf[3] = (apr_byte_t)(len & 0xFF);
-    
+
     return APR_SUCCESS;
 }
 
 static APR_INLINE int ajp_log_overflow(ajp_msg_t *msg, const char *context)
 {
     ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
-                 "%s(): BufferOverflowException %" APR_SIZE_T_FMT 
+                 "%s(): BufferOverflowException %" APR_SIZE_T_FMT
                  " %" APR_SIZE_T_FMT,
                  context, msg->pos, msg->len);
     return AJP_EOVERFLOW;
@@ -176,7 +205,7 @@ apr_status_t ajp_msg_append_uint32(ajp_msg_t *msg, apr_uint32_t value)
 {
     apr_size_t len = msg->len;
 
-    if ((len + 4) > AJP_MSG_BUFFER_SZ) {
+    if ((len + 4) > msg->max_size) {
         return ajp_log_overflow(msg, "ajp_msg_append_uint32");
     }
 
@@ -201,7 +230,7 @@ apr_status_t ajp_msg_append_uint16(ajp_msg_t *msg, apr_uint16_t value)
 {
     apr_size_t len = msg->len;
 
-    if ((len + 2) > AJP_MSG_BUFFER_SZ) {
+    if ((len + 2) > msg->max_size) {
         return ajp_log_overflow(msg, "ajp_msg_append_uint16");
     }
 
@@ -224,7 +253,7 @@ apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value)
 {
     apr_size_t len = msg->len;
 
-    if ((len + 1) > AJP_MSG_BUFFER_SZ) {
+    if ((len + 1) > msg->max_size) {
         return ajp_log_overflow(msg, "ajp_msg_append_uint8");
     }
 
@@ -235,8 +264,8 @@ apr_status_t ajp_msg_append_uint8(ajp_msg_t *msg, apr_byte_t value)
 }
 
 /**
- *  Add a String in AJP message, and transform the String in ASCII 
- *  if convert is set and we're on an EBCDIC machine    
+ *  Add a String in AJP message, and transform the String in ASCII
+ *  if convert is set and we're on an EBCDIC machine
  *
  * @param msg       AJP Message to get value from
  * @param value     Pointer to String
@@ -253,7 +282,7 @@ apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
     }
 
     len = strlen(value);
-    if ((msg->len + len + 2) > AJP_MSG_BUFFER_SZ) {
+    if ((msg->len + len + 3) > msg->max_size) {
         return ajp_log_overflow(msg, "ajp_msg_append_cvt_string");
     }
 
@@ -263,8 +292,10 @@ apr_status_t ajp_msg_append_string_ex(ajp_msg_t *msg, const char *value,
     /* We checked for space !!  */
     memcpy(msg->buf + msg->len, value, len + 1); /* including \0 */
 
-    if (convert)   /* convert from EBCDIC if needed */
-        ajp_xlate_to_ascii((char *)msg->buf + msg->len, len + 1);
+    if (convert) {
+        /* convert from EBCDIC if needed */
+        ap_xlate_proto_to_ascii((char *)msg->buf + msg->len, len + 1);
+    }
 
     msg->len += len + 1;
 
@@ -286,7 +317,7 @@ apr_status_t ajp_msg_append_bytes(ajp_msg_t *msg, const apr_byte_t *value,
         return APR_SUCCESS; /* Shouldn't we indicate an error ? */
     }
 
-    if ((msg->len + valuelen) > AJP_MSG_BUFFER_SZ) {
+    if ((msg->len + valuelen) > msg->max_size) {
         return ajp_log_overflow(msg, "ajp_msg_append_bytes");
     }
 
@@ -316,7 +347,7 @@ apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue)
     value |= ((msg->buf[(msg->pos++)] & 0xFF) << 16);
     value |= ((msg->buf[(msg->pos++)] & 0xFF) << 8);
     value |= ((msg->buf[(msg->pos++)] & 0xFF));
-    
+
     *rvalue = value;
     return APR_SUCCESS;
 }
@@ -332,7 +363,7 @@ apr_status_t ajp_msg_get_uint32(ajp_msg_t *msg, apr_uint32_t *rvalue)
 apr_status_t ajp_msg_get_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
 {
     apr_uint16_t value;
-    
+
     if ((msg->pos + 1) > msg->len) {
         return ajp_log_overflow(msg, "ajp_msg_get_uint16");
     }
@@ -359,10 +390,10 @@ apr_status_t ajp_msg_peek_uint16(ajp_msg_t *msg, apr_uint16_t *rvalue)
     if ((msg->pos + 1) > msg->len) {
         return ajp_log_overflow(msg, "ajp_msg_peek_uint16");
     }
-    
+
     value = ((msg->buf[(msg->pos)] & 0xFF) << 8);
     value += ((msg->buf[(msg->pos + 1)] & 0xFF));
-    
+
     *rvalue = value;
     return APR_SUCCESS;
 }
@@ -380,7 +411,7 @@ apr_status_t ajp_msg_peek_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
     if (msg->pos > msg->len) {
         return ajp_log_overflow(msg, "ajp_msg_peek_uint8");
     }
-    
+
     *rvalue = msg->buf[msg->pos];
     return APR_SUCCESS;
 }
@@ -398,7 +429,7 @@ apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
     if (msg->pos > msg->len) {
         return ajp_log_overflow(msg, "ajp_msg_get_uint8");
     }
-    
+
     *rvalue = msg->buf[msg->pos++];
     return APR_SUCCESS;
 }
@@ -411,23 +442,23 @@ apr_status_t ajp_msg_get_uint8(ajp_msg_t *msg, apr_byte_t *rvalue)
  * @param rvalue    Pointer where value will be returned
  * @return          APR_SUCCESS or error
  */
-apr_status_t ajp_msg_get_string(ajp_msg_t *msg, char **rvalue)
+apr_status_t ajp_msg_get_string(ajp_msg_t *msg, const char **rvalue)
 {
     apr_uint16_t size;
     apr_size_t   start;
     apr_status_t status;
-       
+
     status = ajp_msg_get_uint16(msg, &size);
     start = msg->pos;
 
-    if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) {
+    if ((status != APR_SUCCESS) || (size + start > msg->max_size)) {
         return ajp_log_overflow(msg, "ajp_msg_get_string");
     }
 
     msg->pos += (apr_size_t)size;
     msg->pos++;                   /* a String in AJP is NULL terminated */
 
-    *rvalue = (char *)(msg->buf + start);
+    *rvalue = (const char *)(msg->buf + start);
     return APR_SUCCESS;
 }
 
@@ -451,7 +482,7 @@ apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
     /* save the current position */
     start = msg->pos;
 
-    if ((status != APR_SUCCESS) || (size + start > AJP_MSG_BUFFER_SZ)) {
+    if ((status != APR_SUCCESS) || (size + start > msg->max_size)) {
         return ajp_log_overflow(msg, "ajp_msg_get_bytes");
     }
     msg->pos += (apr_size_t)size;   /* only bytes, no trailer */
@@ -467,39 +498,22 @@ apr_status_t ajp_msg_get_bytes(ajp_msg_t *msg, apr_byte_t **rvalue,
  * Create an AJP Message from pool
  *
  * @param pool      memory pool to allocate AJP message from
+ * @param size      size of the buffer to create
  * @param rmsg      Pointer to newly created AJP message
  * @return          APR_SUCCESS or error
  */
-apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg)
+apr_status_t ajp_msg_create(apr_pool_t *pool, apr_size_t size, ajp_msg_t **rmsg)
 {
     ajp_msg_t *msg = (ajp_msg_t *)apr_pcalloc(pool, sizeof(ajp_msg_t));
 
-    if (!msg) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
-                      "ajp_msg_create(): can't allocate AJP message memory");
-        return APR_ENOPOOL;
-    }
-    
     msg->server_side = 0;
 
-    msg->buf = (apr_byte_t *)apr_palloc(pool, AJP_MSG_BUFFER_SZ);
-    
-    /* XXX: This should never happen
-     * In case if the OS cannont allocate 8K of data
-     * we are in serious trouble
-     * No need to check the alloc return value, cause the
-     * core dump is probably the best solution anyhow.
-     */
-    if (msg->buf == NULL) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
-                      "ajp_msg_create(): can't allocate AJP message memory");
-        return APR_ENOPOOL;
-    }
-
+    msg->buf = (apr_byte_t *)apr_palloc(pool, size);
     msg->len = 0;
     msg->header_len = AJP_HEADER_LEN;
+    msg->max_size = size;
     *rmsg = msg;
-    
+
     return APR_SUCCESS;
 }
 
@@ -512,17 +526,11 @@ apr_status_t ajp_msg_create(apr_pool_t *pool, ajp_msg_t **rmsg)
  */
 apr_status_t ajp_msg_copy(ajp_msg_t *smsg, ajp_msg_t *dmsg)
 {
-    if (dmsg == NULL) {
-        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
-                     "ajp_msg_copy(): destination msg is null");
-        return AJP_EINVAL;
-    }
-    
-    if (smsg->len > AJP_MSG_BUFFER_SZ) {
+    if (smsg->len > smsg->max_size) {
         ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL,
                      "ajp_msg_copy(): destination buffer too "
-                     "small %" APR_SIZE_T_FMT ", max size is %d",
-                     smsg->len, AJP_MSG_BUFFER_SZ);
+                     "small %" APR_SIZE_T_FMT ", max size is %" APR_SIZE_T_FMT,
+                     smsg->len, smsg->max_size);
         return  AJP_ETOSMALL;
     }
 
@@ -551,11 +559,11 @@ apr_status_t ajp_msg_serialize_ping(ajp_msg_t *msg)
 
     if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_PING)) != APR_SUCCESS)
         return rc;
-        
+
     return APR_SUCCESS;
 }
 
-/** 
+/**
  * Serialize in an AJP Message a CPING command
  *
  * +-----------------------+
@@ -572,6 +580,6 @@ apr_status_t ajp_msg_serialize_cping(ajp_msg_t *msg)
 
     if ((rc = ajp_msg_append_uint8(msg, CMD_AJP13_CPING)) != APR_SUCCESS)
         return rc;
-        
+
     return APR_SUCCESS;
 }