Example #1
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)
{
  static int eok;
  struct MHD_Response *response;
  struct MHD_PostProcessor *pp;
  int ret;

  if (0 != strcmp ("POST", method))
    {
      return MHD_NO;            /* unexpected method */
    }
  pp = *unused;
  if (pp == NULL)
    {
      eok = 0;
      pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok);
      if (pp == NULL)
        return MHD_NO;
      *unused = pp;
    }
  MHD_post_process (pp, upload_data, *upload_data_size);
  if ((eok == 3) && (0 == *upload_data_size))
    {
      response = MHD_create_response_from_buffer (strlen (url),
						  (void *) url,
						  MHD_RESPMEM_MUST_COPY);
      ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
      MHD_destroy_response (response);
      MHD_destroy_post_processor (pp);
      *unused = NULL;
      return ret;
    }
  *upload_data_size = 0;
  return MHD_YES;
}
Example #2
0
/**
 * process http get request
 * @param url        [request url string]
 * @param connection [http connect handle]
 */
inline void process_get_url_requert(const char *url,
                                    struct MHD_Connection * connection)
{
	struct MHD_Response *response;
	int fd, size, type;

	if (url_to_file(url, &fd, &size, &type))
	{
		response =
		    MHD_create_response_from_fd(size, fd);
		set_mime(response, type);
		MHD_queue_response(connection, MHD_HTTP_OK, response);
		MHD_destroy_response(response);
		return;
	}
	else if (url_to_api(url, connection, false, NULL, 0, NULL))
		return;
	else
		return_404(connection);
}
static int
callback(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)
{
  struct callback_closure *cbc = calloc(1, sizeof(struct callback_closure));
  struct MHD_Response *r;

  r = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024,
					 &called_twice, cbc,
					 &free);
  MHD_queue_response(connection, MHD_HTTP_OK, r);
  MHD_destroy_response(r);
  return MHD_YES;
}
Example #4
0
int SendTemplate(MHD_Connection* connection, bool redirect, const char* redirectUrl) {
    SkString debuggerTemplate = generate_template(SkString(FLAGS_source[0]));

    MHD_Response* response = MHD_create_response_from_buffer(
        debuggerTemplate.size(),
        (void*) const_cast<char*>(debuggerTemplate.c_str()),
        MHD_RESPMEM_MUST_COPY);
    MHD_add_response_header (response, "Access-Control-Allow-Origin", "*");

    int status = MHD_HTTP_OK;

    if (redirect) {
        MHD_add_response_header (response, "Location", redirectUrl);
        status = MHD_HTTP_SEE_OTHER;
    }

    int ret = MHD_queue_response(connection, status, response);
    MHD_destroy_response(response);
    return ret;
}
static int
serveMasterNameXML (const void *cls,
	      		  const char *mime,
	      		  struct Session *session,
	      		  struct MHD_Connection *connection)
{
  int ret;
  //char *reply;
  struct MHD_Response *response;
   
