예제 #1
0
static int
handle_cache_events(INKCont contp, INKEvent event, void *edata)
{
  CACHE_URL_DATA *url_data;
  INKVConn connp = (INKVConn) edata;
  char tempstr[32];

  LOG_SET_FUNCTION_NAME("handle_cache_events");

  url_data = (CACHE_URL_DATA *) INKContDataGet(contp);
  if (url_data == INK_ERROR_PTR) {
    LOG_ERROR_AND_RETURN("INKContDataGet");
  }

  if (event != INK_EVENT_HTTP_TXN_CLOSE) {
    INKReleaseAssert(url_data->magic == MAGIC_ALIVE);
  } else {
    INKReleaseAssert((url_data == NULL) || (url_data->magic == MAGIC_ALIVE));
  }

  switch (event) {
  case INK_EVENT_CACHE_OPEN_READ:
    /*handle_cache_read(); */
    INKDebug(DEBUG_TAG, "INK_EVENT_CACHE_OPEN_READ\n");

    if (url_data->pin_time != 0) {
      sprintf(tempstr, "PIN%d", url_data->pin_time);
    } else {
      sprintf(tempstr, "HIT");
    }
    insert_in_response(url_data->txnp, tempstr);

    if (url_data->pin_time != 0) {

      url_data->write_again_after_remove = 1;

      if (INKCacheRemove(contp, url_data->key) == INK_ERROR_PTR) {
        LOG_ERROR("INKCacheRemove");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }
#ifdef DEBUG
      if (INKCacheRemove(NULL, url_data->key) != INK_ERROR_PTR) {
        LOG_ERROR_NEG("INKCacheRemove(NULL, cache_key)");
      }

      if (INKCacheRemove(contp, NULL) != INK_ERROR_PTR) {
        LOG_ERROR_NEG("INKCacheRemove(contp, NULL)");
      }
#endif
      return 0;
    }
#ifdef DEBUG
    if (INKVConnRead(NULL, contp, url_data->bufp, url_data->url_len) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnRead(NULL, contp, bufp, url_len)");
    }

    if (INKVConnRead(connp, NULL, url_data->bufp, url_data->url_len) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnRead(connp, NULL, bufp, url_len)");
    }

    if (INKVConnRead(connp, contp, NULL, url_data->url_len) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnRead(connp, contp, NULL, url_len)");
    }

    if (INKVConnRead(connp, contp, url_data->bufp, -1) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnRead(connp, contp, bufp, -1)");
    }
#endif

    if (INKVConnRead(connp, contp, url_data->bufp, url_data->url_len) == INK_ERROR_PTR) {
      LOG_ERROR("INKVConnRead");
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
      return -1;
    }

    break;

  case INK_EVENT_CACHE_OPEN_READ_FAILED:
    /*handle_cache_read_fail(); */
    INKDebug(DEBUG_TAG, "INK_EVENT_CACHE_OPEN_READ_FAILED(%d)\n", edata);

    if (url_data->pin_time != 0) {
      sprintf(tempstr, "PIN%d", url_data->pin_time);
    } else {
      sprintf(tempstr, "MISS");
    }
    insert_in_response(url_data->txnp, tempstr);

    if (url_data->pin_time != 0) {
      INKDebug(DEBUG_TAG, "url Pinned in cache for %d secs", url_data->pin_time);
      if (INKCacheKeyPinnedSet(url_data->key, url_data->pin_time) == INK_ERROR) {
        LOG_ERROR("INKCacheKeyPinnedSet");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }
#ifdef DEBUG
      if (INKCacheKeyPinnedSet(NULL, url_data->pin_time) != INK_ERROR) {
        LOG_ERROR_NEG("INKCacheKeyPinnedSet(NULL, pin_time)");
      }

      if (INKCacheKeyPinnedSet(url_data->key, -1) != INK_ERROR) {
        LOG_ERROR_NEG("INKCacheKeyPinnedSet(cache_key, -1)");
      }
#endif
    }

    if (INKCacheWrite(contp, url_data->key) == INK_ERROR_PTR) {
      LOG_ERROR("INKCacheWrite");
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
      return -1;
    }
#ifdef DEBUG
    if (INKCacheWrite(contp, NULL) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKCacheWrite(contp, NULL)");
    }

    if (INKCacheWrite(NULL, url_data->key) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKCacheWrite(NULL, url_data->key)");
    }
#endif

    break;

  case INK_EVENT_CACHE_OPEN_WRITE:
    /*handle_cache_write(); */
    INKDebug(DEBUG_TAG, "INK_EVENT_CACHE_OPEN_WRITE\n");

    if (INKIOBufferWrite(url_data->bufp, url_data->url, url_data->url_len) == INK_ERROR) {
      LOG_ERROR("INKIOBufferWrite");
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
      return -1;
    }

    url_data->bufp_reader = INKIOBufferReaderAlloc(url_data->bufp);
    if (url_data->bufp_reader == INK_ERROR_PTR) {
      LOG_ERROR("INKIOBufferReaderAlloc");
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
      return -1;
    }
#ifdef DEBUG
    if (INKVConnWrite(NULL, contp, url_data->bufp_reader, url_data->url_len) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnWrite(NULL, contp, bufp_reader, url_len");
    }

    if (INKVConnWrite(connp, NULL, url_data->bufp_reader, url_data->url_len) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnWrite(connp, NULL, bufp_reader, url_len");
    }

    if (INKVConnWrite(connp, contp, NULL, url_data->url_len) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnWrite(connp, contp, NULL, url_len");
    }

    if (INKVConnWrite(connp, contp, url_data->bufp_reader, -1) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVConnWrite(connp, contp, bufp_reader, -1");
    }
#endif

    if (INKVConnWrite(connp, contp, url_data->bufp_reader, url_data->url_len) == INK_ERROR_PTR) {
      LOG_ERROR("INKVConnWrite");
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
      return -1;
    }

    break;

  case INK_EVENT_CACHE_OPEN_WRITE_FAILED:
    /*handle_cache_write_fail(); */
    INKDebug(DEBUG_TAG, "INK_EVENT_CACHE_OPEN_WRITE_FAILED(%d)\n", edata);
    INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);

    break;

  case INK_EVENT_CACHE_REMOVE:
    /*handle_cache_remove(); */
    INKDebug(DEBUG_TAG, "INK_EVENT_CACHE_REMOVE\n");

    if (url_data->write_again_after_remove != 0) {

      INKDebug(DEBUG_TAG, "url Pinned in cache for %d secs", url_data->pin_time);
      if (url_data->pin_time != 0) {
        if (INKCacheKeyPinnedSet(url_data->key, url_data->pin_time) == INK_ERROR) {
          LOG_ERROR("INKCacheKeyPinnedSet");
          INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
          return -1;
        }
      }

      if (INKCacheWrite(contp, url_data->key) == INK_ERROR_PTR) {
        LOG_ERROR("INKCacheWrite");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }
    } else {
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
    }

    break;

  case INK_EVENT_CACHE_REMOVE_FAILED:
    /*handle_cache_remove_fail(); */
    INKDebug(DEBUG_TAG, "INK_EVENT_CACHE_REMOVE_FAILED(%d)\n", edata);
    INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
    break;

  case INK_EVENT_VCONN_READ_READY:
    INKDebug(DEBUG_TAG, "INK_EVENT_VCONN_READ_READY\n");

    if (INKVIOReenable(edata) == INK_ERROR) {
      LOG_ERROR("INKVIOReenable");
    }
#ifdef DEBUG
    if (INKVIOReenable(NULL) != INK_ERROR) {
      LOG_ERROR_NEG("INKVIOReenable");
    }
#endif

    break;

  case INK_EVENT_VCONN_WRITE_READY:
    INKDebug(DEBUG_TAG, "INK_EVENT_VCONN_WRITE_READY\n");

    if (INKVIOReenable(edata) == INK_ERROR) {
      LOG_ERROR("INKVIOReenable");
    }
#ifdef DEBUG
    if (INKVIOReenable(NULL) != INK_ERROR) {
      LOG_ERROR_NEG("INKVIOReenable");
    }
#endif

    break;

  case INK_EVENT_VCONN_READ_COMPLETE:
    INKDebug(DEBUG_TAG, "INK_EVENT_VCONN_READ_COMPLETE\n");
    {
      INKIOBufferBlock blk;
      char *src;
      char dst[MAX_URL_LEN];
      int avail;
      int url_len_from_cache;

      if ((connp = INKVIOVConnGet(edata)) == INK_ERROR_PTR) {
        LOG_ERROR("INKVIOVConnGet");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }
#ifdef DEBUG
      if (INKVConnCacheObjectSizeGet(NULL, &url_len_from_cache) != INK_ERROR) {
        LOG_ERROR_NEG("INKVConnCacheObjectSizeGet(NULL, &size)");
      }

      if (INKVConnCacheObjectSizeGet(connp, NULL) != INK_ERROR) {
        LOG_ERROR_NEG("INKVConnCacheObjectSizeGet(inkvconn, NULL)");
      }
#endif

      if (INKVConnCacheObjectSizeGet(connp, &url_len_from_cache) == INK_ERROR) {
        LOG_ERROR("INKVConnCacheObjectSizeGet");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }

      if (url_len_from_cache != url_data->url_len) {
        LOG_ERROR("INKVConnCacheObjectSizeGet-mismatch");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }

      if ((connp = INKVIOVConnGet(edata)) == INK_ERROR_PTR) {
        LOG_ERROR("INKVIOVConnGet");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }
#ifdef DEBUG
      if (INKVIOVConnGet(NULL) != INK_ERROR_PTR) {
        LOG_ERROR_NEG("INKVIOVConnGet(null)");
      }

      if (INKVConnClose(NULL) != INK_ERROR) {
        LOG_ERROR_NEG("INKVConnClose(NULL)");
      }
#endif

      if (INKVConnClose(connp) == INK_ERROR) {
        LOG_ERROR("INKVConnClose");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }

      url_data = (CACHE_URL_DATA *) INKContDataGet(contp);
      INKReleaseAssert(url_data->magic == MAGIC_ALIVE);

      url_data->bufp_reader = INKIOBufferReaderAlloc(url_data->bufp);
      blk = INKIOBufferReaderStart(url_data->bufp_reader);
      if (blk == INK_ERROR_PTR) {
        LOG_ERROR("INKIOBufferReaderStart");
        INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
        return -1;
      }

      src = (char *) INKIOBufferBlockReadStart(blk, url_data->bufp_reader, &avail);
      /* FC: make sure we do not copy more than MAX_URL_LEN-1 bytes into dst */
      if (avail < 0) {
        avail = 0;
      }
      avail = (avail < MAX_URL_LEN - 1) ? avail : (MAX_URL_LEN - 1);
      strncpy(dst, src, avail);
      dst[avail] = '\0';

      if (strcmp(dst, url_data->url) != 0) {
        INKDebug(DEBUG_TAG, "URL in cache NO_MATCH\ndst=[%s]\nurl=[%s]\n", dst, url_data->url);
      }
    }
    INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);

    break;

  case INK_EVENT_VCONN_WRITE_COMPLETE:
    INKDebug(DEBUG_TAG, "INK_EVENT_VCONN_WRITE_COMPLETE\n");

    if ((connp = INKVIOVConnGet(edata)) == INK_ERROR_PTR) {
      LOG_ERROR("INKVIOVConnGet");
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
      return -1;
    }
#ifdef DEBUG
    if (INKVIOVConnGet(NULL) != INK_ERROR_PTR) {
      LOG_ERROR_NEG("INKVIOVConnGet(null)");
    }

    if (INKVConnClose(NULL) != INK_ERROR) {
      LOG_ERROR_NEG("INKVConnClose(NULL)");
    }
#endif

    if (INKVConnClose(connp) == INK_ERROR) {
      LOG_ERROR("INKVConnClose");
      INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
      return -1;
    }

    INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
    break;

  case INK_EVENT_VCONN_EOS:
    INKDebug(DEBUG_TAG, "INK_EVENT_VCONN_EOS\n");
    break;

  case INK_EVENT_ERROR:
    INKDebug(DEBUG_TAG, "INK_EVENT_ERROR\n");
    INKHttpTxnReenable(url_data->txnp, INK_EVENT_HTTP_CONTINUE);
    break;

  case INK_EVENT_HTTP_TXN_CLOSE:
    INKDebug(DEBUG_TAG, "INK_EVENT_HTTP_TXN_CLOSE\n");

    if (url_data == NULL) {
      INKHttpTxnReenable(edata, INK_EVENT_HTTP_CONTINUE);
      break;
    }

    if (url_data->url != NULL) {
      INKfree(url_data->url);
    }

    if (INKCacheKeyDestroy(url_data->key) == INK_ERROR) {
      LOG_ERROR("INKCacheKeyDestroy");
    }
#ifdef DEBUG
    if (INKCacheKeyDestroy(NULL) != INK_ERROR) {
      LOG_ERROR_NEG("INKCacheKeyDestroy(NULL)");
    }

    if (INKIOBufferDestroy(NULL) != INK_ERROR) {
      LOG_ERROR_NEG("INKIOBufferDestroy(NULL)");
    }
#endif

    if (INKIOBufferDestroy(url_data->bufp) == INK_ERROR) {
      LOG_ERROR("INKIOBufferDestroy");
    }

    url_data->magic = MAGIC_DEAD;
    INKfree(url_data);
    INKContDestroy(contp);
    INKHttpTxnReenable(edata, INK_EVENT_HTTP_CONTINUE);

    break;

  default:
    ;
  }
  return 0;
}
예제 #2
0
int
get_client_req(INKHttpTxn txnp, char **p_url_line, char **p_client_req, int *neg_test_val, int *pin_val)
{
  INKMBuffer bufp;
  INKReturnCode ret_code;
  INKMLoc hdr_loc;
  INKMLoc url_loc;
  INKMLoc neg_loc;
  INKMLoc pin_loc;

  INKIOBuffer output_buffer;
  INKIOBufferReader reader;
  int total_avail;

  INKIOBufferBlock block;
  const char *block_start;
  int block_avail;

  char *output_string;
  char *url_str;
  int url_len;
  int output_len;

  LOG_SET_FUNCTION_NAME("get_client_req");

  *p_url_line = NULL;
  *p_client_req = NULL;
  *neg_test_val = 0;
  *pin_val = 0;

#ifdef DEBUG
  if (INKHttpTxnClientReqGet(NULL, &bufp, &hdr_loc) != 0) {
    LOG_ERROR_NEG("INKHttpTxnClientReqGet(null, buf, hdr_loc)");
  }

  if (INKHttpTxnClientReqGet(txnp, NULL, &hdr_loc) != 0) {
    LOG_ERROR_NEG("INKHttpTxnClientReqGet(txnp, null, hdr_loc)");
  }

  if (INKHttpTxnClientReqGet(txnp, &bufp, NULL) != 0) {
    LOG_ERROR_NEG("INKHttpTxnClientReqGet(txnp, buf, null)");
  }
#endif

  if (!INKHttpTxnClientReqGet(txnp, &bufp, &hdr_loc)) {
    LOG_ERROR_AND_RETURN("INKHttpTxnClientReqGet");
  }

  if ((url_loc = INKHttpHdrUrlGet(bufp, hdr_loc)) == INK_ERROR_PTR) {
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    LOG_ERROR_AND_RETURN("INKHttpHdrUrlGet");
  }
#ifdef DEBUG
  if (INKUrlStringGet(NULL, url_loc, &url_len) != INK_ERROR_PTR) {
    LOG_ERROR_NEG("INKUrlStringGet(NULL, url_loc, &int)");
  }

  if (INKUrlStringGet(bufp, NULL, &url_len) != INK_ERROR_PTR) {
    LOG_ERROR_NEG("INKUrlStringGet(bufp, NULL, &int)");
  }
#endif

  if ((url_str = INKUrlStringGet(bufp, url_loc, &url_len)) == INK_ERROR_PTR) {
    INKHandleMLocRelease(bufp, hdr_loc, url_loc);
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    LOG_ERROR_AND_RETURN("INKUrlStringGet");
  }

  if (INKHandleMLocRelease(bufp, hdr_loc, url_loc) == INK_ERROR) {
    INKfree(url_str);
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    LOG_ERROR_AND_RETURN("INKHandleMLocRelease");
  }

  url_str[url_len] = '\0';
  *p_url_line = url_str;

  if ((output_buffer = INKIOBufferCreate()) == INK_ERROR_PTR) {
    INKfree(url_str);
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    LOG_ERROR_AND_RETURN("INKIOBufferCreate");
  }
#ifdef DEBUG
  if (INKIOBufferReaderAlloc(NULL) != INK_ERROR_PTR) {
    LOG_ERROR_NEG("INKIOBufferReaderAlloc(NULL)");
  }
#endif

  if ((reader = INKIOBufferReaderAlloc(output_buffer)) == INK_ERROR_PTR) {
    INKfree(url_str);
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    INKIOBufferDestroy(output_buffer);
    LOG_ERROR_AND_RETURN("INKIOBufferReaderAlloc");
  }

  /* This will print  just MIMEFields and not
     the http request line */
  if (INKMimeHdrPrint(bufp, hdr_loc, output_buffer) == INK_ERROR) {
    INKfree(url_str);
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    INKIOBufferDestroy(output_buffer);
    LOG_ERROR_AND_RETURN("INKMimeHdrPrint");
  }

  if ((neg_loc = INKMimeHdrFieldFind(bufp, hdr_loc, "CacheTester-HostNameSet", -1)) == INK_ERROR_PTR) {
    INKfree(url_str);
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    INKIOBufferDestroy(output_buffer);
    LOG_ERROR_AND_RETURN("INKMimeHdrFieldFind");
  }

  if (neg_loc != NULL) {

    ret_code = INKMimeHdrFieldValueIntGet(bufp, hdr_loc, neg_loc, 0, neg_test_val);
    if (ret_code == INK_ERROR) {
      INKfree(url_str);
      INKHandleMLocRelease(bufp, hdr_loc, neg_loc);
      INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
      INKIOBufferDestroy(output_buffer);
      LOG_ERROR_AND_RETURN("INKMimeHdrFieldValueIntGet");
    }

    if (INKHandleMLocRelease(bufp, hdr_loc, neg_loc) == INK_ERROR) {
      INKfree(url_str);
      INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
      INKIOBufferDestroy(output_buffer);
      LOG_ERROR_AND_RETURN("INKHandleMLocRelease");
    }
  }

  if ((pin_loc = INKMimeHdrFieldFind(bufp, hdr_loc, "CacheTester-Pin", -1)) == INK_ERROR_PTR) {
    INKfree(url_str);
    INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
    INKIOBufferDestroy(output_buffer);
    LOG_ERROR_AND_RETURN("INKMimeHdrFieldFind");
  }

  if (pin_loc != NULL) {

    ret_code = INKMimeHdrFieldValueIntGet(bufp, hdr_loc, pin_loc, 0, pin_val);
    if (ret_code == INK_ERROR) {
      INKfree(url_str);
      INKHandleMLocRelease(bufp, hdr_loc, pin_loc);
      INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
      INKIOBufferDestroy(output_buffer);
      LOG_ERROR_AND_RETURN("INKMimeHdrFieldValueIntGet");
    }

    if (INKHandleMLocRelease(bufp, hdr_loc, pin_loc) == INK_ERROR) {
      INKfree(url_str);
      INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc);
      INKIOBufferDestroy(output_buffer);
      LOG_ERROR_AND_RETURN("INKHandleMLocRelease");
    }
  }

  if (INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc) == INK_ERROR) {
    INKfree(url_str);
    INKIOBufferDestroy(output_buffer);
    LOG_ERROR_AND_RETURN("INKHandleMLocRelease");
  }

  /* Find out how the big the complete header is by
     seeing the total bytes in the buffer.  We need to
     look at the buffer rather than the first block to
     see the size of the entire header */
  if ((total_avail = INKIOBufferReaderAvail(reader)) == INK_ERROR) {
    INKfree(url_str);
    INKIOBufferDestroy(output_buffer);
    LOG_ERROR_AND_RETURN("INKIOBufferReaderAvail");
  }
#ifdef DEBUG
  if (INKIOBufferReaderAvail(NULL) != INK_ERROR) {
    LOG_ERROR_NEG("INKIOBufferReaderAvail(NULL)");
  }
#endif

  /* Allocate the string with an extra byte for the string
     terminator */
  output_string = (char *) INKmalloc(total_avail + 1);
  output_len = 0;

  /* We need to loop over all the buffer blocks to make
     sure we get the complete header since the header can
     be in multiple blocks */
  block = INKIOBufferReaderStart(reader);

  if (block == INK_ERROR_PTR) {
    INKfree(url_str);
    INKfree(output_string);
    LOG_ERROR_AND_RETURN("INKIOBufferReaderStart");
  }
#ifdef DEBUG
  if (INKIOBufferReaderStart(NULL) != INK_ERROR_PTR) {
    LOG_ERROR_NEG("INKIOBufferReaderStart(NULL)");
  }

  if (INKIOBufferBlockReadStart(NULL, reader, &block_avail) != INK_ERROR_PTR) {
    LOG_ERROR_NEG("INKIOBufferBlockReadStart(null, reader, &int)");
  }

  if (INKIOBufferBlockReadStart(block, NULL, &block_avail) != INK_ERROR_PTR) {
    LOG_ERROR_NEG("INKIOBufferBlockReadStart(block, null, &int)");
  }
#endif

  while (block) {

    block_start = INKIOBufferBlockReadStart(block, reader, &block_avail);

    if (block_start == INK_ERROR_PTR) {
      INKfree(url_str);
      INKfree(output_string);
      LOG_ERROR_AND_RETURN("INKIOBufferBlockReadStart");
    }

    /* FC paranoia: make sure we don't copy more bytes than buffer size can handle */
    if ((output_len + block_avail) > total_avail) {
      INKfree(url_str);
      INKfree(output_string);
      LOG_ERROR_AND_RETURN("More bytes than expected in IOBuffer");
    }

    /* We'll get a block pointer back even if there is no data
       left to read so check for this condition and break out of
       the loop. A block with no data to read means we've exhausted
       buffer of data since if there was more data on a later 
       block in the chain, this block would have been skipped over */
    if (block_avail == 0) {
      break;
    }

    memcpy(output_string + output_len, block_start, block_avail);
    output_len += block_avail;

    /* Consume the data so that we get to the next block */
    if (INKIOBufferReaderConsume(reader, block_avail) == INK_ERROR) {
      INKfree(url_str);
      INKfree(output_string);
      LOG_ERROR_AND_RETURN("INKIOBufferReaderConsume");
    }
#ifdef DEBUG
    if (INKIOBufferReaderConsume(NULL, block_avail) != INK_ERROR) {
      LOG_ERROR_NEG("INKIOBufferReaderConsume(null, int)");
    }

    if (INKIOBufferReaderConsume(reader, -1) != INK_ERROR) {
      LOG_ERROR_NEG("INKIOBufferReaderConsume(reader, -1)");
    }
#endif

    /* Get the next block now that we've consumed the
       data off the last block */
    if ((block = INKIOBufferReaderStart(reader)) == INK_ERROR_PTR) {
      INKfree(url_str);
      INKfree(output_string);
      LOG_ERROR_AND_RETURN("INKIOBufferReaderStart");
    }
  }

  /* Terminate the string */
  output_string[output_len] = '\0';
  /*output_len++; */

  /* Free up the INKIOBuffer that we used to print out the header */
  if (INKIOBufferReaderFree(reader) == INK_ERROR) {
    INKfree(url_str);
    INKfree(output_string);
    LOG_ERROR_AND_RETURN("INKIOBufferReaderFree");
  }
#ifdef DEBUG
  if (INKIOBufferReaderFree(NULL) != INK_ERROR) {
    LOG_ERROR_NEG("INKIOBufferReaderFree(NULL)");
  }
#endif

  if (INKIOBufferDestroy(output_buffer) == INK_ERROR) {
    INKfree(url_str);
    INKfree(output_string);
    LOG_ERROR_AND_RETURN("INKIOBufferDestroy");
  }

  *p_client_req = output_string;
  return 1;
}
예제 #3
0
static void
handle_dns(INKHttpTxn txnp, INKCont contp)
{
  INKMBuffer bufp;
  INKMLoc hdr_loc;

  INKIOBuffer output_buffer;
  INKIOBufferReader reader;
  int total_avail;

  INKIOBufferBlock block;
  const char *block_start;
  int block_avail;

  char *output_string;
  int output_len;

  if (!INKHttpTxnClientReqGet(txnp, &bufp, &hdr_loc)) {
    INKDebug(DEBUG_TAG, "couldn't retrieve client request header");
    INKError("couldn't retrieve client request header\n");
    goto done;
  }

  output_buffer = INKIOBufferCreate();

  /* INKIOBufferCreate may return an error pointer */
  if ((void *) output_buffer == INK_ERROR_PTR) {
    INKDebug(DEBUG_TAG, "couldn't allocate IOBuffer");
    INKError("couldn't allocate IOBuffer\n");
    goto done;
  }

  reader = INKIOBufferReaderAlloc(output_buffer);

  /* INKIOBufferReaderAlloc may return an error pointer */
  if ((void *) reader == INK_ERROR_PTR) {
    INKDebug(DEBUG_TAG, "couldn't allocate IOBufferReader");
    INKError("couldn't allocate IOBufferReader\n");
    goto done;
  }

  /* This will print  just MIMEFields and not
     the http request line */
  INKDebug(DEBUG_TAG, "Printing the hdrs ... ");
  if (INKMimeHdrPrint(bufp, hdr_loc, output_buffer) == INK_ERROR) {
    INKDebug(DEBUG_TAG, "non-fatal: error printing mime-hdrs");
    INKError("non-fatal: error printing mime-hdrs\n");
  }

  if (INKHandleMLocRelease(bufp, INK_NULL_MLOC, hdr_loc) == INK_ERROR) {
    INKDebug(DEBUG_TAG, "non-fatal: error releasing MLoc");
    INKError("non-fatal: error releasing MLoc\n");
  }

  /* Find out how the big the complete header is by
     seeing the total bytes in the buffer.  We need to
     look at the buffer rather than the first block to
     see the size of the entire header */
  total_avail = INKIOBufferReaderAvail(reader);

  /* INKIOBufferReaderAvail may send an INK_ERROR */
  if ((INKReturnCode) total_avail == INK_ERROR) {
    INKDebug(DEBUG_TAG, "couldn't get available byte-count from IO-read-buffer");
    INKError("couldn't get available byte-count from IO-read-buffer\n");
    goto done;
  }

  /* Allocate the string with an extra byte for the string
     terminator */
  output_string = (char *) INKmalloc(total_avail + 1);
  output_len = 0;

  /* We need to loop over all the buffer blocks to make
     sure we get the complete header since the header can
     be in multiple blocks */
  block = INKIOBufferReaderStart(reader);

  /* INKIOBufferReaderStart may return an error pointer */
  if (block == INK_ERROR_PTR) {
    INKDebug(DEBUG_TAG, "couldn't get from IOBufferBlock");
    INKError("couldn't get from IOBufferBlock\n");
    goto done;
  }

  while (block) {

    block_start = INKIOBufferBlockReadStart(block, reader, &block_avail);

    /* INKIOBufferBlockReadStart may return an error pointer */
    if (block_start == INK_ERROR_PTR) {
      INKDebug(DEBUG_TAG, "couldn't read from IOBuffer");
      INKError("couldn't read from IOBuffer\n");
      goto done;
    }

    /* We'll get a block pointer back even if there is no data
       left to read so check for this condition and break out of
       the loop. A block with no data to read means we've exhausted
       buffer of data since if there was more data on a later 
       block in the chain, this block would have been skipped over */
    if (block_avail == 0) {
      break;
    }

    memcpy(output_string + output_len, block_start, block_avail);
    output_len += block_avail;

    /* Consume the data so that we get to the next block */
    if (INKIOBufferReaderConsume(reader, block_avail) == INK_ERROR) {
      INKDebug(DEBUG_TAG, "error consuming data from the ReaderBlock");
      INKError("error consuming data from the ReaderBlock\n");
    }

    /* Get the next block now that we've consumed the
       data off the last block */
    block = INKIOBufferReaderStart(reader);

    /* INKIOBufferReaderStart may return an error pointer */
    if (block == INK_ERROR_PTR) {
      INKDebug(DEBUG_TAG, "couldn't get from IOBufferBlock");
      INKError("couldn't get from IOBufferBlock\n");
      goto done;
    }
  }

  /* Terminate the string */
  output_string[output_len] = '\0';
  output_len++;

  /* Free up the INKIOBuffer that we used to print out the header */
  if (INKIOBufferReaderFree(reader) != INK_SUCCESS) {
    INKDebug(DEBUG_TAG, "non-fatal: error releasing IOBufferReader");
    INKError("non-fatal: error releasing IOBufferReader\n");
  }

  if (INKIOBufferDestroy(output_buffer) != INK_SUCCESS) {
    INKDebug(DEBUG_TAG, "non-fatal: error destroying IOBuffer");
    INKError("non-fatal: error destroying IOBuffer\n");
  }

  /* Although I'd never do this a production plugin, printf
     the header so that we can see it's all there */
  INKDebug("debug-output-header", "%s", output_string);

  INKfree(output_string);

done:
  INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
}