我正试图在我的猴子服务器中获取我的身体内容时遇到一些问题。 事实上,我真的不知道如何使用fastcgi库函数访问我的数据。
我正在邮递员发送一个PUT请求http://my.server.address:myport/cgi/something
具有以下JSON内容:
{ "hello" : "bonjour" }
在服务器端
在主线程上运行:
int32_t SWEB_traiter_requette_f(int32_t i32_socket_listen) {
FCGX_Request st_request;
(void)FCGX_InitRequest(&st_request, i32_socket_listen, 0);
if (FCGX_Accept_r(&st_request) == 0) {
ULOG_log_f(ULOG_PRIO_INFO, "accepting request");
(void)pthread_mutex_lock(gpu_mutex_s_web);
traiter_requette_s_web_f(&st_request,FCGX_GetParam("REQUEST_URI", st_request.envp));
(void)pthread_mutex_unlock(gpu_mutex_s_web);
(void)FCGX_Finish_r(&st_request);
}
FCGX_Free(&st_request,i32_socket_listen);
return 0;
}
这就是我处理请求的方式:
static void traiter_requette_s_web_f(FCGX_Request *pst_request,const char * pc_url) {
size_t z_len;
char_t *pc_data;
int i = 0;
int ch;
char_t *sz_content_len = FCGX_GetParam("CONTENT_LENGTH" , pst_request->envp);
char_t *sz_method = FCGX_GetParam("REQUEST_METHOD" , pst_request->envp);
char_t *sz_contenttype = FCGX_GetParam("CONTENT_TYPE" , pst_request->envp);
if (sz_contenttype != NULL){
/* first method ..... not working */
ch = FCGX_GetChar(pst_request->in);
while(ch != -1){
i++;
z_len = strtol(sz_content_len, NULL, 10);
pc_data = calloc(1,z_len+1);
if (pc_data == NULL){
//LCOV_EXCL_START
ULOG_log_f(ULOG_PRIO_ERREUR, "Erreur d'allocation de psz_data");
return;
//LCOV_EXCL_STOP
}
pc_data[i-1] = (char_t) ch;
ch = FCGX_GetChar(pst_request->in);
if (ch == -1 )
{
pc_data=(char*)realloc(pc_data,(i + 1)*sizeof(char));
pc_data[i] = '\0';
}
}
printf("data !! : %s\n",pc_data);
/* second method .... not working */
z_len = strtol(sz_content_len, NULL, 10);
pc_data = calloc(1,z_len+1);
if (pc_data == NULL){
//LCOV_EXCL_START
ULOG_log_f(ULOG_PRIO_ERREUR, "Erreur d'allocation de psz_data");
return;
//LCOV_EXCL_STOP
}
(void)FCGX_GetStr(pc_data,z_len,pst_request->in);
printf("data !! : %s\n",pc_data);
}
}
也许我对pc_data
做错了,这不是如何访问正文。
如何访问我的请求正文?
答案 0 :(得分:0)
似乎我的代码漏了。
这是您在POST请求中获取正文内容的方式:
static void traiter_requette_s_web_f(FCGX_Request *pst_request,const char * pc_url) {
size_t z_len;
int i = 0;
int ch;
char_t *sz_content_len = FCGX_GetParam("CONTENT_LENGTH" , pst_request->envp);
char_t *sz_method = FCGX_GetParam("REQUEST_METHOD" , pst_request->envp);
char_t *sz_contenttype = FCGX_GetParam("CONTENT_TYPE" , pst_request->envp);
char_t *pc_data = FCGX_GetParam("REQUEST_URI", pst_request->envp);
if (sz_contenttype != NULL)
{
z_len = strtol(sz_content_len, NULL, 10);
pc_data = calloc(1,z_len+1);
if (pc_data == NULL){
return;
}
ch = FCGX_GetChar(pst_request->in);
while(ch != -1){
i++;
pc_data[i-1] = (char_t) ch;
ch = FCGX_GetChar(pst_request->in);
if (ch == -1 )
{
pc_data=(char*)realloc(pc_data,(i + 1)*sizeof(char));
pc_data[i] = '\0';
}
}
printf("data !! : %s\n",pc_data);
}
}
答案 1 :(得分:0)
根据https://fossies.org/dox/FCGI-0.78/,您可以在POST请求中阅读正文内容
#define DATA_READ_CHUNK 81912
void read_payload(size_t content_length, std::string&str) {
size_t read_length = 0;
size_t len = 0;
//DATA_READ_CHUNK should be 8192 according to FCGX_Accept_r(FCGX_Request *reqDataPtr) line 2154 file fcgiapp.c
while (true) {
char* buff;
if (content_length > DATA_READ_CHUNK) {
buff = (char*)malloc(DATA_READ_CHUNK + 1);
len = DATA_READ_CHUNK;
}
else {
buff = (char*)malloc(content_length + 1);
len = content_length;
}
buff[len] = '\0';
std::cin.read(buff, len);
str.append(buff, len);
free(buff);
content_length -= len;
if (content_length <= 0)break;
}
fclose(stdin);
}