Exemplo n.º 1
0
void
ts_http_fetcher_launch(http_fetcher *fch)
{
    int64_t     hdr_len;
    // post body , content-length

    TSIOBufferWrite(fch->req_buffer, "\r\n", sizeof("\r\n")-1);
    hdr_len = TSIOBufferReaderAvail(fch->req_reader);

    fch->http_vc = TSHttpConnect(&(fch->aip));

    fch->hdr_buffer = TSIOBufferSizedCreate(TS_IOBUFFER_SIZE_INDEX_8K);
    fch->hdr_reader = TSIOBufferReaderAlloc(fch->hdr_buffer);
    fch->hdr_bufp = TSMBufferCreate();
    fch->hdr_loc = TSHttpHdrCreate(fch->hdr_bufp);

    fch->resp_buffer = TSIOBufferCreate();
    fch->resp_reader = TSIOBufferReaderAlloc(fch->resp_buffer);
    fch->http_parser = TSHttpParserCreate();

    fch->fetch_contp = TSContCreate(ts_http_fetch_handler, fch->mutexp);
    TSContDataSet(fch->fetch_contp, fch);

    fch->read_vio = TSVConnRead(fch->http_vc, fch->fetch_contp, fch->resp_buffer, INT64_MAX);

    if (fch->method == TS_FETCH_METHOD_POST && fch->post_cl >= 0) {
        fch->write_vio = TSVConnWrite(fch->http_vc, fch->fetch_contp, fch->req_reader, hdr_len + fch->post_cl);
    } else {
        fch->write_vio = TSVConnWrite(fch->http_vc, fch->fetch_contp, fch->req_reader, hdr_len);
    }

    fch->launched = 1;
}
Exemplo n.º 2
0
void
ts_http_fetcher_extract(http_fetcher *fch)
{
    TSMLoc      cl_loc, te_loc;
    const char  *val;
    int         val_len;
    int         i, n;

    fch->status_code = TSHttpHdrStatusGet(fch->hdr_bufp, fch->hdr_loc);

    cl_loc = TSMimeHdrFieldFind(fch->hdr_bufp, fch->hdr_loc, TS_MIME_FIELD_CONTENT_LENGTH, TS_MIME_LEN_CONTENT_LENGTH);
    if (cl_loc)
        fch->resp_cl = TSMimeHdrFieldValueInt64Get(fch->hdr_bufp, fch->hdr_loc, cl_loc, -1);
    else
        fch->resp_cl = -1;

    te_loc = TSMimeHdrFieldFind(fch->hdr_bufp, fch->hdr_loc, TS_MIME_FIELD_TRANSFER_ENCODING, TS_MIME_LEN_TRANSFER_ENCODING);
    if (te_loc) {
        n = TSMimeHdrFieldValuesCount(fch->hdr_bufp, fch->hdr_loc, te_loc);
        for (i = 0; i < n; i++) {
            val = TSMimeHdrFieldValueStringGet(fch->hdr_bufp, fch->hdr_loc, te_loc, i, &val_len);
            if ((val_len == TS_HTTP_LEN_CHUNKED) && (strncasecmp(val, TS_HTTP_VALUE_CHUNKED, val_len) == 0)) {
                fch->chunked = 1;
                break;
            }
        }
    }

    if (cl_loc)
        TSHandleMLocRelease(fch->hdr_bufp, fch->hdr_loc, cl_loc);

    if (te_loc) {

        if (fch->flags & TS_FLAG_FETCH_FORCE_DECHUNK)
            TSMimeHdrFieldDestroy(fch->hdr_bufp, fch->hdr_loc, te_loc);

        TSHandleMLocRelease(fch->hdr_bufp, fch->hdr_loc, te_loc);
    }

    if (fch->chunked) {
        if (fch->flags & TS_FLAG_FETCH_FORCE_DECHUNK) {
            chunked_info_init(&fch->cinfo, 1);
        } else {
            chunked_info_init(&fch->cinfo, 0);
        }

    } else if (fch->resp_cl >= 0) {
        fch->resp_already = 0;
    }

    fch->flow_buffer = TSIOBufferCreate();
    fch->flow_reader = TSIOBufferReaderAlloc(fch->flow_buffer);

    fch->body_buffer = TSIOBufferCreate();
    fch->body_reader = TSIOBufferReaderAlloc(fch->body_buffer);
}
static void
ts_lua_http_intercept_setup_write(ts_lua_http_intercept_ctx *ictx)
{
  ictx->output.buffer = TSIOBufferCreate();
  ictx->output.reader = TSIOBufferReaderAlloc(ictx->output.buffer);
  ictx->output.vio = TSVConnWrite(ictx->net_vc, ictx->contp, ictx->output.reader, INT64_MAX);
}
Exemplo n.º 4
0
http_fetcher *
ts_http_fetcher_create(TSCont contp, struct sockaddr *addr, int flags)
{
    http_fetcher    *fch;

    fch = (http_fetcher*)TSmalloc(sizeof(http_fetcher));

    fch->flags = flags;
    fch->contp = contp;
    fch->aip = *addr;

    if (flags & TS_FLAG_FETCH_USE_NEW_LOCK) {
        fch->mutexp = TSMutexCreate();
    } else {
        fch->mutexp = TSContMutexGet(contp);
    }

    fch->req_buffer = TSIOBufferCreate();
    fch->req_reader = TSIOBufferReaderAlloc(fch->req_buffer);

    /* initialize */
    fch->http_vc = NULL;
    fch->read_vio = NULL;
    fch->write_vio = NULL;
    fch->method = 0;

    fch->http_parser = NULL;
    fch->hdr_bufp = NULL;
    fch->hdr_loc = NULL;
    fch->hdr_buffer = NULL;
    fch->hdr_reader = NULL;
    fch->resp_buffer = NULL;
    fch->resp_reader = NULL;
    fch->body_buffer = NULL;
    fch->body_reader = NULL;
    fch->flow_buffer = NULL;
    fch->flow_reader = NULL;

    fch->fetch_contp = NULL;
    fch->action = NULL;

    fch->resp_cl = -1;
    fch->resp_already = 0;
    fch->post_cl = 0;
    fch->post_already = 0;

    fch->ctx = NULL;
    fch->status_code = 0;
    fch->ref = 0;

    fch->header_done = 0;
    fch->body_done = 0;
    fch->deleted = 0;
    fch->chunked = 0;
    fch->error = 0;
    fch->launched = 0;
    fch->stopped = 0;

    return fch;
}
Exemplo n.º 5
0
static void
stats_process_accept(TSCont contp, stats_state *my_state) {
	my_state->req_buffer = TSIOBufferCreate();
	my_state->resp_buffer = TSIOBufferCreate();
	my_state->resp_reader = TSIOBufferReaderAlloc(my_state->resp_buffer);
	my_state->read_vio = TSVConnRead(my_state->net_vc, contp, my_state->req_buffer, INT64_MAX);
}
Exemplo n.º 6
0
static int
transform_connect(TSCont contp, TransformData *data)
{
  TSAction action;
  int content_length;
  struct sockaddr_in ip_addr;

  data->state = STATE_CONNECT;

  content_length = TSIOBufferReaderAvail(data->input_reader);
  if (content_length >= 0) {
    data->content_length = content_length;
    data->content_length = htonl(data->content_length);

    /* Prepend the content length to the buffer.
     * If we decide to not send the content to the transforming
     * server then we need to make sure and skip input_reader
     * over the content length.
     */

    {
      TSIOBuffer temp;
      TSIOBufferReader tempReader;

      temp       = TSIOBufferCreate();
      tempReader = TSIOBufferReaderAlloc(temp);

      TSIOBufferWrite(temp, (const char *)&content_length, sizeof(int));
      TSIOBufferCopy(temp, data->input_reader, content_length, 0);

      TSIOBufferReaderFree(data->input_reader);
      TSIOBufferDestroy(data->input_buf);
      data->input_buf    = temp;
      data->input_reader = tempReader;
    }
  } else {
    TSError("[%s] TSIOBufferReaderAvail returns TS_ERROR", PLUGIN_NAME);
    return 0;
  }

  /* TODO: This only supports IPv4, probably should be changed at some point, but
     it's an example ... */
  memset(&ip_addr, 0, sizeof(ip_addr));
  ip_addr.sin_family      = AF_INET;
  ip_addr.sin_addr.s_addr = server_ip; /* Should be in network byte order */
  ip_addr.sin_port        = server_port;
  TSDebug(PLUGIN_NAME, "net connect.");
  action = TSNetConnect(contp, (struct sockaddr const *)&ip_addr);

  if (!TSActionDone(action)) {
    data->pending_action = action;
  }

  return 0;
}
Exemplo n.º 7
0
static int
transform_read_status(TSCont contp, TransformData *data)
{
  data->state = STATE_READ_STATUS;

  data->output_buf    = TSIOBufferCreate();
  data->output_reader = TSIOBufferReaderAlloc(data->output_buf);
  if (data->output_reader != NULL) {
    data->server_vio = TSVConnRead(data->server_vc, contp, data->output_buf, sizeof(int));
  } else {
    TSError("[%s] Error in Allocating a Reader to output buffer. TSIOBufferReaderAlloc returns NULL", PLUGIN_NAME);
  }

  return 0;
}
Exemplo n.º 8
0
static void process_hdr(ib_txn_ctx *data, TSHttpTxn txnp,
                        ironbee_direction *ibd)
{
  ib_conndata_t icdata;
  int rv;
  TSMBuffer bufp;
  TSMLoc hdr_loc;
  TSIOBuffer iobufp;
  TSIOBufferReader readerp;
  TSIOBufferBlock blockp;
  int64_t len;

  TSDebug("ironbee", "process %s headers\n", ibd->word);

  icdata.ib = ironbee;
  icdata.mp = data->ssn->iconn->mp;
  icdata.conn = data->ssn->iconn;

  /* before the HTTP headers comes the request line / response code */
  rv = (*ibd->hdr_get)(txnp, &bufp, &hdr_loc);
  if (rv) {
    TSError ("couldn't retrieve %s header: %d\n", ibd->word, rv);
    return;
  }

  /* Get the data into an IOBuffer so we can access them! */
  //iobufp = TSIOBufferSizedCreate(...);
  iobufp = TSIOBufferCreate();
  TSHttpHdrPrint(bufp, hdr_loc, iobufp);

  readerp = TSIOBufferReaderAlloc(iobufp);
  blockp = TSIOBufferReaderStart(readerp);

  len = TSIOBufferBlockReadAvail(blockp, readerp);
  icdata.data = (void*)TSIOBufferBlockReadStart(blockp, readerp, &len);
  icdata.dlen = icdata.dalloc = len;

  (*ibd->ib_notify)(ironbee, &icdata);

  TSIOBufferDestroy(iobufp);
  TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc);
}
static void
handle_transform(TSCont contp)
{
  TSVConn output_conn;
  TSVIO write_vio;
  MyData *data;
  int64_t towrite;

  /* Get the output connection where we'll write data to. */
  output_conn = TSTransformOutputVConnGet(contp);

  /* Get the write VIO for the write operation that was performed on
     ourself. This VIO contains the buffer that we are to read from
     as well as the continuation we are to call when the buffer is
     empty. */
  write_vio = TSVConnWriteVIOGet(contp);

  /* Get our data structure for this operation. The private data
     structure contains the output VIO and output buffer. If the
     private data structure pointer is NULL, then we'll create it
     and initialize its internals. */
  data = TSContDataGet(contp);
  if (!data) {
    towrite = TSVIONBytesGet(write_vio);
    if (towrite != INT64_MAX) {
      towrite += append_buffer_length;
    }
    data                = my_data_alloc();
    data->output_buffer = TSIOBufferCreate();
    data->output_reader = TSIOBufferReaderAlloc(data->output_buffer);
    data->output_vio    = TSVConnWrite(output_conn, contp, data->output_reader, towrite);
    TSContDataSet(contp, data);
  }

  /* We also check to see if the write VIO's buffer is non-NULL. A
     NULL buffer indicates that the write operation has been
     shutdown and that the continuation does not want us to send any
     more WRITE_READY or WRITE_COMPLETE events. For this simplistic
     transformation that means we're done. In a more complex
     transformation we might have to finish writing the transformed
     data to our output connection. */
  if (!TSVIOBufferGet(write_vio)) {
    if (data->append_needed) {
      data->append_needed = 0;
      TSIOBufferCopy(TSVIOBufferGet(data->output_vio), append_buffer_reader, append_buffer_length, 0);
    }

    TSVIONBytesSet(data->output_vio, TSVIONDoneGet(write_vio) + append_buffer_length);
    TSVIOReenable(data->output_vio);

    return;
  }

  /* Determine how much data we have left to read. For this append
     transform plugin this is also the amount of data we have left
     to write to the output connection. */
  towrite = TSVIONTodoGet(write_vio);
  if (towrite > 0) {
    /* The amount of data left to read needs to be truncated by
       the amount of data actually in the read buffer. */
    int64_t avail = TSIOBufferReaderAvail(TSVIOReaderGet(write_vio));
    if (towrite > avail) {
      towrite = avail;
    }

    if (towrite > 0) {
      /* Copy the data from the read buffer to the output buffer. */
      TSIOBufferCopy(TSVIOBufferGet(data->output_vio), TSVIOReaderGet(write_vio), towrite, 0);

      /* Tell the read buffer that we have read the data and are no
         longer interested in it. */
      TSIOBufferReaderConsume(TSVIOReaderGet(write_vio), towrite);

      /* Modify the write VIO to reflect how much data we've
         completed. */
      TSVIONDoneSet(write_vio, TSVIONDoneGet(write_vio) + towrite);
    }
  }

  /* Now we check the write VIO to see if there is data left to
     read. */
  if (TSVIONTodoGet(write_vio) > 0) {
    if (towrite > 0) {
      /* If there is data left to read, then we reenable the output
         connection by reenabling the output VIO. This will wakeup
         the output connection and allow it to consume data from the
         output buffer. */
      TSVIOReenable(data->output_vio);

      /* Call back the write VIO continuation to let it know that we
         are ready for more data. */
      TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_READY, write_vio);
    }
  } else {
    if (data->append_needed) {
      data->append_needed = 0;
      TSIOBufferCopy(TSVIOBufferGet(data->output_vio), append_buffer_reader, append_buffer_length, 0);
    }

    /* If there is no data left to read, then we modify the output
       VIO to reflect how much data the output connection should
       expect. This allows the output connection to know when it
       is done reading. We then reenable the output connection so
       that it can consume the data we just gave it. */
    TSVIONBytesSet(data->output_vio, TSVIONDoneGet(write_vio) + append_buffer_length);
    TSVIOReenable(data->output_vio);

    /* Call back the write VIO continuation to let it know that we
       have completed the write operation. */
    TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_COMPLETE, write_vio);
  }
}
Exemplo n.º 10
0
static int
handle_buffering(TSCont contp, MyData *data)
{
  TSVIO write_vio;
  int towrite;
  int avail;

  /* Get the write VIO for the write operation that was performed on
     ourself. This VIO contains the buffer that we are to read from
     as well as the continuation we are to call when the buffer is
     empty. */
  write_vio = TSVConnWriteVIOGet(contp);

  /* Create the output buffer and its associated reader */
  if (!data->output_buffer) {
    data->output_buffer = TSIOBufferCreate();
    TSAssert(data->output_buffer);
    data->output_reader = TSIOBufferReaderAlloc(data->output_buffer);
    TSAssert(data->output_reader);
  }

  /* We also check to see if the write VIO's buffer is non-NULL. A
     NULL buffer indicates that the write operation has been
     shutdown and that the continuation does not want us to send any
     more WRITE_READY or WRITE_COMPLETE events. For this buffered
     transformation that means we're done buffering data. */

  if (!TSVIOBufferGet(write_vio)) {
    data->state = STATE_OUTPUT_DATA;
    return 0;
  }

  /* Determine how much data we have left to read. For this bnull
     transform plugin this is also the amount of data we have left
     to write to the output connection. */

  towrite = TSVIONTodoGet(write_vio);
  if (towrite > 0) {
    /* The amount of data left to read needs to be truncated by
       the amount of data actually in the read buffer. */

    avail = TSIOBufferReaderAvail(TSVIOReaderGet(write_vio));
    if (towrite > avail) {
      towrite = avail;
    }

    if (towrite > 0) {
      /* Copy the data from the read buffer to the input buffer. */
      TSIOBufferCopy(data->output_buffer, TSVIOReaderGet(write_vio), towrite, 0);

      /* Tell the read buffer that we have read the data and are no
         longer interested in it. */
      TSIOBufferReaderConsume(TSVIOReaderGet(write_vio), towrite);

      /* Modify the write VIO to reflect how much data we've
         completed. */
      TSVIONDoneSet(write_vio, TSVIONDoneGet(write_vio) + towrite);
    }
  }

  /* Now we check the write VIO to see if there is data left to read. */
  if (TSVIONTodoGet(write_vio) > 0) {
    if (towrite > 0) {
      /* Call back the write VIO continuation to let it know that we
         are ready for more data. */
      TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_READY, write_vio);
    }
  } else {
    data->state = STATE_OUTPUT_DATA;

    /* Call back the write VIO continuation to let it know that we
       have completed the write operation. */
    TSContCall(TSVIOContGet(write_vio), TS_EVENT_VCONN_WRITE_COMPLETE, write_vio);
  }

  return 1;
}
Exemplo n.º 11
0
/**
 * Process data from ATS.
 *
 * Process data from one of the ATS events.
 *
 * @param[in,out] contp - the continuation
 * @param[in,out] ibd - the filter descriptor
 */
