Exemple #1
0
static int
startplugin(INKCont contp, INKEvent event, void *edata)
{

  INKMBuffer bufp;
  INKMLoc hdr_loc;
  INKMLoc field_loc;

  int count;


  INKHttpTxn txnp = (INKHttpTxn) edata;

  if (!INKHttpTxnClientReqGet(txnp, &bufp, &hdr_loc)) {
    printf("Couldn't retrieve Client Request Header\n");
    INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
    return 0;
  }

  if ((field_loc = INKMimeHdrFieldFind(bufp, hdr_loc, FIELD_NAME, -1)) != 0) {
    count = INKMimeFieldValueGetInt(bufp, field_loc, 0);
    INKMimeFieldValueSetInt(bufp, field_loc, 0, value);
  } else {
    field_loc = INKMimeFieldCreate(bufp);
    INKMimeFieldNameSet(bufp, field_loc, FIELD_NAME, -1);
    INKMimeFieldValueInsertInt(bufp, field_loc, value, -1);
    INKMimeHdrFieldInsert(bufp, hdr_loc, field_loc, -1);
  }

  INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
  return 0;
}
Exemple #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;
}
Exemple #3
0
/******************************************************************** 
 * When the client sends the same request the second time, get the
 * header field MY_HDR from the caches response header, print it out
 * and save it in a text file.
 ********************************************************************/
static int
handle_cache_hdr(INKHttpTxn txnp)
{
  LOG_SET_FUNCTION_NAME("handle_cache_hdr");

  INKMBuffer cache_bufp;
  INKMLoc cache_loc = NULL, field_loc = NULL;
  const char *my_hdr;
  int value_count, i, length;
  char output_file[1024], output_str[1024];
  INKFile file;

  output_str[0] = '\0';

  /* get the cached response header */
  if (!INKHttpTxnCachedRespGet(txnp, &cache_bufp, &cache_loc))
    LOG_ERROR_AND_RETURN("INKHttpTxnCachedRespGet");

  /* get the MY_HDR field in the header */
  if ((field_loc = INKMimeHdrFieldFind(cache_bufp, cache_loc, MY_HDR, strlen(MY_HDR))) == INK_ERROR_PTR ||
      field_loc == NULL)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldFind");

  if ((value_count = INKMimeHdrFieldValuesCount(cache_bufp, cache_loc, field_loc)) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValuesCount");
  for (i = 0; i <= value_count - 1; i++) {
    if (INKMimeHdrFieldValueStringGet(cache_bufp, cache_loc, field_loc, i, &my_hdr, &length) == INK_ERROR)
      LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueStringGet");

    /* concatenate the field to output_str */
    if (my_hdr) {
      snprintf(output_str, 1024, "%s MY_HDR(%d): %.*s \n", output_str, i, length, my_hdr);
      INKHandleStringRelease(cache_bufp, field_loc, my_hdr);
    }
  }

  snprintf(output_file, 1024, "%s/write_server_ip.txt", plugin_dir);
  INKDebug(DEBUG_TAG, "Writing record\n%s\nto file %s", output_str, output_file);

  /* append to the text file */
  if (INKMutexLock(file_mutex) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMutexLock");

  if ((file = INKfopen(output_file, "w")) == NULL)
    LOG_ERROR_AND_CLEANUP("INKfopen");
  INKfwrite(file, output_str, strlen(output_str));
  INKfflush(file);
  INKfclose(file);

  if (INKMutexUnlock(file_mutex) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMutexUnlock");

  /* cleanup */
Lcleanup:
  /* release mlocs */
  if (VALID_POINTER(field_loc))
    INKHandleMLocRelease(cache_bufp, cache_loc, field_loc);
  if (VALID_POINTER(cache_loc))
    INKHandleMLocRelease(cache_bufp, INK_NULL_MLOC, cache_loc);

  return 0;
}