source: titan/titan/httpd.h @ 30195

Last change on this file since 30195 was 30195, checked in by obi, 10 years ago

[webif] newsletter update

File size: 21.5 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, "getrccodes") == 0)
173                buf = webgetrccodes(fmt);
174        else if(ostrcmp(query, "getmute") == 0)
175                buf = webgetmute(fmt);
176        else if(ostrcmp(query, "getrecsteampath") == 0)
177                buf = webgetrecsteampath(fmt);
178        else if(ostrcmp(query, "getvol") == 0)
179                buf = webgetvol(fmt);
180        else if(ostrcmp(query, "sendrc") == 0)
181                buf = websendrc(param, fmt);
182        else if(ostrcmp(query, "getrectimer") == 0)
183        {
184                m_lock(&status.waitrcmutex, 24);
185                buf = webgetrectimer(param, 0, fmt);
186                m_unlock(&status.waitrcmutex, 24);
187        }
188        else if(ostrcmp(query, "addrectimer") == 0)
189        {
190                m_lock(&status.waitrcmutex, 24);
191                buf = webaddrectimer(param, fmt);
192                m_unlock(&status.waitrcmutex, 24);
193        }
194        else if(ostrcmp(query, "delrectimer") == 0)
195        {
196                m_lock(&status.waitrcmutex, 24);
197                buf = webdelrectimer(param, fmt);
198                m_unlock(&status.waitrcmutex, 24);
199        }
200        else if(ostrcmp(query, "editrectimer") == 0)
201        {
202                m_lock(&status.waitrcmutex, 24);
203                buf = webeditrectimer(param, fmt);
204                m_unlock(&status.waitrcmutex, 24);
205        }
206        else if(ostrcmp(query, "rectimercheck") == 0)
207        {
208                m_lock(&status.waitrcmutex, 24);
209                buf = webrectimercheck(param, fmt);
210                m_unlock(&status.waitrcmutex, 24);
211        }
212        else if(ostrcmp(query, "rectimersend") == 0)
213        {
214                m_lock(&status.waitrcmutex, 24);
215                buf = webrectimersend(param, fmt);
216                m_unlock(&status.waitrcmutex, 24);
217        }
218        else if(ostrcmp(query, "getrectimerarchive") == 0)
219        {
220                m_lock(&status.waitrcmutex, 24);
221                buf = webgetrectimer(param, 1, fmt);
222                m_unlock(&status.waitrcmutex, 24);
223        }
224        else if(ostrcmp(query, "adjust") == 0)
225        {
226                buf = webadjust(param, fmt);
227        }
228        else if(ostrcmp(query, "adjustsend") == 0)
229        {
230                buf = webadjustsend(param, fmt);
231        }
232        else if(ostrcmp(query, "setvol") == 0)
233                buf = websetvol(param, fmt);
234        else if(ostrcmp(query, "setmute") == 0)
235                buf = websetmute(param, fmt);
236        else if(ostrcmp(query, "getbouquet") == 0)
237        {
238                m_lock(&status.waitrcmutex, 24);
239                buf = webgetbouquet(fmt);
240                m_unlock(&status.waitrcmutex, 24);
241        }
242        else if(ostrcmp(query, "getsat") == 0)
243        {
244                m_lock(&status.waitrcmutex, 24);
245                buf = webgetsat(fmt);
246                m_unlock(&status.waitrcmutex, 24);
247        }
248        else if(ostrcmp(query, "getprovider") == 0)
249        {
250                m_lock(&status.waitrcmutex, 24);
251                buf = webgetprovider(fmt);
252                m_unlock(&status.waitrcmutex, 24);
253        }
254        else if(ostrcmp(query, "getaz") == 0)
255                buf = webgetaz(fmt);
256        else if(ostrcmp(query, "getconfig") == 0)
257                buf = webgetconfig(fmt);
258        else if(ostrcmp(query, "getchannelpage") == 0)
259        {
260                m_lock(&status.waitrcmutex, 24);
261                buf = webgetchannelpage(param, fmt);
262                m_unlock(&status.waitrcmutex, 24);
263        }
264        else if(ostrcmp(query, "getallchannel") == 0)
265        {
266                m_lock(&status.waitrcmutex, 24);
267                buf = webgetchannel(0, 0, 1, fmt);
268                m_unlock(&status.waitrcmutex, 24);
269        }
270        else if(ostrcmp(query, "getbouquetchannel") == 0)
271        {
272                m_lock(&status.waitrcmutex, 24);
273                buf = webgetbouquetchannel(param, fmt);
274                m_unlock(&status.waitrcmutex, 24);
275        }
276        else if(ostrcmp(query, "getcommand") == 0 && param != NULL)
277        {
278                m_lock(&status.waitrcmutex, 24);
279                buf = webgetcommand(param, fmt);
280                m_unlock(&status.waitrcmutex, 24);
281        }
282        else if(ostrcmp(query, "gethelpchoices") == 0)
283        {
284                m_lock(&status.waitrcmutex, 24);
285                buf = webgethelpchoices(fmt);
286                m_unlock(&status.waitrcmutex, 24);
287        }
288        else if(ostrcmp(query, "gettestpage") == 0 && param != NULL)
289        {
290                m_lock(&status.waitrcmutex, 24);
291                buf = webgettestpage(param, fmt);
292                m_unlock(&status.waitrcmutex, 24);
293        }
294        else if(ostrcmp(query, "getsysteminfos") == 0 && param != NULL)
295                buf = webgetsysteminfos(param, fmt);
296        else if(ostrcmp(query, "getsysinfos") == 0 && param != NULL)
297                buf = webgetsysinfos(param, fmt);
298        else if(ostrcmp(query, "getlogs") == 0 && param != NULL)
299                buf = webgetlogs(param, fmt);
300        else if(ostrcmp(query, "getabout") == 0)
301                buf = webgetabout(fmt);
302        else if(ostrcmp(query, "getserviceinfo") == 0)
303                buf = webgetserviceinfo(fmt);
304        else if(ostrcmp(query, "getstreaming") == 0)
305                buf = webgetstreaming(fmt);
306        else if(ostrcmp(query, "getnewsletter") == 0)
307                buf = webgetnewsletter(fmt);
308        else if(ostrcmp(query, "getnewsletterchoices") == 0 && param != NULL)
309                buf = webgetnewsletterchoices(param, fmt);
310        else if(ostrcmp(query, "gethelp") == 0 && param != NULL)
311        {
312                m_lock(&status.waitrcmutex, 24);
313                buf = webgethelp(param, fmt);
314                m_unlock(&status.waitrcmutex, 24);
315        }
316        else if(ostrcmp(query, "getsatchannel") == 0 && param != NULL)
317        {
318                m_lock(&status.waitrcmutex, 24);
319                buf = webgetchannel(atoi(param), 1, 1, fmt);
320                m_unlock(&status.waitrcmutex, 24);
321        }
322        else if(ostrcmp(query, "getproviderchannel") == 0 && param != NULL)
323        {
324                m_lock(&status.waitrcmutex, 24);
325                buf = webgetchannel(atoi(param), 2, 1, fmt);
326                m_unlock(&status.waitrcmutex, 24);
327        }
328        else if(ostrcmp(query, "getazchannel") == 0 && param != NULL)
329        {
330                m_lock(&status.waitrcmutex, 24);
331                buf = webgetchannel(atoi(param), 3, 1, fmt);
332                m_unlock(&status.waitrcmutex, 24);
333        }
334        else if(ostrcmp(query, "switch") == 0)
335                buf = webswitch(param, fmt);
336        else if(ostrcmp(query, "getaktservice") == 0)
337        {
338                m_lock(&status.waitrcmutex, 24);
339                buf = webgetaktservice(fmt);
340                m_unlock(&status.waitrcmutex, 24);
341        }
342        else if(ostrcmp(query, "getservice") == 0)
343        {
344                m_lock(&status.waitrcmutex, 24);
345                buf = webgetservice(param, fmt);
346                m_unlock(&status.waitrcmutex, 24);
347        }
348        else if(ostrcmp(query, "getepg") == 0)
349        {
350                m_lock(&status.waitrcmutex, 24);
351                buf = webgetepg(param, fmt);
352                m_unlock(&status.waitrcmutex, 24);
353        }
354        else if(ostrcmp(query, "getmovieepg") == 0)
355        {
356                m_lock(&status.waitrcmutex, 24);
357                buf = webgetmovieepg(param, getconfig("rec_streampath", NULL), 1, fmt);
358                m_unlock(&status.waitrcmutex, 24);
359        }
360        else if(ostrcmp(query, "getsingleepg") == 0)
361        {
362                m_lock(&status.waitrcmutex, 24);
363                buf = webgetsingleepg(param, fmt);
364                m_unlock(&status.waitrcmutex, 24);
365        }
366        else if(ostrcmp(query, "getgmultiepg") == 0)
367        {
368                m_lock(&status.waitrcmutex, 24);
369                buf = webgetgmultiepg(param, fmt);
370                m_unlock(&status.waitrcmutex, 24);
371        }
372        else if(query != NULL && ostrstr(query, "getepgsearch") == query)
373        {
374                m_lock(&status.waitrcmutex, 24);
375                buf = webgetepgsearch(query, param, fmt);
376                m_unlock(&status.waitrcmutex, 24);
377        }
378        else if(ostrcmp(query, "getsignal") == 0)
379                buf = webgetsignal(fmt);
380        else if(ostrcmp(query, "getmoviefilelist") == 0)
381        {
382                if(fmt == 0)
383                        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);
384                else
385                        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);
386       
387        }
388        else if(ostrcmp(query, "delmoviefile") == 0)
389        {
390                if(fmt == 0)
391                        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);
392                else
393                        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);
394        }
395        else if(ostrcmp(query, "getm3u") == 0)
396        {
397                buf = webgetm3u(param, *connfd, fmt);
398                if(fmt == 0)
399                {
400                        ext = "Content-Disposition: attachment; filename=stream.m3u";
401                        mime = "audio/x-mpegurl";
402                }
403        }
404        else if(ostrcmp(query, "getvideo") == 0)
405                buf = webgetvideo(param, *connfd, fmt);
406        else if(ostrcmp(query, "videoplay") == 0 || ostrcmp(query, "videoplay=") == 0)
407                buf = webvideo(param, fmt);
408        else if(ostrcmp(query, "getdrawcount") == 0)
409                buf = webgetdrawcount(param, fmt);
410        else if(ostrcmp(query, "getshoot") == 0)
411        {
412                webgetshoot(param, fmt);
413                if(fmt == 0)
414                {
415                        ext = "Location: shoot.html";
416                        onlyheader = 1;
417                        code = 302;
418                }
419                else
420                        buf = ostrcat("shoot.html", NULL, 0, 0);
421        }
422        else if(query != NULL && ostrstr(query, "poweroff") == query)
423                oshutdown(1, 1);
424        else if(query != NULL && ostrstr(query, "restart") == query)
425                oshutdown(2, 1);
426        else if(query != NULL && ostrstr(query, "guirestart") == query)
427                oshutdown(3, 1);
428        else if(query != NULL && ostrstr(query, "standby") == query)
429        {
430                status.standby = 2;
431                addtimer(&screenstandby, START, 1000, 1, NULL, NULL, NULL);
432        }
433        else if(query != NULL && ostrstr(query, "boxstatus") == query)
434        {
435                if(status.standby > 0)
436                {
437                        if(fmt == 0)
438                                sendoktext(connfd, "standby", auth);
439                        else
440                                buf = ostrcat("standby", NULL, 0, 0);
441                }
442                else
443                {
444                        if(fmt == 0)
445                                sendoktext(connfd, "running", auth);
446                        else
447                                buf = ostrcat("running", NULL, 0, 0);
448                }
449        }
450        else if(query != NULL && ostrstr(query, "mutestatus") == query)
451        {
452                if(status.mute > 0)
453                {
454                        if(fmt == 0)
455                                sendoktext(connfd, "muteon", auth);
456                        else
457                                buf = ostrcat("muteon", NULL, 0, 0);
458                }
459                else
460                {
461                        if(fmt == 0)
462                                sendoktext(connfd, "muteoff", auth);
463                        else
464                                buf = ostrcat("muteoff", NULL, 0, 0);
465                }
466        }
467        else if(query != NULL && ostrstr(query, "message") == query)
468                buf = websendmessage(query, fmt);
469               
470        if(buf != NULL || onlyheader == 1)
471        {
472                if(buflen == 0 && onlyheader == 0) buflen = strlen(buf);
473
474                header = createheader(buflen, NULL, mime, ext, code, auth);
475                socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
476                if(onlyheader == 0 && auth == 0)
477                        socksend(connfd, (unsigned char*)buf, buflen, 5000 * 1000);
478        }
479        else
480                senderror(connfd, "query", "Error in query string", auth, fmt);
481
482        free(header);
483        free(buf);
484}
485
486void gotdata(int* connfd)
487{
488        int ret = 0, filefd = -1, auth = 0;
489        unsigned char* buf = NULL;
490        char* tmpstr = NULL, *tmpstr1 = NULL, *filename = NULL, *fullfilename = NULL, *header = NULL, *query = NULL;
491
492        buf = malloc(MINMALLOC);
493        if(buf == NULL)
494        {
495                err("no mem");
496                sockclose(connfd);
497                return;
498        }
499        memset(buf, 0, MINMALLOC);
500
501        debug(250, "get client data");
502
503        //read one line
504        unsigned char* pbuf = buf;
505        while(pbuf - buf < MINMALLOC)
506        {
507                unsigned char c;
508
509                ret = sockreceive(connfd, &c, 1, 5000 * 1000);
510                if(ret != 0)
511                {
512                        debug(250, "no client data in buffer");
513                        break;
514                }
515
516                *pbuf = c;
517                if(buf != NULL && (ostrstr((char*)buf, "\n\n") != NULL || ostrstr((char*)buf, "\r\n\r\n") != NULL))
518                        break;
519                pbuf++;
520        }
521
522        if(buf != NULL)
523        {
524                tmpstr = ostrstr((char*)buf, "GET /");
525                tmpstr1 = ostrstr((char*)buf, "Authorization: Basic ");
526        }
527
528        //Auth Password
529        if(status.httpauth != NULL)
530        {
531                if(tmpstr1 != NULL)
532                {
533                        tmpstr1 += 21;
534                        char* tmpstr3 = malloc(255);
535                        if(tmpstr3 != NULL)
536                        {
537                                int l = b64dec(tmpstr3, tmpstr1);
538                                if(l < 255) tmpstr3[l] = '\0';
539                        }
540                        if(ostrncmp(tmpstr3, status.httpauth, strlen(status.httpauth)) != 0)
541                        {
542                                //not ok:
543                                tmpstr1 = NULL;
544                        }
545                        free(tmpstr3); tmpstr3 = NULL;
546                }
547       
548                if(tmpstr1 == NULL) auth = 1;
549        }
550
551        if(tmpstr != NULL)
552        {
553                tmpstr += 4;
554
555                filename = malloc(MINMALLOC);
556                if(filename == NULL)
557                {
558                        err("no mem");
559                        sockclose(connfd);
560                        free(buf); buf = NULL;
561                        tmpstr = NULL;
562                        return;
563                }
564                memset(filename, 0, MINMALLOC);
565
566                ret = sscanf(tmpstr, "%s", filename);
567                if(ret == 1)
568                {
569                        if(ostrstr(filename, "query?rectimersend") != NULL)
570                                stringreplacechar(filename, '+', ' ');
571                        if(ostrstr(filename, "query?rectimercheck") != NULL)
572                                stringreplacechar(filename, '+', ' ');
573                        htmldecode(filename, filename);
574                       
575                        if(ostrstr(filename, "xmessage") == filename + 1  || ostrstr(filename, "/cgi-bin/xmessage") == filename )
576                        {       
577                                xmessage(filename);
578                                sendoktext(connfd, "ok", 0);
579                                //senderror(connfd, "ok", "ok", 0, 0);
580                                free(buf); buf = NULL;
581                                free(filename); filename = NULL;
582                                tmpstr = NULL;
583                                return;
584                        }
585
586                        //create query
587                        query = strchr(filename, '?');
588                        if(query != NULL)
589                        {
590                                *query++ = '\0';
591                                debug(250, "httpd query=%s", query);
592                        }
593
594                        //queryraw
595                        if(ostrcmp(filename, "/queryraw") == 0 && query != NULL)
596                        {
597                                checkquery(connfd, query, auth, 1);
598                                free(buf); buf = NULL;
599                                free(filename); filename = NULL;
600                                tmpstr = NULL;
601                                return;
602                        }
603
604                        //query
605                        if(ostrcmp(filename, "/query") == 0 && query != NULL)
606                        {
607                                checkquery(connfd, query, auth, 0);
608                                free(buf); buf = NULL;
609                                free(filename); filename = NULL;
610                                tmpstr = NULL;
611                                return;
612                        }
613
614                        //create index.html
615                        if(filename[strlen(filename) - 1] == '/')
616                                filename = ostrcat(filename, "index.html", 1, 0);
617
618                        debug(250, "httpd filename=%s", filename);
619
620                        if(ostrstr(filename, "/movie/") != NULL)
621                                fullfilename = ostrcat(filename, NULL, 0, 0);
622                        else
623                        {
624                                fullfilename = ostrcat(getconfig("httpdpath", NULL), filename, 0, 0);
625
626                                if(ostrstr(filename, ".html") != NULL)
627                                {
628                                        debug(250, "filename: %s", filename);
629                                        debug(250, "fullfilename: %s", fullfilename);
630                                       
631                                        char* tmphtml = readfiletomem(fullfilename, 0);
632                       
633                                        debug(250, "#### tmphtml1 ##################################");
634                                        debug(250, "tmphtml1: %s", buf);
635                                       
636                                        while(ostrstr(tmphtml, "_\(\"") != NULL)
637                                        {
638                                                char* tmpstr1 = string_resub("_(\"", "\")", tmphtml, 0);
639                                                if(tmpstr1 == NULL)
640                                                {
641                                                        debug(250, "Skip  string: %s", tmpstr1);
642                                                        break;
643                                                }
644                                                char* tmpstr2 = ostrcat("_(\"", tmpstr1, 0, 0);
645                                                tmpstr2 = ostrcat(tmpstr2, "\")", 1, 0);
646       
647                                                debug(250, "--------------------------------------");
648                                                debug(250, "Search  string: %s", tmpstr2);
649                                                debug(250, "Replace string: %s", tmpstr1);
650                                                debug(250, "Replace %s -> %s", tmpstr2, tmpstr1);
651                                                debug(250, "--------------------------------------");
652       
653                                                tmphtml = string_replace_all(tmpstr2, _(tmpstr1), tmphtml, 1);
654                                                free(tmpstr1), tmpstr1 = NULL;
655                                                free(tmpstr2), tmpstr2 = NULL;
656                                        }
657                                        debug(250, "#### tmphtml2 ##################################");
658                                        debug(250, "tmphtml2: %s", tmphtml);
659                                       
660                                        free(fullfilename), fullfilename = NULL;
661                                        fullfilename = ostrcat("/tmp/.", filename, 0, 0);
662                                        writesys(fullfilename, tmphtml, 0);
663                                        free(tmphtml), tmphtml = NULL;
664                                }
665                        }
666
667                        filefd = open(fullfilename, O_RDONLY | O_LARGEFILE);
668                        if(filefd < 0)
669                        {
670                                perr("open filename=%s", fullfilename);
671                                senderror(connfd, "Open File", "Can't open File", auth, 0);
672                                free(fullfilename); fullfilename = NULL;
673                                free(buf); buf = NULL;
674                                free(filename); filename = NULL;
675                                tmpstr = NULL;
676                                return;
677                        }
678                       
679                        debug(250, "sende OK response to client");
680                        char* rpath = realpath(fullfilename, NULL);
681                        header = createheader(getfilesize(rpath), fullfilename, NULL, NULL, 200, auth);
682                        free(rpath); rpath = NULL;
683                        free(fullfilename); fullfilename = NULL;
684                        ret = socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
685                        free(header); header = NULL;
686
687                        if(ret != 0)
688                        {
689                                sockclose(connfd);
690                                free(buf); buf = NULL;
691                                free(filename); filename = NULL;
692                                tmpstr = NULL;
693                                return;
694                        }
695
696                        //TODO:
697                        int readret = 1;
698                        while(readret > 0 && auth == 0)
699                        {
700                                readret = dvbreadfd(filefd, buf, 0, MINMALLOC, 1000, 0);
701
702                                if(readret > 0)
703                                        socksend(connfd, buf, readret, 5000 * 1000);
704                        }
705                }
706        }
707
708        close(filefd);
709        free(buf); buf = NULL;
710        free(filename); filename = NULL;
711        tmpstr = NULL;
712}
713
714int newconn(int* streamfd, int* connfd)
715{
716        int i, fd = -1;
717
718        fd = sockaccept(streamfd, 1);
719        if(fd < 0) return -1;
720
721        for(i = 0; i < MAXHTTPDCONN; i++)
722        {
723                if(connfd[i] < 0)
724                {
725                        connfd[i] = fd;
726                        debug(250, "accept httpd connection fd=%d", fd);
727                        fd = -1;
728                        break;
729                }
730        }
731
732        if(fd != -1)
733        {
734                debug(250, "all connections in use");
735                sockclose(&fd);
736                return -1;
737        }
738
739        return 0;
740}
741
742void httpdthreadfunc(struct stimerthread* timernode)
743{
744        struct timeval timeout;
745        fd_set rfds;
746        int i, ret = 0, streamfd = -1, connfd[MAXHTTPDCONN], maxfd = -1;
747
748        if(timernode == NULL) return;
749        debug(250, "Starting httpd thread");
750
751        for(i = 0; i < MAXHTTPDCONN; i++)
752                connfd[i] = -1;
753
754        while(timernode->aktion != STOP)
755        {
756                if(streamfd < 0)
757                {
758                        sockportcreate(&streamfd, getconfigint("httpdport", NULL), MAXHTTPDCONN);
759                        if(streamfd < 0) break;
760                        maxfd = streamfd;
761
762                        //set nonblocking
763                        fcntl(streamfd, F_SETFL, fcntl(streamfd, F_GETFL) | O_NONBLOCK);
764                }
765
766                timeout.tv_sec = 1;
767                timeout.tv_usec = 0;
768
769                FD_ZERO(&rfds);
770                FD_SET(streamfd, &rfds);
771
772                for(i = 0; i < MAXHTTPDCONN; i++)
773                {
774                        if(connfd[i] > -1)
775                        {
776                                FD_SET(connfd[i], &rfds);
777                                if(connfd[i] > maxfd) maxfd = connfd[i];
778                        }
779                }
780
781                ret = TEMP_FAILURE_RETRY(select(maxfd + 1, &rfds , NULL, NULL, &timeout));
782
783                if(ret < 0)
784                {
785                        perr("httpd listen socket fd=%d", maxfd);
786                        sleep(1);
787                        continue;
788                }
789                if(ret == 0) continue; //timeout
790
791                if(FD_ISSET(streamfd, &rfds))
792                {
793                        ret = newconn(&streamfd, connfd);
794                        if(ret < 0) continue;
795                }
796
797                for(i = 0; i < MAXHTTPDCONN; i++)
798                {
799                        if(connfd[i] > -1 && FD_ISSET(connfd[i], &rfds))
800                                gotdata(&connfd[i]);
801                }
802        }
803
804        debug(250, "Stopping httpd thread");
805        sockclose(&streamfd);
806        for(i = 0; i < MAXHTTPDCONN; i++)
807                sockclose(&connfd[i]);
808
809        return;
810}
811
812void sendoktext(int* connfd, char* text, int auth)
813{
814        char* buf = NULL;
815        char* header = NULL;
816        buf = webcreatehead(buf, NULL, 1);
817        buf = ostrcat(buf, "<tr><td align=center valign=top><font class=biglabel><br><br>", 1, 0);
818        buf = ostrcat(buf, text, 1, 0);
819        buf = ostrcat(buf, "</font></td></tr>", 1, 0);
820        buf = webcreatetail(buf, 1);
821        int buflen = strlen(buf);
822        header = createheader(buflen, NULL, NULL, NULL, 200, auth);
823        socksend(connfd, (unsigned char*)header, strlen(header), 5000 * 1000);
824        if(auth == 0)
825                socksend(connfd, (unsigned char*)buf, buflen, 5000 * 1000);
826        free(buf); buf=NULL;
827        free(header); header=NULL;
828}
829
830#endif
Note: See TracBrowser for help on using the repository browser.