Greenbone Security Assistant  8.0.0
gsad_http.c File Reference

HTTP handling. More...

#include "gsad_http.h"
#include "gsad_base.h"
#include "gsad_i18n.h"
#include "gsad_settings.h"
#include "utils.h"
#include <assert.h>
#include <gvm/base/networking.h>
#include <gvm/util/xmlutils.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Include dependency graph for gsad_http.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "gsad http"
 GLib log domain. More...
 

Functions

content_type_t guess_content_type (const gchar *path)
 Guess a content type from a file extension. More...
 
void gsad_add_content_type_header (http_response_t *response, content_type_t *ct)
 Adds content-type header fields to a response. More...
 
int send_redirect_to_uri (http_connection_t *connection, const char *uri, const gchar *sid)
 Sends a HTTP redirection to an uri. More...
 
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. More...
 
int handler_send_response (http_connection_t *connection, http_response_t *response, cmd_response_data_t *response_data, const gchar *sid)
 Send response for handle_request. More...
 
int handler_create_response (http_connection_t *connection, gchar *data, cmd_response_data_t *response_data, const gchar *sid)
 Create and send a response. More...
 
http_response_t * create_not_found_response (const gchar *url, cmd_response_data_t *response_data)
 Create a default 404 (not found) http response. More...
 
int handler_send_not_found (http_connection_t *connection, const gchar *url)
 Send a 404 response for a request. More...
 
int handler_send_reauthentication (http_connection_t *connection, int http_status_code, authentication_reason_t reason)
 Allow for reauthentication of a user. More...
 
int remove_sid (http_response_t *response)
 Attach expired SID cookie to response. More...
 
int attach_sid (http_response_t *response, const char *sid)
 Attach SID cookie to a response, resetting "expire" arg. More...
 
int attach_remove_sid (http_response_t *response, const gchar *sid)
 
static int file_reader (void *cls, uint64_t pos, char *buf, int max)
 Reads from a file. More...
 
http_response_t * file_content_response (http_connection_t *connection, const char *url, const char *path, cmd_response_data_t *response_data)
 Create a response to serve a file from a path. More...
 
static int append_param (void *string, enum MHD_ValueKind kind, const char *key, const char *value)
 Append a request param to a string. More...
 
gchar * reconstruct_url (http_connection_t *connection, const char *url)
 Reconstruct the URL for a connection. More...
 
void add_security_headers (http_response_t *response)
 Add security headers to a MHD response. More...
 
void add_guest_chart_content_security_headers (http_response_t *response)
 Add guest chart content security headers to a MHD response. More...
 
void add_cors_headers (http_response_t *response)
 
void add_forbid_caching_headers (http_response_t *response)
 Add header to forbid caching to a HTTP response. More...
 
int get_client_address (http_connection_t *conn, char *client_address)
 Get the client's address. More...
 
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. More...
 
char * gsad_message (credentials_t *credentials, const char *title, const char *function, int line, const char *msg, cmd_response_data_t *response_data)
 Handles fatal errors. More...
 

Detailed Description

HTTP handling.

Definition in file gsad_http.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "gsad http"

GLib log domain.

Definition at line 46 of file gsad_http.c.

Function Documentation

◆ add_cors_headers()

void add_cors_headers ( http_response_t *  response)

Definition at line 861 of file gsad_http.c.

