Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8e52075636 | ||
|
|
7200f079bf | ||
|
|
d155db81de | ||
|
|
25e3bbf039 | ||
|
|
eecf3d854b | ||
|
|
7c2499fc06 | ||
|
|
1aa605c620 | ||
|
|
66287ad6c1 | ||
|
|
9a19485454 | ||
|
|
7d67d578fd | ||
|
|
960d3c7880 | ||
|
|
9eabf3fa81 | ||
|
|
89beadd2ce | ||
|
|
6ecb6c1500 | ||
|
|
51bc5f70c5 | ||
|
|
be62ac9c14 | ||
|
|
161c34384d | ||
|
|
89784ab90a | ||
|
|
d83e547fa9 | ||
|
|
30d86928d5 | ||
|
|
12c32d09fb | ||
|
|
ae6fd6aa35 | ||
|
|
72d3a49352 | ||
|
|
87b62390b7 | ||
|
|
6c93446a1f | ||
|
|
83c106a857 | ||
|
|
facb2846fd | ||
|
|
596c70d3dc | ||
|
|
7beff0201d | ||
|
|
a4c9d1ecb7 | ||
|
|
b7a79a556a | ||
|
|
03ab9e4e0d | ||
|
|
7e855f5c8f | ||
|
|
9e510d5831 | ||
|
|
1689a0d56b | ||
|
|
9ff774e7df | ||
|
|
c78ad0b574 | ||
|
|
117aeaa15d | ||
|
|
e135743efa | ||
|
|
2c1324dc0a | ||
|
|
c7e09bd7b4 | ||
|
|
86824795a0 | ||
|
|
1b0636c2d2 | ||
|
|
2f9d2bbdf6 | ||
|
|
21a1b2c15c | ||
|
|
bdef3d6afd | ||
|
|
a882aab30e | ||
|
|
788b577d65 | ||
|
|
71a22adc06 | ||
|
|
7d1ef2ffe8 | ||
|
|
55f5f74658 | ||
|
|
e1fd82bc46 | ||
|
|
49b8480bfb | ||
|
|
b2ab484883 | ||
|
|
4c3b97be7d | ||
|
|
60e1b64455 | ||
|
|
6bcd02bb1c | ||
|
|
66f302a613 | ||
|
|
1716af6c72 | ||
|
|
749326c83a | ||
|
|
9657577c00 | ||
|
|
9f5216d259 | ||
|
|
c8826e8f9c | ||
|
|
25bc13ef1e | ||
|
|
4d123a15f9 | ||
|
|
2cf1b09767 | ||
|
|
abfd81cb9d | ||
|
|
3895678bee | ||
|
|
e0a1feafe0 | ||
|
|
c75eb24eb8 | ||
|
|
c3aae185df | ||
|
|
2e87783246 | ||
|
|
fbab767b9f | ||
|
|
7bc38b5eaa | ||
|
|
b93456f512 | ||
|
|
ddb700eee5 | ||
|
|
1052ee0766 | ||
|
|
29534218d7 | ||
|
|
d7abd53062 | ||
|
|
aede72fa37 | ||
|
|
b0a5165b44 | ||
|
|
4919f792f2 | ||
|
|
d5927f5d50 | ||
|
|
e68330e045 | ||
|
|
b5d5b3763f | ||
|
|
b6a6687c1e | ||
|
|
3843b670a5 | ||
|
|
249efdd378 | ||
|
|
18ce1669fc | ||
|
|
e8d3b8030e | ||
|
|
a752abf318 | ||
|
|
d3ebcaf5d2 | ||
|
|
0abb8322d9 | ||
|
|
99ed58ec4d | ||
|
|
04450ec659 | ||
|
|
82a378e519 | ||
|
|
29d31ba919 | ||
|
|
4519ea568d | ||
|
|
9f88f31f01 | ||
|
|
4bdea2b467 | ||
|
|
2252a74cd7 | ||
|
|
d1fa7b88e0 | ||
|
|
9ecf42578c | ||
|
|
754fc25924 | ||
|
|
d9551c75b1 | ||
|
|
8d45cf6972 | ||
|
|
f176e1ae59 | ||
|
|
fa8f62fb5d | ||
|
|
0c142f20d7 | ||
|
|
b641fc6b79 | ||
|
|
1252a61479 | ||
|
|
5f631c0b96 | ||
|
|
348c579521 | ||
|
|
7ce9cdf0b9 | ||
|
|
09beadb98a | ||
|
|
5b265a76bd | ||
|
|
c5a4a1c6f2 | ||
|
|
88ada54045 | ||
|
|
c5b1c08d05 | ||
|
|
a0765ade6b | ||
|
|
54250f5fbf | ||
|
|
f3f397f31e | ||
|
|
d806ed5d5f | ||
|
|
37e9a2ee95 | ||
|
|
7ad6a08002 | ||
|
|
90686ee5c9 | ||
|
|
85c5577955 | ||
|
|
30d4a1ad0f | ||
|
|
1b551741b8 | ||
|
|
369f44adfa | ||
|
|
763f4551d5 | ||
|
|
42d7865e14 | ||
|
|
cf62702f08 | ||
|
|
14ccd78782 | ||
|
|
6c4b5efd98 | ||
|
|
05a43c563c | ||
|
|
94d2e86b60 | ||
|
|
d1b8f9e01f | ||
|
|
228acefa36 | ||
|
|
d1d8a77bf6 | ||
|
|
7ea3293aba | ||
|
|
082a498afd | ||
|
|
866c987409 | ||
|
|
1d0e65d17a | ||
|
|
136a37ef73 | ||
|
|
0ce49b5c71 | ||
|
|
490ad3834b | ||
|
|
bcc62c96c8 | ||
|
|
15d6cb3065 | ||
|
|
a490660b38 | ||
|
|
72019b7427 | ||
|
|
21c0024a2f | ||
|
|
d7a02d13e4 | ||
|
|
cb220837b9 | ||
|
|
ddaae51d42 | ||
|
|
629e55f97d | ||
|
|
909ebad276 | ||
|
|
cb7743fa39 | ||
|
|
6197842bca | ||
|
|
77d461dcbf | ||
|
|
47b6060910 | ||
|
|
47d3fe5af6 | ||
|
|
6691a2fdad | ||
|
|
746f56f42a | ||
|
|
87de05881c | ||
|
|
f2fba1d200 | ||
|
|
34d4fb8cf0 | ||
|
|
be64f81eee | ||
|
|
9fe1c93c9b | ||
|
|
a4b6d58b24 | ||
|
|
86f391c701 | ||
|
|
36de38bd11 | ||
|
|
a308872c88 | ||
|
|
a369160296 | ||
|
|
0a09b46c94 | ||
|
|
b0a1cd7b0d | ||
|
|
73a2b92278 | ||
|
|
c3bb5bd89e | ||
|
|
d22ff2fce7 | ||
|
|
fee2980d95 | ||
|
|
d6693ed360 | ||
|
|
4880069775 | ||
|
|
5d217a1262 | ||
|
|
f778d22eac | ||
|
|
3aa76dcaec | ||
|
|
4680573a6b | ||
|
|
95fcb261eb | ||
|
|
5867c67da2 | ||
|
|
80d74e9e34 | ||
|
|
43a0b500f5 | ||
|
|
3d53ed17fb | ||
|
|
4ea452307e | ||
|
|
e61f20f1ed | ||
|
|
ca11f5ab49 | ||
|
|
000d24d31a | ||
|
|
ef92f2e9c5 | ||
|
|
4e89eac8dd | ||
|
|
b015e33ea5 | ||
|
|
96e762d4a1 | ||
|
|
359060a151 | ||
|
|
af1aff7fbb | ||
|
|
517ef216c1 | ||
|
|
734c13cdf5 | ||
|
|
1bf3ad2426 | ||
|
|
884b9a4813 | ||
|
|
7153690016 | ||
|
|
f00b3a2b3a | ||
|
|
c2b5e4f3b8 | ||
|
|
bc15e9ce9a | ||
|
|
70dc6d8dc9 | ||
|
|
10b11b03a8 | ||
|
|
cb602f178c | ||
|
|
464113c5ae | ||
|
|
c34f6c54e7 | ||
|
|
8fb58ebbca | ||
|
|
dce60c6dbb | ||
|
|
f668d08804 | ||
|
|
c41a22195c | ||
|
|
900c05213b | ||
|
|
c5850e4b05 | ||
|
|
ff4649fe42 | ||
|
|
1a38e91042 | ||
|
|
b6196371bb | ||
|
|
d5beb7ba3f | ||
|
|
19acf23c49 | ||
|
|
5cd3291fec | ||
|
|
b3e0f780c3 | ||
|
|
2bdbfc27f6 | ||
|
|
3fc61a3edc | ||
|
|
12339bbf4c | ||
|
|
1fb9f922b0 | ||
|
|
b93bc02ab0 | ||
|
|
747e393a54 | ||
|
|
4e8660a21f | ||
|
|
3fe77fa9a6 | ||
|
|
b4fbaf7c13 | ||
|
|
9884241df8 | ||
|
|
63d7a4fa45 | ||
|
|
8f4b6c41a2 | ||
|
|
4493d65ec9 | ||
|
|
e5910da300 | ||
|
|
c277a1d878 | ||
|
|
7820ad3a59 | ||
|
|
23533b83a4 | ||
|
|
ab650ec5c9 | ||
|
|
8f36597048 | ||
|
|
d0213ee90a | ||
|
|
1e9fabe1ca | ||
|
|
148d3f0562 | ||
|
|
c3057df3f5 | ||
|
|
0b2e474d52 | ||
|
|
95a36b1759 | ||
|
|
86ffa3a3ea | ||
|
|
69e2b2985d | ||
|
|
c431904b56 | ||
|
|
c2a10601b9 | ||
|
|
e9a8e5468d | ||
|
|
22e602debb | ||
|
|
455cec67a5 | ||
|
|
e2dfac946d | ||
|
|
c198d2e5f1 | ||
|
|
9a70abef24 | ||
|
|
53e1c469a2 | ||
|
|
139c2061a8 | ||
|
|
15b361fbac | ||
|
|
3130e59244 | ||
|
|
93286836f9 | ||
|
|
91b57aa9a8 | ||
|
|
220231e647 | ||
|
|
c438bb0bc5 | ||
|
|
95afa323c0 | ||
|
|
bc56353a79 | ||
|
|
1ffccdb7b2 | ||
|
|
e79fe4b8f0 | ||
|
|
aaf1fc3a4a | ||
|
|
8b42353039 | ||
|
|
05c687e251 | ||
|
|
492b8f6950 | ||
|
|
8ccee82059 | ||
|
|
b99a94a652 | ||
|
|
41e5d7e471 | ||
|
|
1adea37c45 | ||
|
|
98f6c8aa4f | ||
|
|
341ae53e85 | ||
|
|
291fc024cd | ||
|
|
f63d833a33 | ||
|
|
ab035901f8 | ||
|
|
20f54059ad | ||
|
|
563c31303e | ||
|
|
421e099f2b | ||
|
|
955d4a3129 | ||
|
|
b84981ad03 | ||
|
|
777b8e94e2 | ||
|
|
6df642623a | ||
|
|
0a3e186f11 | ||
|
|
c2d83a9db0 | ||
|
|
f33d3a4e02 | ||
|
|
86b8a56b05 | ||
|
|
ac6ec15aed | ||
|
|
31632c6361 | ||
|
|
98c99279c3 | ||
|
|
cdb8d7b692 | ||
|
|
93ac3e19f0 | ||
|
|
6a51af9efe | ||
|
|
9d73b3470b | ||
|
|
1c8449cea6 | ||
|
|
3bbbd41196 | ||
|
|
339bbee0df | ||
|
|
9ba6eb716d | ||
|
|
d949c19c7e | ||
|
|
1299d6118f | ||
|
|
244ff92e78 | ||
|
|
53f34dea04 | ||
|
|
1f5c3fac31 | ||
|
|
7014258c8d | ||
|
|
1614e7ba8d | ||
|
|
43b5a4e735 | ||
|
|
43191db63e | ||
|
|
e89be2e944 | ||
|
|
c2fbff6d0e | ||
|
|
b8eca17c55 | ||
|
|
4650e491df | ||
|
|
3e33e57707 | ||
|
|
c80f4c5313 | ||
|
|
132ecbec2f | ||
|
|
0a90cab44a | ||
|
|
d16dbc197f | ||
|
|
127f27e375 | ||
|
|
6523cc4da0 | ||
|
|
2516da1c89 | ||
|
|
0ae2b82b60 | ||
|
|
dab526936f | ||
|
|
780077d118 | ||
|
|
ad8f1d88d4 | ||
|
|
a3c4a48ef7 | ||
|
|
84a11fe3b1 | ||
|
|
2a3cfc9858 | ||
|
|
b9b20c6494 | ||
|
|
a8a7d2cf7e | ||
|
|
50ecc30b4d | ||
|
|
cd89db5c2f | ||
|
|
1ed99999bd | ||
|
|
4afebdcb2b | ||
|
|
c3d9d17e34 | ||
|
|
7bf5144e62 | ||
|
|
3eb4eb8074 | ||
|
|
ab000c684a | ||
|
|
aba60dadec | ||
|
|
f29457e967 | ||
|
|
8742fac5d8 | ||
|
|
0ab1c7ef76 | ||
|
|
47abc641f1 | ||
|
|
7519b2ef3a | ||
|
|
ce8657f12f | ||
|
|
8e366916d5 | ||
|
|
33f53ab000 | ||
|
|
bd15ad4301 | ||
|
|
e5b3b8ad32 | ||
|
|
e116c48bef | ||
|
|
7d7bb3b388 | ||
|
|
0b4bf13335 | ||
|
|
228984a3fc | ||
|
|
f084ad8008 | ||
|
|
255da78bed | ||
|
|
5d5badfcee | ||
|
|
4cf9f99ae9 | ||
|
|
673f25f75a | ||
|
|
961fb96bb7 | ||
|
|
d6475fcd19 | ||
|
|
ac538f755f | ||
|
|
5b2eb4f0e1 | ||
|
|
10c274c774 | ||
|
|
eb3b81297d | ||
|
|
80f4133bbf | ||
|
|
d10fb9451c | ||
|
|
88ae2ecadb | ||
|
|
85d19dad47 | ||
|
|
c5cc414181 | ||
|
|
17703fe3e8 | ||
|
|
04aeeba2fa | ||
|
|
4e11d5cdbf | ||
|
|
be63c0dc89 | ||
|
|
987bc1d580 | ||
|
|
ddd35c0cba | ||
|
|
c97b615785 | ||
|
|
9ab6d084df | ||
|
|
cf8d0a25aa | ||
|
|
0a0852b576 | ||
|
|
90716417c9 | ||
|
|
101afbc9b8 | ||
|
|
a197646867 | ||
|
|
00d1d2c0e7 | ||
|
|
6e50fe4475 | ||
|
|
7942803a75 | ||
|
|
a8b74a9021 | ||
|
|
27a9a62843 | ||
|
|
9b268b6c77 | ||
|
|
3b3ac48edd | ||
|
|
598e96a0cc | ||
|
|
b5dd0e8093 | ||
|
|
9b24fdfd1d | ||
|
|
8313896a85 | ||
|
|
8193302162 | ||
|
|
6e3e5560d0 | ||
|
|
f0c6630a16 | ||
|
|
1db747da29 | ||
|
|
8065439f96 | ||
|
|
62c896a872 | ||
|
|
2db07dfc3b | ||
|
|
34eb3f1e66 | ||
|
|
9d30e892e9 | ||
|
|
0aaba7a461 | ||
|
|
3d6b568c6e | ||
|
|
2b2816cfe4 | ||
|
|
2fa18b47db | ||
|
|
c8af7cdd27 | ||
|
|
f857f6394f | ||
|
|
97d733e129 | ||
|
|
77e44c7c0e | ||
|
|
abf89d32d9 | ||
|
|
177e05deb2 | ||
|
|
eb5645241c | ||
|
|
202300952c | ||
|
|
c7bea65ae7 | ||
|
|
d5a9000d46 | ||
|
|
f62018451f | ||
|
|
0a93e301c9 | ||
|
|
6fe9cae62a | ||
|
|
38e984a008 | ||
|
|
09e4254718 | ||
|
|
d4b55c4ceb | ||
|
|
7d5479985d | ||
|
|
6517af3dea | ||
|
|
7632a5e4af | ||
|
|
7f20c41dd5 | ||
|
|
66b23b11b0 | ||
|
|
448b44094a | ||
|
|
8bbb44f90c | ||
|
|
0fba87c755 | ||
|
|
fea648496c | ||
|
|
944a6b761c | ||
|
|
f1ae183752 | ||
|
|
a0ece4e969 | ||
|
|
0bb14abbdd | ||
|
|
4c37e49274 | ||
|
|
2a2bc33a89 | ||
|
|
41558bbf64 | ||
|
|
4d0cc95ff4 | ||
|
|
cfd28a4f7a | ||
|
|
5a0b6fc35a | ||
|
|
3e59e1c465 | ||
|
|
ae611a6e55 | ||
|
|
a7a2e3deb1 | ||
|
|
cae0a1cbc0 | ||
|
|
244e5f67d3 | ||
|
|
ff078882d5 | ||
|
|
8d76733fad | ||
|
|
fb3a5a7d90 | ||
|
|
f374ac5f9a | ||
|
|
02707364f0 | ||
|
|
104106b9cf | ||
|
|
474e065a30 | ||
|
|
27094896b1 | ||
|
|
cd48bcfd7d | ||
|
|
5baeba53d7 | ||
|
|
3a3b433849 | ||
|
|
a2192ae483 | ||
|
|
023b55f2e5 | ||
|
|
e4309d472b | ||
|
|
c5452fc2ec | ||
|
|
f78ee9d42e | ||
|
|
c8416cc17d | ||
|
|
cf0a969a65 | ||
|
|
06670d3401 | ||
|
|
acfc66cd60 | ||
|
|
6f140202ad | ||
|
|
cfe77a3d58 | ||
|
|
a0527a7f0e | ||
|
|
c90cffa688 | ||
|
|
d7a633bfa3 | ||
|
|
0ea3cdd228 | ||
|
|
ddef065493 | ||
|
|
ac857d18db | ||
|
|
663e928d65 | ||
|
|
189cd8c5f6 | ||
|
|
692f436b3d | ||
|
|
2e65ce8272 | ||
|
|
a35e5bd966 | ||
|
|
83fe5a17f3 | ||
|
|
49bbfaf130 | ||
|
|
cd9c7dcbec | ||
|
|
f868fc861b | ||
|
|
84907d5bc4 | ||
|
|
1cee8a16b9 | ||
|
|
57e32b87c7 | ||
|
|
6926655513 | ||
|
|
8354319e72 | ||
|
|
f9596cc1db | ||
|
|
b2e2ad9a45 | ||
|
|
a07fb2be51 | ||
|
|
7f259f2a79 | ||
|
|
7c0493071f | ||
|
|
c60fe0c6f6 | ||
|
|
8ce34af9f3 | ||
|
|
82761bb875 | ||
|
|
08a10a8af5 | ||
|
|
bd130136bc | ||
|
|
fb04203b72 | ||
|
|
4172c190e5 | ||
|
|
920abd627b | ||
|
|
02b7399f9a | ||
|
|
b8402ed073 | ||
|
|
06c5c4d2a4 | ||
|
|
57a4ec31d1 | ||
|
|
511c9a6d4a | ||
|
|
8ec47111d5 | ||
|
|
8b67274cc0 | ||
|
|
af22fb7a06 | ||
|
|
df5fff5e72 | ||
|
|
d9243d1c0b | ||
|
|
2fb145d39d | ||
|
|
14094aaef7 | ||
|
|
ec752645ee | ||
|
|
ceb2f1da41 | ||
|
|
aab857a66e | ||
|
|
066be0fe30 | ||
|
|
380e87ddfa | ||
|
|
e9a7a2af18 | ||
|
|
a83e7d8c42 | ||
|
|
bf7dd7ac63 | ||
|
|
0fc3370859 | ||
|
|
c416012f97 | ||
|
|
d3a8862bd8 | ||
|
|
08353731ef | ||
|
|
16700244b0 | ||
|
|
49ca223e9c | ||
|
|
38efe632ae | ||
|
|
39a3bb7e44 | ||
|
|
920e8d9145 | ||
|
|
aed6d05d8c | ||
|
|
d85d95a40b | ||
|
|
b5dd54a404 | ||
|
|
f7c0a2ee15 | ||
|
|
874b5bfdbf | ||
|
|
71d09722be | ||
|
|
349d25dccf | ||
|
|
3942914945 | ||
|
|
60c16bbb5d | ||
|
|
70b5f4130e | ||
|
|
17cfecc5c3 | ||
|
|
ebdd18769b | ||
|
|
f974fddd2a | ||
|
|
b1a46da309 | ||
|
|
81879be4be | ||
|
|
0a094d4c1f | ||
|
|
66a96410d4 | ||
|
|
d4b23f08b6 | ||
|
|
d6d956b9d8 | ||
|
|
383b2a48c5 | ||
|
|
ab1c464950 | ||
|
|
f20b5191f2 | ||
|
|
fc113d64f5 | ||
|
|
6f6b12bee7 | ||
|
|
4e6c2867da | ||
|
|
54989e819a | ||
|
|
bf505bb68d | ||
|
|
4072062b72 | ||
|
|
d6ba37dc3c | ||
|
|
a58d21aa28 | ||
|
|
6e8d4129b2 | ||
|
|
2485a8f8d9 | ||
|
|
1ae908f6d5 | ||
|
|
6b923bdff1 | ||
|
|
d39969d1e6 | ||
|
|
eaef5bd133 | ||
|
|
7cc7df8c0e | ||
|
|
551f393da7 | ||
|
|
e81f8373a2 | ||
|
|
dfa83f2c85 | ||
|
|
91bdbc5193 | ||
|
|
707b9ed2d6 | ||
|
|
cd6b846e22 | ||
|
|
807e4ea6ec | ||
|
|
507e8bfac9 | ||
|
|
2ea63a0ea6 | ||
|
|
7988acbff4 | ||
|
|
b8c3740446 | ||
|
|
7bdbd80552 | ||
|
|
577ad2599c | ||
|
|
4d9f8168b8 | ||
|
|
f1d0b451b1 | ||
|
|
ac921d4657 | ||
|
|
f046434f3b | ||
|
|
58b9ee41fa | ||
|
|
d28d7a67f2 | ||
|
|
20370e65d6 | ||
|
|
67626d49c9 | ||
|
|
cd9e179fcb | ||
|
|
0ba28e6153 | ||
|
|
de791765b2 | ||
|
|
e79121acb7 | ||
|
|
4bdec5c1c1 | ||
|
|
fa39d2f2fa | ||
|
|
c55d06ed57 | ||
|
|
3829873552 | ||
|
|
54ebcc5bd8 | ||
|
|
7782500dbb | ||
|
|
86ec6bb2f8 | ||
|
|
02499d672b | ||
|
|
8c2e641ed7 | ||
|
|
29c53082eb | ||
|
|
2fa988fc7b | ||
|
|
72f1d444d3 | ||
|
|
7dcc94202d | ||
|
|
a447ae5eab | ||
|
|
54ad1e4e47 | ||
|
|
39ea91cfc2 | ||
|
|
5ebff5d15e | ||
|
|
731512a947 | ||
|
|
304dbca63d | ||
|
|
18ff0fafe0 | ||
|
|
25de791424 | ||
|
|
6e88c9cfd1 | ||
|
|
9937e88985 | ||
|
|
57f964a139 | ||
|
|
cbd5835112 | ||
|
|
fba32acb73 | ||
|
|
2fdb1f78a2 | ||
|
|
ea2069df51 | ||
|
|
7267fc757d | ||
|
|
ea7d69060d | ||
|
|
6e5e2eb694 | ||
|
|
51466df7b9 | ||
|
|
d6259332d4 | ||
|
|
260229a4d1 | ||
|
|
7776df241c | ||
|
|
e17fdcb077 | ||
|
|
d96629cdf4 | ||
|
|
0c79fcfbd0 | ||
|
|
9125830de6 | ||
|
|
55963d0c9a | ||
|
|
44f7b91826 | ||
|
|
731ea5d50d | ||
|
|
9e517fde00 | ||
|
|
1ff61837ac | ||
|
|
3a8464e4b8 | ||
|
|
39b0b8e2fd | ||
|
|
3a61186491 | ||
|
|
2180e1786c | ||
|
|
8fb5e8ef19 | ||
|
|
a8f54d1851 | ||
|
|
c9721d4d7f | ||
|
|
c848372bbe | ||
|
|
a23daa1547 | ||
|
|
9dba6c806b | ||
|
|
fc635a9340 | ||
|
|
81f738af3f | ||
|
|
f6165d23e4 | ||
|
|
e4a4b6f260 | ||
|
|
0ceee1dc05 | ||
|
|
8a6cf95fad | ||
|
|
37a7031f4e | ||
|
|
8874f3de9d | ||
|
|
3c5d48ea13 | ||
|
|
a0d98b7a39 | ||
|
|
74b75899d3 | ||
|
|
240eca0b68 | ||
|
|
cd54b18de3 | ||
|
|
65e01dbabc | ||
|
|
62276ce63e | ||
|
|
f67fe352cf | ||
|
|
4805797752 | ||
|
|
4c038508f2 | ||
|
|
15db11c0e6 | ||
|
|
97fe505bb4 | ||
|
|
fd45a6d31b | ||
|
|
e1a9ad7f82 | ||
|
|
296affc6e4 | ||
|
|
1889e12b54 | ||
|
|
5e91d919e0 | ||
|
|
5934846852 | ||
|
|
5045dfab74 | ||
|
|
499a7d6a37 | ||
|
|
f72c3fa9a7 | ||
|
|
d0c50632cd | ||
|
|
fffb54e2c6 | ||
|
|
8bf3bd48a6 | ||
|
|
dbe976ad3e | ||
|
|
9a8a6b53ae | ||
|
|
84132b7b2a | ||
|
|
e69a7a1b25 | ||
|
|
7f2bdc217a | ||
|
|
76bb35d5db | ||
|
|
0187f297d1 | ||
|
|
6ff49dc196 | ||
|
|
cd778396ff | ||
|
|
0dcb50d39c | ||
|
|
fd763846e5 | ||
|
|
f4f310cf51 | ||
|
|
0798b72470 | ||
|
|
39a17f8a72 | ||
|
|
78454da3b3 | ||
|
|
e62dc8f3b2 | ||
|
|
e99088348d | ||
|
|
6a12bd7abc | ||
|
|
b39e30fb0c | ||
|
|
92b15395a4 | ||
|
|
7411ff1fca | ||
|
|
f2e7facbc3 | ||
|
|
f1b4f0c463 | ||
|
|
c04e3f80b9 | ||
|
|
717e872fdd | ||
|
|
dbff932d8b | ||
|
|
5c9f463cab | ||
|
|
3a8f47befd | ||
|
|
a3e2a41834 | ||
|
|
b667bd709d | ||
|
|
a4c668e37a | ||
|
|
900bbe724a | ||
|
|
f5a2857620 | ||
|
|
2ef6f7248d | ||
|
|
06b430e57f | ||
|
|
f061dcf8b6 | ||
|
|
99377bdd30 | ||
|
|
c6b5343c9b | ||
|
|
7f4e2a93d0 | ||
|
|
c151f9bc59 | ||
|
|
204bf9acec | ||
|
|
e3bac87016 | ||
|
|
1f09505248 | ||
|
|
7f99013179 | ||
|
|
8d72c2a7b2 | ||
|
|
c722cfec18 | ||
|
|
835af2cae1 | ||
|
|
bd2a4097b2 | ||
|
|
fcc75c6a3f | ||
|
|
e08b234326 | ||
|
|
5bd3df7a0f | ||
|
|
5bbf24bf41 | ||
|
|
ace1dc53b2 | ||
|
|
100480a1d5 | ||
|
|
a89a2df603 | ||
|
|
988ad66a12 | ||
|
|
85d6292b8f | ||
|
|
98ab0950c9 | ||
|
|
a7f9043072 | ||
|
|
6d8b747ff2 | ||
|
|
a17eae4142 | ||
|
|
7b05d70042 | ||
|
|
f328907da4 | ||
|
|
1cea7ba487 | ||
|
|
6848452149 | ||
|
|
f9a1d47f13 | ||
|
|
492dcdca8d | ||
|
|
4ef3a778c1 | ||
|
|
31c5ef1bc3 | ||
|
|
4656e7b0dd | ||
|
|
4216cf2ac1 | ||
|
|
37e4f51668 | ||
|
|
1dae0373c8 | ||
|
|
e928f36456 | ||
|
|
fcb94a1742 | ||
|
|
8ff010ca0f | ||
|
|
241090a82b | ||
|
|
618bef0eb2 | ||
|
|
3b69f5d500 | ||
|
|
b298d46a40 | ||
|
|
75059d1bdc | ||
|
|
15e9a92798 | ||
|
|
bafc30bec6 | ||
|
|
0a8f839281 | ||
|
|
ab48174bac | ||
|
|
4439251f17 | ||
|
|
f9a6403b11 | ||
|
|
786f46c655 | ||
|
|
5ebd9fe7a5 | ||
|
|
2302ae5e88 | ||
|
|
8b12263dbc | ||
|
|
376d2666a5 | ||
|
|
d90622630f | ||
|
|
da447b1067 | ||
|
|
460d95afc5 | ||
|
|
8f522048d8 | ||
|
|
147c095105 | ||
|
|
fecb11f2fa | ||
|
|
56e50c2dbe | ||
|
|
c1b5e9b260 | ||
|
|
286fa90ee9 | ||
|
|
d63fb70c69 | ||
|
|
4d76a3be4c | ||
|
|
d9c612ef2b | ||
|
|
3b3ca1c1b3 | ||
|
|
2e748fdbf1 | ||
|
|
6c99691d1c | ||
|
|
fd067c8077 | ||
|
|
04012760d1 | ||
|
|
0ec1a141f1 | ||
|
|
7414a4fb06 | ||
|
|
abbd44e276 | ||
|
|
13e80e6bd5 | ||
|
|
ffa981d172 | ||
|
|
806f07db67 | ||
|
|
6746933a3d | ||
|
|
8cb2833fa7 | ||
|
|
1730f3c0c9 | ||
|
|
d6dda04f4f | ||
|
|
1b0825ef4b | ||
|
|
7249b2bd5f | ||
|
|
238bf1a29e | ||
|
|
4cbf81da59 | ||
|
|
d9b7ec18e7 | ||
|
|
ffed9c4d8c | ||
|
|
0dc0c5a0e0 | ||
|
|
57f826ece1 | ||
|
|
729bdebc50 | ||
|
|
2a541175b5 | ||
|
|
5f93f7162f | ||
|
|
3640554503 | ||
|
|
12437fd7fe | ||
|
|
4120f953c7 | ||
|
|
844c7787f9 | ||
|
|
0fe9ec1499 | ||
|
|
b3fb21dacc | ||
|
|
2deaf084e4 | ||
|
|
1b324510cc | ||
|
|
fd839f1827 | ||
|
|
f149f4aab1 | ||
|
|
af6fc64ab9 | ||
|
|
ff159bf30a | ||
|
|
8cdd19bcae | ||
|
|
2efc859829 | ||
|
|
d4a8351464 | ||
|
|
5fa544fc78 | ||
|
|
1070dd2d87 | ||
|
|
af98854ce4 | ||
|
|
7cace648c0 | ||
|
|
447bf4adb0 | ||
|
|
6572c1c692 | ||
|
|
7b48859f8d | ||
|
|
a6230bafe7 | ||
|
|
cc8f1466c7 | ||
|
|
69f810febc | ||
|
|
e5da7440d7 | ||
|
|
849ec1684a | ||
|
|
4f210cd8eb | ||
|
|
285f75764c | ||
|
|
3586203753 | ||
|
|
e406de7e00 | ||
|
|
53ed8502c9 | ||
|
|
ac8c756879 | ||
|
|
07faacdaf9 | ||
|
|
8464844518 | ||
|
|
fe2d1ff027 | ||
|
|
8d129e359e | ||
|
|
39aa56457b | ||
|
|
8491d5da8b | ||
|
|
681196e2c8 | ||
|
|
836ad5ebab | ||
|
|
a3ea1eddfb | ||
|
|
a2c06ac6ba | ||
|
|
a5c697fa2d | ||
|
|
be199b4fe9 | ||
|
|
cc15320cfe | ||
|
|
70bae59ec6 | ||
|
|
d4f3361805 | ||
|
|
332c855d40 | ||
|
|
01e3fa4d13 | ||
|
|
15a0f7a00a | ||
|
|
a751adba2a | ||
|
|
10eed010df | ||
|
|
033ca26216 | ||
|
|
40793cbdce | ||
|
|
48a1b2ec75 | ||
|
|
1a55556c66 | ||
|
|
3edd3ccbe6 | ||
|
|
194933e037 | ||
|
|
38deaedaef | ||
|
|
fc3dd5900c | ||
|
|
b1213425d0 | ||
|
|
40f0b450a8 | ||
|
|
0bab3977a8 | ||
|
|
2c46760b77 | ||
|
|
878c9ae5ae | ||
|
|
c8553de09e | ||
|
|
6738858e11 | ||
|
|
61cd6ab86e | ||
|
|
77704b669a | ||
|
|
770be613d7 | ||
|
|
9790366449 | ||
|
|
89f0a8c530 | ||
|
|
f72a06e25a | ||
|
|
8b975533e5 | ||
|
|
b32f7a9b57 | ||
|
|
e1e0083897 | ||
|
|
ca08ac0e3c | ||
|
|
a5b85b4462 | ||
|
|
3feab3a16d | ||
|
|
5d76e57b81 | ||
|
|
9101cd0339 | ||
|
|
4b2d3f5f5b | ||
|
|
482a471abb | ||
|
|
4d90bb0d02 | ||
|
|
202dfc18be | ||
|
|
13f4742699 | ||
|
|
33e769bd75 | ||
|
|
992f3000ef | ||
|
|
7ea4adae5e | ||
|
|
5ed9991135 | ||
|
|
dc2527c11f | ||
|
|
bee95654f3 | ||
|
|
48296bb1cf | ||
|
|
1ee9735354 | ||
|
|
f8da303ca1 | ||
|
|
6ae6ee9fd2 | ||
|
|
a11eafc5b3 | ||
|
|
efe53b57e4 | ||
|
|
e2330944d4 | ||
|
|
1c18906db7 | ||
|
|
873ceb48bd | ||
|
|
a48b955d89 | ||
|
|
b92fae2bfb | ||
|
|
156be17191 | ||
|
|
92290cfe81 | ||
|
|
29725c97f1 | ||
|
|
56b9cd14cb | ||
|
|
3550cc8f48 | ||
|
|
6a3d01ee6b | ||
|
|
27986213aa | ||
|
|
a2d07ef983 | ||
|
|
3cf4e32d9f | ||
|
|
a54584b716 | ||
|
|
51907e2642 | ||
|
|
5dd9d37ef2 | ||
|
|
f900854e0d | ||
|
|
d20e94f763 | ||
|
|
58de2ad2d7 | ||
|
|
f30c7b75d0 | ||
|
|
c5b8e55e12 | ||
|
|
559874fce7 | ||
|
|
1875653b34 | ||
|
|
6df770e72d | ||
|
|
51d415c8dd | ||
|
|
63bf428c34 | ||
|
|
a5d2772082 | ||
|
|
43c317ef4c | ||
|
|
01ffdb69d0 | ||
|
|
a78f346ac6 | ||
|
|
4ae88c859e | ||
|
|
e8a97f12d7 | ||
|
|
06aabdefbe | ||
|
|
15643f8e67 | ||
|
|
b987df1974 | ||
|
|
cbe63d43df | ||
|
|
e91a7c9599 | ||
|
|
3054952a5e | ||
|
|
3244df9cd1 | ||
|
|
1f75081f6e | ||
|
|
2c83a50a8a | ||
|
|
1c80fc28ee | ||
|
|
810eab9a58 | ||
|
|
256aa0296d | ||
|
|
2eb506c5e0 | ||
|
|
9f78a2902b | ||
|
|
613ae4ee34 | ||
|
|
ab208b71c4 | ||
|
|
68dd8d277d | ||
|
|
36a1927915 | ||
|
|
68437de416 | ||
|
|
459cd1b095 | ||
|
|
6f7f774080 | ||
|
|
fc0bf1953a | ||
|
|
c907e6c5fb | ||
|
|
29b3950ee7 | ||
|
|
0e0c6b8357 | ||
|
|
6f17f3e2c2 | ||
|
|
df107a272e | ||
|
|
b7a0748312 | ||
|
|
421b420a0c | ||
|
|
a478dc5935 | ||
|
|
4d65cb1c0f | ||
|
|
ced823cd95 | ||
|
|
79325bd219 | ||
|
|
5a19135c35 | ||
|
|
290e5f83d4 | ||
|
|
b535cb319a | ||
|
|
e3a9c6afb4 | ||
|
|
3f1a33e4b4 | ||
|
|
441eaa7f6c | ||
|
|
632153a1b4 | ||
|
|
f9d0ad9dc3 | ||
|
|
a67323fc12 | ||
|
|
1daa5e69b1 | ||
|
|
1718490982 | ||
|
|
1d4f97bb4c | ||
|
|
183fc7ced9 | ||
|
|
dcf1cedec9 | ||
|
|
845e608280 | ||
|
|
d6ecfe7f24 | ||
|
|
06b6bf6185 | ||
|
|
04a38b446f | ||
|
|
dfde5785a6 | ||
|
|
1a61455eef | ||
|
|
9f2ecb0144 | ||
|
|
3265060dc8 | ||
|
|
58b8acd68a | ||
|
|
3ec09ceedf | ||
|
|
1ce4aa81ee | ||
|
|
177e829f4b | ||
|
|
b5b672ec89 | ||
|
|
d41419c8d3 | ||
|
|
5e4d9dc17a | ||
|
|
4ac688e560 | ||
|
|
5d0a5cfe72 | ||
|
|
d4b81bb911 | ||
|
|
23122b327c | ||
|
|
f443c64341 | ||
|
|
c84af7d1e2 | ||
|
|
19a28b3c81 | ||
|
|
69944c1d3a | ||
|
|
81bf4921d4 | ||
|
|
91f6eedb24 | ||
|
|
dae150a3a5 | ||
|
|
8a81dce0c1 | ||
|
|
203d4a79e2 | ||
|
|
b04d47c3a9 | ||
|
|
8fcd1906a2 | ||
|
|
9939212379 | ||
|
|
38752f1327 | ||
|
|
6b51ec3371 | ||
|
|
5714056f9f | ||
|
|
21665e367c | ||
|
|
dff844b40a | ||
|
|
54403dfcbc | ||
|
|
61672c7805 | ||
|
|
69bb96113b | ||
|
|
51328efb22 | ||
|
|
f159666ae3 | ||
|
|
7fdb50807c | ||
|
|
890c7b42dc | ||
|
|
55398684cc | ||
|
|
c85edc38aa | ||
|
|
fa368b56ba | ||
|
|
41d7520585 | ||
|
|
0f23618324 | ||
|
|
bf9a2b45ef | ||
|
|
063c017bb7 | ||
|
|
7519a3765b | ||
|
|
56c687b39b | ||
|
|
083d398631 | ||
|
|
466183113f | ||
|
|
923c432977 | ||
|
|
e433ba8d7b | ||
|
|
848c884fff | ||
|
|
0769bbb7ff | ||
|
|
7054b09584 | ||
|
|
2633628ad1 | ||
|
|
e5ae1c2ea3 | ||
|
|
d2844580a1 | ||
|
|
42f5a3e2f7 | ||
|
|
8a62005a2d | ||
|
|
616f54c4a2 | ||
|
|
2140e5992a | ||
|
|
cd38c62430 | ||
|
|
67ac6ae191 | ||
|
|
7addae0fdd | ||
|
|
a3e8ba5506 | ||
|
|
6a302c38ab | ||
|
|
129bb6b1c3 | ||
|
|
c8b09c2238 | ||
|
|
76364af243 | ||
|
|
0efa8f9d3f | ||
|
|
f36b452f00 | ||
|
|
0be132f3b5 | ||
|
|
d31d8a764b | ||
|
|
68b29b683d | ||
|
|
eee293518c | ||
|
|
c4adc86f5e | ||
|
|
3ebda76def | ||
|
|
7564623d46 | ||
|
|
dc6b44571f | ||
|
|
ccd0167e40 | ||
|
|
7dd1004b93 | ||
|
|
19dd02acfd | ||
|
|
5e31914dc1 | ||
|
|
8296e0f485 | ||
|
|
afa5e3f469 | ||
|
|
f57fcbcb50 | ||
|
|
ce53e9dd2b | ||
|
|
55c2634416 | ||
|
|
fe0d340c03 | ||
|
|
5a02c9dbed | ||
|
|
2c4ffcf70d | ||
|
|
53c3e1338f | ||
|
|
79454a9072 | ||
|
|
21c2c5650b | ||
|
|
81ee559905 | ||
|
|
4a637a6e9a | ||
|
|
aaf4802d80 | ||
|
|
96310d761b | ||
|
|
96150bf568 | ||
|
|
f49df51544 | ||
|
|
673796fb3a | ||
|
|
482ab33c03 | ||
|
|
fb53cd37e7 | ||
|
|
c9fc7a1320 | ||
|
|
5f59b7e747 | ||
|
|
024b24932c | ||
|
|
604c0b189f | ||
|
|
356f1f5119 | ||
|
|
06659844b6 | ||
|
|
57cf363be9 | ||
|
|
2fc1ff68cf | ||
|
|
6c4fecc4f7 | ||
|
|
0991ad7cc8 | ||
|
|
d03b96b699 | ||
|
|
a690c1442f | ||
|
|
d742b67878 | ||
|
|
41971a95b2 | ||
|
|
b6d700077a | ||
|
|
c091e6910b | ||
|
|
35f633b533 | ||
|
|
85e4ed7c25 | ||
|
|
3b345a81c4 | ||
|
|
a55c48a4ce | ||
|
|
6f4c21c218 | ||
|
|
5060640408 | ||
|
|
f1cc8379e7 | ||
|
|
4f9627c612 | ||
|
|
012f241005 | ||
|
|
ccf67dbf05 | ||
|
|
a43709b4a8 | ||
|
|
e95c074965 | ||
|
|
4ca73566b6 | ||
|
|
d0b347517c | ||
|
|
7bed7ff482 | ||
|
|
d38b0dbf5b | ||
|
|
29d6c4ac04 | ||
|
|
8148b6d037 | ||
|
|
3327e0e871 | ||
|
|
0456f0f59b | ||
|
|
72cf78de2c | ||
|
|
1d0a89879c | ||
|
|
d1cc952859 | ||
|
|
f56c5b201d | ||
|
|
077b9eb7a4 | ||
|
|
15c333feb0 | ||
|
|
31c20ab32c | ||
|
|
69215cf9eb | ||
|
|
f89b51eace | ||
|
|
c581b5e532 | ||
|
|
df311203dc | ||
|
|
4927195ee4 | ||
|
|
63be1ebd38 | ||
|
|
796dc18cfb | ||
|
|
14e116553f | ||
|
|
c82ccd6cf7 | ||
|
|
0adb080319 | ||
|
|
dbb38fffae | ||
|
|
672a68219a | ||
|
|
22cb0efd00 | ||
|
|
a68d3d63b2 | ||
|
|
cf48c7280a | ||
|
|
1295973028 | ||
|
|
d5b9eda0f2 | ||
|
|
41c078d25c | ||
|
|
5614224e7d | ||
|
|
9ba44cb0d5 | ||
|
|
c18828af7b | ||
|
|
8a5f8439b4 | ||
|
|
8f0374569e | ||
|
|
09dde636d2 | ||
|
|
d347247bf3 | ||
|
|
a6c66fb669 | ||
|
|
b4dbc825b1 | ||
|
|
4a0cc8223e | ||
|
|
7db10857b3 | ||
|
|
7e927b43ad | ||
|
|
9eea072f2a | ||
|
|
aa2ff674db | ||
|
|
de24bfdf2f | ||
|
|
2a084c3d98 | ||
|
|
db8df41396 | ||
|
|
bda05d9372 | ||
|
|
59bc2af4d6 | ||
|
|
00a23ee330 | ||
|
|
51ad50a641 | ||
|
|
148e1926e8 | ||
|
|
96c023df41 | ||
|
|
44f37f49a1 | ||
|
|
a8a30f6d3f | ||
|
|
ba80b1cd55 | ||
|
|
b56e985ccc | ||
|
|
67521fd9f6 | ||
|
|
8c394f44b8 | ||
|
|
77b4fff268 | ||
|
|
f56a0c6075 | ||
|
|
9ce8486059 | ||
|
|
04a321150a | ||
|
|
d333c3c09e |
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
BasedOnStyle: Google
|
||||||
|
AllowShortFunctionsOnASingleLine: false
|
||||||
|
SpaceAfterCStyleCast: true
|
||||||
|
PointerBindsToType: false
|
||||||
|
DerivePointerBinding: false
|
||||||
|
IncludeBlocks: Preserve
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
name: build
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
linux:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Install packages
|
||||||
|
run: sudo apt-get install libmbedtls-dev
|
||||||
|
- name: make
|
||||||
|
run: make test test++ IPV6=0
|
||||||
|
- name: openssl
|
||||||
|
run: make test SSL=OPENSSL IPV6=0
|
||||||
|
- name: examples
|
||||||
|
run: make ex
|
||||||
|
macos:
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: Install packages
|
||||||
|
run: HOMEBREW_NO_AUTO_UPDATE=1 brew install jq mbedtls openssl
|
||||||
|
- name: make
|
||||||
|
run: make test++ upload-coverage IPV6=0
|
||||||
|
- name: openssl
|
||||||
|
run: make test SSL=OPENSSL IPV6=0
|
||||||
|
- name: exports
|
||||||
|
run: make mg_prefix
|
||||||
|
windows:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: vc98
|
||||||
|
run: make vc98
|
||||||
|
- name: vc2017
|
||||||
|
run: make vc2017
|
||||||
|
- name: mingw
|
||||||
|
run: make mingw
|
||||||
|
- name: mingw++
|
||||||
|
run: make mingw++
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
|
Copyright (c) 2004-2013 Sergey Lyubka
|
||||||
Copyright (c) 2013-2015 Cesanta Software Limited
|
Copyright (c) 2013-2020 Cesanta Software Limited
|
||||||
All rights reserved
|
All rights reserved
|
||||||
|
|
||||||
This software is dual-licensed: you can redistribute it and/or modify
|
This software is dual-licensed: you can redistribute it and/or modify
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
SRCS = $(wildcard src/*.c)
|
||||||
|
HDRS = $(wildcard src/*.h)
|
||||||
|
DEFS ?= -DMG_MAX_HTTP_HEADERS=5 -DMG_ENABLE_LINES -DMG_ENABLE_HTTP_DEBUG_ENDPOINT=1 -DMG_ENABLE_DIRECTORY_LISTING=1 -DMG_ENABLE_SSI=1
|
||||||
|
CFLAGS ?= -W -Wall -Werror -Isrc -I. -O0 -g $(DEFS) $(TFLAGS) $(EXTRA)
|
||||||
|
SSL ?= MBEDTLS
|
||||||
|
CDIR ?= $(realpath $(CURDIR))
|
||||||
|
VC98 = docker run --rm -e WINEDEBUG=-all -v $(CDIR):$(CDIR) -w $(CDIR) docker.io/mdashnet/vc98
|
||||||
|
VC2017 = docker run --rm -e WINEDEBUG=-all -v $(CDIR):$(CDIR) -w $(CDIR) docker.io/mdashnet/vc2017
|
||||||
|
MINGW = docker run --rm -v $(CDIR):$(CDIR) -w $(CDIR) docker.io/mdashnet/mingw
|
||||||
|
GCC = docker run --rm -v $(CDIR):$(CDIR) -w $(CDIR) mdashnet/cc2
|
||||||
|
VCFLAGS = /nologo /W3 /O2 /I. $(DEFS) $(TFLAGS)
|
||||||
|
CLANG ?= clang # /usr/local/opt/llvm\@9/bin/clang
|
||||||
|
IPV6 ?= 1
|
||||||
|
ASAN_OPTIONS ?=
|
||||||
|
EXAMPLES := $(wildcard examples/*)
|
||||||
|
EXAMPLE_TARGET ?= example
|
||||||
|
.PHONY: ex test
|
||||||
|
|
||||||
|
ifeq "$(SSL)" "MBEDTLS"
|
||||||
|
MBEDTLS_DIR ?= $(shell brew --cellar mbedtls)
|
||||||
|
MBEDTLS_VER ?= $(shell brew info mbedtls --json | jq -j .[0].installed[0].version)
|
||||||
|
MBEDTLS ?= $(MBEDTLS_DIR)/$(MBEDTLS_VER)
|
||||||
|
CFLAGS += -DMG_ENABLE_MBEDTLS=1 -I$(MBEDTLS)/include -I/usr/include
|
||||||
|
LDFLAGS ?= -L$(MBEDTLS)/lib -lmbedtls -lmbedcrypto -lmbedx509
|
||||||
|
endif
|
||||||
|
ifeq "$(SSL)" "OPENSSL"
|
||||||
|
OPENSSL_DIR ?= $(shell brew --cellar openssl)
|
||||||
|
OPENSSL_VER ?= $(shell brew info openssl --json | jq -j .[0].installed[0].version)
|
||||||
|
OPENSSL ?= $(OPENSSL_DIR)/$(OPENSSL_VER)
|
||||||
|
CFLAGS += -DMG_ENABLE_OPENSSL=1 -I$(OPENSSL)/include
|
||||||
|
LDFLAGS ?= -L$(OPENSSL)/lib -lssl -lcrypto
|
||||||
|
endif
|
||||||
|
|
||||||
|
all: mg_prefix test test++ ex vc98 vc2017 mingw mingw++ linux linux++ infer fuzz
|
||||||
|
|
||||||
|
ex:
|
||||||
|
@for X in $(EXAMPLES); do $(MAKE) -C $$X $(EXAMPLE_TARGET); done
|
||||||
|
|
||||||
|
# Check that all external (exported) symbols have "mg_" prefix
|
||||||
|
mg_prefix: mongoose.c mongoose.h
|
||||||
|
$(CLANG) mongoose.c $(CFLAGS) -c -o /tmp/x.o && nm /tmp/x.o | grep ' T' | grep -v 'mg_' ; test $$? = 1
|
||||||
|
|
||||||
|
# C++ build
|
||||||
|
test++: CLANG = g++ -Wno-deprecated -Wno-missing-field-initializers
|
||||||
|
test++: unamalgamated
|
||||||
|
|
||||||
|
# Make sure we can build from an unamalgamated sources
|
||||||
|
unamalgamated: $(SRCS) $(HDRS) Makefile
|
||||||
|
$(CLANG) src/*.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -g -o unit_test
|
||||||
|
|
||||||
|
fuzz: mongoose.c mongoose.h Makefile test/fuzz.c
|
||||||
|
$(CLANG) mongoose.c test/fuzz.c $(CFLAGS) -DMG_ENABLE_LINES -DMG_ENABLE_LOG=0 -fsanitize=fuzzer,signed-integer-overflow,address $(LDFLAGS) -g -o fuzzer
|
||||||
|
$(DEBUGGER) ./fuzzer
|
||||||
|
|
||||||
|
# make CLANG=/usr/local/opt/llvm\@8/bin/clang ASAN_OPTIONS=detect_leaks=1
|
||||||
|
test: CFLAGS += -DMG_ENABLE_IPV6=$(IPV6) -fsanitize=address#,undefined
|
||||||
|
test: mongoose.c mongoose.h Makefile test/unit_test.c
|
||||||
|
$(CLANG) mongoose.c test/unit_test.c $(CFLAGS) -coverage $(LDFLAGS) -g -o unit_test
|
||||||
|
ASAN_OPTIONS=$(ASAN_OPTIONS) $(DEBUGGER) ./unit_test
|
||||||
|
|
||||||
|
coverage: test
|
||||||
|
gcov -l -n *.gcno | sed '/^$$/d' | sed 'N;s/\n/ /'
|
||||||
|
gcov mongoose.gcno >/dev/null
|
||||||
|
|
||||||
|
upload-coverage: coverage
|
||||||
|
curl -s https://codecov.io/bash | /bin/bash
|
||||||
|
|
||||||
|
infer:
|
||||||
|
infer run -- cc test/unit_test.c -c -W -Wall -Werror -Isrc -I. -O2 -DMG_ENABLE_MBEDTLS=1 -DMG_ENABLE_LINES -I/usr/local/Cellar/mbedtls/2.23.0/include -DMG_ENABLE_IPV6=1 -g -o /dev/null
|
||||||
|
|
||||||
|
#vc98: VCFLAGS += -DMG_ENABLE_IPV6=1
|
||||||
|
vc98: Makefile mongoose.c mongoose.h test/unit_test.c
|
||||||
|
$(VC98) wine cl mongoose.c test/unit_test.c $(VCFLAGS) ws2_32.lib /Fe$@.exe
|
||||||
|
$(VC98) wine $@.exe
|
||||||
|
|
||||||
|
#vc2017: VCFLAGS += -DMG_ENABLE_IPV6=1
|
||||||
|
vc2017: Makefile mongoose.c mongoose.h test/unit_test.c
|
||||||
|
$(VC2017) wine64 cl mongoose.c test/unit_test.c $(VCFLAGS) ws2_32.lib /Fe$@.exe
|
||||||
|
$(VC2017) wine64 $@.exe
|
||||||
|
|
||||||
|
mingw: Makefile mongoose.c mongoose.h test/unit_test.c
|
||||||
|
$(MINGW) i686-w64-mingw32-gcc mongoose.c test/unit_test.c -W -Wall -Werror -I. $(DEFS) -lwsock32 -o test.exe
|
||||||
|
$(VC98) wine test.exe
|
||||||
|
|
||||||
|
mingw++: Makefile mongoose.c mongoose.h test/unit_test.c
|
||||||
|
$(MINGW) i686-w64-mingw32-g++ mongoose.c test/unit_test.c -W -Wall -Werror -I. $(DEFS) -lwsock32 -o test.exe
|
||||||
|
# Note: for some reason, a binary built with mingw g++, fails to run
|
||||||
|
|
||||||
|
#linux: CFLAGS += -DMG_ENABLE_IPV6=$(IPV6)
|
||||||
|
linux: CFLAGS += -fsanitize=address,undefined
|
||||||
|
linux: Makefile mongoose.c mongoose.h test/unit_test.c
|
||||||
|
$(GCC) $(CC) mongoose.c test/unit_test.c $(CFLAGS) $(LDFLAGS) -o unit_test_gcc
|
||||||
|
$(GCC) ./unit_test_gcc
|
||||||
|
|
||||||
|
linux++: CC = g++ -Wno-missing-field-initializers
|
||||||
|
linux++: linux
|
||||||
|
|
||||||
|
mongoose.c: $(SRCS) Makefile
|
||||||
|
(cat src/license.h; echo; echo '#include "mongoose.h"' ; (for F in src/private.h src/*.c ; do echo; echo '#ifdef MG_ENABLE_LINES'; echo "#line 1 \"$$F\""; echo '#endif'; cat $$F | sed -e 's,#include ".*,,'; done))> $@
|
||||||
|
|
||||||
|
mongoose.h: $(HDRS) Makefile
|
||||||
|
(cat src/license.h src/version.h ; cat src/arch.h src/arch_*.h src/config.h src/str.h src/log.h src/timer.h src/util.h src/url.h src/iobuf.h src/base64.h src/md5.h src/sha1.h src/event.h src/net.h src/http.h src/ssi.h src/tls.h src/ws.h src/sntp.h src/mqtt.h src/dns.h | sed -e 's,#include ".*,,' -e 's,^#pragma once,,')> $@
|
||||||
|
|
||||||
|
clean: EXAMPLE_TARGET = clean
|
||||||
|
clean: ex
|
||||||
|
rm -rf $(PROG) *.o *.dSYM unit_test* ut fuzzer *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb slow-unit* _CL_* infer-out data.txt crash-*
|
||||||
@@ -1,49 +1,56 @@
|
|||||||
# <img src="http://www.cesanta.com/hubfs/www.cesanta.com/Images/mongoose_library.png" width="64" height="64"> Mongoose - Embedded Web Server
|
# Mongoose - Embedded Web Server / Embedded Networking Library
|
||||||
|
|
||||||

|

|
||||||
|
[](https://github.com/cesanta/mongoose/actions)
|
||||||
|
[](https://codecov.io/gh/cesanta/mongoose)
|
||||||
|
[](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:mongoose)
|
||||||
|
|
||||||
Mongoose is ideal for embedded environments. It has been designed
|
Mongoose is a networking library for C/C++. It implements event-driven
|
||||||
for connecting devices and bringing them online.
|
non-blocking APIs for TCP, UDP, HTTP, WebSocket, MQTT. It has been designed
|
||||||
[Mongoose](https://www.cesanta.com/products)
|
for connecting devices and bringing them online. On the market since 2004, used
|
||||||
has been on the market since 2004 and attracted
|
by vast number of open source and commercial products - it even runs on the
|
||||||
over 1 million cumulative downloads.
|
International Space Station! Mongoose makes embedded network programming fast,
|
||||||
|
robust, and easy.
|
||||||
|
|
||||||
It provides easy to use event-driven interface that allows to implement
|
- [Download Mongoose Source Code here](https://www.cesanta.com/download.html)
|
||||||
network protocols or scalable network applications with little effort.
|
- [Read Mongoose documentation](https://cesanta.com/docs/)
|
||||||
Mongoose helps developers to manage the complexity of network programming
|
|
||||||
and let them concentrate on the logic, saving time and money.
|
|
||||||
|
|
||||||
- [Download Mongoose Source Code here](http://hubs.ly/H01bWvx0)
|
Connectivity is vital for software and embedded devices, but there are many
|
||||||
- [Ask questions on support forum](http://forum.cesanta.com/index.php?p=/categories/mongoose)
|
pitfalls to consider when embedding a web server. This white paper breaks down
|
||||||
- [Read User Guide and API reference](https://docs.cesanta.com/mongoose)
|
on the top 9 things to avoid when embedding a web server.
|
||||||
- [Study mongoose example code](https://github.com/cesanta/mongoose/tree/master/examples)
|
|
||||||
|
|
||||||
> Nothing overdone. Nothing less. So unbelievably easy to use.
|
- [Download “9 Things NOT to do when embedding a web server” white paper here](https://www.cesanta.com/whitepaper.html)
|
||||||
> Just how good software should be! -- Pritin Tyagaraj, SAP
|
|
||||||
|
|
||||||
|
Looking for a complete IoT solution? Check out
|
||||||
|
- [VCON](https://vcon.io) - Arduino compatible MCU modules with built-in firmware OTA updates
|
||||||
|
- [Mongoose OS](https://mongoose-os.com) - open source embedded operating system for low-power connected microcontrollers. Secure, designed for commercial Internet of Things products
|
||||||
|
|
||||||
|
# Support
|
||||||
|
- [Study Mongoose examples](https://github.com/cesanta/mongoose/tree/master/examples)
|
||||||
|
- [Support Forum - ask your technical questions here](https://forum.cesanta.com/c/mongoose-library/6)
|
||||||
|
- [Commercial licensing and support available](https://www.cesanta.com/licensing.html)
|
||||||
|
- [Check out latest releases](https://github.com/cesanta/mongoose/releases)
|
||||||
|
|
||||||
# Features
|
# Features
|
||||||
|
|
||||||
* Cross-platform: works on Linux/UNIX, MacOS, QNX, eCos, Windows, Android,
|
* Cross-platform: works on Linux/UNIX, MacOS, QNX, eCos, Windows, Android,
|
||||||
iPhone, FreeRTOS (TI CC3200, ESP8266), etc
|
iPhone, FreeRTOS, etc
|
||||||
* Single-threaded, asynchronous, non-blocking core with simple event-based API
|
* Supported embedded architectures: ESP32, ESP8266, TI CC3200, TI MSP432, NRF52, STM32, PIC32, NXP, and more
|
||||||
* Native support for [PicoTCP embedded TCP/IP stack](http://www.picotcp.com),
|
* Builtin protocols: plain TCP/UDP, HTTP, MQTT, Websocket
|
||||||
[LWIP embedded TCP/IP stack](https://en.wikipedia.org/wiki/LwIP)
|
* SSL/TLS support: mbedTLS, OpenSSL or custom via API
|
||||||
* Builtin protocols:
|
* Asynchronous DNS resolver
|
||||||
- plain TCP, plain UDP, SSL/TLS (over TCP, one-way or two-way)
|
|
||||||
- HTTP client, HTTP server
|
|
||||||
- Websocket client, Websocket server
|
|
||||||
- JSON-RPC client, JSON-RPC server
|
|
||||||
- MQTT client, MQTT broker
|
|
||||||
- CoAP client, CoAP server
|
|
||||||
- DNS client, DNS server, async DNS resolver
|
|
||||||
* Tiny static and run-time footprint
|
* Tiny static and run-time footprint
|
||||||
* Source code is both ISO C and ISO C++ compliant
|
* Source code is both ISO C and ISO C++ compliant
|
||||||
* Very easy to integrate: just copy
|
* Very easy to integrate: just copy
|
||||||
[mongoose.c](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c) and
|
[mongoose.c](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.c) and
|
||||||
[mongoose.h](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.h)
|
[mongoose.h](https://raw.githubusercontent.com/cesanta/mongoose/master/mongoose.h)
|
||||||
files to your build tree
|
files to your build tree
|
||||||
* Extensively tested and production-ready, trusted by many blue chip businesses
|
|
||||||
|
# Licensing
|
||||||
|
|
||||||
|
Mongoose is released under Commercial and [GNU GPL v.2](http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) open source licenses.
|
||||||
|
|
||||||
|
Commercial Projects: [Contact us for commercial license.](https://www.cesanta.com/contact.html)
|
||||||
|
|
||||||
# Dashboard Example
|
# Dashboard Example
|
||||||
|
|
||||||
@@ -51,54 +58,17 @@ Mongoose is often used to implement device dashboards and real-time
|
|||||||
data exchange over Websocket. Here is a dashboard example that illustrates
|
data exchange over Websocket. Here is a dashboard example that illustrates
|
||||||
the functionality:
|
the functionality:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
[Developing a new product? Contact us today to discuss how Mongoose can help
|
[Developing a new product? Contact us today to discuss how Mongoose can help.](https://www.cesanta.com/contact.html)
|
||||||
](https://www.cesanta.com/contact)
|
|
||||||
|
|
||||||
# Contributions
|
# Contributions
|
||||||
|
|
||||||
People who have agreed to the
|
Contributions are welcome! Please follow the guidelines below:
|
||||||
[Cesanta CLA](https://docs.cesanta.com/contributors_la.shtml)
|
|
||||||
can make contributions. Note that the CLA isn't a copyright
|
|
||||||
_assigment_ but rather a copyright _license_.
|
|
||||||
You retain the copyright on your contributions.
|
|
||||||
|
|
||||||
# Licensing
|
- Sign [Cesanta CLA](https://cesanta.com/cla.html) and send GitHub pull request
|
||||||
|
- When making pull requests, please make sure that it has only one commit,
|
||||||
|
and imlements/fixes only one piece of functionality
|
||||||
|
|
||||||
You are using Mongoose under
|
# Looking for a pre-compiled Mongoose web server Windows or Mac binary?
|
||||||
[GPLv2](http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
|
- [Download pre-compiled Mongoose web server binary.](https://www.cesanta.com/binary.html)
|
||||||
and until you commercialize your
|
|
||||||
application, can continue to use Mongoose free of charge.
|
|
||||||
|
|
||||||
Once you commercialize your product, in order to comply with GPLv2
|
|
||||||
you need to either open the source code of your product fully or
|
|
||||||
alternatively purchase a commercial license.
|
|
||||||
We offer [three levels of commercial licensing](https://www.cesanta.com/products):
|
|
||||||
|
|
||||||
- Mongoose Lite: cross platform integration, SSL/TLS, HTTP support
|
|
||||||
- Mongoose: Mongoose Lite + HTTP CGI, HTTP SSI, HTTP Digest Auth,
|
|
||||||
WebSocket support
|
|
||||||
- Mongoose Premium: Mongoose + Plain TCP, UDP, JSON-RPC, MQTT, DNS,
|
|
||||||
CoAP support
|
|
||||||
|
|
||||||
|
|
||||||
[Enquire about commercial licensing here](https://www.cesanta.com/contact)
|
|
||||||
|
|
||||||
# Mongoose Binary
|
|
||||||
|
|
||||||
This is our easy to use web server for web developers (PHP, Ruby, Python, etc)
|
|
||||||
and web designers. Available in three editions to suit your needs: free, pro
|
|
||||||
(USD 5) and dev edition (from USD 8). To install, simply download, double-click
|
|
||||||
to start and run browser - that's all!
|
|
||||||
|
|
||||||
[Download Mongoose Binary here](https://www.cesanta.com/products#binary)
|
|
||||||
|
|
||||||
# Other products by Cesanta
|
|
||||||
|
|
||||||
- [Smart.js](https://github.com/cesanta/smart.js) -
|
|
||||||
Generic, hardware independent, full-stack IoT software platform
|
|
||||||
- [V7](https://github.com/cesanta/v7) - Embedded JavaScript engine
|
|
||||||
- [Frozen](https://github.com/cesanta/frozen) - JSON parser and generator
|
|
||||||
- [SLRE](https://github.com/cesanta/slre) - Super Light Regular Expression
|
|
||||||
library
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
*.exe
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
#############################################################
|
|
||||||
# Required variables for each makefile
|
|
||||||
# Discard this section from all parent makefiles
|
|
||||||
# Expected variables (with automatic defaults):
|
|
||||||
# CSRCS (all "C" files in the dir)
|
|
||||||
# SUBDIRS (all subdirs with a Makefile)
|
|
||||||
# GEN_LIBS - list of libs to be generated ()
|
|
||||||
# GEN_IMAGES - list of object file images to be generated ()
|
|
||||||
# GEN_BINS - list of binaries to be generated ()
|
|
||||||
# COMPONENTS_xxx - a list of libs/objs in the form
|
|
||||||
# subdir/lib to be extracted and rolled up into
|
|
||||||
# a generated lib/image xxx.a ()
|
|
||||||
#
|
|
||||||
TARGET = eagle
|
|
||||||
#FLAVOR = release
|
|
||||||
FLAVOR = debug
|
|
||||||
|
|
||||||
EXTRA_CCFLAGS += -Wall -Werror
|
|
||||||
|
|
||||||
ifndef PDIR # {
|
|
||||||
GEN_IMAGES= eagle.app.v6.out
|
|
||||||
GEN_BINS= eagle.app.v6.bin
|
|
||||||
SPECIAL_MKTARGETS=$(APP_MKTARGETS)
|
|
||||||
SUBDIRS=user
|
|
||||||
|
|
||||||
endif # } PDIR
|
|
||||||
|
|
||||||
LDDIR = $(SDK_PATH)/ld
|
|
||||||
|
|
||||||
CCFLAGS += -Os
|
|
||||||
|
|
||||||
TARGET_LDFLAGS = \
|
|
||||||
-nostdlib \
|
|
||||||
-Wl,-EL \
|
|
||||||
--longcalls \
|
|
||||||
--text-section-literals
|
|
||||||
|
|
||||||
ifeq ($(FLAVOR),debug)
|
|
||||||
TARGET_LDFLAGS += -g -O2
|
|
||||||
endif
|
|
||||||
|
|
||||||
ifeq ($(FLAVOR),release)
|
|
||||||
TARGET_LDFLAGS += -g -O0
|
|
||||||
endif
|
|
||||||
|
|
||||||
COMPONENTS_eagle.app.v6 = \
|
|
||||||
user/libuser.a
|
|
||||||
|
|
||||||
LINKFLAGS_eagle.app.v6 = \
|
|
||||||
-L$(SDK_PATH)/lib \
|
|
||||||
-Wl,--gc-sections \
|
|
||||||
-nostdlib \
|
|
||||||
-T$(LD_FILE) \
|
|
||||||
-Wl,--no-check-sections \
|
|
||||||
-u call_user_start \
|
|
||||||
-Wl,-static \
|
|
||||||
-Wl,--start-group \
|
|
||||||
-lcirom \
|
|
||||||
-lgcc \
|
|
||||||
-lhal \
|
|
||||||
-lphy \
|
|
||||||
-lpp \
|
|
||||||
-lnet80211 \
|
|
||||||
-lwpa \
|
|
||||||
-lcrypto \
|
|
||||||
-lmain \
|
|
||||||
-lfreertos \
|
|
||||||
-llwip \
|
|
||||||
$(DEP_LIBS_eagle.app.v6) \
|
|
||||||
-Wl,--end-group
|
|
||||||
|
|
||||||
DEPENDS_eagle.app.v6 = \
|
|
||||||
$(LD_FILE) \
|
|
||||||
$(LDDIR)/eagle.rom.addr.v6.ld
|
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# Configuration i.e. compile options etc.
|
|
||||||
# Target specific stuff (defines etc.) goes in here!
|
|
||||||
# Generally values applying to a tree are captured in the
|
|
||||||
# makefile at its root level - these are then overridden
|
|
||||||
# for a subtree within the makefile rooted therein
|
|
||||||
#
|
|
||||||
|
|
||||||
#UNIVERSAL_TARGET_DEFINES = \
|
|
||||||
|
|
||||||
# Other potential configuration flags include:
|
|
||||||
# -DTXRX_TXBUF_DEBUG
|
|
||||||
# -DTXRX_RXBUF_DEBUG
|
|
||||||
# -DWLAN_CONFIG_CCX
|
|
||||||
CONFIGURATION_DEFINES = -DICACHE_FLASH
|
|
||||||
|
|
||||||
DEFINES += \
|
|
||||||
$(UNIVERSAL_TARGET_DEFINES) \
|
|
||||||
$(CONFIGURATION_DEFINES)
|
|
||||||
|
|
||||||
DDEFINES += \
|
|
||||||
$(UNIVERSAL_TARGET_DEFINES) \
|
|
||||||
$(CONFIGURATION_DEFINES)
|
|
||||||
|
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# Recursion Magic - Don't touch this!!
|
|
||||||
#
|
|
||||||
# Each subtree potentially has an include directory
|
|
||||||
# corresponding to the common APIs applicable to modules
|
|
||||||
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
|
||||||
# of a module can only contain the include directories up
|
|
||||||
# its parent path, and not its siblings
|
|
||||||
#
|
|
||||||
# Required for each makefile to inherit from the parent
|
|
||||||
#
|
|
||||||
|
|
||||||
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
|
||||||
sinclude $(SDK_PATH)/Makefile
|
|
||||||
|
|
||||||
.PHONY: FORCE
|
|
||||||
FORCE:
|
|
||||||
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
This is a Mongoose "Hello, world" that can be compiled under ESP8266 RTOS SDK.
|
|
||||||
|
|
||||||
It sets up an AP (SSID `Mongoose`) and serves a "hello world" page on http://192.168.4.1/
|
|
||||||
|
|
||||||
Most of the the boilerplate comes from [project_template](https://github.com/espressif/ESP8266_RTOS_SDK/tree/master/examples/project_template) (@ [3ca6af5](https://github.com/espressif/ESP8266_RTOS_SDK/tree/3ca6af5da68678d809b34c7cd98bee71e0235039/examples/project_template)) with minimal changes.
|
|
||||||
|
|
||||||
To build with no changes to the SDK, you will need a module with 1MB (8Mb) flash or more.
|
|
||||||
|
|
||||||
Compile (for NodeMCU 1.0):
|
|
||||||
|
|
||||||
```
|
|
||||||
$ export SDK_PATH=/path/to/ESP8266_RTOS_SDK
|
|
||||||
$ export BIN_PATH=./bin
|
|
||||||
$ make clean; make BOOT=new APP=1 SPI_SPEED=40 SPI_MODE=dio SPI_SIZE_MAP=6
|
|
||||||
```
|
|
||||||
|
|
||||||
Flash (using [esptool](https://github.com/themadinventor/esptool)):
|
|
||||||
|
|
||||||
```
|
|
||||||
$ esptool.py --port /dev/ttyUSB0 --baud 230400 \
|
|
||||||
write_flash --flash_mode=dio --flash_size=32m \
|
|
||||||
0x00000 ${SDK_PATH}/bin/boot_v1.4\(b1\).bin \
|
|
||||||
0x01000 ${BIN_PATH}/upgrade/user1.4096.new.6.bin
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
The output can be made to fit in 512KB (4Mb), but stock linker scripts do not reserve enough space for code.
|
|
||||||
Custom linker script is provided for that, so you can use it for smaller devices like so (example that will work with ESP-01):
|
|
||||||
|
|
||||||
```
|
|
||||||
$ make clean; make BOOT=none APP=0 SPI_SPEED=40 SPI_MODE=qio SPI_SIZE_MAP=0 LD_FILE=ld/eagle.app.v6.512.compact.ld
|
|
||||||
$ esptool.py --port /dev/ttyUSB0 --baud 230400 \
|
|
||||||
write_flash --flash_mode=qio --flash_size=4m \
|
|
||||||
0x00000 ${BIN_PATH}/eagle.flash.bin \
|
|
||||||
0x10000 ${BIN_PATH}/eagle.irom0text.bin
|
|
||||||
```
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
:<<!
|
|
||||||
******NOTICE******
|
|
||||||
MUST set SDK_PATH & BIN_PATH firstly!!!
|
|
||||||
example:
|
|
||||||
export SDK_PATH=~/ESP8266_RTOS_SDK
|
|
||||||
export BIN_PATH=~/esp8266_bin
|
|
||||||
!
|
|
||||||
|
|
||||||
echo "gen_misc.sh version 20150911"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
if [ $SDK_PATH ]; then
|
|
||||||
echo "SDK_PATH:"
|
|
||||||
echo "$SDK_PATH"
|
|
||||||
echo ""
|
|
||||||
else
|
|
||||||
echo "ERROR: Please export SDK_PATH in gen_misc.sh firstly, exit!!!"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $BIN_PATH ]; then
|
|
||||||
echo "BIN_PATH:"
|
|
||||||
echo "$BIN_PATH"
|
|
||||||
echo ""
|
|
||||||
else
|
|
||||||
echo "ERROR: Please export BIN_PATH in gen_misc.sh firstly, exit!!!"
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Please check SDK_PATH & BIN_PATH, enter (Y/y) to continue:"
|
|
||||||
read input
|
|
||||||
|
|
||||||
if [[ $input != Y ]] && [[ $input != y ]]; then
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
echo "Please follow below steps(1-5) to generate specific bin(s):"
|
|
||||||
echo "STEP 1: use boot_v1.2+ by default"
|
|
||||||
boot=new
|
|
||||||
|
|
||||||
echo "boot mode: $boot"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
echo "STEP 2: choose bin generate(0=eagle.flash.bin+eagle.irom0text.bin, 1=user1.bin, 2=user2.bin)"
|
|
||||||
echo "enter (0/1/2, default 0):"
|
|
||||||
read input
|
|
||||||
|
|
||||||
if [ -z "$input" ]; then
|
|
||||||
if [ $boot != none ]; then
|
|
||||||
boot=none
|
|
||||||
echo "ignore boot"
|
|
||||||
fi
|
|
||||||
app=0
|
|
||||||
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
|
||||||
elif [ $input == 1 ]; then
|
|
||||||
if [ $boot == none ]; then
|
|
||||||
app=0
|
|
||||||
echo "choose no boot before"
|
|
||||||
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
|
||||||
else
|
|
||||||
app=1
|
|
||||||
echo "generate bin: user1.bin"
|
|
||||||
fi
|
|
||||||
elif [ $input == 2 ]; then
|
|
||||||
if [ $boot == none ]; then
|
|
||||||
app=0
|
|
||||||
echo "choose no boot before"
|
|
||||||
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
|
||||||
else
|
|
||||||
app=2
|
|
||||||
echo "generate bin: user2.bin"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
if [ $boot != none ]; then
|
|
||||||
boot=none
|
|
||||||
echo "ignore boot"
|
|
||||||
fi
|
|
||||||
app=0
|
|
||||||
echo "generate bin: eagle.flash.bin+eagle.irom0text.bin"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
echo "STEP 3: choose spi speed(0=20MHz, 1=26.7MHz, 2=40MHz, 3=80MHz)"
|
|
||||||
echo "enter (0/1/2/3, default 2):"
|
|
||||||
read input
|
|
||||||
|
|
||||||
if [ -z "$input" ]; then
|
|
||||||
spi_speed=40
|
|
||||||
elif [ $input == 0 ]; then
|
|
||||||
spi_speed=20
|
|
||||||
elif [ $input == 1 ]; then
|
|
||||||
spi_speed=26.7
|
|
||||||
elif [ $input == 3 ]; then
|
|
||||||
spi_speed=80
|
|
||||||
else
|
|
||||||
spi_speed=40
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "spi speed: $spi_speed MHz"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
echo "STEP 4: choose spi mode(0=QIO, 1=QOUT, 2=DIO, 3=DOUT)"
|
|
||||||
echo "enter (0/1/2/3, default 0):"
|
|
||||||
read input
|
|
||||||
|
|
||||||
if [ -z "$input" ]; then
|
|
||||||
spi_mode=QIO
|
|
||||||
elif [ $input == 1 ]; then
|
|
||||||
spi_mode=QOUT
|
|
||||||
elif [ $input == 2 ]; then
|
|
||||||
spi_mode=DIO
|
|
||||||
elif [ $input == 3 ]; then
|
|
||||||
spi_mode=DOUT
|
|
||||||
else
|
|
||||||
spi_mode=QIO
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "spi mode: $spi_mode"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
echo "STEP 5: choose spi size and map"
|
|
||||||
echo " 0= 512KB( 256KB+ 256KB)"
|
|
||||||
echo " 2=1024KB( 512KB+ 512KB)"
|
|
||||||
echo " 3=2048KB( 512KB+ 512KB)"
|
|
||||||
echo " 4=4096KB( 512KB+ 512KB)"
|
|
||||||
echo " 5=2048KB(1024KB+1024KB)"
|
|
||||||
echo " 6=4096KB(1024KB+1024KB)"
|
|
||||||
echo "enter (0/2/3/4/5/6, default 0):"
|
|
||||||
read input
|
|
||||||
|
|
||||||
if [ -z "$input" ]; then
|
|
||||||
spi_size_map=0
|
|
||||||
echo "spi size: 512KB"
|
|
||||||
echo "spi ota map: 256KB + 256KB"
|
|
||||||
elif [ $input == 2 ]; then
|
|
||||||
spi_size_map=2
|
|
||||||
echo "spi size: 1024KB"
|
|
||||||
echo "spi ota map: 512KB + 512KB"
|
|
||||||
elif [ $input == 3 ]; then
|
|
||||||
spi_size_map=3
|
|
||||||
echo "spi size: 2048KB"
|
|
||||||
echo "spi ota map: 512KB + 512KB"
|
|
||||||
elif [ $input == 4 ]; then
|
|
||||||
spi_size_map=4
|
|
||||||
echo "spi size: 4096KB"
|
|
||||||
echo "spi ota map: 512KB + 512KB"
|
|
||||||
elif [ $input == 5 ]; then
|
|
||||||
spi_size_map=5
|
|
||||||
echo "spi size: 2048KB"
|
|
||||||
echo "spi ota map: 1024KB + 1024KB"
|
|
||||||
elif [ $input == 6 ]; then
|
|
||||||
spi_size_map=6
|
|
||||||
echo "spi size: 4096KB"
|
|
||||||
echo "spi ota map: 1024KB + 1024KB"
|
|
||||||
else
|
|
||||||
spi_size_map=0
|
|
||||||
echo "spi size: 512KB"
|
|
||||||
echo "spi ota map: 256KB + 256KB"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
echo "start..."
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
make clean
|
|
||||||
|
|
||||||
make BOOT=$boot APP=$app SPI_SPEED=$spi_speed SPI_MODE=$spi_mode SPI_SIZE_MAP=$spi_size_map
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
/* eagle.flash.bin @ 0x00000 */
|
|
||||||
/* eagle.irom0text.bin @ 0x10000 */
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Flash map for 512KB flash.
|
|
||||||
*
|
|
||||||
* There is 0x8000 (32KB) available between 0x8000 and 0x10000.
|
|
||||||
*
|
|
||||||
* Note: IROM images starts at 0x10000 (ignore Makefile output which says 0x40000.
|
|
||||||
*/
|
|
||||||
|
|
||||||
MEMORY
|
|
||||||
{
|
|
||||||
dport0_0_seg : org = 0x3FF00000, len = 0x10
|
|
||||||
dram0_0_seg : org = 0x3FFE8000, len = 0x18000
|
|
||||||
iram1_0_seg : org = 0x40100000, len = 0x8000
|
|
||||||
irom0_0_seg : org = 0x40210000, len = 0x6C000
|
|
||||||
}
|
|
||||||
|
|
||||||
INCLUDE "../ld/eagle.app.v6.common.ld"
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
This is a simple project template.
|
|
||||||
|
|
||||||
sample_lib is an example for multi-level folder Makefile, notice the folder structure and each Makefile, you can get the clue.
|
|
||||||
|
|
||||||
|
|
||||||
HOWTO:
|
|
||||||
1. Copy this folder to anywhere.
|
|
||||||
Example:
|
|
||||||
Copy to ~/workspace/project_template
|
|
||||||
You can rename this folder as you like.
|
|
||||||
|
|
||||||
2. Export SDK_PATH and BIN_PATH.
|
|
||||||
Example:
|
|
||||||
Your SDK path is ~/esp_iot_rtos_sdk, and want generate bin at ~/esp8266_bin.
|
|
||||||
Do follow steps:
|
|
||||||
1>. export SDK_PATH=~/esp_iot_rtos_sdk
|
|
||||||
2>. export BIN_PATH=~/esp8266_bin
|
|
||||||
SDK and project are seperate, you can update SDK without change your project.
|
|
||||||
|
|
||||||
3. Enter project_template folder, run ./gen_misc.sh, and follow the tips and steps.
|
|
||||||
|
|
||||||
|
|
||||||
Compile Options:
|
|
||||||
(1) COMPILE
|
|
||||||
Possible value: xcc
|
|
||||||
Default value:
|
|
||||||
If not set, use gcc by default.
|
|
||||||
|
|
||||||
(2) BOOT
|
|
||||||
Possible value: none/old/new
|
|
||||||
none: no need boot
|
|
||||||
old: use boot_v1.1
|
|
||||||
new: use boot_v1.2
|
|
||||||
Default value: new
|
|
||||||
|
|
||||||
(3) APP
|
|
||||||
Possible value: 0/1/2
|
|
||||||
0: original mode, generate eagle.app.v6.flash.bin and eagle.app.v6.irom0text.bin
|
|
||||||
1: generate user1
|
|
||||||
2: generate user2
|
|
||||||
Default value: 0
|
|
||||||
|
|
||||||
(3) SPI_SPEED
|
|
||||||
Possible value: 20/26.7/40/80
|
|
||||||
Default value: 40
|
|
||||||
|
|
||||||
(4) SPI_MODE
|
|
||||||
Possible value: QIO/QOUT/DIO/DOUT
|
|
||||||
Default value: QIO
|
|
||||||
|
|
||||||
(4) SPI_SIZE_MAP
|
|
||||||
Possible value: 0/2/3/4/5/6
|
|
||||||
Default value: 0
|
|
||||||
|
|
||||||
For example:
|
|
||||||
make COMPILE=gcc BOOT=new APP=1 SPI_SPEED=40 SPI_MODE=QIO SPI_SIZE_MAP=0
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
|
|
||||||
#############################################################
|
|
||||||
# Required variables for each makefile
|
|
||||||
# Discard this section from all parent makefiles
|
|
||||||
# Expected variables (with automatic defaults):
|
|
||||||
# CSRCS (all "C" files in the dir)
|
|
||||||
# SUBDIRS (all subdirs with a Makefile)
|
|
||||||
# GEN_LIBS - list of libs to be generated ()
|
|
||||||
# GEN_IMAGES - list of images to be generated ()
|
|
||||||
# COMPONENTS_xxx - a list of libs/objs in the form
|
|
||||||
# subdir/lib to be extracted and rolled up into
|
|
||||||
# a generated lib/image xxx.a ()
|
|
||||||
#
|
|
||||||
ifndef PDIR
|
|
||||||
GEN_LIBS = libuser.a
|
|
||||||
endif
|
|
||||||
|
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# Configuration i.e. compile options etc.
|
|
||||||
# Target specific stuff (defines etc.) goes in here!
|
|
||||||
# Generally values applying to a tree are captured in the
|
|
||||||
# makefile at its root level - these are then overridden
|
|
||||||
# for a subtree within the makefile rooted therein
|
|
||||||
#
|
|
||||||
DEFINES += -DMG_DISABLE_DAV -DMG_DISABLE_SYNC_RESOLVER -DMG_DISABLE_CGI \
|
|
||||||
-DMG_DISABLE_SOCKETPAIR -DMG_DISABLE_DIRECTORY_LISTING \
|
|
||||||
-DMG_MAX_HTTP_HEADERS=20 -DMG_MAX_HTTP_REQUEST_SIZE=1024 \
|
|
||||||
-DMG_MAX_PATH=40 -DMG_MAX_HTTP_SEND_IOBUF=1024 \
|
|
||||||
-DMG_ESP8266 -DRTOS_SDK -DMG_LWIP
|
|
||||||
|
|
||||||
#############################################################
|
|
||||||
# Recursion Magic - Don't touch this!!
|
|
||||||
#
|
|
||||||
# Each subtree potentially has an include directory
|
|
||||||
# corresponding to the common APIs applicable to modules
|
|
||||||
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
|
||||||
# of a module can only contain the include directories up
|
|
||||||
# its parent path, and not its siblings
|
|
||||||
#
|
|
||||||
# Required for each makefile to inherit from the parent
|
|
||||||
#
|
|
||||||
|
|
||||||
INCLUDES += -I ./
|
|
||||||
PDIR := ../$(PDIR)
|
|
||||||
sinclude $(PDIR)Makefile
|
|
||||||
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#include <math.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "esp_common.h"
|
|
||||||
|
|
||||||
/* Makes fprintf(stdout) and stderr work. */
|
|
||||||
_ssize_t _write_r(struct _reent *r, int fd, void *buf, size_t len) {
|
|
||||||
if (fd == 1 || fd == 2) {
|
|
||||||
size_t i;
|
|
||||||
for (i = 0; i < len; i++) {
|
|
||||||
os_putc(((char *) buf)[i]);
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* You'll need to implement _open_r and friends if you want file operations. See
|
|
||||||
* https://github.com/cesanta/smart.js/blob/master/platforms/esp8266/user/esp_fs.c
|
|
||||||
* for SPIFFS-based implementation.
|
|
||||||
*/
|
|
||||||
|
|
||||||
void abort(void) __attribute__((no_instrument_function));
|
|
||||||
void abort(void) {
|
|
||||||
/* cause an unaligned access exception, that will drop you into gdb */
|
|
||||||
*(int *) 1 = 1;
|
|
||||||
while (1)
|
|
||||||
; /* avoid gcc warning because stdlib abort() has noreturn attribute */
|
|
||||||
}
|
|
||||||
|
|
||||||
void _exit(int status) {
|
|
||||||
printf("_exit(%d)\n", status);
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) {
|
|
||||||
uint32_t time = system_get_time();
|
|
||||||
tp->tv_sec = time / 1000000;
|
|
||||||
tp->tv_usec = time % 1000000;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.c
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
../../../mongoose.h
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "esp_common.h"
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
#define AP_SSID "Mongoose"
|
|
||||||
#define AP_PASS "Mongoose"
|
|
||||||
#define AP_CHAN 9
|
|
||||||
#define MG_LISTEN_ADDR "80"
|
|
||||||
|
|
||||||
#define MG_TASK_STACK_SIZE 2048
|
|
||||||
#define MG_TASK_PRIORITY 1
|
|
||||||
|
|
||||||
void uart_div_modify(int uart_no, unsigned int freq);
|
|
||||||
|
|
||||||
void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
|
||||||
static const char *reply_fmt =
|
|
||||||
"HTTP/1.0 200 OK\r\n"
|
|
||||||
"Connection: close\r\n"
|
|
||||||
"Content-Type: text/plain\r\n"
|
|
||||||
"\r\n"
|
|
||||||
"Hello %s\n";
|
|
||||||
LOG(LL_DEBUG, ("conn %p ev %d", nc, ev));
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_ACCEPT: {
|
|
||||||
char addr[32];
|
|
||||||
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
|
|
||||||
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
|
|
||||||
LOG(LL_INFO, ("Connection %p from %s", nc, addr));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case MG_EV_HTTP_REQUEST: {
|
|
||||||
char addr[32];
|
|
||||||
struct http_message *hm = (struct http_message *) p;
|
|
||||||
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
|
|
||||||
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
|
|
||||||
LOG(LL_INFO,
|
|
||||||
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
|
|
||||||
hm->method.p, (int) hm->uri.len, hm->uri.p));
|
|
||||||
mg_printf(nc, reply_fmt, addr);
|
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case MG_EV_CLOSE: {
|
|
||||||
LOG(LL_INFO, ("Connection %p closed", nc));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup_ap() {
|
|
||||||
struct softap_config cfg;
|
|
||||||
|
|
||||||
wifi_set_opmode_current(SOFTAP_MODE);
|
|
||||||
memset(&cfg, 0, sizeof(cfg));
|
|
||||||
strcpy((char *) cfg.ssid, AP_SSID);
|
|
||||||
strcpy((char *) cfg.password, AP_PASS);
|
|
||||||
cfg.ssid_len = strlen((const char *) cfg.ssid);
|
|
||||||
cfg.authmode =
|
|
||||||
strlen((const char *) cfg.password) ? AUTH_WPA2_PSK : AUTH_OPEN;
|
|
||||||
cfg.channel = AP_CHAN;
|
|
||||||
cfg.ssid_hidden = 0;
|
|
||||||
cfg.max_connection = 10;
|
|
||||||
cfg.beacon_interval = 100; /* ms */
|
|
||||||
|
|
||||||
LOG(LL_INFO, ("Setting up AP '%s' on channel %d", cfg.ssid, cfg.channel));
|
|
||||||
wifi_softap_set_config_current(&cfg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mg_task(void *arg) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
|
|
||||||
cs_log_set_level(LL_INFO);
|
|
||||||
|
|
||||||
LOG(LL_INFO, ("SDK version: %s", system_get_sdk_version()));
|
|
||||||
setup_ap();
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
|
|
||||||
nc = mg_bind(&mgr, MG_LISTEN_ADDR, ev_handler);
|
|
||||||
if (nc == NULL) {
|
|
||||||
LOG(LL_ERROR, ("Error setting up listener!"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
xTaskHandle s_mg_task_handle;
|
|
||||||
|
|
||||||
void user_init(void) {
|
|
||||||
uart_div_modify(0, UART_CLK_FREQ / 115200);
|
|
||||||
|
|
||||||
setvbuf(stdout, NULL, _IONBF, 0);
|
|
||||||
setvbuf(stderr, NULL, _IONBF, 0);
|
|
||||||
|
|
||||||
xTaskCreate(mg_task, (const signed char *) "mongoose", MG_TASK_STACK_SIZE,
|
|
||||||
NULL, MG_TASK_PRIORITY, &s_mg_task_handle);
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
# Copyright (c) 2014 Cesanta Software
|
|
||||||
# All rights reserved
|
|
||||||
|
|
||||||
SUBDIRS = $(sort $(dir $(wildcard */)))
|
|
||||||
X = $(SUBDIRS)
|
|
||||||
|
|
||||||
.PHONY: $(SUBDIRS)
|
|
||||||
|
|
||||||
all: $(SUBDIRS)
|
|
||||||
|
|
||||||
$(SUBDIRS):
|
|
||||||
@$(MAKE) -C $@
|
|
||||||
|
|
||||||
clean:
|
|
||||||
for d in $(SUBDIRS) ; do $(MAKE) -C $$d clean ; done
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
PROG = api_server
|
|
||||||
SOURCES = $(PROG).c sqlite3.c db_plugin_sqlite.c ../../mongoose.c
|
|
||||||
CFLAGS = -W -Wall -pthread $(CFLAGS_EXTRA)
|
|
||||||
|
|
||||||
ifeq ($(OS), Windows_NT)
|
|
||||||
else
|
|
||||||
UNAME_S := $(shell uname -s)
|
|
||||||
ifeq ($(UNAME_S), Linux)
|
|
||||||
CFLAGS += -ldl -lm
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
all: $(PROG)
|
|
||||||
|
|
||||||
$(PROG): $(SOURCES)
|
|
||||||
$(CC) $(SOURCES) -o $@ $(CFLAGS)
|
|
||||||
|
|
||||||
$(PROG).exe: $(SOURCES)
|
|
||||||
cl $(SOURCES) /I.. /MD /Fe$@
|
|
||||||
|
|
||||||
test: $(PROG)
|
|
||||||
sh unit_test.sh $$(pwd)/$(PROG)
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
|
|
||||||
@@ -1,104 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "db_plugin.h"
|
|
||||||
|
|
||||||
static const char *s_http_port = "8000";
|
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
|
||||||
static int s_sig_num = 0;
|
|
||||||
static void *s_db_handle = NULL;
|
|
||||||
static const char *s_db_path = "api_server.db";
|
|
||||||
static const struct mg_str s_get_method = MG_STR("GET");
|
|
||||||
static const struct mg_str s_put_method = MG_STR("PUT");
|
|
||||||
static const struct mg_str s_delele_method = MG_STR("DELETE");
|
|
||||||
|
|
||||||
static void signal_handler(int sig_num) {
|
|
||||||
signal(sig_num, signal_handler);
|
|
||||||
s_sig_num = sig_num;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int has_prefix(const struct mg_str *uri, const struct mg_str *prefix) {
|
|
||||||
return uri->len > prefix->len && memcmp(uri->p, prefix->p, prefix->len) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int is_equal(const struct mg_str *s1, const struct mg_str *s2) {
|
|
||||||
return s1->len == s2->len && memcmp(s1->p, s2->p, s2->len) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
static const struct mg_str api_prefix = MG_STR("/api/v1");
|
|
||||||
struct http_message *hm = (struct http_message *) ev_data;
|
|
||||||
struct mg_str key;
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_HTTP_REQUEST:
|
|
||||||
if (has_prefix(&hm->uri, &api_prefix)) {
|
|
||||||
key.p = hm->uri.p + api_prefix.len;
|
|
||||||
key.len = hm->uri.len - api_prefix.len;
|
|
||||||
if (is_equal(&hm->method, &s_get_method)) {
|
|
||||||
db_op(nc, hm, &key, s_db_handle, API_OP_GET);
|
|
||||||
} else if (is_equal(&hm->method, &s_put_method)) {
|
|
||||||
db_op(nc, hm, &key, s_db_handle, API_OP_SET);
|
|
||||||
} else if (is_equal(&hm->method, &s_delele_method)) {
|
|
||||||
db_op(nc, hm, &key, s_db_handle, API_OP_DEL);
|
|
||||||
} else {
|
|
||||||
mg_printf(nc, "%s",
|
|
||||||
"HTTP/1.0 501 Not Implemented\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
/* Open listening socket */
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
s_http_server_opts.document_root = "web_root";
|
|
||||||
|
|
||||||
/* Parse command line arguments */
|
|
||||||
for (i = 1; i < argc; i++) {
|
|
||||||
if (strcmp(argv[i], "-D") == 0) {
|
|
||||||
mgr.hexdump_file = argv[++i];
|
|
||||||
} else if (strcmp(argv[i], "-f") == 0) {
|
|
||||||
s_db_path = argv[++i];
|
|
||||||
} else if (strcmp(argv[i], "-r") == 0) {
|
|
||||||
s_http_server_opts.document_root = argv[++i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
signal(SIGINT, signal_handler);
|
|
||||||
signal(SIGTERM, signal_handler);
|
|
||||||
|
|
||||||
/* Open database */
|
|
||||||
if ((s_db_handle = db_open(s_db_path)) == NULL) {
|
|
||||||
fprintf(stderr, "Cannot open DB [%s]\n", s_db_path);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Run event loop until signal is received */
|
|
||||||
printf("Starting RESTful server on port %s\n", s_http_port);
|
|
||||||
while (s_sig_num == 0) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Cleanup */
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
db_close(&s_db_handle);
|
|
||||||
|
|
||||||
printf("Exiting on signal %d\n", s_sig_num);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef DB_PLUGIN_HEADER_DEFINED
|
|
||||||
#define DB_PLUGIN_HEADER_DEFINED
|
|
||||||
|
|
||||||
#include "../../mongoose.h"
|
|
||||||
|
|
||||||
void *db_open(const char *db_path);
|
|
||||||
void db_close(void **db_handle);
|
|
||||||
|
|
||||||
enum { API_OP_GET, API_OP_SET, API_OP_DEL };
|
|
||||||
|
|
||||||
void db_op(struct mg_connection *nc, const struct http_message *hm,
|
|
||||||
const struct mg_str *key, void *db, int op);
|
|
||||||
|
|
||||||
#endif /* DB_PLUGIN_HEADER_DEFINED */
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "db_plugin.h"
|
|
||||||
#include "sqlite3.h"
|
|
||||||
|
|
||||||
void *db_open(const char *db_path) {
|
|
||||||
sqlite3 *db = NULL;
|
|
||||||
if (sqlite3_open_v2(db_path, &db, SQLITE_OPEN_READWRITE |
|
|
||||||
SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX, NULL) == SQLITE_OK) {
|
|
||||||
sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS kv(key PRIMARY KEY, val);",
|
|
||||||
0, 0, 0);
|
|
||||||
}
|
|
||||||
return db;
|
|
||||||
}
|
|
||||||
|
|
||||||
void db_close(void **db_handle) {
|
|
||||||
if (db_handle != NULL && *db_handle != NULL) {
|
|
||||||
sqlite3_close(*db_handle);
|
|
||||||
*db_handle = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void op_set(struct mg_connection *nc, const struct http_message *hm,
|
|
||||||
const struct mg_str *key, void *db) {
|
|
||||||
sqlite3_stmt *stmt = NULL;
|
|
||||||
char value[200];
|
|
||||||
const struct mg_str *body = hm->query_string.len > 0 ?
|
|
||||||
&hm->query_string : &hm->body;
|
|
||||||
|
|
||||||
mg_get_http_var(body, "value", value, sizeof(value));
|
|
||||||
if (sqlite3_prepare_v2(db, "INSERT OR REPLACE INTO kv VALUES (?, ?);",
|
|
||||||
-1, &stmt, NULL) == SQLITE_OK) {
|
|
||||||
sqlite3_bind_text(stmt, 1, key->p, key->len, SQLITE_STATIC);
|
|
||||||
sqlite3_bind_text(stmt, 2, value, strlen(value), SQLITE_STATIC);
|
|
||||||
sqlite3_step(stmt);
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
}
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void op_get(struct mg_connection *nc, const struct http_message *hm,
|
|
||||||
const struct mg_str *key, void *db) {
|
|
||||||
sqlite3_stmt *stmt = NULL;
|
|
||||||
const char *data = NULL;
|
|
||||||
int result;
|
|
||||||
(void) hm;
|
|
||||||
|
|
||||||
if (sqlite3_prepare_v2(db, "SELECT val FROM kv WHERE key = ?;",
|
|
||||||
-1, &stmt, NULL) == SQLITE_OK) {
|
|
||||||
sqlite3_bind_text(stmt, 1, key->p, key->len, SQLITE_STATIC);
|
|
||||||
result = sqlite3_step(stmt);
|
|
||||||
data = (char *) sqlite3_column_text(stmt, 0);
|
|
||||||
if ((result == SQLITE_OK || result == SQLITE_ROW) && data != NULL) {
|
|
||||||
mg_printf(nc, "HTTP/1.1 200 OK\r\n"
|
|
||||||
"Content-Type: text/plain\r\n"
|
|
||||||
"Content-Length: %d\r\n\r\n%s",
|
|
||||||
(int) strlen(data), data);
|
|
||||||
} else {
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 404 Not Found\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
}
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
} else {
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 500 Server Error\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void op_del(struct mg_connection *nc, const struct http_message *hm,
|
|
||||||
const struct mg_str *key, void *db) {
|
|
||||||
sqlite3_stmt *stmt = NULL;
|
|
||||||
int result;
|
|
||||||
(void) hm;
|
|
||||||
|
|
||||||
if (sqlite3_prepare_v2(db, "DELETE FROM kv WHERE key = ?;",
|
|
||||||
-1, &stmt, NULL) == SQLITE_OK) {
|
|
||||||
sqlite3_bind_text(stmt, 1, key->p, key->len, SQLITE_STATIC);
|
|
||||||
result = sqlite3_step(stmt);
|
|
||||||
if (result == SQLITE_OK || result == SQLITE_ROW) {
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
|
||||||
} else {
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 404 Not Found\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
}
|
|
||||||
sqlite3_finalize(stmt);
|
|
||||||
} else {
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 500 Server Error\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void db_op(struct mg_connection *nc, const struct http_message *hm,
|
|
||||||
const struct mg_str *key, void *db, int op) {
|
|
||||||
switch (op) {
|
|
||||||
case API_OP_GET: op_get(nc, hm, key, db); break;
|
|
||||||
case API_OP_SET: op_set(nc, hm, key, db); break;
|
|
||||||
case API_OP_DEL: op_del(nc, hm, key, db); break;
|
|
||||||
default:
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.0 501 Not Implemented\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
PROG=$1
|
|
||||||
PORT=${2:-8000} # If second param is given, this is load balancer port
|
|
||||||
DB_FILE=/tmp/_$$.db
|
|
||||||
URL=http://127.0.0.1:$PORT/api/v1
|
|
||||||
|
|
||||||
cleanup() {
|
|
||||||
rm -rf $DB_FILE
|
|
||||||
kill -9 $PID >/dev/null 2>&1
|
|
||||||
}
|
|
||||||
|
|
||||||
#set -x
|
|
||||||
trap cleanup EXIT
|
|
||||||
|
|
||||||
cleanup
|
|
||||||
$PROG -f $DB_FILE &
|
|
||||||
PID=$!
|
|
||||||
|
|
||||||
#sleep 1
|
|
||||||
curl -s -X PUT -d 'value=123' $URL/foo
|
|
||||||
curl -s -X PUT -d 'value=success' $URL/bar/baz
|
|
||||||
|
|
||||||
# Fetch existing key
|
|
||||||
RESULT=$(curl -s $URL/bar/baz)
|
|
||||||
test "$RESULT" = "success" || exit 1
|
|
||||||
|
|
||||||
# Delete it
|
|
||||||
curl -s -X DELETE $URL/bar/baz
|
|
||||||
|
|
||||||
# Make sure it's deleted - GET must result in 404
|
|
||||||
RESULT=$(curl -s -i $URL/bar/baz | head -1 | tr -d '\r')
|
|
||||||
test "$RESULT" = "HTTP/1.1 404 Not Found" || exit 1
|
|
||||||
|
|
||||||
exit 0
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>RESTful API demo</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<style type="text/css">
|
|
||||||
* { outline: none; }
|
|
||||||
body {
|
|
||||||
background-color: #789; margin: 0;
|
|
||||||
padding: 0; font: 16px/1.4 Helvetica, Arial, sans-serif;
|
|
||||||
font: 16px/1.4 Helvetica, Arial, sans-serif;
|
|
||||||
}
|
|
||||||
div.content {
|
|
||||||
width: 800px; margin: 2em auto; padding: 20px 50px;
|
|
||||||
background-color: #fff; border-radius: 1em;
|
|
||||||
}
|
|
||||||
label { display: inline-block; min-width: 7em; }
|
|
||||||
input { border: 1px solid #ccc; padding: 0.2em; }
|
|
||||||
a:link, a:visited { color: #69c; text-decoration: none; }
|
|
||||||
@media (max-width: 700px) {
|
|
||||||
body { background-color: #fff; }
|
|
||||||
div.content { width: auto; margin: 0 auto; padding: 1em; }
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
||||||
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
|
|
||||||
<script language="javascript" type="text/javascript">
|
|
||||||
jQuery(function() {
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="content">
|
|
||||||
<h1>API server</h1>
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
# This "makefile" is only intended to prevent errors during main makefile execution
|
|
||||||
all:
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
# Arduino Restful Client
|
|
||||||
|
|
||||||
This example demonstrates how to use [Mongoose](https://www.cesanta.com/mongoose) to send HTTP commands from Arduino.
|
|
||||||
|
|
||||||
Example sends free memory size and current board uptime, but it can be modified to send any user-specific data.
|
|
||||||
|
|
||||||
At the moment this example supports [Arduino Mega 2560](http://arduino.cc/en/Main/ArduinoBoardMega2560) board (and compatible) with either W5100-based
|
|
||||||
network shield (like [Arduino Ethernet Shield](http://arduino.cc/en/Main/ArduinoEthernetShield)) or [CC3000](http://www.ti.com/product/cc3000)-based WIFI Shield.
|
|
||||||
|
|
||||||
## Build and run instructions:
|
|
||||||
|
|
||||||
###To run with Arduino Ethernet (W5100) shield:
|
|
||||||
1. Add (Sketch->Add file...) the following files to sketch:
|
|
||||||
- /mongoose/mongoose.h
|
|
||||||
- /mongoose/mongoose.c
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
3. Buils and start (in console) /Users/alex/Projects/mongoose/examples/restful_server example
|
|
||||||
4. Make `board_ip` and `board_mac` variables suitable for your network and board
|
|
||||||
5. Change IP address in `s_target_address` variable to IP address of host running restful_server
|
|
||||||
6. Uncomment line `#include <Ethernet.h>`
|
|
||||||
7. Compile and flash sketch
|
|
||||||
8. restful_server will start to show current uptime and free memory size (with 5 seconds interval)
|
|
||||||
|
|
||||||
###To run with Adafruit WiFi (CC3000) shield:
|
|
||||||
1. Add (Sketch->Add files...) the following files to sketch:
|
|
||||||
- /mongoose/mongoose.h
|
|
||||||
- /mongoose/mongoose.c
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
2. Import Adafruit CC3000 library for mongoose
|
|
||||||
(select Sketch->Import Library...->Add library... and point
|
|
||||||
/mongoose/platforms/arduino_wifi_CC3000/adafruit_CC3000_lib_mongoose folder)
|
|
||||||
3. Buils and start (in console) /Users/alex/Projects/mongoose/examples/restful_server example
|
|
||||||
4. Make the following variables suitable for your network
|
|
||||||
- `board_ip`
|
|
||||||
- `subnet_mask`
|
|
||||||
- `gateway`
|
|
||||||
- `dns`
|
|
||||||
- `wlan_ssid`
|
|
||||||
- `wlan_pwd`
|
|
||||||
- `wlan_security`
|
|
||||||
5. Change IP address in `s_target_address` variable to IP address of host running restful_server
|
|
||||||
6. Uncomment line `#include <Adafruit_CC3000.h>`
|
|
||||||
8. Compile and flash sketch
|
|
||||||
9. restful_server will start to show current uptime and free memory size (with 5 seconds interval)
|
|
||||||
@@ -1,141 +0,0 @@
|
|||||||
|
|
||||||
/*
|
|
||||||
* Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*
|
|
||||||
* Build and run instructions:
|
|
||||||
* To run with Arduino Ethernet (W5100) shield:
|
|
||||||
* -----------------------------------------------------------
|
|
||||||
* 1. Add (Sketch->Add file...) the following files to sketch:
|
|
||||||
* - /mongoose/mongoose.h
|
|
||||||
* - /mongoose/mongoose.c
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
* 2. Buils and run in console /Users/alex/Projects/mongoose/examples/restful_server example
|
|
||||||
* 3. Make board_ip and board_mac variables suitable for your network and board
|
|
||||||
* 4. Uncomment line #include <Ethernet.h>
|
|
||||||
* 5. Change IP address in s_target_address variable to IP address of host running restful_server
|
|
||||||
* 6. Compile & flash sketch
|
|
||||||
* 7. restful_server server will start to show current uptime and free memory size (with 5 second interval)
|
|
||||||
*
|
|
||||||
* To run with Adafruit WiFi (CC3000) shield:
|
|
||||||
* -----------------------------------------------------------
|
|
||||||
* 1. Add (Sketch->Add files...) the following files to sketch:
|
|
||||||
* - /mongoose/mongoose.h
|
|
||||||
* - /mongoose/mongoose.c
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
* 2. Import Adafruit CC3000 library for mongoose (select Sketch->Import Library...->Add library... and point
|
|
||||||
* /mongoose/platforms/arduino_wifi_CC3000/adafruit_CC3000_lib_mongoose folder
|
|
||||||
* 3. Buils and run in console /Users/alex/Projects/mongoose/examples/restful_server example
|
|
||||||
* 4. Make the following variables suitable for your network
|
|
||||||
* - board_ip
|
|
||||||
* - subnet_mask
|
|
||||||
* - gateway
|
|
||||||
* - dns
|
|
||||||
* - wlan_ssid
|
|
||||||
* - wlan_pwd
|
|
||||||
* - wlan_security
|
|
||||||
* 5. Uncomment line #include <Adafruit_CC3000.h>
|
|
||||||
* 6. Compile & flash sketch
|
|
||||||
* 7. restful_server server will start to show current uptime and free memory size (with 5 second interval) *
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
//#include <Ethernet.h>
|
|
||||||
//#include <Adafruit_CC3000.h>
|
|
||||||
#include <SPI.h>
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
// CHANGE THESE VARIABLES
|
|
||||||
// NB: Devices with the same address must not end up on the same network.
|
|
||||||
// Use MAC address provided by device manufacturer (e.g. on a sticker).
|
|
||||||
// If there isn't one, use a random one from the locally administered range.
|
|
||||||
// See http://en.wikipedia.org/wiki/MAC_address for details.
|
|
||||||
static uint8_t board_mac[] = {
|
|
||||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
|
||||||
};
|
|
||||||
static uint8_t board_ip[] = {192, 168, 10, 177};
|
|
||||||
|
|
||||||
#ifdef WIFI_CC3000
|
|
||||||
static uint8_t subnet_mask[] = {255, 255, 255, 0};
|
|
||||||
static uint8_t gateway[] = {192, 168, 10, 254};
|
|
||||||
static uint8_t dns_ip[] = {192, 168, 10, 254};
|
|
||||||
|
|
||||||
static const char *wlan_ssid = "mynetwork";
|
|
||||||
static const char *wlan_pwd = "mypassword";
|
|
||||||
static int wlan_security = WLAN_SEC_WPA2;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static const char *s_target_address = "192.168.10.3:8000";
|
|
||||||
|
|
||||||
/////////////////////////////////////////////
|
|
||||||
|
|
||||||
static const char *s_request = "/printcontent";
|
|
||||||
|
|
||||||
static uint32_t IP2U32(uint8_t* iparr) {
|
|
||||||
return ((uint32_t)iparr[0] << 24) | ((uint32_t)iparr[1] << 16) | (iparr[2] << 8) | (iparr[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int get_data_to_send(char* buf, int buf_size) {
|
|
||||||
// Adding data to send
|
|
||||||
// It could be any sensor data, now just put uptime & free memory size here
|
|
||||||
return snprintf(buf, buf_size, "Uptime: %lus Free memory: %db",
|
|
||||||
millis()/1000, get_freememsize());
|
|
||||||
}
|
|
||||||
static void rfc_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
int connect_status;
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case NS_CONNECT:
|
|
||||||
connect_status = * (int *) ev_data;
|
|
||||||
|
|
||||||
if (connect_status == 0) {
|
|
||||||
char buf[100];
|
|
||||||
int len = get_data_to_send(buf, sizeof(buf));
|
|
||||||
mg_printf(nc, "POST %s HTTP/1.0\r\nHost: %s\r\nContent-Lenght: %d"
|
|
||||||
"\r\n\r\n%s", s_request, s_target_address, len, buf);
|
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
||||||
} else {
|
|
||||||
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct mg_mgr mgr;
|
|
||||||
static struct mg_connection *nc;
|
|
||||||
|
|
||||||
void setup()
|
|
||||||
{
|
|
||||||
Serial.begin(9600);
|
|
||||||
Serial.println("Initialization...");
|
|
||||||
#if defined(ETHERNET_W5100)
|
|
||||||
avr_netinit(board_mac, board_ip);
|
|
||||||
#elif defined(WIFI_CC3000)
|
|
||||||
if (avr_netinit(wlan_ssid, wlan_pwd, wlan_security, IP2U32(board_ip),
|
|
||||||
IP2U32(subnet_mask), IP2U32(gateway), IP2U32(dns_ip)) != 0) {
|
|
||||||
Serial.println("Initialization error, check network settings");
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
Serial.println("Initialization done");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
nc = mg_connect(&mgr, s_target_address, rfc_ev_handler);
|
|
||||||
if (nc != NULL) {
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t time_to_finish = millis() + 5000;
|
|
||||||
while (millis() < time_to_finish) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
# This "makefile" is only intended to prevent errors during main makefile execution
|
|
||||||
all:
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
# Arduino Restful Server
|
|
||||||
|
|
||||||
This example demonstrates how to use [Mongoose](https://www.cesanta.com/mongoose) to control Arduino
|
|
||||||
using HTTP requests.
|
|
||||||
Example just blinks by LED when Mongoose receives HTTP command, but it can be modified to execute any user-specific code.
|
|
||||||
|
|
||||||
At the moment this example supports [Arduino Mega 2560](http://arduino.cc/en/Main/ArduinoBoardMega2560) board (and compatible) with either W5100-based
|
|
||||||
network shield (like [Arduino Ethernet Shield](http://arduino.cc/en/Main/ArduinoEthernetShield)) or [CC3000](http://www.ti.com/product/cc3000)-based WIFI Shield.
|
|
||||||
|
|
||||||
## Build and run instructions:
|
|
||||||
|
|
||||||
###To run with Arduino Ethernet (W5100) shield:
|
|
||||||
1. Add (Sketch->Add file...) the following files to sketch:
|
|
||||||
- /mongoose/mongoose.h
|
|
||||||
- /mongoose/mongoose.c
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
2. Make `board_ip` and `board_mac` variables suitable for your network and board
|
|
||||||
3. Uncomment line `#include <Ethernet.h>`
|
|
||||||
4. Compile and flash sketch
|
|
||||||
5. Run `curl http://<board_ip/blink`
|
|
||||||
LED attached to PIN 13 will blink and board free memory size and board uptime will be displayed.
|
|
||||||
|
|
||||||
|
|
||||||
###To run with Adafruit WiFi (CC3000) shield:
|
|
||||||
1. Add (Sketch->Add files...) the following files to sketch:
|
|
||||||
- /mongoose/mongoose.h
|
|
||||||
- /mongoose/mongoose.c
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
- /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
2. Import Adafruit CC3000 library for mongoose
|
|
||||||
(select Sketch->Import Library...->Add library... and point
|
|
||||||
/mongoose/platforms/arduino_wifi_CC3000/adafruit_CC3000_lib_mongoose folder)
|
|
||||||
3. Make the following variables suitable for your network
|
|
||||||
- `board_ip`
|
|
||||||
- `subnet_mask`
|
|
||||||
- `gateway`
|
|
||||||
- `dns`
|
|
||||||
- `wlan_ssid`
|
|
||||||
- `wlan_pwd`
|
|
||||||
- `wlan_security`
|
|
||||||
5. Uncomment line `#include <Adafruit_CC3000.h>`
|
|
||||||
4. Compile and flash sketch
|
|
||||||
5. Run curl `http://<board_ip/blink`
|
|
||||||
LED attached to PIN 13 will blink and board free memory size and board uptime will be displayed.
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*
|
|
||||||
* Build and run instructions:
|
|
||||||
* To run with Arduino Ethernet (W5100) shield:
|
|
||||||
* -----------------------------------------------------------
|
|
||||||
* 1. Add (Sketch->Add file...) the following files to sketch:
|
|
||||||
* - /mongoose/mongoose.h
|
|
||||||
* - /mongoose/mongoose.c
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
* 2. Make board_ip and board_mac variables suitable for your network and board
|
|
||||||
* 3. Uncomment line #include <Ethernet.h>
|
|
||||||
* 4. Compile & flash sketch
|
|
||||||
* 5. Run curl http://<board_ip/blink
|
|
||||||
* LED attached to PIN 13 will blink and board free memory size and uptime will responsed
|
|
||||||
*
|
|
||||||
* To run with Adafruit WiFi (CC3000) shield:
|
|
||||||
* -----------------------------------------------------------
|
|
||||||
* 1. Add (Sketch->Add files...) the following files to sketch:
|
|
||||||
* - /mongoose/mongoose.h
|
|
||||||
* - /mongoose/mongoose.c
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.h
|
|
||||||
* - /mongoose/platforms/arduino_ethernet_W5100/avrsupport.cpp
|
|
||||||
* 2. Import Adafruit CC3000 library for mongoose (select Sketch->Import Library...->Add library... and point
|
|
||||||
* /mongoose/platforms/arduino_wifi_CC3000/adafruit_CC3000_lib_mongoose folder
|
|
||||||
* 3. Make the following variables suitable for your network
|
|
||||||
* - board_ip
|
|
||||||
* - subnet_mask
|
|
||||||
* - gateway
|
|
||||||
* - dns
|
|
||||||
* - wlan_ssid
|
|
||||||
* - wlan_pwd
|
|
||||||
* - wlan_security
|
|
||||||
* 5. Uncomment line #include <Adafruit_CC3000.h>
|
|
||||||
* 4. Compile & flash sketch
|
|
||||||
* 5. Run curl http://<board_ip/blink
|
|
||||||
* LED attached to PIN 13 will blink and board free memory size and uptime will responsed
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
//#include <Ethernet.h>
|
|
||||||
//#include <Adafruit_CC3000.h>
|
|
||||||
#include <SPI.h>
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
// CHANGE THESE VARIABLES
|
|
||||||
// NB: Devices with the same address must not end up on the same network.
|
|
||||||
// Use MAC address provided by device manufacturer (e.g. on a sticker).
|
|
||||||
// If there isn't one, use a random one from the locally administered range.
|
|
||||||
// See http://en.wikipedia.org/wiki/MAC_address for details.
|
|
||||||
static uint8_t board_mac[] = {
|
|
||||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
|
||||||
};
|
|
||||||
|
|
||||||
static uint8_t board_ip[] = {192, 168, 10, 8};
|
|
||||||
|
|
||||||
#ifdef WIFI_CC3000
|
|
||||||
static uint8_t subnet_mask[] = {255, 255, 255, 0};
|
|
||||||
static uint8_t gateway[] = {192, 168, 10, 254};
|
|
||||||
static uint8_t dns_ip[] = {192, 168, 10, 254};
|
|
||||||
|
|
||||||
static const char *wlan_ssid = "mynetwork";
|
|
||||||
static const char *wlan_pwd = "mypassword";
|
|
||||||
static int wlan_security = WLAN_SEC_WPA2;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
///////////////////////////////////////////////
|
|
||||||
|
|
||||||
static const char *s_http_port = "60000";
|
|
||||||
|
|
||||||
static uint32_t IP2U32(uint8_t* iparr) {
|
|
||||||
return ((uint32_t)iparr[0] << 24) | ((uint32_t)iparr[1] << 16) | (iparr[2] << 8) | (iparr[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rfs_ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
struct http_message *hm = (struct http_message *) ev_data;
|
|
||||||
char buf[100];
|
|
||||||
int clen;
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case NS_HTTP_REQUEST:
|
|
||||||
if (mg_vcmp(&hm->uri, "/blink") == 0) {
|
|
||||||
blink(1, 500);
|
|
||||||
}
|
|
||||||
|
|
||||||
clen = snprintf(buf, sizeof(buf),
|
|
||||||
"Free memory size: %d Uptime: %d",
|
|
||||||
(int)get_freememsize(), (int)time(NULL));
|
|
||||||
|
|
||||||
mg_printf_http_chunk(nc, "HTTP/1.1 200 OK\r\n"
|
|
||||||
"Content-Length: %d\r\n"
|
|
||||||
"Transfer-Encoding: chunked\r\n\r\n"
|
|
||||||
"%s",
|
|
||||||
clen, buf);
|
|
||||||
|
|
||||||
mg_send_http_chunk(nc, "", 0);
|
|
||||||
break;
|
|
||||||
case NS_SEND:
|
|
||||||
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct mg_connection *nc;
|
|
||||||
static struct mg_mgr mgr;
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(9600);
|
|
||||||
Serial.println("Initialization...");
|
|
||||||
#if defined(ETHERNET_W5100)
|
|
||||||
avr_netinit(board_mac, board_ip);
|
|
||||||
#elif defined(WIFI_CC3000)
|
|
||||||
if (avr_netinit(wlan_ssid, wlan_pwd, wlan_security, IP2U32(board_ip),
|
|
||||||
IP2U32(subnet_mask), IP2U32(gateway), IP2U32(dns_ip)) != 0) {
|
|
||||||
Serial.println("Initialization error, check network settings");
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
nc = mg_bind(&mgr, s_http_port, rfs_ev_handler);
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
Serial.println("Initialization done");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
PROG = big_upload
|
|
||||||
include ../examples.mk
|
|
||||||
@@ -1,153 +0,0 @@
|
|||||||
// Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
//
|
|
||||||
// This example demonstrates how to handle very large requests without keeping
|
|
||||||
// them in memory.
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
static const char *s_http_port = "8000";
|
|
||||||
|
|
||||||
struct file_writer_data {
|
|
||||||
FILE *fp;
|
|
||||||
size_t bytes_left;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void handle_request(struct mg_connection *nc) {
|
|
||||||
// This handler gets called only when we don't reset proto_handler in
|
|
||||||
// handle_recv, so we just return a page unconditionally.
|
|
||||||
mg_printf(nc, "%s",
|
|
||||||
"HTTP/1.1 200 OK\r\n"
|
|
||||||
"Content-Type: text/html\r\n"
|
|
||||||
"Connection: close\r\n"
|
|
||||||
"\r\n"
|
|
||||||
"<html><body>Upload example."
|
|
||||||
"<form method=\"POST\" action=\"/upload\" "
|
|
||||||
" enctype=\"multipart/form-data\">"
|
|
||||||
"<input type=\"file\" name=\"file\" /> <br/>"
|
|
||||||
"<input type=\"submit\" value=\"Upload\" />"
|
|
||||||
"</form></body></html>");
|
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_recv(struct mg_connection *nc) {
|
|
||||||
struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
|
|
||||||
|
|
||||||
if (data == NULL) {
|
|
||||||
// This is a new connection, try to parse HTTP request.
|
|
||||||
struct http_message hm;
|
|
||||||
int req_len = mg_parse_http(nc->recv_mbuf.buf, nc->recv_mbuf.len, &hm,
|
|
||||||
1 /* is_req */);
|
|
||||||
|
|
||||||
if (req_len < 0 ||
|
|
||||||
(req_len == 0 && nc->recv_mbuf.len >= MG_MAX_HTTP_REQUEST_SIZE)) {
|
|
||||||
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
|
||||||
} else if (req_len == 0) {
|
|
||||||
// Request is not complete yet, do nothing.
|
|
||||||
} else if (mg_vcasecmp(&hm.method, "POST") == 0 &&
|
|
||||||
mg_vcmp(&hm.uri, "/upload") == 0) {
|
|
||||||
// This is the request that we don't want to buffer in memory.
|
|
||||||
|
|
||||||
if (hm.body.len == (size_t) ~0 || hm.body.len == 0) {
|
|
||||||
mg_printf(nc, "%s",
|
|
||||||
"HTTP/1.1 411 Content-Length required\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset proto_handler so Mongoose's http_handler() won't get confused
|
|
||||||
// once we start removing data from the buffer.
|
|
||||||
nc->proto_handler = NULL;
|
|
||||||
|
|
||||||
// Headers will be inaccessible later, so put everything we need into
|
|
||||||
// user_data.
|
|
||||||
data = calloc(1, sizeof(struct file_writer_data));
|
|
||||||
data->bytes_left = hm.body.len;
|
|
||||||
data->fp = tmpfile();
|
|
||||||
if (data->fp == NULL) {
|
|
||||||
mg_printf(nc, "%s",
|
|
||||||
"HTTP/1.1 500 Failed to open a file\r\n"
|
|
||||||
"Content-Length: 0\r\n\r\n");
|
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
nc->user_data = (void *) data;
|
|
||||||
|
|
||||||
// Remove the headers from the buffer.
|
|
||||||
mbuf_remove(&nc->recv_mbuf, hm.body.p - nc->recv_mbuf.buf);
|
|
||||||
|
|
||||||
// Invoke itself again to write the piece of the body that is already in
|
|
||||||
// the buffer.
|
|
||||||
handle_recv(nc);
|
|
||||||
}
|
|
||||||
} else { // data != NULL
|
|
||||||
size_t to_write = data->bytes_left, written = 0;
|
|
||||||
if (nc->recv_mbuf.len < to_write) to_write = nc->recv_mbuf.len;
|
|
||||||
written = fwrite(nc->recv_mbuf.buf, 1, to_write, data->fp);
|
|
||||||
mbuf_remove(&nc->recv_mbuf, written);
|
|
||||||
data->bytes_left -= written;
|
|
||||||
if (data->bytes_left <= 0) {
|
|
||||||
// Request is complete, do something meaningful here.
|
|
||||||
|
|
||||||
mg_printf(nc,
|
|
||||||
"HTTP/1.1 200 OK\r\n"
|
|
||||||
"Content-Type: text/plain\r\n"
|
|
||||||
"Connection: close\r\n\r\n"
|
|
||||||
"Written %ld of POST data to a temp file\n\n",
|
|
||||||
(long) ftell(data->fp));
|
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
||||||
// handle_close will free the resources.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure we free all allocated resources
|
|
||||||
static void handle_close(struct mg_connection *nc) {
|
|
||||||
struct file_writer_data *data = (struct file_writer_data *) nc->user_data;
|
|
||||||
|
|
||||||
if (data != NULL) {
|
|
||||||
fclose(data->fp);
|
|
||||||
free(data);
|
|
||||||
nc->user_data = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
(void) ev_data;
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_HTTP_REQUEST:
|
|
||||||
// Invoked when the full HTTP request is in the buffer (including body).
|
|
||||||
handle_request(nc);
|
|
||||||
break;
|
|
||||||
case MG_EV_RECV:
|
|
||||||
// Invoked every time new data arrives.
|
|
||||||
handle_recv(nc);
|
|
||||||
break;
|
|
||||||
case MG_EV_CLOSE:
|
|
||||||
handle_close(nc);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
||||||
|
|
||||||
// Set up HTTP server parameters
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
|
|
||||||
printf("Starting web server on port %s\n", s_http_port);
|
|
||||||
for (;;) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
PROG ?= example
|
||||||
|
CFLAGS ?= -DMG_IO_SIZE=8192 -DMG_ENABLE_LINES
|
||||||
|
|
||||||
|
all: $(PROG)
|
||||||
|
$(DEBUGGER) ./$(PROG) $(ARGS)
|
||||||
|
|
||||||
|
$(PROG): main.c
|
||||||
|
$(CC) ../../mongoose.c -I../.. -W -Wall $(CFLAGS) -o $(PROG) main.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
// Copyright (c) 2020 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
//
|
||||||
|
// Example captive DNS server (captive portal).
|
||||||
|
// 1. Run `make`. This builds and starts a server on port 5533
|
||||||
|
// 2. Run `dig -t A www.google.com -4 @localhost -p 5533`
|
||||||
|
|
||||||
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
static const char *s_listen_url = "udp://0.0.0.0:5533";
|
||||||
|
|
||||||
|
// DNS answer section. We response with IP 1.2.3.4 - you can change it
|
||||||
|
// in the last 4 bytes of this array
|
||||||
|
uint8_t answer[] = {
|
||||||
|
0xc0, 0x0c, // Point to the name in the DNS question
|
||||||
|
0, 1, // 2 bytes - record type, A
|
||||||
|
0, 1, // 2 bytes - address class, INET
|
||||||
|
0, 0, 0, 120, // 4 bytes - TTL
|
||||||
|
0, 4, // 2 bytes - address length
|
||||||
|
1, 2, 3, 4 // 4 bytes - IP address
|
||||||
|
};
|
||||||
|
|
||||||
|
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||||
|
if (ev == MG_EV_READ) {
|
||||||
|
struct mg_dns_rr rr; // Parse first question, offset 12 is header size
|
||||||
|
size_t n = mg_dns_parse_rr(c->recv.buf, c->recv.len, 12, true, &rr);
|
||||||
|
if (n > 0) {
|
||||||
|
char buf[12 + n + sizeof(answer)];
|
||||||
|
struct mg_dns_header *h = (struct mg_dns_header *) buf;
|
||||||
|
memset(buf, 0, sizeof(buf)); // Clear the whole datagram
|
||||||
|
h->txnid = ((struct mg_dns_header *) c->recv.buf)->txnid; // Copy tnxid
|
||||||
|
h->num_questions = mg_htons(1); // We use only the 1st question
|
||||||
|
h->num_answers = mg_htons(1); // And only one answer
|
||||||
|
h->flags = mg_htons(0x8400); // Authoritative response
|
||||||
|
memcpy(buf + sizeof(*h), c->recv.buf + sizeof(*h), n); // Copy question
|
||||||
|
memcpy(buf + sizeof(*h) + n, answer, sizeof(answer)); // And answer
|
||||||
|
mg_send(c, buf, sizeof(buf)); // And send it!
|
||||||
|
}
|
||||||
|
mg_iobuf_delete(&c->recv, c->recv.len);
|
||||||
|
}
|
||||||
|
(void) fn_data;
|
||||||
|
(void) ev_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
struct mg_mgr mgr;
|
||||||
|
|
||||||
|
mg_log_set("3"); // Set log level
|
||||||
|
mg_mgr_init(&mgr); // Initialise event manager
|
||||||
|
mg_listen(&mgr, s_listen_url, fn, NULL); // Start DNS server
|
||||||
|
for (;;) mg_mgr_poll(&mgr, 1000); // Event loop
|
||||||
|
mg_mgr_free(&mgr);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
PROG = captive_dns_server
|
|
||||||
MODULE_CFLAGS=-DMG_ENABLE_DNS_SERVER
|
|
||||||
|
|
||||||
include ../rules.mk
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2014 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Try it out with:
|
|
||||||
* $ dig -t A www.google.com -4 @localhost -p 5533
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "../../mongoose.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
static int s_exit_flag = 0;
|
|
||||||
static in_addr_t s_our_ip_addr;
|
|
||||||
static const char *s_listening_addr = "udp://:5533";
|
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
struct mg_dns_message *msg;
|
|
||||||
struct mg_dns_resource_record *rr;
|
|
||||||
struct mg_dns_reply reply;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case MG_DNS_MESSAGE:
|
|
||||||
msg = (struct mg_dns_message *) ev_data;
|
|
||||||
reply = mg_dns_create_reply(&nc->send_mbuf, msg);
|
|
||||||
|
|
||||||
for (i = 0; i < msg->num_questions; i++) {
|
|
||||||
rr = &msg->questions[i];
|
|
||||||
if (rr->rtype == MG_DNS_A_RECORD) {
|
|
||||||
mg_dns_reply_record(&reply, rr, NULL, rr->rtype, 3600,
|
|
||||||
&s_our_ip_addr, 4);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* We don't set the error flag even if there were no answers
|
|
||||||
* maching the MG_DNS_A_RECORD query type.
|
|
||||||
* This indicates that we have (syntetic) answers for MG_DNS_A_RECORD.
|
|
||||||
* See http://goo.gl/QWvufr for a distinction between NXDOMAIN and NODATA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
mg_dns_send_reply(nc, &reply);
|
|
||||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
s_our_ip_addr = inet_addr("127.0.0.1");
|
|
||||||
|
|
||||||
/* Parse command line arguments */
|
|
||||||
for (i = 1; i < argc; i++) {
|
|
||||||
if (strcmp(argv[i], "-D") == 0) {
|
|
||||||
mgr.hexdump_file = argv[++i];
|
|
||||||
} else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) {
|
|
||||||
s_listening_addr = argv[++i];
|
|
||||||
} else {
|
|
||||||
s_our_ip_addr = inet_addr(argv[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fprintf(stderr, "Listening on '%s'\n", s_listening_addr);
|
|
||||||
if ((nc = mg_bind(&mgr, s_listening_addr, ev_handler)) == NULL) {
|
|
||||||
fprintf(stderr, "cannot bind to socket\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
mg_set_protocol_dns(nc);
|
|
||||||
|
|
||||||
while (s_exit_flag == 0) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
PROG = coap_client
|
|
||||||
SOURCES = $(PROG).c ../../mongoose.c
|
|
||||||
CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA) -DMG_ENABLE_COAP
|
|
||||||
|
|
||||||
all: $(PROG)
|
|
||||||
|
|
||||||
$(PROG): $(SOURCES)
|
|
||||||
$(CC) $(SOURCES) -o $@ $(CFLAGS)
|
|
||||||
|
|
||||||
$(PROG).exe: $(SOURCES)
|
|
||||||
cl $(SOURCES) /I../.. /MD /Fe$@
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
|
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*
|
|
||||||
* This program sends CoAP CON-message to server (coap.me by default)
|
|
||||||
* and waits for answer.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
static int s_time_to_exit = 0;
|
|
||||||
static char *s_default_address = "udp://coap.me:5683";
|
|
||||||
|
|
||||||
static void coap_handler(struct mg_connection *nc, int ev, void *p) {
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_CONNECT: {
|
|
||||||
struct mg_coap_message cm;
|
|
||||||
uint32_t res;
|
|
||||||
|
|
||||||
memset(&cm, 0, sizeof(cm));
|
|
||||||
cm.msg_id = 1;
|
|
||||||
cm.msg_type = MG_COAP_MSG_CON;
|
|
||||||
printf("Sending CON...\n");
|
|
||||||
res = mg_coap_send_message(nc, &cm);
|
|
||||||
if (res == 0) {
|
|
||||||
printf("Sent CON with msg_id = %d\n", cm.msg_id);
|
|
||||||
} else {
|
|
||||||
printf("Error: %d\n", res);
|
|
||||||
s_time_to_exit = 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case MG_EV_COAP_ACK:
|
|
||||||
case MG_EV_COAP_RST: {
|
|
||||||
struct mg_coap_message *cm = (struct mg_coap_message *) p;
|
|
||||||
printf("ACK/RST for message with msg_id = %d received\n", cm->msg_id);
|
|
||||||
s_time_to_exit = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
char *address = s_default_address;
|
|
||||||
|
|
||||||
if (argc > 1) {
|
|
||||||
address = argv[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Using %s as CoAP server\n", address);
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, 0);
|
|
||||||
|
|
||||||
nc = mg_connect(&mgr, address, coap_handler);
|
|
||||||
if (nc == NULL) {
|
|
||||||
printf("Unable to connect to %s\n", address);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
mg_set_protocol_coap(nc);
|
|
||||||
|
|
||||||
while (!s_time_to_exit) {
|
|
||||||
mg_mgr_poll(&mgr, 1000000);
|
|
||||||
}
|
|
||||||
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
PROG = coap_server
|
|
||||||
SOURCES = $(PROG).c ../../mongoose.c
|
|
||||||
CFLAGS = -W -Wall -I../.. $(CFLAGS_EXTRA) -DMG_ENABLE_COAP
|
|
||||||
|
|
||||||
all: $(PROG)
|
|
||||||
|
|
||||||
$(PROG): $(SOURCES)
|
|
||||||
$(CC) $(SOURCES) -o $@ $(CFLAGS)
|
|
||||||
|
|
||||||
$(PROG).exe: $(SOURCES)
|
|
||||||
cl $(SOURCES) /I../.. /MD /Fe$@
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf *.gc* *.dSYM *.exe *.obj *.o a.out $(PROG)
|
|
||||||
@@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
* All rights reserved
|
|
||||||
*
|
|
||||||
* This program listens on 5683 for CoAP messages,
|
|
||||||
* sends ACK is nessesary and dump everything received.
|
|
||||||
* It is possible to use ../coap_client to send message.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
static char *s_default_address = "udp://:5683";
|
|
||||||
static int s_sig_received = 0;
|
|
||||||
|
|
||||||
static void signal_handler(int sig_num) {
|
|
||||||
signal(sig_num, signal_handler);
|
|
||||||
s_sig_received = sig_num;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void coap_handler(struct mg_connection *nc, int ev, void *p) {
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_COAP_CON: {
|
|
||||||
uint32_t res;
|
|
||||||
struct mg_coap_message *cm = (struct mg_coap_message *) p;
|
|
||||||
printf("CON with msg_id = %d received\n", cm->msg_id);
|
|
||||||
res = mg_coap_send_ack(nc, cm->msg_id);
|
|
||||||
if (res == 0) {
|
|
||||||
printf("Successfully sent ACK for message with msg_id = %d\n",
|
|
||||||
cm->msg_id);
|
|
||||||
} else {
|
|
||||||
printf("Error: %d\n", res);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case MG_EV_COAP_NOC:
|
|
||||||
case MG_EV_COAP_ACK:
|
|
||||||
case MG_EV_COAP_RST: {
|
|
||||||
struct mg_coap_message *cm = (struct mg_coap_message *) p;
|
|
||||||
printf("ACK/RST/NOC with msg_id = %d received\n", cm->msg_id);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
|
|
||||||
signal(SIGTERM, signal_handler);
|
|
||||||
signal(SIGINT, signal_handler);
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, 0);
|
|
||||||
|
|
||||||
nc = mg_bind(&mgr, s_default_address, coap_handler);
|
|
||||||
if (nc == NULL) {
|
|
||||||
printf("Unable to start listener at %s\n", s_default_address);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Listening for CoAP messages at %s\n", s_default_address);
|
|
||||||
|
|
||||||
mg_set_protocol_coap(nc);
|
|
||||||
|
|
||||||
while (!s_sig_received) {
|
|
||||||
mg_mgr_poll(&mgr, 1000000);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Exiting on signal %d\n", s_sig_received);
|
|
||||||
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
PROG ?= example
|
||||||
|
|
||||||
|
all: $(PROG)
|
||||||
|
$(DEBUGGER) ./$(PROG)
|
||||||
|
|
||||||
|
$(PROG):
|
||||||
|
$(CC) ../../mongoose.c -I../.. $(CFLAGS) $(EXTRA) -o $(PROG) main.c mjson.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb log.txt
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
# A complete device dashboard
|
||||||
|
|
||||||
|
This example is a demonstration of how Mongoose Library could be integrated
|
||||||
|
into an embedded device and provide a complete device dashbord with the
|
||||||
|
following features:
|
||||||
|
|
||||||
|
- Multiple logins with different permissions (admin and user)
|
||||||
|
- Login screen for non-authenticated connections
|
||||||
|
- A [preact](https://preactjs.com/)-based dashboard with multiple pages
|
||||||
|
- Internal variables
|
||||||
|
- A form that changes those variables
|
||||||
|
- All changes are propagates to all connected clients
|
||||||
|
- Live log stream
|
||||||
|
- Live video stream
|
||||||
|
|
||||||
|
|
||||||
|
In essense, this example is a combination of several other examples
|
||||||
|
pulled together (multiple-logins, dashboard, live-log, video-stream).
|
||||||
|
|
||||||
|
# Screenshots
|
||||||
|
|
||||||
|
This is a login screen that prompts for user/password
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
# Main dashboard
|
||||||
|
|
||||||
|
A main dashboard page shows internal data, live video stream, and a form
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
A live log page shows live log stream coming from device
|
||||||
|
|
||||||
|

|
||||||
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 9.8 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 9.7 KiB |
|
After Width: | Height: | Size: 10 KiB |
@@ -0,0 +1,204 @@
|
|||||||
|
// Copyright (c) 2020 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
|
||||||
|
#include "mjson.h"
|
||||||
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
// Authenticated user.
|
||||||
|
// A user can be authenticated by:
|
||||||
|
// - a name:pass pair
|
||||||
|
// - a token
|
||||||
|
// When a user is shown a login screen, she enters a user:pass. If successful,
|
||||||
|
// a server returns user info which includes token. From that point on,
|
||||||
|
// client can use token for authentication. Tokens could be refreshed/changed
|
||||||
|
// on a server side, forcing clients to re-login.
|
||||||
|
struct user {
|
||||||
|
const char *name, *pass, *token;
|
||||||
|
};
|
||||||
|
|
||||||
|
// This is a configuration structure we're going to show on a dashboard
|
||||||
|
static struct config {
|
||||||
|
int value1;
|
||||||
|
char *value2;
|
||||||
|
} s_config = {123, NULL};
|
||||||
|
|
||||||
|
// Stringifies the config. A caller must free() it.
|
||||||
|
static char *stringify_config(struct config *cfg) {
|
||||||
|
char *s = NULL;
|
||||||
|
mjson_printf(mjson_print_dynamic_buf, &s, "{%Q:%d,%Q:%Q}", "value1",
|
||||||
|
cfg->value1, "value2", cfg->value2);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update config structure. Return true if changed, false otherwise
|
||||||
|
static bool update_config(struct mg_http_message *hm, struct config *cfg) {
|
||||||
|
bool changed = false;
|
||||||
|
char buf[256];
|
||||||
|
double dv;
|
||||||
|
if (mjson_get_number(hm->body.ptr, hm->body.len, "$.value1", &dv)) {
|
||||||
|
s_config.value1 = dv;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
if (mjson_get_string(hm->body.ptr, hm->body.len, "$.value2", buf,
|
||||||
|
sizeof(buf)) > 0) {
|
||||||
|
free(s_config.value2);
|
||||||
|
s_config.value2 = strdup(buf);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse HTTP requests, return authenticated user or NULL
|
||||||
|
static struct user *getuser(struct mg_http_message *hm) {
|
||||||
|
// In production, make passwords strong and tokens randomly generated
|
||||||
|
// In this example, user list is kept in RAM. In production, it can
|
||||||
|
// be backed by file, database, or some other method.
|
||||||
|
static struct user users[] = {
|
||||||
|
{"admin", "admin", "admin_token"},
|
||||||
|
{"user1", "pass1", "user1_token"},
|
||||||
|
{"user2", "pass2", "user2_token"},
|
||||||
|
{NULL, NULL, NULL},
|
||||||
|
};
|
||||||
|
char user[256], pass[256];
|
||||||
|
struct user *u;
|
||||||
|
mg_http_creds(hm, user, sizeof(user), pass, sizeof(pass));
|
||||||
|
if (user[0] != '\0' && pass[0] != '\0') {
|
||||||
|
// Both user and password is set, search by user/password
|
||||||
|
for (u = users; u->name != NULL; u++)
|
||||||
|
if (strcmp(user, u->name) == 0 && strcmp(pass, u->pass) == 0) return u;
|
||||||
|
} else if (user[0] == '\0') {
|
||||||
|
// Only password is set, search by token
|
||||||
|
for (u = users; u->name != NULL; u++)
|
||||||
|
if (strcmp(pass, u->token) == 0) return u;
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify all config watchers about the config change
|
||||||
|
static void notify_config_change(struct mg_mgr *mgr) {
|
||||||
|
struct mg_connection *c;
|
||||||
|
char *s = stringify_config(&s_config);
|
||||||
|
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||||
|
if (c->label[0] == 'W') mg_http_printf_chunk(c, "%s\n", s);
|
||||||
|
}
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTP request handler function
|
||||||
|
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||||
|
if (ev == MG_EV_HTTP_MSG) {
|
||||||
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||||
|
struct user *u = getuser(hm);
|
||||||
|
// LOG(LL_INFO, ("%p [%.*s] auth %s", c->fd, (int) hm->uri.len, hm->uri.ptr,
|
||||||
|
// u ? u->name : "NULL"));
|
||||||
|
if (u == NULL && mg_http_match_uri(hm, "/api/#")) {
|
||||||
|
// All URIs starting with /api/ must be authenticated
|
||||||
|
mg_printf(c, "%s", "HTTP/1.1 403 Denied\r\nContent-Length: 0\r\n\r\n");
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||||
|
char *s = stringify_config(&s_config);
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s\n",
|
||||||
|
(int) strlen(s) + 1, s);
|
||||||
|
free(s);
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||||
|
// Admins only
|
||||||
|
if (strcmp(u->name, "admin") == 0) {
|
||||||
|
if (update_config(hm, &s_config)) notify_config_change(fn_data);
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
||||||
|
} else {
|
||||||
|
mg_printf(c, "%s", "HTTP/1.1 403 Denied\r\nContent-Length: 0\r\n\r\n");
|
||||||
|
}
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/config/watch")) {
|
||||||
|
c->label[0] = 'W'; // Mark ourselves as a config watcher
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/video1")) {
|
||||||
|
c->label[0] = 'S'; // Mark that connection as live streamer
|
||||||
|
mg_printf(
|
||||||
|
c, "%s",
|
||||||
|
"HTTP/1.0 200 OK\r\n"
|
||||||
|
"Cache-Control: no-cache\r\n"
|
||||||
|
"Pragma: no-cache\r\nExpires: Thu, 01 Dec 1994 16:00:00 GMT\r\n"
|
||||||
|
"Content-Type: multipart/x-mixed-replace; boundary=--foo\r\n\r\n");
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/log/static")) {
|
||||||
|
mg_http_serve_file(c, hm, "log.txt", "text/plain", "");
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/log/live")) {
|
||||||
|
c->label[0] = 'L'; // Mark that connection as live log listener
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/login")) {
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
|
mg_http_printf_chunk(c, "{\"user\":\"%s\",\"token\":\"%s\"}\n", u->name,
|
||||||
|
u->token);
|
||||||
|
mg_http_printf_chunk(c, "");
|
||||||
|
} else {
|
||||||
|
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||||
|
mg_http_serve_dir(c, ev_data, &opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The image stream is simulated by sending MJPEG frames specified by the
|
||||||
|
// "files" array of file names.
|
||||||
|
static void broadcast_mjpeg_frame(struct mg_mgr *mgr) {
|
||||||
|
const char *files[] = {"images/1.jpg", "images/2.jpg", "images/3.jpg",
|
||||||
|
"images/4.jpg", "images/5.jpg", "images/6.jpg"};
|
||||||
|
size_t nfiles = sizeof(files) / sizeof(files[0]);
|
||||||
|
static size_t i;
|
||||||
|
const char *path = files[i++ % nfiles];
|
||||||
|
size_t size = mg_file_size(path);
|
||||||
|
char *data = mg_file_read(path); // Read next file
|
||||||
|
struct mg_connection *c;
|
||||||
|
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||||
|
if (c->label[0] != 'S') continue; // Skip non-stream connections
|
||||||
|
if (data == NULL || size == 0) continue; // Skip on file read error
|
||||||
|
mg_printf(c,
|
||||||
|
"--foo\r\nContent-Type: image/jpeg\r\n"
|
||||||
|
"Content-Length: %lu\r\n\r\n",
|
||||||
|
(unsigned long) size);
|
||||||
|
mg_send(c, data, size);
|
||||||
|
mg_send(c, "\r\n", 2);
|
||||||
|
}
|
||||||
|
free(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mjpeg_cb(void *arg) {
|
||||||
|
broadcast_mjpeg_frame(arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_message(const char *filename, const char *message) {
|
||||||
|
FILE *fp = fopen(filename, "a");
|
||||||
|
if (fp != NULL) {
|
||||||
|
fprintf(fp, "%s", message);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void broadcast_message(struct mg_mgr *mgr, const char *message) {
|
||||||
|
struct mg_connection *c;
|
||||||
|
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||||
|
if (c->label[0] == 'L') mg_http_printf_chunk(c, "%s", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Timer function - called periodically.
|
||||||
|
// Prepare log message. Save it to a file, and broadcast.
|
||||||
|
static void log_cb(void *arg) {
|
||||||
|
char buf[64];
|
||||||
|
snprintf(buf, sizeof(buf), "Time is: %lu\n", (unsigned long) time(NULL));
|
||||||
|
log_message("log.txt", buf);
|
||||||
|
broadcast_message(arg, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
struct mg_mgr mgr;
|
||||||
|
struct mg_timer t1, t2;
|
||||||
|
|
||||||
|
mg_mgr_init(&mgr);
|
||||||
|
mg_http_listen(&mgr, "http://localhost:8000", cb, &mgr);
|
||||||
|
mg_timer_init(&t1, 500, MG_TIMER_REPEAT, mjpeg_cb, &mgr);
|
||||||
|
mg_timer_init(&t2, 1000, MG_TIMER_REPEAT, log_cb, &mgr);
|
||||||
|
for (;;) mg_mgr_poll(&mgr, 50);
|
||||||
|
mg_timer_free(&t1);
|
||||||
|
mg_timer_free(&t2);
|
||||||
|
mg_mgr_free(&mgr);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,981 @@
|
|||||||
|
// Copyright (c) 2018-2020 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
#include "mjson.h"
|
||||||
|
|
||||||
|
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||||
|
#else
|
||||||
|
#define va_copy(x, y) (x) = (y)
|
||||||
|
#define snprintf _snprintf
|
||||||
|
#define alloca _alloca
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int mjson_esc(int c, int esc) {
|
||||||
|
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
|
||||||
|
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
|
||||||
|
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mjson_pass_string(const char *s, int len) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
if (s[i] == '\\' && i + 1 < len && mjson_esc(s[i + 1], 1)) {
|
||||||
|
i++;
|
||||||
|
} else if (s[i] == '\0') {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
} else if (s[i] == '"') {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson(const char *s, int len, mjson_cb_t cb, void *ud) {
|
||||||
|
enum { S_VALUE, S_KEY, S_COLON, S_COMMA_OR_EOO } expecting = S_VALUE;
|
||||||
|
unsigned char nesting[MJSON_MAX_DEPTH];
|
||||||
|
int i, depth = 0;
|
||||||
|
#define MJSONCALL(ev) \
|
||||||
|
if (cb != NULL && cb(ev, s, start, i - start + 1, ud)) return i + 1;
|
||||||
|
|
||||||
|
// In the ascii table, the distance between `[` and `]` is 2.
|
||||||
|
// Ditto for `{` and `}`. Hence +2 in the code below.
|
||||||
|
#define MJSONEOO() \
|
||||||
|
do { \
|
||||||
|
if (c != nesting[depth - 1] + 2) return MJSON_ERROR_INVALID_INPUT; \
|
||||||
|
depth--; \
|
||||||
|
if (depth == 0) { \
|
||||||
|
MJSONCALL(tok); \
|
||||||
|
return i + 1; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
int start = i;
|
||||||
|
unsigned char c = ((unsigned char *) s)[i];
|
||||||
|
int tok = c;
|
||||||
|
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
|
||||||
|
// printf("- %c [%.*s] %d %d\n", c, i, s, depth, expecting);
|
||||||
|
switch (expecting) {
|
||||||
|
case S_VALUE:
|
||||||
|
if (c == '{') {
|
||||||
|
if (depth >= (int) sizeof(nesting)) return MJSON_ERROR_TOO_DEEP;
|
||||||
|
nesting[depth++] = c;
|
||||||
|
expecting = S_KEY;
|
||||||
|
break;
|
||||||
|
} else if (c == '[') {
|
||||||
|
if (depth >= (int) sizeof(nesting)) return MJSON_ERROR_TOO_DEEP;
|
||||||
|
nesting[depth++] = c;
|
||||||
|
break;
|
||||||
|
} else if (c == ']' && depth > 0) { // Empty array
|
||||||
|
MJSONEOO();
|
||||||
|
} else if (c == 't' && i + 3 < len && memcmp(&s[i], "true", 4) == 0) {
|
||||||
|
i += 3;
|
||||||
|
tok = MJSON_TOK_TRUE;
|
||||||
|
} else if (c == 'n' && i + 3 < len && memcmp(&s[i], "null", 4) == 0) {
|
||||||
|
i += 3;
|
||||||
|
tok = MJSON_TOK_NULL;
|
||||||
|
} else if (c == 'f' && i + 4 < len && memcmp(&s[i], "false", 5) == 0) {
|
||||||
|
i += 4;
|
||||||
|
tok = MJSON_TOK_FALSE;
|
||||||
|
} else if (c == '-' || ((c >= '0' && c <= '9'))) {
|
||||||
|
char *end = NULL;
|
||||||
|
strtod(&s[i], &end);
|
||||||
|
if (end != NULL) i += end - &s[i] - 1;
|
||||||
|
tok = MJSON_TOK_NUMBER;
|
||||||
|
} else if (c == '"') {
|
||||||
|
int n = mjson_pass_string(&s[i + 1], len - i - 1);
|
||||||
|
if (n < 0) return n;
|
||||||
|
i += n + 1;
|
||||||
|
tok = MJSON_TOK_STRING;
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
if (depth == 0) {
|
||||||
|
MJSONCALL(tok);
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
expecting = S_COMMA_OR_EOO;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case S_KEY:
|
||||||
|
if (c == '"') {
|
||||||
|
int n = mjson_pass_string(&s[i + 1], len - i - 1);
|
||||||
|
if (n < 0) return n;
|
||||||
|
i += n + 1;
|
||||||
|
tok = MJSON_TOK_KEY;
|
||||||
|
expecting = S_COLON;
|
||||||
|
} else if (c == '}') { // Empty object
|
||||||
|
MJSONEOO();
|
||||||
|
expecting = S_COMMA_OR_EOO;
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case S_COLON:
|
||||||
|
if (c == ':') {
|
||||||
|
expecting = S_VALUE;
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case S_COMMA_OR_EOO:
|
||||||
|
if (depth <= 0) return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
if (c == ',') {
|
||||||
|
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
|
||||||
|
} else if (c == ']' || c == '}') {
|
||||||
|
MJSONEOO();
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
MJSONCALL(tok);
|
||||||
|
}
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct msjon_get_data {
|
||||||
|
const char *path; // Lookup json path
|
||||||
|
int pos; // Current path index
|
||||||
|
int d1; // Current depth of traversal
|
||||||
|
int d2; // Expected depth of traversal
|
||||||
|
int i1; // Index in an array
|
||||||
|
int i2; // Expected index in an array
|
||||||
|
int obj; // If the value is array/object, offset where it starts
|
||||||
|
const char **tokptr; // Destination
|
||||||
|
int *toklen; // Destination length
|
||||||
|
int tok; // Returned token
|
||||||
|
};
|
||||||
|
|
||||||
|
static int mjson_plen(const char *s) {
|
||||||
|
int i = 0;
|
||||||
|
while (s[i] != '\0' && s[i] != '.' && s[i] != '[') i++;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mjson_get_cb(int tok, const char *s, int off, int len, void *ud) {
|
||||||
|
struct msjon_get_data *data = (struct msjon_get_data *) ud;
|
||||||
|
// printf("--> %2x %2d %2d %2d %2d\t'%s'\t'%.*s'\t\t'%.*s'\n", tok, data->d1,
|
||||||
|
// data->d2, data->i1, data->i2, data->path + data->pos, off, s, len,
|
||||||
|
// s + off);
|
||||||
|
if (data->tok != MJSON_TOK_INVALID) return 1; // Found
|
||||||
|
|
||||||
|
if (tok == '{') {
|
||||||
|
if (!data->path[data->pos] && data->d1 == data->d2) data->obj = off;
|
||||||
|
data->d1++;
|
||||||
|
} else if (tok == '[') {
|
||||||
|
if (data->d1 == data->d2 && data->path[data->pos] == '[') {
|
||||||
|
data->i1 = 0;
|
||||||
|
data->i2 = (int) strtod(&data->path[data->pos + 1], NULL);
|
||||||
|
if (data->i1 == data->i2) {
|
||||||
|
data->d2++;
|
||||||
|
data->pos += 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!data->path[data->pos] && data->d1 == data->d2) data->obj = off;
|
||||||
|
data->d1++;
|
||||||
|
} else if (tok == ',') {
|
||||||
|
if (data->d1 == data->d2 + 1) {
|
||||||
|
data->i1++;
|
||||||
|
if (data->i1 == data->i2) {
|
||||||
|
while (data->path[data->pos] != ']') data->pos++;
|
||||||
|
data->pos++;
|
||||||
|
data->d2++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (tok == MJSON_TOK_KEY && data->d1 == data->d2 + 1 &&
|
||||||
|
data->path[data->pos] == '.' && s[off] == '"' &&
|
||||||
|
s[off + len - 1] == '"' &&
|
||||||
|
mjson_plen(&data->path[data->pos + 1]) == len - 2 &&
|
||||||
|
!memcmp(s + off + 1, &data->path[data->pos + 1], len - 2)) {
|
||||||
|
data->d2++;
|
||||||
|
data->pos += len - 1;
|
||||||
|
} else if (tok == MJSON_TOK_KEY && data->d1 == data->d2) {
|
||||||
|
return 1; // Exhausted path, not found
|
||||||
|
} else if (tok == '}' || tok == ']') {
|
||||||
|
data->d1--;
|
||||||
|
// data->d2--;
|
||||||
|
if (!data->path[data->pos] && data->d1 == data->d2 && data->obj != -1) {
|
||||||
|
data->tok = tok - 2;
|
||||||
|
if (data->tokptr) *data->tokptr = s + data->obj;
|
||||||
|
if (data->toklen) *data->toklen = off - data->obj + 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
} else if (MJSON_TOK_IS_VALUE(tok)) {
|
||||||
|
// printf("TOK --> %d\n", tok);
|
||||||
|
if (data->d1 == data->d2 && !data->path[data->pos]) {
|
||||||
|
data->tok = tok;
|
||||||
|
if (data->tokptr) *data->tokptr = s + off;
|
||||||
|
if (data->toklen) *data->toklen = len;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum mjson_tok mjson_find(const char *s, int len, const char *jp,
|
||||||
|
const char **tokptr, int *toklen) {
|
||||||
|
struct msjon_get_data data = {jp, 1, 0, 0, 0,
|
||||||
|
0, -1, tokptr, toklen, MJSON_TOK_INVALID};
|
||||||
|
if (jp[0] != '$') return MJSON_TOK_INVALID;
|
||||||
|
if (mjson(s, len, mjson_get_cb, &data) < 0) return MJSON_TOK_INVALID;
|
||||||
|
return (enum mjson_tok) data.tok;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_number(const char *s, int len, const char *path, double *v) {
|
||||||
|
const char *p;
|
||||||
|
int tok, n;
|
||||||
|
if ((tok = mjson_find(s, len, path, &p, &n)) == MJSON_TOK_NUMBER) {
|
||||||
|
if (v != NULL) *v = strtod(p, NULL);
|
||||||
|
}
|
||||||
|
return tok == MJSON_TOK_NUMBER ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_bool(const char *s, int len, const char *path, int *v) {
|
||||||
|
int tok = mjson_find(s, len, path, NULL, NULL);
|
||||||
|
if (tok == MJSON_TOK_TRUE && v != NULL) *v = 1;
|
||||||
|
if (tok == MJSON_TOK_FALSE && v != NULL) *v = 0;
|
||||||
|
return tok == MJSON_TOK_TRUE || tok == MJSON_TOK_FALSE ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mjson_unescape(const char *s, int len, char *to, int n) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0, j = 0; i < len && j < n; i++, j++) {
|
||||||
|
if (s[i] == '\\' && i + 1 < len) {
|
||||||
|
int c = mjson_esc(s[i + 1], 0);
|
||||||
|
if (c == 0) return -1;
|
||||||
|
to[j] = c;
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
to[j] = s[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j >= n) return -1;
|
||||||
|
if (n > 0) to[j] = '\0';
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_string(const char *s, int len, const char *path, char *to,
|
||||||
|
int n) {
|
||||||
|
const char *p;
|
||||||
|
int sz;
|
||||||
|
if (mjson_find(s, len, path, &p, &sz) != MJSON_TOK_STRING) return -1;
|
||||||
|
return mjson_unescape(p + 1, sz - 2, to, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_hex(const char *s, int len, const char *x, char *to, int n) {
|
||||||
|
const char *p;
|
||||||
|
int i, j, sz;
|
||||||
|
if (mjson_find(s, len, x, &p, &sz) != MJSON_TOK_STRING) return -1;
|
||||||
|
for (i = j = 0; i < sz - 3 && j < n; i += 2, j++) {
|
||||||
|
#define HEXTOI(x) (x >= '0' && x <= '9' ? x - '0' : x - 'W')
|
||||||
|
unsigned char a = *(const unsigned char *) (p + i + 1);
|
||||||
|
unsigned char b = *(const unsigned char *) (p + i + 2);
|
||||||
|
((unsigned char *) to)[j] = (HEXTOI(a) << 4) | HEXTOI(b);
|
||||||
|
}
|
||||||
|
if (j < n) to[j] = '\0';
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
static int mjson_base64rev(int c) {
|
||||||
|
if (c >= 'A' && c <= 'Z') {
|
||||||
|
return c - 'A';
|
||||||
|
} else if (c >= 'a' && c <= 'z') {
|
||||||
|
return c + 26 - 'a';
|
||||||
|
} else if (c >= '0' && c <= '9') {
|
||||||
|
return c + 52 - '0';
|
||||||
|
} else if (c == '+') {
|
||||||
|
return 62;
|
||||||
|
} else if (c == '/') {
|
||||||
|
return 63;
|
||||||
|
} else {
|
||||||
|
return 64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_base64_dec(const char *src, int n, char *dst, int dlen) {
|
||||||
|
const char *end = src + n;
|
||||||
|
int len = 0;
|
||||||
|
while (src + 3 < end && len < dlen) {
|
||||||
|
int a = mjson_base64rev(src[0]), b = mjson_base64rev(src[1]),
|
||||||
|
c = mjson_base64rev(src[2]), d = mjson_base64rev(src[3]);
|
||||||
|
dst[len++] = (a << 2) | (b >> 4);
|
||||||
|
if (src[2] != '=' && len < dlen) {
|
||||||
|
dst[len++] = (b << 4) | (c >> 2);
|
||||||
|
if (src[3] != '=' && len < dlen) {
|
||||||
|
dst[len++] = (c << 6) | d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
src += 4;
|
||||||
|
}
|
||||||
|
if (len < dlen) dst[len] = '\0';
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_base64(const char *s, int len, const char *path, char *to,
|
||||||
|
int n) {
|
||||||
|
const char *p;
|
||||||
|
int sz;
|
||||||
|
if (mjson_find(s, len, path, &p, &sz) != MJSON_TOK_STRING) return 0;
|
||||||
|
return mjson_base64_dec(p + 1, sz - 2, to, n);
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_BASE64
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_NEXT
|
||||||
|
struct nextdata {
|
||||||
|
int off, len, depth, t, vo, arrayindex;
|
||||||
|
int *koff, *klen, *voff, *vlen, *vtype;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int next_cb(int tok, const char *s, int off, int len, void *ud) {
|
||||||
|
struct nextdata *d = (struct nextdata *) ud;
|
||||||
|
// int i;
|
||||||
|
switch (tok) {
|
||||||
|
case '{':
|
||||||
|
case '[':
|
||||||
|
if (d->depth == 0 && tok == '[') d->arrayindex = 0;
|
||||||
|
if (d->depth == 1 && off > d->off) {
|
||||||
|
d->vo = off;
|
||||||
|
d->t = tok == '{' ? MJSON_TOK_OBJECT : MJSON_TOK_ARRAY;
|
||||||
|
if (d->voff) *d->voff = off;
|
||||||
|
if (d->vtype) *d->vtype = d->t;
|
||||||
|
}
|
||||||
|
d->depth++;
|
||||||
|
break;
|
||||||
|
case '}':
|
||||||
|
case ']':
|
||||||
|
d->depth--;
|
||||||
|
if (d->depth == 1 && d->vo) {
|
||||||
|
d->len = off + len;
|
||||||
|
if (d->vlen) *d->vlen = d->len - d->vo;
|
||||||
|
if (d->arrayindex >= 0) {
|
||||||
|
if (d->koff) *d->koff = d->arrayindex; // koff holds array index
|
||||||
|
if (d->klen) *d->klen = 0; // klen holds 0
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (d->depth == 1 && d->arrayindex >= 0) d->arrayindex++;
|
||||||
|
break;
|
||||||
|
case ',':
|
||||||
|
case ':':
|
||||||
|
break;
|
||||||
|
case MJSON_TOK_KEY:
|
||||||
|
if (d->depth == 1 && d->off < off) {
|
||||||
|
if (d->koff) *d->koff = off; // And report back to the user
|
||||||
|
if (d->klen) *d->klen = len; // If we have to
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (d->depth != 1) break;
|
||||||
|
// If we're iterating over the array
|
||||||
|
if (off > d->off) {
|
||||||
|
d->len = off + len;
|
||||||
|
if (d->vlen) *d->vlen = len; // value length
|
||||||
|
if (d->voff) *d->voff = off; // value offset
|
||||||
|
if (d->vtype) *d->vtype = tok; // value type
|
||||||
|
if (d->arrayindex >= 0) {
|
||||||
|
if (d->koff) *d->koff = d->arrayindex; // koff holds array index
|
||||||
|
if (d->klen) *d->klen = 0; // klen holds 0
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (d->arrayindex >= 0) d->arrayindex++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
(void) s;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_next(const char *s, int n, int off, int *koff, int *klen, int *voff,
|
||||||
|
int *vlen, int *vtype) {
|
||||||
|
struct nextdata d = {off, 0, 0, 0, 0, -1, koff, klen, voff, vlen, vtype};
|
||||||
|
mjson(s, n, next_cb, &d);
|
||||||
|
return d.len;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRINT
|
||||||
|
int mjson_print_fixed_buf(const char *ptr, int len, void *fndata) {
|
||||||
|
struct mjson_fixedbuf *fb = (struct mjson_fixedbuf *) fndata;
|
||||||
|
int i, left = fb->size - 1 - fb->len;
|
||||||
|
if (left < len) len = left;
|
||||||
|
for (i = 0; i < len; i++) fb->ptr[fb->len + i] = ptr[i];
|
||||||
|
fb->len += len;
|
||||||
|
fb->ptr[fb->len] = '\0';
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_dynamic_buf(const char *ptr, int len, void *fndata) {
|
||||||
|
char *s, *buf = *(char **) fndata;
|
||||||
|
int curlen = buf == NULL ? 0 : strlen(buf);
|
||||||
|
if ((s = (char *) realloc(buf, curlen + len + 1)) == NULL) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
memcpy(s + curlen, ptr, len);
|
||||||
|
s[curlen + len] = '\0';
|
||||||
|
*(char **) fndata = s;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_null(const char *ptr, int len, void *userdata) {
|
||||||
|
(void) ptr;
|
||||||
|
(void) userdata;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_file(const char *ptr, int len, void *userdata) {
|
||||||
|
return fwrite(ptr, 1, len, (FILE *) userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_buf(mjson_print_fn_t fn, void *fndata, const char *buf,
|
||||||
|
int len) {
|
||||||
|
return fn(buf, len, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_int(mjson_print_fn_t fn, void *fndata, int value,
|
||||||
|
int is_signed) {
|
||||||
|
char buf[20];
|
||||||
|
int len = snprintf(buf, sizeof(buf), is_signed ? "%d" : "%u", value);
|
||||||
|
return fn(buf, len, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_long(mjson_print_fn_t fn, void *fndata, long value,
|
||||||
|
int is_signed) {
|
||||||
|
char buf[20];
|
||||||
|
const char *fmt = (is_signed ? "%ld" : "%lu");
|
||||||
|
int len = snprintf(buf, sizeof(buf), fmt, value);
|
||||||
|
return fn(buf, len, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_dbl(mjson_print_fn_t fn, void *fndata, double d,
|
||||||
|
const char *fmt) {
|
||||||
|
char buf[40];
|
||||||
|
int n = snprintf(buf, sizeof(buf), fmt, d);
|
||||||
|
return fn(buf, n, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_str(mjson_print_fn_t fn, void *fndata, const char *s, int len) {
|
||||||
|
int i, n = fn("\"", 1, fndata);
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
char c = mjson_esc(s[i], 1);
|
||||||
|
if (c) {
|
||||||
|
n += fn("\\", 1, fndata);
|
||||||
|
n += fn(&c, 1, fndata);
|
||||||
|
} else {
|
||||||
|
n += fn(&s[i], 1, fndata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n + fn("\"", 1, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
int mjson_print_b64(mjson_print_fn_t fn, void *fndata, const unsigned char *s,
|
||||||
|
int n) {
|
||||||
|
const char *t =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
int i, len = fn("\"", 1, fndata);
|
||||||
|
for (i = 0; i < n; i += 3) {
|
||||||
|
int a = s[i], b = i + 1 < n ? s[i + 1] : 0, c = i + 2 < n ? s[i + 2] : 0;
|
||||||
|
char buf[4] = {t[a >> 2], t[(a & 3) << 4 | (b >> 4)], '=', '='};
|
||||||
|
if (i + 1 < n) buf[2] = t[(b & 15) << 2 | (c >> 6)];
|
||||||
|
if (i + 2 < n) buf[3] = t[c & 63];
|
||||||
|
len += fn(buf, sizeof(buf), fndata);
|
||||||
|
}
|
||||||
|
return len + fn("\"", 1, fndata);
|
||||||
|
}
|
||||||
|
#endif /* MJSON_ENABLE_BASE64 */
|
||||||
|
|
||||||
|
int mjson_vprintf(mjson_print_fn_t fn, void *fndata, const char *fmt,
|
||||||
|
va_list xap) {
|
||||||
|
int i = 0, n = 0;
|
||||||
|
va_list ap;
|
||||||
|
va_copy(ap, xap);
|
||||||
|
while (fmt[i] != '\0') {
|
||||||
|
if (fmt[i] == '%') {
|
||||||
|
char fc = fmt[++i];
|
||||||
|
int is_long = 0;
|
||||||
|
if (fc == 'l') {
|
||||||
|
is_long = 1;
|
||||||
|
fc = fmt[i + 1];
|
||||||
|
}
|
||||||
|
if (fc == 'Q') {
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_str(fn, fndata, buf ? buf : "", buf ? strlen(buf) : 0);
|
||||||
|
} else if (strncmp(&fmt[i], ".*Q", 3) == 0) {
|
||||||
|
int len = va_arg(ap, int);
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_str(fn, fndata, buf, len);
|
||||||
|
i += 2;
|
||||||
|
} else if (fc == 'd' || fc == 'u') {
|
||||||
|
int is_signed = (fc == 'd');
|
||||||
|
if (is_long) {
|
||||||
|
long val = va_arg(ap, long);
|
||||||
|
n += mjson_print_long(fn, fndata, val, is_signed);
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
int val = va_arg(ap, int);
|
||||||
|
n += mjson_print_int(fn, fndata, val, is_signed);
|
||||||
|
}
|
||||||
|
} else if (fc == 'B') {
|
||||||
|
const char *s = va_arg(ap, int) ? "true" : "false";
|
||||||
|
n += mjson_print_buf(fn, fndata, s, strlen(s));
|
||||||
|
} else if (fc == 's') {
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_buf(fn, fndata, buf, strlen(buf));
|
||||||
|
} else if (strncmp(&fmt[i], ".*s", 3) == 0) {
|
||||||
|
int len = va_arg(ap, int);
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_buf(fn, fndata, buf, len);
|
||||||
|
i += 2;
|
||||||
|
} else if (fc == 'g') {
|
||||||
|
n += mjson_print_dbl(fn, fndata, va_arg(ap, double), "%g");
|
||||||
|
} else if (fc == 'f') {
|
||||||
|
n += mjson_print_dbl(fn, fndata, va_arg(ap, double), "%f");
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
} else if (fc == 'V') {
|
||||||
|
int len = va_arg(ap, int);
|
||||||
|
const char *buf = va_arg(ap, const char *);
|
||||||
|
n += mjson_print_b64(fn, fndata, (unsigned char *) buf, len);
|
||||||
|
#endif
|
||||||
|
} else if (fc == 'H') {
|
||||||
|
const char *hex = "0123456789abcdef";
|
||||||
|
int i, len = va_arg(ap, int);
|
||||||
|
const unsigned char *p = va_arg(ap, const unsigned char *);
|
||||||
|
n += fn("\"", 1, fndata);
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
n += fn(&hex[(p[i] >> 4) & 15], 1, fndata);
|
||||||
|
n += fn(&hex[p[i] & 15], 1, fndata);
|
||||||
|
}
|
||||||
|
n += fn("\"", 1, fndata);
|
||||||
|
} else if (fc == 'M') {
|
||||||
|
mjson_vprint_fn_t vfn = va_arg(ap, mjson_vprint_fn_t);
|
||||||
|
n += vfn(fn, fndata, &ap);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
n += mjson_print_buf(fn, fndata, &fmt[i++], 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
va_end(xap);
|
||||||
|
va_end(ap);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_printf(mjson_print_fn_t fn, void *fndata, const char *fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
int len;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = mjson_vprintf(fn, fndata, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
#endif /* MJSON_ENABLE_PRINT */
|
||||||
|
|
||||||
|
#if MJSON_IMPLEMENT_STRTOD
|
||||||
|
static inline int is_digit(int c) {
|
||||||
|
return c >= '0' && c <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
/* NOTE: strtod() implementation by Yasuhiro Matsumoto. */
|
||||||
|
double strtod(const char *str, char **end) {
|
||||||
|
double d = 0.0;
|
||||||
|
int sign = 1, n = 0;
|
||||||
|
const char *p = str, *a = str;
|
||||||
|
|
||||||
|
/* decimal part */
|
||||||
|
if (*p == '-') {
|
||||||
|
sign = -1;
|
||||||
|
++p;
|
||||||
|
} else if (*p == '+')
|
||||||
|
++p;
|
||||||
|
if (is_digit(*p)) {
|
||||||
|
d = (double) (*p++ - '0');
|
||||||
|
while (*p && is_digit(*p)) {
|
||||||
|
d = d * 10.0 + (double) (*p - '0');
|
||||||
|
++p;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
a = p;
|
||||||
|
} else if (*p != '.')
|
||||||
|
goto done;
|
||||||
|
d *= sign;
|
||||||
|
|
||||||
|
/* fraction part */
|
||||||
|
if (*p == '.') {
|
||||||
|
double f = 0.0;
|
||||||
|
double base = 0.1;
|
||||||
|
++p;
|
||||||
|
|
||||||
|
if (is_digit(*p)) {
|
||||||
|
while (*p && is_digit(*p)) {
|
||||||
|
f += base * (*p - '0');
|
||||||
|
base /= 10.0;
|
||||||
|
++p;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d += f * sign;
|
||||||
|
a = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* exponential part */
|
||||||
|
if ((*p == 'E') || (*p == 'e')) {
|
||||||
|
int e = 0;
|
||||||
|
++p;
|
||||||
|
|
||||||
|
sign = 1;
|
||||||
|
if (*p == '-') {
|
||||||
|
sign = -1;
|
||||||
|
++p;
|
||||||
|
} else if (*p == '+')
|
||||||
|
++p;
|
||||||
|
|
||||||
|
if (is_digit(*p)) {
|
||||||
|
while (*p == '0') ++p;
|
||||||
|
e = (int) (*p++ - '0');
|
||||||
|
while (*p && is_digit(*p)) {
|
||||||
|
e = e * 10 + (int) (*p - '0');
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
e *= sign;
|
||||||
|
} else if (!is_digit(*(a - 1))) {
|
||||||
|
a = str;
|
||||||
|
goto done;
|
||||||
|
} else if (*p == 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
if (d == 2.2250738585072011 && e == -308) {
|
||||||
|
d = 0.0;
|
||||||
|
a = p;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
if (d == 2.2250738585072012 && e <= -308) {
|
||||||
|
d *= 1.0e-308;
|
||||||
|
a = p;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 10; i++) d *= 10;
|
||||||
|
}
|
||||||
|
a = p;
|
||||||
|
} else if (p > str && !is_digit(*(p - 1))) {
|
||||||
|
a = str;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
if (end) *end = (char *) a;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_MERGE
|
||||||
|
int mjson_merge(const char *s, int n, const char *s2, int n2,
|
||||||
|
mjson_print_fn_t fn, void *userdata) {
|
||||||
|
int koff, klen, voff, vlen, t, t2, k, off = 0, len = 0, comma = 0;
|
||||||
|
if (n < 2) return len;
|
||||||
|
len += fn("{", 1, userdata);
|
||||||
|
while ((off = mjson_next(s, n, off, &koff, &klen, &voff, &vlen, &t)) != 0) {
|
||||||
|
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||||
|
char path[klen + 1];
|
||||||
|
#else
|
||||||
|
char *path = (char *) alloca(klen + 1);
|
||||||
|
#endif
|
||||||
|
const char *val;
|
||||||
|
memcpy(path, "$.", 2);
|
||||||
|
memcpy(path + 2, s + koff + 1, klen - 2);
|
||||||
|
path[klen] = '\0';
|
||||||
|
if ((t2 = mjson_find(s2, n2, path, &val, &k)) != MJSON_TOK_INVALID) {
|
||||||
|
if (t2 == MJSON_TOK_NULL) continue; // null deletes the key
|
||||||
|
} else {
|
||||||
|
val = s + voff; // Key is not found in the update. Copy the old value.
|
||||||
|
}
|
||||||
|
if (comma) len += fn(",", 1, userdata);
|
||||||
|
len += fn(s + koff, klen, userdata);
|
||||||
|
len += fn(":", 1, userdata);
|
||||||
|
if (t == MJSON_TOK_OBJECT && t2 == MJSON_TOK_OBJECT) {
|
||||||
|
len += mjson_merge(s + voff, vlen, val, k, fn, userdata);
|
||||||
|
} else {
|
||||||
|
if (t2 != MJSON_TOK_INVALID) vlen = k;
|
||||||
|
len += fn(val, vlen, userdata);
|
||||||
|
}
|
||||||
|
comma = 1;
|
||||||
|
}
|
||||||
|
// Add missing keys
|
||||||
|
off = 0;
|
||||||
|
while ((off = mjson_next(s2, n2, off, &koff, &klen, &voff, &vlen, &t)) != 0) {
|
||||||
|
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||||
|
char path[klen + 1];
|
||||||
|
#else
|
||||||
|
char *path = (char *) alloca(klen + 1);
|
||||||
|
#endif
|
||||||
|
const char *val;
|
||||||
|
if (t == MJSON_TOK_NULL) continue;
|
||||||
|
memcpy(path, "$.", 2);
|
||||||
|
memcpy(path + 2, s2 + koff + 1, klen - 2);
|
||||||
|
path[klen] = '\0';
|
||||||
|
if (mjson_find(s, n, path, &val, &vlen) != MJSON_TOK_INVALID) continue;
|
||||||
|
if (comma) len += fn(",", 1, userdata);
|
||||||
|
len += fn(s2 + koff, klen, userdata);
|
||||||
|
len += fn(":", 1, userdata);
|
||||||
|
len += fn(s2 + voff, vlen, userdata);
|
||||||
|
comma = 1;
|
||||||
|
}
|
||||||
|
len += fn("}", 1, userdata);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_MERGE
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRETTY
|
||||||
|
struct prettydata {
|
||||||
|
int level;
|
||||||
|
int len;
|
||||||
|
int prev;
|
||||||
|
const char *pad;
|
||||||
|
int padlen;
|
||||||
|
mjson_print_fn_t fn;
|
||||||
|
void *userdata;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int pretty_cb(int ev, const char *s, int off, int len, void *ud) {
|
||||||
|
struct prettydata *d = (struct prettydata *) ud;
|
||||||
|
int i;
|
||||||
|
switch (ev) {
|
||||||
|
case '{':
|
||||||
|
case '[':
|
||||||
|
d->level++;
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
case '}':
|
||||||
|
case ']':
|
||||||
|
d->level--;
|
||||||
|
if (d->prev != '[' && d->prev != '{' && d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
case ',':
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
if (d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ':':
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
if (d->padlen > 0) d->len += d->fn(" ", 1, d->userdata);
|
||||||
|
break;
|
||||||
|
case MJSON_TOK_KEY:
|
||||||
|
if (d->prev == '{' && d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (d->prev == '[' && d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
d->prev = ev;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_pretty(const char *s, int n, const char *pad, mjson_print_fn_t fn,
|
||||||
|
void *userdata) {
|
||||||
|
struct prettydata d = {0, 0, 0, pad, strlen(pad), fn, userdata};
|
||||||
|
if (mjson(s, n, pretty_cb, &d) < 0) return -1;
|
||||||
|
return d.len;
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_PRETTY
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_RPC
|
||||||
|
struct jsonrpc_ctx jsonrpc_default_context;
|
||||||
|
struct jsonrpc_userdata {
|
||||||
|
mjson_print_fn_t fn;
|
||||||
|
void *fndata;
|
||||||
|
};
|
||||||
|
|
||||||
|
int mjson_globmatch(const char *s1, int n1, const char *s2, int n2) {
|
||||||
|
int i = 0, j = 0, ni = 0, nj = 0;
|
||||||
|
while (i < n1 || j < n2) {
|
||||||
|
if (i < n1 && j < n2 && (s1[i] == '?' || s2[j] == s1[i])) {
|
||||||
|
i++, j++;
|
||||||
|
} else if (i < n1 && (s1[i] == '*' || s1[i] == '#')) {
|
||||||
|
ni = i, nj = j + 1, i++;
|
||||||
|
} else if (nj > 0 && nj <= n2 && (s1[i - 1] == '#' || s2[j] != '/')) {
|
||||||
|
i = ni, j = nj;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int jsonrpc_printer(const char *buf, int len, void *userdata) {
|
||||||
|
struct jsonrpc_userdata *u = (struct jsonrpc_userdata *) userdata;
|
||||||
|
return u->fn(buf, len, u->fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_errorv(struct jsonrpc_request *r, int code,
|
||||||
|
const char *message, const char *data_fmt,
|
||||||
|
va_list ap) {
|
||||||
|
if (r->id_len == 0) return;
|
||||||
|
mjson_printf(r->fn, r->fndata,
|
||||||
|
"{\"id\":%.*s,\"error\":{\"code\":%d,\"message\":%Q", r->id_len,
|
||||||
|
r->id, code, message == NULL ? "" : message);
|
||||||
|
if (data_fmt != NULL) {
|
||||||
|
mjson_printf(r->fn, r->fndata, ",\"data\":");
|
||||||
|
mjson_vprintf(r->fn, r->fndata, data_fmt, ap);
|
||||||
|
}
|
||||||
|
mjson_printf(r->fn, r->fndata, "}}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_error(struct jsonrpc_request *r, int code,
|
||||||
|
const char *message, const char *data_fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, data_fmt);
|
||||||
|
jsonrpc_return_errorv(r, code, message, data_fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_successv(struct jsonrpc_request *r, const char *result_fmt,
|
||||||
|
va_list ap) {
|
||||||
|
if (r->id_len == 0) return;
|
||||||
|
mjson_printf(r->fn, r->fndata, "{\"id\":%.*s,\"result\":", r->id_len, r->id);
|
||||||
|
if (result_fmt != NULL) {
|
||||||
|
mjson_vprintf(r->fn, r->fndata, result_fmt, ap);
|
||||||
|
} else {
|
||||||
|
mjson_printf(r->fn, r->fndata, "%s", "null");
|
||||||
|
}
|
||||||
|
mjson_printf(r->fn, r->fndata, "}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
|
||||||
|
...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, result_fmt);
|
||||||
|
jsonrpc_return_successv(r, result_fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *req, int req_sz,
|
||||||
|
mjson_print_fn_t fn, void *fndata) {
|
||||||
|
const char *result = NULL, *error = NULL;
|
||||||
|
int result_sz = 0, error_sz = 0;
|
||||||
|
struct jsonrpc_method *m = NULL;
|
||||||
|
struct jsonrpc_userdata d;
|
||||||
|
struct jsonrpc_request r = {req, req_sz, 0, 0, 0, 0, 0,
|
||||||
|
0, &jsonrpc_printer, NULL, NULL};
|
||||||
|
|
||||||
|
d.fn = fn;
|
||||||
|
d.fndata = fndata;
|
||||||
|
r.fndata = &d;
|
||||||
|
|
||||||
|
// Is is a response frame?
|
||||||
|
mjson_find(req, req_sz, "$.result", &result, &result_sz);
|
||||||
|
if (result == NULL) mjson_find(req, req_sz, "$.error", &error, &error_sz);
|
||||||
|
if (result_sz > 0 || error_sz > 0) {
|
||||||
|
if (ctx->response_cb != NULL) ctx->response_cb(req, req_sz, ctx->userdata);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method must exist and must be a string
|
||||||
|
if (mjson_find(req, req_sz, "$.method", &r.method, &r.method_len) !=
|
||||||
|
MJSON_TOK_STRING) {
|
||||||
|
mjson_printf(fn, fndata, "{\"error\":{\"code\":-32700,\"message\":%.*Q}}\n",
|
||||||
|
req_sz, req);
|
||||||
|
ctx->in_len = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id and params are optional
|
||||||
|
mjson_find(req, req_sz, "$.id", &r.id, &r.id_len);
|
||||||
|
mjson_find(req, req_sz, "$.params", &r.params, &r.params_len);
|
||||||
|
|
||||||
|
for (m = ctx->methods; m != NULL; m = m->next) {
|
||||||
|
if (mjson_globmatch(m->method, m->method_sz, r.method + 1,
|
||||||
|
r.method_len - 2) > 0) {
|
||||||
|
if (r.params == NULL) r.params = "";
|
||||||
|
r.userdata = m->cbdata;
|
||||||
|
m->cb(&r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m == NULL) {
|
||||||
|
jsonrpc_return_error(&r, JSONRPC_ERROR_NOT_FOUND, "method not found", NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int jsonrpc_print_methods(mjson_print_fn_t fn, void *fndata,
|
||||||
|
va_list *ap) {
|
||||||
|
struct jsonrpc_ctx *ctx = va_arg(*ap, struct jsonrpc_ctx *);
|
||||||
|
struct jsonrpc_method *m;
|
||||||
|
int len = 0;
|
||||||
|
for (m = ctx->methods; m != NULL; m = m->next) {
|
||||||
|
if (m != ctx->methods) len += mjson_print_buf(fn, fndata, ",", 1);
|
||||||
|
len += mjson_print_str(fn, fndata, m->method, strlen(m->method));
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rpclist(struct jsonrpc_request *r) {
|
||||||
|
jsonrpc_return_success(r, "[%M]", jsonrpc_print_methods, r->userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t response_cb,
|
||||||
|
void *userdata) {
|
||||||
|
ctx->response_cb = response_cb;
|
||||||
|
ctx->userdata = userdata;
|
||||||
|
jsonrpc_ctx_export(ctx, MJSON_RPC_LIST_NAME, rpclist, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_ctx_process_byte(struct jsonrpc_ctx *ctx, unsigned char ch,
|
||||||
|
mjson_print_fn_t fn, void *p) {
|
||||||
|
if (ctx->in_len >= (int) sizeof(ctx->in)) ctx->in_len = 0; // Overflow
|
||||||
|
if (ch == '\n') { // If new line, parse frame
|
||||||
|
if (ctx->in_len > 1) jsonrpc_ctx_process(ctx, ctx->in, ctx->in_len, fn, p);
|
||||||
|
ctx->in_len = 0;
|
||||||
|
} else {
|
||||||
|
ctx->in[ctx->in_len] = ch; // Append to the buffer
|
||||||
|
ctx->in_len++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_init(mjson_print_fn_t response_cb, void *userdata) {
|
||||||
|
jsonrpc_ctx_init(&jsonrpc_default_context, response_cb, userdata);
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_RPC
|
||||||
@@ -0,0 +1,220 @@
|
|||||||
|
// Copyright (c) 2018-2020 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
#ifndef MJSON_H
|
||||||
|
#define MJSON_H
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_PRINT
|
||||||
|
#define MJSON_ENABLE_PRINT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_RPC
|
||||||
|
#define MJSON_ENABLE_RPC 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_BASE64
|
||||||
|
#define MJSON_ENABLE_BASE64 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_MERGE
|
||||||
|
#define MJSON_ENABLE_MERGE 0
|
||||||
|
#elif MJSON_ENABLE_MERGE
|
||||||
|
#define MJSON_ENABLE_NEXT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_PRETTY
|
||||||
|
#define MJSON_ENABLE_PRETTY 0
|
||||||
|
#elif MJSON_ENABLE_PRETTY
|
||||||
|
#define MJSON_ENABLE_NEXT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_NEXT
|
||||||
|
#define MJSON_ENABLE_NEXT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef MJSON_RPC_IN_BUF_SIZE
|
||||||
|
#define MJSON_RPC_IN_BUF_SIZE 256
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_RPC_LIST_NAME
|
||||||
|
#define MJSON_RPC_LIST_NAME "rpc.list"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum {
|
||||||
|
MJSON_ERROR_INVALID_INPUT = -1,
|
||||||
|
MJSON_ERROR_TOO_DEEP = -2,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum mjson_tok {
|
||||||
|
MJSON_TOK_INVALID = 0,
|
||||||
|
MJSON_TOK_KEY = 1,
|
||||||
|
MJSON_TOK_STRING = 11,
|
||||||
|
MJSON_TOK_NUMBER = 12,
|
||||||
|
MJSON_TOK_TRUE = 13,
|
||||||
|
MJSON_TOK_FALSE = 14,
|
||||||
|
MJSON_TOK_NULL = 15,
|
||||||
|
MJSON_TOK_ARRAY = 91,
|
||||||
|
MJSON_TOK_OBJECT = 123,
|
||||||
|
};
|
||||||
|
#define MJSON_TOK_IS_VALUE(t) ((t) > 10 && (t) < 20)
|
||||||
|
|
||||||
|
typedef int (*mjson_cb_t)(int ev, const char *s, int off, int len, void *ud);
|
||||||
|
|
||||||
|
#ifndef MJSON_MAX_DEPTH
|
||||||
|
#define MJSON_MAX_DEPTH 20
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int mjson(const char *s, int len, mjson_cb_t cb, void *ud);
|
||||||
|
enum mjson_tok mjson_find(const char *s, int len, const char *jp,
|
||||||
|
const char **tokptr, int *toklen);
|
||||||
|
int mjson_get_number(const char *s, int len, const char *path, double *v);
|
||||||
|
int mjson_get_bool(const char *s, int len, const char *path, int *v);
|
||||||
|
int mjson_get_string(const char *s, int len, const char *path, char *to, int n);
|
||||||
|
int mjson_get_hex(const char *s, int len, const char *path, char *to, int n);
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_NEXT
|
||||||
|
int mjson_next(const char *s, int n, int off, int *koff, int *klen, int *voff,
|
||||||
|
int *vlen, int *vtype);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
int mjson_get_base64(const char *s, int len, const char *path, char *to, int n);
|
||||||
|
int mjson_base64_dec(const char *src, int n, char *dst, int dlen);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRINT
|
||||||
|
typedef int (*mjson_print_fn_t)(const char *buf, int len, void *userdata);
|
||||||
|
typedef int (*mjson_vprint_fn_t)(mjson_print_fn_t, void *, va_list *);
|
||||||
|
|
||||||
|
struct mjson_fixedbuf {
|
||||||
|
char *ptr;
|
||||||
|
int size, len;
|
||||||
|
};
|
||||||
|
|
||||||
|
int mjson_printf(mjson_print_fn_t, void *, const char *fmt, ...);
|
||||||
|
int mjson_vprintf(mjson_print_fn_t, void *, const char *fmt, va_list ap);
|
||||||
|
int mjson_print_str(mjson_print_fn_t, void *, const char *s, int len);
|
||||||
|
int mjson_print_int(mjson_print_fn_t, void *, int value, int is_signed);
|
||||||
|
int mjson_print_long(mjson_print_fn_t, void *, long value, int is_signed);
|
||||||
|
int mjson_print_buf(mjson_print_fn_t fn, void *, const char *buf, int len);
|
||||||
|
|
||||||
|
int mjson_print_null(const char *ptr, int len, void *userdata);
|
||||||
|
int mjson_print_file(const char *ptr, int len, void *userdata);
|
||||||
|
int mjson_print_fixed_buf(const char *ptr, int len, void *userdata);
|
||||||
|
int mjson_print_dynamic_buf(const char *ptr, int len, void *userdata);
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRETTY
|
||||||
|
int mjson_pretty(const char *, int, const char *, mjson_print_fn_t, void *);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_MERGE
|
||||||
|
int mjson_merge(const char *, int, const char *, int, mjson_print_fn_t, void *);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MJSON_ENABLE_PRINT
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_RPC
|
||||||
|
|
||||||
|
void jsonrpc_init(mjson_print_fn_t, void *userdata);
|
||||||
|
int mjson_globmatch(const char *s1, int n1, const char *s2, int n2);
|
||||||
|
|
||||||
|
struct jsonrpc_request {
|
||||||
|
const char *frame; // Points to the whole frame
|
||||||
|
int frame_len; // Frame length
|
||||||
|
const char *params; // Points to the "params" in the request frame
|
||||||
|
int params_len; // Length of the "params"
|
||||||
|
const char *id; // Points to the "id" in the request frame
|
||||||
|
int id_len; // Length of the "id"
|
||||||
|
const char *method; // Points to the "method" in the request frame
|
||||||
|
int method_len; // Length of the "method"
|
||||||
|
mjson_print_fn_t fn; // Printer function
|
||||||
|
void *fndata; // Printer function data
|
||||||
|
void *userdata; // Callback's user data as specified at export time
|
||||||
|
};
|
||||||
|
|
||||||
|
struct jsonrpc_method {
|
||||||
|
const char *method;
|
||||||
|
int method_sz;
|
||||||
|
void (*cb)(struct jsonrpc_request *);
|
||||||
|
void *cbdata;
|
||||||
|
struct jsonrpc_method *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Main RPC context, stores current request information and a list of
|
||||||
|
// exported RPC methods.
|
||||||
|
struct jsonrpc_ctx {
|
||||||
|
struct jsonrpc_method *methods;
|
||||||
|
void *userdata;
|
||||||
|
mjson_print_fn_t response_cb;
|
||||||
|
int in_len;
|
||||||
|
char in[MJSON_RPC_IN_BUF_SIZE];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Registers function fn under the given name within the given RPC context
|
||||||
|
#define jsonrpc_ctx_export(ctx, name, fn, ud) \
|
||||||
|
do { \
|
||||||
|
static struct jsonrpc_method m = {(name), sizeof(name) - 1, (fn), 0, 0}; \
|
||||||
|
m.cbdata = (ud); \
|
||||||
|
m.next = (ctx)->methods; \
|
||||||
|
(ctx)->methods = &m; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t, void *);
|
||||||
|
void jsonrpc_return_error(struct jsonrpc_request *r, int code,
|
||||||
|
const char *message, const char *data_fmt, ...);
|
||||||
|
void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
|
||||||
|
...);
|
||||||
|
void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *req, int req_sz,
|
||||||
|
mjson_print_fn_t fn, void *fndata);
|
||||||
|
void jsonrpc_ctx_process_byte(struct jsonrpc_ctx *ctx, unsigned char ch,
|
||||||
|
mjson_print_fn_t fn, void *fndata);
|
||||||
|
|
||||||
|
extern struct jsonrpc_ctx jsonrpc_default_context;
|
||||||
|
|
||||||
|
#define jsonrpc_export(name, fn, ud) \
|
||||||
|
jsonrpc_ctx_export(&jsonrpc_default_context, (name), (fn), (ud))
|
||||||
|
|
||||||
|
#define jsonrpc_process(buf, len, fn, data) \
|
||||||
|
jsonrpc_ctx_process(&jsonrpc_default_context, (buf), (len), (fn), (data))
|
||||||
|
|
||||||
|
#define jsonrpc_process_byte(x, fn, data) \
|
||||||
|
jsonrpc_ctx_process_byte(&jsonrpc_default_context, (x), (fn), (data))
|
||||||
|
|
||||||
|
#define JSONRPC_ERROR_INVALID -32700 /* Invalid JSON was received */
|
||||||
|
#define JSONRPC_ERROR_NOT_FOUND -32601 /* The method does not exist */
|
||||||
|
#define JSONRPC_ERROR_BAD_PARAMS -32602 /* Invalid params passed */
|
||||||
|
#define JSONRPC_ERROR_INTERNAL -32603 /* Internal JSON-RPC error */
|
||||||
|
|
||||||
|
#endif // MJSON_ENABLE_RPC
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // MJSON_H
|
||||||
|
After Width: | Height: | Size: 134 KiB |
|
After Width: | Height: | Size: 52 KiB |
|
After Width: | Height: | Size: 122 KiB |
@@ -0,0 +1,312 @@
|
|||||||
|
const {h, render, Component} = preact;
|
||||||
|
const html = htm.bind(h);
|
||||||
|
|
||||||
|
class Login extends Component {
|
||||||
|
state = {user: '', pass: ''};
|
||||||
|
onclick = (app) => {
|
||||||
|
const authhdr = 'Basic ' + btoa(this.state.user + ':' + this.state.pass);
|
||||||
|
const headers = {Authorization: authhdr};
|
||||||
|
return axios.get('/api/login', {headers})
|
||||||
|
.then(res => app.setUser(res.data.token))
|
||||||
|
.catch(err => alert('Login failed'));
|
||||||
|
};
|
||||||
|
onpassinput = (ev) => this.setState({pass: ev.target.value});
|
||||||
|
onuserinput = (ev) => this.setState({user: ev.target.value});
|
||||||
|
render({app}, {user, pass, signup}) {
|
||||||
|
return html`
|
||||||
|
<div class='mx-auto bg-light rounded border my-5' style='max-width: 480px;'>
|
||||||
|
<div class='form p-5 rounded form-sm'>
|
||||||
|
<h4 class="text-muted mb-4">Login </h4>
|
||||||
|
<input type='email' placeholder='Email' class='my-2 form-control'
|
||||||
|
oninput=${this.onuserinput} value=${user} />
|
||||||
|
<input type="password" placeholder="Password" class="my-2 form-control"
|
||||||
|
oninput=${this.onpassinput} value=${pass}
|
||||||
|
onchange=${ev => this.onclick(app)} />
|
||||||
|
<div class="mb-4">
|
||||||
|
<button class="btn btn-info btn-block"
|
||||||
|
disabled=${!user || !pass} onclick="${ev => this.onclick(app)}"
|
||||||
|
> Login </button>
|
||||||
|
</div>
|
||||||
|
<div class="text-muted small">
|
||||||
|
Valid logins: admin:admin, user1:pass1, user2:pass2
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Dropdown extends Component {
|
||||||
|
state = {show: false};
|
||||||
|
render(props, state) {
|
||||||
|
const onclick = x => this.setState({show: x});
|
||||||
|
const show = state.show ? 'show' : '';
|
||||||
|
return html`
|
||||||
|
<div class="dropdown autoexpand ${props.cls}">
|
||||||
|
<div type="buttonx" onclick=${() => onclick(!state.show)}
|
||||||
|
class="dropdown-toggle my-0 ${props.bcls}"
|
||||||
|
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
|
||||||
|
${props.title}
|
||||||
|
</div>
|
||||||
|
<div onclick=${() => onclick(false)} style=${props.dstyle}
|
||||||
|
class="dropdown-menu ${props.dcls} ${show}">
|
||||||
|
${props.children}
|
||||||
|
</div>
|
||||||
|
</div>`;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const NavLink = ({href, title, url}) => html`<a class="nav-item nav-link
|
||||||
|
${url == href ? 'active' : ''}"
|
||||||
|
target=${href.match(/^http/) ? '_blank' : ''}
|
||||||
|
href="${href.match(/^http/) ? '' : '#'}${href}">${title}</a>`;
|
||||||
|
|
||||||
|
class Header extends Component {
|
||||||
|
state = {expanded: false};
|
||||||
|
ontoggle = () => this.setState({expanded: !this.state.expanded});
|
||||||
|
render(props, state) {
|
||||||
|
const u = props.app.state.user || {};
|
||||||
|
return html`
|
||||||
|
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
|
||||||
|
<a class="navbar-brand" href="#">
|
||||||
|
<img src="images/logo.png" width="26" height="26" alt="" class="mr-2" />
|
||||||
|
MyProduct
|
||||||
|
</a>
|
||||||
|
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"
|
||||||
|
onclick=${() => this.ontoggle()} >
|
||||||
|
<span class="navbar-toggler-icon"></span>
|
||||||
|
</button>
|
||||||
|
<div class="navbar-collapse ${state.expanded ? '' : 'collapse'}"
|
||||||
|
id="navbarNav">
|
||||||
|
<div class="nav navbar-nav mr-auto">
|
||||||
|
<${NavLink} href="/" title="Dashboard" url=${props.url} />
|
||||||
|
<${NavLink} href="/logs" title="Logs" url=${props.url} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<form class="form-inline">
|
||||||
|
<${Dropdown} title="${u.user}" cls="mr-2"
|
||||||
|
bcls="btn btn-sm btn-outline-light pointer" dcls="m-0 dropdown-menu-right">
|
||||||
|
<div onclick=${() => props.app.setUser('')}
|
||||||
|
class="dropdown-item small pointer text-center">logout</div>
|
||||||
|
</${Dropdown}>
|
||||||
|
<img src="images/user.png" class="rounded-circle nav-item mr-2" width="30" />
|
||||||
|
</form>
|
||||||
|
</nav>
|
||||||
|
`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const Info = () => html`<div class="alert alert-secondary my-3">
|
||||||
|
This dashboard shows values kept in server memory. Many clients
|
||||||
|
can open this page. As soon as any client changes any value,
|
||||||
|
all clients update automatically.
|
||||||
|
The JS code that watches state changes, reconnects on network failures.
|
||||||
|
That means if server restarts, dashboard on all connected clients
|
||||||
|
refresh automatically.
|
||||||
|
You can use <code>curl</code> command-line utility:
|
||||||
|
<br/><code>curl localhost:8000/api/config/get</code>
|
||||||
|
<br/><code>curl localhost:8000/api/config/watch</code>
|
||||||
|
<br/><code>curl localhost:8000/api/config/set -d '{"a":123}'</code>
|
||||||
|
<br/><span class="badge badge-danger">NOTE:</span> administrators
|
||||||
|
can change settings values, whilst users cannot.
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
class DashVideo extends Component {
|
||||||
|
render(props, state) {
|
||||||
|
const onclick = ev => axios.post(
|
||||||
|
'/api/config/set', {value1: +state.value1, value2: state.value2});
|
||||||
|
// alert(JSON.stringify(state));
|
||||||
|
return html`<div id="form">
|
||||||
|
<div><b>Change values</b></div>
|
||||||
|
<table>
|
||||||
|
<tr><td>Value 1 (number):</td><td><input value=${state.value1}
|
||||||
|
onInput=${linkState(this, 'value1')} /></td></tr>
|
||||||
|
<tr><td>Value 2 (string):</td><td><input value=${state.value2}
|
||||||
|
onInput=${linkState(this, 'value2')}/></td></tr>
|
||||||
|
<tr><td></td>
|
||||||
|
<td><button onClick=${onclick}>Save values</button></td></tr>
|
||||||
|
</table>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class DashForm extends Component {
|
||||||
|
render(props, state) {
|
||||||
|
const onclick = ev => axios.post(
|
||||||
|
'/api/config/set', {value1: +state.value1, value2: state.value2});
|
||||||
|
// alert(JSON.stringify(state));
|
||||||
|
return html`<div id="form">
|
||||||
|
<div><b>Change values</b></div>
|
||||||
|
<table>
|
||||||
|
<tr><td>Value 1 (number):</td><td><input class="form-control form-control-sm"
|
||||||
|
value=${state.value1}
|
||||||
|
onInput=${linkState(this, 'value1')} /></td></tr>
|
||||||
|
<tr><td>Value 2 (string):</td><td><input class="form-control form-control-sm"
|
||||||
|
value=${state.value2}
|
||||||
|
onInput=${linkState(this, 'value2')}/></td></tr>
|
||||||
|
<tr><td></td>
|
||||||
|
<td><button class="btn btn-primary btn-block"
|
||||||
|
onClick=${onclick}>Save values</button></td></tr>
|
||||||
|
</table>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class DashSettings extends Component {
|
||||||
|
state = {value1: null, value2: null};
|
||||||
|
componentDidMount() {
|
||||||
|
axios.get('/api/config/get')
|
||||||
|
.then(r => this.setState(r.data))
|
||||||
|
.catch(e => console.log(e));
|
||||||
|
var self = this;
|
||||||
|
var f = function(reader) {
|
||||||
|
return reader.read().then(function(result) {
|
||||||
|
var data = String.fromCharCode.apply(null, result.value);
|
||||||
|
self.setState(JSON.parse(data));
|
||||||
|
// console.log(JSON.parse(data));
|
||||||
|
if (!result.done) return f(reader);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
fetch('/api/config/watch')
|
||||||
|
.then(r => r.body.getReader())
|
||||||
|
.then(f)
|
||||||
|
.catch(e => setTimeout(x => self.componentDidMount(), 1000));
|
||||||
|
}
|
||||||
|
render(props, state) {
|
||||||
|
return html
|
||||||
|
`<div id="dashboard" style="display: flex; justify-content: flex-evenly;">
|
||||||
|
<div style="width:50%;text-align:center;">Value 1 (number):
|
||||||
|
<div style="font-size:140%;"><b>${state.value1}</b></div></div>
|
||||||
|
<div style="width:50%;text-align:center;">Value 2 (string):
|
||||||
|
<div style="font-size:140%;"><b>${state.value2}</b></div></div>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class AdminDashboard extends Component {
|
||||||
|
render(props, state) {
|
||||||
|
return html`<div class="container-fluid">
|
||||||
|
<${Info} />
|
||||||
|
<div class="card-deck">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Settings</div>
|
||||||
|
<div class="card-body"><${DashSettings} /></div>
|
||||||
|
</div>
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Video stream</div>
|
||||||
|
<div class="card-body"><img src="/api/video1" class="mw-100" /></div>
|
||||||
|
</div>
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Change settings</div>
|
||||||
|
<div class="card-body"><${DashForm} /></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class UserDashboard extends Component {
|
||||||
|
render(props, state) {
|
||||||
|
return html`<div class="container-fluid">
|
||||||
|
<${Info} />
|
||||||
|
<div class="card-deck">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Settings</div>
|
||||||
|
<div class="card-body"><${DashSettings} /></div>
|
||||||
|
</div>
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Video stream</div>
|
||||||
|
<div class="card-body"><img src="/api/video1" class="mw-100" /></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Logs extends Component {
|
||||||
|
state = {live: '', static: ''};
|
||||||
|
componentDidMount() {
|
||||||
|
var self = this;
|
||||||
|
var f = function(r) {
|
||||||
|
return r.read().then(function(result) {
|
||||||
|
var data = String.fromCharCode.apply(null, result.value);
|
||||||
|
var live = self.state.live + data;
|
||||||
|
self.setState({live}); // Append live log
|
||||||
|
if (!result.done) f(r); // Read next chunk
|
||||||
|
});
|
||||||
|
};
|
||||||
|
fetch('/api/log/static')
|
||||||
|
.then(r => r.text())
|
||||||
|
.then(r => {
|
||||||
|
self.setState({static: r});
|
||||||
|
})
|
||||||
|
.then(r => fetch('/api/log/live'))
|
||||||
|
.then(r => r.body.getReader())
|
||||||
|
.then(f);
|
||||||
|
}
|
||||||
|
render(props, state) {
|
||||||
|
return html`<div class="container-fluid">
|
||||||
|
<div class="alert alert-secondary my-3">
|
||||||
|
A div below shows file log.txt, which is produced by the server.
|
||||||
|
The server appends a new log message to that file every second,
|
||||||
|
and a div below also shows that, implementing a "live log" feature.
|
||||||
|
JS code on this page first fetches /api/log/static that returns
|
||||||
|
log.txt contents, then fetches /api/log/live that does not return,
|
||||||
|
but feeds chunks of data as they arrive (live log).
|
||||||
|
<br/><br/>
|
||||||
|
You can also use <code>curl</code> command-line utility to see
|
||||||
|
live logs:
|
||||||
|
<br/><code>curl localhost:8000/api/log/live</code>
|
||||||
|
</div>
|
||||||
|
<div class="card-deck">
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Static</div>
|
||||||
|
<div class="card-body"><pre class="log">${state.static}</pre></div>
|
||||||
|
</div>
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-header">Live</div>
|
||||||
|
<div class="card-body"><pre class="log">${state.live}</pre></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class App extends Component {
|
||||||
|
state = {user: null, url: '/'};
|
||||||
|
setUser(token) {
|
||||||
|
const maxAge = token ? 86400 : 0;
|
||||||
|
document.cookie = `access_token=${token};path=/;max-age=${maxAge}`;
|
||||||
|
// this.setState({token});
|
||||||
|
return axios.get('/api/login')
|
||||||
|
.then(res => this.setState({user: res.data}))
|
||||||
|
.catch(err => this.setState({user: {}}));
|
||||||
|
}
|
||||||
|
componentDidMount() {
|
||||||
|
const getCookie = name => {
|
||||||
|
var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
|
||||||
|
return v ? v[2] : '';
|
||||||
|
};
|
||||||
|
this.setUser(getCookie('access_token')); // Move from state1 to 2 or 3
|
||||||
|
}
|
||||||
|
render(props, state) {
|
||||||
|
// We have three states:
|
||||||
|
// 1. Beginning. We don't know if we have a valid auth token or not
|
||||||
|
// 2. We sent an /api/login request, and failed to login
|
||||||
|
// 3. We sent an /api/login request, and succeeded to login
|
||||||
|
|
||||||
|
if (!state.user) return ''; // State 1
|
||||||
|
if (!state.user.user) return h(Login, {app: this}); // State 2
|
||||||
|
return h( // State 3
|
||||||
|
'div', {}, h(Header, {url: state.url, app: this}),
|
||||||
|
h(preactRouter.Router, {
|
||||||
|
history: History.createHashHistory(),
|
||||||
|
onChange: ev => this.setState({url: ev.url}),
|
||||||
|
},
|
||||||
|
h(state.user.user == 'admin' ? AdminDashboard : UserDashboard,
|
||||||
|
{default: true, app: this}),
|
||||||
|
h(Logs, {path: 'logs', app: this})));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
window.onload = () => render(h(App), document.body);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/* axios v0.21.1 | (c) 2020 by Matt Zabriskie */
|
||||||
|
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.axios=t():e.axios=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){e.exports=n(1)},function(e,t,n){"use strict";function r(e){var t=new i(e),n=s(i.prototype.request,t);return o.extend(n,i.prototype,t),o.extend(n,t),n}var o=n(2),s=n(3),i=n(4),a=n(22),u=n(10),c=r(u);c.Axios=i,c.create=function(e){return r(a(c.defaults,e))},c.Cancel=n(23),c.CancelToken=n(24),c.isCancel=n(9),c.all=function(e){return Promise.all(e)},c.spread=n(25),c.isAxiosError=n(26),e.exports=c,e.exports.default=c},function(e,t,n){"use strict";function r(e){return"[object Array]"===R.call(e)}function o(e){return"undefined"==typeof e}function s(e){return null!==e&&!o(e)&&null!==e.constructor&&!o(e.constructor)&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}function i(e){return"[object ArrayBuffer]"===R.call(e)}function a(e){return"undefined"!=typeof FormData&&e instanceof FormData}function u(e){var t;return t="undefined"!=typeof ArrayBuffer&&ArrayBuffer.isView?ArrayBuffer.isView(e):e&&e.buffer&&e.buffer instanceof ArrayBuffer}function c(e){return"string"==typeof e}function f(e){return"number"==typeof e}function p(e){return null!==e&&"object"==typeof e}function d(e){if("[object Object]"!==R.call(e))return!1;var t=Object.getPrototypeOf(e);return null===t||t===Object.prototype}function l(e){return"[object Date]"===R.call(e)}function h(e){return"[object File]"===R.call(e)}function m(e){return"[object Blob]"===R.call(e)}function y(e){return"[object Function]"===R.call(e)}function g(e){return p(e)&&y(e.pipe)}function v(e){return"undefined"!=typeof URLSearchParams&&e instanceof URLSearchParams}function x(e){return e.replace(/^\s*/,"").replace(/\s*$/,"")}function w(){return("undefined"==typeof navigator||"ReactNative"!==navigator.product&&"NativeScript"!==navigator.product&&"NS"!==navigator.product)&&("undefined"!=typeof window&&"undefined"!=typeof document)}function b(e,t){if(null!==e&&"undefined"!=typeof e)if("object"!=typeof e&&(e=[e]),r(e))for(var n=0,o=e.length;n<o;n++)t.call(null,e[n],n,e);else for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.call(null,e[s],s,e)}function E(){function e(e,n){d(t[n])&&d(e)?t[n]=E(t[n],e):d(e)?t[n]=E({},e):r(e)?t[n]=e.slice():t[n]=e}for(var t={},n=0,o=arguments.length;n<o;n++)b(arguments[n],e);return t}function j(e,t,n){return b(t,function(t,r){n&&"function"==typeof t?e[r]=S(t,n):e[r]=t}),e}function C(e){return 65279===e.charCodeAt(0)&&(e=e.slice(1)),e}var S=n(3),R=Object.prototype.toString;e.exports={isArray:r,isArrayBuffer:i,isBuffer:s,isFormData:a,isArrayBufferView:u,isString:c,isNumber:f,isObject:p,isPlainObject:d,isUndefined:o,isDate:l,isFile:h,isBlob:m,isFunction:y,isStream:g,isURLSearchParams:v,isStandardBrowserEnv:w,forEach:b,merge:E,extend:j,trim:x,stripBOM:C}},function(e,t){"use strict";e.exports=function(e,t){return function(){for(var n=new Array(arguments.length),r=0;r<n.length;r++)n[r]=arguments[r];return e.apply(t,n)}}},function(e,t,n){"use strict";function r(e){this.defaults=e,this.interceptors={request:new i,response:new i}}var o=n(2),s=n(5),i=n(6),a=n(7),u=n(22);r.prototype.request=function(e){"string"==typeof e?(e=arguments[1]||{},e.url=arguments[0]):e=e||{},e=u(this.defaults,e),e.method?e.method=e.method.toLowerCase():this.defaults.method?e.method=this.defaults.method.toLowerCase():e.method="get";var t=[a,void 0],n=Promise.resolve(e);for(this.interceptors.request.forEach(function(e){t.unshift(e.fulfilled,e.rejected)}),this.interceptors.response.forEach(function(e){t.push(e.fulfilled,e.rejected)});t.length;)n=n.then(t.shift(),t.shift());return n},r.prototype.getUri=function(e){return e=u(this.defaults,e),s(e.url,e.params,e.paramsSerializer).replace(/^\?/,"")},o.forEach(["delete","get","head","options"],function(e){r.prototype[e]=function(t,n){return this.request(u(n||{},{method:e,url:t,data:(n||{}).data}))}}),o.forEach(["post","put","patch"],function(e){r.prototype[e]=function(t,n,r){return this.request(u(r||{},{method:e,url:t,data:n}))}}),e.exports=r},function(e,t,n){"use strict";function r(e){return encodeURIComponent(e).replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+").replace(/%5B/gi,"[").replace(/%5D/gi,"]")}var o=n(2);e.exports=function(e,t,n){if(!t)return e;var s;if(n)s=n(t);else if(o.isURLSearchParams(t))s=t.toString();else{var i=[];o.forEach(t,function(e,t){null!==e&&"undefined"!=typeof e&&(o.isArray(e)?t+="[]":e=[e],o.forEach(e,function(e){o.isDate(e)?e=e.toISOString():o.isObject(e)&&(e=JSON.stringify(e)),i.push(r(t)+"="+r(e))}))}),s=i.join("&")}if(s){var a=e.indexOf("#");a!==-1&&(e=e.slice(0,a)),e+=(e.indexOf("?")===-1?"?":"&")+s}return e}},function(e,t,n){"use stricLine truncated
|
||||||
|
//# sourceMappingURL=axios.min.map
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
/*!
|
||||||
|
* Bootstrap v4.3.1 (https://getbootstrap.com/)
|
||||||
|
* Copyright 2011-2019 The Bootstrap Authors
|
||||||
|
* Copyright 2011-2019 Twitter, Inc.
|
||||||
|
* Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
|
||||||
|
*/:root{--blue:#007bff;--indigo:#6610f2;--purple:#6f42c1;--pink:#e83e8c;--red:#dc3545;--orange:#fd7e14;--yellow:#ffc107;--green:#28a745;--teal:#20c997;--cyan:#17a2b8;--white:#fff;--gray:#6c757d;--gray-dark:#343a40;--primary:#007bff;--secondary:#6c757d;--success:#28a745;--info:#17a2b8;--warning:#ffc107;--danger:#dc3545;--light:#f8f9fa;--dark:#343a40;--breakpoint-xs:0;--breakpoint-sm:576px;--breakpoint-md:768px;--breakpoint-lg:992px;--breakpoint-xl:1200px;--font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";--font-family-monospace:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace}*,::after,::before{box-sizing:border-box}html{font-family:sans-serif;line-height:1.15;-webkit-text-size-adjust:100%;-webkit-tap-highlight-color:transparent}article,aside,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}body{margin:0;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,"Noto Sans",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";font-size:1rem;font-weight:400;line-height:1.5;color:#212529;text-align:left;background-color:#fff}[tabindex="-1"]:focus{outline:0!important}hr{box-sizing:content-box;height:0;overflow:visible}h1,h2,h3,h4,h5,h6{margin-top:0;margin-bottom:.5rem}p{margin-top:0;margin-bottom:1rem}abbr[data-original-title],abbr[title]{text-decoration:underline;-webkit-text-decoration:underline dotted;text-decoration:underline dotted;cursor:help;border-bottom:0;-webkit-text-decoration-skip-ink:none;text-decoration-skip-ink:none}address{margin-bottom:1rem;font-style:normal;line-height:inherit}dl,ol,ul{margin-top:0;margin-bottom:1rem}ol ol,ol ul,ul ol,ul ul{margin-bottom:0}dt{font-weight:700}dd{margin-bottom:.5rem;margin-left:0}blockquote{margin:0 0 1rem}b,strong{font-weight:bolder}small{font-size:80%}sub,sup{position:relative;font-size:75%;line-height:0;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}a{color:#007bff;text-decoration:none;background-color:transparent}a:hover{color:#0056b3;text-decoration:underline}a:not([href]):not([tabindex]){color:inherit;text-decoration:none}a:not([href]):not([tabindex]):focus,a:not([href]):not([tabindex]):hover{color:inherit;text-decoration:none}a:not([href]):not([tabindex]):focus{outline:0}code,kbd,pre,samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,"Liberation Mono","Courier New",monospace;font-size:1em}pre{margin-top:0;margin-bottom:1rem;overflow:auto}figure{margin:0 0 1rem}img{vertical-align:middle;border-style:none}svg{overflow:hidden;vertical-align:middle}table{border-collapse:collapse}caption{padding-top:.75rem;padding-bottom:.75rem;color:#6c757d;text-align:left;caption-side:bottom}th{text-align:inherit}label{display:inline-block;margin-bottom:.5rem}button{border-radius:0}button:focus{outline:1px dotted;outline:5px auto -webkit-focus-ring-color}button,input,optgroup,select,textarea{margin:0;font-family:inherit;font-size:inherit;line-height:inherit}button,input{overflow:visible}button,select{text-transform:none}select{word-wrap:normal}[type=button],[type=reset],[type=submit],button{-webkit-appearance:button}[type=button]:not(:disabled),[type=reset]:not(:disabled),[type=submit]:not(:disabled),button:not(:disabled){cursor:pointer}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{padding:0;border-style:none}input[type=checkbox],input[type=radio]{box-sizing:border-box;padding:0}input[type=date],input[type=datetime-local],input[type=month],input[type=time]{-webkit-appearance:listbox}textarea{overflow:auto;resize:vertical}fieldset{min-width:0;padding:0;margin:0;border:0}legend{display:block;width:100%;max-width:100%;padding:0;margin-bottom:.5rem;font-size:1.5rem;line-height:inherit;color:inherit;white-space:normal}progress{vertical-align:baseline}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button{height:auto}[type=search]{outline-offset:-2px;-webkit-appearance:none}[type=search]::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{font:inherit;-webkit-appearance:button}output{display:inline-block}summary{display:list-item;cursor:pointer}template{display:none}[hidden]{display:none!important}.h1,.h2,.h3,.h4,.h5,.h6,h1,h2,h3,h4,h5,h6{margin-bottom:.5rem;font-weight:500;line-height:1.2}.h1,h1{font-size:2.5rem}.h2,h2{font-size:2rem}.h3,h3{font-size:1.75rem}.h4,h4{font-size:1.5rem}.h5,h5{font-size:1.25rem}.h6,h6{font-size:1rem}.lead{font-size:1.25rem;font-weight:300}.display-1{font-size:6rem;font-weight:300;line-height:1.2}.display-2{font-size:5.5rem;font-weight:300;line-height:1.2}.display-3{font-size:4.5rem;font-weight:300;line-height:1.2}.display-4{font-size:3.5rem;font-weight:300;line-height:1.2}hr{margin-top:1rem;margin-bottom:1rem;border:0;border-top:1px solid rgba(0,0,0,.1)}.small,Line truncated
|
||||||
|
/*# sourceMappingURL=bootstrap.min.css.map */
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
!function(t,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define([],n):"object"==typeof exports?exports.History=n():t.History=n()}(this,function(){return function(t){function n(o){if(e[o])return e[o].exports;var r=e[o]={exports:{},id:o,loaded:!1};return t[o].call(r.exports,r,r.exports,n),r.loaded=!0,r.exports}var e={};return n.m=t,n.c=e,n.p="",n(0)}([function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}n.__esModule=!0,n.createPath=n.parsePath=n.locationsAreEqual=n.createLocation=n.createMemoryHistory=n.createHashHistory=n.createBrowserHistory=void 0;var r=e(2);Object.defineProperty(n,"createLocation",{enumerable:!0,get:function(){return r.createLocation}}),Object.defineProperty(n,"locationsAreEqual",{enumerable:!0,get:function(){return r.locationsAreEqual}});var i=e(1);Object.defineProperty(n,"parsePath",{enumerable:!0,get:function(){return i.parsePath}}),Object.defineProperty(n,"createPath",{enumerable:!0,get:function(){return i.createPath}});var a=e(7),c=o(a),u=e(8),s=o(u),f=e(9),l=o(f);n.createBrowserHistory=c.default,n.createHashHistory=s.default,n.createMemoryHistory=l.default},function(t,n){"use strict";n.__esModule=!0;n.addLeadingSlash=function(t){return"/"===t.charAt(0)?t:"/"+t},n.stripLeadingSlash=function(t){return"/"===t.charAt(0)?t.substr(1):t},n.stripPrefix=function(t,n){return 0===t.indexOf(n)?t.substr(n.length):t},n.stripTrailingSlash=function(t){return"/"===t.charAt(t.length-1)?t.slice(0,-1):t},n.parsePath=function(t){var n=t||"/",e="",o="",r=n.indexOf("#");r!==-1&&(o=n.substr(r),n=n.substr(0,r));var i=n.indexOf("?");return i!==-1&&(e=n.substr(i),n=n.substr(0,i)),n=decodeURI(n),{pathname:n,search:"?"===e?"":e,hash:"#"===o?"":o}},n.createPath=function(t){var n=t.pathname,e=t.search,o=t.hash,r=encodeURI(n||"/");return e&&"?"!==e&&(r+="?"===e.charAt(0)?e:"?"+e),o&&"#"!==o&&(r+="#"===o.charAt(0)?o:"#"+o),r}},function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}n.__esModule=!0,n.locationsAreEqual=n.createLocation=void 0;var r=Object.assign||function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var o in e)Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o])}return t},i=e(10),a=o(i),c=e(11),u=o(c),s=e(1);n.createLocation=function(t,n,e,o){var i=void 0;return"string"==typeof t?(i=(0,s.parsePath)(t),i.state=n):(i=r({},t),void 0===i.pathname&&(i.pathname=""),i.search?"?"!==i.search.charAt(0)&&(i.search="?"+i.search):i.search="",i.hash?"#"!==i.hash.charAt(0)&&(i.hash="#"+i.hash):i.hash="",void 0!==n&&void 0===i.state&&(i.state=n)),i.key=e,o&&(i.pathname?"/"!==i.pathname.charAt(0)&&(i.pathname=(0,a.default)(i.pathname,o.pathname)):i.pathname=o.pathname),i},n.locationsAreEqual=function(t,n){return t.pathname===n.pathname&&t.search===n.search&&t.hash===n.hash&&t.key===n.key&&(0,u.default)(t.state,n.state)}},function(t,n,e){"use strict";var o=function(){};t.exports=o},function(t,n,e){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}n.__esModule=!0;var r=e(3),i=(o(r),function(){var t=null,n=function(n){return t=n,function(){t===n&&(t=null)}},e=function(n,e,o,r){if(null!=t){var i="function"==typeof t?t(n,e):t;"string"==typeof i?"function"==typeof o?o(i,r):r(!0):r(i!==!1)}else r(!0)},o=[],r=function(t){var n=!0,e=function(){n&&t.apply(void 0,arguments)};return o.push(e),function(){n=!1,o=o.filter(function(t){return t!==e})}},i=function(){for(var t=arguments.length,n=Array(t),e=0;e<t;e++)n[e]=arguments[e];o.forEach(function(t){return t.apply(void 0,n)})};return{setPrompt:n,confirmTransitionTo:e,appendListener:r,notifyListeners:i}});n.default=i},function(t,n){"use strict";n.__esModule=!0;n.canUseDOM=!("undefined"==typeof window||!window.document||!window.document.createElement),n.addEventListener=function(t,n,e){return t.addEventListener?t.addEventListener(n,e,!1):t.attachEvent("on"+n,e)},n.removeEventListener=function(t,n,e){return t.removeEventListener?t.removeEventListener(n,e,!1):t.detachEvent("on"+n,e)},n.getConfirmation=function(t,n){return n(window.confirm(t))},n.supportsHistory=function(){var t=window.navigator.userAgent;return(t.indexOf("Android 2.")===-1&&t.indexOf("Android 4.0")===-1||t.indexOf("Mobile Safari")===-1||t.indexOf("Chrome")!==-1||t.indexOf("Windows Phone")!==-1)&&(window.history&&"pushState"in window.history)},n.supportsPopStateOnHashChange=function(){return window.navigator.userAgent.indexOf("Trident")===-1},n.supportsGoWithoutReloadUsingHash=function(){return window.navigator.userAgent.indexOf("Firefox")===-1},n.isExtraneousPopstateEvent=function(t){return void 0===t.state&&navigator.userAgent.indexOf("CriOS")===-1}},function(t,n,e){"use strict";var o=function(t,n,e,o,r,i,a,c){if(!t){var u;if(void 0===n)u=new Error("Minified exception occurred; use the non-minified dev environment for the full error message and additional helpful warnings.");else{var s=[e,o,r,i,a,c],f=0;u=new Error(n.replace(/%s/g,function(){return s[f++]})),u.name="InLine truncated
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
!function(){function n(n){var t=e.get(this);return t||(t=new Map,e.set(this,t)),1<(t=a(this,t.get(n)||(t.set(n,t=function(n){function t(n){1===u&&(n||(r=r.replace(/^\s*\n\s*|\s*\n\s*$/g,"")))?p.push(0,n,r):3===u&&(n||r)?(p.push(3,n,r),u=2):2===u&&"..."===r&&n?p.push(4,n,0):2===u&&r&&!n?p.push(5,0,!0,r):5<=u&&((r||!n&&5===u)&&(p.push(u,0,r,s),u=6),n&&(p.push(u,n,0,s),u=6)),r=""}for(var e,s,u=1,r="",h="",p=[0],a=0;a<n.length;a++){a&&(1===u&&t(),t(a));for(var o=0;o<n[a].length;o++)e=n[a][o],1===u?"<"===e?(t(),p=[p],u=3):r+=e:4===u?r="--"===r&&">"===e?(u=1,""):e+r[0]:h?e===h?h="":r+=e:'"'===e||"'"===e?h=e:">"===e?(t(),u=1):u&&("="===e?(u=5,s=r,r=""):"/"===e&&(u<5||">"===n[a][o+1])?(t(),3===u&&(p=p[0]),(p=(u=p)[0]).push(2,0,u),u=0):" "===e||"\t"===e||"\n"===e||"\r"===e?(t(),u=2):r+=e),3===u&&"!--"===r&&(u=4,p=p[0])}return t(),p}(n)),t),arguments,[])).length?t:t[0]}var a=function(n,t,e,s){var u;t[0]=0;for(var r=1;r<t.length;r++){var h=t[r++],p=t[r]?(t[0]|=h?1:2,e[t[r++]]):t[++r];3===h?s[0]=p:4===h?s[1]=Object.assign(s[1]||{},p):5===h?(s[1]=s[1]||{})[t[++r]]=p:6===h?s[1][t[++r]]+=p+"":h?(u=n.apply(p,a(n,p,e,["",null])),s.push(u),p[0]?t[0]|=2:(t[r-2]=0,t[r]=u)):s.push(p)}return s},e=new Map;"undefined"!=typeof module?module.exports=n:self.htm=n}();
|
||||||
|
After Width: | Height: | Size: 3.2 KiB |
|
After Width: | Height: | Size: 3.8 KiB |
@@ -0,0 +1,19 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<title>example</title>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<link rel="stylesheet" href="bootstrap.min.css" />
|
||||||
|
<link rel="stylesheet" href="style.css" />
|
||||||
|
<script src="preact.min.js"></script>
|
||||||
|
<script src="preact-router.min.js"></script>
|
||||||
|
<script src="htm.min.js"></script>
|
||||||
|
<script src="history.min.js"></script>
|
||||||
|
<script src="linkstate.min.js"></script>
|
||||||
|
<script src="axios.min.js"></script>
|
||||||
|
<script src="app.js"></script>
|
||||||
|
</head>
|
||||||
|
<body></body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
function linkState(component, key, eventPath) {
|
||||||
|
let path = key.split('.'), cache = component.__lsc || (component.__lsc = {});
|
||||||
|
|
||||||
|
return cache[key + eventPath] || (cache[key + eventPath] = function(e) {
|
||||||
|
let t = e && e.target || this, state = {}, obj = state,
|
||||||
|
v = typeof eventPath === 'string' ?
|
||||||
|
delve(e, eventPath) :
|
||||||
|
t.nodeName ? (t.type.match(/^che|rad/) ? t.checked : t.value) :
|
||||||
|
e,
|
||||||
|
i = 0;
|
||||||
|
for (; i < path.length - 1; i++) {
|
||||||
|
obj = obj[path[i]] ||
|
||||||
|
(obj[path[i]] = !i && component.state[path[i]] || {});
|
||||||
|
}
|
||||||
|
obj[path[i]] = v;
|
||||||
|
component.setState(state);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("preact")):"function"==typeof define&&define.amd?define(["preact"],e):t.preactRouter=e(t.preact)}(this,function(p){function i(t,e){for(var n in e)t[n]=e[n];return t}function u(t,e,n){var r,o=/(?:\?([^#]*))?(#.*)?$/,i=t.match(o),u={};if(i&&i[1])for(var a=i[1].split("&"),p=0;p<a.length;p++){var c=a[p].split("=");u[decodeURIComponent(c[0])]=decodeURIComponent(c.slice(1).join("="))}t=y(t.replace(o,"")),e=y(e||"");for(var f=Math.max(t.length,e.length),l=0;l<f;l++)if(e[l]&&":"===e[l].charAt(0)){var s=e[l].replace(/(^:|[+*?]+$)/g,""),h=(e[l].match(/[+*?]+$/)||v)[0]||"",d=~h.indexOf("+"),g=~h.indexOf("*"),m=t[l]||"";if(!m&&!g&&(h.indexOf("?")<0||d)){r=!1;break}if(u[s]=decodeURIComponent(m),d||g){u[s]=t.slice(l).map(decodeURIComponent).join("/");break}}else if(e[l]!==t[l]){r=!1;break}return(!0===n.default||!1!==r)&&u}function e(t,e){return t.rank<e.rank?1:t.rank>e.rank?-1:t.index-e.index}function n(t,e){return t.index=e,t.rank=(n=t).props.default?0:function(t){return y(t).map(r).join("")}(n.props.path),t.props;var n}function y(t){return t.replace(/(^\/+|\/+$)/g,"").split("/")}function r(t){return":"==t.charAt(0)?1+"*+?".indexOf(t.charAt(t.length-1))||4:5}function o(){var t;return""+((t=g&&g.location?g.location:g&&g.getCurrentLocation?g.getCurrentLocation():"undefined"!=typeof location?location:b).pathname||"")+(t.search||"")}function a(t,e){return void 0===e&&(e=!1),"string"!=typeof t&&t.url&&(e=t.replace,t=t.url),function(t){for(var e=m.length;e--;)if(m[e].canRoute(t))return 1;return}(t)&&(n=t,void 0===(r=e?"replace":"push")&&(r="push"),g&&g[r]?g[r](n):"undefined"!=typeof history&&history[r+"State"]&&history[r+"State"](null,null,n)),c(t);var n,r}function c(t){for(var e=!1,n=0;n<m.length;n++)!0===m[n].routeTo(t)&&(e=!0);for(var r=C.length;r--;)C[r](t);return e}function f(t){if(t&&t.getAttribute){var e=t.getAttribute("href"),n=t.getAttribute("target");if(e&&e.match(/^\//g)&&(!n||n.match(/^_?self$/i)))return a(e)}}function l(t){if(!(t.ctrlKey||t.metaKey||t.altKey||t.shiftKey||0!==t.button))return f(t.currentTarget||t.target||this),s(t)}function s(t){return t&&(t.stopImmediatePropagation&&t.stopImmediatePropagation(),t.stopPropagation&&t.stopPropagation(),t.preventDefault()),!1}function h(t){if(!(t.ctrlKey||t.metaKey||t.altKey||t.shiftKey||0!==t.button)){var e=t.target;do{if("A"===(e.nodeName+"").toUpperCase()&&e.getAttribute("href")){if(e.hasAttribute("native"))return;if(f(e))return s(t)}}while(e=e.parentNode)}}var d,v={},g=null,m=[],C=[],b={},U=!1,t=((d=p.Component)&&(k.__proto__=d),((k.prototype=Object.create(d&&d.prototype)).constructor=k).prototype.shouldComponentUpdate=function(t){return!0!==t.static||t.url!==this.props.url||t.onChange!==this.props.onChange},k.prototype.canRoute=function(t){return 0<this.getMatchingChildren(p.toChildArray(this.props.children),t,!1).length},k.prototype.routeTo=function(t){this.setState({url:t});var e=this.canRoute(t);return this.updating||this.forceUpdate(),e},k.prototype.componentWillMount=function(){m.push(this),this.updating=!0},k.prototype.componentDidMount=function(){var e=this;g&&(this.unlisten=g.listen(function(t){e.routeTo(""+(t.pathname||"")+(t.search||""))})),this.updating=!1},k.prototype.componentWillUnmount=function(){"function"==typeof this.unlisten&&this.unlisten(),m.splice(m.indexOf(this),1)},k.prototype.componentWillUpdate=function(){this.updating=!0},k.prototype.componentDidUpdate=function(){this.updating=!1},k.prototype.getMatchingChildren=function(t,r,o){return t.filter(n).sort(e).map(function(t){var e=u(r,t.props.path,t.props);if(e){if(!1===o)return t;var n={url:r,matches:e};return i(n,e),delete n.ref,delete n.key,p.cloneElement(t,n)}}).filter(Boolean)},k.prototype.render=function(t,e){var n=t.children,r=t.onChange,o=e.url,i=this.getMatchingChildren(p.toChildArray(n),o,!0),u=i[0]||null,a=this.previousUrl;return o!==a&&(this.previousUrl=o,"function"==typeof r&&r({router:this,url:o,previous:a,active:i,current:u})),u},k);function k(t){d.call(this,t),t.history&&(g=t.history),this.state={url:t.url||o()},U||("function"==typeof addEventListener&&(g||addEventListener("popstate",function(){c(o())}),addEventListener("click",h)),U=!0)}return t.subscribers=C,t.getCurrentUrl=o,t.route=a,(t.Router=t).Route=function(t){return p.createElement(t.component,t)},t.Link=function(t){return p.createElement("a",i({onClick:l},t))},t.exec=u,t});
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
!function(){var n,l,u,t,i,o,r,e,f={},c=[],a=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;function s(n,l){for(var u in l)n[u]=l[u];return n}function v(n){var l=n.parentNode;l&&l.removeChild(n)}function h(n,l,u){var t,i=arguments,o={};for(t in l)"key"!==t&&"ref"!==t&&(o[t]=l[t]);if(arguments.length>3)for(u=[u],t=3;t<arguments.length;t++)u.push(i[t]);if(null!=u&&(o.children=u),"function"==typeof n&&null!=n.defaultProps)for(t in n.defaultProps)void 0===o[t]&&(o[t]=n.defaultProps[t]);return y(n,o,l&&l.key,l&&l.ref,null)}function y(l,u,t,i,o){var r={type:l,props:u,key:t,ref:i,__k:null,__:null,__b:0,__e:null,__d:void 0,__c:null,constructor:void 0,__v:o};return null==o&&(r.__v=r),n.vnode&&n.vnode(r),r}function p(n){return n.children}function d(n,l){this.props=n,this.context=l}function _(n,l){if(null==l)return n.__?_(n.__,n.__.__k.indexOf(n)+1):null;for(var u;l<n.__k.length;l++)if(null!=(u=n.__k[l])&&null!=u.__e)return u.__e;return"function"==typeof n.type?_(n):null}function m(n){var l,u;if(null!=(n=n.__)&&null!=n.__c){for(n.__e=n.__c.base=null,l=0;l<n.__k.length;l++)if(null!=(u=n.__k[l])&&null!=u.__e){n.__e=n.__c.base=u.__e;break}return m(n)}}function k(l){(!l.__d&&(l.__d=!0)&&u.push(l)&&!w.__r++||i!==n.debounceRendering)&&((i=n.debounceRendering)||t)(w)}function w(){for(var n;w.__r=u.length;)n=u.sort(function(n,l){return n.__v.__b-l.__v.__b}),u=[],n.some(function(n){var l,u,t,i,o,r,e;n.__d&&(r=(o=(l=n).__v).__e,(e=l.__P)&&(u=[],(t=s({},o)).__v=t,i=N(e,o,t,l.__n,void 0!==e.ownerSVGElement,null,u,null==r?_(o):r),z(u,o),i!=r&&m(o)))})}function g(n,l,u,t,i,o,r,e,a,s){var h,d,m,k,w,g,x,C=t&&t.__k||c,A=C.length;for(a==f&&(a=null!=r?r[0]:A?_(t,0):null),u.__k=[],h=0;h<l.length;h++)if(null!=(k=u.__k[h]=null==(k=l[h])||"boolean"==typeof k?null:"string"==typeof k||"number"==typeof k?y(null,k,null,null,k):Array.isArray(k)?y(p,{children:k},null,null,null):null!=k.__e||null!=k.__c?y(k.type,k.props,k.key,null,k.__v):k)){if(k.__=u,k.__b=u.__b+1,null===(m=C[h])||m&&k.key==m.key&&k.type===m.type)C[h]=void 0;else for(d=0;d<A;d++){if((m=C[d])&&k.key==m.key&&k.type===m.type){C[d]=void 0;break}m=null}w=N(n,k,m=m||f,i,o,r,e,a,s),(d=k.ref)&&m.ref!=d&&(x||(x=[]),m.ref&&x.push(m.ref,null,k),x.push(d,k.__c||w,k)),null!=w?(null==g&&(g=w),a=b(n,k,m,C,r,w,a),"option"==u.type?n.value="":"function"==typeof u.type&&(u.__d=a)):a&&m.__e==a&&a.parentNode!=n&&(a=_(m))}if(u.__e=g,null!=r&&"function"!=typeof u.type)for(h=r.length;h--;)null!=r[h]&&v(r[h]);for(h=A;h--;)null!=C[h]&&j(C[h],C[h]);if(x)for(h=0;h<x.length;h++)$(x[h],x[++h],x[++h])}function b(n,l,u,t,i,o,r){var e,f,c;if(void 0!==l.__d)e=l.__d,l.__d=void 0;else if(i==u||o!=r||null==o.parentNode)n:if(null==r||r.parentNode!==n)n.appendChild(o),e=null;else{for(f=r,c=0;(f=f.nextSibling)&&c<t.length;c+=2)if(f==o)break n;n.insertBefore(o,r),e=r}return void 0!==e?e:o.nextSibling}function x(n,l,u,t,i){var o;for(o in u)"children"===o||"key"===o||o in l||A(n,o,null,u[o],t);for(o in l)i&&"function"!=typeof l[o]||"children"===o||"key"===o||"value"===o||"checked"===o||u[o]===l[o]||A(n,o,l[o],u[o],t)}function C(n,l,u){"-"===l[0]?n.setProperty(l,u):n[l]="number"==typeof u&&!1===a.test(l)?u+"px":null==u?"":u}function A(n,l,u,t,i){var o,r,e,f,c;if(i?"className"===l&&(l="class"):"class"===l&&(l="className"),"style"===l)if(o=n.style,"string"==typeof u)o.cssText=u;else{if("string"==typeof t&&(o.cssText="",t=null),t)for(f in t)u&&f in u||C(o,f,"");if(u)for(c in u)t&&u[c]===t[c]||C(o,c,u[c])}else"o"===l[0]&&"n"===l[1]?(r=l!==(l=l.replace(/Capture$/,"")),e=l.toLowerCase(),l=(e in n?e:l).slice(2),u?(t||n.addEventListener(l,P,r),(n.l||(n.l={}))[l]=u):n.removeEventListener(l,P,r)):"list"!==l&&"tagName"!==l&&"form"!==l&&"type"!==l&&"size"!==l&&!i&&l in n?n[l]=null==u?"":u:"function"!=typeof u&&"dangerouslySetInnerHTML"!==l&&(l!==(l=l.replace(/^xlink:?/,""))?null==u||!1===u?n.removeAttributeNS("http://www.w3.org/1999/xlink",l.toLowerCase()):n.setAttributeNS("http://www.w3.org/1999/xlink",l.toLowerCase(),u):null==u||!1===u&&!/^ar/.test(l)?n.removeAttribute(l):n.setAttribute(l,u))}function P(l){this.l[l.type](n.event?n.event(l):l)}function E(n,l,u){var t,i;for(t=0;t<n.__k.length;t++)(i=n.__k[t])&&(i.__=n,i.__e&&("function"==typeof i.type&&i.__k.length>1&&E(i,l,u),l=b(u,i,i,n.__k,null,i.__e,l),"function"==typeof n.type&&(n.__d=l)))}function N(l,u,t,i,o,r,e,f,c){var a,v,h,y,_,m,k,w,b,x,C,A=u.type;if(void 0!==u.constructor)return null;(a=n.__b)&&a(u);try{n:if("function"==typeof A){if(w=u.props,b=(a=A.contextType)&&i[a.__c],x=a?b?b.props.value:a.__:i,t.__c?k=(v=u.__c=t.__c).__=v.__E:("prototype"in A&&A.prototype.render?u.__c=v=new A(w,x):(u.__c=v=new d(w,x),v.constructor=A,v.render=F),b&&b.sub(v),v.props=w,v.state||(v.state={}),v.context=x,v.__n=i,h=v.__d=!0,v.__h=[]),null==v.__s&&(v.__s=v.state),null!=A.getDerivedStateFromProps&&(v.__s==v.state&&(v.__s=s({},v.__s)),s(v.__s,A.getDerivedStateFromProps(w,v.__s))),y=v.props,_=v.state,h)null==A.getDerivedStateFromProps&&null!=v.componentWillMount&&v.componenLine truncated
|
||||||
|
//# sourceMappingURL=preact.min.js.map
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
html, body { margin: 0; padding: 0; height: 100%; }
|
||||||
|
select, input, label::before, textarea { outline: none; box-shadow:none !important; border: 1px solid #ccc !important; }
|
||||||
|
.btn:focus,.btn:active:focus,.btn.active:focus,
|
||||||
|
.btn.focus,.btn:active.focus,.btn.active.focus { outline: none; box-shadow:none !important; }
|
||||||
|
.dropdown.autoexpand:hover>.dropdown-menu { display: block; }
|
||||||
|
.pointer { cursor: pointer; }
|
||||||
|
.log { max-height: 10em; overflow: auto; height: 100%; }
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
PROG = server
|
|
||||||
include ../examples.mk
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
// Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
static const char *s_http_port = "8000";
|
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
|
|
||||||
if (ev == MG_EV_HTTP_REQUEST) {
|
|
||||||
mg_serve_http(nc, p, s_http_server_opts); // Serves static content
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
cs_stat_t st;
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
||||||
if (nc == NULL) {
|
|
||||||
fprintf(stderr, "Cannot bind to %s\n", s_http_port);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up HTTP server parameters
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
s_http_server_opts.document_root = "web_root"; // Set up web root directory
|
|
||||||
|
|
||||||
if (mg_stat(s_http_server_opts.document_root, &st) != 0) {
|
|
||||||
fprintf(stderr, "%s", "Cannot find web_root directory, exiting\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Starting web server on port %s\n", s_http_port);
|
|
||||||
for (;;) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>Connected device demo</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="stylesheet" type="text/css" href="style.css">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="content">
|
|
||||||
<img src="logo.png" style="float:right; height: 50px; border-radius: 3px;">
|
|
||||||
<h1>Connected device demo - static content only</h1>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
This page shows initial steps on how to make a device connected.
|
|
||||||
Mongoose serves static content from the <code>web_root</code>
|
|
||||||
directory. Images, CSS, HTML, JavaScript files could be dropped into the
|
|
||||||
<code>web_root</code> directory - and they'll be served happily
|
|
||||||
by Mongoose.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
Below is a placeholder for the
|
|
||||||
settings section, and a real-time dashboard, which will be
|
|
||||||
implemented in the following examples.
|
|
||||||
Both settings section, and a dashboard section, could be organised
|
|
||||||
differently on this UI - for example, in different tabs. But, to
|
|
||||||
make the demonstration clearer, we show everything on one page.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<h1>Settings section</h1>
|
|
||||||
<p>Nothing here yet</p>
|
|
||||||
|
|
||||||
<h1>Dashboard section</h1>
|
|
||||||
<p>Nothing here yet</p>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
Before Width: | Height: | Size: 5.7 KiB |
@@ -1,31 +0,0 @@
|
|||||||
* {
|
|
||||||
outline: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
background-color: #36c;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
font: 16px/1.4 Helvetica, Arial, sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
div.content {
|
|
||||||
width: 800px;
|
|
||||||
margin: 1em auto;
|
|
||||||
padding: 2em;
|
|
||||||
background-color: #fff;
|
|
||||||
border-radius: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
background: #f0f0f0;
|
|
||||||
padding: 0 0.3em;
|
|
||||||
color: #396;
|
|
||||||
border-radius: 0.3em;
|
|
||||||
}
|
|
||||||
|
|
||||||
a:link, a:visited {
|
|
||||||
color: #69c;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
PROG = server
|
|
||||||
include ../examples.mk
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
// Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
struct device_settings {
|
|
||||||
char setting1[100];
|
|
||||||
char setting2[100];
|
|
||||||
};
|
|
||||||
|
|
||||||
static const char *s_http_port = "8000";
|
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
|
||||||
static struct device_settings s_settings = { "value1", "value2" };
|
|
||||||
|
|
||||||
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
|
||||||
// Get form variables and store settings values
|
|
||||||
mg_get_http_var(&hm->body, "setting1", s_settings.setting1,
|
|
||||||
sizeof(s_settings.setting1));
|
|
||||||
mg_get_http_var(&hm->body, "setting2", s_settings.setting2,
|
|
||||||
sizeof(s_settings.setting2));
|
|
||||||
|
|
||||||
// Send response
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 302 OK\r\nLocation: /\r\n\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_ssi_call(struct mg_connection *nc, const char *param) {
|
|
||||||
if (strcmp(param, "setting1") == 0) {
|
|
||||||
mg_printf_html_escape(nc, "%s", s_settings.setting1);
|
|
||||||
} else if (strcmp(param, "setting2") == 0) {
|
|
||||||
mg_printf_html_escape(nc, "%s", s_settings.setting2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
struct http_message *hm = (struct http_message *) ev_data;
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_HTTP_REQUEST:
|
|
||||||
if (mg_vcmp(&hm->uri, "/save") == 0) {
|
|
||||||
handle_save(nc, hm);
|
|
||||||
} else {
|
|
||||||
mg_serve_http(nc, hm, s_http_server_opts); // Serve static content
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MG_EV_SSI_CALL:
|
|
||||||
handle_ssi_call(nc, ev_data);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
cs_stat_t st;
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
||||||
if (nc == NULL) {
|
|
||||||
fprintf(stderr, "Cannot bind to %s\n", s_http_port);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up HTTP server parameters
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
s_http_server_opts.document_root = "web_root"; // Set up web root directory
|
|
||||||
|
|
||||||
if (mg_stat(s_http_server_opts.document_root, &st) != 0) {
|
|
||||||
fprintf(stderr, "%s", "Cannot find web_root directory, exiting\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Starting web server on port %s\n", s_http_port);
|
|
||||||
for (;;) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>Connected device demo</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="stylesheet" type="text/css" href="style.css">
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="content">
|
|
||||||
<img src="logo.png" style="float:right; height: 50px; border-radius: 3px;">
|
|
||||||
<h1>Connected device demo - settings section</h1>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
This page is one of the series of examples that show how to make
|
|
||||||
devices connected with Mongoose. Here, we create a settings section
|
|
||||||
that expose some internal device variables, and allow user to change
|
|
||||||
them. They could be some stored in a database, in a file, or in
|
|
||||||
firmware RAM. In this example, we store them in RAM, and use two
|
|
||||||
variables, one integer and one string. We use SSI
|
|
||||||
<code><!--#call parameter_name --></code> directive to fetch
|
|
||||||
variable values from Mongoose.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<h1>Settings section</h1>
|
|
||||||
<form method="POST" action="/save">
|
|
||||||
<fieldset>
|
|
||||||
<legend>Device settings</legend>
|
|
||||||
<label>Setting 1:</label> <input type="text"
|
|
||||||
name="setting1" value="<!--#call setting1 -->" >
|
|
||||||
<label>Setting 2:</label> <input type="text"
|
|
||||||
name="setting2" value="<!--#call setting2 -->" >
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<button type="submit">Save Settings</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</fieldset>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<h1>Dashboard section</h1>
|
|
||||||
<p>Nothing here yet</p>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
Before Width: | Height: | Size: 5.7 KiB |
@@ -1,67 +0,0 @@
|
|||||||
* {
|
|
||||||
outline: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
background: #36c;
|
|
||||||
color: #555;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
font: 16px/1.4 Helvetica, Arial, sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
div.content {
|
|
||||||
width: 800px;
|
|
||||||
margin: 1em auto;
|
|
||||||
padding: 2em;
|
|
||||||
background-color: #fff;
|
|
||||||
border-radius: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset {
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
padding: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset legend {
|
|
||||||
font-style: italic;
|
|
||||||
color: silver;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset label {
|
|
||||||
text-align: right;
|
|
||||||
padding: 0 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type=text] {
|
|
||||||
font-size: 100%;
|
|
||||||
padding: 0.2em;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
color: #666;
|
|
||||||
background: #eee;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
padding: 0.2em 2em;
|
|
||||||
font-size: 100%;
|
|
||||||
cursor: pointer;
|
|
||||||
margin-top: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
button:hover {
|
|
||||||
background: #ccc;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
background: #f0f0f0;
|
|
||||||
padding: 0 0.3em;
|
|
||||||
color: #396;
|
|
||||||
border-radius: 0.3em;
|
|
||||||
}
|
|
||||||
|
|
||||||
a:link, a:visited {
|
|
||||||
color: #69c;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
PROG = server
|
|
||||||
include ../examples.mk
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
// Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
struct device_settings {
|
|
||||||
char setting1[100];
|
|
||||||
char setting2[100];
|
|
||||||
};
|
|
||||||
|
|
||||||
static const char *s_http_port = "8000";
|
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
|
||||||
static struct device_settings s_settings = { "value1", "value2" };
|
|
||||||
|
|
||||||
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
|
||||||
// Get form variables and store settings values
|
|
||||||
mg_get_http_var(&hm->body, "setting1", s_settings.setting1,
|
|
||||||
sizeof(s_settings.setting1));
|
|
||||||
mg_get_http_var(&hm->body, "setting2", s_settings.setting2,
|
|
||||||
sizeof(s_settings.setting2));
|
|
||||||
|
|
||||||
// Send response
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 302 OK\r\nLocation: /\r\n\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_get_cpu_usage(struct mg_connection *nc) {
|
|
||||||
// Generate random value, as an example of changing CPU usage
|
|
||||||
// Getting real CPU usage depends on the OS.
|
|
||||||
int cpu_usage = (double) rand() / RAND_MAX * 100.0;
|
|
||||||
|
|
||||||
// Use chunked encoding in order to avoid calculating Content-Length
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
|
||||||
|
|
||||||
// Output JSON object which holds CPU usage data
|
|
||||||
mg_printf_http_chunk(nc, "{ \"result\": %d }", cpu_usage);
|
|
||||||
|
|
||||||
// Send empty chunk, the end of response
|
|
||||||
mg_send_http_chunk(nc, "", 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_ssi_call(struct mg_connection *nc, const char *param) {
|
|
||||||
if (strcmp(param, "setting1") == 0) {
|
|
||||||
mg_printf_html_escape(nc, "%s", s_settings.setting1);
|
|
||||||
} else if (strcmp(param, "setting2") == 0) {
|
|
||||||
mg_printf_html_escape(nc, "%s", s_settings.setting2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
struct http_message *hm = (struct http_message *) ev_data;
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_HTTP_REQUEST:
|
|
||||||
if (mg_vcmp(&hm->uri, "/save") == 0) {
|
|
||||||
handle_save(nc, hm);
|
|
||||||
} else if (mg_vcmp(&hm->uri, "/get_cpu_usage") == 0) {
|
|
||||||
handle_get_cpu_usage(nc);
|
|
||||||
} else {
|
|
||||||
mg_serve_http(nc, hm, s_http_server_opts); // Serve static content
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MG_EV_SSI_CALL:
|
|
||||||
handle_ssi_call(nc, ev_data);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
cs_stat_t st;
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
||||||
if (nc == NULL) {
|
|
||||||
fprintf(stderr, "Cannot bind to %s\n", s_http_port);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up HTTP server parameters
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
s_http_server_opts.document_root = "web_root"; // Set up web root directory
|
|
||||||
|
|
||||||
if (mg_stat(s_http_server_opts.document_root, &st) != 0) {
|
|
||||||
fprintf(stderr, "%s", "Cannot find web_root directory, exiting\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Starting web server on port %s\n", s_http_port);
|
|
||||||
for (;;) {
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
}
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>Connected device demo</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="stylesheet" type="text/css" href="style.css">
|
|
||||||
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
|
|
||||||
<script type="text/javascript" src="main.js"></script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="content">
|
|
||||||
<img src="logo.png" style="float:right; height: 50px; border-radius: 3px;">
|
|
||||||
<h1>Connected device demo - RESTful call</h1>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
This page is one of the series of examples that show how to make
|
|
||||||
devices connected with Mongoose. Here we add a RESTful call
|
|
||||||
to the Mongoose to show CPU usage stats on a dashboard.
|
|
||||||
RESTful call is done using Jquery's <code>ajax</code> call.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<h1>Settings section</h1>
|
|
||||||
<form method="POST" action="/save">
|
|
||||||
<fieldset>
|
|
||||||
<legend>Device settings</legend>
|
|
||||||
<label>Setting 1:</label> <input type="text"
|
|
||||||
name="setting1" value="<!--#call setting1 -->" >
|
|
||||||
<label>Setting 2:</label> <input type="text"
|
|
||||||
name="setting2" value="<!--#call setting2 -->" >
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<button type="submit">Save Settings</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</fieldset>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<h1>Dashboard section</h1>
|
|
||||||
<label>CPU usage:</label> <span id="cpu_usage"> </span>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
/*! jQuery v1.11.3 | (c) 2005, 2015 jQuery Foundation, Inc. | jquery.org/license */
|
|
||||||
!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){var c=[],d=c.slice,e=c.concat,f=c.push,g=c.indexOf,h={},i=h.toString,j=h.hasOwnProperty,k={},l="1.11.3",m=function(a,b){return new m.fn.init(a,b)},n=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,o=/^-ms-/,p=/-([\da-z])/gi,q=function(a,b){return b.toUpperCase()};m.fn=m.prototype={jquery:l,constructor:m,selector:"",length:0,toArray:function(){return d.call(this)},get:function(a){return null!=a?0>a?this[a+this.length]:this[a]:d.call(this)},pushStack:function(a){var b=m.merge(this.constructor(),a);return b.prevObject=this,b.context=this.context,b},each:function(a,b){return m.each(this,a,b)},map:function(a){return this.pushStack(m.map(this,function(b,c){return a.call(b,c,b)}))},slice:function(){return this.pushStack(d.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(a){var b=this.length,c=+a+(0>a?b:0);return this.pushStack(c>=0&&b>c?[this[c]]:[])},end:function(){return this.prevObject||this.constructor(null)},push:f,sort:c.sort,splice:c.splice},m.extend=m.fn.extend=function(){var a,b,c,d,e,f,g=arguments[0]||{},h=1,i=arguments.length,j=!1;for("boolean"==typeof g&&(j=g,g=arguments[h]||{},h++),"object"==typeof g||m.isFunction(g)||(g={}),h===i&&(g=this,h--);i>h;h++)if(null!=(e=arguments[h]))for(d in e)a=g[d],c=e[d],g!==c&&(j&&c&&(m.isPlainObject(c)||(b=m.isArray(c)))?(b?(b=!1,f=a&&m.isArray(a)?a:[]):f=a&&m.isPlainObject(a)?a:{},g[d]=m.extend(j,f,c)):void 0!==c&&(g[d]=c));return g},m.extend({expando:"jQuery"+(l+Math.random()).replace(/\D/g,""),isReady:!0,error:function(a){throw new Error(a)},noop:function(){},isFunction:function(a){return"function"===m.type(a)},isArray:Array.isArray||function(a){return"array"===m.type(a)},isWindow:function(a){return null!=a&&a==a.window},isNumeric:function(a){return!m.isArray(a)&&a-parseFloat(a)+1>=0},isEmptyObject:function(a){var b;for(b in a)return!1;return!0},isPlainObject:function(a){var b;if(!a||"object"!==m.type(a)||a.nodeType||m.isWindow(a))return!1;try{if(a.constructor&&!j.call(a,"constructor")&&!j.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}if(k.ownLast)for(b in a)return j.call(a,b);for(b in a);return void 0===b||j.call(a,b)},type:function(a){return null==a?a+"":"object"==typeof a||"function"==typeof a?h[i.call(a)]||"object":typeof a},globalEval:function(b){b&&m.trim(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(o,"ms-").replace(p,q)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toLowerCase()===b.toLowerCase()},each:function(a,b,c){var d,e=0,f=a.length,g=r(a);if(c){if(g){for(;f>e;e++)if(d=b.apply(a[e],c),d===!1)break}else for(e in a)if(d=b.apply(a[e],c),d===!1)break}else if(g){for(;f>e;e++)if(d=b.call(a[e],e,a[e]),d===!1)break}else for(e in a)if(d=b.call(a[e],e,a[e]),d===!1)break;return a},trim:function(a){return null==a?"":(a+"").replace(n,"")},makeArray:function(a,b){var c=b||[];return null!=a&&(r(Object(a))?m.merge(c,"string"==typeof a?[a]:a):f.call(c,a)),c},inArray:function(a,b,c){var d;if(b){if(g)return g.call(b,a,c);for(d=b.length,c=c?0>c?Math.max(0,d+c):c:0;d>c;c++)if(c in b&&b[c]===a)return c}return-1},merge:function(a,b){var c=+b.length,d=0,e=a.length;while(c>d)a[e++]=b[d++];if(c!==c)while(void 0!==b[d])a[e++]=b[d++];return a.length=e,a},grep:function(a,b,c){for(var d,e=[],f=0,g=a.length,h=!c;g>f;f++)d=!b(a[f],f),d!==h&&e.push(a[f]);return e},map:function(a,b,c){var d,f=0,g=a.length,h=r(a),i=[];if(h)for(;g>f;f++)d=b(a[f],f,c),null!=d&&i.push(d);else for(f in a)d=b(a[f],f,c),null!=d&&i.push(d);return e.apply([],i)},guid:1,proxy:function(a,b){var c,e,f;return"string"==typeof b&&(f=a[b],b=a,a=f),m.isFunction(a)?(c=d.call(arguments,2),e=function(){return a.apply(b||this,c.concat(d.call(arguments)))},e.guid=a.guid=a.guid||m.guid++,e):void 0},now:function(){return+new Date},support:k}),m.each("Boolean Number String Function Array Date RegExp Object Error".split(" "),function(a,b){h["[object "+b+"]"]=b.toLowerCase()});function r(a){var b="length"in a&&a.length,c=m.type(a);return"function"===c||m.isWindow(a)?!1:1===a.nodeType&&b?!0:"array"===c||0===b||"number"==typeof b&&b>0&&b-1 in a}var s=function(a){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u="sizzle"+1*new Date,v=a.document,w=0,x=0,y=ha(),z=ha(),A=ha(),B=function(a,b){return a===b&&(l=!0),0},C=1<<31,D={}.hasOwnProperty,E=[],F=E.pop,G=E.push,H=E.push,I=E.slice,J=function(a,b){for(var c=0,d=a.length;d>c;c++)if(a[c]===b)return c;return-1},K="checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",L="[\\x20\\t\\r\\n\\f]",M="(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+",N=M.replace("w","w#"),O="\\["+L+"*("+M+")(?:"+L+"*([*^$|!~]?=)"+L+"*(?:'((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^Line truncated
|
|
||||||
|
|
||||||
return!0}function Q(a,b,d,e){if(m.acceptData(a)){var f,g,h=m.expando,i=a.nodeType,j=i?m.cache:a,k=i?a[h]:a[h]&&h;if(k&&j[k]&&(e||j[k].data)||void 0!==d||"string"!=typeof b)return k||(k=i?a[h]=c.pop()||m.guid++:h),j[k]||(j[k]=i?{}:{toJSON:m.noop}),("object"==typeof b||"function"==typeof b)&&(e?j[k]=m.extend(j[k],b):j[k].data=m.extend(j[k].data,b)),g=j[k],e||(g.data||(g.data={}),g=g.data),void 0!==d&&(g[m.camelCase(b)]=d),"string"==typeof b?(f=g[b],null==f&&(f=g[m.camelCase(b)])):f=g,f}}function R(a,b,c){if(m.acceptData(a)){var d,e,f=a.nodeType,g=f?m.cache:a,h=f?a[m.expando]:m.expando;if(g[h]){if(b&&(d=c?g[h]:g[h].data)){m.isArray(b)?b=b.concat(m.map(b,m.camelCase)):b in d?b=[b]:(b=m.camelCase(b),b=b in d?[b]:b.split(" ")),e=b.length;while(e--)delete d[b[e]];if(c?!P(d):!m.isEmptyObject(d))return}(c||(delete g[h].data,P(g[h])))&&(f?m.cleanData([a],!0):k.deleteExpando||g!=g.window?delete g[h]:g[h]=null)}}}m.extend({cache:{},noData:{"applet ":!0,"embed ":!0,"object ":"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"},hasData:function(a){return a=a.nodeType?m.cache[a[m.expando]]:a[m.expando],!!a&&!P(a)},data:function(a,b,c){return Q(a,b,c)},removeData:function(a,b){return R(a,b)},_data:function(a,b,c){return Q(a,b,c,!0)},_removeData:function(a,b){return R(a,b,!0)}}),m.fn.extend({data:function(a,b){var c,d,e,f=this[0],g=f&&f.attributes;if(void 0===a){if(this.length&&(e=m.data(f),1===f.nodeType&&!m._data(f,"parsedAttrs"))){c=g.length;while(c--)g[c]&&(d=g[c].name,0===d.indexOf("data-")&&(d=m.camelCase(d.slice(5)),O(f,d,e[d])));m._data(f,"parsedAttrs",!0)}return e}return"object"==typeof a?this.each(function(){m.data(this,a)}):arguments.length>1?this.each(function(){m.data(this,a,b)}):f?O(f,a,m.data(f,a)):void 0},removeData:function(a){return this.each(function(){m.removeData(this,a)})}}),m.extend({queue:function(a,b,c){var d;return a?(b=(b||"fx")+"queue",d=m._data(a,b),c&&(!d||m.isArray(c)?d=m._data(a,b,m.makeArray(c)):d.push(c)),d||[]):void 0},dequeue:function(a,b){b=b||"fx";var c=m.queue(a,b),d=c.length,e=c.shift(),f=m._queueHooks(a,b),g=function(){m.dequeue(a,b)};"inprogress"===e&&(e=c.shift(),d--),e&&("fx"===b&&c.unshift("inprogress"),delete f.stop,e.call(a,g,f)),!d&&f&&f.empty.fire()},_queueHooks:function(a,b){var c=b+"queueHooks";return m._data(a,c)||m._data(a,c,{empty:m.Callbacks("once memory").add(function(){m._removeData(a,b+"queue"),m._removeData(a,c)})})}}),m.fn.extend({queue:function(a,b){var c=2;return"string"!=typeof a&&(b=a,a="fx",c--),arguments.length<c?m.queue(this[0],a):void 0===b?this:this.each(function(){var c=m.queue(this,a,b);m._queueHooks(this,a),"fx"===a&&"inprogress"!==c[0]&&m.dequeue(this,a)})},dequeue:function(a){return this.each(function(){m.dequeue(this,a)})},clearQueue:function(a){return this.queue(a||"fx",[])},promise:function(a,b){var c,d=1,e=m.Deferred(),f=this,g=this.length,h=function(){--d||e.resolveWith(f,[f])};"string"!=typeof a&&(b=a,a=void 0),a=a||"fx";while(g--)c=m._data(f[g],a+"queueHooks"),c&&c.empty&&(d++,c.empty.add(h));return h(),e.promise(b)}});var S=/[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,T=["Top","Right","Bottom","Left"],U=function(a,b){return a=b||a,"none"===m.css(a,"display")||!m.contains(a.ownerDocument,a)},V=m.access=function(a,b,c,d,e,f,g){var h=0,i=a.length,j=null==c;if("object"===m.type(c)){e=!0;for(h in c)m.access(a,b,h,c[h],!0,f,g)}else if(void 0!==d&&(e=!0,m.isFunction(d)||(g=!0),j&&(g?(b.call(a,d),b=null):(j=b,b=function(a,b,c){return j.call(m(a),c)})),b))for(;i>h;h++)b(a[h],c,g?d:d.call(a[h],h,b(a[h],c)));return e?a:j?b.call(a):i?b(a[0],c):f},W=/^(?:checkbox|radio)$/i;!function(){var a=y.createElement("input"),b=y.createElement("div"),c=y.createDocumentFragment();if(b.innerHTML=" <link/><table></table><a href='/a'>a</a><input type='checkbox'/>",k.leadingWhitespace=3===b.firstChild.nodeType,k.tbody=!b.getElementsByTagName("tbody").length,k.htmlSerialize=!!b.getElementsByTagName("link").length,k.html5Clone="<:nav></:nav>"!==y.createElement("nav").cloneNode(!0).outerHTML,a.type="checkbox",a.checked=!0,c.appendChild(a),k.appendChecked=a.checked,b.innerHTML="<textarea>x</textarea>",k.noCloneChecked=!!b.cloneNode(!0).lastChild.defaultValue,c.appendChild(b),b.innerHTML="<input type='radio' checked='checked' name='t'/>",k.checkClone=b.cloneNode(!0).cloneNode(!0).lastChild.checked,k.noCloneEvent=!0,b.attachEvent&&(b.attachEvent("onclick",function(){k.noCloneEvent=!1}),b.cloneNode(!0).click()),null==k.deleteExpando){k.deleteExpando=!0;try{delete b.test}catch(d){k.deleteExpando=!1}}}(),function(){var b,c,d=y.createElement("div");for(b in{submit:!0,change:!0,focusin:!0})c="on"+b,(k[b+"Bubbles"]=c in a)||(d.setAttribute(c,"t"),k[b+"Bubbles"]=d.attributes[c].expando===!1);d=null}();var X=/^(?:input|select|textarea)$/i,Y=/^key/,Z=/^(?:mouse|pointer|contextmenu)|click/,$=/^(?:focusinfocus|focusoutblur)$/,_=/^([^.]*)(?:\.(.+)|)$/;function aa(){return!0}function ba(){return!1}function ca(){try{return y.activeElement}catch(a){}}m.event={global:{},add:function(a,b,c,d,e){var
Line truncated
|
|
||||||
return new Za.prototype.init(a,b,c,d,e)}m.Tween=Za,Za.prototype={constructor:Za,init:function(a,b,c,d,e,f){this.elem=a,this.prop=c,this.easing=e||"swing",this.options=b,this.start=this.now=this.cur(),this.end=d,this.unit=f||(m.cssNumber[c]?"":"px")},cur:function(){var a=Za.propHooks[this.prop];return a&&a.get?a.get(this):Za.propHooks._default.get(this)},run:function(a){var b,c=Za.propHooks[this.prop];return this.options.duration?this.pos=b=m.easing[this.easing](a,this.options.duration*a,0,1,this.options.duration):this.pos=b=a,this.now=(this.end-this.start)*b+this.start,this.options.step&&this.options.step.call(this.elem,this.now,this),c&&c.set?c.set(this):Za.propHooks._default.set(this),this}},Za.prototype.init.prototype=Za.prototype,Za.propHooks={_default:{get:function(a){var b;return null==a.elem[a.prop]||a.elem.style&&null!=a.elem.style[a.prop]?(b=m.css(a.elem,a.prop,""),b&&"auto"!==b?b:0):a.elem[a.prop]},set:function(a){m.fx.step[a.prop]?m.fx.step[a.prop](a):a.elem.style&&(null!=a.elem.style[m.cssProps[a.prop]]||m.cssHooks[a.prop])?m.style(a.elem,a.prop,a.now+a.unit):a.elem[a.prop]=a.now}}},Za.propHooks.scrollTop=Za.propHooks.scrollLeft={set:function(a){a.elem.nodeType&&a.elem.parentNode&&(a.elem[a.prop]=a.now)}},m.easing={linear:function(a){return a},swing:function(a){return.5-Math.cos(a*Math.PI)/2}},m.fx=Za.prototype.init,m.fx.step={};var $a,_a,ab=/^(?:toggle|show|hide)$/,bb=new RegExp("^(?:([+-])=|)("+S+")([a-z%]*)$","i"),cb=/queueHooks$/,db=[ib],eb={"*":[function(a,b){var c=this.createTween(a,b),d=c.cur(),e=bb.exec(b),f=e&&e[3]||(m.cssNumber[a]?"":"px"),g=(m.cssNumber[a]||"px"!==f&&+d)&&bb.exec(m.css(c.elem,a)),h=1,i=20;if(g&&g[3]!==f){f=f||g[3],e=e||[],g=+d||1;do h=h||".5",g/=h,m.style(c.elem,a,g+f);while(h!==(h=c.cur()/d)&&1!==h&&--i)}return e&&(g=c.start=+g||+d||0,c.unit=f,c.end=e[1]?g+(e[1]+1)*e[2]:+e[2]),c}]};function fb(){return setTimeout(function(){$a=void 0}),$a=m.now()}function gb(a,b){var c,d={height:a},e=0;for(b=b?1:0;4>e;e+=2-b)c=T[e],d["margin"+c]=d["padding"+c]=a;return b&&(d.opacity=d.width=a),d}function hb(a,b,c){for(var d,e=(eb[b]||[]).concat(eb["*"]),f=0,g=e.length;g>f;f++)if(d=e[f].call(c,b,a))return d}function ib(a,b,c){var d,e,f,g,h,i,j,l,n=this,o={},p=a.style,q=a.nodeType&&U(a),r=m._data(a,"fxshow");c.queue||(h=m._queueHooks(a,"fx"),null==h.unqueued&&(h.unqueued=0,i=h.empty.fire,h.empty.fire=function(){h.unqueued||i()}),h.unqueued++,n.always(function(){n.always(function(){h.unqueued--,m.queue(a,"fx").length||h.empty.fire()})})),1===a.nodeType&&("height"in b||"width"in b)&&(c.overflow=[p.overflow,p.overflowX,p.overflowY],j=m.css(a,"display"),l="none"===j?m._data(a,"olddisplay")||Fa(a.nodeName):j,"inline"===l&&"none"===m.css(a,"float")&&(k.inlineBlockNeedsLayout&&"inline"!==Fa(a.nodeName)?p.zoom=1:p.display="inline-block")),c.overflow&&(p.overflow="hidden",k.shrinkWrapBlocks()||n.always(function(){p.overflow=c.overflow[0],p.overflowX=c.overflow[1],p.overflowY=c.overflow[2]}));for(d in b)if(e=b[d],ab.exec(e)){if(delete b[d],f=f||"toggle"===e,e===(q?"hide":"show")){if("show"!==e||!r||void 0===r[d])continue;q=!0}o[d]=r&&r[d]||m.style(a,d)}else j=void 0;if(m.isEmptyObject(o))"inline"===("none"===j?Fa(a.nodeName):j)&&(p.display=j);else{r?"hidden"in r&&(q=r.hidden):r=m._data(a,"fxshow",{}),f&&(r.hidden=!q),q?m(a).show():n.done(function(){m(a).hide()}),n.done(function(){var b;m._removeData(a,"fxshow");for(b in o)m.style(a,b,o[b])});for(d in o)g=hb(q?r[d]:0,d,n),d in r||(r[d]=g.start,q&&(g.end=g.start,g.start="width"===d||"height"===d?1:0))}}function jb(a,b){var c,d,e,f,g;for(c in a)if(d=m.camelCase(c),e=b[d],f=a[c],m.isArray(f)&&(e=f[1],f=a[c]=f[0]),c!==d&&(a[d]=f,delete a[c]),g=m.cssHooks[d],g&&"expand"in g){f=g.expand(f),delete a[d];for(c in f)c in a||(a[c]=f[c],b[c]=e)}else b[d]=e}function kb(a,b,c){var d,e,f=0,g=db.length,h=m.Deferred().always(function(){delete i.elem}),i=function(){if(e)return!1;for(var b=$a||fb(),c=Math.max(0,j.startTime+j.duration-b),d=c/j.duration||0,f=1-d,g=0,i=j.tweens.length;i>g;g++)j.tweens[g].run(f);return h.notifyWith(a,[j,f,c]),1>f&&i?c:(h.resolveWith(a,[j]),!1)},j=h.promise({elem:a,props:m.extend({},b),opts:m.extend(!0,{specialEasing:{}},c),originalProperties:b,originalOptions:c,startTime:$a||fb(),duration:c.duration,tweens:[],createTween:function(b,c){var d=m.Tween(a,j.opts,b,c,j.opts.specialEasing[b]||j.opts.easing);return j.tweens.push(d),d},stop:function(b){var c=0,d=b?j.tweens.length:0;if(e)return this;for(e=!0;d>c;c++)j.tweens[c].run(1);return b?h.resolveWith(a,[j,b]):h.rejectWith(a,[j,b]),this}}),k=j.props;for(jb(k,j.opts.specialEasing);g>f;f++)if(d=db[f].call(j,a,k,j.opts))return d;return m.map(k,hb,j),m.isFunction(j.opts.start)&&j.opts.start.call(a,j),m.fx.timer(m.extend(i,{elem:a,anim:j,queue:j.opts.queue})),j.progress(j.opts.progress).done(j.opts.done,j.opts.complete).fail(j.opts.fail).always(j.opts.always)}m.Animation=m.extend(kb,{tweener:function(a,b){m.isFunction(a)?(b=a,a=["*"]):a=a.split(" ");for(var c,d=0,e=a.length;e>d;d++)c=a[d],eb[cLine truncated
|
|
||||||
|
Before Width: | Height: | Size: 5.7 KiB |
@@ -1,12 +0,0 @@
|
|||||||
$(document).ready(function() {
|
|
||||||
// Start 1-second timer to call RESTful endpoint
|
|
||||||
setInterval(function() {
|
|
||||||
$.ajax({
|
|
||||||
url: '/get_cpu_usage',
|
|
||||||
dataType: 'json',
|
|
||||||
success: function(json) {
|
|
||||||
$('#cpu_usage').text(json.result + '% ');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 1000);
|
|
||||||
});
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
* {
|
|
||||||
outline: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
background: #36c;
|
|
||||||
color: #555;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
font: 16px/1.4 Helvetica, Arial, sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
div.content {
|
|
||||||
width: 800px;
|
|
||||||
margin: 1em auto;
|
|
||||||
padding: 2em;
|
|
||||||
background-color: #fff;
|
|
||||||
border-radius: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset {
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
padding: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset legend {
|
|
||||||
font-style: italic;
|
|
||||||
color: silver;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset label {
|
|
||||||
text-align: right;
|
|
||||||
padding: 0 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type=text] {
|
|
||||||
font-size: 100%;
|
|
||||||
padding: 0.2em;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
color: #666;
|
|
||||||
background: #eee;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
padding: 0.2em 2em;
|
|
||||||
font-size: 100%;
|
|
||||||
cursor: pointer;
|
|
||||||
margin-top: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
button:hover {
|
|
||||||
background: #ccc;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
background: #f0f0f0;
|
|
||||||
padding: 0 0.3em;
|
|
||||||
color: #396;
|
|
||||||
border-radius: 0.3em;
|
|
||||||
}
|
|
||||||
|
|
||||||
a:link, a:visited {
|
|
||||||
color: #69c;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
PROG = server
|
|
||||||
include ../examples.mk
|
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
// Copyright (c) 2015 Cesanta Software Limited
|
|
||||||
// All rights reserved
|
|
||||||
|
|
||||||
#include "mongoose.h"
|
|
||||||
|
|
||||||
struct device_settings {
|
|
||||||
char setting1[100];
|
|
||||||
char setting2[100];
|
|
||||||
};
|
|
||||||
|
|
||||||
static const char *s_http_port = "8000";
|
|
||||||
static struct mg_serve_http_opts s_http_server_opts;
|
|
||||||
static struct device_settings s_settings = { "value1", "value2" };
|
|
||||||
|
|
||||||
static void handle_save(struct mg_connection *nc, struct http_message *hm) {
|
|
||||||
// Get form variables and store settings values
|
|
||||||
mg_get_http_var(&hm->body, "setting1", s_settings.setting1,
|
|
||||||
sizeof(s_settings.setting1));
|
|
||||||
mg_get_http_var(&hm->body, "setting2", s_settings.setting2,
|
|
||||||
sizeof(s_settings.setting2));
|
|
||||||
|
|
||||||
// Send response
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 302 OK\r\nLocation: /\r\n\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_get_cpu_usage(struct mg_connection *nc) {
|
|
||||||
// Generate random value, as an example of changing CPU usage
|
|
||||||
// Getting real CPU usage depends on the OS.
|
|
||||||
int cpu_usage = (double) rand() / RAND_MAX * 100.0;
|
|
||||||
|
|
||||||
// Use chunked encoding in order to avoid calculating Content-Length
|
|
||||||
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
|
||||||
|
|
||||||
// Output JSON object which holds CPU usage data
|
|
||||||
mg_printf_http_chunk(nc, "{ \"result\": %d }", cpu_usage);
|
|
||||||
|
|
||||||
// Send empty chunk, the end of response
|
|
||||||
mg_send_http_chunk(nc, "", 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handle_ssi_call(struct mg_connection *nc, const char *param) {
|
|
||||||
if (strcmp(param, "setting1") == 0) {
|
|
||||||
mg_printf_html_escape(nc, "%s", s_settings.setting1);
|
|
||||||
} else if (strcmp(param, "setting2") == 0) {
|
|
||||||
mg_printf_html_escape(nc, "%s", s_settings.setting2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
|
||||||
struct http_message *hm = (struct http_message *) ev_data;
|
|
||||||
|
|
||||||
switch (ev) {
|
|
||||||
case MG_EV_HTTP_REQUEST:
|
|
||||||
if (mg_vcmp(&hm->uri, "/save") == 0) {
|
|
||||||
handle_save(nc, hm);
|
|
||||||
} else if (mg_vcmp(&hm->uri, "/get_cpu_usage") == 0) {
|
|
||||||
handle_get_cpu_usage(nc);
|
|
||||||
} else {
|
|
||||||
mg_serve_http(nc, hm, s_http_server_opts); // Serve static content
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MG_EV_SSI_CALL:
|
|
||||||
handle_ssi_call(nc, ev_data);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void push_data_to_all_websocket_connections(struct mg_mgr *m) {
|
|
||||||
struct mg_connection *c;
|
|
||||||
int memory_usage = (double) rand() / RAND_MAX * 100.0;
|
|
||||||
|
|
||||||
for (c = mg_next(m, NULL); c != NULL; c = mg_next(m, c)) {
|
|
||||||
if (c->flags & MG_F_IS_WEBSOCKET) {
|
|
||||||
mg_printf_websocket_frame(c, WEBSOCKET_OP_TEXT, "%d", memory_usage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
struct mg_connection *nc;
|
|
||||||
cs_stat_t st;
|
|
||||||
|
|
||||||
mg_mgr_init(&mgr, NULL);
|
|
||||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
|
||||||
if (nc == NULL) {
|
|
||||||
fprintf(stderr, "Cannot bind to %s\n", s_http_port);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up HTTP server parameters
|
|
||||||
mg_set_protocol_http_websocket(nc);
|
|
||||||
s_http_server_opts.document_root = "web_root"; // Set up web root directory
|
|
||||||
|
|
||||||
if (mg_stat(s_http_server_opts.document_root, &st) != 0) {
|
|
||||||
fprintf(stderr, "%s", "Cannot find web_root directory, exiting\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("Starting web server on port %s\n", s_http_port);
|
|
||||||
for (;;) {
|
|
||||||
static time_t last_time;
|
|
||||||
time_t now = time(NULL);
|
|
||||||
mg_mgr_poll(&mgr, 1000);
|
|
||||||
if (now - last_time > 0) {
|
|
||||||
push_data_to_all_websocket_connections(&mgr);
|
|
||||||
last_time = now;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mg_mgr_free(&mgr);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>Connected device demo</title>
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<link rel="stylesheet" type="text/css" href="style.css">
|
|
||||||
<script type="text/javascript" src="jquery-1.11.3.min.js"></script>
|
|
||||||
<script src="jquery.flot.min.js"></script>
|
|
||||||
<script src="jquery.flot.time.js"></script>
|
|
||||||
<script type="text/javascript" src="main.js"></script>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div class="content">
|
|
||||||
<img src="logo.png" style="float:right; height: 50px; border-radius: 3px;">
|
|
||||||
<h1>Connected device demo - Websocket push</h1>
|
|
||||||
|
|
||||||
<p>
|
|
||||||
This page is one of the series of examples that show how to make
|
|
||||||
devices connected with Mongoose. Here we add a real-time Websocket
|
|
||||||
server push, and update dashboard graph as data arrives.
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<h1>Settings section</h1>
|
|
||||||
<form method="POST" action="/save">
|
|
||||||
<fieldset>
|
|
||||||
<legend>Device settings</legend>
|
|
||||||
<label>Setting 1:</label> <input type="text"
|
|
||||||
name="setting1" value="<!--#call setting1 -->" >
|
|
||||||
<label>Setting 2:</label> <input type="text"
|
|
||||||
name="setting2" value="<!--#call setting2 -->" >
|
|
||||||
|
|
||||||
<div>
|
|
||||||
<button type="submit">Save Settings</button>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
</fieldset>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<h1>Dashboard section</h1>
|
|
||||||
<label>CPU usage:</label> <span id="cpu_usage"> </span>
|
|
||||||
<div id="graphs"></div>
|
|
||||||
|
|
||||||
</div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
/*! jQuery v1.11.3 | (c) 2005, 2015 jQuery Foundation, Inc. | jquery.org/license */
|
|
||||||
!function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){var c=[],d=c.slice,e=c.concat,f=c.push,g=c.indexOf,h={},i=h.toString,j=h.hasOwnProperty,k={},l="1.11.3",m=function(a,b){return new m.fn.init(a,b)},n=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,o=/^-ms-/,p=/-([\da-z])/gi,q=function(a,b){return b.toUpperCase()};m.fn=m.prototype={jquery:l,constructor:m,selector:"",length:0,toArray:function(){return d.call(this)},get:function(a){return null!=a?0>a?this[a+this.length]:this[a]:d.call(this)},pushStack:function(a){var b=m.merge(this.constructor(),a);return b.prevObject=this,b.context=this.context,b},each:function(a,b){return m.each(this,a,b)},map:function(a){return this.pushStack(m.map(this,function(b,c){return a.call(b,c,b)}))},slice:function(){return this.pushStack(d.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(a){var b=this.length,c=+a+(0>a?b:0);return this.pushStack(c>=0&&b>c?[this[c]]:[])},end:function(){return this.prevObject||this.constructor(null)},push:f,sort:c.sort,splice:c.splice},m.extend=m.fn.extend=function(){var a,b,c,d,e,f,g=arguments[0]||{},h=1,i=arguments.length,j=!1;for("boolean"==typeof g&&(j=g,g=arguments[h]||{},h++),"object"==typeof g||m.isFunction(g)||(g={}),h===i&&(g=this,h--);i>h;h++)if(null!=(e=arguments[h]))for(d in e)a=g[d],c=e[d],g!==c&&(j&&c&&(m.isPlainObject(c)||(b=m.isArray(c)))?(b?(b=!1,f=a&&m.isArray(a)?a:[]):f=a&&m.isPlainObject(a)?a:{},g[d]=m.extend(j,f,c)):void 0!==c&&(g[d]=c));return g},m.extend({expando:"jQuery"+(l+Math.random()).replace(/\D/g,""),isReady:!0,error:function(a){throw new Error(a)},noop:function(){},isFunction:function(a){return"function"===m.type(a)},isArray:Array.isArray||function(a){return"array"===m.type(a)},isWindow:function(a){return null!=a&&a==a.window},isNumeric:function(a){return!m.isArray(a)&&a-parseFloat(a)+1>=0},isEmptyObject:function(a){var b;for(b in a)return!1;return!0},isPlainObject:function(a){var b;if(!a||"object"!==m.type(a)||a.nodeType||m.isWindow(a))return!1;try{if(a.constructor&&!j.call(a,"constructor")&&!j.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}if(k.ownLast)for(b in a)return j.call(a,b);for(b in a);return void 0===b||j.call(a,b)},type:function(a){return null==a?a+"":"object"==typeof a||"function"==typeof a?h[i.call(a)]||"object":typeof a},globalEval:function(b){b&&m.trim(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(o,"ms-").replace(p,q)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toLowerCase()===b.toLowerCase()},each:function(a,b,c){var d,e=0,f=a.length,g=r(a);if(c){if(g){for(;f>e;e++)if(d=b.apply(a[e],c),d===!1)break}else for(e in a)if(d=b.apply(a[e],c),d===!1)break}else if(g){for(;f>e;e++)if(d=b.call(a[e],e,a[e]),d===!1)break}else for(e in a)if(d=b.call(a[e],e,a[e]),d===!1)break;return a},trim:function(a){return null==a?"":(a+"").replace(n,"")},makeArray:function(a,b){var c=b||[];return null!=a&&(r(Object(a))?m.merge(c,"string"==typeof a?[a]:a):f.call(c,a)),c},inArray:function(a,b,c){var d;if(b){if(g)return g.call(b,a,c);for(d=b.length,c=c?0>c?Math.max(0,d+c):c:0;d>c;c++)if(c in b&&b[c]===a)return c}return-1},merge:function(a,b){var c=+b.length,d=0,e=a.length;while(c>d)a[e++]=b[d++];if(c!==c)while(void 0!==b[d])a[e++]=b[d++];return a.length=e,a},grep:function(a,b,c){for(var d,e=[],f=0,g=a.length,h=!c;g>f;f++)d=!b(a[f],f),d!==h&&e.push(a[f]);return e},map:function(a,b,c){var d,f=0,g=a.length,h=r(a),i=[];if(h)for(;g>f;f++)d=b(a[f],f,c),null!=d&&i.push(d);else for(f in a)d=b(a[f],f,c),null!=d&&i.push(d);return e.apply([],i)},guid:1,proxy:function(a,b){var c,e,f;return"string"==typeof b&&(f=a[b],b=a,a=f),m.isFunction(a)?(c=d.call(arguments,2),e=function(){return a.apply(b||this,c.concat(d.call(arguments)))},e.guid=a.guid=a.guid||m.guid++,e):void 0},now:function(){return+new Date},support:k}),m.each("Boolean Number String Function Array Date RegExp Object Error".split(" "),function(a,b){h["[object "+b+"]"]=b.toLowerCase()});function r(a){var b="length"in a&&a.length,c=m.type(a);return"function"===c||m.isWindow(a)?!1:1===a.nodeType&&b?!0:"array"===c||0===b||"number"==typeof b&&b>0&&b-1 in a}var s=function(a){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u="sizzle"+1*new Date,v=a.document,w=0,x=0,y=ha(),z=ha(),A=ha(),B=function(a,b){return a===b&&(l=!0),0},C=1<<31,D={}.hasOwnProperty,E=[],F=E.pop,G=E.push,H=E.push,I=E.slice,J=function(a,b){for(var c=0,d=a.length;d>c;c++)if(a[c]===b)return c;return-1},K="checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",L="[\\x20\\t\\r\\n\\f]",M="(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+",N=M.replace("w","w#"),O="\\["+L+"*("+M+")(?:"+L+"*([*^$|!~]?=)"+L+"*(?:'((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^Line truncated
|
|
||||||
|
|
||||||
return!0}function Q(a,b,d,e){if(m.acceptData(a)){var f,g,h=m.expando,i=a.nodeType,j=i?m.cache:a,k=i?a[h]:a[h]&&h;if(k&&j[k]&&(e||j[k].data)||void 0!==d||"string"!=typeof b)return k||(k=i?a[h]=c.pop()||m.guid++:h),j[k]||(j[k]=i?{}:{toJSON:m.noop}),("object"==typeof b||"function"==typeof b)&&(e?j[k]=m.extend(j[k],b):j[k].data=m.extend(j[k].data,b)),g=j[k],e||(g.data||(g.data={}),g=g.data),void 0!==d&&(g[m.camelCase(b)]=d),"string"==typeof b?(f=g[b],null==f&&(f=g[m.camelCase(b)])):f=g,f}}function R(a,b,c){if(m.acceptData(a)){var d,e,f=a.nodeType,g=f?m.cache:a,h=f?a[m.expando]:m.expando;if(g[h]){if(b&&(d=c?g[h]:g[h].data)){m.isArray(b)?b=b.concat(m.map(b,m.camelCase)):b in d?b=[b]:(b=m.camelCase(b),b=b in d?[b]:b.split(" ")),e=b.length;while(e--)delete d[b[e]];if(c?!P(d):!m.isEmptyObject(d))return}(c||(delete g[h].data,P(g[h])))&&(f?m.cleanData([a],!0):k.deleteExpando||g!=g.window?delete g[h]:g[h]=null)}}}m.extend({cache:{},noData:{"applet ":!0,"embed ":!0,"object ":"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"},hasData:function(a){return a=a.nodeType?m.cache[a[m.expando]]:a[m.expando],!!a&&!P(a)},data:function(a,b,c){return Q(a,b,c)},removeData:function(a,b){return R(a,b)},_data:function(a,b,c){return Q(a,b,c,!0)},_removeData:function(a,b){return R(a,b,!0)}}),m.fn.extend({data:function(a,b){var c,d,e,f=this[0],g=f&&f.attributes;if(void 0===a){if(this.length&&(e=m.data(f),1===f.nodeType&&!m._data(f,"parsedAttrs"))){c=g.length;while(c--)g[c]&&(d=g[c].name,0===d.indexOf("data-")&&(d=m.camelCase(d.slice(5)),O(f,d,e[d])));m._data(f,"parsedAttrs",!0)}return e}return"object"==typeof a?this.each(function(){m.data(this,a)}):arguments.length>1?this.each(function(){m.data(this,a,b)}):f?O(f,a,m.data(f,a)):void 0},removeData:function(a){return this.each(function(){m.removeData(this,a)})}}),m.extend({queue:function(a,b,c){var d;return a?(b=(b||"fx")+"queue",d=m._data(a,b),c&&(!d||m.isArray(c)?d=m._data(a,b,m.makeArray(c)):d.push(c)),d||[]):void 0},dequeue:function(a,b){b=b||"fx";var c=m.queue(a,b),d=c.length,e=c.shift(),f=m._queueHooks(a,b),g=function(){m.dequeue(a,b)};"inprogress"===e&&(e=c.shift(),d--),e&&("fx"===b&&c.unshift("inprogress"),delete f.stop,e.call(a,g,f)),!d&&f&&f.empty.fire()},_queueHooks:function(a,b){var c=b+"queueHooks";return m._data(a,c)||m._data(a,c,{empty:m.Callbacks("once memory").add(function(){m._removeData(a,b+"queue"),m._removeData(a,c)})})}}),m.fn.extend({queue:function(a,b){var c=2;return"string"!=typeof a&&(b=a,a="fx",c--),arguments.length<c?m.queue(this[0],a):void 0===b?this:this.each(function(){var c=m.queue(this,a,b);m._queueHooks(this,a),"fx"===a&&"inprogress"!==c[0]&&m.dequeue(this,a)})},dequeue:function(a){return this.each(function(){m.dequeue(this,a)})},clearQueue:function(a){return this.queue(a||"fx",[])},promise:function(a,b){var c,d=1,e=m.Deferred(),f=this,g=this.length,h=function(){--d||e.resolveWith(f,[f])};"string"!=typeof a&&(b=a,a=void 0),a=a||"fx";while(g--)c=m._data(f[g],a+"queueHooks"),c&&c.empty&&(d++,c.empty.add(h));return h(),e.promise(b)}});var S=/[+-]?(?:\d*\.|)\d+(?:[eE][+-]?\d+|)/.source,T=["Top","Right","Bottom","Left"],U=function(a,b){return a=b||a,"none"===m.css(a,"display")||!m.contains(a.ownerDocument,a)},V=m.access=function(a,b,c,d,e,f,g){var h=0,i=a.length,j=null==c;if("object"===m.type(c)){e=!0;for(h in c)m.access(a,b,h,c[h],!0,f,g)}else if(void 0!==d&&(e=!0,m.isFunction(d)||(g=!0),j&&(g?(b.call(a,d),b=null):(j=b,b=function(a,b,c){return j.call(m(a),c)})),b))for(;i>h;h++)b(a[h],c,g?d:d.call(a[h],h,b(a[h],c)));return e?a:j?b.call(a):i?b(a[0],c):f},W=/^(?:checkbox|radio)$/i;!function(){var a=y.createElement("input"),b=y.createElement("div"),c=y.createDocumentFragment();if(b.innerHTML=" <link/><table></table><a href='/a'>a</a><input type='checkbox'/>",k.leadingWhitespace=3===b.firstChild.nodeType,k.tbody=!b.getElementsByTagName("tbody").length,k.htmlSerialize=!!b.getElementsByTagName("link").length,k.html5Clone="<:nav></:nav>"!==y.createElement("nav").cloneNode(!0).outerHTML,a.type="checkbox",a.checked=!0,c.appendChild(a),k.appendChecked=a.checked,b.innerHTML="<textarea>x</textarea>",k.noCloneChecked=!!b.cloneNode(!0).lastChild.defaultValue,c.appendChild(b),b.innerHTML="<input type='radio' checked='checked' name='t'/>",k.checkClone=b.cloneNode(!0).cloneNode(!0).lastChild.checked,k.noCloneEvent=!0,b.attachEvent&&(b.attachEvent("onclick",function(){k.noCloneEvent=!1}),b.cloneNode(!0).click()),null==k.deleteExpando){k.deleteExpando=!0;try{delete b.test}catch(d){k.deleteExpando=!1}}}(),function(){var b,c,d=y.createElement("div");for(b in{submit:!0,change:!0,focusin:!0})c="on"+b,(k[b+"Bubbles"]=c in a)||(d.setAttribute(c,"t"),k[b+"Bubbles"]=d.attributes[c].expando===!1);d=null}();var X=/^(?:input|select|textarea)$/i,Y=/^key/,Z=/^(?:mouse|pointer|contextmenu)|click/,$=/^(?:focusinfocus|focusoutblur)$/,_=/^([^.]*)(?:\.(.+)|)$/;function aa(){return!0}function ba(){return!1}function ca(){try{return y.activeElement}catch(a){}}m.event={global:{},add:function(a,b,c,d,e){var
Line truncated
|
|
||||||
return new Za.prototype.init(a,b,c,d,e)}m.Tween=Za,Za.prototype={constructor:Za,init:function(a,b,c,d,e,f){this.elem=a,this.prop=c,this.easing=e||"swing",this.options=b,this.start=this.now=this.cur(),this.end=d,this.unit=f||(m.cssNumber[c]?"":"px")},cur:function(){var a=Za.propHooks[this.prop];return a&&a.get?a.get(this):Za.propHooks._default.get(this)},run:function(a){var b,c=Za.propHooks[this.prop];return this.options.duration?this.pos=b=m.easing[this.easing](a,this.options.duration*a,0,1,this.options.duration):this.pos=b=a,this.now=(this.end-this.start)*b+this.start,this.options.step&&this.options.step.call(this.elem,this.now,this),c&&c.set?c.set(this):Za.propHooks._default.set(this),this}},Za.prototype.init.prototype=Za.prototype,Za.propHooks={_default:{get:function(a){var b;return null==a.elem[a.prop]||a.elem.style&&null!=a.elem.style[a.prop]?(b=m.css(a.elem,a.prop,""),b&&"auto"!==b?b:0):a.elem[a.prop]},set:function(a){m.fx.step[a.prop]?m.fx.step[a.prop](a):a.elem.style&&(null!=a.elem.style[m.cssProps[a.prop]]||m.cssHooks[a.prop])?m.style(a.elem,a.prop,a.now+a.unit):a.elem[a.prop]=a.now}}},Za.propHooks.scrollTop=Za.propHooks.scrollLeft={set:function(a){a.elem.nodeType&&a.elem.parentNode&&(a.elem[a.prop]=a.now)}},m.easing={linear:function(a){return a},swing:function(a){return.5-Math.cos(a*Math.PI)/2}},m.fx=Za.prototype.init,m.fx.step={};var $a,_a,ab=/^(?:toggle|show|hide)$/,bb=new RegExp("^(?:([+-])=|)("+S+")([a-z%]*)$","i"),cb=/queueHooks$/,db=[ib],eb={"*":[function(a,b){var c=this.createTween(a,b),d=c.cur(),e=bb.exec(b),f=e&&e[3]||(m.cssNumber[a]?"":"px"),g=(m.cssNumber[a]||"px"!==f&&+d)&&bb.exec(m.css(c.elem,a)),h=1,i=20;if(g&&g[3]!==f){f=f||g[3],e=e||[],g=+d||1;do h=h||".5",g/=h,m.style(c.elem,a,g+f);while(h!==(h=c.cur()/d)&&1!==h&&--i)}return e&&(g=c.start=+g||+d||0,c.unit=f,c.end=e[1]?g+(e[1]+1)*e[2]:+e[2]),c}]};function fb(){return setTimeout(function(){$a=void 0}),$a=m.now()}function gb(a,b){var c,d={height:a},e=0;for(b=b?1:0;4>e;e+=2-b)c=T[e],d["margin"+c]=d["padding"+c]=a;return b&&(d.opacity=d.width=a),d}function hb(a,b,c){for(var d,e=(eb[b]||[]).concat(eb["*"]),f=0,g=e.length;g>f;f++)if(d=e[f].call(c,b,a))return d}function ib(a,b,c){var d,e,f,g,h,i,j,l,n=this,o={},p=a.style,q=a.nodeType&&U(a),r=m._data(a,"fxshow");c.queue||(h=m._queueHooks(a,"fx"),null==h.unqueued&&(h.unqueued=0,i=h.empty.fire,h.empty.fire=function(){h.unqueued||i()}),h.unqueued++,n.always(function(){n.always(function(){h.unqueued--,m.queue(a,"fx").length||h.empty.fire()})})),1===a.nodeType&&("height"in b||"width"in b)&&(c.overflow=[p.overflow,p.overflowX,p.overflowY],j=m.css(a,"display"),l="none"===j?m._data(a,"olddisplay")||Fa(a.nodeName):j,"inline"===l&&"none"===m.css(a,"float")&&(k.inlineBlockNeedsLayout&&"inline"!==Fa(a.nodeName)?p.zoom=1:p.display="inline-block")),c.overflow&&(p.overflow="hidden",k.shrinkWrapBlocks()||n.always(function(){p.overflow=c.overflow[0],p.overflowX=c.overflow[1],p.overflowY=c.overflow[2]}));for(d in b)if(e=b[d],ab.exec(e)){if(delete b[d],f=f||"toggle"===e,e===(q?"hide":"show")){if("show"!==e||!r||void 0===r[d])continue;q=!0}o[d]=r&&r[d]||m.style(a,d)}else j=void 0;if(m.isEmptyObject(o))"inline"===("none"===j?Fa(a.nodeName):j)&&(p.display=j);else{r?"hidden"in r&&(q=r.hidden):r=m._data(a,"fxshow",{}),f&&(r.hidden=!q),q?m(a).show():n.done(function(){m(a).hide()}),n.done(function(){var b;m._removeData(a,"fxshow");for(b in o)m.style(a,b,o[b])});for(d in o)g=hb(q?r[d]:0,d,n),d in r||(r[d]=g.start,q&&(g.end=g.start,g.start="width"===d||"height"===d?1:0))}}function jb(a,b){var c,d,e,f,g;for(c in a)if(d=m.camelCase(c),e=b[d],f=a[c],m.isArray(f)&&(e=f[1],f=a[c]=f[0]),c!==d&&(a[d]=f,delete a[c]),g=m.cssHooks[d],g&&"expand"in g){f=g.expand(f),delete a[d];for(c in f)c in a||(a[c]=f[c],b[c]=e)}else b[d]=e}function kb(a,b,c){var d,e,f=0,g=db.length,h=m.Deferred().always(function(){delete i.elem}),i=function(){if(e)return!1;for(var b=$a||fb(),c=Math.max(0,j.startTime+j.duration-b),d=c/j.duration||0,f=1-d,g=0,i=j.tweens.length;i>g;g++)j.tweens[g].run(f);return h.notifyWith(a,[j,f,c]),1>f&&i?c:(h.resolveWith(a,[j]),!1)},j=h.promise({elem:a,props:m.extend({},b),opts:m.extend(!0,{specialEasing:{}},c),originalProperties:b,originalOptions:c,startTime:$a||fb(),duration:c.duration,tweens:[],createTween:function(b,c){var d=m.Tween(a,j.opts,b,c,j.opts.specialEasing[b]||j.opts.easing);return j.tweens.push(d),d},stop:function(b){var c=0,d=b?j.tweens.length:0;if(e)return this;for(e=!0;d>c;c++)j.tweens[c].run(1);return b?h.resolveWith(a,[j,b]):h.rejectWith(a,[j,b]),this}}),k=j.props;for(jb(k,j.opts.specialEasing);g>f;f++)if(d=db[f].call(j,a,k,j.opts))return d;return m.map(k,hb,j),m.isFunction(j.opts.start)&&j.opts.start.call(a,j),m.fx.timer(m.extend(i,{elem:a,anim:j,queue:j.opts.queue})),j.progress(j.opts.progress).done(j.opts.done,j.opts.complete).fail(j.opts.fail).always(j.opts.always)}m.Animation=m.extend(kb,{tweener:function(a,b){m.isFunction(a)?(b=a,a=["*"]):a=a.split(" ");for(var c,d=0,e=a.length;e>d;d++)c=a[d],eb[cLine truncated
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
/* Javascript plotting library for jQuery, version 0.8.3.
|
|
||||||
|
|
||||||
Copyright (c) 2007-2014 IOLA and Ole Laursen.
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
*/
|
|
||||||
(function($){$.color={};$.color.make=function(r,g,b,a){var o={};o.r=r||0;o.g=g||0;o.b=b||0;o.a=a!=null?a:1;o.add=function(c,d){for(var i=0;i<c.length;++i)o[c.charAt(i)]+=d;return o.normalize()};o.scale=function(c,f){for(var i=0;i<c.length;++i)o[c.charAt(i)]*=f;return o.normalize()};o.toString=function(){if(o.a>=1){return"rgb("+[o.r,o.g,o.b].join(",")+")"}else{return"rgba("+[o.r,o.g,o.b,o.a].join(",")+")"}};o.normalize=function(){function clamp(min,value,max){return value<min?min:value>max?max:value}o.r=clamp(0,parseInt(o.r),255);o.g=clamp(0,parseInt(o.g),255);o.b=clamp(0,parseInt(o.b),255);o.a=clamp(0,o.a,1);return o};o.clone=function(){return $.color.make(o.r,o.b,o.g,o.a)};return o.normalize()};$.color.extract=function(elem,css){var c;do{c=elem.css(css).toLowerCase();if(c!=""&&c!="transparent")break;elem=elem.parent()}while(elem.length&&!$.nodeName(elem.get(0),"body"));if(c=="rgba(0, 0, 0, 0)")c="transparent";return $.color.parse(c)};$.color.parse=function(str){var res,m=$.color.make;if(res=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(str))return m(parseInt(res[1],10),parseInt(res[2],10),parseInt(res[3],10));if(res=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))return m(parseInt(res[1],10),parseInt(res[2],10),parseInt(res[3],10),parseFloat(res[4]));if(res=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(str))return m(parseFloat(res[1])*2.55,parseFloat(res[2])*2.55,parseFloat(res[3])*2.55);if(res=/rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))return m(parseFloat(res[1])*2.55,parseFloat(res[2])*2.55,parseFloat(res[3])*2.55,parseFloat(res[4]));if(res=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(str))return m(parseInt(res[1],16),parseInt(res[2],16),parseInt(res[3],16));if(res=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(str))return m(parseInt(res[1]+res[1],16),parseInt(res[2]+res[2],16),parseInt(res[3]+res[3],16));var name=$.trim(str).toLowerCase();if(name=="transparent")return m(255,255,255,0);else{res=lookupColors[name]||[0,0,0];return m(res[0],res[1],res[2])}};var lookupColors={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);(function($){var hasOwnProperty=Object.prototype.hasOwnProperty;if(!$.fn.detach){$.fn.detach=function(){return this.each(function(){if(this.parentNode){this.parentNode.removeChild(this)}})}}function Canvas(cls,container){var element=container.children("."+cls)[0];if(element==null){element=document.createElement("canvas");element.className=cls;$(element).css({direction:"ltr",position:"absolute",left:0,top:0}).appendTo(container);if(!element.getContext){if(window.G_vmlCanvasManager){element=window.G_vmlCanvasManager.initElement(element)}else{throw new Error("Canvas is not available. If you're using IE with a fall-back such as Excanvas, then there's either a mistake in your conditional include, or the page has no DOCTYPE and is rendering in Quirks Mode.")}}}this.element=element;var context=this.context=element.getContext("2d");var devicePixelRatio=window.devicePixelRatio||1,backingStoreRatio=context.webkitBackingStorePixelRatio||context.mozBackingStorePixelRatio||context.msBackingStorePixelRatio||context.oBackingStorePixelRatio||context.backingStorePixelRatio||1;this.pixelRatio=devicePixelRatio/backingStoreRatio;this.resize(container.width(),container.height());this.textContainer=null;this.text={};this._textCache={}}Canvas.prototype.resize=function(width,height){if(width<=0||height<=0){throw new Error("Invalid dimensions for plot, width = "+width+", height = "+height)}var element=this.element,context=this.context,pixelRatio=this.pixelRatio;if(this.width!=width){element.width=width*pixelRatio;element.style.width=width+"px";this.width=width}if(this.height!=height){element.height=height*pixelRatio;element.style.height=height+"px";this.height=height}context.restore();context.save();context.scale(pixelRatio,pixelRatio)};Canvas.prototype.clear=function(){this.context.clearRect(0,0,this.width,this.height)};Canvas.prototype.render=function(){vaLine truncated
|
|
||||||
if(yrange.from==null)yrange.from=yrange.axis.min;if(yrange.to==null)yrange.to=yrange.axis.max;if(xrange.to<xrange.axis.min||xrange.from>xrange.axis.max||yrange.to<yrange.axis.min||yrange.from>yrange.axis.max)continue;xrange.from=Math.max(xrange.from,xrange.axis.min);xrange.to=Math.min(xrange.to,xrange.axis.max);yrange.from=Math.max(yrange.from,yrange.axis.min);yrange.to=Math.min(yrange.to,yrange.axis.max);var xequal=xrange.from===xrange.to,yequal=yrange.from===yrange.to;if(xequal&&yequal){continue}xrange.from=Math.floor(xrange.axis.p2c(xrange.from));xrange.to=Math.floor(xrange.axis.p2c(xrange.to));yrange.from=Math.floor(yrange.axis.p2c(yrange.from));yrange.to=Math.floor(yrange.axis.p2c(yrange.to));if(xequal||yequal){var lineWidth=m.lineWidth||options.grid.markingsLineWidth,subPixel=lineWidth%2?.5:0;ctx.beginPath();ctx.strokeStyle=m.color||options.grid.markingsColor;ctx.lineWidth=lineWidth;if(xequal){ctx.moveTo(xrange.to+subPixel,yrange.from);ctx.lineTo(xrange.to+subPixel,yrange.to)}else{ctx.moveTo(xrange.from,yrange.to+subPixel);ctx.lineTo(xrange.to,yrange.to+subPixel)}ctx.stroke()}else{ctx.fillStyle=m.color||options.grid.markingsColor;ctx.fillRect(xrange.from,yrange.to,xrange.to-xrange.from,yrange.from-yrange.to)}}}axes=allAxes();bw=options.grid.borderWidth;for(var j=0;j<axes.length;++j){var axis=axes[j],box=axis.box,t=axis.tickLength,x,y,xoff,yoff;if(!axis.show||axis.ticks.length==0)continue;ctx.lineWidth=1;if(axis.direction=="x"){x=0;if(t=="full")y=axis.position=="top"?0:plotHeight;else y=box.top-plotOffset.top+(axis.position=="top"?box.height:0)}else{y=0;if(t=="full")x=axis.position=="left"?0:plotWidth;else x=box.left-plotOffset.left+(axis.position=="left"?box.width:0)}if(!axis.innermost){ctx.strokeStyle=axis.options.color;ctx.beginPath();xoff=yoff=0;if(axis.direction=="x")xoff=plotWidth+1;else yoff=plotHeight+1;if(ctx.lineWidth==1){if(axis.direction=="x"){y=Math.floor(y)+.5}else{x=Math.floor(x)+.5}}ctx.moveTo(x,y);ctx.lineTo(x+xoff,y+yoff);ctx.stroke()}ctx.strokeStyle=axis.options.tickColor;ctx.beginPath();for(i=0;i<axis.ticks.length;++i){var v=axis.ticks[i].v;xoff=yoff=0;if(isNaN(v)||v<axis.min||v>axis.max||t=="full"&&(typeof bw=="object"&&bw[axis.position]>0||bw>0)&&(v==axis.min||v==axis.max))continue;if(axis.direction=="x"){x=axis.p2c(v);yoff=t=="full"?-plotHeight:t;if(axis.position=="top")yoff=-yoff}else{y=axis.p2c(v);xoff=t=="full"?-plotWidth:t;if(axis.position=="left")xoff=-xoff}if(ctx.lineWidth==1){if(axis.direction=="x")x=Math.floor(x)+.5;else y=Math.floor(y)+.5}ctx.moveTo(x,y);ctx.lineTo(x+xoff,y+yoff)}ctx.stroke()}if(bw){bc=options.grid.borderColor;if(typeof bw=="object"||typeof bc=="object"){if(typeof bw!=="object"){bw={top:bw,right:bw,bottom:bw,left:bw}}if(typeof bc!=="object"){bc={top:bc,right:bc,bottom:bc,left:bc}}if(bw.top>0){ctx.strokeStyle=bc.top;ctx.lineWidth=bw.top;ctx.beginPath();ctx.moveTo(0-bw.left,0-bw.top/2);ctx.lineTo(plotWidth,0-bw.top/2);ctx.stroke()}if(bw.right>0){ctx.strokeStyle=bc.right;ctx.lineWidth=bw.right;ctx.beginPath();ctx.moveTo(plotWidth+bw.right/2,0-bw.top);ctx.lineTo(plotWidth+bw.right/2,plotHeight);ctx.stroke()}if(bw.bottom>0){ctx.strokeStyle=bc.bottom;ctx.lineWidth=bw.bottom;ctx.beginPath();ctx.moveTo(plotWidth+bw.right,plotHeight+bw.bottom/2);ctx.lineTo(0,plotHeight+bw.bottom/2);ctx.stroke()}if(bw.left>0){ctx.strokeStyle=bc.left;ctx.lineWidth=bw.left;ctx.beginPath();ctx.moveTo(0-bw.left/2,plotHeight+bw.bottom);ctx.lineTo(0-bw.left/2,0);ctx.stroke()}}else{ctx.lineWidth=bw;ctx.strokeStyle=options.grid.borderColor;ctx.strokeRect(-bw/2,-bw/2,plotWidth+bw,plotHeight+bw)}}ctx.restore()}function drawAxisLabels(){$.each(allAxes(),function(_,axis){var box=axis.box,legacyStyles=axis.direction+"Axis "+axis.direction+axis.n+"Axis",layer="flot-"+axis.direction+"-axis flot-"+axis.direction+axis.n+"-axis "+legacyStyles,font=axis.options.font||"flot-tick-label tickLabel",tick,x,y,halign,valign;surface.removeText(layer);if(!axis.show||axis.ticks.length==0)return;for(var i=0;i<axis.ticks.length;++i){tick=axis.ticks[i];if(!tick.label||tick.v<axis.min||tick.v>axis.max)continue;if(axis.direction=="x"){halign="center";x=plotOffset.left+axis.p2c(tick.v);if(axis.position=="bottom"){y=box.top+box.padding}else{y=box.top+box.height-box.padding;valign="bottom"}}else{valign="middle";y=plotOffset.top+axis.p2c(tick.v);if(axis.position=="left"){x=box.left+box.width-box.padding;halign="right"}else{x=box.left+box.padding}}surface.addText(layer,x,y,tick.label,font,null,null,halign,valign)}})}function drawSeries(series){if(series.lines.show)drawSeriesLines(series);if(series.bars.show)drawSeriesBars(series);if(series.points.show)drawSeriesPoints(series)}function drawSeriesLines(series){function plotLine(datapoints,xoffset,yoffset,axisx,axisy){var points=datapoints.points,ps=datapoints.pointsize,prevx=null,prevy=null;ctx.beginPath();for(var i=ps;i<points.length;i+=ps){var x1=points[i-ps],y1=points[i-ps+1],x2=points[i],y2=points[i+1];if(x1==null||x2==null)continue;if(y1<=y2&&y1<axisy.min){if(y2<axLine truncated
|
|
||||||
@@ -1,432 +0,0 @@
|
|||||||
/* Pretty handling of time axes.
|
|
||||||
|
|
||||||
Copyright (c) 2007-2014 IOLA and Ole Laursen.
|
|
||||||
Licensed under the MIT license.
|
|
||||||
|
|
||||||
Set axis.mode to "time" to enable. See the section "Time series data" in
|
|
||||||
API.txt for details.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
(function($) {
|
|
||||||
|
|
||||||
var options = {
|
|
||||||
xaxis: {
|
|
||||||
timezone: null, // "browser" for local to the client or timezone for timezone-js
|
|
||||||
timeformat: null, // format string to use
|
|
||||||
twelveHourClock: false, // 12 or 24 time in time mode
|
|
||||||
monthNames: null // list of names of months
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// round to nearby lower multiple of base
|
|
||||||
|
|
||||||
function floorInBase(n, base) {
|
|
||||||
return base * Math.floor(n / base);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns a string with the date d formatted according to fmt.
|
|
||||||
// A subset of the Open Group's strftime format is supported.
|
|
||||||
|
|
||||||
function formatDate(d, fmt, monthNames, dayNames) {
|
|
||||||
|
|
||||||
if (typeof d.strftime == "function") {
|
|
||||||
return d.strftime(fmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
var leftPad = function(n, pad) {
|
|
||||||
n = "" + n;
|
|
||||||
pad = "" + (pad == null ? "0" : pad);
|
|
||||||
return n.length == 1 ? pad + n : n;
|
|
||||||
};
|
|
||||||
|
|
||||||
var r = [];
|
|
||||||
var escape = false;
|
|
||||||
var hours = d.getHours();
|
|
||||||
var isAM = hours < 12;
|
|
||||||
|
|
||||||
if (monthNames == null) {
|
|
||||||
monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dayNames == null) {
|
|
||||||
dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
||||||
}
|
|
||||||
|
|
||||||
var hours12;
|
|
||||||
|
|
||||||
if (hours > 12) {
|
|
||||||
hours12 = hours - 12;
|
|
||||||
} else if (hours == 0) {
|
|
||||||
hours12 = 12;
|
|
||||||
} else {
|
|
||||||
hours12 = hours;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < fmt.length; ++i) {
|
|
||||||
|
|
||||||
var c = fmt.charAt(i);
|
|
||||||
|
|
||||||
if (escape) {
|
|
||||||
switch (c) {
|
|
||||||
case 'a': c = "" + dayNames[d.getDay()]; break;
|
|
||||||
case 'b': c = "" + monthNames[d.getMonth()]; break;
|
|
||||||
case 'd': c = leftPad(d.getDate()); break;
|
|
||||||
case 'e': c = leftPad(d.getDate(), " "); break;
|
|
||||||
case 'h': // For back-compat with 0.7; remove in 1.0
|
|
||||||
case 'H': c = leftPad(hours); break;
|
|
||||||
case 'I': c = leftPad(hours12); break;
|
|
||||||
case 'l': c = leftPad(hours12, " "); break;
|
|
||||||
case 'm': c = leftPad(d.getMonth() + 1); break;
|
|
||||||
case 'M': c = leftPad(d.getMinutes()); break;
|
|
||||||
// quarters not in Open Group's strftime specification
|
|
||||||
case 'q':
|
|
||||||
c = "" + (Math.floor(d.getMonth() / 3) + 1); break;
|
|
||||||
case 'S': c = leftPad(d.getSeconds()); break;
|
|
||||||
case 'y': c = leftPad(d.getFullYear() % 100); break;
|
|
||||||
case 'Y': c = "" + d.getFullYear(); break;
|
|
||||||
case 'p': c = (isAM) ? ("" + "am") : ("" + "pm"); break;
|
|
||||||
case 'P': c = (isAM) ? ("" + "AM") : ("" + "PM"); break;
|
|
||||||
case 'w': c = "" + d.getDay(); break;
|
|
||||||
}
|
|
||||||
r.push(c);
|
|
||||||
escape = false;
|
|
||||||
} else {
|
|
||||||
if (c == "%") {
|
|
||||||
escape = true;
|
|
||||||
} else {
|
|
||||||
r.push(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return r.join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
// To have a consistent view of time-based data independent of which time
|
|
||||||
// zone the client happens to be in we need a date-like object independent
|
|
||||||
// of time zones. This is done through a wrapper that only calls the UTC
|
|
||||||
// versions of the accessor methods.
|
|
||||||
|
|
||||||
function makeUtcWrapper(d) {
|
|
||||||
|
|
||||||
function addProxyMethod(sourceObj, sourceMethod, targetObj, targetMethod) {
|
|
||||||
sourceObj[sourceMethod] = function() {
|
|
||||||
return targetObj[targetMethod].apply(targetObj, arguments);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
var utc = {
|
|
||||||
date: d
|
|
||||||
};
|
|
||||||
|
|
||||||
// support strftime, if found
|
|
||||||
|
|
||||||
if (d.strftime != undefined) {
|
|
||||||
addProxyMethod(utc, "strftime", d, "strftime");
|
|
||||||
}
|
|
||||||
|
|
||||||
addProxyMethod(utc, "getTime", d, "getTime");
|
|
||||||
addProxyMethod(utc, "setTime", d, "setTime");
|
|
||||||
|
|
||||||
var props = ["Date", "Day", "FullYear", "Hours", "Milliseconds", "Minutes", "Month", "Seconds"];
|
|
||||||
|
|
||||||
for (var p = 0; p < props.length; p++) {
|
|
||||||
addProxyMethod(utc, "get" + props[p], d, "getUTC" + props[p]);
|
|
||||||
addProxyMethod(utc, "set" + props[p], d, "setUTC" + props[p]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return utc;
|
|
||||||
};
|
|
||||||
|
|
||||||
// select time zone strategy. This returns a date-like object tied to the
|
|
||||||
// desired timezone
|
|
||||||
|
|
||||||
function dateGenerator(ts, opts) {
|
|
||||||
if (opts.timezone == "browser") {
|
|
||||||
return new Date(ts);
|
|
||||||
} else if (!opts.timezone || opts.timezone == "utc") {
|
|
||||||
return makeUtcWrapper(new Date(ts));
|
|
||||||
} else if (typeof timezoneJS != "undefined" && typeof timezoneJS.Date != "undefined") {
|
|
||||||
var d = new timezoneJS.Date();
|
|
||||||
// timezone-js is fickle, so be sure to set the time zone before
|
|
||||||
// setting the time.
|
|
||||||
d.setTimezone(opts.timezone);
|
|
||||||
d.setTime(ts);
|
|
||||||
return d;
|
|
||||||
} else {
|
|
||||||
return makeUtcWrapper(new Date(ts));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// map of app. size of time units in milliseconds
|
|
||||||
|
|
||||||
var timeUnitSize = {
|
|
||||||
"second": 1000,
|
|
||||||
"minute": 60 * 1000,
|
|
||||||
"hour": 60 * 60 * 1000,
|
|
||||||
"day": 24 * 60 * 60 * 1000,
|
|
||||||
"month": 30 * 24 * 60 * 60 * 1000,
|
|
||||||
"quarter": 3 * 30 * 24 * 60 * 60 * 1000,
|
|
||||||
"year": 365.2425 * 24 * 60 * 60 * 1000
|
|
||||||
};
|
|
||||||
|
|
||||||
// the allowed tick sizes, after 1 year we use
|
|
||||||
// an integer algorithm
|
|
||||||
|
|
||||||
var baseSpec = [
|
|
||||||
[1, "second"], [2, "second"], [5, "second"], [10, "second"],
|
|
||||||
[30, "second"],
|
|
||||||
[1, "minute"], [2, "minute"], [5, "minute"], [10, "minute"],
|
|
||||||
[30, "minute"],
|
|
||||||
[1, "hour"], [2, "hour"], [4, "hour"],
|
|
||||||
[8, "hour"], [12, "hour"],
|
|
||||||
[1, "day"], [2, "day"], [3, "day"],
|
|
||||||
[0.25, "month"], [0.5, "month"], [1, "month"],
|
|
||||||
[2, "month"]
|
|
||||||
];
|
|
||||||
|
|
||||||
// we don't know which variant(s) we'll need yet, but generating both is
|
|
||||||
// cheap
|
|
||||||
|
|
||||||
var specMonths = baseSpec.concat([[3, "month"], [6, "month"],
|
|
||||||
[1, "year"]]);
|
|
||||||
var specQuarters = baseSpec.concat([[1, "quarter"], [2, "quarter"],
|
|
||||||
[1, "year"]]);
|
|
||||||
|
|
||||||
function init(plot) {
|
|
||||||
plot.hooks.processOptions.push(function (plot, options) {
|
|
||||||
$.each(plot.getAxes(), function(axisName, axis) {
|
|
||||||
|
|
||||||
var opts = axis.options;
|
|
||||||
|
|
||||||
if (opts.mode == "time") {
|
|
||||||
axis.tickGenerator = function(axis) {
|
|
||||||
|
|
||||||
var ticks = [];
|
|
||||||
var d = dateGenerator(axis.min, opts);
|
|
||||||
var minSize = 0;
|
|
||||||
|
|
||||||
// make quarter use a possibility if quarters are
|
|
||||||
// mentioned in either of these options
|
|
||||||
|
|
||||||
var spec = (opts.tickSize && opts.tickSize[1] ===
|
|
||||||
"quarter") ||
|
|
||||||
(opts.minTickSize && opts.minTickSize[1] ===
|
|
||||||
"quarter") ? specQuarters : specMonths;
|
|
||||||
|
|
||||||
if (opts.minTickSize != null) {
|
|
||||||
if (typeof opts.tickSize == "number") {
|
|
||||||
minSize = opts.tickSize;
|
|
||||||
} else {
|
|
||||||
minSize = opts.minTickSize[0] * timeUnitSize[opts.minTickSize[1]];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var i = 0; i < spec.length - 1; ++i) {
|
|
||||||
if (axis.delta < (spec[i][0] * timeUnitSize[spec[i][1]]
|
|
||||||
+ spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2
|
|
||||||
&& spec[i][0] * timeUnitSize[spec[i][1]] >= minSize) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var size = spec[i][0];
|
|
||||||
var unit = spec[i][1];
|
|
||||||
|
|
||||||
// special-case the possibility of several years
|
|
||||||
|
|
||||||
if (unit == "year") {
|
|
||||||
|
|
||||||
// if given a minTickSize in years, just use it,
|
|
||||||
// ensuring that it's an integer
|
|
||||||
|
|
||||||
if (opts.minTickSize != null && opts.minTickSize[1] == "year") {
|
|
||||||
size = Math.floor(opts.minTickSize[0]);
|
|
||||||
} else {
|
|
||||||
|
|
||||||
var magn = Math.pow(10, Math.floor(Math.log(axis.delta / timeUnitSize.year) / Math.LN10));
|
|
||||||
var norm = (axis.delta / timeUnitSize.year) / magn;
|
|
||||||
|
|
||||||
if (norm < 1.5) {
|
|
||||||
size = 1;
|
|
||||||
} else if (norm < 3) {
|
|
||||||
size = 2;
|
|
||||||
} else if (norm < 7.5) {
|
|
||||||
size = 5;
|
|
||||||
} else {
|
|
||||||
size = 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
size *= magn;
|
|
||||||
}
|
|
||||||
|
|
||||||
// minimum size for years is 1
|
|
||||||
|
|
||||||
if (size < 1) {
|
|
||||||
size = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
axis.tickSize = opts.tickSize || [size, unit];
|
|
||||||
var tickSize = axis.tickSize[0];
|
|
||||||
unit = axis.tickSize[1];
|
|
||||||
|
|
||||||
var step = tickSize * timeUnitSize[unit];
|
|
||||||
|
|
||||||
if (unit == "second") {
|
|
||||||
d.setSeconds(floorInBase(d.getSeconds(), tickSize));
|
|
||||||
} else if (unit == "minute") {
|
|
||||||
d.setMinutes(floorInBase(d.getMinutes(), tickSize));
|
|
||||||
} else if (unit == "hour") {
|
|
||||||
d.setHours(floorInBase(d.getHours(), tickSize));
|
|
||||||
} else if (unit == "month") {
|
|
||||||
d.setMonth(floorInBase(d.getMonth(), tickSize));
|
|
||||||
} else if (unit == "quarter") {
|
|
||||||
d.setMonth(3 * floorInBase(d.getMonth() / 3,
|
|
||||||
tickSize));
|
|
||||||
} else if (unit == "year") {
|
|
||||||
d.setFullYear(floorInBase(d.getFullYear(), tickSize));
|
|
||||||
}
|
|
||||||
|
|
||||||
// reset smaller components
|
|
||||||
|
|
||||||
d.setMilliseconds(0);
|
|
||||||
|
|
||||||
if (step >= timeUnitSize.minute) {
|
|
||||||
d.setSeconds(0);
|
|
||||||
}
|
|
||||||
if (step >= timeUnitSize.hour) {
|
|
||||||
d.setMinutes(0);
|
|
||||||
}
|
|
||||||
if (step >= timeUnitSize.day) {
|
|
||||||
d.setHours(0);
|
|
||||||
}
|
|
||||||
if (step >= timeUnitSize.day * 4) {
|
|
||||||
d.setDate(1);
|
|
||||||
}
|
|
||||||
if (step >= timeUnitSize.month * 2) {
|
|
||||||
d.setMonth(floorInBase(d.getMonth(), 3));
|
|
||||||
}
|
|
||||||
if (step >= timeUnitSize.quarter * 2) {
|
|
||||||
d.setMonth(floorInBase(d.getMonth(), 6));
|
|
||||||
}
|
|
||||||
if (step >= timeUnitSize.year) {
|
|
||||||
d.setMonth(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
var carry = 0;
|
|
||||||
var v = Number.NaN;
|
|
||||||
var prev;
|
|
||||||
|
|
||||||
do {
|
|
||||||
|
|
||||||
prev = v;
|
|
||||||
v = d.getTime();
|
|
||||||
ticks.push(v);
|
|
||||||
|
|
||||||
if (unit == "month" || unit == "quarter") {
|
|
||||||
if (tickSize < 1) {
|
|
||||||
|
|
||||||
// a bit complicated - we'll divide the
|
|
||||||
// month/quarter up but we need to take
|
|
||||||
// care of fractions so we don't end up in
|
|
||||||
// the middle of a day
|
|
||||||
|
|
||||||
d.setDate(1);
|
|
||||||
var start = d.getTime();
|
|
||||||
d.setMonth(d.getMonth() +
|
|
||||||
(unit == "quarter" ? 3 : 1));
|
|
||||||
var end = d.getTime();
|
|
||||||
d.setTime(v + carry * timeUnitSize.hour + (end - start) * tickSize);
|
|
||||||
carry = d.getHours();
|
|
||||||
d.setHours(0);
|
|
||||||
} else {
|
|
||||||
d.setMonth(d.getMonth() +
|
|
||||||
tickSize * (unit == "quarter" ? 3 : 1));
|
|
||||||
}
|
|
||||||
} else if (unit == "year") {
|
|
||||||
d.setFullYear(d.getFullYear() + tickSize);
|
|
||||||
} else {
|
|
||||||
d.setTime(v + step);
|
|
||||||
}
|
|
||||||
} while (v < axis.max && v != prev);
|
|
||||||
|
|
||||||
return ticks;
|
|
||||||
};
|
|
||||||
|
|
||||||
axis.tickFormatter = function (v, axis) {
|
|
||||||
|
|
||||||
var d = dateGenerator(v, axis.options);
|
|
||||||
|
|
||||||
// first check global format
|
|
||||||
|
|
||||||
if (opts.timeformat != null) {
|
|
||||||
return formatDate(d, opts.timeformat, opts.monthNames, opts.dayNames);
|
|
||||||
}
|
|
||||||
|
|
||||||
// possibly use quarters if quarters are mentioned in
|
|
||||||
// any of these places
|
|
||||||
|
|
||||||
var useQuarters = (axis.options.tickSize &&
|
|
||||||
axis.options.tickSize[1] == "quarter") ||
|
|
||||||
(axis.options.minTickSize &&
|
|
||||||
axis.options.minTickSize[1] == "quarter");
|
|
||||||
|
|
||||||
var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]];
|
|
||||||
var span = axis.max - axis.min;
|
|
||||||
var suffix = (opts.twelveHourClock) ? " %p" : "";
|
|
||||||
var hourCode = (opts.twelveHourClock) ? "%I" : "%H";
|
|
||||||
var fmt;
|
|
||||||
|
|
||||||
if (t < timeUnitSize.minute) {
|
|
||||||
fmt = hourCode + ":%M:%S" + suffix;
|
|
||||||
} else if (t < timeUnitSize.day) {
|
|
||||||
if (span < 2 * timeUnitSize.day) {
|
|
||||||
fmt = hourCode + ":%M" + suffix;
|
|
||||||
} else {
|
|
||||||
fmt = "%b %d " + hourCode + ":%M" + suffix;
|
|
||||||
}
|
|
||||||
} else if (t < timeUnitSize.month) {
|
|
||||||
fmt = "%b %d";
|
|
||||||
} else if ((useQuarters && t < timeUnitSize.quarter) ||
|
|
||||||
(!useQuarters && t < timeUnitSize.year)) {
|
|
||||||
if (span < timeUnitSize.year) {
|
|
||||||
fmt = "%b";
|
|
||||||
} else {
|
|
||||||
fmt = "%b %Y";
|
|
||||||
}
|
|
||||||
} else if (useQuarters && t < timeUnitSize.year) {
|
|
||||||
if (span < timeUnitSize.year) {
|
|
||||||
fmt = "Q%q";
|
|
||||||
} else {
|
|
||||||
fmt = "Q%q %Y";
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fmt = "%Y";
|
|
||||||
}
|
|
||||||
|
|
||||||
var rt = formatDate(d, fmt, opts.monthNames, opts.dayNames);
|
|
||||||
|
|
||||||
return rt;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$.plot.plugins.push({
|
|
||||||
init: init,
|
|
||||||
options: options,
|
|
||||||
name: 'time',
|
|
||||||
version: '1.0'
|
|
||||||
});
|
|
||||||
|
|
||||||
// Time-axis support used to be in Flot core, which exposed the
|
|
||||||
// formatDate function on the plot object. Various plugins depend
|
|
||||||
// on the function, so we need to re-expose it here.
|
|
||||||
|
|
||||||
$.plot.formatDate = formatDate;
|
|
||||||
$.plot.dateGenerator = dateGenerator;
|
|
||||||
|
|
||||||
})(jQuery);
|
|
||||||
|
Before Width: | Height: | Size: 5.7 KiB |
@@ -1,75 +0,0 @@
|
|||||||
$(document).ready(function() {
|
|
||||||
// Start 1-second timer to call RESTful endpoint
|
|
||||||
setInterval(function() {
|
|
||||||
$.ajax({
|
|
||||||
url: '/get_cpu_usage',
|
|
||||||
dataType: 'json',
|
|
||||||
success: function(json) {
|
|
||||||
$('#cpu_usage').text(json.result + '% ');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, 1000);
|
|
||||||
|
|
||||||
// Initialize graph
|
|
||||||
var $c = $('<div class="graph"/>').appendTo('#graphs');
|
|
||||||
var dur = 120;
|
|
||||||
var past = Date.now() - dur * 1000;
|
|
||||||
var options = {
|
|
||||||
lines: {
|
|
||||||
fill: true,
|
|
||||||
lineWidth: 0,
|
|
||||||
fillColor: {colors: [ { opacity: 0 }, { opacity: 1 } ] }
|
|
||||||
},
|
|
||||||
grid: { borderWidth: 1, borderColor: '#ccc'},
|
|
||||||
xaxis: { mode: 'time', ticks: 5 },
|
|
||||||
legend: { labelBoxBorderColor: '#fff' },
|
|
||||||
colors: [ '#fec', '#396', '#e39', '9e2' ],
|
|
||||||
hooks: {
|
|
||||||
draw: [function(plot, canvas) {
|
|
||||||
canvas.font = '13px sans-serif';
|
|
||||||
canvas.fillStyle = '#aaa';
|
|
||||||
canvas.fillText('Resource Usage', 35, 25);
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
var plot = $.plot($c, [], options);
|
|
||||||
|
|
||||||
var updateGraph = function(counter) {
|
|
||||||
var data = plot.getData();
|
|
||||||
var now = Date.now();
|
|
||||||
var oldest = now - dur * 1000;
|
|
||||||
data[0] = {
|
|
||||||
show: false,
|
|
||||||
data: [[oldest, null], [now, null]]
|
|
||||||
};
|
|
||||||
|
|
||||||
console.log(data);
|
|
||||||
|
|
||||||
// Remove old points
|
|
||||||
$.each(data, function(di, d) {
|
|
||||||
while (d.data.length > 0 && d.data[0][0] < oldest) {
|
|
||||||
d.data.shift();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add new points
|
|
||||||
var new_datapoint = [now, counter];
|
|
||||||
if (data[1]) {
|
|
||||||
data[1].label = 'memory';
|
|
||||||
data[1].data.push(new_datapoint);
|
|
||||||
} else {
|
|
||||||
data[1] = [{ data: [new_datapoint] }];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Redraw the graph
|
|
||||||
plot.setData(data);
|
|
||||||
plot.setupGrid();
|
|
||||||
plot.draw();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create Websocket connection. For simplicity, no reconnect logic is here.
|
|
||||||
var ws = new WebSocket('ws://' + location.host);
|
|
||||||
ws.onmessage = function(ev) {
|
|
||||||
updateGraph(ev.data);
|
|
||||||
};
|
|
||||||
});
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
* {
|
|
||||||
outline: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
background: #36c;
|
|
||||||
color: #555;
|
|
||||||
margin: 0;
|
|
||||||
padding: 0;
|
|
||||||
font: 16px/1.4 Helvetica, Arial, sans-serif;
|
|
||||||
}
|
|
||||||
|
|
||||||
div.content {
|
|
||||||
width: 800px;
|
|
||||||
margin: 1em auto;
|
|
||||||
padding: 2em;
|
|
||||||
background-color: #fff;
|
|
||||||
border-radius: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset {
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
padding: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset legend {
|
|
||||||
font-style: italic;
|
|
||||||
color: silver;
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldset label {
|
|
||||||
text-align: right;
|
|
||||||
padding: 0 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type=text] {
|
|
||||||
font-size: 100%;
|
|
||||||
padding: 0.2em;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
color: #666;
|
|
||||||
background: #eee;
|
|
||||||
border: 1px solid #ccc;
|
|
||||||
padding: 0.2em 2em;
|
|
||||||
font-size: 100%;
|
|
||||||
cursor: pointer;
|
|
||||||
margin-top: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
button:hover {
|
|
||||||
background: #ccc;
|
|
||||||
}
|
|
||||||
|
|
||||||
code {
|
|
||||||
background: #f0f0f0;
|
|
||||||
padding: 0 0.3em;
|
|
||||||
color: #396;
|
|
||||||
border-radius: 0.3em;
|
|
||||||
}
|
|
||||||
|
|
||||||
a:link, a:visited {
|
|
||||||
color: #69c;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
#graphs {
|
|
||||||
margin-top: 1em;
|
|
||||||
}
|
|
||||||
|
|
||||||
.graph {
|
|
||||||
min-height: 12em;
|
|
||||||
border: 0px solid #ccc;
|
|
||||||
position: relative;
|
|
||||||
margin: 0 1em 1em 0;
|
|
||||||
width: 36em;
|
|
||||||
display: inline-block;
|
|
||||||
}
|
|
||||||
|
|
||||||
.graph_title {
|
|
||||||
position: absolute;
|
|
||||||
top: 0.5em; left: 1em;
|
|
||||||
color: #ccc;
|
|
||||||
font-weight: bold;
|
|
||||||
z-index: 999;
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
PROG ?= example
|
||||||
|
|
||||||
|
all: $(PROG)
|
||||||
|
$(DEBUGGER) ./$(PROG)
|
||||||
|
|
||||||
|
$(PROG):
|
||||||
|
$(CC) ../../mongoose.c -I../.. $(CFLAGS) $(EXTRA) -o $(PROG) main.c mjson.c
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb log.txt
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
// Copyright (c) 2020 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
|
||||||
|
#include "mjson.h" // JSON parsing and printing
|
||||||
|
#include "mongoose.h"
|
||||||
|
|
||||||
|
// This is a configuration structure we're going to show on a dashboard
|
||||||
|
static struct config {
|
||||||
|
int value1;
|
||||||
|
char *value2;
|
||||||
|
} s_config = {123, NULL};
|
||||||
|
|
||||||
|
// Stringifies the config. A caller must free() it.
|
||||||
|
static char *stringify_config(struct config *cfg) {
|
||||||
|
char *s = NULL;
|
||||||
|
mjson_printf(mjson_print_dynamic_buf, &s, "{%Q:%d,%Q:%Q}", "value1",
|
||||||
|
cfg->value1, "value2", cfg->value2);
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update config structure. Return true if changed, false otherwise
|
||||||
|
static bool update_config(struct mg_http_message *hm, struct config *cfg) {
|
||||||
|
bool changed = false;
|
||||||
|
char buf[256];
|
||||||
|
double dv;
|
||||||
|
if (mjson_get_number(hm->body.ptr, hm->body.len, "$.value1", &dv)) {
|
||||||
|
s_config.value1 = dv;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
if (mjson_get_string(hm->body.ptr, hm->body.len, "$.value2", buf,
|
||||||
|
sizeof(buf)) > 0) {
|
||||||
|
free(s_config.value2);
|
||||||
|
s_config.value2 = strdup(buf);
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify all config watchers about the config change
|
||||||
|
static void notify_config_change(struct mg_mgr *mgr) {
|
||||||
|
struct mg_connection *c;
|
||||||
|
char *s = stringify_config(&s_config);
|
||||||
|
for (c = mgr->conns; c != NULL; c = c->next) {
|
||||||
|
if (c->label[0] == 'W') mg_http_printf_chunk(c, "%s\n", s);
|
||||||
|
}
|
||||||
|
free(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
// HTTP request handler function. It implements the following endpoints:
|
||||||
|
// /api/config/get - returns current config
|
||||||
|
// /api/config/set - updates current config
|
||||||
|
// /api/config/watch - does not return. Sends config as it changes in
|
||||||
|
// chunks all other URI - serves web_root/ directory
|
||||||
|
static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||||
|
if (ev == MG_EV_HTTP_MSG) {
|
||||||
|
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||||
|
if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||||
|
char *s = stringify_config(&s_config);
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: %d\r\n\r\n%s\n",
|
||||||
|
(int) strlen(s) + 1, s);
|
||||||
|
free(s);
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||||
|
if (update_config(hm, &s_config)) notify_config_change(fn_data);
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
|
||||||
|
} else if (mg_http_match_uri(hm, "/api/config/watch")) {
|
||||||
|
c->label[0] = 'W'; // Mark ourselves as a config watcher
|
||||||
|
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||||
|
} else {
|
||||||
|
struct mg_http_serve_opts opts = {.root_dir = "web_root"};
|
||||||
|
mg_http_serve_dir(c, ev_data, &opts);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
struct mg_mgr mgr;
|
||||||
|
mg_mgr_init(&mgr);
|
||||||
|
mg_http_listen(&mgr, "http://localhost:8000", cb, &mgr);
|
||||||
|
for (;;) mg_mgr_poll(&mgr, 1000);
|
||||||
|
mg_mgr_free(&mgr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,981 @@
|
|||||||
|
// Copyright (c) 2018-2020 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
#include "mjson.h"
|
||||||
|
|
||||||
|
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||||
|
#else
|
||||||
|
#define va_copy(x, y) (x) = (y)
|
||||||
|
#define snprintf _snprintf
|
||||||
|
#define alloca _alloca
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static int mjson_esc(int c, int esc) {
|
||||||
|
const char *p, *esc1 = "\b\f\n\r\t\\\"", *esc2 = "bfnrt\\\"";
|
||||||
|
for (p = esc ? esc1 : esc2; *p != '\0'; p++) {
|
||||||
|
if (*p == c) return esc ? esc2[p - esc1] : esc1[p - esc2];
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mjson_pass_string(const char *s, int len) {
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
if (s[i] == '\\' && i + 1 < len && mjson_esc(s[i + 1], 1)) {
|
||||||
|
i++;
|
||||||
|
} else if (s[i] == '\0') {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
} else if (s[i] == '"') {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson(const char *s, int len, mjson_cb_t cb, void *ud) {
|
||||||
|
enum { S_VALUE, S_KEY, S_COLON, S_COMMA_OR_EOO } expecting = S_VALUE;
|
||||||
|
unsigned char nesting[MJSON_MAX_DEPTH];
|
||||||
|
int i, depth = 0;
|
||||||
|
#define MJSONCALL(ev) \
|
||||||
|
if (cb != NULL && cb(ev, s, start, i - start + 1, ud)) return i + 1;
|
||||||
|
|
||||||
|
// In the ascii table, the distance between `[` and `]` is 2.
|
||||||
|
// Ditto for `{` and `}`. Hence +2 in the code below.
|
||||||
|
#define MJSONEOO() \
|
||||||
|
do { \
|
||||||
|
if (c != nesting[depth - 1] + 2) return MJSON_ERROR_INVALID_INPUT; \
|
||||||
|
depth--; \
|
||||||
|
if (depth == 0) { \
|
||||||
|
MJSONCALL(tok); \
|
||||||
|
return i + 1; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
int start = i;
|
||||||
|
unsigned char c = ((unsigned char *) s)[i];
|
||||||
|
int tok = c;
|
||||||
|
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') continue;
|
||||||
|
// printf("- %c [%.*s] %d %d\n", c, i, s, depth, expecting);
|
||||||
|
switch (expecting) {
|
||||||
|
case S_VALUE:
|
||||||
|
if (c == '{') {
|
||||||
|
if (depth >= (int) sizeof(nesting)) return MJSON_ERROR_TOO_DEEP;
|
||||||
|
nesting[depth++] = c;
|
||||||
|
expecting = S_KEY;
|
||||||
|
break;
|
||||||
|
} else if (c == '[') {
|
||||||
|
if (depth >= (int) sizeof(nesting)) return MJSON_ERROR_TOO_DEEP;
|
||||||
|
nesting[depth++] = c;
|
||||||
|
break;
|
||||||
|
} else if (c == ']' && depth > 0) { // Empty array
|
||||||
|
MJSONEOO();
|
||||||
|
} else if (c == 't' && i + 3 < len && memcmp(&s[i], "true", 4) == 0) {
|
||||||
|
i += 3;
|
||||||
|
tok = MJSON_TOK_TRUE;
|
||||||
|
} else if (c == 'n' && i + 3 < len && memcmp(&s[i], "null", 4) == 0) {
|
||||||
|
i += 3;
|
||||||
|
tok = MJSON_TOK_NULL;
|
||||||
|
} else if (c == 'f' && i + 4 < len && memcmp(&s[i], "false", 5) == 0) {
|
||||||
|
i += 4;
|
||||||
|
tok = MJSON_TOK_FALSE;
|
||||||
|
} else if (c == '-' || ((c >= '0' && c <= '9'))) {
|
||||||
|
char *end = NULL;
|
||||||
|
strtod(&s[i], &end);
|
||||||
|
if (end != NULL) i += end - &s[i] - 1;
|
||||||
|
tok = MJSON_TOK_NUMBER;
|
||||||
|
} else if (c == '"') {
|
||||||
|
int n = mjson_pass_string(&s[i + 1], len - i - 1);
|
||||||
|
if (n < 0) return n;
|
||||||
|
i += n + 1;
|
||||||
|
tok = MJSON_TOK_STRING;
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
if (depth == 0) {
|
||||||
|
MJSONCALL(tok);
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
expecting = S_COMMA_OR_EOO;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case S_KEY:
|
||||||
|
if (c == '"') {
|
||||||
|
int n = mjson_pass_string(&s[i + 1], len - i - 1);
|
||||||
|
if (n < 0) return n;
|
||||||
|
i += n + 1;
|
||||||
|
tok = MJSON_TOK_KEY;
|
||||||
|
expecting = S_COLON;
|
||||||
|
} else if (c == '}') { // Empty object
|
||||||
|
MJSONEOO();
|
||||||
|
expecting = S_COMMA_OR_EOO;
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case S_COLON:
|
||||||
|
if (c == ':') {
|
||||||
|
expecting = S_VALUE;
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case S_COMMA_OR_EOO:
|
||||||
|
if (depth <= 0) return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
if (c == ',') {
|
||||||
|
expecting = (nesting[depth - 1] == '{') ? S_KEY : S_VALUE;
|
||||||
|
} else if (c == ']' || c == '}') {
|
||||||
|
MJSONEOO();
|
||||||
|
} else {
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
MJSONCALL(tok);
|
||||||
|
}
|
||||||
|
return MJSON_ERROR_INVALID_INPUT;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct msjon_get_data {
|
||||||
|
const char *path; // Lookup json path
|
||||||
|
int pos; // Current path index
|
||||||
|
int d1; // Current depth of traversal
|
||||||
|
int d2; // Expected depth of traversal
|
||||||
|
int i1; // Index in an array
|
||||||
|
int i2; // Expected index in an array
|
||||||
|
int obj; // If the value is array/object, offset where it starts
|
||||||
|
const char **tokptr; // Destination
|
||||||
|
int *toklen; // Destination length
|
||||||
|
int tok; // Returned token
|
||||||
|
};
|
||||||
|
|
||||||
|
static int mjson_plen(const char *s) {
|
||||||
|
int i = 0;
|
||||||
|
while (s[i] != '\0' && s[i] != '.' && s[i] != '[') i++;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mjson_get_cb(int tok, const char *s, int off, int len, void *ud) {
|
||||||
|
struct msjon_get_data *data = (struct msjon_get_data *) ud;
|
||||||
|
// printf("--> %2x %2d %2d %2d %2d\t'%s'\t'%.*s'\t\t'%.*s'\n", tok, data->d1,
|
||||||
|
// data->d2, data->i1, data->i2, data->path + data->pos, off, s, len,
|
||||||
|
// s + off);
|
||||||
|
if (data->tok != MJSON_TOK_INVALID) return 1; // Found
|
||||||
|
|
||||||
|
if (tok == '{') {
|
||||||
|
if (!data->path[data->pos] && data->d1 == data->d2) data->obj = off;
|
||||||
|
data->d1++;
|
||||||
|
} else if (tok == '[') {
|
||||||
|
if (data->d1 == data->d2 && data->path[data->pos] == '[') {
|
||||||
|
data->i1 = 0;
|
||||||
|
data->i2 = (int) strtod(&data->path[data->pos + 1], NULL);
|
||||||
|
if (data->i1 == data->i2) {
|
||||||
|
data->d2++;
|
||||||
|
data->pos += 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!data->path[data->pos] && data->d1 == data->d2) data->obj = off;
|
||||||
|
data->d1++;
|
||||||
|
} else if (tok == ',') {
|
||||||
|
if (data->d1 == data->d2 + 1) {
|
||||||
|
data->i1++;
|
||||||
|
if (data->i1 == data->i2) {
|
||||||
|
while (data->path[data->pos] != ']') data->pos++;
|
||||||
|
data->pos++;
|
||||||
|
data->d2++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (tok == MJSON_TOK_KEY && data->d1 == data->d2 + 1 &&
|
||||||
|
data->path[data->pos] == '.' && s[off] == '"' &&
|
||||||
|
s[off + len - 1] == '"' &&
|
||||||
|
mjson_plen(&data->path[data->pos + 1]) == len - 2 &&
|
||||||
|
!memcmp(s + off + 1, &data->path[data->pos + 1], len - 2)) {
|
||||||
|
data->d2++;
|
||||||
|
data->pos += len - 1;
|
||||||
|
} else if (tok == MJSON_TOK_KEY && data->d1 == data->d2) {
|
||||||
|
return 1; // Exhausted path, not found
|
||||||
|
} else if (tok == '}' || tok == ']') {
|
||||||
|
data->d1--;
|
||||||
|
// data->d2--;
|
||||||
|
if (!data->path[data->pos] && data->d1 == data->d2 && data->obj != -1) {
|
||||||
|
data->tok = tok - 2;
|
||||||
|
if (data->tokptr) *data->tokptr = s + data->obj;
|
||||||
|
if (data->toklen) *data->toklen = off - data->obj + 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
} else if (MJSON_TOK_IS_VALUE(tok)) {
|
||||||
|
// printf("TOK --> %d\n", tok);
|
||||||
|
if (data->d1 == data->d2 && !data->path[data->pos]) {
|
||||||
|
data->tok = tok;
|
||||||
|
if (data->tokptr) *data->tokptr = s + off;
|
||||||
|
if (data->toklen) *data->toklen = len;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
enum mjson_tok mjson_find(const char *s, int len, const char *jp,
|
||||||
|
const char **tokptr, int *toklen) {
|
||||||
|
struct msjon_get_data data = {jp, 1, 0, 0, 0,
|
||||||
|
0, -1, tokptr, toklen, MJSON_TOK_INVALID};
|
||||||
|
if (jp[0] != '$') return MJSON_TOK_INVALID;
|
||||||
|
if (mjson(s, len, mjson_get_cb, &data) < 0) return MJSON_TOK_INVALID;
|
||||||
|
return (enum mjson_tok) data.tok;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_number(const char *s, int len, const char *path, double *v) {
|
||||||
|
const char *p;
|
||||||
|
int tok, n;
|
||||||
|
if ((tok = mjson_find(s, len, path, &p, &n)) == MJSON_TOK_NUMBER) {
|
||||||
|
if (v != NULL) *v = strtod(p, NULL);
|
||||||
|
}
|
||||||
|
return tok == MJSON_TOK_NUMBER ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_bool(const char *s, int len, const char *path, int *v) {
|
||||||
|
int tok = mjson_find(s, len, path, NULL, NULL);
|
||||||
|
if (tok == MJSON_TOK_TRUE && v != NULL) *v = 1;
|
||||||
|
if (tok == MJSON_TOK_FALSE && v != NULL) *v = 0;
|
||||||
|
return tok == MJSON_TOK_TRUE || tok == MJSON_TOK_FALSE ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int mjson_unescape(const char *s, int len, char *to, int n) {
|
||||||
|
int i, j;
|
||||||
|
for (i = 0, j = 0; i < len && j < n; i++, j++) {
|
||||||
|
if (s[i] == '\\' && i + 1 < len) {
|
||||||
|
int c = mjson_esc(s[i + 1], 0);
|
||||||
|
if (c == 0) return -1;
|
||||||
|
to[j] = c;
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
to[j] = s[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (j >= n) return -1;
|
||||||
|
if (n > 0) to[j] = '\0';
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_string(const char *s, int len, const char *path, char *to,
|
||||||
|
int n) {
|
||||||
|
const char *p;
|
||||||
|
int sz;
|
||||||
|
if (mjson_find(s, len, path, &p, &sz) != MJSON_TOK_STRING) return -1;
|
||||||
|
return mjson_unescape(p + 1, sz - 2, to, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_hex(const char *s, int len, const char *x, char *to, int n) {
|
||||||
|
const char *p;
|
||||||
|
int i, j, sz;
|
||||||
|
if (mjson_find(s, len, x, &p, &sz) != MJSON_TOK_STRING) return -1;
|
||||||
|
for (i = j = 0; i < sz - 3 && j < n; i += 2, j++) {
|
||||||
|
#define HEXTOI(x) (x >= '0' && x <= '9' ? x - '0' : x - 'W')
|
||||||
|
unsigned char a = *(const unsigned char *) (p + i + 1);
|
||||||
|
unsigned char b = *(const unsigned char *) (p + i + 2);
|
||||||
|
((unsigned char *) to)[j] = (HEXTOI(a) << 4) | HEXTOI(b);
|
||||||
|
}
|
||||||
|
if (j < n) to[j] = '\0';
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
static int mjson_base64rev(int c) {
|
||||||
|
if (c >= 'A' && c <= 'Z') {
|
||||||
|
return c - 'A';
|
||||||
|
} else if (c >= 'a' && c <= 'z') {
|
||||||
|
return c + 26 - 'a';
|
||||||
|
} else if (c >= '0' && c <= '9') {
|
||||||
|
return c + 52 - '0';
|
||||||
|
} else if (c == '+') {
|
||||||
|
return 62;
|
||||||
|
} else if (c == '/') {
|
||||||
|
return 63;
|
||||||
|
} else {
|
||||||
|
return 64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_base64_dec(const char *src, int n, char *dst, int dlen) {
|
||||||
|
const char *end = src + n;
|
||||||
|
int len = 0;
|
||||||
|
while (src + 3 < end && len < dlen) {
|
||||||
|
int a = mjson_base64rev(src[0]), b = mjson_base64rev(src[1]),
|
||||||
|
c = mjson_base64rev(src[2]), d = mjson_base64rev(src[3]);
|
||||||
|
dst[len++] = (a << 2) | (b >> 4);
|
||||||
|
if (src[2] != '=' && len < dlen) {
|
||||||
|
dst[len++] = (b << 4) | (c >> 2);
|
||||||
|
if (src[3] != '=' && len < dlen) {
|
||||||
|
dst[len++] = (c << 6) | d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
src += 4;
|
||||||
|
}
|
||||||
|
if (len < dlen) dst[len] = '\0';
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_get_base64(const char *s, int len, const char *path, char *to,
|
||||||
|
int n) {
|
||||||
|
const char *p;
|
||||||
|
int sz;
|
||||||
|
if (mjson_find(s, len, path, &p, &sz) != MJSON_TOK_STRING) return 0;
|
||||||
|
return mjson_base64_dec(p + 1, sz - 2, to, n);
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_BASE64
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_NEXT
|
||||||
|
struct nextdata {
|
||||||
|
int off, len, depth, t, vo, arrayindex;
|
||||||
|
int *koff, *klen, *voff, *vlen, *vtype;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int next_cb(int tok, const char *s, int off, int len, void *ud) {
|
||||||
|
struct nextdata *d = (struct nextdata *) ud;
|
||||||
|
// int i;
|
||||||
|
switch (tok) {
|
||||||
|
case '{':
|
||||||
|
case '[':
|
||||||
|
if (d->depth == 0 && tok == '[') d->arrayindex = 0;
|
||||||
|
if (d->depth == 1 && off > d->off) {
|
||||||
|
d->vo = off;
|
||||||
|
d->t = tok == '{' ? MJSON_TOK_OBJECT : MJSON_TOK_ARRAY;
|
||||||
|
if (d->voff) *d->voff = off;
|
||||||
|
if (d->vtype) *d->vtype = d->t;
|
||||||
|
}
|
||||||
|
d->depth++;
|
||||||
|
break;
|
||||||
|
case '}':
|
||||||
|
case ']':
|
||||||
|
d->depth--;
|
||||||
|
if (d->depth == 1 && d->vo) {
|
||||||
|
d->len = off + len;
|
||||||
|
if (d->vlen) *d->vlen = d->len - d->vo;
|
||||||
|
if (d->arrayindex >= 0) {
|
||||||
|
if (d->koff) *d->koff = d->arrayindex; // koff holds array index
|
||||||
|
if (d->klen) *d->klen = 0; // klen holds 0
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (d->depth == 1 && d->arrayindex >= 0) d->arrayindex++;
|
||||||
|
break;
|
||||||
|
case ',':
|
||||||
|
case ':':
|
||||||
|
break;
|
||||||
|
case MJSON_TOK_KEY:
|
||||||
|
if (d->depth == 1 && d->off < off) {
|
||||||
|
if (d->koff) *d->koff = off; // And report back to the user
|
||||||
|
if (d->klen) *d->klen = len; // If we have to
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (d->depth != 1) break;
|
||||||
|
// If we're iterating over the array
|
||||||
|
if (off > d->off) {
|
||||||
|
d->len = off + len;
|
||||||
|
if (d->vlen) *d->vlen = len; // value length
|
||||||
|
if (d->voff) *d->voff = off; // value offset
|
||||||
|
if (d->vtype) *d->vtype = tok; // value type
|
||||||
|
if (d->arrayindex >= 0) {
|
||||||
|
if (d->koff) *d->koff = d->arrayindex; // koff holds array index
|
||||||
|
if (d->klen) *d->klen = 0; // klen holds 0
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (d->arrayindex >= 0) d->arrayindex++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
(void) s;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_next(const char *s, int n, int off, int *koff, int *klen, int *voff,
|
||||||
|
int *vlen, int *vtype) {
|
||||||
|
struct nextdata d = {off, 0, 0, 0, 0, -1, koff, klen, voff, vlen, vtype};
|
||||||
|
mjson(s, n, next_cb, &d);
|
||||||
|
return d.len;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRINT
|
||||||
|
int mjson_print_fixed_buf(const char *ptr, int len, void *fndata) {
|
||||||
|
struct mjson_fixedbuf *fb = (struct mjson_fixedbuf *) fndata;
|
||||||
|
int i, left = fb->size - 1 - fb->len;
|
||||||
|
if (left < len) len = left;
|
||||||
|
for (i = 0; i < len; i++) fb->ptr[fb->len + i] = ptr[i];
|
||||||
|
fb->len += len;
|
||||||
|
fb->ptr[fb->len] = '\0';
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_dynamic_buf(const char *ptr, int len, void *fndata) {
|
||||||
|
char *s, *buf = *(char **) fndata;
|
||||||
|
int curlen = buf == NULL ? 0 : strlen(buf);
|
||||||
|
if ((s = (char *) realloc(buf, curlen + len + 1)) == NULL) {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
memcpy(s + curlen, ptr, len);
|
||||||
|
s[curlen + len] = '\0';
|
||||||
|
*(char **) fndata = s;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_null(const char *ptr, int len, void *userdata) {
|
||||||
|
(void) ptr;
|
||||||
|
(void) userdata;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_file(const char *ptr, int len, void *userdata) {
|
||||||
|
return fwrite(ptr, 1, len, (FILE *) userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_buf(mjson_print_fn_t fn, void *fndata, const char *buf,
|
||||||
|
int len) {
|
||||||
|
return fn(buf, len, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_int(mjson_print_fn_t fn, void *fndata, int value,
|
||||||
|
int is_signed) {
|
||||||
|
char buf[20];
|
||||||
|
int len = snprintf(buf, sizeof(buf), is_signed ? "%d" : "%u", value);
|
||||||
|
return fn(buf, len, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_long(mjson_print_fn_t fn, void *fndata, long value,
|
||||||
|
int is_signed) {
|
||||||
|
char buf[20];
|
||||||
|
const char *fmt = (is_signed ? "%ld" : "%lu");
|
||||||
|
int len = snprintf(buf, sizeof(buf), fmt, value);
|
||||||
|
return fn(buf, len, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_dbl(mjson_print_fn_t fn, void *fndata, double d,
|
||||||
|
const char *fmt) {
|
||||||
|
char buf[40];
|
||||||
|
int n = snprintf(buf, sizeof(buf), fmt, d);
|
||||||
|
return fn(buf, n, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_print_str(mjson_print_fn_t fn, void *fndata, const char *s, int len) {
|
||||||
|
int i, n = fn("\"", 1, fndata);
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
char c = mjson_esc(s[i], 1);
|
||||||
|
if (c) {
|
||||||
|
n += fn("\\", 1, fndata);
|
||||||
|
n += fn(&c, 1, fndata);
|
||||||
|
} else {
|
||||||
|
n += fn(&s[i], 1, fndata);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n + fn("\"", 1, fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
int mjson_print_b64(mjson_print_fn_t fn, void *fndata, const unsigned char *s,
|
||||||
|
int n) {
|
||||||
|
const char *t =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
int i, len = fn("\"", 1, fndata);
|
||||||
|
for (i = 0; i < n; i += 3) {
|
||||||
|
int a = s[i], b = i + 1 < n ? s[i + 1] : 0, c = i + 2 < n ? s[i + 2] : 0;
|
||||||
|
char buf[4] = {t[a >> 2], t[(a & 3) << 4 | (b >> 4)], '=', '='};
|
||||||
|
if (i + 1 < n) buf[2] = t[(b & 15) << 2 | (c >> 6)];
|
||||||
|
if (i + 2 < n) buf[3] = t[c & 63];
|
||||||
|
len += fn(buf, sizeof(buf), fndata);
|
||||||
|
}
|
||||||
|
return len + fn("\"", 1, fndata);
|
||||||
|
}
|
||||||
|
#endif /* MJSON_ENABLE_BASE64 */
|
||||||
|
|
||||||
|
int mjson_vprintf(mjson_print_fn_t fn, void *fndata, const char *fmt,
|
||||||
|
va_list xap) {
|
||||||
|
int i = 0, n = 0;
|
||||||
|
va_list ap;
|
||||||
|
va_copy(ap, xap);
|
||||||
|
while (fmt[i] != '\0') {
|
||||||
|
if (fmt[i] == '%') {
|
||||||
|
char fc = fmt[++i];
|
||||||
|
int is_long = 0;
|
||||||
|
if (fc == 'l') {
|
||||||
|
is_long = 1;
|
||||||
|
fc = fmt[i + 1];
|
||||||
|
}
|
||||||
|
if (fc == 'Q') {
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_str(fn, fndata, buf ? buf : "", buf ? strlen(buf) : 0);
|
||||||
|
} else if (strncmp(&fmt[i], ".*Q", 3) == 0) {
|
||||||
|
int len = va_arg(ap, int);
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_str(fn, fndata, buf, len);
|
||||||
|
i += 2;
|
||||||
|
} else if (fc == 'd' || fc == 'u') {
|
||||||
|
int is_signed = (fc == 'd');
|
||||||
|
if (is_long) {
|
||||||
|
long val = va_arg(ap, long);
|
||||||
|
n += mjson_print_long(fn, fndata, val, is_signed);
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
int val = va_arg(ap, int);
|
||||||
|
n += mjson_print_int(fn, fndata, val, is_signed);
|
||||||
|
}
|
||||||
|
} else if (fc == 'B') {
|
||||||
|
const char *s = va_arg(ap, int) ? "true" : "false";
|
||||||
|
n += mjson_print_buf(fn, fndata, s, strlen(s));
|
||||||
|
} else if (fc == 's') {
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_buf(fn, fndata, buf, strlen(buf));
|
||||||
|
} else if (strncmp(&fmt[i], ".*s", 3) == 0) {
|
||||||
|
int len = va_arg(ap, int);
|
||||||
|
char *buf = va_arg(ap, char *);
|
||||||
|
n += mjson_print_buf(fn, fndata, buf, len);
|
||||||
|
i += 2;
|
||||||
|
} else if (fc == 'g') {
|
||||||
|
n += mjson_print_dbl(fn, fndata, va_arg(ap, double), "%g");
|
||||||
|
} else if (fc == 'f') {
|
||||||
|
n += mjson_print_dbl(fn, fndata, va_arg(ap, double), "%f");
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
} else if (fc == 'V') {
|
||||||
|
int len = va_arg(ap, int);
|
||||||
|
const char *buf = va_arg(ap, const char *);
|
||||||
|
n += mjson_print_b64(fn, fndata, (unsigned char *) buf, len);
|
||||||
|
#endif
|
||||||
|
} else if (fc == 'H') {
|
||||||
|
const char *hex = "0123456789abcdef";
|
||||||
|
int i, len = va_arg(ap, int);
|
||||||
|
const unsigned char *p = va_arg(ap, const unsigned char *);
|
||||||
|
n += fn("\"", 1, fndata);
|
||||||
|
for (i = 0; i < len; i++) {
|
||||||
|
n += fn(&hex[(p[i] >> 4) & 15], 1, fndata);
|
||||||
|
n += fn(&hex[p[i] & 15], 1, fndata);
|
||||||
|
}
|
||||||
|
n += fn("\"", 1, fndata);
|
||||||
|
} else if (fc == 'M') {
|
||||||
|
mjson_vprint_fn_t vfn = va_arg(ap, mjson_vprint_fn_t);
|
||||||
|
n += vfn(fn, fndata, &ap);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
n += mjson_print_buf(fn, fndata, &fmt[i++], 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
va_end(xap);
|
||||||
|
va_end(ap);
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_printf(mjson_print_fn_t fn, void *fndata, const char *fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
int len;
|
||||||
|
va_start(ap, fmt);
|
||||||
|
len = mjson_vprintf(fn, fndata, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
#endif /* MJSON_ENABLE_PRINT */
|
||||||
|
|
||||||
|
#if MJSON_IMPLEMENT_STRTOD
|
||||||
|
static inline int is_digit(int c) {
|
||||||
|
return c >= '0' && c <= '9';
|
||||||
|
}
|
||||||
|
|
||||||
|
/* NOTE: strtod() implementation by Yasuhiro Matsumoto. */
|
||||||
|
double strtod(const char *str, char **end) {
|
||||||
|
double d = 0.0;
|
||||||
|
int sign = 1, n = 0;
|
||||||
|
const char *p = str, *a = str;
|
||||||
|
|
||||||
|
/* decimal part */
|
||||||
|
if (*p == '-') {
|
||||||
|
sign = -1;
|
||||||
|
++p;
|
||||||
|
} else if (*p == '+')
|
||||||
|
++p;
|
||||||
|
if (is_digit(*p)) {
|
||||||
|
d = (double) (*p++ - '0');
|
||||||
|
while (*p && is_digit(*p)) {
|
||||||
|
d = d * 10.0 + (double) (*p - '0');
|
||||||
|
++p;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
a = p;
|
||||||
|
} else if (*p != '.')
|
||||||
|
goto done;
|
||||||
|
d *= sign;
|
||||||
|
|
||||||
|
/* fraction part */
|
||||||
|
if (*p == '.') {
|
||||||
|
double f = 0.0;
|
||||||
|
double base = 0.1;
|
||||||
|
++p;
|
||||||
|
|
||||||
|
if (is_digit(*p)) {
|
||||||
|
while (*p && is_digit(*p)) {
|
||||||
|
f += base * (*p - '0');
|
||||||
|
base /= 10.0;
|
||||||
|
++p;
|
||||||
|
++n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
d += f * sign;
|
||||||
|
a = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* exponential part */
|
||||||
|
if ((*p == 'E') || (*p == 'e')) {
|
||||||
|
int e = 0;
|
||||||
|
++p;
|
||||||
|
|
||||||
|
sign = 1;
|
||||||
|
if (*p == '-') {
|
||||||
|
sign = -1;
|
||||||
|
++p;
|
||||||
|
} else if (*p == '+')
|
||||||
|
++p;
|
||||||
|
|
||||||
|
if (is_digit(*p)) {
|
||||||
|
while (*p == '0') ++p;
|
||||||
|
e = (int) (*p++ - '0');
|
||||||
|
while (*p && is_digit(*p)) {
|
||||||
|
e = e * 10 + (int) (*p - '0');
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
e *= sign;
|
||||||
|
} else if (!is_digit(*(a - 1))) {
|
||||||
|
a = str;
|
||||||
|
goto done;
|
||||||
|
} else if (*p == 0)
|
||||||
|
goto done;
|
||||||
|
|
||||||
|
if (d == 2.2250738585072011 && e == -308) {
|
||||||
|
d = 0.0;
|
||||||
|
a = p;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
if (d == 2.2250738585072012 && e <= -308) {
|
||||||
|
d *= 1.0e-308;
|
||||||
|
a = p;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
for (i = 0; i < 10; i++) d *= 10;
|
||||||
|
}
|
||||||
|
a = p;
|
||||||
|
} else if (p > str && !is_digit(*(p - 1))) {
|
||||||
|
a = str;
|
||||||
|
goto done;
|
||||||
|
}
|
||||||
|
|
||||||
|
done:
|
||||||
|
if (end) *end = (char *) a;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_MERGE
|
||||||
|
int mjson_merge(const char *s, int n, const char *s2, int n2,
|
||||||
|
mjson_print_fn_t fn, void *userdata) {
|
||||||
|
int koff, klen, voff, vlen, t, t2, k, off = 0, len = 0, comma = 0;
|
||||||
|
if (n < 2) return len;
|
||||||
|
len += fn("{", 1, userdata);
|
||||||
|
while ((off = mjson_next(s, n, off, &koff, &klen, &voff, &vlen, &t)) != 0) {
|
||||||
|
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||||
|
char path[klen + 1];
|
||||||
|
#else
|
||||||
|
char *path = (char *) alloca(klen + 1);
|
||||||
|
#endif
|
||||||
|
const char *val;
|
||||||
|
memcpy(path, "$.", 2);
|
||||||
|
memcpy(path + 2, s + koff + 1, klen - 2);
|
||||||
|
path[klen] = '\0';
|
||||||
|
if ((t2 = mjson_find(s2, n2, path, &val, &k)) != MJSON_TOK_INVALID) {
|
||||||
|
if (t2 == MJSON_TOK_NULL) continue; // null deletes the key
|
||||||
|
} else {
|
||||||
|
val = s + voff; // Key is not found in the update. Copy the old value.
|
||||||
|
}
|
||||||
|
if (comma) len += fn(",", 1, userdata);
|
||||||
|
len += fn(s + koff, klen, userdata);
|
||||||
|
len += fn(":", 1, userdata);
|
||||||
|
if (t == MJSON_TOK_OBJECT && t2 == MJSON_TOK_OBJECT) {
|
||||||
|
len += mjson_merge(s + voff, vlen, val, k, fn, userdata);
|
||||||
|
} else {
|
||||||
|
if (t2 != MJSON_TOK_INVALID) vlen = k;
|
||||||
|
len += fn(val, vlen, userdata);
|
||||||
|
}
|
||||||
|
comma = 1;
|
||||||
|
}
|
||||||
|
// Add missing keys
|
||||||
|
off = 0;
|
||||||
|
while ((off = mjson_next(s2, n2, off, &koff, &klen, &voff, &vlen, &t)) != 0) {
|
||||||
|
#if !defined(_MSC_VER) || _MSC_VER >= 1700
|
||||||
|
char path[klen + 1];
|
||||||
|
#else
|
||||||
|
char *path = (char *) alloca(klen + 1);
|
||||||
|
#endif
|
||||||
|
const char *val;
|
||||||
|
if (t == MJSON_TOK_NULL) continue;
|
||||||
|
memcpy(path, "$.", 2);
|
||||||
|
memcpy(path + 2, s2 + koff + 1, klen - 2);
|
||||||
|
path[klen] = '\0';
|
||||||
|
if (mjson_find(s, n, path, &val, &vlen) != MJSON_TOK_INVALID) continue;
|
||||||
|
if (comma) len += fn(",", 1, userdata);
|
||||||
|
len += fn(s2 + koff, klen, userdata);
|
||||||
|
len += fn(":", 1, userdata);
|
||||||
|
len += fn(s2 + voff, vlen, userdata);
|
||||||
|
comma = 1;
|
||||||
|
}
|
||||||
|
len += fn("}", 1, userdata);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_MERGE
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRETTY
|
||||||
|
struct prettydata {
|
||||||
|
int level;
|
||||||
|
int len;
|
||||||
|
int prev;
|
||||||
|
const char *pad;
|
||||||
|
int padlen;
|
||||||
|
mjson_print_fn_t fn;
|
||||||
|
void *userdata;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int pretty_cb(int ev, const char *s, int off, int len, void *ud) {
|
||||||
|
struct prettydata *d = (struct prettydata *) ud;
|
||||||
|
int i;
|
||||||
|
switch (ev) {
|
||||||
|
case '{':
|
||||||
|
case '[':
|
||||||
|
d->level++;
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
case '}':
|
||||||
|
case ']':
|
||||||
|
d->level--;
|
||||||
|
if (d->prev != '[' && d->prev != '{' && d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
case ',':
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
if (d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ':':
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
if (d->padlen > 0) d->len += d->fn(" ", 1, d->userdata);
|
||||||
|
break;
|
||||||
|
case MJSON_TOK_KEY:
|
||||||
|
if (d->prev == '{' && d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
if (d->prev == '[' && d->padlen > 0) {
|
||||||
|
d->len += d->fn("\n", 1, d->userdata);
|
||||||
|
for (i = 0; i < d->level; i++)
|
||||||
|
d->len += d->fn(d->pad, d->padlen, d->userdata);
|
||||||
|
}
|
||||||
|
d->len += d->fn(s + off, len, d->userdata);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
d->prev = ev;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mjson_pretty(const char *s, int n, const char *pad, mjson_print_fn_t fn,
|
||||||
|
void *userdata) {
|
||||||
|
struct prettydata d = {0, 0, 0, pad, strlen(pad), fn, userdata};
|
||||||
|
if (mjson(s, n, pretty_cb, &d) < 0) return -1;
|
||||||
|
return d.len;
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_PRETTY
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_RPC
|
||||||
|
struct jsonrpc_ctx jsonrpc_default_context;
|
||||||
|
struct jsonrpc_userdata {
|
||||||
|
mjson_print_fn_t fn;
|
||||||
|
void *fndata;
|
||||||
|
};
|
||||||
|
|
||||||
|
int mjson_globmatch(const char *s1, int n1, const char *s2, int n2) {
|
||||||
|
int i = 0, j = 0, ni = 0, nj = 0;
|
||||||
|
while (i < n1 || j < n2) {
|
||||||
|
if (i < n1 && j < n2 && (s1[i] == '?' || s2[j] == s1[i])) {
|
||||||
|
i++, j++;
|
||||||
|
} else if (i < n1 && (s1[i] == '*' || s1[i] == '#')) {
|
||||||
|
ni = i, nj = j + 1, i++;
|
||||||
|
} else if (nj > 0 && nj <= n2 && (s1[i - 1] == '#' || s2[j] != '/')) {
|
||||||
|
i = ni, j = nj;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int jsonrpc_printer(const char *buf, int len, void *userdata) {
|
||||||
|
struct jsonrpc_userdata *u = (struct jsonrpc_userdata *) userdata;
|
||||||
|
return u->fn(buf, len, u->fndata);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_errorv(struct jsonrpc_request *r, int code,
|
||||||
|
const char *message, const char *data_fmt,
|
||||||
|
va_list ap) {
|
||||||
|
if (r->id_len == 0) return;
|
||||||
|
mjson_printf(r->fn, r->fndata,
|
||||||
|
"{\"id\":%.*s,\"error\":{\"code\":%d,\"message\":%Q", r->id_len,
|
||||||
|
r->id, code, message == NULL ? "" : message);
|
||||||
|
if (data_fmt != NULL) {
|
||||||
|
mjson_printf(r->fn, r->fndata, ",\"data\":");
|
||||||
|
mjson_vprintf(r->fn, r->fndata, data_fmt, ap);
|
||||||
|
}
|
||||||
|
mjson_printf(r->fn, r->fndata, "}}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_error(struct jsonrpc_request *r, int code,
|
||||||
|
const char *message, const char *data_fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, data_fmt);
|
||||||
|
jsonrpc_return_errorv(r, code, message, data_fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_successv(struct jsonrpc_request *r, const char *result_fmt,
|
||||||
|
va_list ap) {
|
||||||
|
if (r->id_len == 0) return;
|
||||||
|
mjson_printf(r->fn, r->fndata, "{\"id\":%.*s,\"result\":", r->id_len, r->id);
|
||||||
|
if (result_fmt != NULL) {
|
||||||
|
mjson_vprintf(r->fn, r->fndata, result_fmt, ap);
|
||||||
|
} else {
|
||||||
|
mjson_printf(r->fn, r->fndata, "%s", "null");
|
||||||
|
}
|
||||||
|
mjson_printf(r->fn, r->fndata, "}\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
|
||||||
|
...) {
|
||||||
|
va_list ap;
|
||||||
|
va_start(ap, result_fmt);
|
||||||
|
jsonrpc_return_successv(r, result_fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *req, int req_sz,
|
||||||
|
mjson_print_fn_t fn, void *fndata) {
|
||||||
|
const char *result = NULL, *error = NULL;
|
||||||
|
int result_sz = 0, error_sz = 0;
|
||||||
|
struct jsonrpc_method *m = NULL;
|
||||||
|
struct jsonrpc_userdata d;
|
||||||
|
struct jsonrpc_request r = {req, req_sz, 0, 0, 0, 0, 0,
|
||||||
|
0, &jsonrpc_printer, NULL, NULL};
|
||||||
|
|
||||||
|
d.fn = fn;
|
||||||
|
d.fndata = fndata;
|
||||||
|
r.fndata = &d;
|
||||||
|
|
||||||
|
// Is is a response frame?
|
||||||
|
mjson_find(req, req_sz, "$.result", &result, &result_sz);
|
||||||
|
if (result == NULL) mjson_find(req, req_sz, "$.error", &error, &error_sz);
|
||||||
|
if (result_sz > 0 || error_sz > 0) {
|
||||||
|
if (ctx->response_cb != NULL) ctx->response_cb(req, req_sz, ctx->userdata);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method must exist and must be a string
|
||||||
|
if (mjson_find(req, req_sz, "$.method", &r.method, &r.method_len) !=
|
||||||
|
MJSON_TOK_STRING) {
|
||||||
|
mjson_printf(fn, fndata, "{\"error\":{\"code\":-32700,\"message\":%.*Q}}\n",
|
||||||
|
req_sz, req);
|
||||||
|
ctx->in_len = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id and params are optional
|
||||||
|
mjson_find(req, req_sz, "$.id", &r.id, &r.id_len);
|
||||||
|
mjson_find(req, req_sz, "$.params", &r.params, &r.params_len);
|
||||||
|
|
||||||
|
for (m = ctx->methods; m != NULL; m = m->next) {
|
||||||
|
if (mjson_globmatch(m->method, m->method_sz, r.method + 1,
|
||||||
|
r.method_len - 2) > 0) {
|
||||||
|
if (r.params == NULL) r.params = "";
|
||||||
|
r.userdata = m->cbdata;
|
||||||
|
m->cb(&r);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (m == NULL) {
|
||||||
|
jsonrpc_return_error(&r, JSONRPC_ERROR_NOT_FOUND, "method not found", NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int jsonrpc_print_methods(mjson_print_fn_t fn, void *fndata,
|
||||||
|
va_list *ap) {
|
||||||
|
struct jsonrpc_ctx *ctx = va_arg(*ap, struct jsonrpc_ctx *);
|
||||||
|
struct jsonrpc_method *m;
|
||||||
|
int len = 0;
|
||||||
|
for (m = ctx->methods; m != NULL; m = m->next) {
|
||||||
|
if (m != ctx->methods) len += mjson_print_buf(fn, fndata, ",", 1);
|
||||||
|
len += mjson_print_str(fn, fndata, m->method, strlen(m->method));
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void rpclist(struct jsonrpc_request *r) {
|
||||||
|
jsonrpc_return_success(r, "[%M]", jsonrpc_print_methods, r->userdata);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t response_cb,
|
||||||
|
void *userdata) {
|
||||||
|
ctx->response_cb = response_cb;
|
||||||
|
ctx->userdata = userdata;
|
||||||
|
jsonrpc_ctx_export(ctx, MJSON_RPC_LIST_NAME, rpclist, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_ctx_process_byte(struct jsonrpc_ctx *ctx, unsigned char ch,
|
||||||
|
mjson_print_fn_t fn, void *p) {
|
||||||
|
if (ctx->in_len >= (int) sizeof(ctx->in)) ctx->in_len = 0; // Overflow
|
||||||
|
if (ch == '\n') { // If new line, parse frame
|
||||||
|
if (ctx->in_len > 1) jsonrpc_ctx_process(ctx, ctx->in, ctx->in_len, fn, p);
|
||||||
|
ctx->in_len = 0;
|
||||||
|
} else {
|
||||||
|
ctx->in[ctx->in_len] = ch; // Append to the buffer
|
||||||
|
ctx->in_len++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void jsonrpc_init(mjson_print_fn_t response_cb, void *userdata) {
|
||||||
|
jsonrpc_ctx_init(&jsonrpc_default_context, response_cb, userdata);
|
||||||
|
}
|
||||||
|
#endif // MJSON_ENABLE_RPC
|
||||||
@@ -0,0 +1,220 @@
|
|||||||
|
// Copyright (c) 2018-2020 Cesanta Software Limited
|
||||||
|
// All rights reserved
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files (the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions:
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
|
||||||
|
#ifndef MJSON_H
|
||||||
|
#define MJSON_H
|
||||||
|
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_PRINT
|
||||||
|
#define MJSON_ENABLE_PRINT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_RPC
|
||||||
|
#define MJSON_ENABLE_RPC 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_BASE64
|
||||||
|
#define MJSON_ENABLE_BASE64 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_MERGE
|
||||||
|
#define MJSON_ENABLE_MERGE 0
|
||||||
|
#elif MJSON_ENABLE_MERGE
|
||||||
|
#define MJSON_ENABLE_NEXT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_PRETTY
|
||||||
|
#define MJSON_ENABLE_PRETTY 0
|
||||||
|
#elif MJSON_ENABLE_PRETTY
|
||||||
|
#define MJSON_ENABLE_NEXT 1
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_ENABLE_NEXT
|
||||||
|
#define MJSON_ENABLE_NEXT 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef MJSON_RPC_IN_BUF_SIZE
|
||||||
|
#define MJSON_RPC_IN_BUF_SIZE 256
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MJSON_RPC_LIST_NAME
|
||||||
|
#define MJSON_RPC_LIST_NAME "rpc.list"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum {
|
||||||
|
MJSON_ERROR_INVALID_INPUT = -1,
|
||||||
|
MJSON_ERROR_TOO_DEEP = -2,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum mjson_tok {
|
||||||
|
MJSON_TOK_INVALID = 0,
|
||||||
|
MJSON_TOK_KEY = 1,
|
||||||
|
MJSON_TOK_STRING = 11,
|
||||||
|
MJSON_TOK_NUMBER = 12,
|
||||||
|
MJSON_TOK_TRUE = 13,
|
||||||
|
MJSON_TOK_FALSE = 14,
|
||||||
|
MJSON_TOK_NULL = 15,
|
||||||
|
MJSON_TOK_ARRAY = 91,
|
||||||
|
MJSON_TOK_OBJECT = 123,
|
||||||
|
};
|
||||||
|
#define MJSON_TOK_IS_VALUE(t) ((t) > 10 && (t) < 20)
|
||||||
|
|
||||||
|
typedef int (*mjson_cb_t)(int ev, const char *s, int off, int len, void *ud);
|
||||||
|
|
||||||
|
#ifndef MJSON_MAX_DEPTH
|
||||||
|
#define MJSON_MAX_DEPTH 20
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int mjson(const char *s, int len, mjson_cb_t cb, void *ud);
|
||||||
|
enum mjson_tok mjson_find(const char *s, int len, const char *jp,
|
||||||
|
const char **tokptr, int *toklen);
|
||||||
|
int mjson_get_number(const char *s, int len, const char *path, double *v);
|
||||||
|
int mjson_get_bool(const char *s, int len, const char *path, int *v);
|
||||||
|
int mjson_get_string(const char *s, int len, const char *path, char *to, int n);
|
||||||
|
int mjson_get_hex(const char *s, int len, const char *path, char *to, int n);
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_NEXT
|
||||||
|
int mjson_next(const char *s, int n, int off, int *koff, int *klen, int *voff,
|
||||||
|
int *vlen, int *vtype);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_BASE64
|
||||||
|
int mjson_get_base64(const char *s, int len, const char *path, char *to, int n);
|
||||||
|
int mjson_base64_dec(const char *src, int n, char *dst, int dlen);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRINT
|
||||||
|
typedef int (*mjson_print_fn_t)(const char *buf, int len, void *userdata);
|
||||||
|
typedef int (*mjson_vprint_fn_t)(mjson_print_fn_t, void *, va_list *);
|
||||||
|
|
||||||
|
struct mjson_fixedbuf {
|
||||||
|
char *ptr;
|
||||||
|
int size, len;
|
||||||
|
};
|
||||||
|
|
||||||
|
int mjson_printf(mjson_print_fn_t, void *, const char *fmt, ...);
|
||||||
|
int mjson_vprintf(mjson_print_fn_t, void *, const char *fmt, va_list ap);
|
||||||
|
int mjson_print_str(mjson_print_fn_t, void *, const char *s, int len);
|
||||||
|
int mjson_print_int(mjson_print_fn_t, void *, int value, int is_signed);
|
||||||
|
int mjson_print_long(mjson_print_fn_t, void *, long value, int is_signed);
|
||||||
|
int mjson_print_buf(mjson_print_fn_t fn, void *, const char *buf, int len);
|
||||||
|
|
||||||
|
int mjson_print_null(const char *ptr, int len, void *userdata);
|
||||||
|
int mjson_print_file(const char *ptr, int len, void *userdata);
|
||||||
|
int mjson_print_fixed_buf(const char *ptr, int len, void *userdata);
|
||||||
|
int mjson_print_dynamic_buf(const char *ptr, int len, void *userdata);
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_PRETTY
|
||||||
|
int mjson_pretty(const char *, int, const char *, mjson_print_fn_t, void *);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_MERGE
|
||||||
|
int mjson_merge(const char *, int, const char *, int, mjson_print_fn_t, void *);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // MJSON_ENABLE_PRINT
|
||||||
|
|
||||||
|
#if MJSON_ENABLE_RPC
|
||||||
|
|
||||||
|
void jsonrpc_init(mjson_print_fn_t, void *userdata);
|
||||||
|
int mjson_globmatch(const char *s1, int n1, const char *s2, int n2);
|
||||||
|
|
||||||
|
struct jsonrpc_request {
|
||||||
|
const char *frame; // Points to the whole frame
|
||||||
|
int frame_len; // Frame length
|
||||||
|
const char *params; // Points to the "params" in the request frame
|
||||||
|
int params_len; // Length of the "params"
|
||||||
|
const char *id; // Points to the "id" in the request frame
|
||||||
|
int id_len; // Length of the "id"
|
||||||
|
const char *method; // Points to the "method" in the request frame
|
||||||
|
int method_len; // Length of the "method"
|
||||||
|
mjson_print_fn_t fn; // Printer function
|
||||||
|
void *fndata; // Printer function data
|
||||||
|
void *userdata; // Callback's user data as specified at export time
|
||||||
|
};
|
||||||
|
|
||||||
|
struct jsonrpc_method {
|
||||||
|
const char *method;
|
||||||
|
int method_sz;
|
||||||
|
void (*cb)(struct jsonrpc_request *);
|
||||||
|
void *cbdata;
|
||||||
|
struct jsonrpc_method *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Main RPC context, stores current request information and a list of
|
||||||
|
// exported RPC methods.
|
||||||
|
struct jsonrpc_ctx {
|
||||||
|
struct jsonrpc_method *methods;
|
||||||
|
void *userdata;
|
||||||
|
mjson_print_fn_t response_cb;
|
||||||
|
int in_len;
|
||||||
|
char in[MJSON_RPC_IN_BUF_SIZE];
|
||||||
|
};
|
||||||
|
|
||||||
|
// Registers function fn under the given name within the given RPC context
|
||||||
|
#define jsonrpc_ctx_export(ctx, name, fn, ud) \
|
||||||
|
do { \
|
||||||
|
static struct jsonrpc_method m = {(name), sizeof(name) - 1, (fn), 0, 0}; \
|
||||||
|
m.cbdata = (ud); \
|
||||||
|
m.next = (ctx)->methods; \
|
||||||
|
(ctx)->methods = &m; \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t, void *);
|
||||||
|
void jsonrpc_return_error(struct jsonrpc_request *r, int code,
|
||||||
|
const char *message, const char *data_fmt, ...);
|
||||||
|
void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
|
||||||
|
...);
|
||||||
|
void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *req, int req_sz,
|
||||||
|
mjson_print_fn_t fn, void *fndata);
|
||||||
|
void jsonrpc_ctx_process_byte(struct jsonrpc_ctx *ctx, unsigned char ch,
|
||||||
|
mjson_print_fn_t fn, void *fndata);
|
||||||
|
|
||||||
|
extern struct jsonrpc_ctx jsonrpc_default_context;
|
||||||
|
|
||||||
|
#define jsonrpc_export(name, fn, ud) \
|
||||||
|
jsonrpc_ctx_export(&jsonrpc_default_context, (name), (fn), (ud))
|
||||||
|
|
||||||
|
#define jsonrpc_process(buf, len, fn, data) \
|
||||||
|
jsonrpc_ctx_process(&jsonrpc_default_context, (buf), (len), (fn), (data))
|
||||||
|
|
||||||
|
#define jsonrpc_process_byte(x, fn, data) \
|
||||||
|
jsonrpc_ctx_process_byte(&jsonrpc_default_context, (x), (fn), (data))
|
||||||
|
|
||||||
|
#define JSONRPC_ERROR_INVALID -32700 /* Invalid JSON was received */
|
||||||
|
#define JSONRPC_ERROR_NOT_FOUND -32601 /* The method does not exist */
|
||||||
|
#define JSONRPC_ERROR_BAD_PARAMS -32602 /* Invalid params passed */
|
||||||
|
#define JSONRPC_ERROR_INTERNAL -32603 /* Internal JSON-RPC error */
|
||||||
|
|
||||||
|
#endif // MJSON_ENABLE_RPC
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif // MJSON_H
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
const {h, render, Component} = preact;
|
||||||
|
const html = htm.bind(h);
|
||||||
|
|
||||||
|
const Info = () => html`<div id="info">
|
||||||
|
This dashboard shows values kept in server memory. Many clients
|
||||||
|
can open this page. As soon as any client changes any value,
|
||||||
|
all clients update automatically.
|
||||||
|
<br/><br/>
|
||||||
|
The JS code that watches state changes, reconnects on network failures.
|
||||||
|
That means if server restarts, dashboard on all connected clients
|
||||||
|
refresh automatically.
|
||||||
|
<br/><br/>
|
||||||
|
|
||||||
|
You can use <code>curl</code> command-line utility:
|
||||||
|
<br/><code>curl localhost:8000/api/config/get</code>
|
||||||
|
<br/><code>curl localhost:8000/api/config/watch</code>
|
||||||
|
<br/><code>curl localhost:8000/api/config/set -d '{"a":123}'</code>
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
class Dashboard extends Component {
|
||||||
|
state = {value1: null, value2: null};
|
||||||
|
componentDidMount() {
|
||||||
|
axios.get('/api/config/get')
|
||||||
|
.then(r => this.setState(r.data))
|
||||||
|
.catch(e => console.log(e));
|
||||||
|
var self = this;
|
||||||
|
var f = function(reader) {
|
||||||
|
return reader.read().then(function(result) {
|
||||||
|
var data = String.fromCharCode.apply(null, result.value);
|
||||||
|
self.setState(JSON.parse(data));
|
||||||
|
// console.log(JSON.parse(data));
|
||||||
|
if (!result.done) return f(reader);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
fetch('/api/config/watch')
|
||||||
|
.then(r => r.body.getReader())
|
||||||
|
.then(f)
|
||||||
|
.catch(e => setTimeout(x => self.componentDidMount(), 1000));
|
||||||
|
}
|
||||||
|
render(props, state) {
|
||||||
|
return html
|
||||||
|
`<div id="dashboard" style="display: flex; justify-content: flex-evenly;">
|
||||||
|
<div style="width:50%;text-align:center;">Value 1 (number):
|
||||||
|
<div style="font-size:140%;"><b>${state.value1}</b></div></div>
|
||||||
|
<div style="width:50%;text-align:center;">Value 2 (string):
|
||||||
|
<div style="font-size:140%;"><b>${state.value2}</b></div></div>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Form extends Component {
|
||||||
|
render(props, state) {
|
||||||
|
const onclick = ev => axios.post(
|
||||||
|
'/api/config/set', {value1: +state.value1, value2: state.value2});
|
||||||
|
// alert(JSON.stringify(state));
|
||||||
|
return html`<div id="form">
|
||||||
|
<div><b>Change values</b></div>
|
||||||
|
<table>
|
||||||
|
<tr><td>Value 1 (number):</td><td><input value=${state.value1}
|
||||||
|
onInput=${linkState(this, 'value1')} /></td></tr>
|
||||||
|
<tr><td>Value 2 (string):</td><td><input value=${state.value2}
|
||||||
|
onInput=${linkState(this, 'value2')}/></td></tr>
|
||||||
|
<tr><td></td>
|
||||||
|
<td><button onClick=${onclick}>Save values</button></td></tr>
|
||||||
|
</table>
|
||||||
|
</div>`
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const App = () => html`<div id="container">
|
||||||
|
<${Info} />
|
||||||
|
<${Dashboard} />
|
||||||
|
<${Form} />
|
||||||
|
</div>`;
|
||||||
|
|
||||||
|
window.onload = () => render(h(App), document.body);
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
/* axios v0.21.1 | (c) 2020 by Matt Zabriskie */
|
||||||
|
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.axios=t():e.axios=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return e[r].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){e.exports=n(1)},function(e,t,n){"use strict";function r(e){var t=new i(e),n=s(i.prototype.request,t);return o.extend(n,i.prototype,t),o.extend(n,t),n}var o=n(2),s=n(3),i=n(4),a=n(22),u=n(10),c=r(u);c.Axios=i,c.create=function(e){return r(a(c.defaults,e))},c.Cancel=n(23),c.CancelToken=n(24),c.isCancel=n(9),c.all=function(e){return Promise.all(e)},c.spread=n(25),c.isAxiosError=n(26),e.exports=c,e.exports.default=c},function(e,t,n){"use strict";function r(e){return"[object Array]"===R.call(e)}function o(e){return"undefined"==typeof e}function s(e){return null!==e&&!o(e)&&null!==e.constructor&&!o(e.constructor)&&"function"==typeof e.constructor.isBuffer&&e.constructor.isBuffer(e)}function i(e){return"[object ArrayBuffer]"===R.call(e)}function a(e){return"undefined"!=typeof FormData&&e instanceof FormData}function u(e){var t;return t="undefined"!=typeof ArrayBuffer&&ArrayBuffer.isView?ArrayBuffer.isView(e):e&&e.buffer&&e.buffer instanceof ArrayBuffer}function c(e){return"string"==typeof e}function f(e){return"number"==typeof e}function p(e){return null!==e&&"object"==typeof e}function d(e){if("[object Object]"!==R.call(e))return!1;var t=Object.getPrototypeOf(e);return null===t||t===Object.prototype}function l(e){return"[object Date]"===R.call(e)}function h(e){return"[object File]"===R.call(e)}function m(e){return"[object Blob]"===R.call(e)}function y(e){return"[object Function]"===R.call(e)}function g(e){return p(e)&&y(e.pipe)}function v(e){return"undefined"!=typeof URLSearchParams&&e instanceof URLSearchParams}function x(e){return e.replace(/^\s*/,"").replace(/\s*$/,"")}function w(){return("undefined"==typeof navigator||"ReactNative"!==navigator.product&&"NativeScript"!==navigator.product&&"NS"!==navigator.product)&&("undefined"!=typeof window&&"undefined"!=typeof document)}function b(e,t){if(null!==e&&"undefined"!=typeof e)if("object"!=typeof e&&(e=[e]),r(e))for(var n=0,o=e.length;n<o;n++)t.call(null,e[n],n,e);else for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.call(null,e[s],s,e)}function E(){function e(e,n){d(t[n])&&d(e)?t[n]=E(t[n],e):d(e)?t[n]=E({},e):r(e)?t[n]=e.slice():t[n]=e}for(var t={},n=0,o=arguments.length;n<o;n++)b(arguments[n],e);return t}function j(e,t,n){return b(t,function(t,r){n&&"function"==typeof t?e[r]=S(t,n):e[r]=t}),e}function C(e){return 65279===e.charCodeAt(0)&&(e=e.slice(1)),e}var S=n(3),R=Object.prototype.toString;e.exports={isArray:r,isArrayBuffer:i,isBuffer:s,isFormData:a,isArrayBufferView:u,isString:c,isNumber:f,isObject:p,isPlainObject:d,isUndefined:o,isDate:l,isFile:h,isBlob:m,isFunction:y,isStream:g,isURLSearchParams:v,isStandardBrowserEnv:w,forEach:b,merge:E,extend:j,trim:x,stripBOM:C}},function(e,t){"use strict";e.exports=function(e,t){return function(){for(var n=new Array(arguments.length),r=0;r<n.length;r++)n[r]=arguments[r];return e.apply(t,n)}}},function(e,t,n){"use strict";function r(e){this.defaults=e,this.interceptors={request:new i,response:new i}}var o=n(2),s=n(5),i=n(6),a=n(7),u=n(22);r.prototype.request=function(e){"string"==typeof e?(e=arguments[1]||{},e.url=arguments[0]):e=e||{},e=u(this.defaults,e),e.method?e.method=e.method.toLowerCase():this.defaults.method?e.method=this.defaults.method.toLowerCase():e.method="get";var t=[a,void 0],n=Promise.resolve(e);for(this.interceptors.request.forEach(function(e){t.unshift(e.fulfilled,e.rejected)}),this.interceptors.response.forEach(function(e){t.push(e.fulfilled,e.rejected)});t.length;)n=n.then(t.shift(),t.shift());return n},r.prototype.getUri=function(e){return e=u(this.defaults,e),s(e.url,e.params,e.paramsSerializer).replace(/^\?/,"")},o.forEach(["delete","get","head","options"],function(e){r.prototype[e]=function(t,n){return this.request(u(n||{},{method:e,url:t,data:(n||{}).data}))}}),o.forEach(["post","put","patch"],function(e){r.prototype[e]=function(t,n,r){return this.request(u(r||{},{method:e,url:t,data:n}))}}),e.exports=r},function(e,t,n){"use strict";function r(e){return encodeURIComponent(e).replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+").replace(/%5B/gi,"[").replace(/%5D/gi,"]")}var o=n(2);e.exports=function(e,t,n){if(!t)return e;var s;if(n)s=n(t);else if(o.isURLSearchParams(t))s=t.toString();else{var i=[];o.forEach(t,function(e,t){null!==e&&"undefined"!=typeof e&&(o.isArray(e)?t+="[]":e=[e],o.forEach(e,function(e){o.isDate(e)?e=e.toISOString():o.isObject(e)&&(e=JSON.stringify(e)),i.push(r(t)+"="+r(e))}))}),s=i.join("&")}if(s){var a=e.indexOf("#");a!==-1&&(e=e.slice(0,a)),e+=(e.indexOf("?")===-1?"?":"&")+s}return e}},function(e,t,n){"use stricLine truncated
|
||||||
|
//# sourceMappingURL=axios.min.map
|
||||||