  const char *pCmd = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "Cmd");
  printf("Cmd = %s\n", pCmd);
  
  // The XML is filled up by the CommandDispatcher using a static string object.
  // Since this is a .c file, we cannot use class objects here, so we basically
  // extract a char point from it.
  const char *pString;   
  // Dispatch the command
  CommandDispatcher(&pString, pCmd);
  //strcpy(pString, MASTER_NAME);
  //sprintf(pString, reply, String);
  //free(reply);
  
  fprintf(stdout, "%s\n", pString);
  
  /* return static form */
  response = MHD_create_response_from_buffer (strlen (pString),
					      (void *) pString,
					      MHD_RESPMEM_PERSISTENT);				  
  if (NULL == response)
    return MHD_NO;
  add_session_cookie (session, response);
  MHD_add_response_header (response,
			   MHD_HTTP_HEADER_CONTENT_ENCODING,
			   mime);
  ret = MHD_queue_response (connection,
			    MHD_HTTP_OK,
			    response);
  MHD_destroy_response (response);
  return ret;
}
Example #6
0
static 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)
{
    char buf[5],page[REPLY_PAGE_SIZE], *dyn_format_str = NULL;
	char format_str[50] = "$c:$i:$o:$p0";
    struct MHD_Response *response;
    struct dnetc_values *shmem;
    int i,ret;

	if(strcmp(method,"GET") != 0)
		return MHD_NO;
	
    shmem = (struct dnetc_values *) cls;
	if(!shmem->running)
	{
		snprintf(page,REPLY_PAGE_SIZE,"NONE");
	}
	else
	{
		MHD_get_connection_values (connection, MHD_GET_ARGUMENT_KIND, parse_http_get_param, &dyn_format_str);
		if(dyn_format_str == NULL)
		{
			for(i=1;i<shmem->n_cpu;i++)
			{
				snprintf(buf,5,":$p%d",i);
				strcat(format_str, buf);
			}
			sprint_formated_data(page,REPLY_PAGE_SIZE,format_str,shmem);
		}
		else
		{
			sprint_formated_data(page,REPLY_PAGE_SIZE,dyn_format_str,shmem);
			free(dyn_format_str);
		}
	}

    response = MHD_create_response_from_data (strlen(page), (void *) page, MHD_NO, MHD_YES);
    ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    MHD_destroy_response (response);

    return ret;
}
Example #7
0
static 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)
{
	std::string s;

	if(!strcmp(method,"POST")) {
		if (*con_cls == NULL) {
			*con_cls = new std::string();
			return MHD_YES;
		}
		if (*upload_data_size) {
			std::string ss(upload_data, *upload_data_size);
			(*((std::string*)*con_cls)) += ss;
			*upload_data_size = 0;
			return MHD_YES;
		}
		else {
			Workbench::cur()->prevent_update();
			s = ((HTTPServer*)cls)->answer(url, (*((std::string*)*con_cls)));
			Workbench::cur()->allow_update();
			delete (std::string*)*con_cls;
			*con_cls = 0;
		}
	}
	else s = ((HTTPServer*)cls)->answer(url, "");

	struct MHD_Response *response;
	int ret;
	char* ss = new char[s.length()+1];
	strcpy(ss, s.c_str()); ss[s.length()] = 0;
	response =  MHD_create_response_from_buffer (strlen(ss), ss, MHD_RESPMEM_MUST_FREE);
	MHD_add_response_header(response, "Access-Control-Allow-Origin", "*");
	MHD_add_response_header(response, "Access-Control-Allow-Methods", "POST, GET, OPTIONS");
	MHD_add_response_header(response, "Access-Control-Allow-Headers", "X-Requested-With");
	ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
	MHD_destroy_response (response);

	return ret;
}
Example #8
0
static 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
)