862 {
863  const gchar *http_cors_origin = get_http_cors_origin ();
864 
865  if (strcmp (http_cors_origin, ""))
866  {
867  MHD_add_response_header (response, "Access-Control-Allow-Origin",
869  MHD_add_response_header (response, "Access-Control-Allow-Credentials",
870  "true");
871  }
872 }
const gchar * get_http_cors_origin()
const gchar * http_cors_origin
Current value of for HTTP header "Access-Control-Allow-Origin".
Definition: gsad_settings.c:77

References get_http_cors_origin(), and http_cors_origin.

Referenced by handler_send_response(), send_redirect_to_uri(), and send_response().

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

◆ add_forbid_caching_headers()

void add_forbid_caching_headers ( http_response_t *  response)

Add header to forbid caching to a HTTP response.

Parameters
[in]responseThe HTTP response to add the headers to.
[in]allow_caching1 to allow caching, 0 to forbid.

Definition at line 881 of file gsad_http.c.

882 {
883  MHD_add_response_header (response, MHD_HTTP_HEADER_EXPIRES, "-1");
884  MHD_add_response_header (response, MHD_HTTP_HEADER_CACHE_CONTROL,
885  "no-cache, no-store");
886  MHD_add_response_header (response, MHD_HTTP_HEADER_PRAGMA, "no-cache");
887 }

Referenced by handler_send_response(), send_redirect_to_uri(), and send_response().

Here is the caller graph for this function:

◆ add_guest_chart_content_security_headers()

void add_guest_chart_content_security_headers ( http_response_t *  response)

Add guest chart content security headers to a MHD response.

Definition at line 850 of file gsad_http.c.

851 {
852  if (strcmp (get_http_x_frame_options (), ""))
853  MHD_add_response_header (response, "X-Frame-Options",
855  if (strcmp (get_http_content_security_policy (), ""))
856  MHD_add_response_header (response, "Content-Security-Policy",
858 }
const gchar * get_http_x_frame_options()
const gchar * get_http_content_security_policy()
const gchar * get_http_guest_chart_content_security_policy()
const gchar * get_http_guest_chart_x_frame_options()

References get_http_content_security_policy(), get_http_guest_chart_content_security_policy(), get_http_guest_chart_x_frame_options(), and get_http_x_frame_options().

Here is the call graph for this function:

◆ add_security_headers()

void add_security_headers ( http_response_t *  response)

Add security headers to a MHD response.

Definition at line 828 of file gsad_http.c.

829 {
831  const gchar *http_content_security_policy =
833  const gchar *http_strict_transport_security =
835 
836  if (strcmp (http_x_frame_options, ""))
837  MHD_add_response_header (response, "X-Frame-Options", http_x_frame_options);
838  if (strcmp (http_content_security_policy, ""))
839  MHD_add_response_header (response, "Content-Security-Policy",
842  MHD_add_response_header (response, "Strict-Transport-Security",
844 }
const gchar * get_http_strict_transport_security()
const gchar * get_http_x_frame_options()
const gchar * http_strict_transport_security
Current value of for HTTP header "Strict-Transport-Security".
Definition: gsad_settings.c:72
const gchar * http_x_frame_options
Current value for HTTP header "X-Frame-Options".
Definition: gsad_settings.c:52
const gchar * http_content_security_policy
Current value for HTTP header "Content-Security-Policy".
Definition: gsad_settings.c:57
const gchar * get_http_content_security_policy()

References get_http_content_security_policy(), get_http_strict_transport_security(), get_http_x_frame_options(), http_content_security_policy, http_strict_transport_security, and http_x_frame_options.

Referenced by handler_send_response(), send_redirect_to_uri(), and send_response().

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

◆ append_param()

static int append_param ( void *  string,
enum MHD_ValueKind  kind,
const char *  key,
const char *  value 
)
static

Append a request param to a string.

Parameters
[in]stringString.
[in]kindKind of request data.
[in]keyKey.
[in]valueValue.
Returns
MHD_YES.

Definition at line 775 of file gsad_http.c.

777 {
778  if (value == NULL)
779  /* http://foo/bar?key */
780  return MHD_YES;
781  if (key == NULL)
782  {
783  assert (0);
784  return MHD_YES;
785  }
786  /* http://foo/bar?key=value */
787  if (strcmp (key, "token") && strcmp (key, "r"))
788  {
789  g_string_append ((GString *) string, key);
790  g_string_append ((GString *) string, "=");
791  g_string_append ((GString *) string, value);
792  g_string_append ((GString *) string, "&");
793  }
794  return MHD_YES;
795 }

Referenced by reconstruct_url().

Here is the caller graph for this function:

◆ attach_remove_sid()

int attach_remove_sid ( http_response_t *  response,
const gchar *  sid 
)

Attach or remove session id

If sid is "0" the session id will be removed. Otherwise if the sid is not NULL the sid will be attached to the response.

Parameters
[in]responseHTTP response
[in]sidSession ID
Returns
MHD_YES on success, MHD_NO on failure

Definition at line 636 of file gsad_http.c.

637 {
638  if (sid)
639  {
640  if (str_equal (sid, REMOVE_SID))
641  {
642  if (remove_sid (response) == MHD_NO)
643  {
644  MHD_destroy_response (response);
645  g_warning ("%s: failed to remove SID, dropping request",
646  __FUNCTION__);
647  return MHD_NO;
648  }
649  }
650  else
651  {
652  if (attach_sid (response, sid) == MHD_NO)
653  {
654  MHD_destroy_response (response);
655  g_warning ("%s: failed to attach SID, dropping request",
656  __FUNCTION__);
657  return MHD_NO;
658  }
659  }
660  }
661  return MHD_YES;
662 }
int attach_sid(http_response_t *response, const char *sid)
Attach SID cookie to a response, resetting "expire" arg.
Definition: gsad_http.c:558
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
Definition: utils.c:35
#define REMOVE_SID
Definition: gsad_http.h:60
int remove_sid(http_response_t *response)
Attach expired SID cookie to response.
Definition: gsad_http.c:512

References attach_sid(), REMOVE_SID, remove_sid(), and str_equal().

Referenced by handler_send_response(), send_redirect_to_uri(), and send_response().

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

◆ attach_sid()

int attach_sid ( http_response_t *  response,
const char *  sid 
)

Attach SID cookie to a response, resetting "expire" arg.

Parameters
[in]responseResponse.
[in]sidSession ID.
Returns
MHD_NO in case of problems. MHD_YES if all is OK.

Definition at line 558 of file gsad_http.c.

559 {
560  int ret, timeout;
561  gchar *value;
562  gchar *locale;
563  char expires[EXPIRES_LENGTH + 1];
564  struct tm expire_time_broken;
565  time_t now, expire_time;
566  gchar *tz;
567 
568  /* Set up the expires param. */
569 
570  /* Store current TZ, switch to GMT. */
571  tz = getenv ("TZ") ? g_strdup (getenv ("TZ")) : NULL;
572  if (setenv ("TZ", "GMT", 1) == -1)
573  {
574  g_critical ("%s: failed to set TZ\n", __FUNCTION__);
575  g_free (tz);
576  exit (EXIT_FAILURE);
577  }
578  tzset ();
579 
580  locale = g_strdup (setlocale (LC_ALL, NULL));
581  setlocale (LC_ALL, "C");
582 
583  timeout = get_session_timeout () * 60 + 30;
584 
585  now = time (NULL);
586  expire_time = now + timeout;
587  if (localtime_r (&expire_time, &expire_time_broken) == NULL)
588  abort ();
589  ret = strftime (expires, EXPIRES_LENGTH, "%a, %d %b %Y %T GMT",
590  &expire_time_broken);
591  if (ret == 0)
592  abort ();
593 
594  setlocale (LC_ALL, locale);
595  g_free (locale);
596 
597  /* Revert to stored TZ. */
598  if (tz)
599  {
600  if (setenv ("TZ", tz, 1) == -1)
601  {
602  g_warning ("%s: Failed to switch to original TZ", __FUNCTION__);
603  g_free (tz);
604  exit (EXIT_FAILURE);
605  }
606  }
607  else
608  unsetenv ("TZ");
609  g_free (tz);
610 
611  /* Add the cookie.
612  *
613  * Tim Brown's suggested cookie included a domain attribute. How would
614  * we get the domain in here? Maybe a --domain option. */
615 
616  value = g_strdup_printf (
617  SID_COOKIE_NAME "=%s; expires=%s; max-age=%d; path=/; %sHTTPonly", sid,
618  expires, timeout, (is_use_secure_cookie () ? "secure; " : ""));
619  ret = MHD_add_response_header (response, "Set-Cookie", value);
620  g_free (value);
621  return ret;
622 }
int get_session_timeout()
gboolean is_use_secure_cookie()
#define EXPIRES_LENGTH
Max length of cookie expires param.
Definition: gsad_http.h:44
#define SID_COOKIE_NAME
Name of the cookie used to store the SID.
Definition: gsad_http.h:58

References EXPIRES_LENGTH, get_session_timeout(), is_use_secure_cookie(), and SID_COOKIE_NAME.

Referenced by attach_remove_sid().

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

◆ create_not_found_response()

http_response_t* create_not_found_response ( const gchar *  url,
cmd_response_data_t *  response_data 
)

Create a default 404 (not found) http response.

Parameters
[in]urlRequested (not found) url
[out]response_dataResponse data to return
Returns
A http response

Definition at line 393 of file gsad_http.c.

394 {
395  http_response_t *response;
396  int len;
397 
398  cmd_response_data_set_status_code (response_data, MHD_HTTP_NOT_FOUND);
399 
400  gchar *msg = g_strdup_printf (
401  "<!DOCTYPE html>"
402  "<html>"
403  "<head>"
404  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
405  "<link rel=\"icon\" href=\"/img/favicon.gif\" type=\"image/gif\"></link>"
406  "<title>Greenbone Security Assistant</title>"
407  "</head>"
408  "<body>"
409  "<h1>URL not found</h1>"
410  "<p>"
411  "The requested URL %s is not available"
412  "</p>"
413  "</body>"
414  "</html>",
415  url
416  );
417 
418  cmd_response_data_set_content_type (response_data,
420 
421  cmd_response_data_set_content_length (response_data, strlen (msg));
422 
423  len = cmd_response_data_get_content_length (response_data);
424  response = MHD_create_response_from_buffer (len, msg, MHD_RESPMEM_MUST_COPY);
425  g_free (msg);
426  return response;
427 }
void cmd_response_data_set_content_type(cmd_response_data_t *data, content_type_t content_type)
Set content type of cmd_response_data_t struct.
Definition: gsad_cmd.c:134
void cmd_response_data_set_content_length(cmd_response_data_t *data, gsize content_length)
Set response content length of cmd_response_data_t struct.
Definition: gsad_cmd.c:186
struct MHD_Response http_response_t
Definition: gsad_http.h:137
void cmd_response_data_set_status_code(cmd_response_data_t *data, int http_status_code)
Set status code of cmd_response_data_t struct.
Definition: gsad_cmd.c:160
gsize cmd_response_data_get_content_length(cmd_response_data_t *data)
Get response content length of cmd_response_data_t struct.
Definition: gsad_cmd.c:200

References cmd_response_data_get_content_length(), cmd_response_data_set_content_length(), cmd_response_data_set_content_type(), cmd_response_data_set_status_code(), and GSAD_CONTENT_TYPE_TEXT_HTML.

Referenced by file_content_response(), and handler_send_not_found().

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

◆ file_content_response()

http_response_t* file_content_response ( http_connection_t *  connection,
const char *  url,
const char *  path,
cmd_response_data_t *  response_data 
)

Create a response to serve a file from a path.

Parameters
[in]connectionConnection.
[in]urlRequested URL.
[in]pathPath to file.
[out]response_dataReturn response data
Returns
Response to send in combination with the response code. NULL only if file information could not be retrieved.

Definition at line 694 of file gsad_http.c.

696 {
697  char date_2822[DATE_2822_LEN];
698  struct tm *mtime;
699  time_t next_week;
700  http_response_t *response;
701  FILE *file;
702  struct stat buf;
703 
704  cmd_response_data_set_status_code (response_data, MHD_HTTP_OK);
705 
706  if (!g_file_test (path, (G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)))
707  {
708  /* path does not exists or is not a file */
709  /* return index.html to show page not found via js */
710  g_debug ("File %s not found. Return index.html", path);
711 
712  path = "index.html";
713  cmd_response_data_set_status_code (response_data, MHD_HTTP_NOT_FOUND);
714  }
715 
716  file = fopen (path, "r"); /* this file is just read and sent */
717 
718  if (file == NULL)
719  {
720  g_debug ("File %s failed, ", path);
721  return create_not_found_response (url, response_data);
722  }
723 
724  /* Guess content type. */
726 
727  if (stat (path, &buf))
728  {
729  /* File information could not be retrieved. */
730  g_critical ("%s: file <%s> can not be stat'ed.\n", __FUNCTION__, path);
731  fclose (file);
732  return create_not_found_response (url, response_data);
733  }
734 
735  /* Make sure the requested path really is a file. */
736  if ((buf.st_mode & S_IFMT) != S_IFREG)
737  {
738  fclose (file);
739  return create_not_found_response (url, response_data);
740  }
741 
742  response = MHD_create_response_from_callback (
743  buf.st_size, 32 * 1024, (MHD_ContentReaderCallback) &file_reader, file,
744  (MHD_ContentReaderFreeCallback) &fclose);
745 
746  mtime = localtime (&buf.st_mtime);
747  if (mtime
748  && strftime (date_2822, DATE_2822_LEN, "%a, %d %b %Y %H:%M:%S %Z", mtime))
749  {
750  MHD_add_response_header (response, "Last-Modified", date_2822);
751  }
752 
753  next_week = time (NULL) + 7 * 24 * 60 * 60;
754  mtime = localtime (&next_week);
755  if (mtime
756  && strftime (date_2822, DATE_2822_LEN, "%a, %d %b %Y %H:%M:%S %Z", mtime))
757  {
758  MHD_add_response_header (response, "Expires", date_2822);
759  }
760 
761  return response;
762 }
static int file_reader(void *cls, uint64_t pos, char *buf, int max)
Reads from a file.
Definition: gsad_http.c:674
content_type_t guess_content_type(const gchar *path)
Guess a content type from a file extension.
Definition: gsad_http.c:56
void cmd_response_data_set_content_type(cmd_response_data_t *data, content_type_t content_type)
Set content type of cmd_response_data_t struct.
Definition: gsad_cmd.c:134
http_response_t * create_not_found_response(const gchar *url, cmd_response_data_t *response_data)
Create a default 404 (not found) http response.
Definition: gsad_http.c:393
struct MHD_Response http_response_t
Definition: gsad_http.h:137
#define DATE_2822_LEN
At least maximum length of rfc2822 format date.
Definition: gsad_http.h:39
void cmd_response_data_set_status_code(cmd_response_data_t *data, int http_status_code)
Set status code of cmd_response_data_t struct.
Definition: gsad_cmd.c:160

References cmd_response_data_set_content_type(), cmd_response_data_set_status_code(), create_not_found_response(), DATE_2822_LEN, file_reader(), and guess_content_type().

Referenced by handle_index(), handle_static_config(), and handle_static_file().

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

◆ file_reader()

static int file_reader ( void *  cls,
uint64_t  pos,
char *  buf,
int  max 
)
static

Reads from a file.

Parameters
[in]clsFile.
[in]posPosition in file to start reading.
[out]bufBuffer to read into.
[in]maxMaximum number of bytes to read.
Returns
The number of bytes read.

Definition at line 674 of file gsad_http.c.

675 {
676  FILE *file = cls;
677 
678  fseek (file, pos, SEEK_SET);
679  return fread (buf, 1, max, file);
680 }

Referenced by file_content_response().

Here is the caller graph for this function:

◆ get_client_address()

int get_client_address ( http_connection_t *  conn,
char *  client_address 
)

Get the client's address.

Parameters
[in]connConnection.
[out]client_addressBuffer to store client address. Must have at least INET6_ADDRSTRLEN bytes.
Returns
0 success, 1 invalid UTF-8 in X-Real-IP header

Definition at line 899 of file gsad_http.c.

900 {
901  const char *x_real_ip;
902 
903  /* First try X-Real-IP header (unless told to ignore), then MHD connection. */
904 
905  x_real_ip = MHD_lookup_connection_value (conn, MHD_HEADER_KIND, "X-Real-IP");
906 
907  if (!is_ignore_http_x_real_ip () && x_real_ip
908  && g_utf8_validate (x_real_ip, -1, NULL) == FALSE)
909  return 1;
910  else if (!is_ignore_http_x_real_ip () && x_real_ip != NULL)
911  strncpy (client_address, x_real_ip, INET6_ADDRSTRLEN);
912  else if (is_unix_socket ())
913  strncpy (client_address, "unix_socket", INET6_ADDRSTRLEN);
914  else
915  {
916  const union MHD_ConnectionInfo *info;
917 
918  info = MHD_get_connection_info (conn, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
919  sockaddr_as_str ((struct sockaddr_storage *) info->client_addr,
920  client_address);
921  }
922  return 0;
923 }
gboolean is_unix_socket()
gboolean is_ignore_http_x_real_ip()

References is_ignore_http_x_real_ip(), and is_unix_socket().

Referenced by get_user_from_connection(), handle_gmp_post(), and handle_setup_credentials().

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

◆ gsad_add_content_type_header()

void gsad_add_content_type_header ( http_response_t *  response,
content_type_t *  ct 
)

Adds content-type header fields to a response.

This function should be called only once per response and is the only function where values of enum content_types are translated into strings.

Parameters
[in,out]responseResponse to add header to.
[in]ctContent Type to set.

Definition at line 89 of file gsad_http.c.

90 {
91  if (!response)
92  return;
93 
94  switch (*ct)
95  {
97  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
98  "application/deb");
99  break;
101  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
102  "application/exe");
103  break;
105  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
106  "application/xhtml+xml");
107  break;
109  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
110  "application/key");
111  break;
113  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
114  "application/nbe");
115  break;
117  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
118  "application/pdf");
119  break;
121  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
122  "application/rpm");
123  break;
125  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
126  "application/xml; charset=utf-8");
127  break;
129  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
130  "image/png");
131  break;
133  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
134  "image/svg+xml");
135  break;
137  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
138  "image/gif");
139  break;
141  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
142  "application/octet-stream");
143  break;
145  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
146  "text/css");
147  break;
149  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
150  "text/html; charset=utf-8");
151  break;
153  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
154  "text/javascript");
155  break;
157  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
158  "text/plain; charset=utf-8");
159  break;
161  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
162  "application/json; charset=utf-8");
163  break;
165  break;
166  default:
167  MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE,
168  "text/plain; charset=utf-8");
169  break;
170  }
171 }

