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