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