⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nq_soc_http.c

📁 mtk socket编程
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************************************************
*   Copyright Statement:
*   -------------------   
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of Beijing NetQin Tech. Co., Ltd. (C) 2008
*    
***********************************************************************************
*    
* Filename :
* ----------------   
*  nq_soc_http.c  
*    
* Description : 
* ----------------   
*    
*    
* Author : 
* ----------------   
*   yuanzhonghua 
*    
* Date created : 
* ----------------   
*    2008/11/20 
*    
*    
*===================================================================
*    
*             				REVISIONS HISTORY
*---------------------------------------------------------------------------------
 * Date		Username		Description of Modification
 * --------     ----------    ---------------------------------------------------------
*    
*    
**********************************************************************************/

#include "string.h"
#include "stdlib.h"

#include "nq_soc_types.h"
#include "nq_soc_core.h"
#include "nq_soc_http.h"

static NQ_zh_http_context *g_nq_zh_http_context_p = NULL;

/*
* http 报文头选项域
*/
static NQ_zh_defhttpreqf g_nq_zh_ReqTable[]={
	{NQ_ZH_HTTPREQF_GET,     "GET ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_HEAD,     "HEAD ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_POST,     "POST ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_PUT,     "PUT ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_DELETE,     "DELETE ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_OPTION,     "OPTION ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_TRACE,     "TRACE ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_PATCH,     "PATCH ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_COPY,     "COPY ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_LINK,     "LINK ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_UNLINK,     "UNLINK ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_WRAPPED,     "WRAPPED ",      NULL, NULL},
		
	{NQ_ZH_HTTPREQF_METHOD_MAX,     "METHOD_MAX ",      NULL, NULL}, //TAG, NOT USED, BUT DON'T MODIFY

	{NQ_ZH_HTTPREQF_CacheControl,     "Cache-Control: ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_Connection,       "Connection: ",         NULL, NULL},
	{NQ_ZH_HTTPREQF_Date,             "Date: ",               NULL, NULL},
	{NQ_ZH_HTTPREQF_Pragma,           "Pragma: ",             NULL, NULL},
	{NQ_ZH_HTTPREQF_Trailer,          "Trailer: ",            NULL, NULL},
	{NQ_ZH_HTTPREQF_TransferEncoding,	"Transfer-Encoding: ",  NULL, NULL},
	{NQ_ZH_HTTPREQF_Upgrade,	        "Upgrade: ",            NULL, NULL},
	{NQ_ZH_HTTPREQF_Via,	            "Via: ",                NULL, NULL},
	{NQ_ZH_HTTPREQF_Warning,	        "Warning: ",            NULL, NULL},
	{NQ_ZH_HTTPREQF_Accept,           "Accept: ",             "*/*", NULL},
	{NQ_ZH_HTTPREQF_AcceptCharset,    "Accept-Charset: ",     NULL, NULL},
	{NQ_ZH_HTTPREQF_AcceptEncoding,   "Accept-Encoding: ",    NULL, NULL},
	{NQ_ZH_HTTPREQF_AcceptLanguage,   "Accept-Language: ",    NULL, NULL},
	{NQ_ZH_HTTPREQF_Authorization,    "Authorization: ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_Expect,           "Expect: ",             NULL, NULL},
	{NQ_ZH_HTTPREQF_From,             "From: ",               NULL, NULL},
	{NQ_ZH_HTTPREQF_Host,             "Host: ",               NULL, NULL},
	{NQ_ZH_HTTPREQF_IfMatch,          "If-Match: ",           NULL, NULL},
	{NQ_ZH_HTTPREQF_IfModifiedSince,  "If-Modified-Since: " , NULL, NULL},
	{NQ_ZH_HTTPREQF_IfNoneMatch,      "If-NoneMatch: ",       NULL, NULL},
	{NQ_ZH_HTTPREQF_IfRange,          "IfRange: ",            NULL, NULL},
	{NQ_ZH_HTTPREQF_IfUnmodifiedSince,"If-Unmodified-Since: ",NULL, NULL},
	{NQ_ZH_HTTPREQF_MaxForwords,      "Max-Forwards: ",       NULL, NULL},
	{NQ_ZH_HTTPREQF_ProxyAuthorization,"Proxy-Authorization: ",NULL, NULL},
	{NQ_ZH_HTTPREQF_Range,            "Range: ",              NULL, NULL},
	{NQ_ZH_HTTPREQF_Referer,          "Referer: ",            NULL, NULL},
	{NQ_ZH_HTTPREQF_TE,               "TE: ",                 NULL, NULL},
	{NQ_ZH_HTTPREQF_MDN,              "MDN: ",                NULL, NULL},
	{NQ_ZH_HTTPREQF_UserAgent,        "User-Agent: ",         NULL, NULL},
	{NQ_ZH_HTTPREQF_Mode,				"Mode: ",	        NULL, NULL},
	{NQ_ZH_HTTPREQF_TimeStep,         "TimeStep: ",         NULL, NULL},
	{NQ_ZH_HTTPREQF_RandKey,			"RandKey: ",         NULL, NULL},
	{NQ_ZH_HTTPREQF_CID,				"cid: ",         NULL, NULL},
	{NQ_ZH_HTTPREQF_batch,			"batch: ",				NULL, NULL},

	{NQ_ZH_HTTPREQF_Allow,            "Allow: ",              NULL, NULL},
	{NQ_ZH_HTTPREQF_ContentType,      "Content-type: ",       NULL, NULL},
	{NQ_ZH_HTTPREQF_ContentEncoding,  "Content-Encoding: ",   NULL, NULL},
	{NQ_ZH_HTTPREQF_ContentLanguage,  "Content-Language: ",   NULL, NULL},	
	{NQ_ZH_HTTPREQF_ContentLocation,  "Content-Location: ",   NULL, NULL},
	{NQ_ZH_HTTPREQF_ContentMD5,       "Content-MD5: ",        NULL, NULL},
	{NQ_ZH_HTTPREQF_ContentRange,     "Content-Range: ",      NULL, NULL},	
	{NQ_ZH_HTTPREQF_ContentLength,    "Content-Length: ",     NULL, NULL},
	{NQ_ZH_HTTPREQF_Expires,          "Expires: ",            NULL, NULL},
	{NQ_ZH_HTTPREQF_LastModified,     "Last-Modified: ",      NULL, NULL},
	{NQ_ZH_HTTPREQF_UaProfile,        "x-wap-profile=",       NULL, NULL},
	{NQ_ZH_HTTPREQF_extensionheader,	 "",                    NULL, NULL},
	{NQ_ZH_HTTPREQF_MAX,         NULL,                  NULL, NULL}
};

/******************************************************************************
 * nq_zh_http_destroy  
 *  
 * Description:
 *  销毁函数,释放发送和接收buffer占用的内存
 *  
 * Input: 
 *  
 *  
 * Output: 
 *  
 * Returns: 
 *  
 * 
 * modification history
 * -----------------------------------	-------
 * 
 ******************************************************************************/
void nq_zh_http_destroy(void)
{
	if(g_nq_zh_http_context_p)
	{
		if(g_nq_zh_http_context_p->request_buf)
		{
			nq_zh_free_mem(g_nq_zh_http_context_p->request_buf);
			g_nq_zh_http_context_p->request_buf = NULL;
		}
		if(g_nq_zh_http_context_p->recv_buf)
		{
			nq_zh_free_mem(g_nq_zh_http_context_p->recv_buf);
			g_nq_zh_http_context_p->recv_buf = NULL;
		}

		nq_zh_free_mem(g_nq_zh_http_context_p);
		g_nq_zh_http_context_p = NULL;
		
	}
}


/******************************************************************************
 * nq_zh_http_init  
 *  
 * Description:
 *  初始化函数,为发送和接收buffer 分配足够的内存
 *  
 * Input: 
 *  
 *  
 * Output: 
 *  
 * Returns: 
 *  
 * 
 * modification history
 * -----------------------------------	-------
 * 
 ******************************************************************************/
kal_int32 nq_zh_http_init(void)
{
	if(g_nq_zh_http_context_p == NULL)
	{
		g_nq_zh_http_context_p = (NQ_zh_http_context*)nq_zh_malloc_mem(sizeof(NQ_zh_http_context));
		if(!g_nq_zh_http_context_p)
		{
			goto abort;
		}
		memset(g_nq_zh_http_context_p, 0, sizeof(NQ_zh_http_context));
		g_nq_zh_http_context_p->request_buf = (kal_int8*)nq_zh_malloc_mem(NQ_ZH_HTTP_REQ_BUFFER_MAX);
		if(!g_nq_zh_http_context_p->request_buf)
		{
			goto abort;
		}
		memset(g_nq_zh_http_context_p->request_buf, 0, NQ_ZH_HTTP_REQ_BUFFER_MAX);
		g_nq_zh_http_context_p->recv_buf = (kal_int8*)nq_zh_malloc_mem(NQ_ZH_HTTP_RECV_BUFFER_MAX);
		if(!g_nq_zh_http_context_p->recv_buf)
		{
			goto abort;
		}
		memset(g_nq_zh_http_context_p->recv_buf, 0, NQ_ZH_HTTP_RECV_BUFFER_MAX);
		
		g_nq_zh_http_context_p->recv_offset = 0;
		g_nq_zh_http_context_p->request_offset = 0;
		g_nq_zh_http_context_p->recv_len_max = NQ_ZH_HTTP_RECV_BUFFER_MAX;
		return NQ_ZH_SUCCESS;
	}
	else
	{ 
		memset(g_nq_zh_http_context_p->request_buf, 0, NQ_ZH_HTTP_REQ_BUFFER_MAX);
		memset(g_nq_zh_http_context_p->recv_buf, 0, NQ_ZH_HTTP_RECV_BUFFER_MAX);
		g_nq_zh_http_context_p->recv_offset = 0;
		g_nq_zh_http_context_p->request_offset = 0;
		g_nq_zh_http_context_p->recv_len_max = NQ_ZH_HTTP_RECV_BUFFER_MAX;
		return NQ_ZH_SUCCESS;
	}
	
abort:
	nq_zh_http_destroy();
	return NQ_ZH_FAILED;
}


/******************************************************************************
 * nq_zh_http_SetDefaultOption  
 *  
 * Description:
 *  设置http报文头默认选项
 *  
 * Input: 
 *  
 *  
 * Output: 
 *  
 * Returns: 
 *  
 * 
 * modification history
 * -----------------------------------	-------
 * 
 ******************************************************************************/
kal_int32 nq_zh_http_SetDefaultOption(void)
{
	nq_zh_http_SetOption(NQ_ZH_HTTPREQF_Host, "10.0.0.172:80");
	nq_zh_http_SetOption(NQ_ZH_HTTPREQF_Accept, "*/*");
#if 0	
	nq_zh_http_SetOption(NQ_ZH_HTTPREQF_AcceptLanguage, "zh-cn, en;q=0.7 ");
	nq_zh_http_SetOption(NQ_ZH_HTTPREQF_AcceptCharset, "utf-8, gb2312");
	nq_zh_http_SetOption(NQ_ZH_HTTPREQF_AcceptEncoding, "gzip, deflate");
#endif
	return 1;
}

/******************************************************************************
 * nq_zh_http_SetOption  
 *  
 * Description:
 *  设置http报文头选项
 *  
 * Input: 
 *  NQ_ZH_HTTPREQF Field	报文头选项域
 *  kal_char* szContent		报文头选项内容
 *  
 * Output: 
 *  
 * Returns: 
 *  设置结果
 * 
 * modification history
 * -----------------------------------	-------
 * 
 ******************************************************************************/
kal_int32 nq_zh_http_SetOption(NQ_ZH_HTTPREQF Field, kal_char* szContent)
{
	kal_int32 i = 0;
	kal_int32 ret = 0, length = 0;

	while ( g_nq_zh_ReqTable[i].szHead != NULL )
	{
		if(i == NQ_ZH_HTTPREQF_METHOD_MAX)
		{
			i++;
			continue;
		}
		if ( g_nq_zh_ReqTable[i].id == Field )
		{
			//fill head string
			length = strlen(g_nq_zh_ReqTable[i].szHead);				
			memcpy(g_nq_zh_http_context_p->request_buf + g_nq_zh_http_context_p->request_offset, (char *)g_nq_zh_ReqTable[i].szHead, length);
			g_nq_zh_http_context_p->request_offset += length;
			
			if ( szContent != NULL )				
			{
				//fill content string
				length = strlen(szContent);
				memcpy(g_nq_zh_http_context_p->request_buf + g_nq_zh_http_context_p->request_offset, (char *)szContent, length);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -