Greenbone Security Assistant  9.0.1
gsad_http_handler.h File Reference
#include "gsad_http.h"
Include dependency graph for gsad_http_handler.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct http_handler http_handler_t
 
typedef void(* http_handler_free_func_t) (http_handler_t *)
 
typedef int(* http_handler_func_t) (http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
 

Functions

http_handler_t * http_handler_add (http_handler_t *handlers, http_handler_t *handler)
 
int http_handler_next (http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
 
int http_handler_start (http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
 
http_handler_t * http_handler_new (http_handler_func_t)
 
void http_handler_free (http_handler_t *handler)
 
http_handler_t * init_http_handlers ()
 
void cleanup_http_handlers ()
 
http_handler_t * url_handler_new (const gchar *regexp, http_handler_t *handler)
 
http_handler_t * url_handler_add_func (http_handler_t *handlers, const gchar *regexp, http_handler_func_t handle)
 
http_handler_t * method_router_new ()
 
void method_router_set_get_handler (http_handler_t *router, http_handler_t *handler)
 
void method_router_set_post_handler (http_handler_t *router, http_handler_t *handler)
 
int handle_request (void *cls, http_connection_t *connection, const char *url, const char *method, const char *version, const char *upload_data, size_t *upload_data_size, void **con_cls)
 HTTP request handler for GSAD. More...
 

Typedef Documentation

◆ http_handler_free_func_t

typedef void(* http_handler_free_func_t) (http_handler_t *)

Definition at line 32 of file gsad_http_handler.h.

◆ http_handler_func_t

typedef int(* http_handler_func_t) (http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)

Definition at line 34 of file gsad_http_handler.h.

◆ http_handler_t

typedef struct http_handler http_handler_t

Definition at line 30 of file gsad_http_handler.h.

Function Documentation

◆ cleanup_http_handlers()

void cleanup_http_handlers ( )

Definition at line 818 of file gsad_http_handler.c.

819 {
820  g_debug ("Cleaning up http handlers");
821 
823 
825 }

References gvm_validator_free(), handlers, http_handler_free(), and http_validator.

Here is the call graph for this function:

◆ handle_request()

int handle_request ( void *  cls,
http_connection_t *  connection,
const char *  url,
const char *  method,
const char *  version,
const char *  upload_data,
size_t *  upload_data_size,
void **  con_cls 
)

HTTP request handler for GSAD.

This routine is an MHD_AccessHandlerCallback, the request handler for microhttpd.

Parameters
[in]clsA pointer to http_handler_t
[in]connectionConnection handle, e.g. used to send response.
[in]urlThe URL requested.
[in]method"GET" or "POST", others are disregarded.
[in]versionNot used for this callback.
[in]upload_dataData used for POST requests.
[in]upload_data_sizeSize of upload_data.
[out]con_clsFor exchange of connection-related data (here a struct gsad_connection_info).
Returns
MHD_NO in case of problems. MHD_YES if all is OK.

Definition at line 846 of file gsad_http_handler.c.

850 {
851  gsad_connection_info_t *con_info;
853  char *full_url;
854  gboolean new_connection;
855 
856  handlers = (http_handler_t *) cls;
857  con_info = *con_cls;
858  new_connection = (*con_cls == NULL);
859 
860  /* Never respond on first call of a GET. */
861  if ((str_equal (method, "GET")) && new_connection)
862  {
863  /* First call for this request, a GET. */
864 
865  /* Freed by MHD_OPTION_NOTIFY_COMPLETED callback, free_resources. */
866  con_info = g_malloc0 (sizeof (gsad_connection_info_t));
867  con_info->params = params_new ();
868  con_info->connectiontype = 2;
869 
870  MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
871  params_mhd_add, con_info->params);
872 
873  params_mhd_validate (con_info->params);
874 
875  *con_cls = (void *) con_info;
876  return MHD_YES;
877  }
878 
879  if (str_equal (method, "POST"))
880  {
881  if (new_connection)
882  {
883  /* First call for this request, a POST. */
884 
885  /* Freed by MHD_OPTION_NOTIFY_COMPLETED callback, free_resources. */
886  con_info = g_malloc0 (sizeof (gsad_connection_info_t));
887 
888  con_info->postprocessor = MHD_create_post_processor (
889  connection, POST_BUFFER_SIZE, serve_post, (void *) con_info);
890 
891  if (con_info->postprocessor == NULL)
892  {
893  g_free (con_info);
894  /* Both bad request or running out of memory will lead here, but
895  * we return the Bad Request page always, to prevent bad requests
896  * from leading to "Internal application error" in the log. */
897  send_response (connection, BAD_REQUEST_PAGE,
899  GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
900  return MHD_YES;
901  }
902 
903  con_info->params = params_new ();
904  con_info->connectiontype = 1;
905 
906  *con_cls = (void *) con_info;
907  return MHD_YES;
908  }
909 
910  /* Second or later call for this request, a POST. */
911 
912  if (*upload_data_size != 0)
913  {
914  MHD_post_process (con_info->postprocessor, upload_data,
915  *upload_data_size);
916  *upload_data_size = 0;
917  return MHD_YES;
918  }
919  }
920 
921  full_url = reconstruct_url (connection, url);
922  g_debug ("============= url: %s\n", full_url);
923  g_free (full_url);
924 
925  if (handlers != NULL)
926  {
927  return http_handler_start (connection, method, url, con_info, handlers,
928  NULL);
929  }
930 
931  return MHD_NO;
932 }

References BAD_REQUEST_PAGE, gsad_connection_info::connectiontype, GSAD_CONTENT_TYPE_TEXT_HTML, handlers, http_handler_start(), MHD_HTTP_NOT_ACCEPTABLE, gsad_connection_info::params, params_mhd_add(), params_mhd_validate(), params_new(), POST_BUFFER_SIZE, gsad_connection_info::postprocessor, reconstruct_url(), send_response(), serve_post(), and str_equal().

Here is the call graph for this function:

◆ http_handler_add()

http_handler_t* http_handler_add ( http_handler_t *  handlers,
http_handler_t *  handler 
)

Definition at line 95 of file gsad_http_handler.c.

96 {
97  http_handler_t *handler = handlers;
98 
99  if (handler == NULL)
100  {
101  return next;
102  }
103 
104  while (handler->next != NULL)
105  {
106  handler = handler->next;
107  }
108 
109  handler->next = next;
110 
111  return handlers;
112 }

References handlers, and http_handler::next.

Referenced by init_http_handlers(), and url_handler_add_func().

Here is the caller graph for this function:

◆ http_handler_free()

void http_handler_free ( http_handler_t *  handler)

Definition at line 167 of file gsad_http_handler.c.

168 {
169  if (!handler)
170  return;
171 
172  if (handler->next)
173  {
174  // free the chain
175  http_handler_free (handler->next);
176  }
177 
178  handler->free (handler);
179 }

References http_handler::free, http_handler_free(), and http_handler::next.

Referenced by cleanup_http_handlers(), http_handler_free(), method_router_free(), and url_handler_free().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ http_handler_new()

http_handler_t* http_handler_new ( http_handler_func_t  )

Definition at line 161 of file gsad_http_handler.c.

162 {
164 }

References http_handler_free_internal(), and http_handler_new_with_data().

Referenced by init_http_handlers(), and url_handler_add_func().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ http_handler_next()

int http_handler_next ( http_connection_t *  connection,
const char *  method,
const char *  url,
gsad_connection_info_t *  con_info,
http_handler_t *  handler,
void *  data 
)

Definition at line 127 of file gsad_http_handler.c.

130 {
131  http_handler_t *next;
132 
133  if (handler == NULL || handler->next == NULL)
134  {
135  return MHD_NO;
136  }
137 
138  next = handler->next;
139  return next->handle (connection, method, url, con_info, next, data);
140 }

References http_handler::handle, and http_handler::next.

Referenced by handle_get_post(), handle_get_user(), handle_invalid_method(), handle_setup_credentials(), handle_setup_user(), handle_url(), and handle_validate().

Here is the caller graph for this function:

◆ http_handler_start()

int http_handler_start ( http_connection_t *  connection,
const char *  method,
const char *  url,
gsad_connection_info_t *  con_info,
http_handler_t *  handler,
void *  data 
)

Definition at line 115 of file gsad_http_handler.c.

118 {
119  if (handler == NULL)
120  {
121  return MHD_NO;
122  }
123  return handler->handle (connection, method, url, con_info, handler, data);
124 }

References http_handler::handle.

Referenced by handle_get_post(), handle_request(), and handle_url().

Here is the caller graph for this function:

◆ init_http_handlers()

http_handler_t* init_http_handlers ( )

Definition at line 759 of file gsad_http_handler.c.

760 {
762  http_handler_t *gmp_post_handler;
763  http_handler_t *url_handlers;
764 
768 
770 
772  gmp_post_handler = http_handler_new (handle_gmp_post);
773 
775 
776  url_handlers = url_handler_new ("^/(img|js|css|locales)/.+$",
778  url_handler_add_func (url_handlers, "^/robots\\.txt$", handle_static_file);
779 
780  url_handler_add_func (url_handlers, "^/config\\.*js$", handle_static_config);
781  url_handler_add_func (url_handlers, "^/static/(img|js|css|media)/.+$",
783  url_handler_add_func (url_handlers, "^/manual/.+$", handle_static_file);
784 
788  http_handler_t *gmp_url_handler = url_handler_new ("^/gmp$", gmp_handler);
789 
790  http_handler_t *system_report_handler = http_handler_new (handle_setup_user);
791  http_handler_add (system_report_handler,
793  http_handler_add (system_report_handler,
795  http_handler_t *system_report_url_handler =
796  url_handler_new ("^/system_report/.+$", system_report_handler);
797 
798  http_handler_t *logout_handler = http_handler_new (handle_get_user);
799  http_handler_add (logout_handler, http_handler_new (handle_logout));
800  http_handler_t *logout_url_handler =
801  url_handler_new ("^/logout/?$", logout_handler);
802 
803  http_handler_add (url_handlers, gmp_url_handler);
804  http_handler_add (url_handlers, system_report_url_handler);
805  http_handler_add (url_handlers, logout_url_handler);
806 
808 
810  method_router_set_post_handler (method_router, gmp_post_handler);
811 
813 
814  return handlers;
815 }

References gvm_validator_add(), gvm_validator_new(), handle_get_user(), handle_gmp_get(), handle_gmp_post(), handle_index(), handle_invalid_method(), handle_logout(), handle_setup_credentials(), handle_setup_user(), handle_static_config(), handle_static_file(), handle_system_report(), handle_validate(), handlers, http_handler_add(), http_handler_new(), http_validator, method_router_new(), method_router_set_get_handler(), method_router_set_post_handler(), SLAVE_ID_REGEXP, TOKEN_REGEXP, url_handler_add_func(), and url_handler_new().

Here is the call graph for this function:

◆ method_router_new()

http_handler_t* method_router_new ( )

Definition at line 217 of file gsad_http_handler.c.

218 {
219  method_router_t *router = g_malloc0 (sizeof (method_router_t));
220  router->get = NULL;
221  router->post = NULL;
223  router);
224 }

References method_router::get, handle_get_post(), http_handler_new_with_data(), method_router_free(), and method_router::post.

Referenced by init_http_handlers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ method_router_set_get_handler()

void method_router_set_get_handler ( http_handler_t *  router,
http_handler_t *  handler 
)

Definition at line 227 of file gsad_http_handler.c.

228 {
230  method_router->get = handler;
231 }

References http_handler::data, and method_router::get.

Referenced by init_http_handlers().

Here is the caller graph for this function:

◆ method_router_set_post_handler()

void method_router_set_post_handler ( http_handler_t *  router,
http_handler_t *  handler 
)

Definition at line 234 of file gsad_http_handler.c.

235 {
237  method_router->post = handler;
238 }

References http_handler::data, and method_router::post.

Referenced by init_http_handlers().

Here is the caller graph for this function:

◆ url_handler_add_func()

http_handler_t* url_handler_add_func ( http_handler_t *  handlers,
const gchar *  regexp,
http_handler_func_t  handle 
)

Definition at line 290 of file gsad_http_handler.c.

292 {
293  http_handler_t *handler = http_handler_new (handle);
294  http_handler_t *url_handler = url_handler_new (regexp, handler);
295  return http_handler_add (handlers, url_handler);
296 }

References handlers, http_handler_add(), http_handler_new(), and url_handler_new().

Referenced by init_http_handlers().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ url_handler_new()

http_handler_t* url_handler_new ( const gchar *  regexp,
http_handler_t *  handler 
)

Definition at line 283 of file gsad_http_handler.c.

284 {
285  url_map_t *map = url_map_new (regexp, handler);
287 }

References handle_url(), http_handler_new_with_data(), url_handler_free(), and url_map_new().

Referenced by init_http_handlers(), and url_handler_add_func().

Here is the call graph for this function:
Here is the caller graph for this function:
http_handler::data
void * data
Definition: gsad_http_handler.c:81
handle_gmp_post
int handle_gmp_post(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:523
gsad_connection_info::params
params_t * params
Request parameters.
Definition: gsad_http.h:128
handlers
http_handler_t * handlers
Definition: gsad_http_handler.c:92
method_router_set_get_handler
void method_router_set_get_handler(http_handler_t *router, http_handler_t *handler)
Definition: gsad_http_handler.c:227
http_handler_free
void http_handler_free(http_handler_t *handler)
Definition: gsad_http_handler.c:167
MHD_HTTP_NOT_ACCEPTABLE
#define MHD_HTTP_NOT_ACCEPTABLE
The symbol is deprecated, but older versions (0.9.37 - Debian jessie) don't define it yet.
Definition: gsad_http.h:108
method_router
Definition: gsad_http_handler.c:84
params_mhd_add
int params_mhd_add(void *params, enum MHD_ValueKind kind, const char *name, const char *value)
method_router_set_post_handler
void method_router_set_post_handler(http_handler_t *router, http_handler_t *handler)
Definition: gsad_http_handler.c:234
method_router::get
http_handler_t * get
Definition: gsad_http_handler.c:86
handle_setup_credentials
int handle_setup_credentials(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:447
gvm_validator_free
void gvm_validator_free(validator_t validator)
Free a validator.
Definition: validator.c:266
url_map_new
url_map_t * url_map_new(const gchar *regexp, http_handler_t *handler)
Definition: gsad_http_handler.c:262
handle_setup_user
int handle_setup_user(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:396
http_handler::handle
http_handler_func_t handle
Definition: gsad_http_handler.c:79
gsad_connection_info::connectiontype
int connectiontype
1=POST, 2=GET.
Definition: gsad_http.h:131
url_handler_add_func
http_handler_t * url_handler_add_func(http_handler_t *handlers, const gchar *regexp, http_handler_func_t handle)
Definition: gsad_http_handler.c:290
POST_BUFFER_SIZE
#define POST_BUFFER_SIZE
Buffer size for POST processor.
Definition: gsad_http.h:101
gvm_validator_new
validator_t gvm_validator_new()
Create a new validator.
Definition: validator.c:102
handle_invalid_method
int handle_invalid_method(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:333
http_handler_new_with_data
http_handler_t * http_handler_new_with_data(http_handler_func_t func, http_handler_free_func_t freefunc, void *data)
Definition: gsad_http_handler.c:143
method_router::post
http_handler_t * post
Definition: gsad_http_handler.c:87
method_router_new
http_handler_t * method_router_new()
Definition: gsad_http_handler.c:217
http_handler
Definition: gsad_http_handler.c:76
handle_validate
int handle_validate(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:299
params_mhd_validate
void params_mhd_validate(void *params)
handle_static_config
int handle_static_config(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:720
handle_logout
int handle_logout(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:489
gvm_validator_add
void gvm_validator_add(validator_t validator, const char *name, const char *regex)
Add or overwrite a validation rule.
Definition: validator.c:116
http_validator
validator_t http_validator
Validator instance for http params.
Definition: gsad_http_handler.c:60
gsad_connection_info::postprocessor
struct MHD_PostProcessor * postprocessor
POST processor.
Definition: gsad_http.h:127
http_handler_add
http_handler_t * http_handler_add(http_handler_t *handlers, http_handler_t *next)
Definition: gsad_http_handler.c:95
url_handler_free
void url_handler_free(http_handler_t *handler)
Definition: gsad_http_handler.c:271
handle_url
int handle_url(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *current, void *data)
Definition: gsad_http_handler.c:241
handle_system_report
int handle_system_report(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:567
http_handler_free_internal
void http_handler_free_internal(http_handler_t *handler)
Definition: gsad_http_handler.c:155
reconstruct_url
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
Definition: gsad_http.c:785
send_response
int send_response(http_connection_t *connection, const char *content, int status_code, const gchar *sid, content_type_t content_type, const char *content_disposition, size_t content_length)
Sends a HTTP response.
Definition: gsad_http.c:244
url_map
URL regexp to handler function mapping.
Definition: gsad_http_handler.c:68
GSAD_CONTENT_TYPE_TEXT_HTML
@ GSAD_CONTENT_TYPE_TEXT_HTML
Definition: gsad_content_type.h:45
handle_get_post
int handle_get_post(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:182
str_equal
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
Definition: utils.c:35
SLAVE_ID_REGEXP
#define SLAVE_ID_REGEXP
Definition: gsad_http_handler.c:55
TOKEN_REGEXP
#define TOKEN_REGEXP
Definition: gsad_http_handler.c:54
http_handler_start
int http_handler_start(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:115
handle_gmp_get
int handle_gmp_get(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:508
BAD_REQUEST_PAGE
#define BAD_REQUEST_PAGE
Bad request error HTML.
Definition: gsad_http.h:80
handle_get_user
int handle_get_user(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:386
url_handler_new
http_handler_t * url_handler_new(const gchar *regexp, http_handler_t *handler)
Definition: gsad_http_handler.c:283
http_handler::next
http_handler_t * next
Definition: gsad_http_handler.c:78
params_new
params_t * params_new()
Make a params.
Definition: gsad_base.c:147
gsad_connection_info
Connection information.
Definition: gsad_http.h:125
serve_post
int serve_post(void *coninfo_cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size)
Serves part of a POST request.
Definition: gsad_http.c:927
method_router_free
void method_router_free(http_handler_t *handler)
Definition: gsad_http_handler.c:204
handle_static_file
int handle_static_file(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:683
handle_index
int handle_index(http_connection_t *connection, const char *method, const char *url, gsad_connection_info_t *con_info, http_handler_t *handler, void *data)
Definition: gsad_http_handler.c:667
http_handler::free
http_handler_free_func_t free
Definition: gsad_http_handler.c:80
http_handler_new
http_handler_t * http_handler_new(http_handler_func_t func)
Definition: gsad_http_handler.c:161