source: titan/titan/httpd.h @ 37145

Last change on this file since 37145 was 34859, checked in by obi, 9 years ago

webif add system/telnet page

File size: 24.1 KB
Line 
1#ifndef HTTPD_H
2#define HTTPD_H
3
4char* getmimetype(char* name, char* mime)
5{
6        char* dot;
7
8        if(mime != NULL) return mime;
9        if(name == NULL) return "text/html";
10
11        dot = strrchr( name, '.' );
12        if(dot == NULL || strcmp(dot, ".html") == 0 || strcmp(dot, ".htm") == 0)
13                return "text/html";
14        if(strcmp(dot, ".jpg") == 0 || strcmp( dot, ".jpeg" ) == 0)
15                return "image/jpeg";
16        if(strcmp(dot, ".gif") == 0)
17                return "image/gif";
18        if(strcmp(dot, ".png") == 0)
19                return "image/png";
20        if(strcmp(dot, ".css") == 0)
21                return "text/css";
22        if(strcmp(dot, ".au") == 0)
23                return "audio/basic";
24        if(strcmp(dot, ".wav") == 0)
25                return "audio/wav";
26        if(strcmp(dot, ".avi") == 0)
27                return "video/x-msvideo";
28        if(strcmp(dot, ".mov") == 0 || strcmp(dot, ".qt" ) == 0)
29                return "video/quicktime";
30        if(strcmp(dot, ".mpeg") == 0 || strcmp(dot, ".mpe" ) == 0)
31                return "video/mpeg";
32        if(strcmp(dot, ".ts") == 0)
33                return "video/MP2T";   
34        if(strcmp(dot, ".vrml") == 0 || strcmp(dot, ".wrl" ) == 0)
35                return "model/vrml";
36        if(strcmp(dot, ".midi") == 0 || strcmp(dot, ".mid" ) == 0)
37                return "audio/midi";
38        if(strcmp(dot, ".mp3") == 0)
39                return "audio/mpeg";
40        if(strcmp(dot, ".ogg") == 0)
41                return "application/ogg";
42        if(strcmp(dot, ".pac") == 0)
43                return "application/x-ns-proxy-autoconfig";
44
45        return "text/html";
46}
47
48char* createheader(off64_t len, char* filename, char* mime, char* ext, int code, int auth)
49{
50        char* header = NULL, *buf = NULL;
51        time_t now = time(NULL);
52
53        buf = malloc(100);
54        if(buf == NULL)
55        {
56                err("no mem");
57                return NULL;
58        }
59
60        if(auth == 1) code=401;
61        switch(code)
62        {
63                case 200:
64                        header = ostrcat(header, "HTTP/1.1 200 OK\r\n", 1, 0);
65                        break;
66                case 302:
67                        header = ostrcat(header, "HTTP/1.1 302 Found\r\n", 1, 0);
68                        break;
69                case 401:
70                        header = ostrcat(header, "HTTP/1.1 401 Unauthorized\r\n", 1, 0);
71                        break;
72                case 500:
73                        header = ostrcat(header, "HTTP/1.1 500 Internal Server Error\r\n", 1, 0);
74                        break;
75        }
76
77        snprintf(buf, 99, "Server: %s\r\n", PROGNAME);
78        header = ostrcat(header, buf, 1, 0);
79
80        if(auth == 1)
81        {
82                snprintf(buf, 99, "WWW-Authenticate: Basic realm=\"Contol Panel\"\r\n");
83                header = ostrcat(header, buf, 1, 0);
84        }
85
86        strftime(buf, 99, "Date: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&now));
87        header = ostrcat(header, buf, 1, 0);
88
89        header = ostrcat(header, "Content-Type: ", 1, 0);
90        header = ostrcat(header, getmimetype(filename, mime), 1, 0);
91        header = ostrcat(header, "\r\n", 1, 0);
92
93        if(auth == 1) len = 0;
94        snprintf(buf, 99, "Content-Length: %lld\r\n", len);
95        header = ostrcat(header, buf, 1, 0);
96
97        strftime(buf, 99, "Expires: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&now));
98        header = ostrcat(header, buf, 1, 0);
99
100        if(filename != NULL)
101        {
102                now = getfiletime(filename, 1);
103                strftime(buf, 99, "Last-Modified: %a, %d %b %Y %H:%M:%S GMT\r\n", gmtime(&now));
104                header = ostrcat(header, buf, 1, 0);
105        }
106
107        if(ext != NULL)
108        {
109                header = ostrcat(header, ext, 1, 0);
110                header = ostrcat(header, "\r\n", 1, 0);
111        }
112
113        header = ostrcat(header, "Connection: close\r\n", 1, 0);
114        header = ostrcat(header, "\r\n", 1, 0);
115
116        free(buf);
117        return header;
118}
119
120void senderror(int* connfd, char* title, char* text, int auth, int fmt)
121{
122        char* buf = NULL, *header = NULL;;
123
124        if(fmt == 0)
125        {
126                buf = ostrcat(buf, "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><link rel=stylesheet type=text/css href=titan.css><title>Error ", 1, 0);
127                buf = ostrcat(buf, title, 1, 0);
128                buf = ostrcat(buf, "</title></head><body class=body><font class=error>Error - ", 1, 0);
129                buf = ostrcat(buf, text, 1, 0);
130                buf = ostrcat(buf, "</font></body></html>", 1, 0);
131        }
132        else
133        {
134                buf = ostrcat(buf, "Error: ", 1, 0);
135                buf = ostrcat(buf, text, 1, 0);
136        }
137
138        header = createheader(strlen(buf), NULL, NULL, NULL, 500, auth);
139
140        socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
141        socksend(connfd, (unsigned char*)buf, strlen(buf), 5000 * 1000);
142
143        free(header);
144        free(buf);
145}
146
147void checkquery(int* connfd, char* query, int auth, int fmt)
148{
149        char* buf = NULL, *header = NULL, *param = NULL;
150        char* ext = NULL, *mime = NULL;
151        int buflen = 0, onlyheader = 0, code = 200;
152
153        //create param
154        param = strchr(query, '&');
155        if(param != NULL)
156                *param++ = '\0';
157
158        if(ostrcmp(query, "getbouquetepg") == 0)
159        {
160                m_lock(&status.waitrcmutex, 24);
161                buf = webgetbouquetepg(param, fmt);
162                m_unlock(&status.waitrcmutex, 24);
163        }
164        else if(ostrcmp(query, "getchannellock") == 0)
165        {
166                m_lock(&status.waitrcmutex, 24);
167                buf = webgetchannellock(param, fmt);
168                m_unlock(&status.waitrcmutex, 24);
169        }
170        else if(ostrcmp(query, "getsysteminfo") == 0)
171                buf = webgetsysteminfo(fmt);
172        else if(ostrcmp(query, "getrecfreesize") == 0)
173                buf = webgetrecfreesize(fmt);   
174        else if(ostrcmp(query, "getrccodes") == 0)
175                buf = webgetrccodes(fmt);
176        else if(ostrcmp(query, "getmute") == 0)
177                buf = webgetmute(fmt);
178        else if(ostrcmp(query, "getrecsteampath") == 0)
179                buf = webgetrecsteampath(fmt);
180        else if(ostrcmp(query, "getvol") == 0)
181                buf = webgetvol(fmt);
182        else if(ostrcmp(query, "sendrc") == 0)
183                buf = websendrc(param, fmt);
184        else if(ostrcmp(query, "getrectimer") == 0)
185        {
186                m_lock(&status.waitrcmutex, 24);
187                buf = webgetrectimer(param, 0, fmt);
188                m_unlock(&status.waitrcmutex, 24);
189        }
190        else if(ostrcmp(query, "addrectimer") == 0)
191        {
192                m_lock(&status.waitrcmutex, 24);
193                buf = webaddrectimer(param, fmt);
194                m_unlock(&status.waitrcmutex, 24);
195        }
196        else if(ostrcmp(query, "delrectimer") == 0)
197        {
198                m_lock(&status.waitrcmutex, 24);
199                buf = webdelrectimer(param, fmt);
200                m_unlock(&status.waitrcmutex, 24);
201        }
202        else if(ostrcmp(query, "editrectimer") == 0)
203        {
204                m_lock(&status.waitrcmutex, 24);
205                buf = webeditrectimer(param, fmt);
206                m_unlock(&status.waitrcmutex, 24);
207        }
208        else if(ostrcmp(query, "rectimercheck") == 0)
209        {
210                m_lock(&status.waitrcmutex, 24);
211                buf = webrectimercheck(param, fmt);
212                m_unlock(&status.waitrcmutex, 24);
213        }
214        else if(ostrcmp(query, "rectimersend") == 0)
215        {
216                m_lock(&status.waitrcmutex, 24);
217                buf = webrectimersend(param, fmt);
218                m_unlock(&status.waitrcmutex, 24);
219        }
220        else if(ostrcmp(query, "getrectimerarchive") == 0)
221        {
222                m_lock(&status.waitrcmutex, 24);
223                buf = webgetrectimer(param, 1, fmt);
224                m_unlock(&status.waitrcmutex, 24);
225        }
226        else if(ostrcmp(query, "adjust") == 0)
227        {
228                buf = webadjust(param, fmt);
229        }
230        else if(ostrcmp(query, "adjustsend") == 0)
231        {
232                buf = webadjustsend(param, fmt);
233        }
234        else if(ostrcmp(query, "setvol") == 0)
235                buf = websetvol(param, fmt);
236        else if(ostrcmp(query, "setmute") == 0)
237                buf = websetmute(param, fmt);
238        else if(ostrcmp(query, "getbouquet") == 0)
239        {
240                m_lock(&status.waitrcmutex, 24);
241                buf = webgetbouquet(fmt);
242                m_unlock(&status.waitrcmutex, 24);
243        }
244        else if(ostrcmp(query, "getsat") == 0)
245        {
246                m_lock(&status.waitrcmutex, 24);
247                buf = webgetsat(fmt);
248                m_unlock(&status.waitrcmutex, 24);
249        }
250        else if(ostrcmp(query, "getprovider") == 0)
251        {
252                m_lock(&status.waitrcmutex, 24);
253                buf = webgetprovider(fmt);
254                m_unlock(&status.waitrcmutex, 24);
255        }
256        else if(ostrcmp(query, "getaz") == 0)
257                buf = webgetaz(fmt);
258        else if(ostrcmp(query, "getconfig") == 0)
259                buf = webgetconfig(fmt);
260        else if(ostrcmp(query, "getchannelpage") == 0)
261        {
262                m_lock(&status.waitrcmutex, 24);
263                buf = webgetchannelpage(param, fmt);
264                m_unlock(&status.waitrcmutex, 24);
265        }
266        else if(ostrcmp(query, "getallchannel") == 0)
267        {
268                m_lock(&status.waitrcmutex, 24);
269                buf = webgetchannel(0, 0, 1, fmt);
270                m_unlock(&status.waitrcmutex, 24);
271        }
272        else if(ostrcmp(query, "getbouquetchannel") == 0)
273        {
274                m_lock(&status.waitrcmutex, 24);
275                buf = webgetbouquetchannel(param, fmt);
276                m_unlock(&status.waitrcmutex, 24);
277        }
278        else if((ostrcmp(query, "getcommand") == 0 || ostrcmp(query, "getcommand=") == 0))
279        {
280                m_lock(&status.waitrcmutex, 24);
281                buf = webgetcommand(param, fmt);
282                m_unlock(&status.waitrcmutex, 24);             
283        }
284        else if(ostrcmp(query, "getsystem") == 0 && param != NULL)
285        {
286                m_lock(&status.waitrcmutex, 24);
287                buf = webgetsystem(param, fmt);
288                m_unlock(&status.waitrcmutex, 24);
289        }
290        else if(ostrcmp(query, "gethelpchoices") == 0)
291        {
292                m_lock(&status.waitrcmutex, 24);
293                buf = webgethelpchoices(fmt);
294                m_unlock(&status.waitrcmutex, 24);
295        }
296        else if(ostrcmp(query, "gettestpage") == 0 && param != NULL)
297        {
298                m_lock(&status.waitrcmutex, 24);
299                buf = webgettestpage(param, fmt);
300                m_unlock(&status.waitrcmutex, 24);
301        }
302        else if(ostrcmp(query, "getsysteminfos") == 0 && param != NULL)
303                buf = webgetsysteminfos(param, fmt);
304        else if(ostrcmp(query, "getsysinfos") == 0 && param != NULL)
305                buf = webgetsysinfos(param, fmt);
306        else if(ostrcmp(query, "getlogs") == 0 && param != NULL)
307                buf = webgetlogs(param, fmt);
308        else if(ostrcmp(query, "getabout") == 0)
309                buf = webgetabout(fmt);
310        else if(ostrcmp(query, "gettpksection") == 0)
311                buf = webgettpksection(fmt);
312        else if(ostrcmp(query, "gettpklist") == 0 && param != NULL)
313                buf = webgettpklist(param, fmt);
314        else if(ostrcmp(query, "gettpkinstall") == 0 && param != NULL)
315                buf = webgettpkinstall(param, fmt);
316        else if(ostrcmp(query, "gettpkinstallpath") == 0 && param != NULL)
317                buf = webgettpkinstallpath(param, fmt);
318        else if(ostrcmp(query, "gettpktmplist") == 0 && param != NULL)
319                buf = webgettpktmplist(param, fmt);
320        else if(ostrcmp(query, "gettpkremove") == 0 && param != NULL)
321                buf = webgettpkremove(param, fmt);
322        else if(ostrcmp(query, "getrestoredefault") == 0 && param != NULL)
323                buf = webgetrestoredefault(param, fmt);
324        else if(ostrcmp(query, "getchannelsettings") == 0 && param != NULL)
325                buf = webgetchannelsettings(param, fmt);
326        else if(ostrcmp(query, "getrestoredefaultlist") == 0)
327                buf = webgetrestoredefaultlist(fmt);
328        else if(ostrcmp(query, "getchannelsettingslist") == 0)
329                buf = webgetchannelsettingslist(fmt);
330        else if(ostrcmp(query, "gettpkremovelist") == 0)
331                buf = webgettpkremovelist(fmt);
332        else if(ostrcmp(query, "gettpkupgrade") == 0)
333                buf = webgettpkupgrade(fmt);
334        else if(ostrcmp(query, "gettelnet") == 0)
335                buf = webgettelnet(fmt);
336        else if(ostrcmp(query, "getbackup") == 0)
337                buf = webgetbackup(fmt);
338        else if(ostrcmp(query, "getcreatebackup") == 0)
339                buf = webgetcreatebackup(fmt);
340        else if(ostrcmp(query, "getrestore") == 0)
341                buf = webgetrestore(fmt);
342        else if(ostrcmp(query, "getcreaterestore") == 0 && param != NULL)
343                buf = webgetcreaterestore(param, fmt);
344        else if(ostrcmp(query, "getserviceinfo") == 0)
345                buf = webgetserviceinfo(fmt);
346        else if(ostrcmp(query, "getstreaming") == 0 && param != NULL)
347                buf = webgetstreaming(param, fmt);
348        else if(ostrcmp(query, "getstreamingchoices") == 0)
349                buf = webgetstreamingchoices(fmt);             
350        else if(ostrcmp(query, "getnewsletter") == 0 && param != NULL)
351                buf = webgetnewsletter(param, fmt);
352        else if(ostrcmp(query, "getnewsletterchoices") == 0)
353                buf = webgetnewsletterchoices(fmt);
354        else if(ostrcmp(query, "gethelp") == 0 && param != NULL)
355                buf = webgethelp(param, fmt);
356        else if(ostrcmp(query, "getupdatelist") == 0 && param != NULL)
357                buf = webgetupdatelist(param, fmt);
358        else if(ostrcmp(query, "getupdate") == 0 && param != NULL)
359                buf = webgetupdate(param, fmt);
360        else if(ostrcmp(query, "getsatchannel") == 0 && param != NULL)
361        {
362                m_lock(&status.waitrcmutex, 24);
363                buf = webgetchannel(atoi(param), 1, 1, fmt);
364                m_unlock(&status.waitrcmutex, 24);
365        }
366        else if(ostrcmp(query, "getproviderchannel") == 0 && param != NULL)
367        {
368                m_lock(&status.waitrcmutex, 24);
369                buf = webgetchannel(atoi(param), 2, 1, fmt);
370                m_unlock(&status.waitrcmutex, 24);
371        }
372        else if(ostrcmp(query, "getazchannel") == 0 && param != NULL)
373        {
374                m_lock(&status.waitrcmutex, 24);
375                buf = webgetchannel(atoi(param), 3, 1, fmt);
376                m_unlock(&status.waitrcmutex, 24);
377        }
378        else if(ostrcmp(query, "switch") == 0)
379                buf = webswitch(param, fmt);
380        else if(ostrcmp(query, "getaktservice") == 0)
381        {
382                m_lock(&status.waitrcmutex, 24);
383                buf = webgetaktservice(fmt);
384                m_unlock(&status.waitrcmutex, 24);
385        }
386        else if(ostrcmp(query, "getservice") == 0)
387        {
388                m_lock(&status.waitrcmutex, 24);
389                buf = webgetservice(param, fmt);
390                m_unlock(&status.waitrcmutex, 24);
391        }
392        else if(ostrcmp(query, "getepg") == 0)
393        {
394                m_lock(&status.waitrcmutex, 24);
395                buf = webgetepg(param, fmt);
396                m_unlock(&status.waitrcmutex, 24);
397        }
398        else if(ostrcmp(query, "getmovieepg") == 0)
399        {
400                m_lock(&status.waitrcmutex, 24);
401                buf = webgetmovieepg(param, getconfig("rec_streampath", NULL), 1, fmt);
402                m_unlock(&status.waitrcmutex, 24);
403        }
404        else if(ostrcmp(query, "getsingleepg") == 0)
405        {
406                m_lock(&status.waitrcmutex, 24);
407                buf = webgetsingleepg(param, fmt);
408                m_unlock(&status.waitrcmutex, 24);
409        }
410        else if(ostrcmp(query, "getgmultiepg") == 0)
411        {
412                m_lock(&status.waitrcmutex, 24);
413                buf = webgetgmultiepg(param, fmt);
414                m_unlock(&status.waitrcmutex, 24);
415        }
416        else if(query != NULL && ostrstr(query, "getepgsearch") == query)
417        {
418                m_lock(&status.waitrcmutex, 24);
419                buf = webgetepgsearch(query, param, fmt);
420                m_unlock(&status.waitrcmutex, 24);
421        }
422        else if(ostrcmp(query, "getsignal") == 0)
423                buf = webgetsignal(fmt);
424        else if(ostrcmp(query, "getmoviefilelist") == 0)
425        {
426                if(fmt == 0)
427                        buf = webgetfilelist(param, "getmoviefilelist", "delmoviefile", getconfig("rec_streampath", NULL), "*.avi *.dat *.divx *.flv *.mkv *.m4v *.mp4 *.mov *.mpg *.mpeg *.mts *.m2ts *.trp *.ts *.vdr *.vob *.wmv *.rm", 31, fmt);
428                else
429                        buf = webgetfilelist(param, "getmoviefilelist", "delmoviefile", getconfig("rec_streampath", NULL), "*.avi *.dat *.divx *.flv *.mkv *.m4v *.mp4 *.mov *.mpg *.mpeg *.mts *.m2ts *.trp *.ts *.vdr *.vob *.wmv *.rm *.wav *.mp3", 31, fmt);
430       
431        }
432        else if(ostrcmp(query, "delmoviefile") == 0)
433        {
434                if(fmt == 0)
435                        buf = webdelfile(param, "getmoviefilelist", "delmoviefile", getconfig("rec_streampath", NULL), "*.avi *.dat *.divx *.flv *.mkv *.m4v *.mp4 *.mov *.mpg *.mpeg *.mts *.m2ts *.trp *.ts *.vdr *.vob *.wmv *.rm", 31, fmt);
436                else
437                        buf = webdelfile(param, "getmoviefilelist", "delmoviefile", getconfig("rec_streampath", NULL), "*.avi *.dat *.divx *.flv *.mkv *.m4v *.mp4 *.mov *.mpg *.mpeg *.mts *.m2ts *.trp *.ts *.vdr *.vob *.wmv *.rm *.wav *.mp3", 31, fmt);
438        }
439        else if(ostrcmp(query, "getm3u") == 0)
440        {
441                buf = webgetm3u(param, *connfd, fmt);
442                if(fmt == 0)
443                {
444                        ext = "Content-Disposition: attachment; filename=stream.m3u";
445                        mime = "audio/x-mpegurl";
446                }
447        }
448#ifdef MIPSEL
449        else if(ostrcmp(query, "gettranscodem3u") == 0)
450        {
451                buf = webgettranscodem3u(param, *connfd, fmt);
452                if(fmt == 0)
453                {
454                        ext = "Content-Disposition: attachment; filename=transstream.m3u";
455                        mime = "audio/x-mpegurl";
456                }
457        }
458#endif
459        else if(ostrcmp(query, "getbouquetm3u") == 0)
460        {
461                buf = webgetbouquetm3u(param, *connfd, fmt);
462                if(fmt == 0)
463                {
464                        ext = "Content-Disposition: attachment; filename=bouquetstream.m3u";
465                        mime = "audio/x-mpegurl";
466                }
467        }
468        else if(ostrcmp(query, "getvideo") == 0)
469                buf = webgetvideo(param, *connfd, fmt);
470        else if(ostrcmp(query, "videoplay") == 0 || ostrcmp(query, "videoplay=") == 0)
471                buf = webvideo(param, fmt);
472        else if(ostrcmp(query, "getdrawcount") == 0)
473                buf = webgetdrawcount(param, fmt);
474        else if(ostrcmp(query, "getshoot") == 0)
475        {
476                webgetshoot(param, fmt);
477                if(fmt == 0)
478                {
479                        ext = "Location: shoot.html";
480                        onlyheader = 1;
481                        code = 302;
482                }
483                else
484                        buf = ostrcat("shoot.html", NULL, 0, 0);
485        }
486        else if(query != NULL && ostrstr(query, "poweroff") == query)
487                oshutdown(1, 1);
488        else if(query != NULL && ostrstr(query, "restart") == query)
489                oshutdown(2, 1);
490        else if(query != NULL && ostrstr(query, "guirestart") == query)
491                oshutdown(3, 1);
492        else if(query != NULL && ostrstr(query, "standby") == query)
493        {
494                status.standby = 2;
495                addtimer(&screenstandby, START, 1000, 1, NULL, NULL, NULL);
496        }
497        else if(query != NULL && ostrstr(query, "boxstatus") == query)
498        {
499                if(status.standby > 0)
500                {
501                        if(fmt == 0)
502                                sendoktext(connfd, "standby", auth);
503                        else
504                                buf = ostrcat("standby", NULL, 0, 0);
505                }
506                else
507                {
508                        if(fmt == 0)
509                                sendoktext(connfd, "running", auth);
510                        else
511                                buf = ostrcat("running", NULL, 0, 0);
512                }
513        }
514        else if(query != NULL && ostrstr(query, "mutestatus") == query)
515        {
516                if(status.mute > 0)
517                {
518                        if(fmt == 0)
519                                sendoktext(connfd, "muteon", auth);
520                        else
521                                buf = ostrcat("muteon", NULL, 0, 0);
522                }
523                else
524                {
525                        if(fmt == 0)
526                                sendoktext(connfd, "muteoff", auth);
527                        else
528                                buf = ostrcat("muteoff", NULL, 0, 0);
529                }
530        }
531        else if(query != NULL && ostrstr(query, "message") == query)
532                buf = websendmessage(query, fmt);
533               
534        else if(ostrcmp(query, "startplugin") == 0)
535                buf = webstartplugin(param, fmt);
536               
537        if(buf != NULL || onlyheader == 1)
538        {
539                if(buflen == 0 && onlyheader == 0) buflen = strlen(buf);
540
541                header = createheader(buflen, NULL, mime, ext, code, auth);
542                socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
543                if(onlyheader == 0 && auth == 0)
544                        socksend(connfd, (unsigned char*)buf, buflen, 5000 * 1000);
545        }
546        else
547                senderror(connfd, "query", "Error in query string", auth, fmt);
548
549        free(header);
550        free(buf);
551}
552
553void gotdata(int* connfd)
554{
555        int ret = 0, filefd = -1, auth = 0;
556        unsigned char* buf = NULL;
557        char* tmpstr = NULL, *tmpstr1 = NULL, *filename = NULL, *fullfilename = NULL, *header = NULL, *query = NULL;
558
559        buf = malloc(MINMALLOC);
560        if(buf == NULL)
561        {
562                err("no mem");
563                sockclose(connfd);
564                return;
565        }
566        memset(buf, 0, MINMALLOC);
567
568        debug(250, "get client data");
569
570        //read one line
571        unsigned char* pbuf = buf;
572        while(pbuf - buf < MINMALLOC)
573        {
574                unsigned char c;
575
576                ret = sockreceive(connfd, &c, 1, 5000 * 1000);
577                if(ret != 0)
578                {
579                        debug(250, "no client data in buffer");
580                        break;
581                }
582
583                *pbuf = c;
584                if(buf != NULL && (ostrstr((char*)buf, "\n\n") != NULL || ostrstr((char*)buf, "\r\n\r\n") != NULL))
585                        break;
586                pbuf++;
587        }
588
589        if(buf != NULL)
590        {
591                tmpstr = ostrstr((char*)buf, "GET /");
592                tmpstr1 = ostrstr((char*)buf, "Authorization: Basic ");
593        }
594
595        //Auth Password
596        if(status.httpauth != NULL)
597        {
598                if(tmpstr1 != NULL)
599                {
600                        tmpstr1 += 21;
601                        char* tmpstr3 = malloc(255);
602                        if(tmpstr3 != NULL)
603                        {
604                                int l = b64dec(tmpstr3, tmpstr1);
605                                if(l < 255) tmpstr3[l] = '\0';
606                        }
607                        if(ostrncmp(tmpstr3, status.httpauth, strlen(status.httpauth)) != 0)
608                        {
609                                //not ok:
610                                tmpstr1 = NULL;
611                        }
612                        free(tmpstr3); tmpstr3 = NULL;
613                }
614       
615                if(tmpstr1 == NULL) auth = 1;
616        }
617
618        if(tmpstr != NULL)
619        {
620                tmpstr += 4;
621
622                filename = malloc(MINMALLOC);
623                if(filename == NULL)
624                {
625                        err("no mem");
626                        sockclose(connfd);
627                        free(buf); buf = NULL;
628                        tmpstr = NULL;
629                        return;
630                }
631                memset(filename, 0, MINMALLOC);
632
633                ret = sscanf(tmpstr, "%s", filename);
634                if(ret == 1)
635                {
636                        if(ostrstr(filename, "query?rectimersend") != NULL)
637                                stringreplacechar(filename, '+', ' ');
638                        if(ostrstr(filename, "query?rectimercheck") != NULL)
639                                stringreplacechar(filename, '+', ' ');
640                        htmldecode(filename, filename);
641                       
642                        if(ostrstr(filename, "xmessage") == filename + 1  || ostrstr(filename, "/cgi-bin/xmessage") == filename )
643                        {       
644                                xmessage(filename);
645                                sendoktext(connfd, "ok", 0);
646                                //senderror(connfd, "ok", "ok", 0, 0);
647                                free(buf); buf = NULL;
648                                free(filename); filename = NULL;
649                                tmpstr = NULL;
650                                return;
651                        }
652
653                        //create query
654                        query = strchr(filename, '?');
655                        if(query != NULL)
656                        {
657                                *query++ = '\0';
658                                debug(250, "httpd query=%s", query);
659                        }
660
661                        //queryraw
662                        if(ostrcmp(filename, "/queryraw") == 0 && query != NULL)
663                        {
664                                checkquery(connfd, query, auth, 1);
665                                free(buf); buf = NULL;
666                                free(filename); filename = NULL;
667                                tmpstr = NULL;
668                                return;
669                        }
670
671                        //query
672                        if(ostrcmp(filename, "/query") == 0 && query != NULL)
673                        {
674                                checkquery(connfd, query, auth, 0);
675                                free(buf); buf = NULL;
676                                free(filename); filename = NULL;
677                                tmpstr = NULL;
678                                return;
679                        }
680
681                        //create index.html
682                        if(filename[strlen(filename) - 1] == '/')
683                                filename = ostrcat(filename, "index.html", 1, 0);
684
685                        debug(250, "httpd filename=%s", filename);
686
687                        if(ostrstr(filename, "/movie/") != NULL)
688                                fullfilename = ostrcat(filename, NULL, 0, 0);
689                        else
690                        {
691                                fullfilename = ostrcat(getconfig("httpdpath", NULL), filename, 0, 0);
692
693                                if(ostrstr(filename, ".html") != NULL)
694                                {
695                                        debug(250, "filename: %s", filename);
696                                        debug(250, "fullfilename: %s", fullfilename);
697                                       
698                                        char* tmphtml = readfiletomem(fullfilename, 0);
699                       
700                                        debug(250, "#### tmphtml1 ##################################");
701                                        debug(250, "tmphtml1: %s", buf);
702                                       
703                                        while(ostrstr(tmphtml, "_\(\"") != NULL)
704                                        {
705                                                char* tmpstr1 = string_resub("_(\"", "\")", tmphtml, 0);
706                                                if(tmpstr1 == NULL)
707                                                {
708                                                        debug(250, "Skip  string: %s", tmpstr1);
709                                                        break;
710                                                }
711                                                char* tmpstr2 = ostrcat("_(\"", tmpstr1, 0, 0);
712                                                tmpstr2 = ostrcat(tmpstr2, "\")", 1, 0);
713       
714                                                debug(250, "--------------------------------------");
715                                                debug(250, "Search  string: %s", tmpstr2);
716                                                debug(250, "Replace string: %s", tmpstr1);
717                                                debug(250, "Replace %s -> %s", tmpstr2, tmpstr1);
718                                                debug(250, "--------------------------------------");
719       
720                                                tmphtml = string_replace_all(tmpstr2, _(tmpstr1), tmphtml, 1);
721                                                free(tmpstr1), tmpstr1 = NULL;
722                                                free(tmpstr2), tmpstr2 = NULL;
723                                        }
724                                        debug(250, "#### tmphtml2 ##################################");
725                                        debug(250, "tmphtml2: %s", tmphtml);
726                                       
727                                        free(fullfilename), fullfilename = NULL;
728                                        fullfilename = ostrcat("/tmp/.", filename, 0, 0);
729                                        writesys(fullfilename, tmphtml, 0);
730                                        free(tmphtml), tmphtml = NULL;
731                                }
732                        }
733
734                        filefd = open(fullfilename, O_RDONLY | O_LARGEFILE);
735                        if(filefd < 0)
736                        {
737                                perr("open filename=%s", fullfilename);
738                                senderror(connfd, "Open File", "Can't open File", auth, 0);
739                                free(fullfilename); fullfilename = NULL;
740                                free(buf); buf = NULL;
741                                free(filename); filename = NULL;
742                                tmpstr = NULL;
743                                return;
744                        }
745                       
746                        debug(250, "sende OK response to client");
747                        char* rpath = realpath(fullfilename, NULL);
748                        header = createheader(getfilesize(rpath), fullfilename, NULL, NULL, 200, auth);
749                        free(rpath); rpath = NULL;
750                        free(fullfilename); fullfilename = NULL;
751                        ret = socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
752                        free(header); header = NULL;
753
754                        if(ret != 0)
755                        {
756                                sockclose(connfd);
757                                free(buf); buf = NULL;
758                                free(filename); filename = NULL;
759                                tmpstr = NULL;
760                                return;
761                        }
762
763                        //TODO:
764                        int readret = 1;
765                        while(readret > 0 && auth == 0)
766                        {
767                                readret = dvbreadfd(filefd, buf, 0, MINMALLOC, 1000, 0);
768
769                                if(readret > 0)
770                                        socksend(connfd, buf, readret, 5000 * 1000);
771                        }
772                }
773        }
774
775        close(filefd);
776        free(buf); buf = NULL;
777        free(filename); filename = NULL;
778        tmpstr = NULL;
779}
780
781int newconn(int* streamfd, int* connfd)
782{
783        int i, fd = -1;
784
785        fd = sockaccept(streamfd, 1);
786        if(fd < 0) return -1;
787
788        for(i = 0; i < MAXHTTPDCONN; i++)
789        {
790                if(connfd[i] < 0)
791                {
792                        connfd[i] = fd;
793                        debug(250, "accept httpd connection fd=%d", fd);
794                        fd = -1;
795                        break;
796                }
797        }
798
799        if(fd != -1)
800        {
801                debug(250, "all connections in use");
802                sockclose(&fd);
803                return -1;
804        }
805
806        return 0;
807}
808
809void httpdthreadfunc(struct stimerthread* timernode)
810{
811        struct timeval timeout;
812        fd_set rfds;
813        int i, ret = 0, streamfd = -1, connfd[MAXHTTPDCONN], maxfd = -1;
814
815        if(timernode == NULL) return;
816        debug(250, "Starting httpd thread");
817
818        for(i = 0; i < MAXHTTPDCONN; i++)
819                connfd[i] = -1;
820
821        while(timernode->aktion != STOP)
822        {
823                if(streamfd < 0)
824                {
825                        sockportcreate(&streamfd, getconfigint("httpdport", NULL), MAXHTTPDCONN);
826                        if(streamfd < 0) break;
827                        maxfd = streamfd;
828
829                        //set nonblocking
830                        fcntl(streamfd, F_SETFL, fcntl(streamfd, F_GETFL) | O_NONBLOCK);
831                }
832
833                timeout.tv_sec = 1;
834                timeout.tv_usec = 0;
835
836                FD_ZERO(&rfds);
837                FD_SET(streamfd, &rfds);
838
839                for(i = 0; i < MAXHTTPDCONN; i++)
840                {
841                        if(connfd[i] > -1)
842                        {
843                                FD_SET(connfd[i], &rfds);
844                                if(connfd[i] > maxfd) maxfd = connfd[i];
845                        }
846                }
847
848                ret = TEMP_FAILURE_RETRY(select(maxfd + 1, &rfds , NULL, NULL, &timeout));
849
850                if(ret < 0)
851                {
852                        perr("httpd listen socket fd=%d", maxfd);
853                        sleep(1);
854                        continue;
855                }
856                if(ret == 0) continue; //timeout
857
858                if(FD_ISSET(streamfd, &rfds))
859                {
860                        ret = newconn(&streamfd, connfd);
861                        if(ret < 0) continue;
862                }
863
864                for(i = 0; i < MAXHTTPDCONN; i++)
865                {
866                        if(connfd[i] > -1 && FD_ISSET(connfd[i], &rfds))
867                                gotdata(&connfd[i]);
868                }
869        }
870
871        debug(250, "Stopping httpd thread");
872        sockclose(&streamfd);
873        for(i = 0; i < MAXHTTPDCONN; i++)
874                sockclose(&connfd[i]);
875
876        return;
877}
878
879void sendoktext(int* connfd, char* text, int auth)
880{
881        char* buf = NULL;
882        char* header = NULL;
883        buf = webcreatehead(buf, NULL, 1);
884        buf = ostrcat(buf, "<tr><td align=center valign=top><font class=biglabel><br><br>", 1, 0);
885        buf = ostrcat(buf, text, 1, 0);
886        buf = ostrcat(buf, "</font></td></tr>", 1, 0);
887        buf = webcreatetail(buf, 1);
888        int buflen = strlen(buf);
889        header = createheader(buflen, NULL, NULL, NULL, 200, auth);
890        socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
891        if(auth == 0)
892                socksend(connfd, (unsigned char*)buf, buflen, 5000 * 1000);
893        free(buf); buf=NULL;
894        free(header); header=NULL;
895}
896
897#endif
Note: See TracBrowser for help on using the repository browser.