Пример #1
0
int
insert_in_response(INKHttpTxn txnp, char *result_val)
{
  INKMBuffer resp_bufp;
  INKMLoc resp_loc;
  INKMLoc field_loc;

  LOG_SET_FUNCTION_NAME("insert_in_response");

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

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

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

  if (!INKHttpTxnClientRespGet(txnp, &resp_bufp, &resp_loc)) {
    LOG_ERROR_AND_RETURN("INKHttpTxnClientRespGet");
  }

  /* create a new field in the response header */
  if ((field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc)) == INK_ERROR_PTR) {
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
    LOG_ERROR_AND_RETURN("INKMimeHdrFieldCreate");
  }

  /* set its name */
  if (INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, "CacheTester-Result", 18) == INK_ERROR) {
    INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
    LOG_ERROR_AND_RETURN("INKMimeHdrFieldNameSet");
  }

  /* set its value */
  if (INKMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, result_val, strlen(result_val)) == INK_ERROR) {
    INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
    LOG_ERROR_AND_RETURN("INKMimeHdrFieldValueIntInsert");
  }

  /* insert it into the header */
  if (INKMimeHdrFieldAppend(resp_bufp, resp_loc, field_loc) == INK_ERROR) {
    INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
    LOG_ERROR_AND_RETURN("INKMimeHdrFieldAppend");
  }

  if (INKHandleMLocRelease(resp_bufp, resp_loc, field_loc) == INK_ERROR) {
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);
    LOG_ERROR_AND_RETURN("INKHandleMLocRelease");
  }

  if (INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc) == INK_ERROR) {
    LOG_ERROR_AND_RETURN("INKHandleMLocRelease");
  }

  return 1;
}
Пример #2
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);
}
Пример #3
0
/***********************************************************************
 * Get the server ip and request method in the client request. Get the 
 * next hop ip from the server response. 
 * Create a new http header field MY_HDR in the server response and 
 * insert server ip, request method and next hop ip into the field as
 * field values.
 ***********************************************************************/
static int
handle_response_hdr(INKCont contp, INKHttpTxn txnp)
{
  LOG_SET_FUNCTION_NAME("handle_response_hdr");

  INKMBuffer resp_bufp;
  INKMLoc resp_loc = NULL;
  INKMLoc field_loc = NULL;
  unsigned int next_hop_ip = 0;
  unsigned int server_ip = 0;
  const char *request_method = NULL;
  char *r_method = NULL;
  int length;
  INKMBuffer req_bufp;
  INKMLoc req_loc = NULL;
  int incoming_port = 0, port = 0;
  char *hostname = NULL;
  int ret_value = -1;

  /* negative test */
#ifdef DEBUG
  if (INKHttpTxnServerIPGet(NULL) != 0)
    LOG_ERROR_NEG("INKHttpTxnServerIPGet");
  if (INKHttpTxnNextHopIPGet(NULL) != 0)
    LOG_ERROR_NEG("INKHttpTxnNextHopIPGet");
  if (INKHttpTxnParentProxyGet(NULL, &hostname, &port) != INK_ERROR)
    LOG_ERROR_NEG("INKHttpTxnParentProxyGet");
#endif

  /* get the server ip */
  if ((server_ip = INKHttpTxnServerIPGet(txnp)) == 0)
    LOG_ERROR_AND_RETURN("INKHttpTxnServerIPGet");

  /* get the request method */
  if (!INKHttpTxnServerReqGet(txnp, &req_bufp, &req_loc))
    LOG_ERROR_AND_RETURN("INKHttpTxnServerReqGet");

  if ((request_method = INKHttpHdrMethodGet(req_bufp, req_loc, &length)) == INK_ERROR_PTR || request_method == NULL)
    LOG_ERROR_AND_CLEANUP("INKHttpHdrMethodGet");

  r_method = INKstrndup(request_method, length);


  /* get the next hop ip */
  if ((next_hop_ip = INKHttpTxnNextHopIPGet(txnp)) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKHttpTxnNextHopIPGet");


  /* get the client incoming port */
  if ((incoming_port = INKHttpTxnClientIncomingPortGet(txnp)) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKHttpTxnClientIncomingPortGet");


  /* get the parent proxy */
  if (INKHttpTxnParentProxyGet(txnp, &hostname, &port) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKHttpTxnParentProxyGet");
  /* If no parent defined in records.config, set hostname to NULL and port to -1 */
  if (hostname == NULL) {
    hostname = "NULL";
    port = -1;
  }

  /* retrieve the server response header */
  if (!INKHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc))
    LOG_ERROR_AND_CLEANUP("INKHttpTxnServerRespGet");


  /* create and insert into hdr a new mime header field */
  if ((field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc)) == INK_ERROR_PTR || field_loc == NULL)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldCreate");
  if (INKMimeHdrFieldAppend(resp_bufp, resp_loc, field_loc) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldAppend");
  if (INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, MY_HDR, strlen(MY_HDR)) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldNameSet");

  /* Add value to the new mime header field */
  if (INKMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, r_method, strlen(r_method)) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueStringInsert");
  if (INKMimeHdrFieldValueUintInsert(resp_bufp, resp_loc, field_loc, -1, server_ip) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueUintInsert");
  if (INKMimeHdrFieldValueUintInsert(resp_bufp, resp_loc, field_loc, -1, next_hop_ip) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueUintInsert");
  if (INKMimeHdrFieldValueIntInsert(resp_bufp, resp_loc, field_loc, -1, incoming_port) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueIntInsert");
  if (INKMimeHdrFieldValueIntInsert(resp_bufp, resp_loc, field_loc, -1, port) == INK_ERROR)
    LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueIntInsert");

  /* success */
  ret_value = 0;

Lcleanup:
  if (VALID_POINTER(r_method))
    INKfree(r_method);

  /* negative test for INKHandleStringRelease */
#ifdef DEBUG
  if (INKHandleStringRelease(NULL, req_loc, request_method) != INK_ERROR) {
    LOG_ERROR_NEG("INKHandleStringRelease");
  }
#endif

  /* release the buffer handles */
  if (VALID_POINTER(request_method))
    INKHandleStringRelease(req_bufp, req_loc, request_method);
  if (VALID_POINTER(req_loc))
    INKHandleMLocRelease(req_bufp, INK_NULL_MLOC, req_loc);

  /* free the handles and continuation data */
  if (VALID_POINTER(field_loc))
    INKHandleMLocRelease(resp_bufp, resp_loc, field_loc);
  if (VALID_POINTER(resp_loc))
    INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc);

  return ret_value;
}
Пример #4
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;
}