forked from Motion-Project/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webu.c
2313 lines (1899 loc) · 81.8 KB
/
webu.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
/*
* webu.c
*
* Web User control interface control for motion.
*
* This software is distributed under the GNU Public License Version 2
* See also the file 'COPYING'.
*
* Portions of code from Angel Carpintero ([email protected])
* from webhttpd.c Copyright 2004-2005
*
* Majority of module written by MrDave.
*
* Function scheme:
* webu* - All functions in this module have this prefix.
* webu_main - Main entry point from the motion thread and only function exposed.
* webu_html* - Functions that create the display web page.
* webu_html_style* - The style section of the web page
* webu_html_script* - The javascripts of the web page
* webu_html_navbar* - The navbar section of the web page
* webu_text* - Functions that create the text interface.
* webu_process_action - Performs most items under the action menu
* webu_process_config - Saves the parameter values into Motion.
*
* Some function names are long and are not expected to contain any
* logger message that would display the function name to the user.
*
* Functions are generally kept to under one page in length
*
* The "written" variable is used extensively with the writes but even
* if it fails, we choose to continue. The user would get a bad page in
* this situation and would be expected to hit "refresh"
*
* To debug, run code, open page, view source and make copy of html
* into a local file to revise changes then determine applicable section(s)
* in this code to modify to match modified version.
*
* Known Issues:
* The quit/restart uses signals and this should be reconsidered.
* The tracking is "best effort" since developer does not have tracking camera.
* The conf_cmdparse assumes that the pointers to the motion context for each
* camera are always sequential and enforcement of the pointers being sequential
* has not been observed in the other modules. (This is a legacy assumption)
* Known HTML Issues:
* Single and double quotes are not consistently used.
* HTML ids do not follow any naming convention.
* After clicking restart/quit, do something..close page? Try to connect again?
*
* Additional functionality considerations:
* Match stream authentication methods
* Add cors (Cross origin requests)
* Notification to user of items that require restart when changed.
* Notification to user that item successfully implemented (config change/tracking)
* Whether there is a need to perpetuate the non-html interface option.
* Implement post method to handle larger string parameters.
* Provide status pages provided in legacy interface.
* List motion parms somewhere so they can be found by xgettext
*
*
*
*/
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stddef.h>
#include <ctype.h>
#include "motion.h"
#include "webu.h"
#include "translate.h"
/* Some defines of lengths for our buffers */
#define WEBUI_LEN_PARM 512 /* Parameters specified */
#define WEBUI_LEN_BUFF 1024 /* Buffer from the header */
#define WEBUI_LEN_RESP 1024 /* Our responses. (Break up response if more space needed) */
#define WEBUI_LEN_SPRM 10 /* Shorter parameter buffer (method/protocol) */
#define WEBUI_LEN_URLI 512 /* Maximum URL permitted */
#define WEBUI_LEN_THRD 6 /* Maximum length for thread number e.g. 99999 */
struct webui_ctx {
pthread_mutex_t webu_mutex; /* The mutex to lock activity on the pipe*/
int client_socket; /* Client socket to the pipe */
char *auth; /* Authorization provided by user*/
char *auth_parms; /* Authorization parms from config file*/
char *url; /* The URL sent from the client */
char *protocol; /* Protocol provided from the body of request*/
char *method; /* Method provided from the body of the request*/
char *uri_thread; /* Parsed thread number from the url*/
char *uri_cmd1; /* Parsed command(action) from the url*/
char *uri_cmd2; /* Parsed command (set) from the url*/
char *uri_parm1; /* Parameter 1 for the command */
char *uri_value1; /* The value for parameter 1*/
char *uri_parm2; /* Parameter 2 for the command */
char *uri_value2; /* The value for parameter 2*/
char *uri_buffer; /* Buffer of the header request content */
char *hostname; /* Host name provided from header content*/
int cam_count; /* Count of the number of cameras*/
int cam_threads; /* Count of the number of camera threads running*/
char *lang; /* Two character abbreviation for locale language*/
char *lang_full; /* Five character abbreviation for language-country*/
};
static void webu_context_init(struct context **cnt, struct webui_ctx *webui) {
int indx;
webui->auth_parms = NULL;
webui->method = NULL;
webui->url = NULL;
webui->protocol = NULL;
webui->uri_buffer = NULL;
webui->client_socket = -1;
webui->hostname = NULL;
/* These will be re-used for multiple calls
* so we reserve WEBUI_LEN_PARM bytes which should be
* plenty (too much?) space and we
* don't have into overrun problems
*/
webui->method = mymalloc(WEBUI_LEN_SPRM);
webui->url = mymalloc(WEBUI_LEN_URLI);
webui->protocol = mymalloc(WEBUI_LEN_SPRM);
webui->uri_thread = mymalloc(WEBUI_LEN_THRD);
webui->uri_cmd1 = mymalloc(WEBUI_LEN_PARM);
webui->uri_cmd2 = mymalloc(WEBUI_LEN_PARM);
webui->uri_parm1 = mymalloc(WEBUI_LEN_PARM);
webui->uri_value1 = mymalloc(WEBUI_LEN_PARM);
webui->uri_parm2 = mymalloc(WEBUI_LEN_PARM);
webui->uri_value2 = mymalloc(WEBUI_LEN_PARM);
webui->uri_buffer = mymalloc(WEBUI_LEN_BUFF);
webui->lang = mymalloc(3); /* Two digit lang code plus null terminator */
webui->lang_full = mymalloc(6); /* lang code, underscore, country plus null terminator */
/* get the number of cameras and threads */
indx = 0;
while (cnt[++indx]);
webui->cam_threads = indx;
webui->cam_count = indx;
if (indx > 1)
webui->cam_count--;
/* 1 thread, 1 camera = just motion.conf.
* 2 thread, 1 camera, then using motion.conf plus a separate camera file */
snprintf(webui->lang_full, 6,"%s", getenv("LANGUAGE"));
snprintf(webui->lang, 3,"%s",webui->lang_full);
return;
}
static void webu_context_null(struct webui_ctx *webui) {
webui->auth_parms = NULL;
webui->method = NULL;
webui->url = NULL;
webui->protocol = NULL;
webui->uri_buffer = NULL;
webui->hostname = NULL;
webui->uri_thread = NULL;
webui->uri_cmd1 = NULL;
webui->uri_cmd2 = NULL;
webui->uri_parm1 = NULL;
webui->uri_value1 = NULL;
webui->uri_parm2 = NULL;
webui->uri_value2 = NULL;
webui->lang = NULL;
webui->lang_full = NULL;
return;
}
static void webu_context_free(struct webui_ctx *webui) {
if (webui->auth_parms != NULL) free(webui->auth_parms);
if (webui->method != NULL) free(webui->method);
if (webui->url != NULL) free(webui->url);
if (webui->protocol != NULL) free(webui->protocol);
if (webui->uri_buffer != NULL) free(webui->uri_buffer);
if (webui->hostname != NULL) free(webui->hostname);
if (webui->uri_thread != NULL) free(webui->uri_thread);
if (webui->uri_cmd1 != NULL) free(webui->uri_cmd1);
if (webui->uri_cmd2 != NULL) free(webui->uri_cmd2);
if (webui->uri_parm1 != NULL) free(webui->uri_parm1);
if (webui->uri_value1 != NULL) free(webui->uri_value1);
if (webui->uri_parm2 != NULL) free(webui->uri_parm2);
if (webui->uri_value2 != NULL) free(webui->uri_value2);
if (webui->lang != NULL) free(webui->lang);
if (webui->lang_full != NULL) free(webui->lang_full);
webu_context_null(webui);
free(webui);
return;
}
static void webu_clientip(char *buf, int fd) {
/* Return the IP of the connecting client*/
struct sockaddr_in6 client;
socklen_t client_len;
char host[NI_MAXHOST];
int retcd;
strncpy(buf, "Unknown", NI_MAXHOST - 1);
client_len = sizeof(client);
retcd = getpeername(fd, (struct sockaddr *)&client, &client_len);
if (retcd != 0) return;
retcd = getnameinfo((struct sockaddr *)&client, client_len, host, sizeof(host), NULL, 0, NI_NUMERICHOST);
if (retcd != 0) return;
strncpy(buf, host, NI_MAXHOST - 1);
}
static ssize_t webu_read(int fd ,void *buf, ssize_t size) {
/* Reads device and returns number of bytes read*/
ssize_t nread = -1;
struct timeval tm;
fd_set fds;
tm.tv_sec = 1; /* 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;
}
static ssize_t webu_write(int fd, const void *buf, size_t buffer_size) {
ssize_t nwrite = -1;
struct timeval tm;
fd_set fds;
tm.tv_sec = 1;
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, buffer_size)) < 0) {
if (errno != EWOULDBLOCK)
return -1;
}
}
}
return nwrite;
}
static void webu_html_ok(struct webui_ctx *webui){
/* Send message that everything is OK */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
"HTTP/1.1 200 OK\r\n"
"Server: Motion /"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");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
static void webu_html_badreq(struct webui_ctx *webui) {
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
"HTTP/1.0 400 Bad Request\r\n"
"Content-type: text/html\r\n\r\n"
"<!DOCTYPE html>\n"
"<html>\n"
"<body>\n"
"<h1>Bad Request</h1>\n"
"<p>The server did not understand your request.</p>\n"
"</body>\n"
"</html>\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
return;
}
static void webu_text_ok(struct webui_ctx *webui){
/* Send message that everything is OK */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
"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");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
static void webu_text_badreq(struct webui_ctx *webui) {
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
"HTTP/1.0 400 Bad Request\r\n"
"Content-type: text/plain\r\n\r\n"
"Bad Request\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
return;
}
static void webu_resp_auth(struct webui_ctx *webui) {
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response), "%s",
"HTTP/1.0 401 Authorization Required\r\n"
"WWW-Authenticate: Basic realm=\"Motion Security Access\"\r\n\r\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
return;
}
static int webu_nonblock(int serverfd) {
int curfd;
int timeout = 1;
struct sockaddr_in6 client;
socklen_t client_len = sizeof(client);
struct timeval tm;
fd_set fds;
tm.tv_sec = timeout; /* Timeout in seconds */
tm.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(serverfd, &fds);
if (select(serverfd + 1, &fds, NULL, NULL, &tm) > 0) {
if (FD_ISSET(serverfd, &fds)) {
if ((curfd = accept(serverfd, (struct sockaddr *)&client, &client_len)) > 0)
return curfd;
}
}
return -1;
}
static void webu_url_decode(char *urlencoded, size_t length) {
char *data = urlencoded;
char *urldecoded = urlencoded;
int scan_rslt;
while (length > 0) {
if (*data == '%') {
char c[3];
int i;
data++;
length--;
c[0] = *data++;
length--;
c[1] = *data;
c[2] = 0;
scan_rslt = sscanf(c, "%x", &i);
if (scan_rslt < 1) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error decoding");
if (i < 128) {
*urldecoded++ = (char)i;
} else {
*urldecoded++ = '%';
*urldecoded++ = c[0];
*urldecoded++ = c[1];
}
} else if (*data == '<' || *data == '+' || *data == '>') {
*urldecoded++ = ' ';
} else {
*urldecoded++ = *data;
}
data++;
length--;
}
*urldecoded = '\0';
}
static int webu_header_read(struct webui_ctx *webui) {
ssize_t bytes_read = 0, readb = -1;
int uri_length=WEBUI_LEN_BUFF - 1;
int parm_len;
char *st_pos, *en_pos;
bytes_read = webu_read(webui->client_socket, webui->uri_buffer, uri_length);
if (bytes_read <= 0) {
MOTION_LOG(DBG, TYPE_STREAM, SHOW_ERRNO, "First Read Error %d",bytes_read);
return -1;
}
webui->uri_buffer[bytes_read] = '\0';
parm_len = 0;
/* Get the method from the header */
st_pos = webui->uri_buffer;
en_pos = strstr(st_pos," ");
if (en_pos != NULL){
parm_len = en_pos - st_pos + 1;
if (parm_len >= WEBUI_LEN_SPRM){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, "Invalid method. Buffer:>%s<",webui->uri_buffer);
return -1;
}
snprintf(webui->method, parm_len,"%s", st_pos);
}
/* Get the url name */
st_pos = st_pos + parm_len; /* Move past the method */
en_pos = strstr(st_pos," ");
if (en_pos != NULL){
parm_len = en_pos - st_pos + 1;
if (parm_len >= WEBUI_LEN_URLI){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, "Invalid url. Buffer:>%s<",webui->uri_buffer);
return -1;
}
snprintf(webui->url, parm_len,"%s", st_pos);
}
/* Get the protocol name */
st_pos = st_pos + parm_len; /* Move past the url */
en_pos = strstr(st_pos,"\r");
if (en_pos != NULL){
parm_len = en_pos - st_pos + 1;
if (parm_len >= WEBUI_LEN_SPRM){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, "Invalid protocol. Buffer:>%s< %d"
,webui->uri_buffer,parm_len);
return -1;
}
snprintf(webui->protocol, parm_len,"%s", st_pos);
}
/* Read remaining header requests until crlf crlf */
while ((strstr(webui->uri_buffer, "\r\n\r\n") == NULL) &&
(bytes_read != 0) &&
(bytes_read < uri_length)) {
readb = webu_read(webui->client_socket
, webui->uri_buffer + bytes_read
, sizeof (webui->uri_buffer) - bytes_read);
if (readb == -1) {
bytes_read = -1;
break;
}
bytes_read += readb;
if (bytes_read > uri_length) {
MOTION_LOG(WRN, TYPE_STREAM, SHOW_ERRNO, "End of buffer"
" reached waiting for buffer ending");
break;
}
webui->uri_buffer[bytes_read] = '\0';
}
/* Check that last read was OK */
if (bytes_read == -1) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "Invalid last read!");
return -1;
}
if (((strcmp(webui->protocol, "HTTP/1.0")) &&
(strcmp(webui->protocol, "HTTP/1.1"))) ||
(strcmp(webui->method, "GET"))) {
MOTION_LOG(ERR, TYPE_STREAM, SHOW_ERRNO, "Invalid protocol/method");
return -1;
}
return 0;
}
static int webu_header_auth(struct webui_ctx *webui) {
char *authentication=NULL;
char *end_auth = NULL;
char clientip[NI_MAXHOST];
if (webui->auth_parms != NULL) {
if ((authentication = strstr(webui->uri_buffer,"Basic"))) {
authentication = authentication + 6;
if ((end_auth = strstr(authentication,"\r\n"))) {
authentication[end_auth - authentication] = '\0';
} else {
webu_resp_auth(webui);
return -1;
}
if (strcmp(webui->auth_parms, authentication)) {
webu_resp_auth(webui);
webu_clientip(clientip, webui->client_socket);
MOTION_LOG(ALR, TYPE_STREAM, NO_ERRNO, "Failed auth attempt from %s", clientip);
return -1;
}
} else {
webu_resp_auth(webui);
return -1;
}
}
return 0;
}
static int webu_header_hostname(struct webui_ctx *webui) {
/* use the hostname the browser used to connect to us when
* constructing links to the stream ports. If available
* (which it is in all modern browsers) it is more likely to
* work than the result of gethostname(), which is reliant on
* the machine we're running on having it's hostname setup
* correctly and corresponding DNS in place. */
char *end_host;
char *st_host;
char *colon = NULL;
char *end_bracket;
if ((st_host = strstr(webui->uri_buffer,"Host:"))) {
st_host += strlen("Host:");
end_host = strstr(st_host,"\r\n");
if (end_host) {
while (st_host < end_host && isspace(st_host[0]))
st_host++;
while (st_host < end_host && isspace(end_host[-1]))
end_host--;
/* Strip off any port number and colon */
/* hostname is a IPv6 address like "[::1]" */
if (st_host[0] == '[') {
end_bracket = memchr(st_host, ']', end_host - st_host);
// look for the colon after the "]"
colon = memchr(end_bracket, ':', end_host-end_bracket);
} else {
colon = memchr(st_host, ':', end_host - st_host);
}
if (colon) end_host = colon;
if (webui->hostname != NULL) free(webui->hostname);
webui->hostname = mymalloc(end_host-st_host+2);
snprintf(webui->hostname, end_host-st_host+1, "%s", st_host);
}
}
if (webui->hostname == NULL){
/* Set the host that is running Motion */
webui->hostname = mymalloc(WEBUI_LEN_PARM);
memset(webui->hostname,'\0',WEBUI_LEN_PARM);
gethostname(webui->hostname, WEBUI_LEN_PARM - 1);
}
return 0;
}
static void webu_parseurl_parms(struct webui_ctx *webui, char *st_pos) {
int parm_len, last_parm;
char *en_pos;
/* First parse out the "set","get","pan","tilt","x","y"
* from the uri and put them into the cmd2.
* st_pos is at the beginning of the command
* If there is no ? then we are done parsing
*/
last_parm = 0;
en_pos = strstr(st_pos,"?");
if (en_pos != NULL){
parm_len = en_pos - st_pos + 1;
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_cmd2, parm_len,"%s", st_pos);
/* Get the parameter name */
st_pos = st_pos + parm_len; /* Move past the command */
en_pos = strstr(st_pos,"=");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_parm1, parm_len,"%s", st_pos);
if (!last_parm){
/* Get the parameter value */
st_pos = st_pos + parm_len; /* Move past the equals sign */
en_pos = strstr(st_pos,"&");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_value1, parm_len,"%s", st_pos);
}
if (!last_parm){
/* Get the next parameter name */
st_pos = st_pos + parm_len; /* Move past the command */
en_pos = strstr(st_pos,"=");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_parm2, parm_len,"%s", st_pos);
}
if (!last_parm){
/* Get the next parameter value */
st_pos = st_pos + parm_len; /* Move past the equals sign */
en_pos = strstr(st_pos,"&");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_value2, parm_len,"%s", st_pos);
}
}
}
static void webu_parseurl_reset(struct webui_ctx *webui) {
/* Reset the variable to empty strings since they
* are re-used across calls. These are allocated
* larger sizes to allow for multiple variable types.
*/
memset(webui->uri_thread,'\0',WEBUI_LEN_THRD);
memset(webui->uri_cmd1,'\0',WEBUI_LEN_PARM);
memset(webui->uri_cmd2,'\0',WEBUI_LEN_PARM);
memset(webui->uri_parm1,'\0',WEBUI_LEN_PARM);
memset(webui->uri_value1,'\0',WEBUI_LEN_PARM);
memset(webui->uri_parm2,'\0',WEBUI_LEN_PARM);
memset(webui->uri_value2,'\0',WEBUI_LEN_PARM);
}
static int webu_parseurl(struct webui_ctx *webui) {
int retcd, parm_len, last_slash;
char *st_pos, *en_pos;
retcd = 0;
MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, "Sent url: %s",webui->url);
webu_parseurl_reset(webui);
if (webui->url == NULL) {
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, "Invalid url: %s",webui->url);
return -1;
}
if (webui->url[0] != '/'){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, "Invalid url: %s",webui->url);
return -1;
}
webu_url_decode(webui->url, strlen(webui->url));
MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, "Decoded url: %s",webui->url);
/* Home page, nothing else */
if (strlen(webui->url) == 1) return 0;
last_slash = 0;
/* Get thread number */
st_pos = webui->url + 1; /* Move past the first "/" */
if (*st_pos == '-') return -1; /* Never allow a negative thread number */
en_pos = strstr(st_pos,"/");
if (en_pos == NULL){
parm_len = strlen(webui->url);
last_slash = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_THRD) return -1; /* var was malloc'd to WEBUI_LEN_THRD */
snprintf(webui->uri_thread, parm_len,"%s", st_pos);
if (!last_slash){
/* Get cmd1 or action */
st_pos = st_pos + parm_len; /* Move past the thread number */
en_pos = strstr(st_pos,"/");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len ;
last_slash = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */
snprintf(webui->uri_cmd1, parm_len,"%s", st_pos);
}
if (!last_slash){
/* Get cmd2 or action */
st_pos = st_pos + parm_len; /* Move past the first command */
en_pos = strstr(st_pos,"/");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_slash = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */
snprintf(webui->uri_cmd2, parm_len,"%s", st_pos);
}
if ((!strcmp(webui->uri_cmd1,"config") ||
!strcmp(webui->uri_cmd1,"track") ) &&
(strlen(webui->uri_cmd2) > 0)) {
webu_parseurl_parms(webui, st_pos);
}
MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO,
"thread: >%s< cmd1: >%s< cmd2: >%s< parm1:>%s< val1:>%s< parm2:>%s< val2:>%s<"
,webui->uri_thread
,webui->uri_cmd1, webui->uri_cmd2
,webui->uri_parm1, webui->uri_value1
,webui->uri_parm2, webui->uri_value2);
return retcd;
}
static void webu_html_style_navbar(struct webui_ctx *webui) {
/* Write out the style section of the web page */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
" .navbar {\n"
" overflow: hidden;\n"
" background-color: #333;\n"
" font-family: Arial;\n"
" }\n"
" .navbar a {\n"
" float: left;\n"
" font-size: 16px;\n"
" color: white;\n"
" text-align: center;\n"
" padding: 14px 16px;\n"
" text-decoration: none;\n"
" }\n"
" .navbar a:hover, {\n"
" background-color: darkgray;\n"
" }\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
static void webu_html_style_dropdown(struct webui_ctx *webui) {
/* Write out the style section of the web page */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
" .dropdown {\n"
" float: left;\n"
" overflow: hidden;\n"
" }\n"
" .dropdown .dropbtn {\n"
" font-size: 16px;\n"
" border: none;\n"
" outline: none;\n"
" color: white;\n"
" padding: 14px 16px;\n"
" background-color: inherit;\n"
" font-family: inherit;\n"
" margin: 0;\n"
" }\n"
" .dropdown-content {\n"
" display: none;\n"
" position: absolute;\n"
" background-color: #f9f9f9;\n"
" min-width: 160px;\n"
" box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);\n"
" z-index: 1;\n"
" }\n"
" .dropdown-content a {\n"
" float: none;\n"
" color: black;\n"
" padding: 12px 16px;\n"
" text-decoration: none;\n"
" display: block;\n"
" text-align: left;\n"
" }\n"
" .dropdown-content a:hover {\n"
" background-color: lightgray;\n"
" }\n"
" .dropdown:hover .dropbtn {\n"
" background-color: darkgray;\n"
" }\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
static void webu_html_style_input(struct webui_ctx *webui) {
/* Write out the style section of the web page */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
" input , select {\n"
" width: 25%;\n"
" padding: 5px;\n"
" margin: 0;\n"
" display: inline-block;\n"
" border: 1px solid #ccc;\n"
" border-radius: 4px;\n"
" box-sizing: border-box;\n"
" height: 50%;\n"
" font-size: 75%;\n"
" margin-bottom: 5px;\n"
" }\n"
" .frm-input{\n"
" text-align:center;\n"
" }\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
static void webu_html_style_base(struct webui_ctx *webui) {
/* Write out the style section of the web page */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s",
" * {margin: 0; padding: 0; }\n"
" body {\n"
" padding: 0;\n"
" margin: 0;\n"
" font-family: Arial, Helvetica, sans-serif;\n"
" font-size: 16px;\n"
" line-height: 1;\n"
" color: #606c71;\n"
" background-color: #159957;\n"
" background-image: linear-gradient(120deg, #155799, #159957);\n"
" margin-left:0.5% ;\n"
" margin-right:0.5% ;\n"
" width: device-width ;\n"
" }\n"
" img {\n"
" max-width: 100%;\n"
" max-height: 100%;\n"
" height: auto;\n"
" }\n"
" .page-header {\n"
" color: #fff;\n"
" text-align: center;\n"
" margin-top: 0rem;\n"
" margin-bottom: 0rem;\n"
" font-weight: normal;\n"
" }\n");
written = webu_write(webui->client_socket, response, strlen(response));
snprintf(response, sizeof (response),"%s",
" .page-header h4 {\n"
" height: 2px;\n"
" padding: 0;\n"
" margin: 1rem 0;\n"
" border: 0;\n"
" }\n"
" .main-content {\n"
" background-color: #000000;\n"
" text-align: center;\n"
" margin-top: 0rem;\n"
" margin-bottom: 0rem;\n"
" font-weight: normal;\n"
" font-size: 0.90em;\n"
" }\n"
" .header-right{\n"
" float: right;\n"
" color: white;\n"
" }\n"
" .header-center {\n"
" text-align: center;\n"
" color: white;\n"
" margin-top: 10px;\n"
" margin-bottom: 10px;\n"
" }\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
static void webu_html_style(struct webui_ctx *webui) {
/* Write out the style section of the web page */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s", " <style>\n");
written = webu_write(webui->client_socket, response, strlen(response));
webu_html_style_base(webui);
webu_html_style_navbar(webui);
webu_html_style_input(webui);
webu_html_style_dropdown(webui);
snprintf(response, sizeof (response),"%s", " </style>\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}
static void webu_html_head(struct webui_ctx *webui) {
/* Write out the header section of the web page */
ssize_t written;
char response[WEBUI_LEN_RESP];
snprintf(response, sizeof (response),"%s","<head>\n"
" <meta charset=\"UTF-8\">\n"
" <title>Motion</title>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
written = webu_write(webui->client_socket, response, strlen(response));
webu_html_style(webui);
snprintf(response, sizeof (response),"%s", "</head>\n");
written = webu_write(webui->client_socket, response, strlen(response));
if (written <= 0) MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,"Error writing");
}