/**
 * Add an error response to the queue of the server
 *
 * @param connection The client connection which will receive the response
 * 
 * @param http_error_code The http error code to send
 *
 * @return MHD return value, MHD_NO if the response failed to be created, 
 *		   return code of MHD_queue_response otherwise
 */
static int 
send_error( struct MHD_Connection *connection, int http_error_code )
{
	int ret;
	struct MHD_Response *response;

	switch( http_error_code )
	{
		case MHD_HTTP_NOT_FOUND :
			response = MHD_create_response_from_data( strlen("Not Found"), (void *) "Not Found", MHD_NO, MHD_NO );
			break;

		case MHD_HTTP_BAD_REQUEST :
			response = MHD_create_response_from_data( strlen("Bad Request"), (void *) "Bad Request", MHD_NO, MHD_NO );
			break;
		case MHD_HTTP_INTERNAL_SERVER_ERROR :
			response = MHD_create_response_from_data( strlen("Internal Server Error"), (void *) "Internal Server Error", MHD_NO, MHD_NO );
			break;
		default :
			response = MHD_create_response_from_data( strlen("Unknown error"), (void *) "Unknown error", MHD_NO, MHD_NO );
			break;
	} 

	if( !response )
		return MHD_NO;
	ret = MHD_queue_response ( connection, http_error_code, response );
	MHD_destroy_response( response );

	return ret;
}
Пример #2
0
/* ****************************************************************************
*
* restReply - 
*/
void restReply(ConnectionInfo* ciP, std::string answer)
{
  MHD_Response*  response;

  ++replyIx;
  LM_T(LmtServiceOutPayload, ("Response %d: responding with %d bytes, Status Code %d", replyIx, answer.length(), ciP->httpStatusCode));
  LM_T(LmtServiceOutPayload, ("Response payload: '%s'", answer.c_str()));

  if (answer == "")
    response = MHD_create_response_from_data(answer.length(), (void*) answer.c_str(), MHD_NO, MHD_NO);
  else
    response = MHD_create_response_from_data(answer.length(), (void*) answer.c_str(), MHD_YES, MHD_YES);

  if (!response)
    LM_RVE(("MHD_create_response_from_buffer FAILED"));

  if (ciP->httpHeader.size() != 0)
  {
     for (unsigned int hIx = 0; hIx < ciP->httpHeader.size(); ++hIx)
        MHD_add_response_header(response, ciP->httpHeader[hIx].c_str(), ciP->httpHeaderValue[hIx].c_str());
  }

  if (answer != "")
  {
    if (ciP->outFormat == XML)
      MHD_add_response_header(response, "Content-Type", "application/xml");
    else if (ciP->outFormat == JSON)
      MHD_add_response_header(response, "Content-Type", "application/json");
  }

  MHD_queue_response(ciP->connection, ciP->httpStatusCode, response);
  MHD_destroy_response(response);
}
Пример #3
0
static int
ahc_echo (void *cls,
          struct MHD_Connection *connection,
          const char *url,
          const char *method,
          const char *version,
          const char *upload_data, size_t *upload_data_size,
          void **unused)
{
  struct MHD_Response *response;
  char *username;
  const char *password = "******";
  const char *realm = "*****@*****.**";
  int ret;

  username = MHD_digest_auth_get_username(connection);
  if ( (username == NULL) ||
       (0 != strcmp (username, "testuser")) )
    {
      response = MHD_create_response_from_data(strlen (DENIED), 
					       DENIED,
					       MHD_NO, MHD_NO);  
      ret = MHD_queue_auth_fail_response(connection, realm,
					 OPAQUE,
					 response,
					 MHD_NO);    
      MHD_destroy_response(response);  
      return ret;
    }
  ret = MHD_digest_auth_check(connection, realm,
			      username, 
			      password, 
			      300);
  free(username);
  if ( (ret == MHD_INVALID_NONCE) ||
       (ret == MHD_NO) )
    {
      response = MHD_create_response_from_data(strlen (DENIED), 
					       DENIED,
					       MHD_NO, MHD_NO);  
      if (NULL == response) 
	return MHD_NO;
      ret = MHD_queue_auth_fail_response(connection, realm,
					 OPAQUE,
					 response,
					 (ret == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);  
      MHD_destroy_response(response);  
      return ret;
    }
  response = MHD_create_response_from_data(strlen(PAGE), PAGE,
					   MHD_NO, MHD_NO);
  ret = MHD_queue_response(connection, MHD_HTTP_OK, response);  
  MHD_destroy_response(response);
  return ret;
}
Пример #4
0
static int
handle_request(void *cls, struct MHD_Connection *connection,
               const char *url, const char *method,
               const char *version, const char *upload_data,
               size_t *upload_data_size, void **con_cls)
{
    int ret;
    struct MHD_Response *response = NULL;
    struct brubeck_server *brubeck = cls;

    if (!strcmp(method, "GET")) {
        if (!strcmp(url, "/ping")) {
            char *jsonr;
            json_t *pong = json_pack("{s:s, s:i, s:s}",
                                     "version", "brubeck " GIT_SHA,
                                     "pid", (int)getpid(),
                                     "status", "OK");

            jsonr = json_dumps(pong, JSON_PRESERVE_ORDER);
            response = MHD_create_response_from_data(strlen(jsonr), jsonr, 1, 0);
            json_decref(pong);
        }

        else if (!strcmp(url, "/stats"))
            response = send_stats(brubeck);

        else if (starts_with(url, "/metric/"))
            response = send_metric(brubeck, url);
    }
    else if (!strcmp(method, "POST")) {
        if (starts_with(url, "/expire/"))
            response = expire_metric(brubeck, url);
    }

    if (!response) {
        static const char *NOT_FOUND = "404 not found";
        response = MHD_create_response_from_data(
                       strlen(NOT_FOUND), (void *)NOT_FOUND, 0, 0);
        MHD_add_response_header(response, "Connection", "close");
        ret = MHD_queue_response(connection, 404, response);
    } else {
        MHD_add_response_header(response, "Connection", "close");
        MHD_add_response_header(response, "Content-Type", "application/json");
        ret = MHD_queue_response(connection, 200, response);
    }

    MHD_destroy_response(response);
    return ret;
}
Пример #5
0
inline int sendMethodNotAllowedResponse(struct MHD_Connection* connection, bool allowGet) {
    struct MHD_Response* response;
    int ret;

#ifdef MICROHTTPD_DEPRECATED
    response = MHD_create_response_from_data(
        strlen(XML_MWS_METHOD_NOT_ALLOWED), (void*)XML_MWS_METHOD_NOT_ALLOWED, false, false);
#else  // MICROHTTPD_DEPRECATED
    response = MHD_create_response_from_buffer(
        strlen(XML_MWS_METHOD_NOT_ALLOWED), (void*)XML_MWS_METHOD_NOT_ALLOWED, MHD_RESPMEM_PERSISTENT);
#endif  // MICROHTTPD_DEPRECATED
    MHD_add_response_header(response, "Content-Type", "text/xml");
    MHD_add_response_header(response, "Allow",
                            allowGet ? "GET, POST, OPTIONS" : "POST, OPTIONS");
    MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
    MHD_add_response_header(response, "Allow",
                            allowGet ? "GET, POST, OPTIONS" : "POST, OPTIONS");
    MHD_add_response_header(response, "Access-Control-Allow-Headers",
                            "CONTENT-TYPE");
    MHD_add_response_header(response, "Access-Control-Max-Age", "1728000");
    ret = MHD_queue_response(connection, MHD_HTTP_METHOD_NOT_ALLOWED, response);
    MHD_destroy_response(response);

    return ret;
}
Пример #6
0
static int see_other(MHD_Connection* connection, const std::string& new_location) {
  MHD_Response* response = MHD_create_response_from_data(0, 0, MHD_NO, MHD_NO);
  MHD_add_response_header(response, MHD_HTTP_HEADER_LOCATION, new_location.c_str());
  MHD_queue_response(connection, MHD_HTTP_SEE_OTHER, response);
  MHD_destroy_response(response);
  return MHD_YES;
}
Пример #7
0
static int
connection_handler (void *cls,
                    struct MHD_Connection *connection,
                    const char *url,
                    const char *method,
                    const char *version,
                    const char *upload_data, size_t * upload_data_size,
                    void **ptr)
{
  static int i;

  if (*ptr == NULL)
    {
      *ptr = &i;
      return MHD_YES;
    }

  if (*upload_data_size != 0)
    {
      (*upload_data_size) = 0;
      return MHD_YES;
    }

  struct MHD_Response *response =
    MHD_create_response_from_data (strlen ("Response"), "Response", 0, 0);
  int ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);

  return ret;
}
Пример #8
0
int
send_response(struct http_response *resp)
{
	struct MHD_Response *response;
	struct http_header *hdr;
	char *origin;
	int ret;

	response = MHD_create_response_from_data(resp->ndata,
	    (void *)resp->data, MHD_NO, MHD_YES);
	assert(response);
	for (hdr = resp->headers; hdr; hdr = hdr->next)
		MHD_add_response_header(response, hdr->key, hdr->value);
	MHD_add_response_header(response, "Access-Control-Allow-Headers",
	    "Authorization, Origin");
	MHD_add_response_header(response, "Access-Control-Allow-Methods",
	    "GET, POST, PUT, DELETE, OPTIONS");
	origin = http_get_header(resp->connection, "Origin");
	MHD_add_response_header(response, "Access-Control-Allow-Origin",
	    origin ? origin : "*");
	free(origin);
	ret = MHD_queue_response(resp->connection, resp->status, response);
	MHD_destroy_response(response);
	return (ret);
}
Пример #9
0
/**
 *  HTTP access handler call back
 */