{   int r;
    tString u=newString(url);
    u.lower();
    if(compare(u,pagepath)==0) {
        tString s=concatenateStrings(page1,toString(stack,false),page2);
        char* p=s.mbs();
        MHD_Response * response = MHD_create_response_from_buffer (strlen(p),(void*)p,MHD_RESPMEM_MUST_COPY);
        delete[] p;
        r=MHD_queue_response (connection, MHD_HTTP_OK, response);
        MHD_destroy_response (response);
    }
    return r;
}
Example #9
0
int send_redirect_temp(struct MHD_Connection *connection, const char *url)
{
	struct MHD_Response *response;
	int ret;
	char *redirect;

	const char *redirect_body = "<html><head></head><body><a href='%s'>Click here to continue to<br>%s</a></body></html>";
	safe_asprintf(&redirect, redirect_body, url, url);

	response = MHD_create_response_from_buffer(strlen(redirect), redirect, MHD_RESPMEM_MUST_FREE);
	if (!response)
		return send_error(connection, 503);

	//	MHD_set_response_options(response, MHD_RF_HTTP_VERSION_1_0_ONLY, MHD_RO_END);
	MHD_add_response_header(response, "Location", url);
	MHD_add_response_header(response, "Connection", "close");
	ret = MHD_queue_response(connection, MHD_HTTP_TEMPORARY_REDIRECT, response);
	MHD_destroy_response(response);

	return ret;
}
Example #10
0
static int
ahc_echo (void *cls,
          struct MHD_Connection *connection,
          const char *url,
          const char *method,
          const char *version,
          const char *upload_data, unsigned int *upload_data_size,
          void **unused)
{
  const char *me = cls;
  struct MHD_Response *response;
  int ret;

  if (0 != strcmp (me, method))
    return MHD_NO;              /* unexpected method */
  response = MHD_create_response_from_data (strlen (url),
                                            (void *) url, MHD_NO, MHD_YES);
  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);
  return ret;
}
Example #11
0
static int mhd_respond_internal(struct MHD_Connection *connection,
                                enum MHD_RequestTerminationCode code,
                                char *buffer,
                                size_t size,
                                enum MHD_ResponseMemoryMode mode) {
        struct MHD_Response *response;
        int r;

        assert(connection);

        response = MHD_create_response_from_buffer(size, buffer, mode);
        if (!response)
                return MHD_NO;

        log_debug("Queing response %u: %s", code, buffer);
        MHD_add_response_header(response, "Content-Type", "text/plain");
        r = MHD_queue_response(connection, code, response);
        MHD_destroy_response(response);

        return r;
}
Example #12
0
int webu_stream_static(struct webui_ctx *webui) {
    /* Create the response for the static image request*/
    int retcd;
    struct MHD_Response *response;
    char resp_used[20];

    if (webu_stream_checks(webui) == -1) return MHD_NO;

    webu_stream_cnct_count(webui);

    webu_stream_mjpeg_checkbuffers(webui);

    webu_stream_static_getimg(webui);

    if (webui->resp_used == 0) {
        MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, _("Could not get image to stream."));
        return MHD_NO;
    }

    response = MHD_create_response_from_buffer (webui->resp_size
        ,(void *)webui->resp_page, MHD_RESPMEM_MUST_COPY);
    if (!response){
        MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, _("Invalid response"));
        return MHD_NO;
    }

    if (webui->cnt->conf.stream_cors_header != NULL){
        MHD_add_response_header (response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN
            , webui->cnt->conf.stream_cors_header);
    }

    MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "image/jpeg;");
    snprintf(resp_used, 20, "%9ld\r\n\r\n",(long)webui->resp_used);
    MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_LENGTH, resp_used);

    retcd = MHD_queue_response (webui->connection, MHD_HTTP_OK, response);
    MHD_destroy_response (response);

    return retcd;
}
Example #13
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 **ptr)
{
  static int aptr;
  const char *fmt = cls;
  const char *val;
  char *me;
  struct MHD_Response *response;
  int ret;

  if (0 != strcmp (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 */
  val = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "q");
  me = malloc (snprintf (NULL, 0, fmt, "q", val) + 1);
  if (me == NULL)
    return MHD_NO;
  sprintf (me, fmt, "q", val);
  response = MHD_create_response_from_buffer (strlen (me), me,
					      MHD_RESPMEM_MUST_FREE);
  if (response == NULL)
    {
      free (me);
      return MHD_NO;
    }
  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);
  return ret;
}
Example #14
0
int answer (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) {

    const char *page  = "<html><body>Hello, browser!</body></html>";
    struct MHD_Response *response;
    int ret;

    /* Look up query parameters, filling in defaults. */
    const char *lat_s = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "lat");
    double lat = (lat_s == NULL) ? 0.0 : atof(lat_s);
    const char *lon_s = MHD_lookup_connection_value (connection, MHD_GET_ARGUMENT_KIND, "lon");
    double lon = (lon_s == NULL) ? 0.0 : atof(lon_s);
    printf ("lat=%f lon=%f\n", lat, lon);
       
    response = MHD_create_response_from_buffer (strlen (page), (void*) page, MHD_RESPMEM_PERSISTENT);
    ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
    MHD_destroy_response (response);
    return ret;
}
Example #15
0
int list_a_therm(struct MHD_Connection *connection, struct system_data *sysdata, int uri_num)
{
  struct MHD_Response *response;
  int ret;

  DEBUG("\n");
  if ((uri_num >=1) && (uri_num <= NUM_NODES)) {
    strcpy (pagebuff, "{\n");
    strcat (pagebuff, "  \"therm\" :\n");
    ret = print_therm_json(pagebuff, &(sysdata->nodes[uri_num-1].temp));
    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;
  }
  return present_error(connection, "Invalid node #: %d\n", uri_num);
}
Example #16
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)
{
  int *done = cls;
  struct MHD_Response *response;
  int ret;
  (void)version;(void)unused;   /* Unused. Silent compiler warning. */

  if (0 != strcmp ("PUT", method))
    return MHD_NO;              /* unexpected method */
  if ((*done) == 0)
    {
      if (*upload_data_size != 8)
        return MHD_YES;         /* not yet ready */
      if (0 == memcmp (upload_data, "Hello123", 8))
        {
          *upload_data_size = 0;
        }
      else
        {
          printf ("Invalid upload data `%8s'!\n", upload_data);
          return MHD_NO;
        }
      *done = 1;
      return MHD_YES;
    }
  response = MHD_create_response_from_buffer (strlen (url),
					      (void *) url,
					      MHD_RESPMEM_MUST_COPY);
  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);
  return ret;
}
Example #17
0
/**
 * Handler used to generate a 404 reply.
 *
 * @param cls a 'const char *' with the HTML webpage to return
 * @param mime mime type to use
 * @param session session handle 
 * @param connection connection to use
 */
