htp_tx_t *htp_tx_create(htp_connp_t *connp) { if (connp == NULL) return NULL; htp_tx_t *tx = calloc(1, sizeof (htp_tx_t)); if (tx == NULL) return NULL; tx->connp = connp; tx->conn = connp->conn; tx->cfg = connp->cfg; tx->is_config_shared = HTP_CONFIG_SHARED; // Request fields. tx->request_progress = HTP_REQUEST_NOT_STARTED; tx->request_protocol_number = HTP_PROTOCOL_UNKNOWN; tx->request_content_length = -1; tx->parsed_uri_raw = htp_uri_alloc(); if (tx->parsed_uri_raw == NULL) { htp_tx_destroy_incomplete(tx); return NULL; } tx->request_headers = htp_table_create(32); if (tx->request_headers == NULL) { htp_tx_destroy_incomplete(tx); return NULL; } tx->request_params = htp_table_create(32); if (tx->request_params == NULL) { htp_tx_destroy_incomplete(tx); return NULL; } // Response fields. tx->response_progress = HTP_RESPONSE_NOT_STARTED; tx->response_status = NULL; tx->response_status_number = HTP_STATUS_UNKNOWN; tx->response_protocol_number = HTP_PROTOCOL_UNKNOWN; tx->response_content_length = -1; tx->response_headers = htp_table_create(32); if (tx->response_headers == NULL) { htp_tx_destroy_incomplete(tx); return NULL; } return tx; }
htp_status_t htp_tx_destroy(htp_tx_t *tx) { if (tx == NULL) return HTP_ERROR; if (!htp_tx_is_complete(tx)) return HTP_ERROR; htp_tx_destroy_incomplete(tx); return HTP_OK; }
void htp_conn_destroy(htp_conn_t *conn) { if (conn == NULL) return; if (conn->transactions != NULL) { // Destroy individual transactions. Do note that iterating // using the iterator does not work here because some of the // list element may be NULL (and with the iterator it is impossible // to distinguish a NULL element from the end of the list). for (size_t i = 0, n = htp_list_size(conn->transactions); i < n; i++) { htp_tx_t *tx = htp_list_get(conn->transactions, i); if (tx != NULL) { htp_tx_destroy_incomplete(tx); } } htp_list_destroy(conn->transactions); conn->transactions = NULL; } if (conn->messages != NULL) { // Destroy individual messages. for (size_t i = 0, n = htp_list_size(conn->messages); i < n; i++) { htp_log_t *l = htp_list_get(conn->messages, i); free((void *) l->msg); free(l); } htp_list_destroy(conn->messages); conn->messages = NULL; } if (conn->server_addr != NULL) { free(conn->server_addr); } if (conn->client_addr != NULL) { free(conn->client_addr); } free(conn); }