1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/**
*
* Ulfius Framework injection_example program
*
* This example program describes the endpoints injections
*
* Copyright 2016-2022 Nicolas Mora <mail@babelouest.org>
*
* License MIT
*
*/
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <jansson.h>
#include <string.h>
#include <ulfius.h>
#include "u_example.h"
#define PORT 4528
#define PREFIX "/inject"
/**
* callback functions declaration
*/
int callback_first (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_second (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_third (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_fourth (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_fifth (const struct _u_request * request, struct _u_response * response, void * user_data);
int main (void) {
// Initialize the instance
struct _u_instance instance;
y_init_logs("injection_example", Y_LOG_MODE_CONSOLE, Y_LOG_LEVEL_DEBUG, NULL, "Starting injection_example");
if (ulfius_init_instance(&instance, PORT, NULL, NULL) != U_OK) {
y_log_message(Y_LOG_LEVEL_ERROR, "Error ulfius_init_instance, abort");
return(1);
}
// Endpoint list declaration
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/first", 1, &callback_first, NULL);
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/second", 1, &callback_second, NULL);
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/third", 1, &callback_third, NULL);
// Start the framework
if (ulfius_start_framework(&instance) == U_OK) {
y_log_message(Y_LOG_LEVEL_DEBUG, "Start framework on port %d", instance.port);
y_log_message(Y_LOG_LEVEL_DEBUG, "Press <enter> to inject %s/fourth endpoint", PREFIX);
getchar();
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/fourth", 1, &callback_fourth, NULL);
y_log_message(Y_LOG_LEVEL_DEBUG, "Press <enter> to inject %s/fifth endpoint", PREFIX);
getchar();
ulfius_add_endpoint_by_val(&instance, "GET", PREFIX, "/fifth", 1, &callback_fifth, NULL);
y_log_message(Y_LOG_LEVEL_DEBUG, "Press <enter> to remove %s/fourth endpoint", PREFIX);
getchar();
ulfius_remove_endpoint_by_val(&instance, "GET", PREFIX, "/fourth");
y_log_message(Y_LOG_LEVEL_DEBUG, "Press <enter> to quit the application");
// Wait for the user to press <enter> on the console to quit the application
getchar();
} else {
y_log_message(Y_LOG_LEVEL_DEBUG, "Error starting framework");
}
y_log_message(Y_LOG_LEVEL_DEBUG, "End framework");
y_close_logs();
ulfius_stop_framework(&instance);
ulfius_clean_instance(&instance);
return 0;
}
/**
* Callback callback_first
*/
int callback_first (const struct _u_request * request, struct _u_response * response, void * user_data) {
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "Hello World from callback_first!");
return U_CALLBACK_CONTINUE;
}
/**
* Callback callback_second
*/
int callback_second (const struct _u_request * request, struct _u_response * response, void * user_data) {
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "Hello World from callback_second!");
return U_CALLBACK_CONTINUE;
}
/**
* Callback callback_third
*/
int callback_third (const struct _u_request * request, struct _u_response * response, void * user_data) {
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "Hello World from callback_third!");
return U_CALLBACK_CONTINUE;
}
/**
* Callback callback_fourth
*/
int callback_fourth (const struct _u_request * request, struct _u_response * response, void * user_data) {
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "Hello World from callback_fourth!");
return U_CALLBACK_CONTINUE;
}
/**
* Callback callback_fifth
*/
int callback_fifth (const struct _u_request * request, struct _u_response * response, void * user_data) {
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 200, "Hello World from callback_fifth!");
return U_CALLBACK_CONTINUE;
}
|