static int
not_found_page (const void *cls,
		const char *mime,
		struct Session *session,
		struct MHD_Connection *connection)
{
  int ret;
  struct MHD_Response *response;

  /* unsupported HTTP method */
  response = MHD_create_response_from_buffer (strlen (NOT_FOUND_ERROR),
					      (void *) NOT_FOUND_ERROR,
					      MHD_RESPMEM_PERSISTENT);
  ret = MHD_queue_response (connection, 
			    MHD_HTTP_NOT_FOUND, 
			    response);
  MHD_add_response_header (response,
			   MHD_HTTP_HEADER_CONTENT_ENCODING,
			   mime);
  MHD_destroy_response (response);
  return ret;
}
Example #18
0
static INT4 send_page (struct MHD_Connection *connection, const INT1 *page, INT4 status_code)
{
    INT4 ret;
    struct MHD_Response *response;

    if (NULL == page)
    {
        return MHD_NO;
    }

    response = MHD_create_response_from_buffer(strlen(page), (void *) page, MHD_RESPMEM_MUST_COPY);
    if (!response)
    {
        return MHD_NO;
    }

    ret = MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, "application/json");
    ret = MHD_queue_response(connection, status_code, response);

    MHD_destroy_response(response);
    return ret;
}
Example #19
0
int CWebServer::SendErrorResponse(struct MHD_Connection *connection, int errorType, HTTPMethod method)
{
  struct MHD_Response *response = NULL;
  int ret = CreateErrorResponse(connection, errorType, method, response);
  if (ret == MHD_YES)
  {
#ifdef WEBSERVER_DEBUG
    std::multimap<std::string, std::string> headerValues;
    GetRequestHeaderValues(connection, MHD_RESPONSE_HEADER_KIND, headerValues);

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

    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, errorType, response);
    MHD_destroy_response(response);
  }

  return ret;
}
Example #20
0
int present_error(struct MHD_Connection *connection, char * format, ...)
{
  va_list args;
  struct MHD_Response *response;
  int ret;

  DEBUG("\n");
  strcpy (pagebuff, "<html><body>");

  va_start (args, format);
  vsnprintf (&pagebuff[strlen(pagebuff)], PAGE_BUFFER_SIZE-strlen(pagebuff), format, args);

  strcat (pagebuff, "</body></html>");

  response = MHD_create_response_from_data (strlen(pagebuff), (void *)pagebuff, MHD_NO, MHD_NO);

  ret = MHD_queue_response (connection, MHD_HTTP_INTERNAL_SERVER_ERROR, response);
  MHD_destroy_response (response);
  va_end(args);

  return ret;
}
Example #21
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;
}
Example #22
0
/**
 * Queues a response to request basic authentication from the client
 *
 * @param connection The MHD connection structure
 * @param realm the realm presented to the client
 * @return MHD_YES on success, MHD_NO otherwise
 */
int 
MHD_queue_basic_auth_fail_response (struct MHD_Connection *connection,
				    const char *realm, 
				    struct MHD_Response *response) 
{
  int ret;
  size_t hlen = strlen(realm) + strlen("Basic realm=\"\"") + 1;
  char header[hlen];

