예제 #1
0
void
INKPluginInit(int argc, const char *argv[])
{
  INKPluginRegistrationInfo info;

  info.plugin_name = "output-header";
  info.vendor_name = "MyCompany";
  info.support_email = "*****@*****.**";

  if (!INKPluginRegister(INK_SDK_VERSION_5_2, &info)) {
    INKError("[PluginInit] Plugin registration failed.\n");
    goto error;
  }

  if (!check_ts_version()) {
    INKError("[PluginInit] Plugin requires Traffic Server 5.2.0 or later\n");
    goto error;
  }


  INKHttpHookAdd(INK_HTTP_OS_DNS_HOOK, INKContCreate(hdr_plugin, NULL));

error:
  INKError("[PluginInit] Plugin not initialized");
}
예제 #2
0
void
INKPluginInit(int argc, const char *argv[])
{
  INKPluginRegistrationInfo info;

  info.plugin_name = "cluster-RPC";
  info.vendor_name = "MyCompany";
  info.support_email = "*****@*****.**";

  if (!INKPluginRegister(INK_SDK_VERSION_2_0, &info)) {
    INKError("Plugin registration failed. \n");
  }

  if (!check_ts_version()) {
    INKError("Plugin requires Traffic Server 3.5.2 or later\n");
    return;
  }
  clusterRPC_init();
}
예제 #3
0
void
INKPluginInit(int argc, const char *argv[])
{
  INKPluginRegistrationInfo info;

  info.plugin_name = "replace-header";
  info.vendor_name = "MyCompany";
  info.support_email = "*****@*****.**";

  if (!INKPluginRegister(INK_SDK_VERSION_5_2, &info)) {
    INKError("Plugin registration failed. \n");
  }

  if (!check_ts_version()) {
    INKError("Plugin requires Traffic Server 5.2.0 or later\n");
    return;
  }

  INKHttpHookAdd(INK_HTTP_READ_RESPONSE_HDR_HOOK, INKContCreate(replace_header_plugin, NULL));
}
예제 #4
0
static void
replace_header(INKHttpTxn txnp, INKCont contp)
{
  INKMBuffer resp_bufp;
  INKMLoc resp_loc;
  INKMLoc field_loc;

  if (!INKHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc)) {
    INKError("couldn't retrieve server response header.\n");
    goto done;
  }

  field_loc = INKMimeHdrFieldRetrieve(resp_bufp, resp_loc, INK_MIME_FIELD_ACCEPT_RANGES);
  if (field_loc == 0) {
    /* field was not found */

    /* create a new field in the header */
    field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc);
    /* set its name */
    INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, INK_MIME_FIELD_ACCEPT_RANGES, INK_MIME_LEN_ACCEPT_RANGES);
    /* set its value */
    INKMimeHdrFieldValueInsert(resp_bufp, resp_loc, field_loc, "none", 4, -1);
    /* insert it into the header */
    INKMimeHdrFieldInsert(resp_bufp, resp_loc, field_loc, -1);
    INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
  } else {
    /* clear the field */
    INKMimeHdrFieldValuesClear(resp_bufp, resp_loc, field_loc);
    /* set the value to "none" */
    INKMimeHdrFieldValueInsert(resp_bufp, resp_loc, field_loc, "none", 4, -1);
    INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
  }

