/** * Main function */ int main (int argc, char **argv) { // jansson integer type can vary #if JSON_INTEGER_IS_LONG_LONG long long nb_sheep = 0; #else long nb_sheep = 0; #endif // Initialize the instance struct _u_instance instance; if (ulfius_init_instance(&instance, PORT, NULL) != U_OK) { y_log_message(Y_LOG_LEVEL_ERROR, "Error ulfius_init_instance, abort"); return(1); } // MIME types that will define the static files struct _u_map mime_types; u_map_init(&mime_types); u_map_put(&mime_types, ".html", "text/html"); u_map_put(&mime_types, ".css", "text/css"); u_map_put(&mime_types, ".js", "application/javascript"); u_map_put(&mime_types, ".png", "image/png"); u_map_put(&mime_types, ".jpeg", "image/jpeg"); u_map_put(&mime_types, ".jpg", "image/jpeg"); u_map_put(&mime_types, "*", "application/octet-stream"); // Endpoint list declaration // The first 3 are webservices with a specific url // The last endpoint will be called for every GET call and will serve the static files ulfius_add_endpoint_by_val(&instance, "POST", PREFIX, NULL, &callback_sheep_counter_start, &nb_sheep); ulfius_add_endpoint_by_val(&instance, "PUT", PREFIX, NULL, &callback_sheep_counter_add, &nb_sheep); ulfius_add_endpoint_by_val(&instance, "DELETE", PREFIX, NULL, &callback_sheep_counter_reset, &nb_sheep); ulfius_add_endpoint_by_val(&instance, "GET", NULL, "*", &callback_static_file, &mime_types); // Start the framework if (ulfius_start_framework(&instance) == U_OK) { printf("Start sheep counter on port %d\n", instance.port); // Wait for the user to press <enter> on the console to quit the application getchar(); } else { printf("Error starting framework\n"); } // Clean the mime map u_map_clean(&mime_types); printf("End framework\n"); ulfius_stop_framework(&instance); ulfius_clean_instance(&instance); return 0; }
int main (int argc, char **argv) { y_init_logs("test_u_map", Y_LOG_MODE_CONSOLE, Y_LOG_LEVEL_DEBUG, NULL, "Starting test_u_map"); struct _u_map map, * map_copy; char * print, * print_copy; u_map_init(&map); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 1, map is\n%s\n", print); free(print); u_map_put(&map, "key1", "value1"); u_map_put(&map, "key2", "value2"); u_map_put(&map, "key3", "value3"); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 2, map is\n%s\n", print); free(print); u_map_put(&map, "key2", "value4"); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 3, map is\n%s\n", print); free(print); map_copy = u_map_copy(&map); u_map_put(map_copy, "key4", "value5"); print = print_map(&map); print_copy = print_map(map_copy); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 4, map is\n%s\nmap_copy is\n%s\n", print, print_copy); free(print); free(print_copy); u_map_remove_at(&map, 2); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 5: remove item 2 from map, map is\n%s\n", print); free(print); u_map_remove_from_key(map_copy, "key1"); print = print_map(map_copy); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 6: remove key:key1 from map_copy, map is\n%s\n", print); free(print); u_map_remove_from_key(map_copy, "key_nope"); print = print_map(map_copy); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 7: remove key:key_nope from map_copy, map is\n%s\n", print); free(print); u_map_remove_from_key_case(map_copy, "Key2"); print = print_map(map_copy); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 8: remove case key:Key2 from map_copy, map is\n%s\n", print); free(print); u_map_remove_from_value(map_copy, "value3"); print = print_map(map_copy); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 9: remove value:value3 from map_copy, map is\n%s\n", print); free(print); u_map_remove_from_value_case(map_copy, "Value5"); print = print_map(map_copy); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 10: remove case value:Value5 from map_copy, map is\n%s\n", print); free(print); y_log_message(Y_LOG_LEVEL_DEBUG, "map has key key1? %d", u_map_has_key(&map, "key1")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has key key_nope? %d", u_map_has_key(&map, "key_nope")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has key Key1? %d", u_map_has_key(&map, "Key1")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has key Key1 (no case) ? %d", u_map_has_key_case(&map, "Key1")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has key Key_nope (no case) ? %d", u_map_has_key_case(&map, "Key_nope")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has value value1? %d", u_map_has_value(&map, "value1")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has value value_nope? %d", u_map_has_value(&map, "value_nope")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has value Value1? %d", u_map_has_value(&map, "Value1")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has value Value1 (no case) ? %d", u_map_has_value_case(&map, "Value1")); y_log_message(Y_LOG_LEVEL_DEBUG, "map has value Value_nope (no case) ? %d", u_map_has_value_case(&map, "Value_nope")); y_log_message(Y_LOG_LEVEL_DEBUG, "get value from key key1: %s", u_map_get(&map, "key1")); y_log_message(Y_LOG_LEVEL_DEBUG, "get value from key key_nope: %s", u_map_get(&map, "key_nope")); y_log_message(Y_LOG_LEVEL_DEBUG, "get value from key Key1 (no case): %s", u_map_get_case(&map, "Key1")); y_log_message(Y_LOG_LEVEL_DEBUG, "get value from key Key_nope (no case): %s", u_map_get_case(&map, "Key_nope")); put_file_content_in_map(&map, "../sheep_counter/static/sheep.png", 0); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 11, map is\n%s\n", print); free(print); u_map_remove_from_key(&map, "../sheep_counter/static/sheep.png"); put_file_content_in_map(&map, "Makefile", 0); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 12, map is\n%s\n", print); free(print); u_map_put_binary(&map, "Makefile", "Replace the first characters", 0, strlen("Replace the first characters")); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 12, map is\n%s\n", print); free(print); u_map_put_binary(&map, "Makefile", "Append at the end of the value", u_map_get_length(&map, "Makefile"), strlen("Append at the end of the value")); print = print_map(&map); y_log_message(Y_LOG_LEVEL_DEBUG, "iteration 12, map is\n%s\n", print); free(print); u_map_clean(&map); u_map_clean_full(map_copy); y_close_logs(); return 0; }
int main (int argc, char **argv) { struct _u_map headers, url_params, post_params, req_headers; char * string_body = "param1=one¶m2=two"; json_t * json_body = json_object(); struct _u_response response; int res; u_map_init(&headers); u_map_init(&url_params); u_map_put(&url_params, "test", "one"); u_map_put(&url_params, "other_test", "two"); u_map_init(&post_params); u_map_put(&post_params, "third_test", "three"); u_map_put(&post_params, "fourth_test", "four"); u_map_put(&post_params, "extreme_test", "Here ! are %9_ some $ ö\\)]= special châraçters"); u_map_init(&req_headers); u_map_put(&req_headers, "Content-Type", MHD_HTTP_POST_ENCODING_FORM_URLENCODED); json_object_set_new(json_body, "param1", json_string("one")); json_object_set_new(json_body, "param2", json_string("two")); struct _u_request req_list[] = { {"GET", SERVER_URL_PREFIX "/get/", 0, NULL, NULL, NULL, &url_params, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0}, // Parameters in url {"DELETE", SERVER_URL_PREFIX "/delete/", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0}, // No parameters {"POST", SERVER_URL_PREFIX "/post/param/", 0, NULL, NULL, NULL, NULL, NULL, NULL, &post_params, NULL, NULL, 0, string_body, strlen(string_body)}, // Parameters in post_map and string_body {"POST", SERVER_URL_PREFIX "/post/plain/", 0, NULL, NULL, NULL, NULL, &req_headers, NULL, NULL, NULL, NULL, 0, string_body, strlen(string_body)}, // Paremeters in string body, header MHD_HTTP_POST_ENCODING_FORM_URLENCODED {"POST", SERVER_URL_PREFIX "/post/json/", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, json_body, NULL, 0, NULL, 0}, // Parameters in json_body {"PUT", SERVER_URL_PREFIX "/put/plain", 0, NULL, NULL, NULL, NULL, &req_headers, NULL, NULL, NULL, NULL, 0, string_body, strlen(string_body)}, // Paremeters in string body, header MHD_HTTP_POST_ENCODING_FORM_URLENCODED {"PUT", SERVER_URL_PREFIX "/put/json", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, json_body, NULL, 0, NULL, 0}, // Parameters in json_body {"POST", SERVER_URL_PREFIX "/post/param/", 0, NULL, NULL, NULL, NULL, NULL, NULL, &post_params, NULL, NULL, 0, NULL, 0} // Parameters in post_map }; printf("Press <enter> to run get test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[0], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); printf("Press <enter> to run get test with no interest on the response\n"); getchar(); printf("Request sent, result is %d\n", ulfius_send_http_request(&req_list[0], NULL)); printf("Press <enter> to run delete test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[1], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); printf("Press <enter> to run post parameters test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[2], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); printf("Press <enter> to run post plain test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[3], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); printf("Press <enter> to run post json test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[4], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); printf("Press <enter> to run put plain test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[5], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); printf("Press <enter> to run put json test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[6], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); printf("Press <enter> to run post only test\n"); getchar(); ulfius_init_response(&response); res = ulfius_send_http_request(&req_list[7], &response); if (res == U_OK) { print_response(&response); } else { printf("Error in http request: %d\n", res); } ulfius_clean_response(&response); // Wait for the user to press <enter> on the console to quit the application printf("Press <enter> to quit test\n"); getchar(); json_decref(json_body); u_map_clean(&headers); u_map_clean(&url_params); u_map_clean(&post_params); u_map_clean(&req_headers); return 0; }