  snprintf (header, 
	    sizeof (header), 
	    "Basic realm=\"%s\"", 
	    realm);
  ret = MHD_add_response_header (response,
				 MHD_HTTP_HEADER_WWW_AUTHENTICATE,
				 header);
  if (MHD_YES == ret)
    ret = MHD_queue_response (connection, 
			      MHD_HTTP_UNAUTHORIZED, 
			      response);
  return ret;
}
Example #23
0
static int
ahc_cancel (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;
  int ret;

  if (0 != strcmp ("POST", method))
    {
      fprintf (stderr,
	       "Unexpected method `%s'\n", method);
      return MHD_NO; 
    }

  if (*unused == NULL)
    {
      *unused = "wibble";
      /* We don't want the body. Send a 500. */
      response = MHD_create_response_from_buffer (0, NULL, 
						  MHD_RESPMEM_PERSISTENT);
      ret = MHD_queue_response(connection, 500, response);
      if (ret != MHD_YES)
	fprintf(stderr, "Failed to queue response\n");
      MHD_destroy_response(response);
      return ret;
    }
  else
    {
      fprintf(stderr, 
	      "In ahc_cancel again. This should not happen.\n");
      return MHD_NO;
    }
}
Example #24
0
inline int sendOptionsResponse(struct MHD_Connection* connection, bool allowGet) {
    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",
                            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_OK, response);
    MHD_destroy_response(response);

    return ret;
}
Example #25
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 **mptr)
{
  static int marker;
  struct MHD_Response *response;
  int ret;
  (void)cls;(void)url;(void)version;            /* Unused. Silent compiler warning. */
  (void)upload_data;(void)upload_data_size;     /* Unused. Silent compiler warning. */

  if (0 != strcmp ("POST", method))
    {
      printf ("METHOD: %s\n", method);
      return MHD_NO;            /* unexpected method */
    }
  if ((*mptr != NULL) && (0 == *upload_data_size))
    {
      if (*mptr != &marker)
        abort ();
      response = MHD_create_response_from_buffer (2, "OK",
						  MHD_RESPMEM_PERSISTENT);
      ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
      MHD_destroy_response (response);
      *mptr = NULL;
      return ret;
    }
  if (strlen (POST_DATA) != *upload_data_size)
    return MHD_YES;
  *upload_data_size = 0;
  *mptr = &marker;
  return MHD_YES;
}
Example #26
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)
{

	struct plugin *plugin = NULL;
	GSList* iterator = NULL;
	const char *answer = NULL;
	struct MHD_Response *response;
	int ret;

	if (plugin_list == NULL)
		return PLUGIN_ERROR_NULL;

	for (iterator = plugin_list; iterator; iterator = iterator->next) {
		plugin = iterator->data;


		// TODO need to parse the URL - this is too simple.
		if (strstr(url, plugin->name) != NULL) {
			answer = plugin->answer_request(url);
		}
	}

	if (answer == NULL) {
		answer = "<html><body>response not claimed by any plugin</body></html>";
	}

	response = MHD_create_response_from_buffer(strlen(answer), (void*) answer,
			MHD_RESPMEM_PERSISTENT);
	ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
	MHD_destroy_response(response);

	return ret;
}
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)
{
  const char *me = cls;
  struct MHD_Response *response;
  int ret;
  (void)version;(void)upload_data;      /* Unused. Silent compiler warning. */
  (void)upload_data_size;(void)unused;  /* Unused. Silent compiler warning. */

  if (0 != strcmp (me, method))
    return MHD_NO;              /* unexpected method */
  response = MHD_create_response_from_buffer (strlen (url),
					      (void *) url,
					      MHD_RESPMEM_MUST_COPY);
  ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  MHD_destroy_response (response);
  return ret;
}
Example #28
0
/**
 * Handler that returns a simple static HTTP page that
 * is passed in via 'cls'.
 *
 * @param cls a 'const char *' with the HTML webpage to return
 * @param mime mime type to use
 * @param session session handle 
 * @param connection connection to use
 */
static int
serve_simple_form (const void *cls,
		   const char *mime,
		   struct Session *session,
		   struct MHD_Connection *connection)
{
  int ret;
  const char *form = cls;
  struct MHD_Response *response;