done:
  INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
}
예제 #5
0
static void
add_header(INKHttpTxn txnp, INKCont contp)
{
  INKMBuffer req_bufp;
  INKMLoc req_loc;
  INKMLoc field_loc;
  INKMLoc next_field_loc;
  INKMLoc new_field_loc;
  int retval;

  if (!INKHttpTxnClientReqGet(txnp, &req_bufp, &req_loc)) {
    INKError("[add_header] Error while retrieving client request header\n");
    goto done;
  }

  field_loc = INKMimeHdrFieldGet(hdr_bufp, hdr_loc, 0);
  if (new_field_loc == INK_ERROR_PTR) {
    INKError("[add_header] Error while getting field");
    goto error;
  }

  /* Loop on our header containing fields to add */
  while (field_loc) {

    /* First create a new field in the client request header */
    new_field_loc = INKMimeHdrFieldCreate(req_bufp, req_loc);
    if (new_field_loc == INK_ERROR_PTR) {
      INKError("[add_header] Error while creating new field");
      INKHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
      break;
    }

    /* Then copy our new field at this new location */
    retval = INKMimeHdrFieldCopy(req_bufp, req_loc, new_field_loc, hdr_bufp, hdr_loc, field_loc);
    if (retval == INK_ERROR) {
      INKError("[add_header] Error while copying new field");
      INKHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
      break;
    }

    /* Add this field to the Http client request header */
    retval = INKMimeHdrFieldAppend(req_bufp, req_loc, new_field_loc);
    if (retval == INK_ERROR) {
      INKError("[add_header] Error while appending new field");
      INKHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
      break;
    }

    /* We can now release this handle */
    INKHandleMLocRelease(req_bufp, req_loc, new_field_loc);

    next_field_loc = INKMimeHdrFieldNext(hdr_bufp, hdr_loc, field_loc);
    if (next_field_loc == INK_ERROR_PTR) {
      INKError("[add_header] Error while getting next field to add");
      INKHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
      goto error;
    }

    INKHandleMLocRelease(hdr_bufp, hdr_loc, field_loc);
    field_loc = next_field_loc;
  }


error:
  INKHandleMLocRelease(req_bufp, INK_NULL_MLOC, req_loc);

done:
  INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE);
}
예제 #6
0
void
INKPluginInit(int argc, const char *argv[])
{
  INKMLoc field_loc;
  const char *p;
  int i, retval;
  INKPluginRegistrationInfo info;

  info.plugin_name = "add-header";
  info.vendor_name = "MyCompany";
  info.support_email = "*****@*****.**";

  if (!INKPluginRegister(INK_SDK_VERSION_5_2, &info)) {
    INKError("[PluginInit] Plugin registration failed.\n");
    goto error;
  }

  if (!check_ts_version()) {
    INKError("[PluginInit] Plugin requires Traffic Server 5.2.0 or later\n");
    goto error;
  }

  if (argc < 2) {
    INKError("[PluginInit] Usage: %s \"name1: value1\" \"name2: value2\" ...>\n", argv[0]);
    goto error;
  }

  hdr_bufp = INKMBufferCreate();
  if (hdr_bufp == INK_ERROR_PTR) {
    INKError("[PluginInit] Can not create mbuffer");
    goto error;
  }

  hdr_loc = INKMimeHdrCreate(hdr_bufp);
  if (hdr_loc == INK_ERROR_PTR) {
    INKError("[PluginInit] Can not create mime header");
    goto error;
  }

  for (i = 1; i < argc; i++) {
    field_loc = INKMimeHdrFieldCreate(hdr_bufp, hdr_loc);
    if (field_loc == INK_ERROR_PTR) {
      INKError("[PluginInit] Error while creating field");
      goto error;
    }

    retval = INKMimeHdrFieldAppend(hdr_bufp, hdr_loc, field_loc);
    if (retval == INK_ERROR) {
      INKError("[PluginInit] Error while adding field");
      goto error;
    }

    p = strchr(argv[i], ':');
    if (p) {
      retval = INKMimeHdrFieldNameSet(hdr_bufp, hdr_loc, field_loc, argv[i], p - argv[i]);
      if (retval == INK_ERROR) {
        INKError("[PluginInit] Error while naming field");
        goto error;
      }

      p += 1;
      while (isspace(*p)) {
        p += 1;
      }
      retval = INKMimeHdrFieldValueInsert(hdr_bufp, hdr_loc, field_loc, p, strlen(p), -1);
      if (retval == INK_ERROR) {
        INKError("[PluginInit] Error while inserting field value");
        goto error;
      }
    } else {
      retval = INKMimeHdrFieldNameSet(hdr_bufp, hdr_loc, field_loc, argv[i], strlen(argv[i]));
      if (retval == INK_ERROR) {
        INKError("[PluginInit] Error while inserting field value");
        goto error;
      }
    }
  }

  /* Create a continuation with a mutex as there is a shared global structure
     containing the headers to add */
  retval = INKHttpHookAdd(INK_HTTP_READ_REQUEST_HDR_HOOK, INKContCreate(add_header_plugin, INKMutexCreate()));
  if (retval == INK_ERROR) {
    INKError("[PluginInit] Error while registering to hook");
    goto error;
  }

  goto done;

error:
  INKError("[PluginInit] Plugin not initialized");

done:
  return;
}
예제 #7
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);
}