static void process_data(TSCont contp, ibd_ctx *ibd)
{
    int64_t ntodo;
    int64_t navail;
    TSIOBufferReader input_reader, output_reader;
    TSIOBufferBlock block;
    const char *buf;
    int64_t nbytes;
    ib_status_t rc;

    tsib_filter_ctx *fctx = ibd->data;

    tsib_txn_ctx *txndata = TSContDataGet(contp);
    TSVIO  input_vio = TSVConnWriteVIOGet(contp);
    TSIOBuffer in_buf = TSVIOBufferGet(input_vio);

    /* Test whether we're going into an errordoc */
    if (HTTP_CODE(txndata->status)) {  /* We're going to an error document,
                                        * so we discard all this data
                                        */
        ib_log_debug2_tx(txndata->tx, "Status is %d, discarding", txndata->status);
        ibd->data->buffering = IOBUF_DISCARD;
    }

    /* Test for EOS */
    if (in_buf == NULL) {
        if (fctx->output_buffer != NULL) {
            /* flush anything we have buffered.  This is final! */
            rc = flush_data(fctx, -1, 1);
            switch(rc) {
              case IB_OK:
                break;
              case IB_EBADVAL:
                ib_log_error_tx(txndata->tx, "Bad/Inconsistent stream edit(s) ignored.");
                break;
              default:  /* Can't happen unless a new status is introduced */
                ib_log_error_tx(txndata->tx, "Unhandled return value %d", rc);
                break;
            }
        }
        else {
            /* I guess NULL input may mean something other than EOS.
             * This appears to be possible when
             * processing an HTTP error from the backend.
             */
            ib_log_debug2_tx(txndata->tx, "Filter input was null.  No filtering.");
            /* RNS-1268: seems we may have to go through all the motions
             * of creating and enabling an output_vio with no data.
             */
            fctx->output_buffer = TSIOBufferCreate();
            ib_mm_register_cleanup(txndata->tx->mm,
                                   (ib_mm_cleanup_fn_t) TSIOBufferDestroy,
                                   (void*) fctx->output_buffer);
            output_reader = TSIOBufferReaderAlloc(fctx->output_buffer);
            fctx->output_vio = TSVConnWrite(TSTransformOutputVConnGet(contp), contp, output_reader, 0);
            TSVIOReenable(fctx->output_vio);
        }
        return;
    }

    /* Test for first time, and initialise.  */
    if (!fctx->output_buffer) {
        // FIXME - What to choose here and why?
        int64_t output_vio_sz = TSVIONBytesGet(input_vio);
        // NOTE: Using INT64_MAX asserts on 4.2.2: InkAPI.cc:6261: failed assert `sdk_sanity_check_iocore_structure(connp) == TS_SUCCESS`
        //int64_t output_vio_sz = INT64_MAX;
        // NOTE: Does it matter that this is only INT32_MAX as in the examples?
        // int64_t output_vio_sz = INT32_MAX;
        //int64_t output_vio_sz = fctx->have_edits
        //                        ? INT64_MAX
        //                        : TSVIONBytesGet(input_vio);
        fctx->output_buffer = TSIOBufferCreate();
        ib_mm_register_cleanup(txndata->tx->mm,
                               (ib_mm_cleanup_fn_t) TSIOBufferDestroy,
                               (void*) fctx->output_buffer);
        // FIXME - Where is TSIOBufferReaderFree()?
        output_reader = TSIOBufferReaderAlloc(fctx->output_buffer);
        fctx->output_vio = TSVConnWrite(TSTransformOutputVConnGet(contp), contp, output_reader, output_vio_sz);

        fctx->buffer = TSIOBufferCreate();
        ib_mm_register_cleanup(txndata->tx->mm,
                               (ib_mm_cleanup_fn_t) TSIOBufferDestroy,
                               (void*) fctx->buffer);
        // FIXME - Where is TSIOBufferReaderFree()?
        fctx->reader = TSIOBufferReaderAlloc(fctx->buffer);

        /* Get the buffering config */
        if (!HTTP_CODE(txndata->status)) {
            buffer_init(ibd, txndata->tx);
        }
    }

    /* Get any unprocessed bytes. */
    ntodo = TSVIONTodoGet(input_vio);

    /* Test for EOS */
    if (ntodo == 0) {
        ib_log_debug2_tx(txndata->tx, "ntodo zero before consuming data");

        flush_data(fctx, -1, 1);

        /* Call back the input VIO continuation to let it know that we
         * have completed the write operation.
         */
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
        return;
    }

    /* OK, there's some input awaiting our attention */
    input_reader = TSVIOReaderGet(input_vio);
    while (navail = TSIOBufferReaderAvail(input_reader), navail > 0) {
        block = TSIOBufferReaderStart(input_reader);
        buf = TSIOBufferBlockReadStart(block, input_reader, &nbytes);
        rc = (*ibd->ibd->ib_notify_body)(txndata->tx->ib, txndata->tx, buf, nbytes);
        if (rc != IB_OK) {
            ib_log_error_tx(txndata->tx, "Error %d notifying body data.", rc);
        }
        rc = buffer_data_chunk(fctx, input_reader, nbytes);
        switch (rc) {
          case IB_EAGAIN:
          case IB_OK:
            break;
          case IB_EBADVAL:
            ib_log_error_tx(txndata->tx, "Bad/Inconsistent stream edit(s) ignored.");
            break;
          default:  /* Can't happen unless a new status is introduced */
            ib_log_error_tx(txndata->tx, "Unhandled return value %d", rc);
            break;
        }
        TSIOBufferReaderConsume(input_reader, nbytes);
        TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + nbytes);
    }

    ntodo = TSVIONTodoGet(input_vio);
    if (ntodo == 0) {
        ib_log_debug2_tx(txndata->tx, "ntodo zero after consuming data");

        flush_data(fctx, -1, 1);

        /* Call back the input VIO continuation to let it know that we
         * have completed the write operation.
         */
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
    }
    else {
        /* Call back the input VIO continuation to let it know that we
         * are ready for more data.
         */
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);
    }
}
Exemplo n.º 12
0
static void
handle_dns(TSHttpTxn txnp, TSCont contp ATS_UNUSED)
{
  TSMBuffer bufp;
  TSMLoc hdr_loc;

  TSIOBuffer output_buffer;
  TSIOBufferReader reader;
  int total_avail;

  TSIOBufferBlock block;
  const char *block_start;
  int64_t block_avail;

  char *output_string;
  int64_t output_len;

  if (TSHttpTxnClientReqGet(txnp, &bufp, &hdr_loc) != TS_SUCCESS) {
    TSDebug(PLUGIN_NAME, "couldn't retrieve client request header");
    TSError("[%s] Couldn't retrieve client request header", PLUGIN_NAME);
    goto done;
  }

  output_buffer = TSIOBufferCreate();
  reader        = TSIOBufferReaderAlloc(output_buffer);

  /* This will print  just MIMEFields and not
     the http request line */
  TSDebug(PLUGIN_NAME, "Printing the hdrs ... ");
  TSMimeHdrPrint(bufp, hdr_loc, output_buffer);

  if (TSHandleMLocRelease(bufp, TS_NULL_MLOC, hdr_loc) == TS_ERROR) {
    TSDebug(PLUGIN_NAME, "non-fatal: error releasing MLoc");
    TSError("[%s] non-fatal: Couldn't release MLoc", PLUGIN_NAME);
  }

  /* 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 = TSIOBufferReaderAvail(reader);

  /* Allocate the string with an extra byte for the string
     terminator */
  output_string = (char *)TSmalloc(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 = TSIOBufferReaderStart(reader);
  while (block) {
    block_start = TSIOBufferBlockReadStart(block, reader, &block_avail);

    /* 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 */
    TSIOBufferReaderConsume(reader, block_avail);

    /* Get the next block now that we've consumed the
       data off the last block */
    block = TSIOBufferReaderStart(reader);
  }

  /* Terminate the string */
  output_string[output_len] = '\0';
  output_len++;

  /* Free up the TSIOBuffer that we used to print out the header */
  TSIOBufferReaderFree(reader);
  TSIOBufferDestroy(output_buffer);

  /* Although I'd never do this a production plugin, printf
     the header so that we can see it's all there */
  TSDebug(PLUGIN_NAME, "%s", output_string);

  TSfree(output_string);

done:
  TSHttpTxnReenable(txnp, TS_EVENT_HTTP_CONTINUE);
}
Exemplo n.º 13
0
static int
ts_lua_cache_open(lua_State *L)
{
    const char              *keystr, *optstr;
    size_t                  key_len, opt_len;
    int                     operate, n;
    TSCont                  contp;
    TSCacheKey              key;
    TSAction                action;
    ts_lua_cont_info        *ci;
    ts_lua_async_item       *ai;
    ts_lua_cache_info       *info;

    ci = ts_lua_get_cont_info(L);
    if (ci == NULL)
        return 0;

    n = lua_gettop(L);
    if (n < 2) {
        return luaL_error(L, "'ts.cache_open' requires parameter");
    }

    /* keystr */
    if (!lua_isstring(L, 1)) {
        return luaL_error(L, "'ts.cache_open' first param is not string");

    } else if (!lua_isnumber(L, 2)) {
        return luaL_error(L, "'ts.cache_open' second param is not TS_LUA_CACHE_READ/TS_LUA_CACHE_WRITE");
    }

    keystr = luaL_checklstring(L, 1, &key_len);
    operate = lua_tonumber(L, 2);

    if (operate != TS_LUA_CACHE_READ && operate != TS_LUA_CACHE_WRITE) {
        return luaL_error(L, "'ts.cache_open' second param is not TS_LUA_CACHE_READ/TS_LUA_CACHE_WRITE");
    }

    optstr = NULL;
    opt_len = 0;

    /* option */
    if (n >= 3 && lua_isstring(L, 3)) {
        optstr = luaL_checklstring(L, 3, &opt_len);
    }

    key = ts_lua_cache_key_create(keystr, key_len, optstr, opt_len);
    if (key == NULL) {
        return luaL_error(L, "'ts.cache_open' failed");
    }

    info = (ts_lua_cache_info*)TSmalloc(sizeof(ts_lua_cache_info));
    memset(info, 0, sizeof(ts_lua_cache_info));

    info->cache_key = key;
    info->optype = operate;  

    contp = TSContCreate(ts_lua_cache_main_handler, ci->mutex);
    ai = ts_lua_async_create_item(contp, ts_lua_cache_cleanup, info, ci);

    TSContDataSet(contp, ai);

    info->contp = contp;
    info->ioh.buffer = TSIOBufferCreate();
    info->ioh.reader = TSIOBufferReaderAlloc(info->ioh.buffer);

    if (operate == TS_LUA_CACHE_READ) {
        info->reserved.buffer = TSIOBufferCreate();
        info->reserved.reader = TSIOBufferReaderAlloc(info->reserved.buffer);
        info->current_handler = &ts_lua_cache_open_read;

        action = TSCacheRead(contp, key);

    } else {
        info->current_handler = &ts_lua_cache_open_write;
        action = TSCacheWrite(contp, key);
    }

    if (TSActionDone(action)) {     // done
        return 1;

    } else {                        // undone
        info->cache_action = action;
        return lua_yield(L, 0);
    }
}
Exemplo n.º 14
0
/*-------------------------------------------------------------------------
  handle_transform
   Get data from upstream vconn.
   Parse it.
   Include file if include tags found.
   Copy data to downstream vconn.
   Wake up upstream to get more data.

  Input:
    contp      continuation for the current transaction
  Output :
  Return Value:
   0 if failure
   1 if success
  -------------------------------------------------------------------------*/
static int
handle_transform(TSCont contp)
{
  TSVConn output_conn;
  TSVIO input_vio;
  ContData *data;
  TSIOBufferReader input_reader;
  int toread, avail, psi, toconsume, towrite;

  /* Get the output (downstream) vconnection where we'll write data to. */
  output_conn = TSTransformOutputVConnGet(contp);

  /* Get upstream vio */
  input_vio = TSVConnWriteVIOGet(contp);
  data = TSContDataGet(contp);
  TSAssert(data->magic == MAGIC_ALIVE);

  if (!data->output_buffer) {
    data->output_buffer = TSIOBufferCreate();
    data->output_reader = TSIOBufferReaderAlloc(data->output_buffer);

    /* INT64_MAX because we don't know yet how much bytes we'll produce */
    data->output_vio = TSVConnWrite(output_conn, contp, data->output_reader, INT64_MAX);
  }

  /* If the input VIO's buffer is NULL, the transformation is over */
  if (!TSVIOBufferGet(input_vio)) {
    TSDebug(DBG_TAG, "input_vio NULL, terminating transformation");
    TSVIONBytesSet(data->output_vio, data->transform_bytes);
    TSVIOReenable(data->output_vio);
    return 1;
  }

  /* Determine how much data we have left to read. */
  toread = TSVIONTodoGet(input_vio);

  if (toread > 0) {
    input_reader = TSVIOReaderGet(input_vio);
    avail = TSIOBufferReaderAvail(input_reader);

    /* There are some data available for reading. Let's parse it */
    if (avail > 0) {

      /* No need to parse data if there are too few bytes left to contain
         an include command... */
      if (toread > (PSI_START_TAG_LEN + PSI_END_TAG_LEN)) {
        psi = parse_data(contp, input_reader, avail, &toconsume, &towrite);
      } else {
        towrite = avail;
        toconsume = avail;
        psi = 0;
      }

      if (towrite > 0) {
        /* Update the total size of the doc so far */
        data->transform_bytes += towrite;

        /* Copy the data from the read buffer to the output buffer. */
        /* TODO: Should we check the return value of TSIOBufferCopy() ? */
        TSIOBufferCopy(TSVIOBufferGet(data->output_vio), TSVIOReaderGet(input_vio), towrite, 0);
        /* Reenable the output connection so it can read the data we've produced. */
        TSVIOReenable(data->output_vio);
      }

      if (toconsume > 0) {
        /* Consume data we've processed an we are no longer interested in */
        TSIOBufferReaderConsume(input_reader, toconsume);

        /* Modify the input VIO to reflect how much data we've completed. */
        TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + toconsume);
      }

      /* Did we find a psi filename to execute in the data ? */
      if (psi) {
        Job *new_job;
        /* Add a request to include a file into the jobs queue.. */
        /* We'll be called back once it's done with an EVENT_IMMEDIATE */
        TSDebug(DBG_TAG, "Psi filename extracted. Adding an include job to thread queue.");
        data->state = STATE_READ_PSI;

        /* Create a new job request and add it to the queue */
        new_job = job_create(contp, &psi_include, NULL);
        add_to_queue(&job_queue, new_job);

        /* Signal to the threads there is a new job */
        thread_signal_job();

        return 1;
      }
    }
  }

  /* Wake up upstream and downstream vconnections */
  wake_up_streams(contp);

  return 1;
}
static int
ts_lua_transform_handler(TSCont contp, ts_lua_http_transform_ctx *transform_ctx, TSEvent event, int n)
{
  TSVConn output_conn;
  TSVIO input_vio;
  TSIOBufferReader input_reader;
  TSIOBufferBlock blk;
  int64_t toread, towrite, blk_len, upstream_done, input_avail, input_wm_bytes, l;
  const char *start;
  const char *res;
  size_t res_len;
  int ret, eos, write_down, rc, top, empty_input;
  ts_lua_coroutine *crt;
  ts_lua_cont_info *ci;

  lua_State *L;
  TSMutex mtxp;

  ci  = &transform_ctx->cinfo;
  crt = &ci->routine;

  mtxp = crt->mctx->mutexp;
  L    = crt->lua;

  output_conn = TSTransformOutputVConnGet(contp);
  input_vio   = TSVConnWriteVIOGet(contp);

  empty_input = 0;
  if (!TSVIOBufferGet(input_vio)) {
    if (transform_ctx->output.vio) {
      TSDebug(TS_LUA_DEBUG_TAG, "[%s] reenabling output VIO after input VIO does not exist", __FUNCTION__);
      TSVIONBytesSet(transform_ctx->output.vio, transform_ctx->total);
      TSVIOReenable(transform_ctx->output.vio);
      return 0;
    } else {
      TSDebug(TS_LUA_DEBUG_TAG, "[%s] no input VIO and output VIO", __FUNCTION__);
      empty_input = 1;
    }
  } else { // input VIO exists
    input_wm_bytes = TSIOBufferWaterMarkGet(TSVIOBufferGet(input_vio));
    if (transform_ctx->upstream_watermark_bytes >= 0 && transform_ctx->upstream_watermark_bytes != input_wm_bytes) {
      TSDebug(TS_LUA_DEBUG_TAG, "[%s] Setting input_vio watermark to %" PRId64 " bytes", __FUNCTION__,
              transform_ctx->upstream_watermark_bytes);
      TSIOBufferWaterMarkSet(TSVIOBufferGet(input_vio), transform_ctx->upstream_watermark_bytes);
    }
  }

  if (empty_input == 0) {
    input_reader = TSVIOReaderGet(input_vio);
  }

  if (!transform_ctx->output.buffer) {
    transform_ctx->output.buffer = TSIOBufferCreate();
    transform_ctx->output.reader = TSIOBufferReaderAlloc(transform_ctx->output.buffer);

    transform_ctx->reserved.buffer = TSIOBufferCreate();
    transform_ctx->reserved.reader = TSIOBufferReaderAlloc(transform_ctx->reserved.buffer);

    if (empty_input == 0) {
      transform_ctx->upstream_bytes = TSVIONBytesGet(input_vio);
    } else {
      transform_ctx->upstream_bytes = 0;
    }

    transform_ctx->downstream_bytes = INT64_MAX;
  }

  if (empty_input == 0) {
    input_avail   = TSIOBufferReaderAvail(input_reader);
    upstream_done = TSVIONDoneGet(input_vio);
    toread        = TSVIONTodoGet(input_vio);

    if (toread <= input_avail) { // upstream finished
      eos = 1;
    } else {
      eos = 0;
    }
  } else {
    input_avail = 0;
    toread      = 0;
    eos         = 1;
  }

  if (input_avail > 0) {
    // move to the reserved.buffer
    TSIOBufferCopy(transform_ctx->reserved.buffer, input_reader, input_avail, 0);

    // reset input
    TSIOBufferReaderConsume(input_reader, input_avail);
    TSVIONDoneSet(input_vio, upstream_done + input_avail);
  }

  write_down = 0;
  if (empty_input == 0) {
    towrite = TSIOBufferReaderAvail(transform_ctx->reserved.reader);
  } else {
    towrite = 0;
  }

  TSMutexLock(mtxp);
  ts_lua_set_cont_info(L, ci);

  do {
    if (event == TS_LUA_EVENT_COROUTINE_CONT) {
      event = 0;
      goto launch;
    } else {
      n = 2;
    }

    if (towrite == 0 && empty_input == 0) {
      break;
    }

    if (empty_input == 0) {
      blk   = TSIOBufferReaderStart(transform_ctx->reserved.reader);
      start = TSIOBufferBlockReadStart(blk, transform_ctx->reserved.reader, &blk_len);

      lua_pushlightuserdata(L, transform_ctx);
      lua_rawget(L, LUA_GLOBALSINDEX); /* push function */

      if (towrite > blk_len) {
        lua_pushlstring(L, start, (size_t)blk_len);
        towrite -= blk_len;
        TSIOBufferReaderConsume(transform_ctx->reserved.reader, blk_len);
      } else {
        lua_pushlstring(L, start, (size_t)towrite);
        TSIOBufferReaderConsume(transform_ctx->reserved.reader, towrite);
        towrite = 0;
      }

      if (!towrite && eos) {
        lua_pushinteger(L, 1); /* second param, data finished */
      } else {
        lua_pushinteger(L, 0); /* second param, data not finish */
      }
    } else {
      lua_pushlightuserdata(L, transform_ctx);
      lua_rawget(L, LUA_GLOBALSINDEX); /* push function */

      lua_pushlstring(L, "", 0);
      lua_pushinteger(L, 1); /* second param, data finished */
    }

  launch:
    rc  = lua_resume(L, n);
    top = lua_gettop(L);

    switch (rc) {
    case LUA_YIELD: // coroutine yield
      TSMutexUnlock(mtxp);
      return 0;

    case 0: // coroutine success
      if (top == 2) {
        ret = lua_tointeger(L, -1); /* 0 is not finished, 1 is finished */
        res = lua_tolstring(L, -2, &res_len);
      } else { // what hells code are you writing ?
        ret     = 0;
        res     = NULL;
        res_len = 0;
      }
      break;

    default: // coroutine failed
      TSError("[ts_lua] lua_resume failed: %s", lua_tostring(L, -1));
      ret     = 1;
      res     = NULL;
      res_len = 0;
      break;
    }

    if (res && res_len > 0) {
      if (!transform_ctx->output.vio) {
        l = transform_ctx->downstream_bytes;
        if (ret) {
          l = res_len;
        }

        transform_ctx->output.vio = TSVConnWrite(output_conn, contp, transform_ctx->output.reader, l); // HttpSM go on
      }

      TSIOBufferWrite(transform_ctx->output.buffer, res, res_len);
      transform_ctx->total += res_len;
      write_down = 1;
    }

    lua_pop(L, top);

    if (ret || (eos && !towrite)) { // EOS
      eos = 1;
      break;
    }

  } while (towrite > 0);

  TSMutexUnlock(mtxp);

  if (eos && !transform_ctx->output.vio) {
    transform_ctx->output.vio = TSVConnWrite(output_conn, contp, transform_ctx->output.reader, 0);
  }

  if (write_down || eos) {
    TSVIOReenable(transform_ctx->output.vio);
  }

  if (toread > input_avail) { // upstream not finished.
    if (eos) {
      TSVIONBytesSet(transform_ctx->output.vio, transform_ctx->total);
      if (empty_input == 0) {
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_EOS, input_vio);
      }
    } else {
      if (empty_input == 0) {
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);
      }
    }
  } else { // upstream is finished.
    TSVIONBytesSet(transform_ctx->output.vio, transform_ctx->total);
    if (empty_input == 0) {
      TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
    }
  }

  return 0;
}
Exemplo n.º 16
0
static void process_data(TSCont contp, ibd_ctx* ibd)
{
  TSVConn output_conn;
  TSIOBuffer buf_test;
  TSVIO input_vio;
  ib_txn_ctx *data;
  int64_t towrite;
  int64_t avail;
  int first_time = 0;
  char *bufp = NULL;

  TSDebug("ironbee", "Entering process_data()");
  /* Get the output (downstream) vconnection where we'll write data to. */

  output_conn = TSTransformOutputVConnGet(contp);

  /* Get the write VIO for the write operation that was performed on
   * ourself. This VIO contains the buffer that we are to read from
   * as well as the continuation we are to call when the buffer is
   * empty. This is the input VIO (the write VIO for the upstream
   * vconnection).
   */
  input_vio = TSVConnWriteVIOGet(contp);

  data = TSContDataGet(contp);
  if (!ibd->data->output_buffer) {
    first_time = 1;

    ibd->data->output_buffer = TSIOBufferCreate();
    ibd->data->output_reader = TSIOBufferReaderAlloc(ibd->data->output_buffer);
    TSDebug("ironbee", "\tWriting %d bytes on VConn", TSVIONBytesGet(input_vio));
    ibd->data->output_vio = TSVConnWrite(output_conn, contp, ibd->data->output_reader, INT64_MAX);
  }
  if (ibd->data->buf) {
    /* this is the second call to us, and we have data buffered.
     * Feed buffered data to ironbee
     */
        ib_conndata_t icdata;
          icdata.ib = ironbee;
          icdata.mp = data->ssn->iconn->mp;
          icdata.conn = data->ssn->iconn;
          icdata.dalloc = ibd->data->buflen;
          icdata.dlen = ibd->data->buflen;
          icdata.data = (uint8_t *)ibd->data->buf;
          (*ibd->ibd->ib_notify)(ironbee, &icdata);
    TSfree(ibd->data->buf);
    ibd->data->buf = NULL;
    ibd->data->buflen = 0;
  }

  /* test for input data */
  buf_test = TSVIOBufferGet(input_vio);

  if (!buf_test) {
    TSDebug("ironbee", "No more data, finishing");
    TSVIONBytesSet(ibd->data->output_vio, TSVIONDoneGet(input_vio));
    TSVIOReenable(ibd->data->output_vio);
    /* FIXME - is this right here - can conn data be kept across reqs? */
    ibd->data->output_buffer = NULL;
    ibd->data->output_reader = NULL;
    ibd->data->output_vio = NULL;
    return;
  }

  /* Determine how much data we have left to read. For this null
   * transform plugin this is also the amount of data we have left
   * to write to the output connection.
   */
  towrite = TSVIONTodoGet(input_vio);
  TSDebug("ironbee", "\ttoWrite is %" PRId64 "", towrite);

  if (towrite > 0) {
    /* The amount of data left to read needs to be truncated by
     * the amount of data actually in the read buffer.
     */

    /* first time through, we have to buffer the data until
     * after the headers have been sent.  Ugh!
     */
    if (first_time) {
      bufp = ibd->data->buf = TSmalloc(towrite);
      ibd->data->buflen = towrite;
    }
    
    avail = TSIOBufferReaderAvail(TSVIOReaderGet(input_vio));
    TSDebug("ironbee", "\tavail is %" PRId64 "", avail);
    if (towrite > avail) {
      towrite = avail;
    }

    if (towrite > 0) {
      int btowrite = towrite;
      /* Copy the data from the read buffer to the output buffer. */
      TSIOBufferCopy(TSVIOBufferGet(ibd->data->output_vio), TSVIOReaderGet(input_vio), towrite, 0);

      /* feed the data to ironbee, and consume them */
      while (btowrite > 0) {
        ib_conndata_t icdata;
        int64_t ilength;
        TSIOBufferReader input_reader = TSVIOReaderGet(input_vio);
        TSIOBufferBlock blkp = TSIOBufferReaderStart(input_reader);
        const char *ibuf = TSIOBufferBlockReadStart(blkp, input_reader, &ilength);

        /* feed it to ironbee or to buffer */
        if (first_time) {
          memcpy(bufp, ibuf, ilength);
          bufp += ilength;
        }
        else {
          icdata.ib = ironbee;
          icdata.mp = data->ssn->iconn->mp;
          icdata.conn = data->ssn->iconn;
          icdata.dalloc = ilength;
          icdata.dlen = ilength;
          icdata.data = (uint8_t *)ibuf;
          (*ibd->ibd->ib_notify)(ironbee, &icdata);
        }
  //"response", TSHttpTxnClientRespGet, ib_state_notify_conn_data_out
  //      ib_state_notify_conn_data_out(ironbee, &icdata);

        /* and mark it as all consumed */
        btowrite -= ilength;
        TSIOBufferReaderConsume(input_reader, ilength);
        TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + ilength);
      }
    }
  }

  /* Now we check the input VIO to see if there is data left to
   * read.
   */
  if (TSVIONTodoGet(input_vio) > 0) {
    if (towrite > 0) {
      /* If there is data left to read, then we reenable the output
       * connection by reenabling the output VIO. This will wake up
       * the output connection and allow it to consume data from the
       * output buffer.
       */
      TSVIOReenable(ibd->data->output_vio);

      /* Call back the input VIO continuation to let it know that we
       * are ready for more data.
       */
      TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);
    }
  } else {
    /* If there is no data left to read, then we modify the output
     * VIO to reflect how much data the output connection should
     * expect. This allows the output connection to know when it
     * is done reading. We then reenable the output connection so
     * that it can consume the data we just gave it.
     */
    TSVIONBytesSet(ibd->data->output_vio, TSVIONDoneGet(input_vio));
    TSVIOReenable(ibd->data->output_vio);

    /* Call back the input VIO continuation to let it know that we
     * have completed the write operation.
     */
    TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
  }
}
Exemplo n.º 17
0
static int
ts_lua_transform_handler(TSCont contp, ts_lua_transform_ctx *transform_ctx)
{
    TSVConn             output_conn;
    TSVIO               input_vio;
    TSIOBufferReader    input_reader;
    TSIOBufferBlock     blk;
    int64_t             towrite, blk_len, upstream_done, avail, left;
    const char          *start;
    const char          *res;
    size_t              res_len;
    int                 ret, eos;

    lua_State           *L;
    TSMutex             mtxp;

    L = transform_ctx->hctx->lua;
    mtxp = transform_ctx->hctx->mctx->mutexp;

    output_conn = TSTransformOutputVConnGet(contp);
    input_vio = TSVConnWriteVIOGet(contp);
    input_reader = TSVIOReaderGet(input_vio);

    if (!transform_ctx->output_buffer) {
        transform_ctx->output_buffer = TSIOBufferCreate();
        transform_ctx->output_reader = TSIOBufferReaderAlloc(transform_ctx->output_buffer);
        transform_ctx->output_vio = TSVConnWrite(output_conn, contp, transform_ctx->output_reader, INT64_MAX);
    }

    if (!TSVIOBufferGet(input_vio)) {
        TSVIONBytesSet(transform_ctx->output_vio, transform_ctx->total);
        TSVIOReenable(transform_ctx->output_vio);
        return 1;
    }

    if (transform_ctx->eos) {
        return 1;
    }

    left = towrite = TSVIONTodoGet(input_vio);
    upstream_done = TSVIONDoneGet(input_vio);
    avail = TSIOBufferReaderAvail(input_reader);
    eos = 0;

    if (left <= avail)
        eos = 1;

    if (towrite > avail)
        towrite = avail;

    TSMutexLock(mtxp);

    blk = TSIOBufferReaderStart(input_reader);

    do {
        start = TSIOBufferBlockReadStart(blk, input_reader, &blk_len);

        lua_pushlightuserdata(L, transform_ctx);
        lua_rawget(L, LUA_GLOBALSINDEX);                /* push function */

        if (towrite > blk_len) {
            lua_pushlstring(L, start, (size_t)blk_len);
            towrite -= blk_len;
        } else {
            lua_pushlstring(L, start, (size_t)towrite);
            towrite = 0;
        }

        if (!towrite && eos) {
            lua_pushinteger(L, 1);                          /* second param, not finish */ 
        } else {
            lua_pushinteger(L, 0);                          /* second param, not finish */ 
        }

        if (lua_pcall(L, 2, 2, 0)) {
            fprintf(stderr, "lua_pcall failed: %s\n", lua_tostring(L, -1));
        }

        ret = lua_tointeger(L, -1);                         /* 0 is not finished, 1 is finished */
        res = lua_tolstring(L, -2, &res_len);

        if (res && res_len) {
            TSIOBufferWrite(transform_ctx->output_buffer, res, res_len);
            transform_ctx->total += res_len;
        }

        lua_pop(L, 2);

        if (ret || (eos && !towrite)) {            // EOS
            eos = 1;
            break;
        }

        blk = TSIOBufferBlockNext(blk);

    } while (blk && towrite > 0);

    TSMutexUnlock(mtxp);

    TSIOBufferReaderConsume(input_reader, avail);
    TSVIONDoneSet(input_vio, upstream_done + avail);

    if (eos) {
        transform_ctx->eos = 1;
        TSVIONBytesSet(transform_ctx->output_vio, transform_ctx->total);
        TSVIOReenable(transform_ctx->output_vio);
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
    } else {
        TSVIOReenable(transform_ctx->output_vio);
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);
    }

    return 1;
}
static int
handle_buffering(TSCont contp, JCrusherData * data)
{
  TSVIO upstream_vio;
  TSIOBuffer upstream_buffer;
  int64_t toread;
  int64_t avail;

  TSDebug("jcrusher", "Start of handle_buffering()");
  /* Get the write VIO for the write operation that was performed on
     ourself. This VIO contains the buffer that we are to read from
     as well as the continuation we are to call when the buffer is
     empty. */
  upstream_vio = TSVConnWriteVIOGet(contp);

  /* Create the output buffer and its associated reader */
  if (!data->downstream_buffer) {
    data->downstream_buffer = TSIOBufferCreate();
    TSAssert(data->downstream_buffer);
    data->downstream_reader = TSIOBufferReaderAlloc(data->downstream_buffer);
    TSAssert(data->downstream_reader);
  }

  /* We also check to see if the write VIO's buffer is non-NULL. A
     NULL buffer indicates that the write operation has been
     shutdown and that the continuation does not want us to send any
     more WRITE_READY or WRITE_COMPLETE events. For this buffered
     transformation that means we're done buffering data. */

  upstream_buffer = TSVIOBufferGet(upstream_vio);
  if (NULL == upstream_buffer) {
    data->state = STATE_OUTPUT_DATA;
    TSDebug("jcrusher", "handle_buffering - upstream_buffer is NULL");
    return 0;
  }

  /* Determine how much data we have left to read. For this bnull
     transform plugin this is also the amount of data we have left
     to write to the output connection. */

  toread = TSVIONTodoGet(upstream_vio);
  TSDebug("jcrusher", "handle_buffering - toread is %" PRId64, toread);
  if (toread > 0) {
    /* The amount of data left to read needs to be truncated by
       the amount of data actually in the read buffer. */

    avail = TSIOBufferReaderAvail(TSVIOReaderGet(upstream_vio));
    if (toread > avail) {
      toread = avail;
    }
    TSDebug("jcrusher", "handle_buffering - toread is %" PRId64, toread);
    TSDebug("jcrusher", "handle_buffering - avail is %" PRId64, avail);

    TSIOBufferReader upstream_reader = TSVIOReaderGet(upstream_vio);
    TSIOBufferBlock upstream_blk = TSIOBufferReaderStart(upstream_reader);
    const char *input = TSIOBufferBlockReadStart(upstream_blk, upstream_reader, &toread);
    TSDebug("jcrusher", "handle_buffering - just read [%d] bytes from buffer", (int)strlen(input));

    TSDebug("jcrusher", "handle_buffering - parse json input");
    data->json_obj = json_tokener_parse_ex(data->json_tok, input, strlen(input));
    if (json_tokener_success == (data->json_err = json_tokener_get_error(data->json_tok))) {
      TSDebug("jcrusher", "handle_buffering - got json_tokener_success");
      data->state = STATE_OUTPUT_DATA;
      /* Call back the write VIO continuation to let it know that we
         have completed the write operation. */
      TSContCall(TSVIOContGet(upstream_vio), TS_EVENT_VCONN_WRITE_COMPLETE, upstream_vio);
      return 1;
    }
    TSDebug("jcrusher", "handle_buffering - got json_tokener_continue");

    /* Tell the read buffer that we have read the data and are no
       longer interested in it. */
    TSIOBufferReaderConsume(TSVIOReaderGet(upstream_vio), toread);

    /* Modify the upstream VIO to reflect how much data we've
       completed. */
    TSVIONDoneSet(upstream_vio, TSVIONDoneGet(upstream_vio) + toread);

    /* Now we check the upstream VIO to see if there is data left to read. */
    /* Call back the upstream VIO continuation to let it know that we
       are ready for more data. */
    TSContCall(TSVIOContGet(upstream_vio), TS_EVENT_VCONN_WRITE_READY, upstream_vio);
  } else {
    TSDebug("jcrusher", "handle_buffering - seems we read all");
    data->state = STATE_OUTPUT_DATA;
    /* Call back the write VIO continuation to let it know that we
       have completed the write operation. */
    TSContCall(TSVIOContGet(upstream_vio), TS_EVENT_VCONN_WRITE_COMPLETE, upstream_vio);
  }

  TSDebug("jcrusher", "handle_buffering - End");
  return 1;
}
Exemplo n.º 19
0
static int
ts_dechunk_transform_handler(TSCont contp, ts_dechunk_transform_ctx *tc)
{
    TSVConn             output_conn;
    TSVIO               input_vio;
    TSIOBufferReader    input_reader;
    int64_t             towrite, upstream_done, avail;
    int                 ret, eos;

    output_conn = TSTransformOutputVConnGet(contp);
    input_vio = TSVConnWriteVIOGet(contp);
    input_reader = TSVIOReaderGet(input_vio);

    if (!tc->output_buffer) {
        tc->output_buffer = TSIOBufferCreate();
        tc->output_reader = TSIOBufferReaderAlloc(tc->output_buffer);
        tc->output_vio = TSVConnWrite(output_conn, contp, tc->output_reader, INT64_MAX);
    }

    if (!TSVIOBufferGet(input_vio)) {
        TSVIONBytesSet(tc->output_vio, tc->total);
        TSVIOReenable(tc->output_vio);
        return 1;
    }

    towrite = TSVIONTodoGet(input_vio);
    upstream_done = TSVIONDoneGet(input_vio);

    avail = TSIOBufferReaderAvail(input_reader);

    if (towrite > avail) {
        towrite = avail;
        eos = 0;

    } else {
        eos = 1;
    }

    ret = ts_dechunk_process(tc->info, input_reader, tc->output_buffer, eos);

    if (ret < 0) {
        tc->total = TSVIONDoneGet(tc->output_vio) + TSIOBufferReaderAvail(tc->output_reader);
        TSVIONBytesSet(tc->output_vio, tc->total);
        TSVIOReenable(tc->output_vio);
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_ERROR, input_vio);
        return ret;
    }

    if (!ret) {
        TSVIOReenable(tc->output_vio);
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);

    } else {

        tc->total = TSVIONDoneGet(tc->output_vio) + TSIOBufferReaderAvail(tc->output_reader);
        TSVIONBytesSet(tc->output_vio, tc->total);
        TSVIOReenable(tc->output_vio);
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
    }

    return 1;
}
Exemplo n.º 20
0
static void
handle_transform(TSCont contp)
{
  TSVConn output_conn;
  TSIOBuffer buf_test;
  TSVIO input_vio;
  MyData *data;
  int64_t towrite;
  int64_t avail;

  TSDebug("null-transform", "Entering handle_transform()");
  /* Get the output (downstream) vconnection where we'll write data to. */

  output_conn = TSTransformOutputVConnGet(contp);

  /* Get the write VIO for the write operation that was performed on
   * ourself. This VIO contains the buffer that we are to read from
   * as well as the continuation we are to call when the buffer is
   * empty. This is the input VIO (the write VIO for the upstream
   * vconnection).
   */
  input_vio = TSVConnWriteVIOGet(contp);

  /* Get our data structure for this operation. The private data
   * structure contains the output VIO and output buffer. If the
   * private data structure pointer is NULL, then we'll create it
   * and initialize its internals.
   */
  data = TSContDataGet(contp);
  if (!data) {
    data = my_data_alloc();
    data->output_buffer = TSIOBufferCreate();
    data->output_reader = TSIOBufferReaderAlloc(data->output_buffer);
    TSDebug("null-transform", "\tWriting %" PRId64 " bytes on VConn", TSVIONBytesGet(input_vio));
    // data->output_vio = TSVConnWrite(output_conn, contp, data->output_reader, INT32_MAX);
    data->output_vio = TSVConnWrite(output_conn, contp, data->output_reader, INT64_MAX);
    // data->output_vio = TSVConnWrite(output_conn, contp, data->output_reader, TSVIONBytesGet(input_vio));
    TSContDataSet(contp, data);
  }

  /* We also check to see if the input VIO's buffer is non-NULL. A
   * NULL buffer indicates that the write operation has been
   * shutdown and that the upstream continuation does not want us to send any
   * more WRITE_READY or WRITE_COMPLETE events. For this simplistic
   * transformation that means we're done. In a more complex
   * transformation we might have to finish writing the transformed
   * data to our output connection.
   */
  buf_test = TSVIOBufferGet(input_vio);

  if (!buf_test) {
    TSVIONBytesSet(data->output_vio, TSVIONDoneGet(input_vio));
    TSVIOReenable(data->output_vio);
    return;
  }

  /* Determine how much data we have left to read. For this null
   * transform plugin this is also the amount of data we have left
   * to write to the output connection.
   */
  towrite = TSVIONTodoGet(input_vio);
  TSDebug("null-transform", "\ttoWrite is %" PRId64 "", towrite);

  if (towrite > 0) {
    /* The amount of data left to read needs to be truncated by
     * the amount of data actually in the read buffer.
     */
    avail = TSIOBufferReaderAvail(TSVIOReaderGet(input_vio));
    TSDebug("null-transform", "\tavail is %" PRId64 "", avail);
    if (towrite > avail) {
      towrite = avail;
    }

    if (towrite > 0) {
      /* Copy the data from the read buffer to the output buffer. */
      TSIOBufferCopy(TSVIOBufferGet(data->output_vio), TSVIOReaderGet(input_vio), towrite, 0);

      /* Tell the read buffer that we have read the data and are no
       * longer interested in it.
       */
      TSIOBufferReaderConsume(TSVIOReaderGet(input_vio), towrite);

      /* Modify the input VIO to reflect how much data we've
       * completed.
       */
      TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + towrite);
    }
  }

  /* Now we check the input VIO to see if there is data left to
   * read.
   */
  if (TSVIONTodoGet(input_vio) > 0) {
    if (towrite > 0) {
      /* If there is data left to read, then we reenable the output
       * connection by reenabling the output VIO. This will wake up
       * the output connection and allow it to consume data from the
       * output buffer.
       */
      TSVIOReenable(data->output_vio);

      /* Call back the input VIO continuation to let it know that we
       * are ready for more data.
       */
      TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);
    }
  } else {
    /* If there is no data left to read, then we modify the output
     * VIO to reflect how much data the output connection should
     * expect. This allows the output connection to know when it
     * is done reading. We then reenable the output connection so
     * that it can consume the data we just gave it.
     */
    TSVIONBytesSet(data->output_vio, TSVIONDoneGet(input_vio));
    TSVIOReenable(data->output_vio);

    /* Call back the input VIO continuation to let it know that we
     * have completed the write operation.
     */
    TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
  }
}
Exemplo n.º 21
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
  -------------------------------------------------------------------------*/