  /* return static form */
  response = MHD_create_response_from_buffer (strlen (form),
					      (void *) form,
					      MHD_RESPMEM_PERSISTENT);
  add_session_cookie (session, response);
  MHD_add_response_header (response,
			   MHD_HTTP_HEADER_CONTENT_ENCODING,
			   mime);
  ret = MHD_queue_response (connection, 
			    MHD_HTTP_OK, 
			    response);
  MHD_destroy_response (response);
  return ret;
}
static int
write_to_riemann(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)
{
    int marker, ret;
    riemann_client_t *riemann_client = cls;

    if (*ptr != &marker || *upload_data_size != 0) {
        *ptr = &marker;
        *upload_data_size = 0;
        return MHD_YES;
    }

    {
        riemann_message_t msg = RIEMANN_MSG_INIT;
        riemann_event_t event = RIEMANN_EVENT_INIT;
        riemann_event_t *events[] = { NULL };
        attribute_list_t *head, *cur;
        int header_count = 2, i;
        riemann_attribute_pairs_t *attributes;

        events[0] = &event;
        riemann_event_set_host(&event, riemann_field_host);
        riemann_event_set_service(&event, riemann_field_service);

        head = push_riemann_attribute_pair(NULL, "Method", method);
        if (head == NULL) {
            return MHD_NO;
        }
        cur = push_riemann_attribute_pair(head, "URL", url);
        if (cur == NULL) {
            free(head->attr);
            free(head);
            return MHD_NO;
        }
        header_count += MHD_get_connection_values(connection, MHD_HEADER_KIND,
                                                  headers_to_attributes, &cur);

        attributes = calloc(header_count, sizeof(riemann_attribute_pairs_t));
        if (attributes == NULL) {
            cur = head;
            while (cur) {
                free(cur->attr);
                head = cur;
                free(cur);
                cur = head->next;
            }
            return MHD_NO;
        }
        cur = head;
        for (i = 0; i < header_count && cur; ++i) {
            attributes[i].key = cur->attr->key;
            attributes[i].value = cur->attr->value;
            free(cur->attr);
            head = cur->next;
            free(cur);
            cur = head;
        }
        riemann_event_set_attributes(&event, attributes, header_count);
        riemann_message_set_events(&msg, events, 1);

        ret = riemann_client_send_message(riemann_client, &msg, 0, NULL);
        if (ret) {
            fprintf(stderr, "Cannot send message: %s\n", strerror(errno));
        }
        fprintf(stderr, "Notfied riemann about '%s'\n", url);

        riemann_event_free(&event);
        free(attributes);
    }

    {
        struct MHD_Response *response;
        response = MHD_create_response_from_buffer(body_len, (void *)body,
                                                   MHD_RESPMEM_PERSISTENT);
        if (response) {
            MHD_add_response_header(response, "Content-Type", "text/html");
            ret = MHD_queue_response(connection, MHD_HTTP_OK, response);
            MHD_destroy_response(response);
        }
    }
    (void)version;
    (void)upload_data;
    return ret;
}
Example #30
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)
{
	str page = {NULL, 0};
	struct MHD_Response *response;
	int ret;
	void *async_data = NULL;
	struct httpd_cb *cb;
	const char *normalised_url;
	struct post_request *pr;
	str_str_t *kv;
	char *p;
	int cnt_type = HTTPD_STD_CNT_TYPE;
	int accept_type = HTTPD_STD_CNT_TYPE;
	int ret_code = MHD_HTTP_OK;

	LM_DBG("START *** cls=%p, connection=%p, url=%s, method=%s, "
			"versio=%s, upload_data[%zu]=%p, *con_cls=%p\n",
			cls, connection, url, method, version,
			*upload_data_size, upload_data, *con_cls);

	pr = *con_cls;
	if(pr == NULL){
		pr = pkg_malloc(sizeof(struct post_request));
		if(pr==NULL) {
			LM_ERR("oom while allocating post_request structure\n");
			return MHD_NO;
		}
		memset(pr, 0, sizeof(struct post_request));
		*con_cls = pr;
		pr = NULL;
	}

	if(strncmp(method, "POST", 4)==0) {
		if(pr == NULL){
			pr = *con_cls;
			pr->p_list = slinkedl_init(&httpd_alloc, &httpd_free);
			if (pr->p_list==NULL) {
				LM_ERR("oom while allocating list\n");
				return MHD_NO;
			}
			LM_DBG("running MHD_create_post_processor\n");
			pr->pp = MHD_create_post_processor(connection,
											post_buf_size,
											&post_iterator,
											pr);
			if(pr->pp==NULL) {
				if (*upload_data_size == 0) {
					/* We need to wait for morte data before
					 * handling the POST request */
					return MHD_YES;
				}
				LM_DBG("NOT a regular POST :o)\n");
				if (pr->content_type==0 && pr->content_len==0)
					MHD_get_connection_values(connection, MHD_HEADER_KIND,
											&getConnectionHeader, pr);
				if (pr->content_type<=0 || pr->content_len<=0) {
					LM_ERR("got a bogus request\n");
					return MHD_NO;
				}
				if (*upload_data_size != pr->content_len) {
					/* For now, we don't support large POST with truncated data */
					LM_ERR("got a truncated POST request\n");
					return MHD_NO;
				}
				LM_DBG("got ContentType [%d] with len [%d]: %.*s\\n",
					pr->content_type, pr->content_len,
					(int)*upload_data_size, upload_data);
				/* Here we save data. */
				switch (pr->content_type) {
				case HTTPD_TEXT_XML_CNT_TYPE:
				case HTTPD_APPLICATION_JSON_CNT_TYPE:
					/* Save the entire body as 'body' */
					kv = (str_str_t*)slinkedl_append(pr->p_list,
							sizeof(str_str_t) + 1 +
							*upload_data_size);
					p = (char*)(kv + 1);
					kv->key.len = 1; kv->key.s = p;
					memcpy(p, "1", 1);
					p += 1;
					kv->val.len = *upload_data_size;
					kv->val.s = p;
					memcpy(p, upload_data, *upload_data_size);
					break;
				default:
					LM_ERR("Unhandled data for ContentType [%d]\n",
							pr->content_type);
					return MHD_NO;
				}
				/* Mark the fact that we consumed all data */
				*upload_data_size = 0;
				return MHD_YES;
			}

			LM_DBG("pr=[%p] pp=[%p] p_list=[%p]\n",
					pr, pr->pp, pr->p_list);
			return MHD_YES;
		} else {
			if (pr->pp==NULL) {
				if (*upload_data_size == 0) {
					if (pr->content_type==HTTPD_TEXT_XML_CNT_TYPE)
						cnt_type = HTTPD_TEXT_XML_CNT_TYPE;
					if (pr->content_type==HTTPD_APPLICATION_JSON_CNT_TYPE)
						cnt_type = HTTPD_APPLICATION_JSON_CNT_TYPE;
					*con_cls = pr->p_list;
					cb = get_httpd_cb(url);
					if (cb) {
						normalised_url = &url[cb->http_root->len+1];
						LM_DBG("normalised_url=[%s]\n", normalised_url);
						ret_code = cb->callback(cls, (void*)connection,
								normalised_url,
								method, version,
								upload_data, upload_data_size, con_cls,
								&buffer, &page);
					} else {
						page = MI_HTTP_U_URL;
						ret_code = MHD_HTTP_BAD_REQUEST;
					}
					/* slinkedl_traverse(pr->p_list,
							&httpd_print_data, NULL, NULL); */
					slinkedl_list_destroy(*con_cls);
					pkg_free(pr); *con_cls = pr = NULL;
					goto send_response;
				}
				LM_DBG("NOT a regular POST :o)\n");
				if (pr->content_type==0 && pr->content_len==0)
					MHD_get_connection_values(connection, MHD_HEADER_KIND,
											&getConnectionHeader, pr);
				if (pr->content_type<=0 || pr->content_len<=0) {
					LM_ERR("got a bogus request\n");
					return MHD_NO;
				}
				if (*upload_data_size != pr->content_len) {
					/* For now, we don't support large POST with truncated data */
					LM_ERR("got a truncated POST request\n");
					return MHD_NO;
				}
				LM_DBG("got ContentType [%d] with len [%d]: %.*s\\n",
					pr->content_type, pr->content_len,
					(int)*upload_data_size, upload_data);
				/* Here we save data. */
				switch (pr->content_type) {
				case HTTPD_TEXT_XML_CNT_TYPE:
				case HTTPD_APPLICATION_JSON_CNT_TYPE:
					/* Save the entire body as 'body' */
					kv = (str_str_t*)slinkedl_append(pr->p_list,
							sizeof(str_str_t) + 1 +
							*upload_data_size);
					p = (char*)(kv + 1);
					kv->key.len = 1; kv->key.s = p;
					memcpy(p, "1", 1);
					p += 1;
					kv->val.len = *upload_data_size;
					kv->val.s = p;
					memcpy(p, upload_data, *upload_data_size);
					break;
				default:
					LM_ERR("Unhandled data for ContentType [%d]\n",
							pr->content_type);
					return MHD_NO;
				}
				/* Mark the fact that we consumed all data */
				*upload_data_size = 0;
				return MHD_YES;
			}
			LM_DBG("running MHD_post_process: "
					"pp=%p status=%d upload_data_size=%zu\n",
					pr->pp, pr->status, *upload_data_size);
			if (pr->status<0) {
				slinkedl_list_destroy(pr->p_list);
				pr->p_list = NULL;
				/* FIXME:
				 * It might be better to reply with an error
				 * instead of resetting the connection via MHD_NO */
				return MHD_NO;
			}
			ret =MHD_post_process(pr->pp, upload_data, *upload_data_size);
			LM_DBG("ret=%d upload_data_size=%zu\n", ret, *upload_data_size);
			if(*upload_data_size != 0) {
				*upload_data_size = 0;
				return MHD_YES;
			}

			LM_DBG("running MHD_destroy_post_processor: "
					"pr=[%p] pp=[%p] p_list=[%p]\n",
					pr, pr->pp, pr->p_list);
			MHD_destroy_post_processor(pr->pp);
			LM_DBG("done MHD_destroy_post_processor\n");
			/* slinkedl_traverse(pr->p_list, &httpd_print_data, NULL, NULL); */
			*con_cls = pr->p_list;

			cb = get_httpd_cb(url);
			if (cb) {
				normalised_url = &url[cb->http_root->len+1];
				LM_DBG("normalised_url=[%s]\n", normalised_url);
				ret_code = cb->callback(cls, (void*)connection,
						normalised_url,
						method, version,
						upload_data, upload_data_size, con_cls,
						&buffer, &page);
			} else {
				page = MI_HTTP_U_URL;
				ret_code = MHD_HTTP_BAD_REQUEST;
			}
			/* slinkedl_traverse(pr->p_list, &httpd_print_data, NULL, NULL); */
			slinkedl_list_destroy(*con_cls);
			pkg_free(pr); *con_cls = pr = NULL;
		}
	}else if(strncmp(method, "GET", 3)==0) {
		pr = *con_cls;
		MHD_get_connection_values(connection, MHD_HEADER_KIND,
								&getConnectionHeader, pr);
		accept_type = pr->accept_type;
		pkg_free(pr); *con_cls = pr = NULL;
		LM_DBG("accept_type=[%d]\n", accept_type);
		cb = get_httpd_cb(url);
		if (cb) {
			normalised_url = &url[cb->http_root->len+1];
			LM_DBG("normalised_url=[%s]\n", normalised_url);
			ret_code = cb->callback(cls, (void*)connection,
					normalised_url,
					method, version,
					upload_data, upload_data_size, con_cls,
					&buffer, &page);
		} else {
			page = MI_HTTP_U_URL;
			ret_code = MHD_HTTP_BAD_REQUEST;
		}
	}else{
		page = MI_HTTP_U_METHOD;
		ret_code = MHD_HTTP_METHOD_NOT_ACCEPTABLE;
	}

send_response:
	if (page.s) {
		LM_DBG("MHD_create_response_from_data [%p:%d]\n",
			page.s, page.len);
		response = MHD_create_response_from_data(page.len,
							(void*)page.s,
							0, 1);
	} else {
		LM_DBG("MHD_create_response_from_callback\n");
		response = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN,
							buffer.len,
							cb->flush_data_callback,
							(void*)async_data,
							NULL);
	}
	if (cnt_type==HTTPD_TEXT_XML_CNT_TYPE || accept_type==HTTPD_TEXT_XML_CNT_TYPE)
		MHD_add_response_header(response,
								MHD_HTTP_HEADER_CONTENT_TYPE,
								"text/xml; charset=utf-8");
	if (cnt_type==HTTPD_APPLICATION_JSON_CNT_TYPE || accept_type==HTTPD_APPLICATION_JSON_CNT_TYPE)
		MHD_add_response_header(response,
								MHD_HTTP_HEADER_CONTENT_TYPE,
								"application/json");
	ret = MHD_queue_response (connection, ret_code, response);
	MHD_destroy_response (response);

	return ret;
}