source: titan/titan/httpd.h @ 42987

Last change on this file since 42987 was 40757, checked in by gost, 7 years ago

[titan] add new web function

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