程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 如何直接獲取 libmicrohttpd 庫中POST上來的整個數據

如何直接獲取 libmicrohttpd 庫中POST上來的整個數據

編輯:C++入門知識

    由於 libmicrohttpd 庫在處理POST數據的時候是與表單的形勢處理的, HTTP協議中表單的提交和解析有特定的格式。但是在我們的需求中,我們POST上來的數據可能只是一個普通的XML體,並不是按照表單格式提交上來的數據,我們需要處理整個的XML體, 這個時候, libmicrohttpd 不能獲取到整個 POST上來的BODY, 追蹤源碼,我們可以添加一點點代碼即可獲取該數據。修改的源碼如下 :

 

 

[cpp]
/**
* Get a particular header value.  If multiple
* values match the kind, return any one of them.
*
* @param connection connection to get values from
* @param kind what kind of value are we looking for
* @param key the header to look for, NULL to lookup 'trailing' value without a key
* @return NULL if no such item was found
*/ 
const char * MHD_lookup_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key) 

    struct MHD_HTTP_Header *pos; 
 
    if (NULL == connection) 
        return NULL; 
 
        if (kind == MHD_POSTDATA_KIND) return connection->read_buffer;//只需要添加改行代碼即可  
 
    for (pos = connection->headers_received; NULL != pos; pos = pos->next) 
        if ((0 != (pos->kind & kind)) &&  
            ( (key == pos->header) || ( (NULL != pos->header) && 
            (NULL != key) && 
            (0 == strcasecmp (key, pos->header))) )) 
            return pos->value;     
    return NULL; 

/**
* Get a particular header value.  If multiple
* values match the kind, return any one of them.
*
* @param connection connection to get values from
* @param kind what kind of value are we looking for
* @param key the header to look for, NULL to lookup 'trailing' value without a key
* @return NULL if no such item was found
*/
const char * MHD_lookup_connection_value (struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key)
{
 struct MHD_HTTP_Header *pos;

 if (NULL == connection)
  return NULL;

        if (kind == MHD_POSTDATA_KIND) return connection->read_buffer;//只需要添加改行代碼即可

 for (pos = connection->headers_received; NULL != pos; pos = pos->next)
  if ((0 != (pos->kind & kind)) &&
   ( (key == pos->header) || ( (NULL != pos->header) &&
   (NULL != key) &&
   (0 == strcasecmp (key, pos->header))) ))
   return pos->value;   
 return NULL;
}
在上層應用層 ,我們可以用如下的代碼來獲取BODY數據:

 

 

[cpp]
const char* length = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH); 
if (length == NULL) 
    return NULL; 
 
const char* body = MHD_lookup_connection_value (connection, MHD_POSTDATA_KIND, NULL); 
if (body == NULL) 
        return NULL; 
 
         
size_t len = strlen(body); 

 const char* length = MHD_lookup_connection_value (connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
 if (length == NULL)
  return NULL;

 const char* body = MHD_lookup_connection_value (connection, MHD_POSTDATA_KIND, NULL);
 if (body == NULL)
         return NULL;

   
 size_t len = strlen(body);

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved