?? httpreqparse.cpp
字號:
// HttpReqParse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "blocksock.h"
#include "HttpReqParse.h"
#include "string.h"
#include <cstring>
#include <afxhtml.h>
#include <wininet.h>
#define MAXLINELENGTH 100
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
int ReqRespSeq=0;
FILE *fpHtm1;
struct HTTPWorkTable_STRU *table=NULL;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
//CMyHttpBlockSocket sConnect;
int nRetCode = 0;
//CMainFrame CMF;
char HTTPData0[]="GET http://download.microtool.de:80/somedata.exe HTTP/1.1\r\n"
//"GET http://www.sina.com.cn HTTP/1.1\r\n"
//"HTTP/1.1 200 OK\r\n"
"Host:download.microtool.de\r\n"
"Accept:*/*\r\n"
"Pragma:no-cache\r\n"
"Cache-Control:no-cache\r\n"
"Referer:http://download.microtool.de/\r\n"
"User-Agent:Mozilla/4.04[en](Win95;I;Nav)\r\n\r\n";
char HTTPData1[]=//"GET http://download.microtool.de:80/somedata.exe HTTP/1.1\r\n"
//"GET http://www.sina.com.cn HTTP/1.1\r\n"
"HTTP/1.1 200 OK\r\n"
"Host:download.microtool.de\r\n"
"Accept:*/*\r\n"
"Pragma:no-cache\r\n"
"Cache-Control:no-cache\r\n"
"Referer:http://download.microtool.de/\r\n"
"User-Agent:Mozilla/4.04[en](Win95;I;Nav)\r\n\r\n"
"<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.0 Transitional//EN>\r\n"
"<HTML dir=ltr><HEAD><TITLE>The page cannot be displayed</TITLE>\r\n"
"message body1 message body1";
char HTTPData2[]="message body2 message body2";
char HTTPData3[]="message body3 message body3</HTML>";
char HTTPData4[]="GET http://download.microtool.de:80/somedata.exe HTTP/1.1\r\n"
//"GET http://www.sina.com.cn HTTP/1.1\r\n"
//"HTTP/1.1 200 OK\r\n"
"Host:download.microtool.de\r\n"
"Accept:*/*\r\n"
"Pragma:no-cache\r\n"
"Cache-Control:no-cache\r\n"
"Referer:http://download.microtool.de/\r\n"
"User-Agent:Mozilla/4.04[en](Win95;I;Nav)\r\n\r\n";
char HTTPData5[]="HTTP/1.1 200 OK\r\n"
"Via: 1.0 ROUTER\r\n"
"Connection: close"
"Proxy-Connection: close"
"Age: 87"
"Expires: Wed, 28 Dec 2005 07:25:54 GMT"
"Date: Wed, 28 Dec 2005 07:23:54 GMT"
"Content-Type: text/html\r\n\r\n\r\n\r\n"
"<html>\r\n"
"<head>\r\n"
"<title>ChinaRen-年輕人的門戶</title>\r\n"
"<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>\r\n"
"<sohu_cms_include:ren_index_style><style>\r\n"
"td,input,select {font-family:宋體,Arial;font-size:12px;color:#000000;}\r\n"
".ui_top{color:#000000;font-size:12px;line-height:18px;}\r\n"
".ui_top a:link{color:#000000;text-decoration:none;}\r\n"
".ui_top a:visited{color:#000000;text-decoration:none;}\r\n"
".ui_top a:hover {color:#000000;text-decoration:underline;}\r\n"
".black {\r\n"
" color: #333333;\r\n"
" text-decoration: none;\r\n"
" line-height: 19px;\r\n"
" font-size: 12px;\r\n"
"}\r\n\r\n"
".center01 {color: #464646;line-height: 20px;font-size: 12px;}\r\n"
".center01 a:link{color: #464646;text-decoration: none;}\r\n"
".center01 a:visited{color: #464646;text-decoration: none;}\r\n"
".center01 a:hover {color: #464646;text-decoration: underline;}\r\n\r\n"
".center02,a.center02:visited {color: #4F7702;line-height: 20px;font-size: 12px;text-decoration: none;}\r\n"
"a.center02:hover{color: #4F7702;text-decoration:underline;}\r\n\r\n"
".center03,a.center03:visited {color: #016C8B;line-height: 20px;font-size: 12px;text-decoration: none;\r\n";
char HTTPData6[]="chinarennnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn\r\n";
char HTTPData7[]="chinarennnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn\r\n</HTML>";
char HTTPData8[]="GET http://download.microtool.de:80/somedata.exe HTTP/1.1\r\n"
//"GET http://www.sina.com.cn HTTP/1.1\r\n"
//"HTTP/1.1 200 OK\r\n"
"Host:download.microtool.de\r\n"
"Accept:*/*\r\n"
"Pragma:no-cache\r\n"
"Cache-Control:no-cache\r\n"
"Referer:http://download.microtool.de/\r\n"
"User-Agent:Mozilla/4.04[en](Win95;I;Nav)\r\n\r\n";
char HTTPData9[]="HTTP/1.1 200 OK\r\n"
"Via: 1.0 ROUTER\r\n"
"Connection: close"
"Proxy-Connection: close"
"Age: 87"
"Expires: Wed, 28 Dec 2005 07:25:54 GMT"
"Date: Wed, 28 Dec 2005 07:23:54 GMT"
"Content-Type: text/html\r\n\r\n\r\n\r\n"
"<html>\r\n"
"<head>\r\n"
"搜狐首頁</title>\r\n"
"<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>\r\n"
"<sohu_cms_include:ren_index_style><style>\r\n"
"td,input,select {font-family:宋體,Arial;font-size:12px;color:#000000;}\r\n"
".ui_top{color:#000000;font-size:12px;line-height:18px;}\r\n"
".ui_top a:link{color:#000000;text-decoration:none;}\r\n"
".ui_top a:visited{color:#000000;text-decoration:none;}\r\n"
".ui_top a:hover {color:#000000;text-decoration:underline;}\r\n"
".black {\r\n</html>";
char HTTPData10[]="HTTP/1.1 200 OK\r\n"
"Via: 1.0 ROUTER\r\n"
"Connection: close"
"Proxy-Connection: close"
"Age: 87"
"Expires: Wed, 28 Dec 2005 07:25:54 GMT"
"Date: Wed, 28 Dec 2005 07:23:54 GMT"
"Content-Type: text/html\r\n\r\n\r\n\r\n"
"<html>\r\n"
"<head>\r\n"
"搜狐首頁</title>\r\n"
"<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>\r\n"
"<sohu_cms_include:ren_index_style><style>\r\n"
"td,input,select {font-family:宋體,Arial;font-size:12px;color:#000000;}\r\n"
".ui_top{color:#000000;font-size:12px;line-height:18px;}\r\n";
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
CString strHello;
strHello.LoadString(IDS_HELLO);
cout << (LPCTSTR)strHello << endl;
}
func_HTTP_Init();
//printf("request----->%s\n",HTTPData1);
func_HTTP_Main2(HTTPData0);
func_HTTP_Main2(HTTPData1);
func_HTTP_Main2(HTTPData2);
func_HTTP_Main2(HTTPData3);
func_HTTP_Main2(HTTPData4);
func_HTTP_Main2(HTTPData5);
func_HTTP_Main2(HTTPData6);
func_HTTP_Main2(HTTPData7);
func_HTTP_Main2(HTTPData8);
func_HTTP_Main2(HTTPData9);
func_HTTP_Main2(HTTPData10);
return nRetCode;
}
BOOL func_HTTP_ParseHTTPInfoData2(char* pStr, char** ppToken1, char** ppToken3)
{
*ppToken1=pStr;
char* pch=strstr(pStr, "\r\n\r\n");
if(pch) //找到匹配字符第一個' '
{
*pch='\0';//ppToken1字符串至此結束
pch++;
*ppToken3=pch+3;
return TRUE;
}
return FALSE;
}
int func_HTTP_Main2(char *HTTPData)
{
char *HTTPData2;
char* MsgHead;
char* MsgBody;
char* MsgBody2;
char* befHtml;
char* behHtml;
char* befStytle;
char* behStytle;
char* bef_Html;
char* beh_Html;
char* bef_Stytle;
char* beh_Stytle;
//char *URL;
char filename[100];
CString htmfilename;
int i;
char *pch;
if(*HTTPData=='G'&&*(HTTPData+1)=='E'&&*(HTTPData+2)=='T')//GET
{
printf("\n+++++++++++++++++++++request+++++++++++++++++++++++++++++++++\n");
printf("此IP包帶有GET請求消息頭!\n");
for(i=0;i<MaxRequestNum;i++)
{
if(HTTPRequestSeqFlag[i]==0)
{
ReqRespSeq=i;
HTTPRequestSeqFlag[i]=1;
break;
}
}
if(i>=MaxRequestNum)
{printf("工作區滿!\n");return(0);}
table=&HTTPWorkTable[ReqRespSeq];
table->HTTPState=HTTP_Request_State;
table->GETStart=1;
//table->Content_Coding=...
//table->Content_Length=...
//table->Content_Type=...
//table->ReqServiceBuffer=...
printf("此輪序號--->%d\n",ReqRespSeq);
printf("request----->%s\n",HTTPData);
pch=strstr(HTTPData, "Accept-Encoding: gzip");
if(pch)
{
table->GzipState=1;
}
}
else if(*HTTPData=='P'&&*(HTTPData+1)=='O'&&*(HTTPData+2)=='S'&&*(HTTPData+2)=='T')//POST
{
printf("\n+++++++++++++++++++++request+++++++++++++++++++++++++++++++++\n");
printf("此IP包帶有POST請求消息頭!\n");
printf("此輪序號--->%d\n",ReqRespSeq);
printf("request----->%s\n",HTTPData);
for(i=0;i<MaxRequestNum;i++)
{
if(HTTPRequestSeqFlag[i]==0)
{
ReqRespSeq=i;
HTTPRequestSeqFlag[i]=1;
break;
}
}
if(i>=MaxRequestNum)
{printf("工作區滿!\n");return(0);}
table=&HTTPWorkTable[ReqRespSeq];
table->HTTPState=HTTP_Request_State;
table->GETStart=1;
//table->Content_Coding=...
//table->Content_Length=...
//table->Content_Type=...
//table->ReqServiceBuffer=...
pch=strstr(HTTPData, "Accept-Encoding: gzip");
if(pch)
{
table->GzipState=1;
}
}
//else if(....)
else if(*HTTPData=='H'&&*(HTTPData+1)=='T'&&*(HTTPData+2)=='T'&&*(HTTPData+3)=='P'
&&*(HTTPData+4)=='/'&&*(HTTPData+5)=='1'&&*(HTTPData+6)=='.'
&&*(HTTPData+7)=='1'&&*(HTTPData+8)==' ')
{
if(table==NULL)
{
for(i=0;i<MaxRequestNum;i++)
{
if(HTTPRequestSeqFlag[i]==0)
{
ReqRespSeq=i;
HTTPRequestSeqFlag[i]=1;
break;
}
}
if(i>=MaxRequestNum)
{printf("工作區滿!\n");return(0);}
table=&HTTPWorkTable[ReqRespSeq];
}
printf("此IP包帶有響應消息頭!\n");
if(table!=NULL)
table->HTTPState=HTTP_Response_State;
else
{
printf("沒有請求或沒有為請求分配工作區!\n");
return(0);
}
//if(table->GETStart!=1)
//{
//}
strncpy(table->ResponseCode,HTTPData+9,3);
printf("code-->%s\n",table->ResponseCode);
pch=strstr(HTTPData, "Content-Length: 0");
if(pch) table->Content_Length=0;
pch=strstr(HTTPData, "Content-Encoding: gzip");
if(pch) table->GzipState=1;
/*if(table->GzipState==1)
{
func_GzipDecode(HTTPData,&HTTPData2);//有解碼函數嗎?
HTTPData=HTTPData2;
}*/
//table->Content_Coding=...
//table->Content_Length=...
//table->Content_Type=...
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -