Example #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;
}
Example #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;
}
Example #3
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);
}
Example #4
0
static void
handleReadRequest(INKCont pCont, INKHttpTxn pTxn)
{
  LOG_SET_FUNCTION_NAME("handleReadRequest");

  INKMBuffer reqHdrBuf = NULL, newHttpHdrBuf = NULL;
  INKMLoc reqHdrLoc = NULL, newHttpHdrLoc = NULL;

  INKHttpType httpType;

  int iOldHttpVersion, iHttpMethodLength, iHttpVersion;
  const char *sHttpMethod = NULL;
  char *outputString = NULL, *sOldHttpMethod = NULL;

  HdrInfo_T *pReqHdrInfo = NULL, *pNewReqHdrInfo = NULL;

#if 0
  const char *constant_request_header_str =
    "GET http://www.joes-hardware.com/ HTTP/1.0\r\nDate: Wed, 05 Jul 2000 22:12:26 GMT\r\nConnection: Keep-Alive\r\nUser-Agent: Mozilla/4.51 [en] (X11; U; IRIX 6.2 IP22)\r\nHost: www.joes-hardware.com\r\nCache-Control: no-cache\r\nAccept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*\r\nAccept-Charset: iso-8859-1,*,utf-8\r\nAccept-Encoding: gzip\r\nAccept-Language: en\r\nX-Number-Header: 12345\r\nX-Silly-Header: frobnichek grobbledegook\r\nAccept-Charset: windows-1250, koi8-r\r\nX-Silly-Header: wawaaa\r\n\r\n";

#endif


  pReqHdrInfo = initHdr();
  pNewReqHdrInfo = initHdr();

  INKDebug(REQ, "\n>>>>>> handleReadRequest <<<<<<<\n");

  /* Get Request Marshall Buffer */
  if (!INKHttpTxnClientReqGet(pTxn, &reqHdrBuf, &reqHdrLoc)) {
    LOG_API_ERROR_COMMENT("INKHttpTxnClientReqGet", "ERROR: Can't retrieve client req hdr");
    goto done;
  }


    /******* (1): Get every specifics from the HTTP header *********/

  INKDebug(REQ, "--------------------------------");

  getHdrInfo(pReqHdrInfo, reqHdrBuf, reqHdrLoc);
  printHttpHeader(reqHdrBuf, reqHdrLoc, REQ, 1);

#ifdef DEBUG
  negTesting(reqHdrBuf, reqHdrLoc);
#endif

    /*********** (2): Create/Copy/Destroy **********/
  /* For every request, create, copy and destroy a new HTTP header and 
   * print the details */

  INKDebug(REQ, "--------------------------------");
  if ((newHttpHdrBuf = INKMBufferCreate()) == INK_ERROR_PTR) {
    LOG_API_ERROR_COMMENT("INKMBufferCreate", "skipping to section 3");
    goto section_3;             /* Skip to section (3) down the line directly; I hate GOTOs */
  }

    /*** INKHttpHdrCreate ***/
  if ((newHttpHdrLoc = INKHttpHdrCreate(newHttpHdrBuf)) == INK_ERROR_PTR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrCreate", "skipping to section 3");
    goto section_3;             /* Skip to section (3) down the line directly; I hate GOTOs */
  }

  /* Make sure the newly created HTTP header has INKHttpType value of INK_HTTP_TYPE_UNKNOWN */
  if ((httpType = INKHttpHdrTypeGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrTypeGet", "but still continuing...");
  } else if (httpType != INK_HTTP_TYPE_UNKNOWN) {
    LOG_AUTO_ERROR("INKHttpHdrCreate", "Newly created hdr not of type INK_HTTP_TYPE_UNKNOWN");
  }

  /* set the HTTP header type: a new buffer has a type INK_HTTP_TYPE_UNKNOWN by default */
  if (INKHttpHdrTypeSet(newHttpHdrBuf, newHttpHdrLoc, INK_HTTP_TYPE_REQUEST) == INK_ERROR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrTypeSet", "unable to set it to INK_HTTP_TYPE_REQUEST");
  } else if ((httpType = INKHttpHdrTypeGet(newHttpHdrBuf, newHttpHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR_COMMENT("INKHttpHdrTypeGet", "still continuing");
  } else if (httpType != INK_HTTP_TYPE_REQUEST) {
    LOG_AUTO_ERROR("INKHttpHdrTypeSet", "Type not set to INK_HTTP_TYPE_REQUEST");
  }

    /*** INKHttpHdrCopy ***/
  if (INKHttpHdrCopy(newHttpHdrBuf, newHttpHdrLoc, reqHdrBuf, reqHdrLoc) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrCopy");
  }
  getHdrInfo(pNewReqHdrInfo, newHttpHdrBuf, newHttpHdrLoc);

  if (!identical_hdr(pNewReqHdrInfo, pReqHdrInfo)) {
    LOG_AUTO_ERROR("INKHttpHdrCopy", "New req buffer not identical to the original");
  }

  printHttpHeader(newHttpHdrBuf, newHttpHdrLoc, REQ, 2);

  FREE(pNewReqHdrInfo->httpMethod);
  FREE(pNewReqHdrInfo->hostName);

section_3:
    /********* (3): Excercise all the INK__Set on ReqBuf *********/
  INKDebug(REQ, "--------------------------------");

    /*** INKHttpHdrMethodSet ***/
  /* save the original method */
  if ((sHttpMethod = INKHttpHdrMethodGet(reqHdrBuf, reqHdrLoc, &iHttpMethodLength)) == INK_ERROR_PTR) {
    LOG_API_ERROR("INKHttpHdrMethodGet");
  } else {
    sOldHttpMethod = INKstrndup(sHttpMethod, iHttpMethodLength);
  }
  /* change it to some unknown method */
  if (INKHttpHdrMethodSet(reqHdrBuf, reqHdrLoc, "FOOBAR", strlen("FOOBAR")) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrMethodSet");
  } else {
    if ((sHttpMethod = INKHttpHdrMethodGet(reqHdrBuf, reqHdrLoc, &iHttpMethodLength)) == INK_ERROR_PTR) {
      LOG_API_ERROR("INKHttpHdrMethodGet");
    } else if (strncmp(sHttpMethod, "FOOBAR", iHttpMethodLength)) {
      LOG_AUTO_ERROR("INKHttpHdrMethodSet/Get", "GET method different from SET method");
    }
  }

  outputString = INKstrndup(sHttpMethod, iHttpMethodLength);
  INKDebug(REQ, "(3): new HTTP Header Method = %s", outputString);
  FREE(outputString);
  STR_RELEASE(reqHdrBuf, reqHdrLoc, sHttpMethod);

  printHttpHeader(reqHdrBuf, reqHdrLoc, REQ, 3);

  /* set it back to the original method */
  /*INKHttpHdrMethodSet (reqHdrBuf, reqHdrLoc, sOldHttpMethod, iHttpMethodLength); */
  if (INKHttpHdrMethodSet(reqHdrBuf, reqHdrLoc, sOldHttpMethod, strlen(sOldHttpMethod)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrMethodSet");
  } else if ((sHttpMethod = INKHttpHdrMethodGet(reqHdrBuf, reqHdrLoc, &iHttpMethodLength)) == INK_ERROR_PTR) {
    LOG_API_ERROR("INKHttpHdrMethodGet");
  } else if (strncmp(sHttpMethod, sOldHttpMethod, iHttpMethodLength)) {
    LOG_AUTO_ERROR("INKHttpHdrMethodSet/Get", "GET method different from SET method");
  }

  outputString = INKstrndup(sHttpMethod, iHttpMethodLength);
  INKDebug(REQ, "(3): original HTTP Header Method = %s", outputString);
  FREE(outputString);
  STR_RELEASE(reqHdrBuf, reqHdrLoc, sHttpMethod);


    /*** INKHttpHdrVersionSet ***/
  /* get the original version */
  if ((iOldHttpVersion = INKHttpHdrVersionGet(reqHdrBuf, reqHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  }

  /* change it to some unknown version */
  if (INKHttpHdrVersionSet(reqHdrBuf, reqHdrLoc, INK_HTTP_VERSION(10, 10)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionSet");
  } else if ((iHttpVersion = INKHttpHdrVersionGet(reqHdrBuf, reqHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  } else if ((INK_HTTP_MAJOR(iHttpVersion) != 10) || (INK_HTTP_MINOR(iHttpVersion) != 10)) {
    LOG_AUTO_ERROR("INKHttpHdrVersionSet/Get", "SET HTTP version different from GET version");
  }

  INKDebug(REQ, "(3): new HTTP version; Major = %d   Minor = %d",
           INK_HTTP_MAJOR(iHttpVersion), INK_HTTP_MINOR(iHttpVersion));

  /* change it back to the original version */
  if (INKHttpHdrVersionSet(reqHdrBuf, reqHdrLoc, iOldHttpVersion) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionSet");
  } else if ((iHttpVersion = INKHttpHdrVersionGet(reqHdrBuf, reqHdrLoc)) == INK_ERROR) {
    LOG_API_ERROR("INKHttpHdrVersionGet");
  } else if (iHttpVersion != iOldHttpVersion) {
    LOG_AUTO_ERROR("INKHttpHdrVersionSet/Get", "SET HTTP version different from GET version");
  }

  getHdrInfo(pNewReqHdrInfo, reqHdrBuf, reqHdrLoc);
  if (!identical_hdr(pNewReqHdrInfo, pReqHdrInfo)) {
    LOG_AUTO_ERROR("INK..Set", "ReqBuf: Values not restored properly");
  }

  /* (3): clean-up */
  FREE(sOldHttpMethod);

done:
    /*************** Clean-up ***********************/
  /*
     FREE(pReqHdrInfo->httpMethod);
     FREE(pReqHdrInfo->hostName);

     FREE(pNewReqHdrInfo->httpMethod);
     FREE(pNewReqHdrInfo->hostName);

     FREE(pReqHdrInfo);
     FREE(pNewReqHdrInfo);
   */
  freeHdr(pReqHdrInfo);
  freeHdr(pNewReqHdrInfo);

  /* release hdrLoc */
  HANDLE_RELEASE(reqHdrBuf, INK_NULL_MLOC, reqHdrLoc);
  HANDLE_RELEASE(newHttpHdrBuf, INK_NULL_MLOC, newHttpHdrLoc);

  /* destroy hdr */
  HDR_DESTROY(newHttpHdrBuf, newHttpHdrLoc);

  /* destroy mbuffer */
  BUFFER_DESTROY(newHttpHdrBuf);


  if (INKHttpTxnReenable(pTxn, INK_EVENT_HTTP_CONTINUE) == INK_ERROR) {
    LOG_API_ERROR("INKHttpTxnReenable");
  }
  INKDebug(REQ, "..... exiting handleReadRequest ......\n");

}                               /* handleReadReadRequest */
Example #5
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);
}