vc 实现一个 http Server

 

实现 http 的协议解析 以及封装 , 对应不同的访问地址, 调用绑定的处理程序 , 

#include "stdafx.h"

#include "lyocomm\webServer.h"
#include "lyocomm\webServer.cpp"


// http://127.0.0.1:10000/



unsigned int __stdcall favicon(void* requ )
{
	lyoRequest* req = (lyoRequest*) requ;
	char* buf;
	int bufSize;


	FILE * file = NULL;
	errno_t e = fopen_s(&file, "E:\\测试代码\\cWeb\\Debug\\favicon.ico", "r");
	if (!e && file)
	{
		fseek(file, 0,SEEK_END );
		bufSize = ftell(file);

		buf = new char[bufSize];

		fseek(file, 0, SEEK_SET );
		fread(buf,   bufSize, 1 , file) ;

		fclose(file);

	}

	lyoResponse resp;
	resp.status = http_200;
	resp.type = content_html ;
	resp.content = buf ;
	resp.length =  bufSize;

	lyoServer::doResponse(resp, req->sock);
	delete[] buf;
	delete req;

	return 1;

}


unsigned int __stdcall index(void* requ )
{
	lyoRequest* req = (lyoRequest*) requ;
	char* con = "<head><title>index.html</title></head><body><b>web server 测试页面</b></body>";


	lyoResponse resp;
	resp.status = http_200;
	resp.type = content_html ;
	resp.content = con ;
	resp.length = strlen( con);

	lyoServer::doResponse(resp, req->sock);
	delete req;

	return 1;

}


void main(int argc, char * argv[]) {


	lyoServer server = lyoServer("127.0.0.1" , 10000);

	void* p = (PVOID)&server ;

	server.handlers["/index.php"] = &index;
	server.handlers["/"] = &index;
	server.handlers["/favicon.ico"] = &favicon;


	if (server.valid)
	{
		server.listen();
	}

	

}

  以下是测试页面:

 

posted on 2015-04-04 22:20  透明色~  阅读(561)  评论(0编辑  收藏  举报

导航