source: titan/titan/httpd.h @ 32089

Last change on this file since 32089 was 31855, checked in by gost, 9 years ago

[titan] mipsel.. transcode at transcode button

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