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

📄 httpmin.c

📁 MiniWeb是一个针对嵌入式应用而开发的微型Web Server
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include "httppil.h"
#include "httpapi.h"

int MyUrlHandler(UrlHandlerParam* param);
int MyUrlHandlerInit(HttpParam* hp, int uninit);

//URL handler list
UrlHandler urlHandlerList[]={
	{"cfg.htm",MyUrlHandler,MyUrlHandlerInit},
	{NULL},
};

HttpParam httpParam;

struct {
	int ethif;
	char ip[16];
	int mode;
	char tvmode[4];
} cfgdata;

int MyUrlHandlerInit(HttpParam* hp, int uninit)
{
	if (!uninit) {
		memset(&cfgdata,0,sizeof(cfgdata));
		strcpy(cfgdata.ip,"192.168.0.100");
		cfgdata.tvmode[0]=1;
		cfgdata.tvmode[1]=1;
	} else {
		//nothing to do
	}
	return 0;	//0 on success, -1 on failure
}

int MyUrlHandler(UrlHandlerParam* param)
{
	static const char *html_head="<html><body><h2 align='center'>STB Configuration</h2><hr>";
	static const char *html_form_start="<form method='POST' action='cfg.htm'>";
	static const char *html_ethif[]={
		"Network Interface: <input type='radio' value='0' name='if'%s>DHCP ",
		"<input type='radio' name='if' value='1'%s>PPPoE ",
		"<input type='radio' name='if' value='2'%s>Static IP: "};
	static const char *html_ip="<input type='text' name='ip' size='20' value='%s'></p>";
	static const char *html_mode[]={
		"<p>Startup Mode: <select size='1' name='mode'><option value='0'%s>TV Mode</option>",
		"<option value='1'%s>VOD Mode</option>",
		"<option value='2'%s>Storage Mode</option></select></p>"};
	static const char *html_tvmode[]={
		"<p>TV Mode Supported: <input type='checkbox' name='m0' value='1'%s>PAL ",
		"<input type='checkbox' name='m1' value='1'%s>NTSC ",
		"<input type='checkbox' name='m2' value='1'%s>720p ",
		"<input type='checkbox' name='m3' value='1'%s>1080i</p>"};
	static const char *html_form_end="<p><input type='submit' value='Submit' name='B1'><input type='reset' value='Reset' name='B2'></p></form><hr>";
	static const char *html_tail="</body></html>";
	static const char *tvmodes[]={"PAL","NTSC","720p","1080i"};
	char *p=param->pucBuffer,*v;
	int i;

	if (param->pxVars) {
		// processing settings
		if ((v=mwGetVarValue(param->pxVars,"if")))
			cfgdata.ethif=atoi(v);
		if ((v=mwGetVarValue(param->pxVars,"mode")))
			cfgdata.mode=atoi(v);
		if ((v=mwGetVarValue(param->pxVars,"ip")))
			strcpy(cfgdata.ip,v);
		for (i=0; i<4; i++) {
			char buf[4];
			sprintf(buf,"m%d",i);
			cfgdata.tvmode[i]=mwGetVarValue(param->pxVars,buf)?1:0;
		}
		// print new settings in console
		printf("\n--- Configuration ---\nNetwork Interface: %d\n",cfgdata.ethif);
		if (cfgdata.ethif==2) printf("IP: %s\n",cfgdata.ip);
		printf("Startup Mode: %d\nTV Modes:",cfgdata.mode);
		for (i=0; i<4; i++) {
			if (cfgdata.tvmode[i]) printf(" %s",tvmodes[i]);
		}
		printf("\n---------------------\n\n");
	}

	p+=sprintf(p,"%s%s",html_head,html_form_start);
	for (i=0; i<3; i++) {
		p+=sprintf(p,html_ethif[i],(cfgdata.ethif==i)?" checked":"");
	}
	p+=sprintf(p,html_ip,cfgdata.ip);
	for (i=0; i<3; i++) {
		p+=sprintf(p,html_mode[i],(cfgdata.mode==i)?" selected":"");
	}
	for (i=0; i<4; i++) {
		p+=sprintf(p,html_tvmode[i],cfgdata.tvmode[i]?" checked":"");
	}
	p+=sprintf(p,"%s%s%s",html_form_end,param->pxVars?"<p>New settings applied</p>":"",html_tail);
	param->iDataBytes=(int)p-(int)(param->pucBuffer);
	param->fileType=HTTPFILETYPE_HTML;
	return FLAG_DATA_RAW;
}

//////////////////////////////////////////////////////////////////////////
// callback from the web server whenever it needs to substitute variables
//////////////////////////////////////////////////////////////////////////
int DefaultWebSubstCallback(SubstParam* sp)
{
	// the maximum length of variable value should never exceed the number
	// given by sp->iMaxValueBytes
	if (!strcmp(sp->pchParamName,"mykeyword")) {
		return sprintf(sp->pchParamValue, "%d", time(NULL));
	}
	return -1;
}

void MiniWebQuit(int arg) {
	printf("\nCaught signal (%d). MiniWeb shutting down...\n",arg);
	httpParam.bKillWebserver=1;
}

int main(int argc,char* argv[])
{
#ifndef WIN32
	signal(SIGINT, (void *) MiniWebQuit);
	signal(SIGTERM, (void *) MiniWebQuit);
	signal(SIGPIPE, SIG_IGN);
#endif

	httpParam.maxClients=32;
	httpParam.httpPort=80;
	httpParam.maxReqPerConn=99;
	httpParam.pchWebPath="webroot";
	httpParam.pxUrlHandler=urlHandlerList;
	//set web variable substitution callback
	httpParam.pfnSubst=DefaultWebSubstCallback;
	
	InitSocket();
	//start server
	if (!mwServerStart(&httpParam)) {
		//wait
		getchar();
		//shutdown server
		mwServerShutdown(&httpParam);
	}
	UninitSocket();
	return 0;
}

⌨️ 快捷键说明

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