int
http_ahc (void *cls, struct MHD_Connection *connection,
          const char *url, const char *method, const char *upload_data,
          const char *version, size_t *upload_data_size, void **ptr)
{
  static int aptr;
  struct MHD_Response *response;
  int ret;

  if (0 != strcmp (method, MHD_HTTP_METHOD_GET))
    return MHD_NO;              /* unexpected method */
  if (&aptr != *ptr)
    {
      /* do never respond on first call */
      *ptr = &aptr;
      return MHD_YES;
    }
  *ptr = NULL;                  /* reset when done */
  response = MHD_create_response_from_data (strlen (test_data),
					    (void *) test_data,
					    MHD_NO, MHD_NO);
  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);
  return ret;
}
Пример #10
0
inline int
sendOptionsResponse(struct MHD_Connection* connection)
{
    struct MHD_Response* response;
    int                  ret;

#ifdef MICROHTTPD_DEPRECATED
    response = MHD_create_response_from_data(strlen(EMPTY_RESPONSE),
                                             (void*) EMPTY_RESPONSE,
                                             false,
                                             false);
#else // MICROHTTPD_DEPRECATED
    response = MHD_create_response_from_buffer(strlen(EMPTY_RESPONSE),
                                               (void*) EMPTY_RESPONSE,
                                               MHD_RESPMEM_PERSISTENT);
#endif // MICROHTTPD_DEPRECATED
    MHD_add_response_header(response,
                            "Content-Type", "text/plain");
    MHD_add_response_header(response,
                            "Access-Control-Allow-Origin", "*");
    MHD_add_response_header(response,
                            "Access-Control-Allow-Methods", "POST, OPTIONS");
    MHD_add_response_header(response,
                            "Access-Control-Allow-Headers", "CONTENT-TYPE");
    MHD_add_response_header(response,
                            "Access-Control-Max-Age", "1728000");
    ret = MHD_queue_response(connection,
                             MHD_HTTP_OK,
                             response);
    MHD_destroy_response(response);

    return ret;
}
Пример #11
0
inline int
sendXmlGenericResponse(struct MHD_Connection* connection,
                       const char*            xmlGenericResponse,
                       int                    statusCode)
{
    struct MHD_Response* response;
    int                  ret;

#ifdef MICROHTTPD_DEPRECATED
    response = MHD_create_response_from_data(strlen(xmlGenericResponse),
                                             (void*) xmlGenericResponse,
                                             /* must_free = */ 0,
                                             /* must_copy = */ 0);
#else // MICROHTTPD_DEPRECATED
    response = MHD_create_response_from_buffer(strlen(xmlGenericResponse),
                                               (void*) xmlGenericResponse,
                                               MHD_RESPMEM_PERSISTENT);
#endif // MICROHTTPD_DEPRECATED
    MHD_add_response_header(response,
                            "Content-Type", "text/xml");
    ret = MHD_queue_response(connection,
                             statusCode,
                             response);
    MHD_destroy_response(response);

    return ret;
}
Пример #12
0
static struct MHD_Response *
send_metric(struct brubeck_server *server, const char *url)
{
    static const char *metric_types[] = {
        "gauge", "meter", "counter", "histogram", "timer", "internal"
    };
    static const char *expire_status[] = {
        "disabled", "inactive", "active"
    };

    struct brubeck_metric *metric = safe_lookup_metric(
                                        server, url + strlen("/metric/"));

    if (metric) {
        json_t *mj = json_pack("{s:s, s:s, s:i, s:s}",
                               "key", metric->key,
                               "type", metric_types[metric->type],
#if METRIC_SHARD_SPECIFIER
                               "shard", (int)metric->shard,
#else
                               "shard", 0,
#endif
                               "expire", expire_status[metric->expire]
                              );

        char *jsonr = json_dumps(mj, JSON_INDENT(4) | JSON_PRESERVE_ORDER);
        json_decref(mj);
        return MHD_create_response_from_data(
                   strlen(jsonr), jsonr, 1, 0);
    }

    return NULL;
}
Пример #13
0
int CWebServer::AskForAuthentication(struct MHD_Connection *connection)
{
  struct MHD_Response *response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
  if (!response)
  {
    CLog::Log(LOGERROR, "CWebServer: unable to create HTTP Unauthorized response");
    return MHD_NO;
  }

  int ret = AddHeader(response, MHD_HTTP_HEADER_WWW_AUTHENTICATE, "Basic realm=XBMC");
  ret |= AddHeader(response, MHD_HTTP_HEADER_CONNECTION, "close");
  if (!ret)
  {
    CLog::Log(LOGERROR, "CWebServer: unable to prepare HTTP Unauthorized response");
    MHD_destroy_response(response);
    return MHD_NO;
  }

#ifdef WEBSERVER_DEBUG
  std::multimap<std::string, std::string> headerValues;
  GetRequestHeaderValues(connection, MHD_RESPONSE_HEADER_KIND, headerValues);

  CLog::Log(LOGDEBUG, "webserver [OUT] HTTP %d", MHD_HTTP_UNAUTHORIZED);

  for (std::multimap<std::string, std::string>::const_iterator header = headerValues.begin(); header != headerValues.end(); ++header)
    CLog::Log(LOGDEBUG, "webserver [OUT] %s: %s", header->first.c_str(), header->second.c_str());
#endif

  ret = MHD_queue_response(connection, MHD_HTTP_UNAUTHORIZED, response);
  MHD_destroy_response(response);

  return ret;
}
Пример #14
0
int list_all_nodes(struct MHD_Connection *connection, struct system_data *sysdata, int uri_num)
{
  struct MHD_Response *response;
  int i, ret;

  DEBUG("\n");
  strcpy (pagebuff, "{\n");
  strcat (pagebuff, "  \"node\" : [\n");

  for (i=0; i < NUM_NODES; i++) {
    ret = print_node_json(pagebuff, &sysdata->nodes[i]);
    sprintf(&pagebuff[strlen(pagebuff)], "%c\n", i==NUM_NODES-1?' ':',');
    if (ret)
      return present_error(connection, "Error generating node #: %d\n", i+1);
  }
  strcat (pagebuff, "  ]\n}\n");
    
  response = MHD_create_response_from_data (strlen(pagebuff), (void *) pagebuff,
                                            MHD_NO, MHD_NO);

  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);

  return ret;
}
Пример #15
0
int HttpServer::HandleRequest(void *cls,
                              struct MHD_Connection* connection,
                              const char* url,
                              const char* method,
                              const char* version,
                              const char* upload_data,
                              size_t* upload_data_size,
                              void** ptr) {
  Dispatcher& dispatch = *static_cast<Dispatcher*>(cls);
  static int dummy;
  struct MHD_Response * response;
  int ret;

  if (&dummy != *ptr) {
    /* The first time only the headers are valid,
       do not respond in the first round... */
    *ptr = &dummy;
    return MHD_YES;
  }
  *ptr = NULL; /* clear context pointer */

  std::string post_body;
  if (*upload_data_size > 0) {
    post_body = std::string(upload_data, *upload_data_size);
  }
  std::string page = dispatch(method, url, post_body);
  response = MHD_create_response_from_data(page.length(),
                                           (void*) page.c_str(),
                                           MHD_NO,
                                           MHD_YES);
  ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
  MHD_destroy_response(response);
  return ret;
}
Пример #16
0
int CWebServer::CreateErrorResponse(struct MHD_Connection *connection, int responseType, HTTPMethod method, struct MHD_Response *&response)
{
  size_t payloadSize = 0;
  void *payload = NULL;

  if (method != HEAD)
  {
    switch (responseType)
    {
      case MHD_HTTP_NOT_FOUND:
        payloadSize = strlen(PAGE_FILE_NOT_FOUND);
        payload = (void *)PAGE_FILE_NOT_FOUND;
        break;

      case MHD_HTTP_NOT_IMPLEMENTED:
        payloadSize = strlen(NOT_SUPPORTED);
        payload = (void *)NOT_SUPPORTED;
        break;
    }
  }

  response = MHD_create_response_from_data(payloadSize, payload, MHD_NO, MHD_NO);
  if (response == NULL)
  {
    CLog::Log(LOGERROR, "CWebServer: failed to create a HTTP %d error response", responseType);
    return MHD_NO;
  }

  return MHD_YES;
}
Пример #17
0
// wraps the given string in a html page and sends it as response with the given status code
static void sendMessage(MHD_Connection *connection, unsigned int status, std::string message)
{
    std::string page = "<html><body><p>"+message+"</p></body></html>";
    struct MHD_Response* resp = MHD_create_response_from_data(page.size(), (void*)page.data(), 0, 1);
    MHD_add_response_header(resp, "Content-Type", "text/html");
    secure_queue_response(connection, status, resp);
    MHD_destroy_response(resp);
}
Пример #18
0
/**
 * Create a response object.  The response object can be extended with
 * header information and then be used any number of times.
 *
 * @param size size of the data portion of the response
 * @param buffer size bytes containing the response's data portion
 * @param mode flags for buffer management
 * @return NULL on error (i.e. invalid arguments, out of memory)
 * @ingroup response
 */
