example/thread-pool/psi.c
static int
psi_include(INKCont contp, void *edata)
{
#define BUFFER_SIZE 1024
  ContData *data;
  INKFile filep;
  char buf[BUFFER_SIZE];
  char inc_file[PSI_PATH_MAX_SIZE + PSI_FILENAME_MAX_SIZE];
  INKReturnCode retval;

  /* We manipulate plugin continuation data from a separate thread.
     Grab mutex to avoid concurrent access */
  retval = INKMutexLock(INKContMutexGet(contp));
  if (retval != INK_SUCCESS) {
    INKError("[psi_include] Could not lock mutex");
    return 0;
  }

  data = INKContDataGet(contp);
  INKAssert(data->magic == MAGIC_ALIVE);

  if (!data->psi_buffer) {
    data->psi_buffer = INKIOBufferCreate();
    data->psi_reader = INKIOBufferReaderAlloc(data->psi_buffer);

    if ((data->psi_buffer == INK_ERROR_PTR) || (data->psi_reader == INK_ERROR_PTR)) {
      INKError("[psi_include] Could not create iobuffer to store include content");
      goto error;
    }
  }

  /* For security reason, we do not allow to include files that are
     not in the directory <plugin_path>/include.
     Also include file cannot contain any path. */
  sprintf(inc_file, "%s/%s", psi_directory, basename(data->psi_filename));

  /* Read the include file and copy content into iobuffer */
  if ((filep = INKfopen(inc_file, "r")) != NULL) {
    INKDebug(DBG_TAG, "Reading include file %s", inc_file);

    while (INKfgets(filep, buf, BUFFER_SIZE) != NULL) {
      INKIOBufferBlock block;
      int len, avail, ndone, ntodo, towrite;
      char *ptr_block;

      len = strlen(buf);
      ndone = 0;
      ntodo = len;
      while (ntodo > 0) {
        /* INKIOBufferStart allocates more blocks if required */
        block = INKIOBufferStart(data->psi_buffer);
        if (block == INK_ERROR_PTR) {
          INKError("[psi_include] Could not get buffer block");
          goto error;
        }
        ptr_block = INKIOBufferBlockWriteStart(block, &avail);
        if (ptr_block == INK_ERROR_PTR) {
          INKError("[psi_include] Could not get buffer block");
          goto error;
        }
        towrite = MIN(ntodo, avail);

        memcpy(ptr_block, buf + ndone, towrite);
        retval = INKIOBufferProduce(data->psi_buffer, towrite);
        if (retval == INK_ERROR) {
          INKError("[psi_include] Could not produce data");
          goto error;
        }
        ntodo -= towrite;
        ndone += towrite;
      }
    }
    INKfclose(filep);
    data->psi_success = 1;
    if (log) {
      INKTextLogObjectWrite(log, "Successfully included file: %s", inc_file);
    }
  } else {
    data->psi_success = 0;
    if (log) {
      INKTextLogObjectWrite(log, "Failed to include file: %s", inc_file);
    }
  }

  /* Change state and schedule an event EVENT_IMMEDIATE on the plugin continuation
     to let it know we're done. */

  /* Note: if the blocking call was not in the transformation state (i.e. in
     INK_HTTP_READ_REQUEST_HDR, INK_HTTP_OS_DNS and so on...) we could
     use INKHttpTxnReenable to wake up the transaction instead of sending an event. */
done:
  INKContSchedule(contp, 0);
  data->state = STATE_DUMP_PSI;
  INKMutexUnlock(INKContMutexGet(contp));
  return 1;

error:
  INKContSchedule(contp, 0);
  data->psi_success = 0;
  data->state = STATE_READ_DATA;
  INKMutexUnlock(INKContMutexGet(contp));
  return 0;
}
/*-------------------------------------------------------------------------
  psi_include
  Read file to include. Copy its content into an iobuffer.

  This is the function doing blocking calls and called by the plugin's threads

  Input:
    data      continuation for the current transaction
  Output :
    data->psi_buffer  contains the file content
    data->psi_sucess  0 if include failed, 1 if success
  Return Value:
    0  if failure
    1  if success
  -------------------------------------------------------------------------*/
psi_include
Read file to include. Copy its content into an iobuffer.

This is the function doing blocking calls and called by the plugin's threads

