static void replace_header(INKHttpTxn txnp, INKCont contp) { INKMBuffer resp_bufp; INKMLoc resp_loc; INKMLoc field_loc; if (!INKHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc)) { INKError("couldn't retrieve server response header.\n"); goto done; } field_loc = INKMimeHdrFieldRetrieve(resp_bufp, resp_loc, INK_MIME_FIELD_ACCEPT_RANGES); if (field_loc == 0) { /* field was not found */ /* create a new field in the header */ field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc); /* set its name */ INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, INK_MIME_FIELD_ACCEPT_RANGES, INK_MIME_LEN_ACCEPT_RANGES); /* set its value */ INKMimeHdrFieldValueInsert(resp_bufp, resp_loc, field_loc, "none", 4, -1); /* insert it into the header */ INKMimeHdrFieldInsert(resp_bufp, resp_loc, field_loc, -1); INKHandleMLocRelease(resp_bufp, resp_loc, field_loc); INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc); } else { /* clear the field */ INKMimeHdrFieldValuesClear(resp_bufp, resp_loc, field_loc); /* set the value to "none" */ INKMimeHdrFieldValueInsert(resp_bufp, resp_loc, field_loc, "none", 4, -1); INKHandleMLocRelease(resp_bufp, resp_loc, field_loc); INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc); } done: INKHttpTxnReenable(txnp, INK_EVENT_HTTP_CONTINUE); }
/*********************************************************************** * Get the server ip and request method in the client request. Get the * next hop ip from the server response. * Create a new http header field MY_HDR in the server response and * insert server ip, request method and next hop ip into the field as * field values. ***********************************************************************/ static int handle_response_hdr(INKCont contp, INKHttpTxn txnp) { LOG_SET_FUNCTION_NAME("handle_response_hdr"); INKMBuffer resp_bufp; INKMLoc resp_loc = NULL; INKMLoc field_loc = NULL; unsigned int next_hop_ip = 0; unsigned int server_ip = 0; const char *request_method = NULL; char *r_method = NULL; int length; INKMBuffer req_bufp; INKMLoc req_loc = NULL; int incoming_port = 0, port = 0; char *hostname = NULL; int ret_value = -1; /* negative test */ #ifdef DEBUG if (INKHttpTxnServerIPGet(NULL) != 0) LOG_ERROR_NEG("INKHttpTxnServerIPGet"); if (INKHttpTxnNextHopIPGet(NULL) != 0) LOG_ERROR_NEG("INKHttpTxnNextHopIPGet"); if (INKHttpTxnParentProxyGet(NULL, &hostname, &port) != INK_ERROR) LOG_ERROR_NEG("INKHttpTxnParentProxyGet"); #endif /* get the server ip */ if ((server_ip = INKHttpTxnServerIPGet(txnp)) == 0) LOG_ERROR_AND_RETURN("INKHttpTxnServerIPGet"); /* get the request method */ if (!INKHttpTxnServerReqGet(txnp, &req_bufp, &req_loc)) LOG_ERROR_AND_RETURN("INKHttpTxnServerReqGet"); if ((request_method = INKHttpHdrMethodGet(req_bufp, req_loc, &length)) == INK_ERROR_PTR || request_method == NULL) LOG_ERROR_AND_CLEANUP("INKHttpHdrMethodGet"); r_method = INKstrndup(request_method, length); /* get the next hop ip */ if ((next_hop_ip = INKHttpTxnNextHopIPGet(txnp)) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKHttpTxnNextHopIPGet"); /* get the client incoming port */ if ((incoming_port = INKHttpTxnClientIncomingPortGet(txnp)) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKHttpTxnClientIncomingPortGet"); /* get the parent proxy */ if (INKHttpTxnParentProxyGet(txnp, &hostname, &port) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKHttpTxnParentProxyGet"); /* If no parent defined in records.config, set hostname to NULL and port to -1 */ if (hostname == NULL) { hostname = "NULL"; port = -1; } /* retrieve the server response header */ if (!INKHttpTxnServerRespGet(txnp, &resp_bufp, &resp_loc)) LOG_ERROR_AND_CLEANUP("INKHttpTxnServerRespGet"); /* create and insert into hdr a new mime header field */ if ((field_loc = INKMimeHdrFieldCreate(resp_bufp, resp_loc)) == INK_ERROR_PTR || field_loc == NULL) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldCreate"); if (INKMimeHdrFieldAppend(resp_bufp, resp_loc, field_loc) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldAppend"); if (INKMimeHdrFieldNameSet(resp_bufp, resp_loc, field_loc, MY_HDR, strlen(MY_HDR)) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldNameSet"); /* Add value to the new mime header field */ if (INKMimeHdrFieldValueStringInsert(resp_bufp, resp_loc, field_loc, -1, r_method, strlen(r_method)) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueStringInsert"); if (INKMimeHdrFieldValueUintInsert(resp_bufp, resp_loc, field_loc, -1, server_ip) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueUintInsert"); if (INKMimeHdrFieldValueUintInsert(resp_bufp, resp_loc, field_loc, -1, next_hop_ip) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueUintInsert"); if (INKMimeHdrFieldValueIntInsert(resp_bufp, resp_loc, field_loc, -1, incoming_port) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueIntInsert"); if (INKMimeHdrFieldValueIntInsert(resp_bufp, resp_loc, field_loc, -1, port) == INK_ERROR) LOG_ERROR_AND_CLEANUP("INKMimeHdrFieldValueIntInsert"); /* success */ ret_value = 0; Lcleanup: if (VALID_POINTER(r_method)) INKfree(r_method); /* negative test for INKHandleStringRelease */ #ifdef DEBUG if (INKHandleStringRelease(NULL, req_loc, request_method) != INK_ERROR) { LOG_ERROR_NEG("INKHandleStringRelease"); } #endif /* release the buffer handles */ if (VALID_POINTER(request_method)) INKHandleStringRelease(req_bufp, req_loc, request_method); if (VALID_POINTER(req_loc)) INKHandleMLocRelease(req_bufp, INK_NULL_MLOC, req_loc); /* free the handles and continuation data */ if (VALID_POINTER(field_loc)) INKHandleMLocRelease(resp_bufp, resp_loc, field_loc); if (VALID_POINTER(resp_loc)) INKHandleMLocRelease(resp_bufp, INK_NULL_MLOC, resp_loc); return ret_value; }