static int
psi_include(TSCont contp, void *edata)
{
#define BUFFER_SIZE 1024
  ContData *data;
  TSFile filep;
  char buf[BUFFER_SIZE];
  char inc_file[PSI_PATH_MAX_SIZE + PSI_FILENAME_MAX_SIZE];

  /* We manipulate plugin continuation data from a separate thread.
     Grab mutex to avoid concurrent access */
  TSMutexLock(TSContMutexGet(contp));
  data = TSContDataGet(contp);
  TSAssert(data->magic == MAGIC_ALIVE);

  if (!data->psi_buffer) {
    data->psi_buffer = TSIOBufferCreate();
    data->psi_reader = TSIOBufferReaderAlloc(data->psi_buffer);
  }

  /* 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 = TSfopen(inc_file, "r")) != NULL) {
    TSDebug(DBG_TAG, "Reading include file %s", inc_file);

    while (TSfgets(filep, buf, BUFFER_SIZE) != NULL) {
      TSIOBufferBlock block;
      int64_t len, avail, ndone, ntodo, towrite;
      char *ptr_block;

      len = strlen(buf);
      ndone = 0;
      ntodo = len;
      while (ntodo > 0) {
        /* TSIOBufferStart allocates more blocks if required */
        block = TSIOBufferStart(data->psi_buffer);
        ptr_block = TSIOBufferBlockWriteStart(block, &avail);
        towrite = MIN(ntodo, avail);

        memcpy(ptr_block, buf + ndone, towrite);
        TSIOBufferProduce(data->psi_buffer, towrite);
        ntodo -= towrite;
        ndone += towrite;
      }
    }
    TSfclose(filep);
    data->psi_success = 1;
    if (log) {
      TSTextLogObjectWrite(log, "Successfully included file: %s", inc_file);
    }
  } else {
    data->psi_success = 0;
    if (log) {
      TSTextLogObjectWrite(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
     TS_HTTP_READ_REQUEST_HDR, TS_HTTP_OS_DNS and so on...) we could
     use TSHttpTxnReenable to wake up the transaction instead of sending an event. */

  TSContSchedule(contp, 0, TS_THREAD_POOL_DEFAULT);
  data->psi_success = 0;
  data->state = STATE_READ_DATA;
  TSMutexUnlock(TSContMutexGet(contp));

  return 0;
}
Exemplo n.º 22
0
/**
 * Process data from ATS.
 *
 * Process data from one of the ATS events.
 *
 * @param[in,out] contp - the continuation
 * @param[in,out] ibd - the filter descriptor
 */
static void process_data(TSCont contp, ibd_ctx *ibd)
{
    int64_t ntodo;
    int64_t navail;
    TSIOBufferReader input_reader, output_reader;
    TSIOBufferBlock block;
    const char *buf;
    int64_t nbytes;
    ib_status_t rc;

    ib_filter_ctx *fctx = ibd->data;

    ib_txn_ctx *data = TSContDataGet(contp);
    TSVIO  input_vio = TSVConnWriteVIOGet(contp);
    TSIOBuffer in_buf = TSVIOBufferGet(input_vio);

    /* Test whether we're going into an errordoc */
    if (IB_HTTP_CODE(data->status)) {  /* We're going to an error document,
                                        * so we discard all this data
                                        */
        TSDebug("ironbee", "Status is %d, discarding", data->status);
        ibd->data->buffering = IOBUF_DISCARD;
    }

    /* Test for EOS */
    if (in_buf == NULL) {
        /* flush anything we have buffered.  This is final! */
        flush_data(fctx, -1, 1);
        return;
    }

    ntodo = TSVIONTodoGet(input_vio);

    /* Test for first time, and initialise.  */
    if (!fctx->output_buffer) {
        fctx->output_buffer = TSIOBufferCreate();
        ib_mm_register_cleanup(data->tx->mm,
                               (ib_mm_cleanup_fn_t) TSIOBufferDestroy,
                               (void*) fctx->output_buffer);
        output_reader = TSIOBufferReaderAlloc(fctx->output_buffer);
        fctx->output_vio = TSVConnWrite(TSTransformOutputVConnGet(contp), contp, output_reader, TSVIONBytesGet(input_vio));

        fctx->buffer = TSIOBufferCreate();
        ib_mm_register_cleanup(data->tx->mm,
                               (ib_mm_cleanup_fn_t) TSIOBufferDestroy,
                               (void*) fctx->buffer);
        fctx->reader = TSIOBufferReaderAlloc(fctx->buffer);

        /* Get the buffering config */
        if (!IB_HTTP_CODE(data->status)) {
            buffer_init(ibd, data->tx);
        }

/* Do we still have to delay feeding the first data to Ironbee
 * to keep the IB events in their proper order?
 *
 * Appears maybe not, so let's do nothing until it shows signs of breakage.
 */
#if BUFFER_FIRST
        /* First time through we can only buffer data until headers are sent. */
        fctx->first_time = 1;
        input_reader = TSVIOReaderGet(input_vio);
        fctx->buffered = TSIOBufferCopy(fctx->buffer, TSVIOReaderGet(input_vio),
                                        ntodo, 0);
        TSIOBufferReaderConsume(input_reader, fctx->buffered);

        /* Do we need to request more input or just continue? */
        TSVIONDoneSet(input_vio, fctx->buffu`ered + fctx->bytes_done);

        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);
        return;
#endif
    }

    /* second time through we have to feed already-buffered data through
     * ironbee while retaining it in buffer.  Regardless of what else happens.
     */
#if BUFFER_FIRST
    if (fctx->first_time) {
        fctx->first_time = 0;
        for (block = TSIOBufferStart(fctx->buffer);
	     block != NULL;
             block = TSIOBufferBlockNext(block)) {

            //nbytes = TSIOBufferBlockDataSizeGet(block);
            /* FIXME - do this without a reader ? */
            buf = TSIOBufferBlockReadStart(block, fctx->reader, &nbytes);
            //rc = examine_data_chunk(ibd->ibd, data->tx, buf, nbytes);
            rc = (*ibd->ibd->ib_notify_body)(data->tx->ib, data->tx, buf, nbytes);
            if (rc != IB_OK) {
                // FIXME ???
            }
        }
    }
#endif

    /* Test for EOS */
    if (ntodo == 0) {
        TSDebug("[ironbee]", "ntodo zero before consuming data");
        /* Call back the input VIO continuation to let it know that we
         * have completed the write operation.
         */
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
        return;
    }

    /* OK, there's some input awaiting our attention */
    input_reader = TSVIOReaderGet(input_vio);
    while (navail = TSIOBufferReaderAvail(input_reader), navail > 0) {
        block = TSIOBufferReaderStart(input_reader);
        buf = TSIOBufferBlockReadStart(block, input_reader, &nbytes);
        rc = (*ibd->ibd->ib_notify_body)(data->tx->ib, data->tx, buf, nbytes);
        if (rc != IB_OK) {
            // FIXME ???
        }
        rc = buffer_data_chunk(fctx, input_reader, nbytes);
        if (rc != IB_OK) {
            // FIXME ???
        }
        TSIOBufferReaderConsume(input_reader, nbytes);
        TSVIONDoneSet(input_vio, TSVIONDoneGet(input_vio) + nbytes);
    }

    ntodo = TSVIONTodoGet(input_vio);
    if (ntodo == 0) {
        TSDebug("[ironbee]", "ntodo zero after consuming data");
        /* Call back the input VIO continuation to let it know that we
         * have completed the write operation.
         */
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_COMPLETE, input_vio);
    }
    else {
        /* Call back the input VIO continuation to let it know that we
         * are ready for more data.
         */
        TSContCall(TSVIOContGet(input_vio), TS_EVENT_VCONN_WRITE_READY, input_vio);
    }
}