library/src/network/httpd/BP-HTTPdRequestHandlers.cc
// This adds a request handler to the httpd.
This adds a request handler to the httpd.
This adds a request handler to the httpd .
BP_ERROR_T BP_HTTPdRequestHandlerAdd
(
		P_BP_HTTPD_SESSION                       httpd,
		char *                                   handler_path,
		BP_HTTPdRequestHandlerFptr               handler,
		P_BP_HTTPD_REQUEST_HANDLER_SETUP_CONFIGS setup_configs,
		void *                                   external_handler_copy_parameter,
		size_t                                   external_handler_copy_parameter_size_n,
		BP_HTTPdLoginFormCallback                login_form_cb
)
{
	if(!httpd)
		return ERR_FAILURE;
	if(!handler_path)
		return ERR_FAILURE;
	if(!handler)
		return ERR_FAILURE;
	if(!setup_configs)
		return ERR_FAILURE;
	if(setup_configs->posted_data_tmp_directory_literal_path)
	{
		if(!BP_StringIsReasonablePrintableString(setup_configs->posted_data_tmp_directory_literal_path, bpstrlen(setup_configs->posted_data_tmp_directory_literal_path), BP_FALSE, BP_FALSE))
			return ERR_FAILURE;
	}
	char * table_path[10] = {0};
	P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL;
	BP_HASH_TABLE_ENTER_CRITICAL_SECTION(httpd->system_hreg);
	P_BP_HTTPD_REQUEST_HANDLER request_handler = BP_HTTPdRequestHandlerLookup
	(
		httpd,
		handler_path
	);
	if(request_handler)
	{
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, ERR_FAILURE);
	}
	table_path[0] = "request_handlers";
	table_path[1] = NULL;
	BP_ERROR_T err_status = BP_HashRegAddDataToTableByPath
	(
		httpd->system_hreg,
		table_path,
		handler_path,
		BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB,
		NULL,
		sizeof(BP_HTTPD_REQUEST_HANDLER)
	);
	request_handler = BP_HTTPdRequestHandlerLookup(httpd, handler_path);
	if(!request_handler)
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(httpd->system_hreg, ERR_FAILURE);
	request_handler->handler = handler;
	request_handler->handler_path = bpstrdup(handler_path);
	request_handler->supported_http_methods = setup_configs->supported_http_methods;
	request_handler->posted_max_data_size_before_autofail   =  setup_configs->posted_max_data_size_before_autofail;
	request_handler->posted_data_chunk_read_size            =  setup_configs->posted_data_chunk_read_size;
	request_handler->use_tmpfile_for_multipart_form_data = setup_configs->use_tmpfile_for_multipart_form_data;
	request_handler->use_tmpfile_for_octet_stream        = setup_configs->use_tmpfile_for_octet_stream;
	request_handler->http_get_max_key_len = setup_configs->http_get_max_key_len;
	request_handler->http_get_max_val_len = setup_configs->http_get_max_val_len;
	request_handler->http_cookie_max_key_len = setup_configs->http_cookie_max_key_len;
	request_handler->http_cookie_max_val_len = setup_configs->http_cookie_max_val_len;
	request_handler->http_header_max_count = setup_configs->http_header_max_count;
	request_handler->http_header_max_key_len = setup_configs->http_header_max_key_len;
	request_handler->http_header_max_val_len = setup_configs->http_header_max_val_len;
	if(setup_configs->posted_data_tmp_directory_literal_path)
	request_handler->posted_data_tmp_directory_literal_path =  bpstrdup(setup_configs->posted_data_tmp_directory_literal_path);
	if(login_form_cb)
		request_handler->login_callback = login_form_cb;
	if(!request_handler->login_callback)
		request_handler->login_callback = BP_HTTPdDefaultLoginFormCallback;
	if(external_handler_copy_parameter &&  external_handler_copy_parameter_size_n)
	{
		request_handler->external_handler_parameter = ht_calloc(httpd->system_hreg, external_handler_copy_parameter_size_n+1, 1);
		memcpy(request_handler->external_handler_parameter, external_handler_copy_parameter, external_handler_copy_parameter_size_n);
		request_handler->external_handler_parameter_size_n = external_handler_copy_parameter_size_n;
	}
	BP_HASH_TABLE_LEAVE_CRITICAL_SECTION(httpd->system_hreg);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_HTTPdRequestHandlerAdd ( P_BP_HTTPD_SESSION httpd , char * handler_path , BP_HTTPdRequestHandlerFptr handler , P_BP_HTTPD_REQUEST_HANDLER_SETUP_CONFIGS setup_configs , void * external_handler_copy_parameter , size_t external_handler_copy_parameter_size_n , BP_HTTPdLoginFormCallback login_form_cb ) { if ( ! httpd ) return ERR_FAILURE ; if ( ! handler_path ) return ERR_FAILURE ; if ( ! handler ) return ERR_FAILURE ; if ( ! setup_configs ) return ERR_FAILURE ; if ( setup_configs -> posted_data_tmp_directory_literal_path ) { if ( ! BP_StringIsReasonablePrintableString ( setup_configs -> posted_data_tmp_directory_literal_path , bpstrlen ( setup_configs -> posted_data_tmp_directory_literal_path ) , BP_FALSE , BP_FALSE ) ) return ERR_FAILURE ; } char * table_path [ 10 ] = { 0 } ; P_BP_HASH_TABLE_KEYED_ENTRY tmp_lookup = NULL ; BP_HASH_TABLE_ENTER_CRITICAL_SECTION ( httpd -> system_hreg ) ; P_BP_HTTPD_REQUEST_HANDLER request_handler = BP_HTTPdRequestHandlerLookup ( httpd , handler_path ) ; if ( request_handler ) { BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , ERR_FAILURE ) ; } table_path [ 0 ] = " " ; table_path [ 1 ] = NULL ; BP_ERROR_T err_status = BP_HashRegAddDataToTableByPath ( httpd -> system_hreg , table_path , handler_path , BP_HASH_TABLE_KEYED_ENTRY_TYPE_BINARY_BLOB , NULL , sizeof ( BP_HTTPD_REQUEST_HANDLER ) ) ; request_handler = BP_HTTPdRequestHandlerLookup ( httpd , handler_path ) ; if ( ! request_handler ) BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( httpd -> system_hreg , ERR_FAILURE ) ; request_handler -> handler = handler ; request_handler -> handler_path = bpstrdup ( handler_path ) ; request_handler -> supported_http_methods = setup_configs -> supported_http_methods ; request_handler -> posted_max_data_size_before_autofail = setup_configs -> posted_max_data_size_before_autofail ; request_handler -> posted_data_chunk_read_size = setup_configs -> posted_data_chunk_read_size ; request_handler -> use_tmpfile_for_multipart_form_data = setup_configs -> use_tmpfile_for_multipart_form_data ; request_handler -> use_tmpfile_for_octet_stream = setup_configs -> use_tmpfile_for_octet_stream ; request_handler -> http_get_max_key_len = setup_configs -> http_get_max_key_len ; request_handler -> http_get_max_val_len = setup_configs -> http_get_max_val_len ; request_handler -> http_cookie_max_key_len = setup_configs -> http_cookie_max_key_len ; request_handler -> http_cookie_max_val_len = setup_configs -> http_cookie_max_val_len ; request_handler -> http_header_max_count = setup_configs -> http_header_max_count ; request_handler -> http_header_max_key_len = setup_configs -> http_header_max_key_len ; request_handler -> http_header_max_val_len = setup_configs -> http_header_max_val_len ; if ( setup_configs -> posted_data_tmp_directory_literal_path ) request_handler -> posted_data_tmp_directory_literal_path = bpstrdup ( setup_configs -> posted_data_tmp_directory_literal_path ) ; if ( login_form_cb ) request_handler -> login_callback = login_form_cb ; if ( ! request_handler -> login_callback ) request_handler -> login_callback = BP_HTTPdDefaultLoginFormCallback ; if ( external_handler_copy_parameter && external_handler_copy_parameter_size_n ) { request_handler -> external_handler_parameter = ht_calloc ( httpd -> system_hreg , external_handler_copy_parameter_size_n + 1 , 1 ) ; memcpy ( request_handler -> external_handler_parameter , external_handler_copy_parameter , external_handler_copy_parameter_size_n ) ; request_handler -> external_handler_parameter_size_n = external_handler_copy_parameter_size_n ; } BP_HASH_TABLE_LEAVE_CRITICAL_SECTION ( httpd -> system_hreg ) ; return ERR_SUCCESS ; }
This adds a request handler to the httpd.
This adds a request handler to the httpd .
// ensure all parameters are non-null. // Ensure that if they passed in a tmp directory, it's reasonable. // ensure the string is a reasonable printable string // declare dynamic table path // declare the lookup pointer // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Check if User Exists %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // enter critical section before making changes to the httpd hash // table registry. // attempt to lookup user by username only (cannot have duplicate usernames) // if the user already exists, exit immediately // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Attempt to Create New User Structure %%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set the initial path // attempt to add data to the table // attempt to lookup the user after adding // set the request handler (pointer 1 to 1 copy) // set handler path // set the supported http methods // set maximum data size // set read size // tmpfile on multipart form data // tmpfile on octet stream data // set max key and val lengths for retrieving HTTP GET (hard limiter) // set cookie max lengths // set header max lengths // set temp dir (value is checked on entry) // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Set Login Callback %%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // set login callback if desired // set default if there is no other login callback // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Set External Data if Necessary %%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // ensure we have handler // allocate memory via hash registry // copy in parameter // set the external handler parameter size // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Leave Critical Section and Exit %%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // leave critical section // return indicating success