source: titan/titan/httpd.h @ 34375

Last change on this file since 34375 was 33407, checked in by wonderdoc, 9 years ago

[titan] add WebIf function getrecfreesize

File size: 23.4 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 && param != NULL)
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, "gettpkremovelist") == 0)
323                buf = webgettpkremovelist(fmt);
324        else if(ostrcmp(query, "gettpkupgrade") == 0)
325                buf = webgettpkupgrade(fmt);
326        else if(ostrcmp(query, "getbackup") == 0)
327                buf = webgetbackup(fmt);
328        else if(ostrcmp(query, "getrestore") == 0)
329                buf = webgetrestore(fmt);
330        else if(ostrcmp(query, "getserviceinfo") == 0)
331                buf = webgetserviceinfo(fmt);
332        else if(ostrcmp(query, "getstreaming") == 0 && param != NULL)
333                buf = webgetstreaming(param, fmt);
334        else if(ostrcmp(query, "getstreamingchoices") == 0)
335                buf = webgetstreamingchoices(fmt);             
336        else if(ostrcmp(query, "getnewsletter") == 0 && param != NULL)
337                buf = webgetnewsletter(param, fmt);
338        else if(ostrcmp(query, "getnewsletterchoices") == 0)
339                buf = webgetnewsletterchoices(fmt);
340        else if(ostrcmp(query, "gethelp") == 0 && param != NULL)
341                buf = webgethelp(param, fmt);
342        else if(ostrcmp(query, "getupdatelist") == 0 && param != NULL)
343                buf = webgetupdatelist(param, fmt);
344        else if(ostrcmp(query, "getupdate") == 0 && param != NULL)
345                buf = webgetupdate(param, fmt);
346        else if(ostrcmp(query, "getsatchannel") == 0 && param != NULL)
347        {
348                m_lock(&status.waitrcmutex, 24);
349                buf = webgetchannel(atoi(param), 1, 1, fmt);
350                m_unlock(&status.waitrcmutex, 24);
351        }
352        else if(ostrcmp(query, "getproviderchannel") == 0 && param != NULL)
353        {
354                m_lock(&status.waitrcmutex, 24);
355                buf = webgetchannel(atoi(param), 2, 1, fmt);
356                m_unlock(&status.waitrcmutex, 24);
357        }
358        else if(ostrcmp(query, "getazchannel") == 0 && param != NULL)
359        {
360                m_lock(&status.waitrcmutex, 24);
361                buf = webgetchannel(atoi(param), 3, 1, fmt);
362                m_unlock(&status.waitrcmutex, 24);
363        }
364        else if(ostrcmp(query, "switch") == 0)
365                buf = webswitch(param, fmt);
366        else if(ostrcmp(query, "getaktservice") == 0)
367        {
368                m_lock(&status.waitrcmutex, 24);
369                buf = webgetaktservice(fmt);
370                m_unlock(&status.waitrcmutex, 24);
371        }
372        else if(ostrcmp(query, "getservice") == 0)
373        {
374                m_lock(&status.waitrcmutex, 24);
375                buf = webgetservice(param, fmt);
376                m_unlock(&status.waitrcmutex, 24);
377        }
378        else if(ostrcmp(query, "getepg") == 0)
379        {
380                m_lock(&status.waitrcmutex, 24);
381                buf = webgetepg(param, fmt);
382                m_unlock(&status.waitrcmutex, 24);
383        }
384        else if(ostrcmp(query, "getmovieepg") == 0)
385        {
386                m_lock(&status.waitrcmutex, 24);
387                buf = webgetmovieepg(param, getconfig("rec_streampath", NULL), 1, fmt);
388                m_unlock(&status.waitrcmutex, 24);
389        }
390        else if(ostrcmp(query, "getsingleepg") == 0)
391        {
392                m_lock(&status.waitrcmutex, 24);
393                buf = webgetsingleepg(param, fmt);
394                m_unlock(&status.waitrcmutex, 24);
395        }
396        else if(ostrcmp(query, "getgmultiepg") == 0)
397        {
398                m_lock(&status.waitrcmutex, 24);
399                buf = webgetgmultiepg(param, fmt);
400                m_unlock(&status.waitrcmutex, 24);
401        }
402        else if(query != NULL && ostrstr(query, "getepgsearch") == query)
403        {
404                m_lock(&status.waitrcmutex, 24);
405                buf = webgetepgsearch(query, param, fmt);
406                m_unlock(&status.waitrcmutex, 24);
407        }
408        else if(ostrcmp(query, "getsignal") == 0)
409                buf = webgetsignal(fmt);
410        else if(ostrcmp(query, "getmoviefilelist") == 0)
411        {
412                if(fmt == 0)
413                        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);
414                else
415                        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);
416       
417        }
418        else if(ostrcmp(query, "delmoviefile") == 0)
419        {
420                if(fmt == 0)
421                        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);
422                else
423                        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);
424        }
425        else if(ostrcmp(query, "getm3u") == 0)
426        {
427                buf = webgetm3u(param, *connfd, fmt);
428                if(fmt == 0)
429                {
430                        ext = "Content-Disposition: attachment; filename=stream.m3u";
431                        mime = "audio/x-mpegurl";
432                }
433        }
434#ifdef MIPSEL
435        else if(ostrcmp(query, "gettranscodem3u") == 0)
436        {
437                buf = webgettranscodem3u(param, *connfd, fmt);
438                if(fmt == 0)
439                {
440                        ext = "Content-Disposition: attachment; filename=transstream.m3u";
441                        mime = "audio/x-mpegurl";
442                }
443        }
444#endif
445        else if(ostrcmp(query, "getbouquetm3u") == 0)
446        {
447                buf = webgetbouquetm3u(param, *connfd, fmt);
448                if(fmt == 0)
449                {
450                        ext = "Content-Disposition: attachment; filename=bouquetstream.m3u";
451                        mime = "audio/x-mpegurl";
452                }
453        }
454        else if(ostrcmp(query, "getvideo") == 0)
455                buf = webgetvideo(param, *connfd, fmt);
456        else if(ostrcmp(query, "videoplay") == 0 || ostrcmp(query, "videoplay=") == 0)
457                buf = webvideo(param, fmt);
458        else if(ostrcmp(query, "getdrawcount") == 0)
459                buf = webgetdrawcount(param, fmt);
460        else if(ostrcmp(query, "getshoot") == 0)
461        {
462                webgetshoot(param, fmt);
463                if(fmt == 0)
464                {
465                        ext = "Location: shoot.html";
466                        onlyheader = 1;
467                        code = 302;
468                }
469                else
470                        buf = ostrcat("shoot.html", NULL, 0, 0);
471        }
472        else if(query != NULL && ostrstr(query, "poweroff") == query)
473                oshutdown(1, 1);
474        else if(query != NULL && ostrstr(query, "restart") == query)
475                oshutdown(2, 1);
476        else if(query != NULL && ostrstr(query, "guirestart") == query)
477                oshutdown(3, 1);
478        else if(query != NULL && ostrstr(query, "standby") == query)
479        {
480                status.standby = 2;
481                addtimer(&screenstandby, START, 1000, 1, NULL, NULL, NULL);
482        }
483        else if(query != NULL && ostrstr(query, "boxstatus") == query)
484        {
485                if(status.standby > 0)
486                {
487                        if(fmt == 0)
488                                sendoktext(connfd, "standby", auth);
489                        else
490                                buf = ostrcat("standby", NULL, 0, 0);
491                }
492                else
493                {
494                        if(fmt == 0)
495                                sendoktext(connfd, "running", auth);
496                        else
497                                buf = ostrcat("running", NULL, 0, 0);
498                }
499        }
500        else if(query != NULL && ostrstr(query, "mutestatus") == query)
501        {
502                if(status.mute > 0)
503                {
504                        if(fmt == 0)
505                                sendoktext(connfd, "muteon", auth);
506                        else
507                                buf = ostrcat("muteon", NULL, 0, 0);
508                }
509                else
510                {
511                        if(fmt == 0)
512                                sendoktext(connfd, "muteoff", auth);
513                        else
514                                buf = ostrcat("muteoff", NULL, 0, 0);
515                }
516        }
517        else if(query != NULL && ostrstr(query, "message") == query)
518                buf = websendmessage(query, fmt);
519               
520        else if(ostrcmp(query, "startplugin") == 0)
521                buf = webstartplugin(param, fmt);
522               
523        if(buf != NULL || onlyheader == 1)
524        {
525                if(buflen == 0 && onlyheader == 0) buflen = strlen(buf);
526
527                header = createheader(buflen, NULL, mime, ext, code, auth);
528                socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
529                if(onlyheader == 0 && auth == 0)
530                        socksend(connfd, (unsigned char*)buf, buflen, 5000 * 1000);
531        }
532        else
533                senderror(connfd, "query", "Error in query string", auth, fmt);
534
535        free(header);
536        free(buf);
537}
538
539void gotdata(int* connfd)
540{
541        int ret = 0, filefd = -1, auth = 0;
542        unsigned char* buf = NULL;
543        char* tmpstr = NULL, *tmpstr1 = NULL, *filename = NULL, *fullfilename = NULL, *header = NULL, *query = NULL;
544
545        buf = malloc(MINMALLOC);
546        if(buf == NULL)
547        {
548                err("no mem");
549                sockclose(connfd);
550                return;
551        }
552        memset(buf, 0, MINMALLOC);
553
554        debug(250, "get client data");
555
556        //read one line
557        unsigned char* pbuf = buf;
558        while(pbuf - buf < MINMALLOC)
559        {
560                unsigned char c;
561
562                ret = sockreceive(connfd, &c, 1, 5000 * 1000);
563                if(ret != 0)
564                {
565                        debug(250, "no client data in buffer");
566                        break;
567                }
568
569                *pbuf = c;
570                if(buf != NULL && (ostrstr((char*)buf, "\n\n") != NULL || ostrstr((char*)buf, "\r\n\r\n") != NULL))
571                        break;
572                pbuf++;
573        }
574
575        if(buf != NULL)
576        {
577                tmpstr = ostrstr((char*)buf, "GET /");
578                tmpstr1 = ostrstr((char*)buf, "Authorization: Basic ");
579        }
580
581        //Auth Password
582        if(status.httpauth != NULL)
583        {
584                if(tmpstr1 != NULL)
585                {
586                        tmpstr1 += 21;
587                        char* tmpstr3 = malloc(255);
588                        if(tmpstr3 != NULL)
589                        {
590                                int l = b64dec(tmpstr3, tmpstr1);
591                                if(l < 255) tmpstr3[l] = '\0';
592                        }
593                        if(ostrncmp(tmpstr3, status.httpauth, strlen(status.httpauth)) != 0)
594                        {
595                                //not ok:
596                                tmpstr1 = NULL;
597                        }
598                        free(tmpstr3); tmpstr3 = NULL;
599                }
600       
601                if(tmpstr1 == NULL) auth = 1;
602        }
603
604        if(tmpstr != NULL)
605        {
606                tmpstr += 4;
607
608                filename = malloc(MINMALLOC);
609                if(filename == NULL)
610                {
611                        err("no mem");
612                        sockclose(connfd);
613                        free(buf); buf = NULL;
614                        tmpstr = NULL;
615                        return;
616                }
617                memset(filename, 0, MINMALLOC);
618
619                ret = sscanf(tmpstr, "%s", filename);
620                if(ret == 1)
621                {
622                        if(ostrstr(filename, "query?rectimersend") != NULL)
623                                stringreplacechar(filename, '+', ' ');
624                        if(ostrstr(filename, "query?rectimercheck") != NULL)
625                                stringreplacechar(filename, '+', ' ');
626                        htmldecode(filename, filename);
627                       
628                        if(ostrstr(filename, "xmessage") == filename + 1  || ostrstr(filename, "/cgi-bin/xmessage") == filename )
629                        {       
630                                xmessage(filename);
631                                sendoktext(connfd, "ok", 0);
632                                //senderror(connfd, "ok", "ok", 0, 0);
633                                free(buf); buf = NULL;
634                                free(filename); filename = NULL;
635                                tmpstr = NULL;
636                                return;
637                        }
638
639                        //create query
640                        query = strchr(filename, '?');
641                        if(query != NULL)
642                        {
643                                *query++ = '\0';
644                                debug(250, "httpd query=%s", query);
645                        }
646
647                        //queryraw
648                        if(ostrcmp(filename, "/queryraw") == 0 && query != NULL)
649                        {
650                                checkquery(connfd, query, auth, 1);
651                                free(buf); buf = NULL;
652                                free(filename); filename = NULL;
653                                tmpstr = NULL;
654                                return;
655                        }
656
657                        //query
658                        if(ostrcmp(filename, "/query") == 0 && query != NULL)
659                        {
660                                checkquery(connfd, query, auth, 0);
661                                free(buf); buf = NULL;
662                                free(filename); filename = NULL;
663                                tmpstr = NULL;
664                                return;
665                        }
666
667                        //create index.html
668                        if(filename[strlen(filename) - 1] == '/')
669                                filename = ostrcat(filename, "index.html", 1, 0);
670
671                        debug(250, "httpd filename=%s", filename);
672
673                        if(ostrstr(filename, "/movie/") != NULL)
674                                fullfilename = ostrcat(filename, NULL, 0, 0);
675                        else
676                        {
677                                fullfilename = ostrcat(getconfig("httpdpath", NULL), filename, 0, 0);
678
679                                if(ostrstr(filename, ".html") != NULL)
680                                {
681                                        debug(250, "filename: %s", filename);
682                                        debug(250, "fullfilename: %s", fullfilename);
683                                       
684                                        char* tmphtml = readfiletomem(fullfilename, 0);
685                       
686                                        debug(250, "#### tmphtml1 ##################################");
687                                        debug(250, "tmphtml1: %s", buf);
688                                       
689                                        while(ostrstr(tmphtml, "_\(\"") != NULL)
690                                        {
691                                                char* tmpstr1 = string_resub("_(\"", "\")", tmphtml, 0);
692                                                if(tmpstr1 == NULL)
693                                                {
694                                                        debug(250, "Skip  string: %s", tmpstr1);
695                                                        break;
696                                                }
697                                                char* tmpstr2 = ostrcat("_(\"", tmpstr1, 0, 0);
698                                                tmpstr2 = ostrcat(tmpstr2, "\")", 1, 0);
699       
700                                                debug(250, "--------------------------------------");
701                                                debug(250, "Search  string: %s", tmpstr2);
702                                                debug(250, "Replace string: %s", tmpstr1);
703                                                debug(250, "Replace %s -> %s", tmpstr2, tmpstr1);
704                                                debug(250, "--------------------------------------");
705       
706                                                tmphtml = string_replace_all(tmpstr2, _(tmpstr1), tmphtml, 1);
707                                                free(tmpstr1), tmpstr1 = NULL;
708                                                free(tmpstr2), tmpstr2 = NULL;
709                                        }
710                                        debug(250, "#### tmphtml2 ##################################");
711                                        debug(250, "tmphtml2: %s", tmphtml);
712                                       
713                                        free(fullfilename), fullfilename = NULL;
714                                        fullfilename = ostrcat("/tmp/.", filename, 0, 0);
715                                        writesys(fullfilename, tmphtml, 0);
716                                        free(tmphtml), tmphtml = NULL;
717                                }
718                        }
719
720                        filefd = open(fullfilename, O_RDONLY | O_LARGEFILE);
721                        if(filefd < 0)
722                        {
723                                perr("open filename=%s", fullfilename);
724                                senderror(connfd, "Open File", "Can't open File", auth, 0);
725                                free(fullfilename); fullfilename = NULL;
726                                free(buf); buf = NULL;
727                                free(filename); filename = NULL;
728                                tmpstr = NULL;
729                                return;
730                        }
731                       
732                        debug(250, "sende OK response to client");
733                        char* rpath = realpath(fullfilename, NULL);
734                        header = createheader(getfilesize(rpath), fullfilename, NULL, NULL, 200, auth);
735                        free(rpath); rpath = NULL;
736                        free(fullfilename); fullfilename = NULL;
737                        ret = socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
738                        free(header); header = NULL;
739
740                        if(ret != 0)
741                        {
742                                sockclose(connfd);
743                                free(buf); buf = NULL;
744                                free(filename); filename = NULL;
745                                tmpstr = NULL;
746                                return;
747                        }
748
749                        //TODO:
750                        int readret = 1;
751                        while(readret > 0 && auth == 0)
752                        {
753                                readret = dvbreadfd(filefd, buf, 0, MINMALLOC, 1000, 0);
754
755                                if(readret > 0)
756                                        socksend(connfd, buf, readret, 5000 * 1000);
757                        }
758                }
759        }
760
761        close(filefd);
762        free(buf); buf = NULL;
763        free(filename); filename = NULL;
764        tmpstr = NULL;
765}
766
767int newconn(int* streamfd, int* connfd)
768{
769        int i, fd = -1;
770
771        fd = sockaccept(streamfd, 1);
772        if(fd < 0) return -1;
773
774        for(i = 0; i < MAXHTTPDCONN; i++)
775        {
776                if(connfd[i] < 0)
777                {
778                        connfd[i] = fd;
779                        debug(250, "accept httpd connection fd=%d", fd);
780                        fd = -1;
781                        break;
782                }
783        }
784
785        if(fd != -1)
786        {
787                debug(250, "all connections in use");
788                sockclose(&fd);
789                return -1;
790        }
791
792        return 0;
793}
794
795void httpdthreadfunc(struct stimerthread* timernode)
796{
797        struct timeval timeout;
798        fd_set rfds;
799        int i, ret = 0, streamfd = -1, connfd[MAXHTTPDCONN], maxfd = -1;
800
801        if(timernode == NULL) return;
802        debug(250, "Starting httpd thread");
803
804        for(i = 0; i < MAXHTTPDCONN; i++)
805                connfd[i] = -1;
806
807        while(timernode->aktion != STOP)
808        {
809                if(streamfd < 0)
810                {
811                        sockportcreate(&streamfd, getconfigint("httpdport", NULL), MAXHTTPDCONN);
812                        if(streamfd < 0) break;
813                        maxfd = streamfd;
814
815                        //set nonblocking
816                        fcntl(streamfd, F_SETFL, fcntl(streamfd, F_GETFL) | O_NONBLOCK);
817                }
818
819                timeout.tv_sec = 1;
820                timeout.tv_usec = 0;
821
822                FD_ZERO(&rfds);
823                FD_SET(streamfd, &rfds);
824
825                for(i = 0; i < MAXHTTPDCONN; i++)
826                {
827                        if(connfd[i] > -1)
828                        {
829                                FD_SET(connfd[i], &rfds);
830                                if(connfd[i] > maxfd) maxfd = connfd[i];
831                        }
832                }
833
834                ret = TEMP_FAILURE_RETRY(select(maxfd + 1, &rfds , NULL, NULL, &timeout));
835
836                if(ret < 0)
837                {
838                        perr("httpd listen socket fd=%d", maxfd);
839                        sleep(1);
840                        continue;
841                }
842                if(ret == 0) continue; //timeout
843
844                if(FD_ISSET(streamfd, &rfds))
845                {
846                        ret = newconn(&streamfd, connfd);
847                        if(ret < 0) continue;
848                }
849
850                for(i = 0; i < MAXHTTPDCONN; i++)
851                {
852                        if(connfd[i] > -1 && FD_ISSET(connfd[i], &rfds))
853                                gotdata(&connfd[i]);
854                }
855        }
856
857        debug(250, "Stopping httpd thread");
858        sockclose(&streamfd);
859        for(i = 0; i < MAXHTTPDCONN; i++)
860                sockclose(&connfd[i]);
861
862        return;
863}
864
865void sendoktext(int* connfd, char* text, int auth)
866{
867        char* buf = NULL;
868        char* header = NULL;
869        buf = webcreatehead(buf, NULL, 1);
870        buf = ostrcat(buf, "<tr><td align=center valign=top><font class=biglabel><br><br>", 1, 0);
871        buf = ostrcat(buf, text, 1, 0);
872        buf = ostrcat(buf, "</font></td></tr>", 1, 0);
873        buf = webcreatetail(buf, 1);
874        int buflen = strlen(buf);
875        header = createheader(buflen, NULL, NULL, NULL, 200, auth);
876        socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
877        if(auth == 0)
878                socksend(connfd, (unsigned char*)buf, buflen, 5000 * 1000);
879        free(buf); buf=NULL;
880        free(header); header=NULL;
881}
882
883#endif
Note: See TracBrowser for help on using the repository browser.