References GSAD_CONTENT_TYPE_APP_DEB, GSAD_CONTENT_TYPE_APP_EXE, GSAD_CONTENT_TYPE_APP_JSON, GSAD_CONTENT_TYPE_APP_KEY, GSAD_CONTENT_TYPE_APP_NBE, GSAD_CONTENT_TYPE_APP_PDF, GSAD_CONTENT_TYPE_APP_RPM, GSAD_CONTENT_TYPE_APP_XHTML, GSAD_CONTENT_TYPE_APP_XML, GSAD_CONTENT_TYPE_DONE, GSAD_CONTENT_TYPE_IMAGE_GIF, GSAD_CONTENT_TYPE_IMAGE_PNG, GSAD_CONTENT_TYPE_IMAGE_SVG, GSAD_CONTENT_TYPE_OCTET_STREAM, GSAD_CONTENT_TYPE_TEXT_CSS, GSAD_CONTENT_TYPE_TEXT_HTML, GSAD_CONTENT_TYPE_TEXT_JS, and GSAD_CONTENT_TYPE_TEXT_PLAIN.

Referenced by handler_send_response(), and send_response().

Here is the caller graph for this function:

◆ gsad_message()

char* gsad_message ( credentials_t *  credentials,
const char *  title,
const char *  function,
int  line,
const char *  msg,
cmd_response_data_t *  response_data 
)

