Writing Handler Functions

The handler function is the key component of a continuation. It is supposed to examine the event and event data, and then do something appropriate. The probable action might be to schedule another event for the continuation to received, to open up a connection to a server, or simply to destroy itself.

The continuation’s handler function is a function of type TSEventFunc. Its arguments are a continuation, an event, and a pointer to some data (this data is passed to the continuation by the caller - do not confuse this data with the continuation’s own data, associated by TSContDataSet()). When the continuation is called back, the continuation and an event are passed to the handler function. The continuation is a handle to the same continuation that is invoked. The handler function typically has a switch statement to handle the events it receives:

  1. static int some_handler (TScont contp, TSEvent event, void *edata)
  2. {
  3. // .....
  4. switch(event) {
  5. case TS_EVENT_SOME_EVENT_1:
  6. do_some_thing_1;
  7. return;
  8. case TS_EVENT_SOME_EVENT_2:
  9. do_some_thing_2;
  10. return;
  11. case TS_EVENT_SOME_EVENT_3:
  12. do_some_thing_3;
  13. return;
  14. default: break;
  15. }
  16. return 0;
  17. }

Caution

You might notice that a continuation cannot determine if more events are “in flight” toward it. Do not use TSContDestroy() to delete a continuation before you make sure that all incoming events, such as those sent because of TSHttpTxnHookAdd(), have been handled.

Caution

TS_HTTP_SEND_REQUEST_HDR_HOOK may callback several times when the OS crashed. Be careful to use functions such as TSContDestroy in TS_HTTP_SEND_REQUEST_HDR_HOOK hook.

The following table lists events and the corresponding type of void* data passed to handler functions:

EventEvent SenderData Type
TS_EVENT_HTTP_READ_REQUEST_HDRTS_HTTP_READ_REQUEST_HDR_HOOKTSHttpTxn
TS_EVENT_HTTP_PRE_REMAPTS_HTTP_PRE_REMAP_HOOKTSHttpTxn
TS_EVENT_HTTP_OS_DNSTS_HTTP_OS_DNS_HOOKTSHttpTxn
TS_EVENT_HTTP_SEND_REQUEST_HDRTS_HTTP_SEND_REQUEST_HDR_HOOKTSHttpTxn
TS_EVENT_HTTP_READ_CACHE_HDRTS_HTTP_READ_CACHE_HDR_HOOKTSHttpTxn
TS_EVENT_HTTP_READ_RESPONSE_HDRTS_HTTP_READ_RESPONSE_HDR_HOOKTSHttpTxn
TS_EVENT_HTTP_SEND_RESPONSE_HDRTS_HTTP_SEND_RESPONSE_HDR_HOOKTSHttpTxn
TS_EVENT_HTTP_SELECT_ALTTS_HTTP_SELECT_ALT_HOOKTSHttpTxn
TS_EVENT_HTTP_TXN_STARTTS_HTTP_TXN_START_HOOKTSHttpTxn
TS_EVENT_HTTP_TXN_CLOSETS_HTTP_TXN_CLOSE_HOOKTSHttpTxn
TS_EVENT_HTTP_SSN_STARTTS_HTTP_SSN_START_HOOKTSHttpSsn
TS_EVENT_HTTP_SSN_CLOSETS_HTTP_SSN_CLOSE_HOOKTSHttpSsn
TS_EVENT_NONE  
TS_EVENT_CACHE_LOOKUP_COMPLETETS_HTTP_CACHE_LOOKUP_COMPLETE_HOOKTSHttpTxn
TS_EVENT_IMMEDIATETSVConnClose() TSVIOReenable() TSContSchedule() 
TS_EVENT_IMMEDIATETS_HTTP_REQUEST_TRANSFORM_HOOK 
TS_EVENT_IMMEDIATETS_HTTP_RESPONSE_TRANSFORM_HOOK 
TS_EVENT_CACHE_OPEN_READTSCacheRead()Cache VC
TS_EVENT_CACHE_OPEN_READ_FAILEDTSCacheRead()TS_CACHE_ERROR code
TS_EVENT_CACHE_OPEN_WRITETSCacheWrite()Cache VC
TS_EVENT_CACHE_OPEN_WRITE_FAILEDTSCacheWrite()TS_CACHE_ERROR code
TS_EVENT_CACHE_REMOVETSCacheRemove() 
TS_EVENT_CACHE_REMOVE_FAILEDTSCacheRemove()TS_CACHE_ERROR code
TS_EVENT_NET_ACCEPTTSNetAccept() TSHttpTxnServerIntercept() TSHttpTxnIntercept()TSNetVConnection
TS_EVENT_NET_ACCEPT_FAILEDTSNetAccept() TSHttpTxnServerIntercept() TSHttpTxnIntercept() 
TS_EVENT_HOST_LOOKUPTSHostLookup()TSHostLookupResult
TS_EVENT_TIMEOUTTSContSchedule() 
TS_EVENT_ERROR  
TS_EVENT_VCONN_READ_READYTSVConnRead()TSVIO
TS_EVENT_VCONN_WRITE_READYTSVConnWrite()TSVIO
TS_EVENT_VCONN_READ_COMPLETETSVConnRead()TSVIO
TS_EVENT_VCONN_WRITE_COMPLETETSVConnWrite()TSVIO
TS_EVENT_VCONN_EOSTSVConnRead()TSVIO
TS_EVENT_NET_CONNECTTSNetConnect()TSVConn
TS_EVENT_NET_CONNECT_FAILEDTSNetConnect()TSVConn
TS_EVENT_HTTP_CONTINUE  
TS_EVENT_HTTP_ERROR  
TS_EVENT_MGMT_UPDATETSMgmtUpdateRegister() 

The continuation functions are listed below: