Greenbone Security Assistant  8.0.0
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 804 of file gsad_http_handler.c.

805 {
806  g_debug ("Cleaning up http handlers");
807 
809 
811 }
validator_t http_validator
Validator instance for http params.
void gvm_validator_free(validator_t validator)
Free a validator.
Definition: validator.c:266
http_handler_t * handlers
void http_handler_free(http_handler_t *handler)

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 832 of file gsad_http_handler.c.

836 {
837  gsad_connection_info_t *con_info;
839  char *full_url;
840 
841  handlers = (http_handler_t *) cls;
842  con_info = *con_cls;
843 
844  /* Never respond on first call of a GET. */
845  if ((!strcmp (method, "GET")) && *con_cls == NULL)
846  {
847  /* First call for this request, a GET. */
848 
849  /* Freed by MHD_OPTION_NOTIFY_COMPLETED callback, free_resources. */
850  con_info = g_malloc0 (sizeof (gsad_connection_info_t));
851  con_info->params = params_new ();
852  con_info->connectiontype = 2;
853 
854  MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND,
855  params_mhd_add, con_info->params);
856 
857  params_mhd_validate (con_info->params);
858 
859  *con_cls = (void *) con_info;
860  return MHD_YES;
861  }
862 
863  if (!strcmp (method, "POST"))
864  {
865  if (NULL == *con_cls)
866  {
867  /* First call for this request, a POST. */
868 
869  /* Freed by MHD_OPTION_NOTIFY_COMPLETED callback, free_resources. */
870  con_info = g_malloc0 (sizeof (gsad_connection_info_t));
871 
872  con_info->postprocessor = MHD_create_post_processor (
873  connection, POST_BUFFER_SIZE, serve_post, (void *) con_info);
874  if (NULL == con_info->postprocessor)
875  {
876  g_free (con_info);
877  /* Both bad request or running out of memory will lead here, but
878  * we return the Bad Request page always, to prevent bad requests
879  * from leading to "Internal application error" in the log. */
880  send_response (connection, BAD_REQUEST_PAGE,
882  GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
883  return MHD_YES;
884  }
885  con_info->params = params_new ();
886  con_info->connectiontype = 1;
887 
888  *con_cls = (void *) con_info;
889  return MHD_YES;
890  }
891 
892  /* Second or later call for this request, a POST. */
893 
894  if (0 != *upload_data_size)
895  {
896  MHD_post_process (con_info->postprocessor, upload_data,
897  *upload_data_size);
898  *upload_data_size = 0;
899  return MHD_YES;
900  }
901  }
902 
903  full_url = reconstruct_url (connection, url);
904  g_debug ("============= url: %s\n", full_url);
905  g_free (full_url);
906 
907  if (handlers != NULL)
908  {
909  return http_handler_start (connection, method, url, con_info, handlers,
910  NULL);
911  }
912 
913  return MHD_NO;
914 }
#define POST_BUFFER_SIZE
Buffer size for POST processor.
Definition: gsad_http.h:101
params_t * params
Request parameters.
Definition: gsad_http.h:128
int connectiontype
1=POST, 2=GET.
Definition: gsad_http.h:131
Connection information.
Definition: gsad_http.h:125
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
Definition: gsad_http.c:806
params_t * params_new()
Make a params.
Definition: gsad_base.c:147
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
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)
struct MHD_PostProcessor * postprocessor
POST processor.
Definition: gsad_http.h:127
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:948
void params_mhd_validate(void *params)
#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
#define BAD_REQUEST_PAGE
Bad request error HTML.
Definition: gsad_http.h:80
http_handler_t * handlers
int params_mhd_add(void *params, enum MHD_ValueKind kind, const char *name, const char *value)

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(), and serve_post().

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 94 of file gsad_http_handler.c.

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

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 166 of file gsad_http_handler.c.

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

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 160 of file gsad_http_handler.c.

161 {
163 }
http_handler_t * http_handler_new_with_data(http_handler_func_t func, http_handler_free_func_t freefunc, void *data)
void http_handler_free_internal(http_handler_t *handler)

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 126 of file gsad_http_handler.c.

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

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 114 of file gsad_http_handler.c.

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

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 743 of file gsad_http_handler.c.