Handles fatal errors.

Todo:
Make it accept formatted strings.
Parameters
[in]credentialsUser authentication information.
[in]titleThe title for the message.
[in]functionThe function in which the error occurred.
[in]lineThe line number at which the error occurred.
[in]msgThe response message.
[out]response_dataExtra data return for the HTTP response.
Returns
An XML document as a newly allocated string.

Definition at line 978 of file gsad_http.c.

981 {
982  gchar *xml, *xmltitle;
983 
984  if (function)
985  {
986  xmltitle = g_strdup_printf ("<title>%s: %s:%i (GSA %s)</title>", title,
987  function, line, GSAD_VERSION);
988  }
989  else
990  {
991  xmltitle =
992  g_strdup_printf ("<title>%s (GSA %s)</title>", title, GSAD_VERSION);
993  }
994 
995  if (credentials)
996  {
997  gchar *pre;
998  time_t now;
999  char ctime_now[200];
1001 
1002  now = time (NULL);
1003  ctime_r_strip_newline (&now, ctime_now);
1004 
1005  pre = g_markup_printf_escaped (
1006  "<envelope>"
1007  "<version>%s</version>"
1008  "<vendor_version>%s</vendor_version>"
1009  "<token>%s</token>"
1010  "<time>%s</time>"
1011  "<login>%s</login>"
1012  "<role>%s</role>"
1013  "<i18n>%s</i18n>"
1014  "<client_address>%s</client_address>",
1015  GSAD_VERSION, vendor_version_get (), user_get_token (user), ctime_now,
1018 
1019  xml = g_strdup_printf ("%s"
1020  "<gsad_response>"
1021  "%s"
1022  "<message>%s</message>"
1023  "</gsad_response>"
1024  "<capabilities>%s</capabilities>"
1025  "</envelope>",
1026  pre, xmltitle, msg ? msg : "",
1028 
1029  g_free (pre);
1030  }
1031  else
1032  {
1033  xml =
1034  g_strdup_printf ("<envelope>"
1035  "<version>%s</version>"
1036  "<vendor_version>%s</vendor_version>"
1037  "<gsad_response>"
1038  "%s"
1039  "<message>%s</message>"
1040  "<token></token>"
1041  "</gsad_response>"
1042  "</envelope>",
1043  GSAD_VERSION, vendor_version_get (),
1044  xmltitle, msg ? msg : "");
1045  }
1046 
1047  g_free (xmltitle);
1048 
1050  cmd_response_data_set_content_length (response_data, strlen (xml));
1051 
1052  return xml;
1053 }
Structure of credential related information.
const gchar * vendor_version_get()
Get the vendor version.
const gchar * user_get_role(user_t *user)
Definition: gsad_user.c:211
const gchar * user_get_username(user_t *user)
Definition: gsad_user.c:157
const gchar * credentials_get_language(credentials_t *cred)
const gchar * user_get_token(user_t *user)
Definition: gsad_user.c:175
void cmd_response_data_set_content_type(cmd_response_data_t *data, content_type_t content_type)
Set content type of cmd_response_data_t struct.
Definition: gsad_cmd.c:134
const gchar * user_get_capabilities(user_t *user)
Definition: gsad_user.c:181
void cmd_response_data_set_content_length(cmd_response_data_t *data, gsize content_length)
Set response content length of cmd_response_data_t struct.
Definition: gsad_cmd.c:186
User information structure, for sessions.
Definition: gsad_user.c:42
char * ctime_r_strip_newline(time_t *time, char *string)
Return string from ctime_r with newline replaces with terminator.
Definition: gsad_base.c:111
user_t * credentials_get_user(credentials_t *cred)
const gchar * user_get_client_address(user_t *user)
Definition: gsad_user.c:199

References cmd_response_data_set_content_length(), cmd_response_data_set_content_type(), credentials_get_language(), credentials_get_user(), ctime_r_strip_newline(), GSAD_CONTENT_TYPE_APP_XML, user_get_capabilities(), user_get_client_address(), user_get_role(), user_get_token(), user_get_username(), and vendor_version_get().

Referenced by auth_settings_gmp(), bulk_delete_gmp(), check_modify_config(), clone_gmp(), create_agent_gmp(), create_alert_gmp(), create_asset_gmp(), create_config_gmp(), create_container_task_gmp(), create_credential_gmp(), create_filter_gmp(), create_group_gmp(), create_host_gmp(), create_note_gmp(), create_override_gmp(), create_permission_gmp(), create_permissions_gmp(), create_port_list_gmp(), create_port_range_gmp(), create_report_gmp(), create_role_gmp(), create_scanner_gmp(), create_schedule_gmp(), create_tag_gmp(), create_target_gmp(), create_task_gmp(), create_ticket_gmp(), create_user_gmp(), delete_asset_gmp(), delete_resource(), download_agent_gmp(), download_ca_pub(), download_credential_gmp(), download_key_pub(), download_ssl_cert(), edit_alert(), edit_resource(), edit_role(), empty_trashcan_gmp(), export_many(), export_preference_file_gmp(), export_resource(), get_aggregate_gmp(), get_alert(), get_alerts(), get_asset(), get_assets(), get_config(), get_config_family(), get_config_nvt(), get_feeds_gmp(), get_info(), get_many(), get_one(), get_report(), get_setting_gmp(), get_settings_gmp(), get_system_reports_gmp(), get_user(), get_users(), handle_system_report(), handler_send_reauthentication(), import_config_gmp(), import_port_list_gmp(), import_report_format_gmp(), move_task_gmp(), new_alert(), report_alert_gmp(), resource_action(), restore_gmp(), run_wizard_gmp(), save_agent_gmp(), save_alert_gmp(), save_asset_gmp(), save_auth_gmp(), save_config_family_gmp(), save_config_gmp(), save_config_nvt_gmp(), save_container_task_gmp(), save_credential_gmp(), save_filter_gmp(), save_group_gmp(), save_my_settings_gmp(), save_note_gmp(), save_osp_prefs(), save_override_gmp(), save_permission_gmp(), save_port_list_gmp(), save_report_format_gmp(), save_role_gmp(), save_scanner_gmp(), save_schedule_gmp(), save_setting_gmp(), save_tag_gmp(), save_target_gmp(), save_task_gmp(), save_ticket_gmp(), save_user_gmp(), simple_gmpf(), sync_config_gmp(), sync_feed(), test_alert_gmp(), toggle_tag_gmp(), verify_agent_gmp(), verify_report_format_gmp(), verify_scanner_gmp(), wizard(), and wizard_get().

Here is the call graph for this function:

◆ guess_content_type()

content_type_t guess_content_type ( const gchar *  path)

Guess a content type from a file extension.

Parameters
[in]pathfilename with extension
Returns
a content_type_t for the file

Definition at line 56 of file gsad_http.c.

57 {
58  /* Guess content type. */
59  if (g_str_has_suffix (path, ".png"))
61  else if (g_str_has_suffix (path, ".svg"))
63  else if (g_str_has_suffix (path, ".html"))
65  else if (g_str_has_suffix (path, ".css"))
67  else if (g_str_has_suffix (path, ".js"))
69  else if (g_str_has_suffix (path, ".gif"))
71  else if (g_str_has_suffix (path, ".json"))
73  else if (g_str_has_suffix (path, ".txt"))
75  else
77 }

References GSAD_CONTENT_TYPE_APP_JSON, GSAD_CONTENT_TYPE_IMAGE_GIF, GSAD_CONTENT_TYPE_IMAGE_PNG, GSAD_CONTENT_TYPE_IMAGE_SVG, GSAD_CONTENT_TYPE_OCTET_STREAM, GSAD_CONTENT_TYPE_TEXT_CSS, GSAD_CONTENT_TYPE_TEXT_HTML, GSAD_CONTENT_TYPE_TEXT_JS, and GSAD_CONTENT_TYPE_TEXT_PLAIN.

Referenced by file_content_response().

Here is the caller graph for this function:

◆ handler_create_response()

int handler_create_response ( http_connection_t *  connection,
gchar *  data,
cmd_response_data_t *  response_data,
const gchar *  sid 
)

Create and send a response.

The passed response data will be freed and can't be used afterwards

Parameters
[in]connectionConnection handle, e.g. used to send response.
[in]dataData to send in response
[in]response_dataResponse data struct. Response data will be freed.
[in]sidSession ID, or NULL. "0" to remove session.
Returns
MHD_YES on success, else MHD_NO.

Definition at line 368 of file gsad_http.c.

370 {
371  http_response_t *response;
372  gsize len = 0;
373 
374  len = cmd_response_data_get_content_length (response_data);
375  if (len == 0 && data)
376  {
377  len = strlen (data);
378  }
379 
380  response = MHD_create_response_from_buffer (len, data, MHD_RESPMEM_MUST_FREE);
381  return handler_send_response (connection, response, response_data, sid);
382 }
int handler_send_response(http_connection_t *connection, http_response_t *response, cmd_response_data_t *response_data, const gchar *sid)
Send response for handle_request.
Definition: gsad_http.c:295
struct MHD_Response http_response_t
Definition: gsad_http.h:137
gsize cmd_response_data_get_content_length(cmd_response_data_t *data)
Get response content length of cmd_response_data_t struct.
Definition: gsad_cmd.c:200

References cmd_response_data_get_content_length(), and handler_send_response().

Referenced by handle_static_config(), handle_system_report(), handler_send_reauthentication(), and login().

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

◆ handler_send_not_found()

int handler_send_not_found ( http_connection_t *  connection,
const gchar *  url 
)

Send a 404 response for a request.

Parameters
[in]connectionConnection handle, e.g. used to send response.
[in]urlRequested url.
Returns
MHD_YES on success. MHD_NO on errors.

Definition at line 438 of file gsad_http.c.

439 {
440  http_response_t *response;
441  cmd_response_data_t *response_data;
442 
443  response_data = cmd_response_data_new ();
444 
445  response = create_not_found_response (url, response_data);
446  return handler_send_response (connection, response, response_data, NULL);
447 }
cmd_response_data_t * cmd_response_data_new()
Allocates memory for a cmd_response_data_t sturct and initializes it.
Definition: gsad_cmd.c:65
Response information for commands.
Definition: gsad_cmd.c:33
http_response_t * create_not_found_response(const gchar *url, cmd_response_data_t *response_data)
Create a default 404 (not found) http response.
Definition: gsad_http.c:393
int handler_send_response(http_connection_t *connection, http_response_t *response, cmd_response_data_t *response_data, const gchar *sid)
Send response for handle_request.
Definition: gsad_http.c:295
struct MHD_Response http_response_t
Definition: gsad_http.h:137

References cmd_response_data_new(), create_not_found_response(), and handler_send_response().

Referenced by handle_not_found(), and handle_validate().

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

◆ handler_send_reauthentication()

int handler_send_reauthentication ( http_connection_t *  connection,
int  http_status_code,
authentication_reason_t  reason 
)

Allow for reauthentication of a user.

Parameters
[in]connectionConnection handle, e.g. used to send response.
[in]http_status_codeHTTP status code for the response.
[in]reasonReason for re-authentication
Returns
MHD_YES on success. MHD_NO on errors.

Definition at line 459 of file gsad_http.c.

462 {
463  const char *msg;
464 
465  switch (reason)
466  {
467  case LOGIN_FAILED:
468  msg = "Login failed.";
469  break;
470  case LOGIN_ERROR:
471  msg = "Login failed. Error during authentication.";
472  break;
473  case GMP_SERVICE_DOWN:
474  msg = "Login failed. GMP Service is down.";
475  break;
476  case SESSION_EXPIRED:
477  msg = "Session expired. Please login again.";
478  break;
479  case LOGOUT_ALREADY:
480  msg = "Already logged out.";
481  break;
482  case BAD_MISSING_TOKEN:
483  msg = "Token missing or bad. Please login again.";
484  break;
485  case BAD_MISSING_COOKIE:
486  msg = "Cookie missing or bad. Please login again.";
487  break;
488  case LOGOUT:
489  msg = "Successfully logged out.";
490  break;
491  default:
492  msg = "";
493  }
494 
495  cmd_response_data_t *response_data = cmd_response_data_new ();
496  cmd_response_data_set_status_code (response_data, http_status_code);
497 
498  gchar *xml = gsad_message (NULL, "Authentication required", __FUNCTION__,
499  __LINE__, msg, response_data);
500 
501  return handler_create_response (connection, xml, response_data, REMOVE_SID);
502 }
cmd_response_data_t * cmd_response_data_new()
Allocates memory for a cmd_response_data_t sturct and initializes it.
Definition: gsad_cmd.c:65
Response information for commands.
Definition: gsad_cmd.c:33
char * gsad_message(credentials_t *credentials, const char *title, const char *function, int line, const char *msg, cmd_response_data_t *response_data)
Handles fatal errors.
Definition: gsad_http.c:978
int handler_create_response(http_connection_t *connection, gchar *data, cmd_response_data_t *response_data, const gchar *sid)
Create and send a response.
Definition: gsad_http.c:368
#define REMOVE_SID
Definition: gsad_http.h:60
void cmd_response_data_set_status_code(cmd_response_data_t *data, int http_status_code)
Set status code of cmd_response_data_t struct.
Definition: gsad_cmd.c:160

References BAD_MISSING_COOKIE, BAD_MISSING_TOKEN, cmd_response_data_new(), cmd_response_data_set_status_code(), GMP_SERVICE_DOWN, gsad_message(), handler_create_response(), LOGIN_ERROR, LOGIN_FAILED, LOGOUT, LOGOUT_ALREADY, REMOVE_SID, and SESSION_EXPIRED.

Referenced by handle_setup_user(), handle_system_report(), and login().

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

◆ handler_send_response()

int handler_send_response ( http_connection_t *  connection,
http_response_t *  response,
cmd_response_data_t *  response_data,
const gchar *  sid 
)

Send response for handle_request.

The passed response data will be freed and can't be used afterwards

Parameters
[in]connectionConnection handle, e.g. used to send response.
[in]responseResponse.
[in]response_dataResponse data struct. Response data will be freed.
[in]sidSession ID, or NULL. "0" to remove session.
Returns
MHD_YES on success, else MHD_NO.

Definition at line 295 of file gsad_http.c.

297 {
298  int ret;
299  const gchar *content_disposition;
301  int status_code;
302 
303  if (attach_remove_sid (response, sid) == MHD_NO)
304  {
305  cmd_response_data_free (response_data);
306  MHD_destroy_response (response);
307  return MHD_NO;
308  }
309 
311 
313  {
314  MHD_add_response_header (
315  response, MHD_HTTP_HEADER_CONTENT_TYPE,
317  }
318  else
319  {
321  }
322 
323  content_disposition =
325 
326  if (content_disposition != NULL)
327  {
328  MHD_add_response_header (response, "Content-Disposition",
329  content_disposition);
330  }
331 
332  if (cmd_response_data_is_allow_caching (response_data) == FALSE)
333  add_forbid_caching_headers (response);
334  add_security_headers (response);
335  add_cors_headers (response);
336 
337  status_code = cmd_response_data_get_status_code (response_data);
338 
339  cmd_response_data_free (response_data);
340 
341  ret = MHD_queue_response (connection, status_code, response);
342  if (ret == MHD_NO)
343  {
344  /* Assume this was due to a bad request, to keep the MHD "Internal
345  * application error" out of the log. */
347  NULL, GSAD_CONTENT_TYPE_TEXT_HTML, NULL, 0);
348  MHD_destroy_response (response);
349  return MHD_YES;
350  }
351  MHD_destroy_response (response);
352  return ret;
353 }
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:636
int cmd_response_data_get_status_code(cmd_response_data_t *data)
Get http status code of cmd_response_data_t struct.
Definition: gsad_cmd.c:174
const gchar * cmd_response_data_get_content_disposition(cmd_response_data_t *data)
Get content disposition of cmd_response_data_t struct.
Definition: gsad_cmd.c:226
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:861
const gchar * cmd_response_data_get_content_type_string(cmd_response_data_t *data)
Get a content type string if set.
Definition: gsad_cmd.c:255
Special marker for using content type string.
gboolean cmd_response_data_is_allow_caching(cmd_response_data_t *data)
Get allow_caching flag of cmd_response_data_t struct.
Definition: gsad_cmd.c:122
content_type_t cmd_response_data_get_content_type(cmd_response_data_t *data)
Get content type of cmd_response_data_t struct.
Definition: gsad_cmd.c:148
void gsad_add_content_type_header(http_response_t *response, content_type_t *ct)
Adds content-type header fields to a response.
Definition: gsad_http.c:89
enum content_type content_type_t
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
void cmd_response_data_free(cmd_response_data_t *data)
Frees the memory of a cmd_response_data_t struct.
Definition: gsad_cmd.c:81
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:881
#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
content_type
Content types.
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:828

References add_cors_headers(), add_forbid_caching_headers(), add_security_headers(), attach_remove_sid(), BAD_REQUEST_PAGE, cmd_response_data_free(), cmd_response_data_get_content_disposition(), cmd_response_data_get_content_type(), cmd_response_data_get_content_type_string(), cmd_response_data_get_status_code(), cmd_response_data_is_allow_caching(), gsad_add_content_type_header(), GSAD_CONTENT_TYPE_STRING, GSAD_CONTENT_TYPE_TEXT_HTML, MHD_HTTP_NOT_ACCEPTABLE, and send_response().

Referenced by handle_index(), handle_static_config(), handle_static_file(), handler_create_response(), and handler_send_not_found().

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

◆ reconstruct_url()

gchar* reconstruct_url ( http_connection_t *  connection,
const char *  url 
)

Reconstruct the URL for a connection.

Parameters
[in]connectionConnection.
[in]urlBase part of URL.
Returns
URL.

Definition at line 806 of file gsad_http.c.

807 {
808  GString *full_url;
809 
810  full_url = g_string_new (url);
811  /* To simplify appending the token later, ensure there is at least
812  * one param. */
813  g_string_append (full_url, "?r=1&");
814 
815  MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, append_param,
816  full_url);
817 
818  if (full_url->str[strlen (full_url->str) - 1] == '&')
819  full_url->str[strlen (full_url->str) - 1] = '\0';
820 
821  return g_string_free (full_url, FALSE);
822 }
static int append_param(void *string, enum MHD_ValueKind kind, const char *key, const char *value)
Append a request param to a string.
Definition: gsad_http.c:775

References append_param().

Referenced by handle_request().

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

◆ remove_sid()

int remove_sid ( http_response_t *  response)

Attach expired SID cookie to response.

Parameters
[in]responseResponse.
Returns
MHD_NO in case of problems. MHD_YES if all is OK.

Definition at line 512 of file gsad_http.c.

513 {
514  int ret;
515  gchar *value;
516  gchar *locale;
517  char expires[EXPIRES_LENGTH + 1];
518  struct tm expire_time_broken;
519  time_t expire_time;
520 
521  /* Set up the expires param. */
522  locale = g_strdup (setlocale (LC_ALL, NULL));
523  setlocale (LC_ALL, "C");
524 
525  expire_time = time (NULL);
526  if (localtime_r (&expire_time, &expire_time_broken) == NULL)
527  abort ();
528  ret = strftime (expires, EXPIRES_LENGTH, "%a, %d %b %Y %T GMT",
529  &expire_time_broken);
530  if (ret == 0)
531  abort ();
532 
533  setlocale (LC_ALL, locale);
534  g_free (locale);
535 
536  /* Add the cookie.
537  *
538  * Tim Brown's suggested cookie included a domain attribute. How would
539  * we get the domain in here? Maybe a --domain option. */
540 
541  value =
542  g_strdup_printf (SID_COOKIE_NAME "=0; expires=%s; path=/; %sHTTPonly",
543  expires, (is_use_secure_cookie () ? "secure; " : ""));
544  ret = MHD_add_response_header (response, "Set-Cookie", value);
545  g_free (value);
546  return ret;
547 }
gboolean is_use_secure_cookie()
#define EXPIRES_LENGTH
Max length of cookie expires param.
Definition: gsad_http.h:44
#define SID_COOKIE_NAME
Name of the cookie used to store the SID.
Definition: gsad_http.h:58

References EXPIRES_LENGTH, is_use_secure_cookie(), and SID_COOKIE_NAME.

Referenced by attach_remove_sid().

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

◆ send_redirect_to_uri()

int send_redirect_to_uri ( http_connection_t *  connection,
const char *  uri,
const gchar *  sid 
)

Sends a HTTP redirection to an uri.

Parameters
[in]connectionThe connection handle.
[in]uriThe full URI to redirect to.
[in]sidSession ID to add, or NULL.
Returns
MHD_NO in case of a problem. Else MHD_YES.

Definition at line 183 of file gsad_http.c.

185 {
186  int ret;
187  http_response_t *response;
188  char *body;
189 
190  /* Some libmicrohttp versions get into an endless loop in https mode
191  if an empty body is passed. As a workaround and because it is
192  anyway suggested by the HTTP specs, we provide a short body. We
193  assume that uri does not need to be quoted. */
194  body = g_strdup_printf ("<html><body>Code 303 - Redirecting to"
195  " <a href=\"%s\">%s<a/></body></html>\n",
196  uri, uri);
197  response = MHD_create_response_from_buffer (strlen (body), body,
198  MHD_RESPMEM_MUST_FREE);
199 
200  if (!response)
201  {
202  g_warning ("%s: failed to create response, dropping request",
203  __FUNCTION__);
204  return MHD_NO;
205  }
206  ret = MHD_add_response_header (response, MHD_HTTP_HEADER_LOCATION, uri);
207  if (!ret)
208  {
209  MHD_destroy_response (response);
210  g_warning ("%s: failed to add location header, dropping request",
211  __FUNCTION__);
212  return MHD_NO;
213  }
214 
215  if (attach_remove_sid (response, sid) == MHD_NO)
216  {
217  MHD_destroy_response (response);
218  g_warning ("%s: failed to attach SID, dropping request", __FUNCTION__);
219  return MHD_NO;
220  }
221 
222  add_forbid_caching_headers (response);
223  add_security_headers (response);
224  add_cors_headers (response);
225  ret = MHD_queue_response (connection, MHD_HTTP_SEE_OTHER, response);
226  MHD_destroy_response (response);
227  return ret;
228 }
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:636
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:861
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:881
struct MHD_Response http_response_t
Definition: gsad_http.h:137
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:828

References add_cors_headers(), add_forbid_caching_headers(), add_security_headers(), and attach_remove_sid().

Here is the call graph for this function:

◆ 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.

Parameters
[in]connectionThe connection handle.
[in]contentThe content.
[in]status_codeThe HTTP status code.
[in]sidSession ID, or NULL.
[in]content_typeThe content type.
[in]content_dispositionThe content disposition or NULL.
[in]content_lengthContent length, 0 for strlen (content).
Returns
MHD_YES on success, MHD_NO on error.

Definition at line 244 of file gsad_http.c.

247 {
248  http_response_t *response;
249  size_t size;
250  int ret;
251 
252  if (content)
253  size = (content_length ? content_length : strlen (content));
254  else
255  {
256  g_warning ("%s: content is NULL", __FUNCTION__);
257  status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
258  size = 0;
259  }
260  response = MHD_create_response_from_buffer (
261  size, (void *) (content ? content : ""), MHD_RESPMEM_MUST_COPY);
262 
264 
265  if (content_disposition)
266  MHD_add_response_header (response, "Content-Disposition",
267  content_disposition);
268 
269  if (attach_remove_sid (response, sid) == MHD_NO)
270  {
271  return MHD_NO;
272  }
273 
274  add_forbid_caching_headers (response);
275  add_security_headers (response);
276  add_cors_headers (response);
277  ret = MHD_queue_response (connection, status_code, response);
278  MHD_destroy_response (response);
279  return ret;
280 }
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:636
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:861
void gsad_add_content_type_header(http_response_t *response, content_type_t *ct)
Adds content-type header fields to a response.
Definition: gsad_http.c:89
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:881
struct MHD_Response http_response_t
Definition: gsad_http.h:137
content_type
Content types.
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:828

References add_cors_headers(), add_forbid_caching_headers(), add_security_headers(), attach_remove_sid(), and gsad_add_content_type_header().

Referenced by handle_gmp_post(), handle_invalid_method(), handle_logout(), handle_request(), handle_setup_credentials(), handle_validate(), and handler_send_response().

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

◆ 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.

Implements an MHD_PostDataIterator.

Called one or more times to collect the multiple parts (key/value pairs) of a POST request. Fills the params of a gsad_connection_info.

After serve_post, the connection info is free'd.

Parameters
[in,out]coninfo_clsConnection info (a gsad_connection_info).
[in]kindType of request data (header, cookie, etc.).
[in]keyName of data (name of request variable).
[in]filenameName of uploaded file if any, else NULL.
[in]content_typeMIME type of data if known, else NULL.
[in]transfer_encodingTransfer encoding if known, else NULL.
[in]dataData.
[in]offOffset into entire data.
[in]sizeSize of data, in bytes.
Returns
MHD_YES to continue iterating over post data, MHD_NO to stop.

Definition at line 948 of file gsad_http.c.

952 {
953  gsad_connection_info_t *con_info = (gsad_connection_info_t *) coninfo_cls;
954 
955  if (NULL != key)
956  {
957  params_append_mhd (con_info->params, key, filename, data, size, off);
958  return MHD_YES;
959  }
960  return MHD_NO;
961 }
params_t * params
Request parameters.
Definition: gsad_http.h:128
int params_append_mhd(params_t *params, const char *name, const char *filename, const char *chunk_data, int chunk_size, int chunk_offset)
Connection information.
Definition: gsad_http.h:125

References gsad_connection_info::params, and params_append_mhd().

Referenced by handle_request().

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