Repository navigation
Expand file tree
/
Copy pathweb.h
More file actions
758 lines (626 loc) · 20.9 KB
/
Copy pathweb.h
File metadata and controls
758 lines (626 loc) · 20.9 KB
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
/* State and tiny HTTP server for the KeepAwake control page, shared by the
PS5 and PS4 builds.
Keep Awake is either on (resetting the idle timer) or off (payload still
running, so the page can turn it back on). When on it runs in one of
three modes:
always no time limit
timer for a set time, then it turns itself off
auto only while the network is busy (download, FTP, PKG install),
plus a quiet period after it stops
Routes:
GET / the control page (src/index.html)
GET /status JSON status
POST /on keep the console awake with no time limit
POST /on?minutes=N keep it awake for N minutes, then turn off
POST /on?auto=1 keep it awake only while transferring
POST /off stop keeping it awake, but stay running
POST /quit end the payload
POST /settings?open_on_start=0|1&auto_threshold_kb=N&auto_quiet_minutes=N
change any of the settings (saved to
KEEPAWAKE_SETTINGS)
POSTs that carry an Origin header from another host are refused, so a web
page open elsewhere on the network can't drive the console.
The platform source defines WEB_CONSOLE ("PS5" or "PS4") before including
this file and implements the functions declared below. Client sockets must
be non-blocking. */
#include "index_html.h"
#define KEEPAWAKE_PORT 9031
#define KEEPAWAKE_TICK_SECONDS 10
#define WEB_AGAIN -1
#define WEB_ERROR -2
// Requests are handled one at a time, so don't let a slow client hold on long.
#define WEB_TIMEOUT_MS 1000
#define WEB_POLL_MS 10
/* Bytes read, 0 if the client closed, WEB_AGAIN if no data yet, or WEB_ERROR. */
static int web_recv(int fd, void *buf, size_t len);
/* Bytes sent, WEB_AGAIN if the socket is full, or WEB_ERROR. */
static int web_send(int fd, const void *buf, size_t len);
static void web_sleep_ms(int ms);
/* Time since the payload started. */
static long long web_uptime(void);
static long long web_uptime_ms(void);
/* The console's LAN IP address, or an empty string if unknown. */
static void web_get_ip(char *buf, size_t size);
/* Bytes received and sent on all non-loopback interfaces. Returns -1 if the
counters can't be read. */
static int web_net_bytes(unsigned long long *rx, unsigned long long *tx);
/* Show a toast on the console. */
static void web_notify(const char *msg);
#define KA_MAX_MINUTES (7 * 24 * 60)
#ifndef KEEPAWAKE_SETTINGS
#define KEEPAWAKE_SETTINGS "/data/keepawake.cfg"
#endif
static int ka_active = 1; // resetting the idle timer?
static long long ka_changed = 0; // web_uptime() when on/off last changed
static long long ka_next_tick = 0; // web_uptime() when the next tick is due
static long long ka_until = 0; // web_uptime() when the timer ends, 0 if none
static long long ka_minutes = 0; // length of the current timer
static int ka_auto = 0; // on only while transferring?
// Settings, saved to KEEPAWAKE_SETTINGS.
static int ka_open_on_start = 1; // open the page in the console's browser at start
static long long ka_threshold_kb = 500; // speed that counts as transferring, KB/s
static long long ka_quiet_minutes = 5; // stay awake this long after a transfer stops
#define KA_MIN_THRESHOLD_KB 10
#define KA_MAX_THRESHOLD_KB (100 * 1024)
#define KA_MIN_QUIET_MINUTES 1
#define KA_MAX_QUIET_MINUTES 120
/* Find "key=<number>" at the start of a line. Returns 1 if found. */
static int
ka_cfg_number(const char *buf, const char *key, long long *value) {
const char *p = buf;
size_t k = strlen(key);
long long n = 0;
int digits = 0;
// libPS4's strstr() takes plain char pointers.
while((p = strstr((char*)p, (char*)key))) {
if((p == buf || p[-1] == '\n') && p[k] == '=') {
for(p += k + 1; *p >= '0' && *p <= '9'; p++) {
if(++digits > 9) {
return 0;
}
n = n * 10 + (*p - '0');
}
if(digits) {
*value = n;
}
return digits > 0;
}
p += k;
}
return 0;
}
static void
ka_load_settings(void) {
char buf[256];
long long v;
int fd;
int n;
if((fd = open(KEEPAWAKE_SETTINGS, O_RDONLY, 0)) < 0) {
return; // no file yet: keep the defaults
}
n = read(fd, buf, sizeof buf - 1);
close(fd);
if(n <= 0) {
return;
}
buf[n] = 0;
if(ka_cfg_number(buf, "open_on_start", &v) && v <= 1) {
ka_open_on_start = (int)v;
}
if(ka_cfg_number(buf, "auto_threshold_kb", &v) &&
v >= KA_MIN_THRESHOLD_KB && v <= KA_MAX_THRESHOLD_KB) {
ka_threshold_kb = v;
}
if(ka_cfg_number(buf, "auto_quiet_minutes", &v) &&
v >= KA_MIN_QUIET_MINUTES && v <= KA_MAX_QUIET_MINUTES) {
ka_quiet_minutes = v;
}
}
static int
ka_save_settings(void) {
char buf[160];
int fd;
int n;
int ok;
n = snprintf(buf, sizeof buf,
"open_on_start=%d\n"
"auto_threshold_kb=%lld\n"
"auto_quiet_minutes=%lld\n",
ka_open_on_start, ka_threshold_kb, ka_quiet_minutes);
if((fd = open(KEEPAWAKE_SETTINGS, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) {
return -1;
}
ok = write(fd, buf, n) == n;
close(fd);
return ok ? 0 : -1;
}
/* Transfer detection: the interface byte counters are sampled every
KA_SAMPLE_MS and the speed is the change over the last KA_SAMPLES samples
(about 10 seconds), which smooths out bursty traffic. The network counts
as busy while either direction is at or above the threshold. */
#define KA_SAMPLES 6
#define KA_SAMPLE_MS 2000
static struct {
long long ms;
unsigned long long rx;
unsigned long long tx;
} ka_samples[KA_SAMPLES];
static int ka_nsamples = 0;
static int ka_sample_pos = 0; // where the next sample goes
static long long ka_next_sample_ms = 0;
static int ka_net_ok = 0; // counters readable?
static long long ka_rx_rate = 0; // bytes per second
static long long ka_tx_rate = 0;
static long long ka_last_busy = -1; // web_uptime() when last busy, -1 if never
static int ka_auto_awake = 0; // auto mode currently keeping it awake?
static int
ka_transferring(void) {
long long rate = ka_rx_rate > ka_tx_rate ? ka_rx_rate : ka_tx_rate;
return ka_net_ok && rate >= ka_threshold_kb * 1024;
}
static void
ka_sample(void) {
unsigned long long rx;
unsigned long long tx;
long long ms = web_uptime_ms();
int newest;
int oldest;
if(ms < ka_next_sample_ms) {
return;
}
ka_next_sample_ms = ms + KA_SAMPLE_MS;
if(web_net_bytes(&rx, &tx) != 0) {
ka_net_ok = 0;
ka_rx_rate = ka_tx_rate = 0;
return;
}
ka_net_ok = 1;
// Counters that go backwards were reset (or wrapped): start over.
if(ka_nsamples) {
newest = (ka_sample_pos + KA_SAMPLES - 1) % KA_SAMPLES;
if(rx < ka_samples[newest].rx || tx < ka_samples[newest].tx) {
ka_nsamples = 0;
}
}
ka_samples[ka_sample_pos].ms = ms;
ka_samples[ka_sample_pos].rx = rx;
ka_samples[ka_sample_pos].tx = tx;
ka_sample_pos = (ka_sample_pos + 1) % KA_SAMPLES;
if(ka_nsamples < KA_SAMPLES) {
ka_nsamples++;
}
newest = (ka_sample_pos + KA_SAMPLES - 1) % KA_SAMPLES;
oldest = (ka_sample_pos + KA_SAMPLES - ka_nsamples) % KA_SAMPLES;
if(ka_nsamples >= 2 && ka_samples[newest].ms > ka_samples[oldest].ms) {
long long span = ka_samples[newest].ms - ka_samples[oldest].ms;
ka_rx_rate = (long long)(ka_samples[newest].rx - ka_samples[oldest].rx) * 1000 / span;
ka_tx_rate = (long long)(ka_samples[newest].tx - ka_samples[oldest].tx) * 1000 / span;
} else {
ka_rx_rate = ka_tx_rate = 0;
}
if(ka_transferring()) {
ka_last_busy = web_uptime();
}
}
/* Seconds left of the quiet period since the last busy sample. */
static long long
ka_quiet_left(void) {
long long left;
if(ka_last_busy < 0) {
return 0;
}
left = ka_last_busy + ka_quiet_minutes * 60 - web_uptime();
return left > 0 ? left : 0;
}
/* "1 hour 30 minutes", "2 hours", "45 minutes" */
static void
ka_format_minutes(char *buf, size_t size, long long minutes) {
long long h = minutes / 60;
long long m = minutes % 60;
if(h && m) {
snprintf(buf, size, "%lld hour%s %lld minute%s",
h, h == 1 ? "" : "s", m, m == 1 ? "" : "s");
} else if(h) {
snprintf(buf, size, "%lld hour%s", h, h == 1 ? "" : "s");
} else {
snprintf(buf, size, "%lld minute%s", m, m == 1 ? "" : "s");
}
}
static void
ka_set_on(void) {
if(!ka_active) {
ka_active = 1;
ka_changed = web_uptime();
ka_next_tick = 0;
}
}
/* Turn on for the given minutes, or with no time limit (0). Also changes or
removes the timer while already on. */
static void
ka_turn_on(long long minutes) {
int was_plain_on = ka_active && !ka_until && !ka_auto;
char duration[64];
char msg[128];
ka_set_on();
ka_auto = 0;
ka_auto_awake = 0;
ka_until = minutes ? web_uptime() + minutes * 60 : 0;
ka_minutes = minutes;
if(minutes) {
ka_format_minutes(duration, sizeof duration, minutes);
snprintf(msg, sizeof msg, "Keep Awake on for %s", duration);
web_notify(msg);
} else if(!was_plain_on) {
web_notify("Keep Awake turned on\nNo time limit");
}
}
static void
ka_turn_on_auto(void) {
if(ka_active && ka_auto) {
return;
}
ka_set_on();
ka_auto = 1;
ka_until = 0;
ka_minutes = 0;
ka_auto_awake = ka_quiet_left() > 0;
ka_next_tick = 0;
web_notify(ka_auto_awake ?
"Keep Awake on while transferring\nTransfer in progress" :
"Keep Awake on while transferring\nWaiting for a transfer");
}
static void
ka_turn_off(const char *msg) {
if(!ka_active) {
return;
}
ka_active = 0;
ka_until = 0;
ka_auto = 0;
ka_auto_awake = 0;
ka_changed = web_uptime();
web_notify(msg);
}
/* Does the request line start with "<method> <path>", followed by a query or
the end of the path? */
static int
web_route(const char *req, size_t len, const char *method, const char *path) {
size_t m = strlen(method);
size_t p = strlen(path);
char next;
if(len < m + 1 + p + 1 || memcmp(req, method, m) != 0 || req[m] != ' ' ||
memcmp(req + m + 1, path, p) != 0) {
return 0;
}
next = req[m + 1 + p];
return next == ' ' || next == '?';
}
/* Read a whole-number query parameter. Returns 1 and sets *value if it's
there and valid, 0 if absent, -1 if invalid. */
static int
web_query_number(const char *req, size_t len, const char *key, long long *value) {
size_t k = strlen(key);
size_t i;
long long n = 0;
int digits = 0;
// The query runs from '?' to the space before "HTTP/1.x".
for(i = 0; i < len && req[i] != '?' && req[i] != '\r'; i++);
if(i >= len || req[i] != '?') {
return 0;
}
while(i < len && req[i] != ' ' && req[i] != '\r') {
i++; // skip '?' or '&'
if(i + k < len && memcmp(req + i, key, k) == 0 && req[i + k] == '=') {
for(i += k + 1; i < len && req[i] >= '0' && req[i] <= '9'; i++) {
if(++digits > 9) {
return -1;
}
n = n * 10 + (req[i] - '0');
}
if(!digits || (i < len && req[i] != '&' && req[i] != ' ')) {
return -1;
}
*value = n;
return 1;
}
while(i < len && req[i] != '&' && req[i] != ' ' && req[i] != '\r') {
i++;
}
}
return 0;
}
static int
web_send_all(int fd, const void *buf, size_t len) {
const char *p = buf;
int waited = 0;
int rc;
while(len > 0) {
rc = web_send(fd, p, len);
if(rc == WEB_AGAIN) {
if((waited += WEB_POLL_MS) > WEB_TIMEOUT_MS) {
return -1;
}
web_sleep_ms(WEB_POLL_MS);
continue;
}
if(rc <= 0) {
return -1;
}
p += rc;
len -= rc;
}
return 0;
}
static void
web_respond(int fd, const char *status, const char *type,
const void *body, size_t len) {
char head[256];
int n;
n = snprintf(head, sizeof head,
"HTTP/1.1 %s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Cache-Control: no-store\r\n"
"Connection: close\r\n"
"\r\n", status, type, (int)len);
if(web_send_all(fd, head, n) == 0 && len > 0) {
web_send_all(fd, body, len);
}
}
static void
web_respond_json(int fd, const char *status, const char *json) {
web_respond(fd, status, "application/json", json, strlen(json));
}
static void
web_respond_status(int fd) {
long long now = web_uptime();
const char *mode = !ka_active ? "off" : ka_auto ? "auto" : ka_until ? "timer" : "always";
char remaining[24];
char timer[24];
char quiet[24];
char json[768];
char ip[32];
if(ka_active && ka_until) {
snprintf(remaining, sizeof remaining, "%lld", ka_until > now ? ka_until - now : 0);
snprintf(timer, sizeof timer, "%lld", ka_minutes);
} else {
snprintf(remaining, sizeof remaining, "null");
snprintf(timer, sizeof timer, "null");
}
// In auto mode while awake but no longer transferring: time until it rests.
if(ka_active && ka_auto && ka_auto_awake && !ka_transferring()) {
snprintf(quiet, sizeof quiet, "%lld", ka_quiet_left());
} else {
snprintf(quiet, sizeof quiet, "null");
}
web_get_ip(ip, sizeof ip);
snprintf(json, sizeof json,
"{\"version\":\"%s\",\"console\":\"%s\",\"active\":%s,\"mode\":\"%s\","
"\"uptime\":%lld,\"since\":%lld,\"timer\":%s,\"remaining\":%s,"
"\"net\":%s,\"rx_rate\":%lld,\"tx_rate\":%lld,\"transferring\":%s,"
"\"auto_awake\":%s,\"quiet_left\":%s,"
"\"interval\":%d,\"ip\":\"%s\",\"port\":%d,\"open_on_start\":%s,"
"\"auto_threshold_kb\":%lld,\"auto_quiet_minutes\":%lld}",
KEEPAWAKE_VERSION, WEB_CONSOLE, ka_active ? "true" : "false", mode,
now, now - ka_changed, timer, remaining,
ka_net_ok ? "true" : "false", ka_rx_rate, ka_tx_rate,
ka_transferring() ? "true" : "false",
ka_active && ka_auto && ka_auto_awake ? "true" : "false", quiet,
KEEPAWAKE_TICK_SECONDS, ip, KEEPAWAKE_PORT,
ka_open_on_start ? "true" : "false",
ka_threshold_kb, ka_quiet_minutes);
web_respond_json(fd, "200 OK", json);
}
/* POST /settings: any of open_on_start, auto_threshold_kb and
auto_quiet_minutes. Nothing changes unless every given value is valid. */
static void
web_handle_settings(int fd, const char *req, size_t len) {
long long open_on_start = ka_open_on_start;
long long threshold = ka_threshold_kb;
long long quiet = ka_quiet_minutes;
int old_open_on_start = ka_open_on_start;
long long old_threshold = ka_threshold_kb;
long long old_quiet = ka_quiet_minutes;
int got_open = web_query_number(req, len, "open_on_start", &open_on_start);
int got_threshold = web_query_number(req, len, "auto_threshold_kb", &threshold);
int got_quiet = web_query_number(req, len, "auto_quiet_minutes", &quiet);
if(got_open < 0 || got_threshold < 0 || got_quiet < 0 ||
(got_open + got_threshold + got_quiet) == 0 ||
open_on_start > 1 ||
threshold < KA_MIN_THRESHOLD_KB || threshold > KA_MAX_THRESHOLD_KB ||
quiet < KA_MIN_QUIET_MINUTES || quiet > KA_MAX_QUIET_MINUTES) {
web_respond_json(fd, "400 Bad Request",
"{\"error\":\"expected open_on_start=0|1, "
"auto_threshold_kb=10-102400 or auto_quiet_minutes=1-120\"}");
return;
}
ka_open_on_start = (int)open_on_start;
ka_threshold_kb = threshold;
ka_quiet_minutes = quiet;
if(ka_save_settings() != 0) {
// Keep running with what's saved, so the page and the file agree.
ka_open_on_start = old_open_on_start;
ka_threshold_kb = old_threshold;
ka_quiet_minutes = old_quiet;
web_respond_json(fd, "500 Internal Server Error",
"{\"error\":\"couldn't save the settings\"}");
} else {
web_respond_status(fd);
}
}
/* Read the request headers, giving up after WEB_TIMEOUT_MS. Returns the
bytes read. */
static size_t
web_read_request(int fd, char *buf, size_t size) {
size_t len = 0;
int waited = 0;
int rc;
while(len < size - 1) {
rc = web_recv(fd, buf + len, size - 1 - len);
if(rc == WEB_AGAIN) {
if((waited += WEB_POLL_MS) > WEB_TIMEOUT_MS) {
break;
}
web_sleep_ms(WEB_POLL_MS);
continue;
}
if(rc <= 0) {
break;
}
len += rc;
buf[len] = 0;
if(strstr(buf, "\r\n\r\n")) {
break;
}
}
buf[len] = 0;
return len;
}
/* Does the line at p start with "<name>:", ignoring case? */
static int
web_header_is(const char *p, const char *name) {
for(; *name; p++, name++) {
if((*p | 0x20) != (*name | 0x20)) {
return 0;
}
}
return *p == ':';
}
/* Copy the value of a request header into out. Returns 1 if it's present. */
static int
web_header(const char *req, const char *name, char *out, size_t size) {
const char *p = req;
size_t i;
while((p = strstr((char*)p, (char*)"\r\n"))) {
p += 2;
if(web_header_is(p, name)) {
for(p += strlen(name) + 1; *p == ' '; p++);
for(i = 0; *p && *p != '\r' && i < size - 1; i++, p++) {
out[i] = *p;
}
out[i] = 0;
return 1;
}
}
return 0;
}
/* A browser sends Origin on cross-site POSTs. Refuse one that doesn't match
the host it was sent to; curl and the other instance send none. */
static int
web_origin_ok(const char *req) {
char origin[128];
char host[128];
const char *o;
if(!web_header(req, "Origin", origin, sizeof origin)) {
return 1;
}
o = strstr(origin, "://");
return o && web_header(req, "Host", host, sizeof host) &&
strlen(o + 3) == strlen(host) && memcmp(o + 3, host, strlen(host)) == 0;
}
/* Handle one client connection. Returns 1 if the payload should quit. */
static int
web_handle_client(int fd) {
char req[2048];
long long minutes = 0;
long long auto_arg = 0;
size_t len;
int minutes_rc;
int auto_rc;
if(!(len = web_read_request(fd, req, sizeof req))) {
return 0;
}
if(memcmp(req, "POST ", 5) == 0 && !web_origin_ok(req)) {
web_respond_json(fd, "403 Forbidden", "{\"error\":\"cross-origin request\"}");
return 0;
}
if(web_route(req, len, "GET", "/") ||
web_route(req, len, "GET", "/index.html")) {
web_respond(fd, "200 OK", "text/html; charset=utf-8",
index_html, index_html_len);
} else if(web_route(req, len, "GET", "/status")) {
web_respond_status(fd);
} else if(web_route(req, len, "POST", "/on")) {
minutes_rc = web_query_number(req, len, "minutes", &minutes);
auto_rc = web_query_number(req, len, "auto", &auto_arg);
if(minutes_rc < 0 || minutes > KA_MAX_MINUTES || auto_rc < 0 ||
auto_arg > 1 || (auto_arg && minutes)) {
web_respond_json(fd, "400 Bad Request",
"{\"error\":\"use minutes=0-10080 or auto=1\"}");
} else {
if(auto_arg) {
ka_turn_on_auto();
} else {
ka_turn_on(minutes);
}
web_respond_status(fd);
}
} else if(web_route(req, len, "POST", "/off")) {
ka_turn_off("Keep Awake turned off");
web_respond_status(fd);
} else if(web_route(req, len, "POST", "/quit")) {
web_respond_json(fd, "200 OK", "{\"quit\":true}");
return 1;
} else if(web_route(req, len, "POST", "/settings")) {
web_handle_settings(fd, req, len);
} else if(web_route(req, len, "GET", "/on") ||
web_route(req, len, "GET", "/off") ||
web_route(req, len, "GET", "/quit") ||
web_route(req, len, "GET", "/settings")) {
// Only POST changes anything, so link previews and prefetching can't.
web_respond_json(fd, "405 Method Not Allowed",
"{\"error\":\"use POST\"}");
} else if(web_route(req, len, "GET", "/favicon.ico")) {
web_respond(fd, "204 No Content", "text/plain", "", 0);
} else {
web_respond_json(fd, "404 Not Found", "{\"error\":\"not found\"}");
}
return 0;
}
/* Call from the main loop. Samples the network, resets the idle timer when
it's due and handles the timer and auto mode. Returns the seconds until it
next needs calling. */
static int
ka_tick(void) {
long long now = web_uptime();
long long wait;
int awake;
ka_sample();
if(ka_active && ka_until && now >= ka_until) {
ka_turn_off("Keep Awake timer finished\nTurned off");
}
if(!ka_active) {
return KA_SAMPLE_MS / 1000;
}
if(ka_auto) {
awake = ka_quiet_left() > 0;
if(awake && !ka_auto_awake) {
ka_next_tick = 0;
web_notify("Transfer detected\nKeeping the " WEB_CONSOLE " awake");
} else if(!awake && ka_auto_awake) {
web_notify("Transfers finished\nThe " WEB_CONSOLE " can rest again");
}
ka_auto_awake = awake;
if(!awake) {
return KA_SAMPLE_MS / 1000;
}
}
if(now >= ka_next_tick) {
sceSystemServicePowerTick();
ka_next_tick = now + KEEPAWAKE_TICK_SECONDS;
}
wait = ka_next_tick - now;
if(ka_until && ka_until - now < wait) {
wait = ka_until - now;
}
if(wait > KA_SAMPLE_MS / 1000) {
wait = KA_SAMPLE_MS / 1000;
}
return wait > 0 ? (int)wait : 1;
}
/* What a new instance sends to make the running one quit. */
static const char web_quit_request[] =
"POST /quit HTTP/1.0\r\n"
"Host: localhost\r\n"
"Content-Length: 0\r\n"
"\r\n";