void AXIS2_CALL axis2_udp_receiver_free( axis2_transport_receiver_t * trn_receiver, const axutil_env_t * env) { axis2_udp_receiver_impl_t * receiver = NULL; receiver = AXIS2_INTF_TO_IMPL(trn_receiver); if (!receiver) { AXIS2_LOG_ERROR (env->log, AXIS2_LOG_SI, "failure, receiver value is null , nothing to free"); return; } if (!receiver->stop) { receiver->stop = AXIS2_TRUE; } /* We do not free the conf_ctx as this is not the right place to do it */ receiver->conf_ctx = NULL; /* Close the sockets */ if (receiver->send_socket != AXIS2_INVALID_SOCKET) { axutil_network_handler_close_socket(env, receiver->send_socket); } /* we free this socket only if we owns it */ if (receiver->owns_socket && receiver->socket != AXIS2_INVALID_SOCKET) { axutil_network_handler_close_socket(env, receiver->socket); } if (receiver->mutex) { axutil_thread_mutex_destroy(receiver->mutex); } AXIS2_FREE(env->allocator, receiver); }
AXIS2_EXTERN axis2_status_t AXIS2_CALL axis2_simple_http_svr_conn_close( axis2_simple_http_svr_conn_t * svr_conn, const axutil_env_t * env) { axutil_stream_free(svr_conn->stream, env); if(-1 != svr_conn->socket) { axutil_network_handler_close_socket(env, svr_conn->socket); svr_conn->socket = -1; } return AXIS2_SUCCESS; }
/** executes as new entry arises */ void *AXIS2_CALL tcpmon_entry_new_entry_funct( axutil_thread_t * thd, void *data) { tcpmon_entry_request_data_t *req_data = (tcpmon_entry_request_data_t *) data; const axutil_env_t *env = NULL; int client_socket = -1; int host_socket = -1; tcpmon_session_t *session; TCPMON_SESSION_TRANS_ERROR_FUNCT on_trans_fault_funct; TCPMON_SESSION_NEW_ENTRY_FUNCT on_new_entry; axutil_stream_t *client_stream = NULL; axutil_stream_t *host_stream = NULL; int buffer_size = 0; axis2_char_t *headers = NULL; axis2_char_t *content = NULL; axis2_char_t *buffer = NULL; time_t now; struct tm *localTime = NULL; int arrived_secs = 0; int sent_secs = 0; int time_diff_i = 0; int target_port = 0; axis2_char_t *target_host = NULL; int test_bit = 0; int format_bit = 0; tcpmon_entry_t *entry = NULL; tcpmon_entry_impl_t *entry_impl = NULL; env = req_data->env; client_socket = req_data->socket; session = req_data->session; on_trans_fault_funct = tcpmon_session_get_on_trans_fault(session, env); on_new_entry = tcpmon_session_get_on_new_entry(session, env); entry = tcpmon_entry_create(env); entry_impl = AXIS2_INTF_TO_IMPL(entry); target_port = TCPMON_SESSION_GET_TARGET_PORT(session, env); target_host = TCPMON_SESSION_GET_TARGET_HOST(session, env); if (target_port == -1 || target_host == NULL) { axutil_network_handler_close_socket(env, client_socket); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Missing target port and host" "input missing"); if (on_trans_fault_funct) { (on_trans_fault_funct) (env, "Missing target port and host"); } if (thd) { AXIS2_FREE(env->allocator, thd); } if (data) { AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); } return NULL; } client_stream = axutil_stream_create_socket(env, client_socket); if (!client_stream) { axutil_network_handler_close_socket(env, client_socket); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error in creating client stream" "handling response"); /** call the callback */ if (on_trans_fault_funct) { (on_trans_fault_funct) (env, "error in creating the client stream"); } if (thd) { AXIS2_FREE(env->allocator, thd); } if (data) { AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); } return NULL; } buffer = tcpmon_util_read_current_stream(env, client_stream, &buffer_size, &headers, &content); headers =(char *) tcpmon_util_str_replace(env, headers,"localhost", target_host); test_bit = TCPMON_SESSION_GET_TEST_BIT(session, env); if (test_bit) { tcpmon_util_write_to_file("reqest", buffer); } format_bit = TCPMON_SESSION_GET_FORMAT_BIT(session, env); TCPMON_ENTRY_SET_FORMAT_BIT(entry, env, format_bit); now = time(NULL); localTime = localtime(&now); sprintf(entry_impl->sent_time, "%d:%d:%d", localTime->tm_hour, localTime->tm_min, localTime->tm_sec); sent_secs = localTime->tm_hour * 60 * 60 + localTime->tm_min * 60 + localTime->tm_sec; /*free ( localTime); */ entry_impl->sent_headers = headers; entry_impl->sent_data = content; entry_impl->sent_data_length = buffer_size; if (on_new_entry) { (on_new_entry) (env, entry, 0); } host_socket = (int)axutil_network_handler_open_socket(env, target_host, target_port); if (-1 == host_socket) { axutil_stream_write(client_stream, env, NULL, 0); axutil_stream_free(client_stream, env); axutil_network_handler_close_socket(env, client_socket); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error in creating host_socket" "creating socket"); /** call the callback */ if (on_trans_fault_funct) { (on_trans_fault_funct) (env, "error in creating the host socket"); } if (thd) { AXIS2_FREE(env->allocator, thd); } if (data) { AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); } return NULL; } host_stream = axutil_stream_create_socket(env, host_socket); if (!host_stream) { axutil_stream_write(client_stream, env, NULL, 0); axutil_stream_free(client_stream, env); axutil_network_handler_close_socket(env, client_socket); axutil_network_handler_close_socket(env, host_socket); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Error in creating host stream" "handling response"); /** call the callback */ if (on_trans_fault_funct) { (on_trans_fault_funct) (env, "error in creating the host stream"); } if (thd) { AXIS2_FREE(env->allocator, thd); } if (data) { AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); } return NULL; } axutil_stream_write(host_stream, env, buffer, buffer_size); AXIS2_FREE(env->allocator, buffer); buffer = tcpmon_util_read_current_stream(env, host_stream, &buffer_size, &headers, &content); test_bit = TCPMON_SESSION_GET_TEST_BIT(session, env); if (test_bit) { tcpmon_util_write_to_file("response", buffer); } now = time(NULL); localTime = localtime(&now); sprintf(entry_impl->arrived_time, "%d:%d:%d", localTime->tm_hour, localTime->tm_min, localTime->tm_sec); arrived_secs = localTime->tm_hour * 60 * 60 + localTime->tm_min * 60 + localTime->tm_sec; /*free ( localTime); */ time_diff_i = arrived_secs - sent_secs; if (time_diff_i < 0) { time_diff_i += 24 * 60 * 60; } sprintf(entry_impl->time_diff, "%d sec(s)", time_diff_i); entry_impl->arrived_headers = headers; entry_impl->arrived_data = content; entry_impl->arrived_data_length = buffer_size; if (buffer == NULL || buffer_size == 0) { entry_impl->is_success = 0; } else { entry_impl->is_success = 1; } if (on_new_entry) { (on_new_entry) (env, entry, 1); } axutil_stream_write(client_stream, env, buffer, buffer_size); AXIS2_FREE(env->allocator, buffer); axutil_stream_free(client_stream, env); axutil_stream_free(host_stream, env); axutil_network_handler_close_socket(env, client_socket); axutil_network_handler_close_socket(env, host_socket); if (entry_impl) { tcpmon_entry_free(&(entry_impl->entry), env); } if (thd) { AXIS2_FREE(env->allocator, thd); } if (data) { AXIS2_FREE(env->allocator, (tcpmon_entry_request_data_t *)data); } return NULL; }
AXIS2_EXTERN axis2_status_t AXIS2_CALL axis2_http_client_send( axis2_http_client_t * client, const axutil_env_t * env, axis2_http_simple_request_t * request, axis2_char_t * ssl_pp) { char *wire_format = NULL; axutil_array_list_t *headers = NULL; char *str_header = NULL; char *str_request_line = NULL; int written = 0; axis2_status_t status = AXIS2_FAILURE; axis2_bool_t chunking_enabled = AXIS2_FALSE; axis2_char_t *host = NULL; unsigned int port = 0; /* In the MTOM case request body is not set. Instead mime_parts array_list is there */ /*if(client->req_body) { AXIS2_FREE(env->allocator, client->req_body); client->req_body = NULL; }*/ if(!client->req_body && !(client->doing_mtom)) { client->req_body_size = axis2_http_simple_request_get_body_bytes(request, env, &client->req_body); } if(client->dump_input_msg == AXIS2_TRUE) { return AXIS2_SUCCESS; } if(!client->url) { AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_NULL_URL, AXIS2_FAILURE); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Request url not set"); return AXIS2_FAILURE; } host = axutil_url_get_host(client->url, env); port = axutil_url_get_port(client->url, env); if(client->proxy_enabled) { if(!client->proxy_host || client->proxy_port <= 0) { AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Proxy port or Host not set"); return AXIS2_FAILURE; } if(client->sockfd < 0) { client->sockfd = (int)axutil_network_handler_open_socket(env, client->proxy_host, client->proxy_port); } } else { /*Proxy is not enabled*/ if(client->sockfd < 0) { client->sockfd = (int)axutil_network_handler_open_socket(env, host, port); } } if(client->sockfd < 0) { AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_SOCKET_ERROR, AXIS2_FAILURE); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Socket Creation failed."); return AXIS2_FAILURE; } if(client->timeout > 0) { /*Set the receiving time out*/ axutil_network_handler_set_sock_option(env, client->sockfd, SO_RCVTIMEO, client->timeout); /*Set the sending time out*/ axutil_network_handler_set_sock_option(env, client->sockfd, SO_SNDTIMEO, client->timeout); } if(0 == axutil_strcasecmp(axutil_url_get_protocol(client->url, env), AXIS2_TRANSPORT_URL_HTTPS)) { #ifdef AXIS2_SSL_ENABLED if (client->proxy_enabled) { if (AXIS2_SUCCESS != axis2_http_client_connect_ssl_host(client, env, host, port)) { axutil_network_handler_close_socket(env, client->sockfd); client->sockfd = -1; AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "HTTPS connection creation failed"); return AXIS2_FAILURE; } } if(!client->data_stream) { client->data_stream = axutil_stream_create_ssl(env, client->sockfd, axis2_http_client_get_server_cert(client, env), axis2_http_client_get_key_file(client, env), ssl_pp); client->owns_stream = AXIS2_TRUE; } #else axutil_network_handler_close_socket(env, client->sockfd); client->sockfd = -1; AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_INVALID_TRANSPORT_PROTOCOL, AXIS2_FAILURE); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Invalid Transport Protocol, HTTPS transport not enabled."); return AXIS2_FAILURE; #endif } else { if(!client->data_stream) { client->data_stream = axutil_stream_create_socket(env, client->sockfd); client->owns_stream = AXIS2_TRUE; } } if(!client->data_stream) { axutil_network_handler_close_socket(env, client->sockfd); client->sockfd = -1; AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Data stream creation failed for Host %s and %d port", host, port); return AXIS2_FAILURE; } /*Accessing HTTP headers*/ headers = axis2_http_simple_request_get_headers(request, env); if(headers) { int header_count = axutil_array_list_size(headers, env); int i = 0; char *str_header2 = NULL; for(i = 0; i < header_count; i++) { axis2_char_t *header_ext_form = NULL; axis2_http_header_t *tmp_header = (axis2_http_header_t *)axutil_array_list_get(headers, env, i); if(!tmp_header) { /* This continue is added as a safey mechanism, * However I see a problem with this logic, AFAIC * see there can't be null headers in the headers * array list, because number of headers in "headers" * array list count with axutil_array_list_size, * therefore this check and continue might not have a * real effect.*/ continue; } /* check whether we have transfer encoding and then see whether the * value is "chunked" */ if(!axutil_strcmp(axis2_http_header_get_name(tmp_header, env), AXIS2_HTTP_HEADER_TRANSFER_ENCODING) && !axutil_strcmp(axis2_http_header_get_value( tmp_header, env), AXIS2_HTTP_HEADER_TRANSFER_ENCODING_CHUNKED)) { chunking_enabled = AXIS2_TRUE; } header_ext_form = axis2_http_header_to_external_form(tmp_header, env); /* str_header2 is to hold intermediate value of str_header */ str_header2 = axutil_stracat(env, str_header, header_ext_form); if(str_header) { AXIS2_FREE(env->allocator, str_header); str_header = NULL; } if(header_ext_form) { AXIS2_FREE(env->allocator, header_ext_form); header_ext_form = NULL; } /* str_header has all HTTP headers to send. */ str_header = str_header2; } } if(AXIS2_FALSE == client->proxy_enabled) { str_request_line = axis2_http_request_line_to_string( axis2_http_simple_request_get_request_line(request, env), env); } else { /* proxy enabled case */ /* we need the request line in the format * POST http://host:port/path HTTP/1.x if we have enabled proxies */ axis2_char_t *host_port_str = NULL; axis2_char_t *host = axutil_url_get_host(client->url, env); axis2_http_request_line_t *request_line = axis2_http_simple_request_get_request_line( request, env); axis2_char_t *path = axis2_http_request_line_get_uri(request_line, env); host_port_str = AXIS2_MALLOC(env->allocator, axutil_strlen(host) + axutil_strlen(path) + 20 * sizeof(axis2_char_t)); if(!host_port_str) { axutil_network_handler_close_socket(env, client->sockfd); client->sockfd = -1; AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Memory allocation failed for host %s and %s path", host, path); return AXIS2_FAILURE; } sprintf(host_port_str, "http://%s:%d%s", host, axutil_url_get_port(client->url, env), path); str_request_line = AXIS2_MALLOC(env->allocator, axutil_strlen(host_port_str) + 20 * sizeof(axis2_char_t)); if(!str_request_line) { axutil_network_handler_close_socket(env, client->sockfd); client->sockfd = -1; AXIS2_FREE(env->allocator, host_port_str); AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE); AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "memory allocation failed for host %s and %s path", host, path); return AXIS2_FAILURE; } sprintf(str_request_line, "%s %s %s\r\n", axis2_http_request_line_get_method(request_line, env), host_port_str, axis2_http_request_line_get_http_version(request_line, env)); AXIS2_FREE(env->allocator, host_port_str); host_port_str = NULL; } /* Here first we send the http header part */ wire_format = axutil_stracat(env, str_request_line, str_header); AXIS2_FREE(env->allocator, str_header); str_header = NULL; AXIS2_FREE(env->allocator, str_request_line); str_request_line = NULL; written = axutil_stream_write(client->data_stream, env, wire_format, axutil_strlen(wire_format)); AXIS2_FREE(env->allocator, wire_format); wire_format = NULL; /* Then we write the two new line charaters before the http body*/ written = axutil_stream_write(client->data_stream, env, AXIS2_HTTP_CRLF, 2); /* When sending MTOM it is bit different. We keep the attachment + other mime headers in an array_list and send them one by one */ if(client->doing_mtom) { /*axis2_status_t status = AXIS2_SUCCESS; */ axutil_http_chunked_stream_t *chunked_stream = NULL; /* If the callback name is not there, then we will check whether there * is any mime_parts which has type callback. If we found then no point * of continuing we should return a failure */ if(!(client->mtom_sending_callback_name)) { if(axis2_http_transport_utils_is_callback_required(env, client->mime_parts)) { AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Sender callback not specified"); return AXIS2_FAILURE; } } /* For MTOM we automatically enabled chunking */ chunked_stream = axutil_http_chunked_stream_create(env, client->data_stream); /* This method will write the Attachment + data to the wire */ status = axis2_http_transport_utils_send_mtom_message(chunked_stream, env, client->mime_parts, client->mtom_sending_callback_name); axutil_http_chunked_stream_free(chunked_stream, env); chunked_stream = NULL; } /* Non MTOM case */ else if(client->req_body_size > 0 && client->req_body) { int len = 0; written = 0; /* Keep on writing data in a loop until we finised with all the data in the buffer */ if(!chunking_enabled) { status = AXIS2_SUCCESS; while(written < client->req_body_size) { len = 0; len = axutil_stream_write(client->data_stream, env, client->req_body + written, client->req_body_size - written); if(-1 == len) { status = AXIS2_FAILURE; break; } else { written += len; } } } else { /* Not MTOM but chunking is enabled */ axutil_http_chunked_stream_t *chunked_stream = NULL; chunked_stream = axutil_http_chunked_stream_create(env, client->data_stream); status = AXIS2_SUCCESS; if(!chunked_stream) { axutil_network_handler_close_socket(env, client->sockfd); client->sockfd = -1; AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Creatoin of chunked stream failed"); return AXIS2_FAILURE; } while(written < client->req_body_size) { written = axutil_http_chunked_stream_write(chunked_stream, env, client->req_body, client->req_body_size); if(-1 == written) { status = AXIS2_FAILURE; break; } } if(AXIS2_SUCCESS == status) { /* Writing the trailing null charactor */ axutil_http_chunked_stream_write_last_chunk(chunked_stream, env); } axutil_http_chunked_stream_free(chunked_stream, env); } } client->request_sent = AXIS2_TRUE; return status; }
AXIS2_EXTERN void AXIS2_CALL axis2_http_client_free( axis2_http_client_t * http_client, const axutil_env_t * env) { if(http_client->proxy_host) { AXIS2_FREE(env->allocator, http_client->proxy_host); } if(http_client->proxy_host_port) { AXIS2_FREE(env->allocator, http_client->proxy_host_port); } if(http_client->url) { axutil_url_free(http_client->url, env); } if(http_client->response) { axis2_http_simple_response_free(http_client->response, env); } if(-1 != http_client->sockfd) { #ifdef AXIS2_SSL_ENABLED if(http_client->data_stream) { if(http_client->data_stream->stream_type == AXIS2_STREAM_SOCKET) { axutil_network_handler_close_socket(env, http_client->sockfd); /** ssl streams of type AXIS2_STREAM_BASIC will be handled by SSL_shutdown(); */ } } #else axutil_network_handler_close_socket(env, http_client->sockfd); #endif http_client->sockfd = -1; } if(http_client->data_stream && http_client->owns_stream) { axutil_stream_free(http_client->data_stream,env); } if(http_client->req_body) { AXIS2_FREE(env->allocator, http_client->req_body); } /* There is no other appropriate place to free the mime_part list when a * particular client send requests. */ if(http_client->mime_parts) { int i = 0; for(i = 0; i < axutil_array_list_size(http_client->mime_parts, env); i++) { axiom_mime_part_t *mime_part = NULL; mime_part = (axiom_mime_part_t *)axutil_array_list_get(http_client->mime_parts, env, i); if(mime_part) { axiom_mime_part_free(mime_part, env); } } axutil_array_list_free(http_client->mime_parts, env); } AXIS2_FREE(env->allocator, http_client); return; }