forked from sackmotion/motion
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webhttpd.c
2555 lines (2317 loc) · 109 KB
/
webhttpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* webhttpd.c
*
* HTTP Control interface for motion.
*
* Specs : http://www.lavrsen.dk/twiki/bin/view/Motion/MotionHttpAPI
*
* Copyright 2004-2005 by Angel Carpintero ([email protected])
* This software is distributed under the GNU Public License Version 2
* See also the file 'COPYING'.
*
*/
#include "webhttpd.h" /* already includes motion.h */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stddef.h>
/* Timeout in seconds, used for read and write */
const int NONBLOCK_TIMEOUT = 1;
pthread_mutex_t httpd_mutex;
// This is a dummy variable use to kill warnings when not checking sscanf and similar functions
int warningkill;
static const char *ini_template =
"<html><head><title>Motion "VERSION"</title></head>\n"
"<body>\n";
static const char *set_template =
"<html><head><script language='javascript'>"
"function show() {top.location.href="
"'set?'+document.n.onames.options[document.n.onames.selectedIndex].value"
"+'='+document.s.valor.value;"
"}</script>\n<title>Motion "VERSION"</title>\n"
"</head><body>\n";
static const char *end_template =
"</body>\n"
"</html>\n";
static const char *ok_response =
"HTTP/1.1 200 OK\r\n"
"Server: Motion-httpd/"VERSION"\r\n"
"Connection: close\r\n"
"Max-Age: 0\r\n"
"Expires: 0\r\n"
"Cache-Control: no-cache\r\n"
"Cache-Control: private\r\n"
"Pragma: no-cache\r\n"
"Content-type: text/html\r\n\r\n";
static const char *ok_response_raw =
"HTTP/1.1 200 OK\r\n"
"Server: Motion-httpd/"VERSION"\r\n"
"Connection: close\r\n"
"Max-Age: 0\r\n"
"Expires: 0\r\n"
"Cache-Control: no-cache\r\n"
"Cache-Control: private\r\n"
"Pragma: no-cache\r\n"
"Content-type: text/plain\r\n\r\n";
static const char *bad_request_response =
"HTTP/1.0 400 Bad Request\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Bad Request</h1>\n"
"<p>The server did not understand your request.</p>\n"
"</body>\n"
"</html>\n";
static const char *bad_request_response_raw =
"HTTP/1.0 400 Bad Request\r\n"
"Content-type: text/plain\r\n\r\n"
"Bad Request";
static const char *not_found_response_template =
"HTTP/1.0 404 Not Found\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Not Found</h1>\n"
"<p>The requested URL was not found on the server.</p>\n"
"</body>\n"
"</html>\n";
static const char *not_found_response_template_raw =
"HTTP/1.0 404 Not Found\r\n"
"Content-type: text/plain\r\n\r\n"
"Not Found";
static const char *not_found_response_valid =
"HTTP/1.0 404 Not Valid\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Not Valid</h1>\n"
"<p>The requested URL is not valid.</p>\n"
"</body>\n"
"</html>\n";
static const char *not_found_response_valid_raw =
"HTTP/1.0 404 Not Valid\r\n"
"Content-type: text/plain\r\n\r\n"
"The requested URL is not valid.";
static const char *not_valid_syntax =
"HTTP/1.0 404 Not Valid Syntax\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Not Valid Syntax</h1>\n"
"</body>\n"
"</html>\n";
static const char *not_valid_syntax_raw =
"HTTP/1.0 404 Not Valid Syntax\r\n"
"Content-type: text/plain\r\n\r\n"
"Not Valid Syntax\n";
static const char *not_track =
"HTTP/1.0 200 OK\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Tracking Not Enabled</h1>\n";
static const char *not_track_raw =
"HTTP/1.0 200 OK\r\n"
"Content-type: text/plain\r\n\r\n"
"Tracking Not Enabled";
static const char *track_error =
"HTTP/1.0 200 OK\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Track Error</h1>\n";
static const char *track_error_raw =
"HTTP/1.0 200 OK\r\n"
"Content-type: text/plain\r\n\r\n"
"Track Error";
static const char *error_value =
"HTTP/1.0 200 OK\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Value Error</h1>\n";
static const char *error_value_raw =
"HTTP/1.0 200 OK\r\n"
"Content-type: text/plain\r\n\r\n"
"Value Error";
static const char *not_found_response_valid_command =
"HTTP/1.0 404 Not Valid Command\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Not Valid Command</h1>\n"
"<p>The requested URL is not valid Command.</p>\n"
"</body>\n"
"</html>\n";
static const char *not_found_response_valid_command_raw =
"HTTP/1.0 404 Not Valid Command\r\n"
"Content-type: text/plain\n\n"
"Not Valid Command\n";
static const char *bad_method_response_template =
"HTTP/1.0 501 Method Not Implemented\r\n"
"Content-type: text/html\r\n\r\n"
"<html>\n"
"<body>\n"
"<h1>Method Not Implemented</h1>\n"
"<p>The method is not implemented by this server.</p>\n"
"</body>\n"
"</html>\n";
static const char *bad_method_response_template_raw =
"HTTP/1.0 501 Method Not Implemented\r\n"
"Content-type: text/plain\r\n\r\n"
"Method Not Implemented\n";
static const char *request_auth_response_template=
"HTTP/1.0 401 Authorization Required\r\n"
"WWW-Authenticate: Basic realm=\"Motion Security Access\"\r\n";
/**
* write_nonblock
*/
static ssize_t write_nonblock(int fd, const void *buf, size_t size)
{
ssize_t nwrite = -1;
struct timeval tm;
fd_set fds;
tm.tv_sec = NONBLOCK_TIMEOUT;
tm.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (select(fd + 1, NULL, &fds, NULL, &tm) > 0) {
if (FD_ISSET(fd, &fds)) {
if ((nwrite = write(fd , buf, size)) < 0) {
if (errno != EWOULDBLOCK)
return -1;
}
}
}
return nwrite;
}
/**
* read_nonblock
*/
static ssize_t read_nonblock(int fd ,void *buf, ssize_t size)
{
ssize_t nread = -1;
struct timeval tm;
fd_set fds;
tm.tv_sec = NONBLOCK_TIMEOUT; /* Timeout in seconds */
tm.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
if (select(fd + 1, &fds, NULL, NULL, &tm) > 0) {
if (FD_ISSET(fd, &fds)) {
if ((nread = read(fd , buf, size)) < 0) {
if (errno != EWOULDBLOCK)
return -1;
}
}
}
return nread;
}
/**
* send_template_ini_client
*/
static void send_template_ini_client(int client_socket, const char *template)
{
ssize_t nwrite = 0;
nwrite = write_nonblock(client_socket, ok_response, strlen(ok_response));
nwrite += write_nonblock(client_socket, template, strlen(template));
if (nwrite != (ssize_t)(strlen(ok_response) + strlen(template)))
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: failure write");
}
/**
* send_template_ini_client_raw
*/
static void send_template_ini_client_raw(int client_socket)
{
ssize_t nwrite = 0;
nwrite = write_nonblock(client_socket, ok_response_raw, strlen(ok_response_raw));
if (nwrite != (ssize_t)strlen(ok_response_raw))
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: failure write");
}
/**
* send_template
*/
static void send_template(int client_socket, char *res)
{
ssize_t nwrite = 0;
nwrite = write_nonblock(client_socket, res, strlen(res));
if (nwrite != (ssize_t)strlen(res))
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "%s: failure write");
}
/**
* send_template_raw
*/
static void send_template_raw(int client_socket, char *res)
{
ssize_t nwrite = 0;
nwrite = write_nonblock(client_socket, res, strlen(res));
}
/**
* send_template_end_client
*/
static void send_template_end_client(int client_socket)
{
ssize_t nwrite = 0;
nwrite = write_nonblock(client_socket, end_template, strlen(end_template));
}
/**
* response_client
*/
static void response_client(int client_socket, const char *template, char *back)
{
ssize_t nwrite = 0;
nwrite = write_nonblock(client_socket, template, strlen(template));
if (back != NULL) {
send_template(client_socket, back);
send_template_end_client(client_socket);
}
}
/**
* replace
*/
static char *replace(const char *str, const char *old, const char *new)
{
char *ret, *r;
const char *p, *q;
size_t oldlen = strlen(old);
size_t count, retlen, newlen = strlen(new);
if (oldlen != newlen) {
for (count = 0, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen)
count++;
/* this is undefined if p - str > PTRDIFF_MAX */
retlen = p - str + strlen(p) + count * (newlen - oldlen);
} else
retlen = strlen(str);
ret = mymalloc(retlen + 1);
for (r = ret, p = str; (q = strstr(p, old)) != NULL; p = q + oldlen) {
/* this is undefined if q - p > PTRDIFF_MAX */
ptrdiff_t l = q - p;
memcpy(r, p, l);
r += l;
memcpy(r, new, newlen);
r += newlen;
}
strcpy(r, p);
return ret;
}
/**
* url_decode
* This function decode the values from GET request following the http RFC.
*
* Returns nothing.
*/
static void url_decode(char *urlencoded, size_t length)
{
char *data = urlencoded;
char *urldecoded = urlencoded;
while (length > 0) {
if (*data == '%') {
char c[3];
int i;
data++;
length--;
c[0] = *data++;
length--;
c[1] = *data;
c[2] = 0;
warningkill = sscanf(c, "%x", &i);
if (i < 128) {
*urldecoded++ = (char)i;
} else {
*urldecoded++ = '%';
*urldecoded++ = c[0];
*urldecoded++ = c[1];
}
} else if (*data == '+') {
*urldecoded++ = ' ';
} else {
*urldecoded++ = *data;
}
data++;
length--;
}
*urldecoded = '\0';
}
/**
* config
* Manages/parses the config action for motion ( set , get , write , list ).
*
* Returns 1 to exit from function.
*/
static unsigned int config(char *pointer, char *res, unsigned int length_uri,
unsigned int thread, int client_socket, void *userdata)
{
char question='\0';
char command[256] = {'\0'};
unsigned int i;
struct context **cnt = userdata;
warningkill = sscanf(pointer, "%255[a-z]%c", command , &question);
if (!strcmp(command, "list")) {
pointer = pointer + 4;
length_uri = length_uri - 4;
if (length_uri == 0) {
const char *value = NULL;
char *retval = NULL;
/*call list*/
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, ini_template);
sprintf(res, "<a href=/%hu/config><– back</a><br><br>\n<b>Thread %hu</b>\n<ul>",
thread, thread);
send_template(client_socket, res);
for (i=0; config_params[i].param_name != NULL; i++) {
if ((thread != 0) && (config_params[i].main_thread))
continue;
value = config_params[i].print(cnt, NULL, i, thread);
if (value == NULL) {
retval = NULL;
/* Only get the thread value for main thread */
if (thread == 0)
config_params[i].print(cnt, &retval, i, thread);
/* thread value*/
if (retval) {
if (!strcmp(retval,"")) {
free(retval);
retval = mystrdup("No threads");
} else {
char *temp = retval;
size_t retval_miss = 0;
size_t retval_len = strlen(retval);
unsigned int ind = 0;
char thread_strings[1024] = {'\0'};
while (retval_miss != retval_len) {
while (*temp != '\n') {
thread_strings[ind++] = *temp;
retval_miss++;
temp++;
}
temp++;
thread_strings[ind++] = '<';
thread_strings[ind++] = 'b';
thread_strings[ind++] = 'r';
thread_strings[ind++] = '>';
retval_miss++;
}
free(retval);
retval = NULL;
retval = mystrdup(thread_strings);
}
sprintf(res, "<li><a href=/%hu/config/set?%s>%s</a> = %s</li>\n", thread,
config_params[i].param_name, config_params[i].param_name, retval);
free(retval);
} else if (thread != 0) {
/* get the value from main thread for the rest of threads */
value = config_params[i].print(cnt, NULL, i, 0);
sprintf(res, "<li><a href=/%hu/config/set?%s>%s</a> = %s</li>\n", thread,
config_params[i].param_name, config_params[i].param_name,
value ? value : "(not defined)");
} else {
sprintf(res, "<li><a href=/%hu/config/set?%s>%s</a> = %s</li>\n", thread,
config_params[i].param_name, config_params[i].param_name,
"(not defined)");
}
} else {
sprintf(res, "<li><a href=/%hu/config/set?%s>%s</a> = %s</li>\n", thread,
config_params[i].param_name, config_params[i].param_name, value);
}
send_template(client_socket, res);
}
sprintf(res, "</ul><a href=/%hu/config><– back</a>", thread);
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
for (i=0; config_params[i].param_name != NULL; i++) {
value = config_params[i].print(cnt, NULL, i, thread);
if (value == NULL)
value = config_params[i].print(cnt, NULL, i, 0);
sprintf(res, "%s = %s\n", config_params[i].param_name, value);
send_template_raw(client_socket, res);
}
}
} else {
/*error*/
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else if (!strcmp(command, "set")) {
/* set?param_name=value */
pointer = pointer + 3;
length_uri = length_uri - 3;
if ((length_uri != 0) && (question == '?')) {
pointer++;
length_uri--;
warningkill = sscanf(pointer,"%255[-0-9a-z_]%c", command, &question);
/*check command , question == '=' length_uri too*/
if ((question == '=') && (command[0] != '\0')) {
length_uri = length_uri - strlen(command) - 1;
pointer = pointer + strlen(command) + 1;
/* check if command exists and type of command and not end of URI */
i=0;
while (config_params[i].param_name != NULL) {
if ((thread != 0) && (config_params[i].main_thread)) {
i++;
continue;
}
if (!strcasecmp(command, config_params[i].param_name))
break;
i++;
}
if (config_params[i].param_name) {
if (length_uri > 0) {
char Value[1024] = {'\0'};
warningkill = sscanf(pointer,"%1023s", Value);
length_uri = length_uri - strlen(Value);
if ((length_uri == 0) && (strlen(Value) > 0)) {
/* FIXME need to assure that is a valid value */
url_decode(Value, strlen(Value));
conf_cmdparse(cnt + thread, config_params[i].param_name, Value);
if (cnt[0]->conf.webcontrol_html_output) {
sprintf(res,
"<a href=/%hu/config/list><– back</a>"
"<br><br>\n<b>Thread %hu</b>\n"
"<ul><li><a href=/%hu/config/set?%s>%s</a> = %s"
"</li></ul><b>Done</b>",
thread, thread, thread, config_params[i].param_name,
config_params[i].param_name, Value);
send_template_ini_client(client_socket, ini_template);
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "%s = %s\nDone\n", config_params[i].param_name, Value);
send_template_raw(client_socket, res);
}
} else {
/*error*/
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_valid_syntax, NULL);
else
response_client(client_socket, not_valid_syntax_raw, NULL);
}
} else {
char *type = NULL;
type = mystrdup(config_type(&config_params[i]));
if (!strcmp(type, "string")) {
char *value = NULL;
conf_cmdparse(cnt+thread, config_params[i].param_name, value);
free(type);
type = mystrdup("(null)");
} else if (!strcmp(type, "int")) {
free(type);
type = mystrdup("0");
conf_cmdparse(cnt+thread, config_params[i].param_name, type);
} else if (!strcmp(type, "bool")) {
free(type);
type = mystrdup("off");
conf_cmdparse(cnt+thread, config_params[i].param_name, type);
} else {
free(type);
type = mystrdup("unknown");
}
if (cnt[0]->conf.webcontrol_html_output) {
sprintf(res,
"<a href=/%hu/config/list><– back</a><br><br>\n"
"<b>Thread %hu</b>\n<ul><li><a href=/%hu/config/set?%s>%s</a>"
"= %s</li></ul><br><b>Done</b>", thread, thread, thread,
config_params[i].param_name, config_params[i].param_name, type);
send_template_ini_client(client_socket, ini_template);
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "%s = %s\nDone\n", config_params[i].param_name, type);
send_template_raw(client_socket, res);
}
free(type);
}
} else {
/*error*/
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else {
/* Show param_name dialogue only for html output */
if ((cnt[0]->conf.webcontrol_html_output) && (command[0] != '\0') &&
(((length_uri = length_uri - strlen(command)) == 0))) {
i=0;
while (config_params[i].param_name != NULL) {
if ((thread != 0) && (config_params[i].main_thread)) {
i++;
continue;
}
if (!strcasecmp(command, config_params[i].param_name))
break;
i++;
}
/* param_name exists */
if (config_params[i].param_name) {
const char *value = NULL;
char *text_help = NULL;
char *sharp = NULL;
value = config_params[i].print(cnt, NULL, i, thread);
sharp = strstr(config_params[i].param_help, "#\n\n#");
if (sharp == NULL)
sharp = strstr(config_params[i].param_help, "#");
sharp++;
text_help = replace(sharp, "\n#", "<br>");
send_template_ini_client(client_socket, ini_template);
if (!strcmp ("bool", config_type(&config_params[i]))) {
char option[80] = {'\0'};
if ((value == NULL) && (thread != 0))
value = config_params[i].print(cnt, NULL, i, 0);
if (!strcmp ("on", value))
sprintf(option, "<option value='on' selected>on</option>\n"
"<option value='off'>off</option>\n");
else
sprintf(option, "<option value='on'>on</option>\n"
"<option value='off' selected>off</option>\n");
sprintf(res, "<a href=/%hu/config/list><– back</a><br><br>\n"
"<b>Thread %hu</b>\n"
"<form action=set?>\n"
"<b>%s</b> <select name='%s'>\n"
"%s"
"</select><input type='submit' value='set'>\n"
" "
"<a href='%s#%s' target=_blank>[help]</a>"
"</form>\n<hr><i>%s</i>", thread, thread,
config_params[i].param_name, config_params[i].param_name,
option, TWIKI_URL, config_params[i].param_name, text_help);
} else {
if (value == NULL) {
if (thread != 0)
/* get the value from main thread for the rest of threads */
value = config_params[i].print(cnt, NULL, i, 0);
if (value == NULL) value = "";
}
sprintf(res, "<a href=/%hu/config/list><– back</a><br><br>\n"
"<b>Thread %hu</b>\n<form action=set?>\n"
"<b>%s</b> <input type=text name='%s' value='%s' size=80>\n"
"<input type='submit' value='set'>\n"
" "
"<a href='%s#%s' target=_blank>[help]</a>"
"</form>\n<hr><i>%s</i>", thread, thread,
config_params[i].param_name, config_params[i].param_name,
value, TWIKI_URL, config_params[i].param_name, text_help);
}
send_template(client_socket, res);
send_template_end_client(client_socket);
free(text_help);
} else {
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else {
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
}
} else if (length_uri == 0) {
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, set_template);
sprintf(res, "<a href=/%hu/config><– back</a><br><br>\n<b>Thread %hu</b>\n"
"<form name='n'>\n<select name='onames'>\n", thread, thread);
send_template(client_socket, res);
for (i=0; config_params[i].param_name != NULL; i++) {
if ((thread != 0) && (config_params[i].main_thread))
continue;
sprintf(res, "<option value='%s'>%s</option>\n",
config_params[i].param_name, config_params[i].param_name);
send_template(client_socket, res);
}
sprintf(res, "</select>\n</form>\n"
"<form action=set name='s'"
"ONSUBMIT='if (!this.submitted) return false; else return true;'>\n"
"<input type=text name='valor' value=''>\n"
"<input type='button' value='set' onclick='javascript:show()'>\n"
"</form>\n");
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "set needs param_name=value\n");
send_template_raw(client_socket, res);
}
} else {
/*error*/
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else if (!strcmp(command, "get")) {
/* get?query=param_name */
pointer = pointer + 3;
length_uri = length_uri - 3;
if ((length_uri > 7) && (question == '?')) {
/* 8 -> query=param_name FIXME minimum length param_name */
pointer++;
length_uri--;
warningkill = sscanf(pointer,"%255[-0-9a-z]%c", command, &question);
if ((question == '=') && (!strcmp(command, "query"))) {
pointer = pointer + 6;
length_uri = length_uri - 6;
warningkill = sscanf(pointer, "%255[-0-9a-z_]", command);
/*check if command exist, length_uri too*/
length_uri = length_uri - strlen(command);
if (length_uri == 0) {
const char *value = NULL;
i = 0;
while (config_params[i].param_name != NULL) {
if ((thread != 0) && (config_params[i].main_thread)) {
i++;
continue;
}
if (!strcasecmp(command, config_params[i].param_name))
break;
i++;
}
/*
* FIXME bool values or commented values maybe that should be
* solved with config_type
*/
if (config_params[i].param_name) {
const char *type = NULL;
type = config_type(&config_params[i]);
if (!strcmp(type, "unknown")) {
/* error doesn't exists this param_name */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
return 1;
} else {
char *text_help = NULL;
char *sharp = NULL;
value = config_params[i].print(cnt, NULL, i, thread);
sharp = strstr(config_params[i].param_help, "#\n\n#");
if (sharp == NULL)
sharp = strstr(config_params[i].param_help, "#");
sharp++;
text_help = replace(sharp, "\n#", "<br>");
if (value == NULL)
value = config_params[i].print(cnt, NULL, i, 0);
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, ini_template);
sprintf(res, "<a href=/%hu/config/get><– back</a><br><br>\n"
"<b>Thread %hu</b><br>\n<ul><li>%s = %s "
" <a href='%s#%s' target=_blank>"
"[help]</a></li></ul><hr><i>%s</i>",
thread, thread, config_params[i].param_name, value,
TWIKI_URL, config_params[i].param_name, text_help);
send_template(client_socket, res);
send_template_end_client(client_socket);
free(text_help);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "%s = %s\nDone\n", config_params[i].param_name, value);
send_template_raw(client_socket, res);
}
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
}
} else if (length_uri == 0) {
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, ini_template);
sprintf(res, "<a href=/%hu/config><– back</a><br><br>\n<b>Thread %hu</b><br>\n"
"<form action=get>\n"
"<select name='query'>\n", thread, thread);
send_template(client_socket, res);
for (i=0; config_params[i].param_name != NULL; i++) {
if ((thread != 0) && (config_params[i].main_thread))
continue;
sprintf(res, "<option value='%s'>%s</option>\n",
config_params[i].param_name, config_params[i].param_name);
send_template(client_socket, res);
}
sprintf(res, "</select>\n"
"<input type='submit' value='get'>\n"
"</form>\n");
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "get needs param_name\n");
send_template_raw(client_socket, res);
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_valid_syntax, NULL);
else
response_client(client_socket, not_valid_syntax_raw, NULL);
}
} else if (!strcmp(command, "write")) {
pointer = pointer + 5;
length_uri = length_uri - 5;
if (length_uri == 0) {
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, ini_template);
sprintf(res, "<a href=/%hu/config><– back</a><br><br>"
"Are you sure? <a href=/%hu/config/writeyes>Yes</a>\n", thread, thread);
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
conf_print(cnt);
send_template_ini_client_raw(client_socket);
sprintf(res, "Thread %hu write\nDone\n", thread);
send_template_raw(client_socket, res);
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else if (!strcmp(command, "writeyes")) {
pointer = pointer + 8;
length_uri = length_uri - 8;
if (length_uri == 0) {
conf_print(cnt);
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, ini_template);
sprintf(res, "<a href=/%hu/config><– back</a><br><br>\n<b>Thread %hu</b> write done !\n",
thread, thread);
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "Thread %hu write\nDone\n", thread);
send_template_raw(client_socket, res);
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output) {
response_client(client_socket, not_found_response_valid_command, NULL);
}
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
return 1;
}
/**
* action
* manages/parses the actions for motion ( makemovie , snapshot , restart , quit ).
*
* Returns
* 0 for restart & quit
* 1 for makemovie & snaphost
*/
static unsigned int action(char *pointer, char *res, unsigned int length_uri,
unsigned int thread, int client_socket, void *userdata)
{
/* parse action commands */
char command[256] = {'\0'};
struct context **cnt = userdata;
unsigned int i = 0;
warningkill = sscanf(pointer, "%255[a-z]" , command);
if (!strcmp(command, "makemovie")) {
pointer = pointer + 9;
length_uri = length_uri - 9;
if (length_uri == 0) {
/* call makemovie */
if (thread == 0) {
while (cnt[++i])
cnt[i]->makemovie = 1;
} else {
cnt[thread]->makemovie = 1;
}
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, ini_template);
sprintf(res, "<a href=/%hu/action><– back</a><br><br>\n"
"makemovie for thread %hu done<br>\n", thread, thread);
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "makemovie for thread %hu\nDone\n", thread);
send_template_raw(client_socket, res);
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}
} else if (!strcmp(command, "snapshot")) {
pointer = pointer + 8;
length_uri = length_uri - 8;
if (length_uri == 0) {
/* call snapshot */
if (thread == 0) {
while (cnt[++i])
cnt[i]->snapshot = 1;
} else {
cnt[thread]->snapshot = 1;
}
cnt[thread]->snapshot = 1;
if (cnt[0]->conf.webcontrol_html_output) {
send_template_ini_client(client_socket, ini_template);
sprintf(res, "<a href=/%hu/action><– back</a><br><br>\n"
"snapshot for thread %hu done<br>\n", thread, thread);
send_template(client_socket, res);
send_template_end_client(client_socket);
} else {
send_template_ini_client_raw(client_socket);
sprintf(res, "snapshot for thread %hu\nDone\n", thread);
send_template_raw(client_socket, res);
}
} else {
/* error */
if (cnt[0]->conf.webcontrol_html_output)
response_client(client_socket, not_found_response_valid_command, NULL);
else
response_client(client_socket, not_found_response_valid_command_raw, NULL);
}