data      continuation for the current transaction
Output :
data->psi_buffer  contains the file content
data->psi_sucess  0 if include failed, 1 if success
Return Value:
0  if failure
1  if success
psi_include Read file to include . Copy its content into an iobuffer . This is the function doing blocking calls and called by the plugin ' s threads data continuation for the current transaction Output : data - > psi_buffer contains the file content data - > psi_sucess 0 if include failed 1 if success Return Value : 0 if failure 1 if success
static int
psi_include(INKCont contp, void *edata)
{
#define BUFFER_SIZE 1024
  ContData *data;
  INKFile filep;
  char buf[BUFFER_SIZE];
  char inc_file[PSI_PATH_MAX_SIZE + PSI_FILENAME_MAX_SIZE];
  INKReturnCode retval;
  retval = INKMutexLock(INKContMutexGet(contp));
  if (retval != INK_SUCCESS) {
    INKError("[psi_include] Could not lock mutex");
    return 0;
  }
  data = INKContDataGet(contp);
  INKAssert(data->magic == MAGIC_ALIVE);
  if (!data->psi_buffer) {
    data->psi_buffer = INKIOBufferCreate();
    data->psi_reader = INKIOBufferReaderAlloc(data->psi_buffer);
    if ((data->psi_buffer == INK_ERROR_PTR) || (data->psi_reader == INK_ERROR_PTR)) {
      INKError("[psi_include] Could not create iobuffer to store include content");
      goto error;
    }
  }
  sprintf(inc_file, "%s/%s", psi_directory, basename(data->psi_filename));
  if ((filep = INKfopen(inc_file, "r")) != NULL) {
    INKDebug(DBG_TAG, "Reading include file %s", inc_file);
    while (INKfgets(filep, buf, BUFFER_SIZE) != NULL) {
      INKIOBufferBlock block;
      int len, avail, ndone, ntodo, towrite;
      char *ptr_block;
      len = strlen(buf);
      ndone = 0;
      ntodo = len;
      while (ntodo > 0) {
        block = INKIOBufferStart(data->psi_buffer);
        if (block == INK_ERROR_PTR) {
          INKError("[psi_include] Could not get buffer block");
          goto error;
        }
        ptr_block = INKIOBufferBlockWriteStart(block, &avail);
        if (ptr_block == INK_ERROR_PTR) {
          INKError("[psi_include] Could not get buffer block");
          goto error;
        }
        towrite = MIN(ntodo, avail);
        memcpy(ptr_block, buf + ndone, towrite);
        retval = INKIOBufferProduce(data->psi_buffer, towrite);
        if (retval == INK_ERROR) {
          INKError("[psi_include] Could not produce data");
          goto error;
        }
        ntodo -= towrite;
        ndone += towrite;
      }
    }
    INKfclose(filep);
    data->psi_success = 1;
    if (log) {
      INKTextLogObjectWrite(log, "Successfully included file: %s", inc_file);
    }
  } else {
    data->psi_success = 0;
    if (log) {
      INKTextLogObjectWrite(log, "Failed to include file: %s", inc_file);
    }
  }
done:
  INKContSchedule(contp, 0);
  data->state = STATE_DUMP_PSI;
  INKMutexUnlock(INKContMutexGet(contp));
  return 1;
error:
  INKContSchedule(contp, 0);
  data->psi_success = 0;
  data->state = STATE_READ_DATA;
  INKMutexUnlock(INKContMutexGet(contp));
  return 0;
}
static int psi_include ( INKCont contp , void * edata ) { #define BUFFER_SIZE  1024 ContData * data ; INKFile filep ; char buf [ BUFFER_SIZE ] ; char inc_file [ PSI_PATH_MAX_SIZE + PSI_FILENAME_MAX_SIZE ] ; INKReturnCode retval ; retval = INKMutexLock ( INKContMutexGet ( contp ) ) ; if ( retval != INK_SUCCESS ) { INKError ( " " ) ; return 0 ; } data = INKContDataGet ( contp ) ; INKAssert ( data -> magic == MAGIC_ALIVE ) ; if ( ! data -> psi_buffer ) { data -> psi_buffer = INKIOBufferCreate ( ) ; data -> psi_reader = INKIOBufferReaderAlloc ( data -> psi_buffer ) ; if ( ( data -> psi_buffer == INK_ERROR_PTR ) || ( data -> psi_reader == INK_ERROR_PTR ) ) { INKError ( " " ) ; goto error ; } } sprintf ( inc_file , " " , psi_directory , basename ( data -> psi_filename ) ) ; if ( ( filep = INKfopen ( inc_file , " " ) ) != NULL ) { INKDebug ( DBG_TAG , " " , inc_file ) ; while ( INKfgets ( filep , buf , BUFFER_SIZE ) != NULL ) { INKIOBufferBlock block ; int len , avail , ndone , ntodo , towrite ; char * ptr_block ; len = strlen ( buf ) ; ndone = 0 ; ntodo = len ; while ( ntodo > 0 ) { block = INKIOBufferStart ( data -> psi_buffer ) ; if ( block == INK_ERROR_PTR ) { INKError ( " " ) ; goto error ; } ptr_block = INKIOBufferBlockWriteStart ( block , & avail ) ; if ( ptr_block == INK_ERROR_PTR ) { INKError ( " " ) ; goto error ; } towrite = MIN ( ntodo , avail ) ; memcpy ( ptr_block , buf + ndone , towrite ) ; retval = INKIOBufferProduce ( data -> psi_buffer , towrite ) ; if ( retval == INK_ERROR ) { INKError ( " " ) ; goto error ; } ntodo -= towrite ; ndone += towrite ; } } INKfclose ( filep ) ; data -> psi_success = 1 ; if ( log ) { INKTextLogObjectWrite ( log , " " , inc_file ) ; } } else { data -> psi_success = 0 ; if ( log ) { INKTextLogObjectWrite ( log , " " , inc_file ) ; } } done : INKContSchedule ( contp , 0 ) ; data -> state = STATE_DUMP_PSI ; INKMutexUnlock ( INKContMutexGet ( contp ) ) ; return 1 ; error : INKContSchedule ( contp , 0 ) ; data -> psi_success = 0 ; data -> state = STATE_READ_DATA ; INKMutexUnlock ( INKContMutexGet ( contp ) ) ; return 0 ; }
psi_include
Read file to include.
psi_include Read file to include .
/* We manipulate plugin continuation data from a separate thread.
     Grab mutex to avoid concurrent access */ /* For security reason, we do not allow to include files that are
     not in the directory <plugin_path>/include.
     Also include file cannot contain any path. */ /* Read the include file and copy content into iobuffer */ /* INKIOBufferStart allocates more blocks if required */ /* Change state and schedule an event EVENT_IMMEDIATE on the plugin continuation
     to let it know we're done. */ /* Note: if the blocking call was not in the transformation state (i.e. in
     INK_HTTP_READ_REQUEST_HDR, INK_HTTP_OS_DNS and so on...) we could
     use INKHttpTxnReenable to wake up the transaction instead of sending an event. */