744 {
746  http_handler_t *not_found_handler;
747  http_handler_t *gmp_post_handler;
748  http_handler_t *url_handlers;
749 
753 
755 
757  not_found_handler = http_handler_new (handle_not_found);
758  gmp_post_handler = http_handler_new (handle_gmp_post);
759 
761 
762  url_handlers = url_handler_new ("^/(img|js|css|locales)/.+$",
764  url_handler_add_func (url_handlers, "^/robots.txt$", handle_static_file);
765 
766  url_handler_add_func (url_handlers, "^/config.*js$", handle_static_config);
767  url_handler_add_func (url_handlers, "^/static/(img|js|css)/.+$",
769 
773  http_handler_t *gmp_url_handler = url_handler_new ("^/gmp$", gmp_handler);
774 
775  http_handler_t *system_report_handler = http_handler_new (handle_setup_user);
776  http_handler_add (system_report_handler,
778  http_handler_add (system_report_handler,
780  http_handler_t *system_report_url_handler =
781  url_handler_new ("^/system_report/.+$", system_report_handler);
782 
783  http_handler_t *logout_handler = http_handler_new (handle_get_user);
784  http_handler_add (logout_handler, http_handler_new (handle_logout));
785  http_handler_t *logout_url_handler =
786  url_handler_new ("^/logout/?$", logout_handler);
787 
788  http_handler_add (url_handlers, gmp_url_handler);
789  http_handler_add (url_handlers, system_report_url_handler);
790  http_handler_add (url_handlers, logout_url_handler);
791 
792  url_handler_add_func (url_handlers, "^/.*$", handle_index);
793  http_handler_add (url_handlers, not_found_handler);
794 
796  method_router_set_post_handler (method_router, gmp_post_handler);
797 
799 
800  return handlers;
801 }
int handle_not_found(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 * url_handler_add_func(http_handler_t *handlers, const gchar *regexp, http_handler_func_t handle)
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)
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)
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)
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)
validator_t http_validator
Validator instance for http params.
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)
http_handler_t * http_handler_add(http_handler_t *handlers, http_handler_t *next)
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)
#define SLAVE_ID_REGEXP
http_handler_t * url_handler_new(const gchar *regexp, http_handler_t *handler)
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)
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)
http_handler_t * method_router_new()
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)
http_handler_t * handlers
validator_t gvm_validator_new()
Create a new validator.
Definition: validator.c:102
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)
void gvm_validator_add(validator_t validator, const char *name, const char *regex)
Add or overwrite a validation rule.
Definition: validator.c:116
#define TOKEN_REGEXP
http_handler_t * http_handler_new(http_handler_func_t func)
void method_router_set_get_handler(http_handler_t *router, http_handler_t *handler)
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)
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)
void method_router_set_post_handler(http_handler_t *router, http_handler_t *handler)

References gvm_validator_add(), gvm_validator_new(), handle_get_user(), handle_gmp_get(), handle_gmp_post(), handle_index(), handle_invalid_method(), handle_logout(), handle_not_found(), 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 216 of file gsad_http_handler.c.

217 {
218  method_router_t *router = g_malloc0 (sizeof (method_router_t));
219  router->get = NULL;
220  router->post = NULL;
222  router);
223 }
http_handler_t * http_handler_new_with_data(http_handler_func_t func, http_handler_free_func_t freefunc, void *data)
http_handler_t * get
void method_router_free(http_handler_t *handler)
http_handler_t * 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)

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 226 of file gsad_http_handler.c.

227 {
229  method_router->get = handler;
230 }
http_handler_t * get

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 233 of file gsad_http_handler.c.

234 {
236  method_router->post = handler;
237 }
http_handler_t * post

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 289 of file gsad_http_handler.c.

291 {
292  http_handler_t *handler = http_handler_new (handle);
293  http_handler_t *url_handler = url_handler_new (regexp, handler);
294  return http_handler_add (handlers, url_handler);
295 }
http_handler_t * http_handler_add(http_handler_t *handlers, http_handler_t *next)
http_handler_t * url_handler_new(const gchar *regexp, http_handler_t *handler)
http_handler_t * handlers
http_handler_t * http_handler_new(http_handler_func_t func)

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 282 of file gsad_http_handler.c.

283 {
284  url_map_t *map = url_map_new (regexp, handler);
286 }
http_handler_t * http_handler_new_with_data(http_handler_func_t func, http_handler_free_func_t freefunc, void *data)
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)
URL regexp to handler function mapping.
void url_handler_free(http_handler_t *handler)
url_map_t * url_map_new(const gchar *regexp, http_handler_t *handler)

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: