16 #define DEFAULT_HTTP_PORT 80
17 extern char rx_buf[MAX_URI_SIZE];
18 extern char tx_buf[MAX_URI_SIZE];
19 extern uint8 BUFPUB[1024];
21 uint8 *homepage_default =
"/ipconfig.htm";
24 void mid(
char* src,
char* s1,
char* s2,
char* sub)
30 sub1=strstr((
char*)src,(
char*)s1);
31 sub1+=strlen((
char*)s1);
32 sub2=strstr((
char*)sub1,(
char*)s2);
35 strncpy((
char*)sub,(
char*)sub1,n);
40 void WebServer(uint8 s)
43 uint32 header_len=0, content_len=0, received_len=0;
47 ret =
TCPRecv(s, (int8*)rx_buf, MAX_URI_SIZE);
50 *(((uint8*)rx_buf)+ret) =
'\0';
52 if(strstr(rx_buf,
"Content-Length: ")){
53 mid((
char*)rx_buf,
"Content-Length: ",
"\r\n", sub);
54 content_len=atoi(sub);
55 header_len = (uint32)(strstr(rx_buf,
"\r\n\r\n") - rx_buf + 4);
58 while(received_len!=(content_len+header_len))
60 ret =
TCPRecv(s, (int8*)rx_buf+received_len, MAX_URI_SIZE);
64 *(((uint8*)rx_buf)+received_len) =
'\0';
67 HTTPProcessor(s, (
char*)rx_buf);
68 memset(rx_buf,0x00,MAX_URI_SIZE);
70 IINCHIP_WRITE(Sn_CR(s),Sn_CR_DISCON);
71 while( IINCHIP_READ(Sn_CR(s)) ) ;
76 DBG(
"UDP Socket Close");
80 LOGA(
"HTTP Server Started - ch(%d)",(uint16)s);
94 void cgi_callback_add(
char *tokken, cgi_func get_func, cgi_func set_func)
96 static uint16 total=0;
100 ERR(
"description string is NULL");
103 }
else if(total >= MAX_CGI_CALLBACK) {
104 ERR(
"not enough space");
108 len = strlen(tokken);
116 int32 HTTPSend(uint8 s,
char *src,
char *dest, uint16 len, uint8 mode)
119 char *oldtmp=0, *newtmp=0;
122 char *tmp = (
char*)BUFPUB;
126 while((newtmp = strstr(oldtmp,
"<="))){
128 ret +=
TCPSend(s, (int8*)oldtmp, (uint16)(newtmp-oldtmp));
131 strncat(dest, oldtmp, (uint16)(newtmp-oldtmp));
132 ret += (uint16)(newtmp-oldtmp);
135 ret += (uint16)(newtmp-oldtmp);
137 mid(newtmp,
"<=",
">", sub);
138 for(i=0; i<MAX_CGI_CALLBACK; i++){
141 i = MAX_CGI_CALLBACK;
148 ret +=
TCPSend(s, (int8*)tmp, mlen);
151 strncat(dest, tmp, mlen);
159 if(i==MAX_CGI_CALLBACK){
160 mlen = sprintf(tmp,
"<=%s>", sub);
163 ret +=
TCPSend(s, (int8*)tmp, mlen);
166 strncat(dest, tmp, mlen);
172 oldtmp = newtmp + strlen(sub)+3;
176 ret +=
TCPSend(s, (int8*)oldtmp, (len-(uint16)(oldtmp-src)));
179 strncat(dest, oldtmp, (len-(uint16)(oldtmp-src)));
180 ret += (len-(uint16)(oldtmp-src));
183 ret += (len-(uint16)(oldtmp-src));
188 void FILESend(uint8 s, st_http_request *http_request,
char* buf)
190 uint32 file_len=0, file_len_tmp=0;
191 uint32 send_len=0, content_len=0;
193 char* name=(
char*)http_request->URI;
198 if(!search_file_rom((
unsigned char *)name, &content, &file_len))
200 memcpy(buf, ERROR_HTML_PAGE,
sizeof(ERROR_HTML_PAGE));
201 TCPSend(s, (int8*)buf, strlen((
char const*)buf));
205 file_len_tmp = file_len;
209 if(file_len_tmp>1024)
211 read_from_flashbuf(content+send_len, (uint8*)buf, 1024);
214 content_len += HTTPSend(NULL, buf, NULL, 1024, 2);
221 read_from_flashbuf(content+send_len, (uint8*)buf, file_len_tmp);
222 buf[file_len_tmp] =
'\0';
225 content_len += HTTPSend(NULL, buf, NULL, file_len_tmp, 2);
227 send_len+=file_len_tmp;
228 file_len_tmp-=file_len_tmp;
232 TCPSend(s, (int8*)buf, strlen((
char const*)buf));
239 read_from_flashbuf(content+send_len, (uint8*)buf, 1024);
242 if(HTTPSend(s, buf, NULL, 1024, 0)<0)
252 read_from_flashbuf(content+send_len, (uint8*)buf, file_len);
253 buf[file_len] =
'\0';
256 HTTPSend(s, buf, NULL, (uint16)file_len, 0);
265 void HTTPProcessor(uint8 s,
char * buf)
267 uint8* http_response;
268 st_http_request *http_request;
270 http_response = (uint8*)rx_buf;
271 http_request = (st_http_request*)tx_buf;
273 memset(tx_buf,0x00,MAX_URI_SIZE);
275 memset(rx_buf,0x00,MAX_URI_SIZE);
278 switch (http_request->METHOD)
281 memcpy(http_response, ERROR_REQUEST_PAGE,
sizeof(ERROR_REQUEST_PAGE));
282 TCPSend(s, (int8 *)http_response, strlen((
char*)http_response));
287 if (!strcmp((
char*)http_request->URI,
"/")) strcpy(http_request->URI, (
char const*)homepage_default);
289 RESTProcessor(http_request);
294 if(http_request->TYPE == PTYPE_PL || http_request->TYPE ==
PTYPE_CGI)
296 CGIProcessor(http_request, (
char*)http_response);
299 FILESend(s, http_request, (
char*)http_response);
308 void RESTProcessor(st_http_request *http_request)
313 void CGIProcessor(st_http_request *http_request,
char* buf)
317 char *oldtmp=0, *newtmp=0;
318 char sub[32], *subtmp=0;
320 char* name=(
char*)http_request->URI;
325 if(search_file_rom((
unsigned char *)name, &content, &file_len))
330 read_from_flashbuf(content, (uint8*)buf, file_len);
331 buf[file_len] =
'\0';
332 HTTPSend(NULL, buf, (
char*)BUFPUB, file_len, 1);
334 oldtmp=(
char*)BUFPUB;
336 while((newtmp = strstr(oldtmp,
"<?"))){
337 mid(newtmp,
"<?",
"?>", sub);
338 oldtmp = newtmp + strlen(sub) + 4;
340 subtmp = strstr(sub,
"SetValue");
341 mid(subtmp,
"(",
")", sub);
343 for(i=0; i<MAX_CGI_CALLBACK; i++){
349 i = MAX_CGI_CALLBACK;
360 if(i==MAX_CGI_CALLBACK){