drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.44k stars 1.1k forks source link

Segmentation Fault testing views with csp file #245

Closed Dalot closed 5 years ago

Dalot commented 5 years ago

Describe the bug I was following the wiki to create a csp file. Although I am getting a segmentation fault when I make a request to the endpoint.

To Reproduce Steps to reproduce the behavior:

  1. Create a Controller, register path
  2. Create csp file and its respective source files
  3. Build
  4. go to localhost/my_path/p1=a&p2=b&p3=c

Expected behavior Expected html as seen in wiki with a table of parameters

Screenshots image

Segmentation Fault when request happens: image

Desktop (please complete the following information):

Controller Source files:

demo_v1_user.h:

#pragma once
#include <drogon/HttpController.h>
using namespace drogon;
namespace demo
{
    namespace v1
    {
        class User:public drogon::HttpController<User>
        {
        public:
            METHOD_LIST_BEGIN
            //use METHOD_ADD to add your custom processing function here;
            //METHOD_ADD(User::get,"/get/{2}/{1}",Get);//path is /demo/v1/User/get/{arg2}/{arg1}
            //METHOD_ADD(User::your_method_name,"/{1}/{2}/list",Get);//path is /demo/v1/User/{arg1}/{arg2}/list
            //ADD_METHOD_TO(User::your_method_name,"/absolute/path/{1}/{2}/list",Get);//path is /demo/v1//absolute/path/{arg1}/{arg2}/list
            METHOD_ADD(User::getParameters,"list_para",Get);
            METHOD_LIST_END
            //your declaration of processing function maybe like this:
            //void get(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,int p1,std::string p2);
            //void your_method_name(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,double p1,int p2) const;
            void getParameters(const HttpRequestPtr &req,
                                   std::function<void (const HttpResponsePtr &)> &&callback);

        };
    }
}

demo_v1_user.cc:

#include "demo_v1_User.h"
using namespace demo::v1;
//add definition of your processing function here
void User::getParameters(const HttpRequestPtr &req,
                                   std::function<void (const HttpResponsePtr &)> &&callback)
{
    auto para = req->getParameters();
    HttpViewData data;
    data.insert("title","ListParameters");
    data.insert("parameters",para);
    auto resp=HttpResponse::newHttpViewResponse("ListParameters.csp",data);
    callback(resp);
};

Csp file:

<!DOCTYPE html>
<html>
<%c++
    auto para=@@.get<std::map<std::string,std::string>>("parameters");
%>
<head>
    <meta charset="UTF-8">
    <title>[[ title ]]</title>
</head>
<body>
    <%c++ if(para.size()>0){%>
    <H1>Parameters</H1>
    <table border="1">
      <tr>
        <th>name</th>
        <th>value</th>
      </tr>
      <%c++ for(auto iter:para){%>
      <tr>
        <td>{%iter.first%}</td>
        <td><%c++ $$<<iter.second;%></td>
      </tr>
      <%c++}%>
    </table>
    <%c++ }else{%>
    <H1>no parameter</H1>
    <%c++}%>
</body>
</html>
an-tao commented 5 years ago

Sorry, this is a mistake on wiki, please use std::unordered_map instead of std::map in the csp file and then regenerate view source files.

behindmagic9 commented 1 year ago

but its not working with parameters there , still showing the error " 20230711 17:58:01.426882 UTC 37478 ERROR Bad type - HttpViewData.h:50 " in the terminal while running or on reload and on browser it showing no parameters on this url http://localhost:1025/demo/v1/user/list_para?p1=a&p2=b&p3=c