struct MHD_Response *
MHD_create_response_from_buffer (size_t size,
				 void *buffer,
				 enum MHD_ResponseMemoryMode mode)
{
  return MHD_create_response_from_data (size,
					buffer,
					mode == MHD_RESPMEM_MUST_FREE,
					mode == MHD_RESPMEM_MUST_COPY);
}
Пример #19
0
/*
 * web_send_file
 * Read files and send them out
 */
int web_send_file (struct MHD_Connection *conn, const char *filename, const int code, const bool unl)
{
	struct stat buf;
	FILE *fp;
	struct MHD_Response *response;
	const char *p;
	const char *ct = NULL;
	int ret;

	if ((p = strchr(filename, '.')) != NULL) {
		p++;
		if (strcmp(p, "xml") == 0)
			ct = "text/xml";
		else if (strcmp(p, "js") == 0)
			ct = "text/javascript";
	}
	if (stat(filename, &buf) == -1 ||
			((fp = fopen(filename, "r")) == NULL)) {
		if (strcmp(p, "xml") == 0)
			response = MHD_create_response_from_data(0, (void *)"", MHD_NO, MHD_NO);
		else {
			int len = strlen(FNF) + strlen(filename) - 1; // len(%s) + 1 for \0
			char *s = (char *)malloc(len);
			if (s == NULL) {
				fprintf(stderr, "Out of memory FNF\n");
				exit(1);
			}
			snprintf(s, len, FNF, filename);
			response = MHD_create_response_from_data(len, (void *)s, MHD_YES, MHD_NO); // free
		}
	} else
		response = MHD_create_response_from_callback(buf.st_size, 32 * 1024, &web_read_file, fp,
				&web_close_file);
	if (response == NULL)
		return MHD_YES;
	if (ct != NULL)
		MHD_add_response_header(response, "Content-type", ct);
	ret = MHD_queue_response(conn, code, response);
	MHD_destroy_response(response);
	if (unl)
		unlink(filename);
	return ret;
}
Пример #20
0
int
send_response(struct MHD_Connection* conn, const char* body, int status_code)
{
    struct MHD_Response* response;
    if (body) {
        response = MHD_create_response_from_data(strlen(body), (void*)body, MHD_NO, MHD_YES);
    } else {
        response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_YES);
    }

    if (!response) {
        return MHD_NO;
    }

    int ret = MHD_queue_response(conn, status_code, response);
    MHD_destroy_response(response);

    return ret;
}
Пример #21
0
int answer_to_connection (void *cls, struct MHD_Connection *connection, const char *url,
			  const char *method, const char *version, const char *upload_data,
			  size_t *upload_data_size, void **con_cls)
{
  TDB_CONTEXT *db = (TDB_CONTEXT*) cls;
  const char *page;
  struct MHD_Response *response;
  int ret;

  // We assume that nginx won't pass us / itself.
  if (strcmp(url, "/")==0)
    {
      printf("\nSetup nginx properly!");
      return MHD_NO;
    }

  TDB_DATA key, data;
  key.dptr = strdup(url);
  key.dsize = strlen(url);

  response = MHD_create_response_from_data (strlen(page),
					    (void*) page, MHD_YES, MHD_NO);

  if (strcmp(method, "GET")==0)
    {
      TDB_DATA data;
      data = tdb_fetch (db, key);
      if (data.dptr)
	{
	  MHD_add_response_header (response, "Location", data.dptr);
	  ret = MHD_queue_response (connection, MHD_HTTP_MOVED_PERMANENTLY, response);
	}
      else
	{
	  ret = MHD_queue_response (connection, 404, response);
	}
    }
  else if (strcmp(method, "POST")==0)
    {
      printf ("%s -> %s", url, upload_data);

      data.dptr = strdup(upload_data);
      data.dsize = strlen(upload_data);
      tdb_store(db, key, data, TDB_REPLACE);
      ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    }

  free(data.dptr);
  free(key.dptr);
    
  MHD_destroy_response(response);

  return ret;
}
Пример #22
0
int CWebServer::CreateMemoryDownloadResponse(struct MHD_Connection *connection, const void *data, size_t size, bool free, bool copy, struct MHD_Response *&response)
{
  response = MHD_create_response_from_data(size, const_cast<void*>(data), free ? MHD_YES : MHD_NO, copy ? MHD_YES : MHD_NO);
  if (response == NULL)
  {
    CLog::Log(LOGERROR, "CWebServer: failed to create a HTTP download response");
    return MHD_NO;
  }

  return MHD_YES;
}
Пример #23
0
int CWebServer::CreateRedirect(struct MHD_Connection *connection, const std::string &strURL, struct MHD_Response *&response)
{
  response = MHD_create_response_from_data(0, NULL, MHD_NO, MHD_NO);
  if (response == NULL)
  {
    CLog::Log(LOGERROR, "CWebServer: failed to create HTTP redirect response to %s", strURL.c_str());
    return MHD_NO;
  }

  AddHeader(response, MHD_HTTP_HEADER_LOCATION, strURL);
  return MHD_YES;
}
Пример #24
0
int send_reponse (struct MHD_Connection *connection, const char *page, int status_code)
{
    MDW_LOG_DEBUG("send_reponse");	
	
	struct MHD_Response *response = MHD_create_response_from_data(strlen (page), (void *) page, 0 /*must_free*/, 0 /*must_copy*/);   			     
	if (!response)
		return MHD_NO;

	int ret = MHD_queue_response (connection, status_code, response);
	MHD_destroy_response (response);

	return ret;
}
Пример #25
0
static struct MHD_Response *
expire_metric(struct brubeck_server *server, const char *url)
{
    struct brubeck_metric *metric = safe_lookup_metric(
                                        server, url + strlen("/expire/"));

    if (metric) {
        metric->expire = BRUBECK_EXPIRE_DISABLED;
        return MHD_create_response_from_data(
                   0, "", 0, 0);
    }
    return NULL;
}
Пример #26
0
// add security related headers and send the response on the given connection
// the reference counter is not touched
// this function is a wrapper around MHD_queue_response
// MHD_queue_response should be replaced with this function
static void secure_queue_response(MHD_Connection *connection, unsigned int status_code, struct MHD_Response* response)
{
    // TODO: protect againts handling untrusted content to the browser
    // see:
    // http://www.dotnetnoob.com/2012/09/security-through-http-response-headers.html
    // http://www.w3.org/TR/CSP2/
    // https://code.google.com/p/doctype-mirror/wiki/ArticleContentSniffing

    // check content type
    // don't server when no type or no whitelisted type is given
    // TODO sending invalid mime types is as bad as not sending them TODO
    /*
    std::vector<std::string> allowed_types;
    allowed_types.push_back("text/html");
    allowed_types.push_back("application/json");
    allowed_types.push_back("image/png");
    */
    const char* type = MHD_get_response_header(response, "Content-Type");
    if(type == 0 /*|| std::find(allowed_types.begin(), allowed_types.end(), std::string(type)) == allowed_types.end()*/)
    {
        std::string page;
        if(type == 0)
            page = "<html><body><p>Fatal Error: no content type was set on this response. This is a bug.</p></body></html>";
        else
            page = "<html><body><p>Fatal Error: this content type is not allowed. This is a bug.<br/> Content-Type: "+std::string(type)+"</p></body></html>";
        struct MHD_Response* resp = MHD_create_response_from_data(page.size(), (void*)page.data(), 0, 1);
        MHD_add_response_header(resp, "Content-Type", "text/html");
        MHD_queue_response(connection, MHD_HTTP_INTERNAL_SERVER_ERROR, resp);
        MHD_destroy_response(resp);
    }

    // tell Internet Explorer to not do content sniffing
    MHD_add_response_header(response, "X-Content-Type-Options", "nosniff");

    // Content security policy header, its a new technology and not implemented everywhere

    // get own host name as the browser sees it
    std::string host;
    MHD_get_connection_values(connection, MHD_HEADER_KIND, _extract_host_header_it_cb, (void*)&host);

    std::string csp;
    csp += "default-src 'none';";
    csp += "script-src '"+host+STATIC_FILES_ENTRY_PATH+"';";
    csp += "font-src '"+host+STATIC_FILES_ENTRY_PATH+"';";
    csp += "img-src 'self';"; // allow images from all paths on this server
    csp += "media-src 'self';"; // allow media files from all paths on this server

    MHD_add_response_header(response, "X-Content-Security-Policy", csp.c_str());

    MHD_queue_response(connection, status_code, response);
}
Пример #27
0
int test_http_server_timeout_handler(void * cls,
		    struct MHD_Connection * connection,
		    const char * url,
		    const char * method,
            const char * version,
		    const char * upload_data,
		    size_t * upload_data_size, 
    		void ** ptr) {

	test_http_server * handle = cls;
	struct MHD_Response * response;
	int ret;
	int * c_count;
	if (*ptr == NULL) {
		handle->log = apr_hash_make(handle->pool);
	  	/* The first time only the headers are valid,
		 do not respond in the first round... */
	  	*ptr = cls; //just to flag its not null....
	  	int * c_count = apr_palloc(handle->pool, sizeof(int));
		*c_count = 0;
		*ptr = c_count;
	  	return MHD_YES;
	}
	c_count = *ptr;
	if(*c_count == 0) {
		//log the request....
		apr_hash_set(handle->log,  apr_pstrdup(handle->pool, "METHOD"), APR_HASH_KEY_STRING, apr_pstrdup(handle->pool, method));
		apr_hash_set(handle->log,  apr_pstrdup(handle->pool, "URL"), APR_HASH_KEY_STRING, apr_pstrdup(handle->pool, url));
		apr_hash_set(handle->log,  apr_pstrdup(handle->pool, "VERSION"), APR_HASH_KEY_STRING, apr_pstrdup(handle->pool, version));
		apr_hash_set(handle->log,  apr_pstrdup(handle->pool, "DATA_LENGTH"), APR_HASH_KEY_STRING, apr_pmemdup (handle->pool, upload_data_size, sizeof(size_t)));
		apr_hash_set(handle->log,  apr_pstrdup(handle->pool, "DATA"), APR_HASH_KEY_STRING, apr_pmemdup (handle->pool, upload_data, *upload_data_size));
	}
	*c_count = *c_count+1;
	if(*upload_data_size == 0) {
		//cause a timeout...
		apr_sleep(handle->sleep_duration);
		response = MHD_create_response_from_data(strlen(handle->response),
						   (void*) handle->response,
						   MHD_NO,
						   MHD_NO);
		ret = MHD_queue_response(connection,
				   handle->response_code,
				   response);
		MHD_destroy_response(response);
		return ret;
	} else {
		*upload_data_size = 0; //we processed everything?
		return MHD_YES;
	}

}
Пример #28
0
int scan_therms(struct MHD_Connection *connection, struct system_data *sysdata, int uri_num)
{
  const char *page = "<html><body>Scan therms</body></html>";
  struct MHD_Response *response;
  int ret;

  DEBUG("\n");
  response = MHD_create_response_from_data (strlen(page), (void *) page,
                                            MHD_NO, MHD_NO);

  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);

  return ret;
}
Пример #29
0
int unsupported_handler(struct MHD_Connection *connection)
{
  const char *page = "<html><body>Unsupported request</body></html>";
  struct MHD_Response *response;
  int ret;

  DEBUG("\n");
  response = MHD_create_response_from_data (strlen(page), (void *) page,
                                            MHD_NO, MHD_NO);

  ret = MHD_queue_response (connection, MHD_HTTP_NOT_FOUND, response);
  MHD_destroy_response (response);

  return ret;
}
Пример #30
0
/*
 * web_send_data
 * Send internal HTML string
 */
static int web_send_data (struct MHD_Connection *connection, const char *data,
		const int code, bool free, bool copy, const char *ct)
{
	struct MHD_Response *response;
	int ret;

	response = MHD_create_response_from_data(strlen(data), (void *)data, free ? MHD_YES : MHD_NO, copy ? MHD_YES : MHD_NO);
	if (response == NULL)
		return MHD_NO;
	if (ct != NULL)
		MHD_add_response_header(response, "Content-type", ct);
	ret = MHD_queue_response(connection, code, response);
	MHD_destroy_response(response);
	return ret;
}