-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpmin.c
More file actions
145 lines (132 loc) · 4.49 KB
/
httpmin.c
File metadata and controls
145 lines (132 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#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;
}