-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.c
More file actions
2908 lines (2284 loc) · 72.6 KB
/
Copy pathecho.c
File metadata and controls
2908 lines (2284 loc) · 72.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* MIT License
*
* Copyright (c) 2023 Alex Chen
*
* 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 <stdbool.h>
#include <strings.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include "echo.h"
#include "conf.h"
#define SND_CHUNK_LEN_MIN 512
#if ECO_CONF_DEF_SND_CHUNK_LEN < SND_CHUNK_LEN_MIN
#error "Configuration ECO_CONF_DEF_SND_CHUNK_LEN is too small."
#endif
#define ECO_VER_MAJOR_STR "1"
#define ECO_VER_MINOR_STR "0"
#define ECO_VER_MICRO_STR "0"
#define ECO_NAME_STR "ecHo"
#define ECO_USER_AGENT_STR ECO_NAME_STR "/" \
ECO_VER_MAJOR_STR "." \
ECO_VER_MINOR_STR "." \
ECO_VER_MICRO_STR
const char *EcoRes_ToStr(EcoRes res) {
switch (res) {
case EcoRes_Ok: return "Operation success";
case EcoRes_Err: return "Operation failure";
case EcoRes_NoMem: return "No enough memory";
case EcoRes_NotFound: return "Not found";
case EcoRes_BadOpt: return "Bad option";
case EcoRes_BadArg: return "Bad argument";
case EcoRes_Again: return "Try again";
case EcoRes_BadScheme: return "Invalid scheme";
case EcoRes_BadHost: return "Invalid host";
case EcoRes_BadPort: return "Invalid port";
case EcoRes_BadPath: return "Invalid path";
case EcoRes_BadQuery: return "Invalid query";
case EcoRes_BadFmt: return "Invalid format";
case EcoRes_BadChar: return "Invalid character";
case EcoRes_BadStatLine: return "Invalid status line";
case EcoRes_BadHttpVer: return "Invalid HTTP version";
case EcoRes_BadStatCode: return "Invalid status code";
case EcoRes_BadReasonPhase: return "Invalid reason phase";
case EcoRes_BadHdrLine: return "Invalid header line";
case EcoRes_BadHdrKey: return "Invalid header key";
case EcoRes_BadHdrVal: return "Invalid header value";
case EcoRes_BadEmpLine: return "Invalid empty line";
case EcoRes_BadChanOpen: return "Channel open failed";
case EcoRes_BadChanSetOpt: return "Channel set option failed";
case EcoRes_BadChanRead: return "Channel read failed";
case EcoRes_BadChanWrite: return "Channel write failed";
case EcoRes_BadChanClose: return "Channel close failed";
case EcoRes_ReachEnd: return "Reach end";
case EcoRes_NoChanHook: return "No channel hook set";
case EcoRes_NoReq: return "No request set";
default: return "Unknown result";
}
}
const char *EcoHttpVer_ToStr(EcoHttpVer ver) {
switch (ver) {
case EcoHttpVer_0_9: return "0.9";
case EcoHttpVer_1_0: return "1.0";
case EcoHttpVer_1_1: return "1.1";
default: return "1.1";
}
}
const char *EcoHttpMeth_ToStr(EcoHttpMeth meth) {
switch (meth) {
case EcoHttpMeth_Get: return "GET";
case EcoHttpMeth_Post: return "POST";
case EcoHttpMeth_Head: return "HEAD";
case EcoHttpMeth_Put: return "PUT";
default: return "GET";
}
}
EcoHttpRsp *EcoHttpRsp_New(void);
void EcoHttpRsp_Del(EcoHttpRsp *cli);
#define KVP_ARY_INIT_CAP 8
static uint32_t EcoHash_HashBuf(const void *buf, size_t len) {
uint32_t hash = 5381;
uint8_t byte;
for (size_t i = 0; i < len; i++) {
byte = ((uint8_t *)buf)[i];
if (byte >= 'A' &&
byte <= 'Z') {
byte = byte - 'A' + 'a';
}
hash = ((hash << 5) + hash) + byte;
}
return hash;
}
static uint32_t EcoHash_HashStr(const char *str) {
return EcoHash_HashBuf(str, strlen(str));
}
void EcoHdrTab_Init(EcoHdrTab *tab) {
tab->kvpAry = NULL;
tab->kvpCap = 0;
tab->kvpNum = 0;
}
EcoHdrTab *EcoHdrTab_New(void) {
EcoHdrTab *newTab;
newTab = (EcoHdrTab *)malloc(sizeof(EcoHdrTab));
if (newTab == NULL) {
return NULL;
}
EcoHdrTab_Init(newTab);
return newTab;
}
void EcoHdrTab_Deinit(EcoHdrTab *tab) {
if (tab->kvpAry != NULL) {
for (size_t i = 0; i < tab->kvpNum; i++) {
EcoKvp *curKvp = tab->kvpAry + i;
free(curKvp->keyBuf);
free(curKvp->valBuf);
}
free(tab->kvpAry);
tab->kvpAry = NULL;
}
tab->kvpCap = 0;
tab->kvpNum = 0;
}
void EcoHdrTab_Del(EcoHdrTab *tab) {
EcoHdrTab_Deinit(tab);
free(tab);
}
/**
* @brief Convert header key to lowercase.
*/
static void LowHdrKey(char *keyBuf, size_t keyLen) {
for (size_t i = 0; i < keyLen; i++) {
char ch = keyBuf[i];
if (ch >= 'A' &&
ch <= 'Z') {
keyBuf[i] = ch - 'A' + 'a';
}
}
}
/**
* @brief Check if the byte is valid in header key string.
*
* @param byte Byte to be checked.
*/
static bool IsHdrKeyByte(uint8_t byte) {
static const uint8_t lookUpTab[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
return lookUpTab[byte] == 1 ? true : false;
}
/**
* @brief Validate header key string.
*
* @param keyBuf Key string buffer.
* @param keyLen Key string length.
*/
static EcoRes ChkHdrKey(const char *keyBuf, size_t keyLen) {
for (size_t i = 0; i < keyLen; i++) {
if (IsHdrKeyByte(keyBuf[i]) == false) {
return EcoRes_BadHdrKey;
}
}
return EcoRes_Ok;
}
/**
* @brief Check if the byte is valid in header value string.
*
* @param byte Byte to be checked.
*/
static bool IsHdrValByte(uint8_t byte) {
static const uint8_t lookUpTab[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
return lookUpTab[byte] == 1 ? true : false;
}
/**
* @brief Validate header value string.
*
* @param valBuf Value string buffer.
* @param valLen Value string length.
*/
static EcoRes ChkHdrVal(const char *valBuf, size_t valLen) {
for (size_t i = 0; i < valLen; i++) {
if (IsHdrValByte(valBuf[i]) == false) {
return EcoRes_BadHdrVal;
}
}
return EcoRes_Ok;
}
/**
* @brief Try to search a key in the given header table.
*
* @param tab Header table.
* @param keyBuf Key string buffer.
* @param keyLen Key string length.
* @param kvp Key-value pair result.
*/
static EcoRes EcoHdrTab_FindByBufAndLen(EcoHdrTab *tab, const char *keyBuf,
size_t keyLen, EcoKvp **kvp) {
uint32_t keyHash = EcoHash_HashBuf(keyBuf, keyLen);
for (size_t i = 0; i < tab->kvpNum; i++) {
EcoKvp *curKvp = tab->kvpAry + i;
if (keyHash == curKvp->keyHash &&
keyLen == curKvp->keyLen &&
strncasecmp(keyBuf, curKvp->keyBuf, curKvp->keyLen) == 0) {
if (kvp != NULL) {
*kvp = curKvp;
}
return EcoRes_Ok;
}
}
return EcoRes_NotFound;
}
static EcoRes EcoHdrTab_GetNextSlot(EcoHdrTab *tab, EcoKvp **kvp) {
if (tab->kvpAry == NULL) {
tab->kvpAry = (EcoKvp *)malloc(sizeof(EcoKvp) * KVP_ARY_INIT_CAP);
if (tab->kvpAry == NULL) {
return EcoRes_NoMem;
}
tab->kvpCap = KVP_ARY_INIT_CAP;
tab->kvpNum = 0;
*kvp = tab->kvpAry;
} else {
if (tab->kvpNum + 1 > tab->kvpCap) {
tab->kvpAry = (EcoKvp *)realloc(tab->kvpAry, sizeof(EcoKvp) * tab->kvpCap * 2);
if (tab->kvpAry == NULL) {
return EcoRes_NoMem;
}
tab->kvpCap *= 2;
}
*kvp = tab->kvpAry + tab->kvpNum;
}
return EcoRes_Ok;
}
static EcoRes EcoHdrTab_AddByBufAndLen(EcoHdrTab *tab,
const void *keyBuf, size_t keyLen,
const void *valBuf, size_t valLen) {
EcoKvp *kvp;
EcoRes res;
res = EcoHdrTab_FindByBufAndLen(tab, keyBuf, keyLen, &kvp);
if (res == EcoRes_NotFound) {
EcoHdrTab_GetNextSlot(tab, &kvp);
kvp->keyBuf = (char *)malloc(keyLen + 1);
if (kvp->keyBuf == NULL) {
return EcoRes_NoMem;
}
kvp->valBuf = (char *)malloc(valLen + 1);
if (kvp->valBuf == NULL) {
free(kvp->keyBuf);
kvp->keyBuf = NULL;
return EcoRes_NoMem;
}
memcpy(kvp->keyBuf, keyBuf, keyLen);
kvp->keyBuf[keyLen] = '\0';
memcpy(kvp->valBuf, valBuf, valLen);
kvp->valBuf[valLen] = '\0';
kvp->keyLen = keyLen;
kvp->valLen = valLen;
LowHdrKey(kvp->keyBuf, kvp->keyLen);
kvp->keyHash = EcoHash_HashBuf(kvp->keyBuf, kvp->keyLen);;
tab->kvpNum++;
return EcoRes_Ok;
} else {
char *newBuf;
newBuf = (char *)malloc(valLen + 1);
if (newBuf == NULL) {
return EcoRes_NoMem;
}
free(kvp->valBuf);
memcpy(newBuf, valBuf, valLen);
newBuf[valLen] = '\0';
kvp->valBuf = newBuf;
kvp->valLen = valLen;
return EcoRes_Ok;
}
}
EcoRes EcoHdrTab_Add(EcoHdrTab *tab, const char *key, const char *val) {
size_t keyLen = strlen(key);
size_t valLen = strlen(val);
EcoKvp *curKvp;
EcoRes res;
res = ChkHdrKey(key, keyLen);
if (res != EcoRes_Ok) {
return res;
}
res = ChkHdrVal(val, valLen);
if (res != EcoRes_Ok) {
return res;
}
res = EcoHdrTab_AddByBufAndLen(tab, key, keyLen, val, valLen);
if (res != EcoRes_Ok) {
return res;
}
return EcoRes_Ok;
}
EcoRes EcoHdrTab_AddLine(EcoHdrTab *tab, const char *line) {
typedef enum _FsmStat {
FsmStat_1stKeyCh,
FsmStat_OthKeyChOrColon,
FsmStat_1stSpcOr1stValCh,
FsmStat_OthSpcOr1stValCh,
FsmStat_OthValCh,
} FsmStat;
FsmStat fsmStat = FsmStat_1stKeyCh;
size_t lineLen = strlen(line);
const char *colon;
const char *valBuf;
size_t keyLen;
size_t valLen;
EcoRes res;
if (lineLen < 2) {
return EcoRes_BadHdrLine;
}
for (size_t i = 0; i < lineLen; i++) {
uint8_t byte = line[i];
switch (fsmStat) {
case FsmStat_1stKeyCh:
if (IsHdrKeyByte(byte)) {
fsmStat = FsmStat_OthKeyChOrColon;
break;
}
return EcoRes_BadHdrKey;
case FsmStat_OthKeyChOrColon:
if (byte == ':') {
colon = line + i;
fsmStat = FsmStat_1stSpcOr1stValCh;
break;
}
if (IsHdrKeyByte(byte)) {
break;
}
return EcoRes_BadHdrKey;
case FsmStat_1stSpcOr1stValCh:
if (byte == ' ') {
fsmStat = FsmStat_OthSpcOr1stValCh;
break;
}
if (IsHdrValByte(byte)) {
valBuf = line + i;
fsmStat = FsmStat_OthValCh;
break;
}
return EcoRes_BadHdrVal;
case FsmStat_OthSpcOr1stValCh:
if (byte == ' ') {
break;
}
if (IsHdrValByte(byte)) {
valBuf = line + i;
fsmStat = FsmStat_OthValCh;
break;
}
return EcoRes_BadHdrVal;
case FsmStat_OthValCh:
if (IsHdrValByte(byte)) {
break;
}
return EcoRes_BadHdrVal;
}
}
keyLen = (size_t)(colon - line);
switch (fsmStat) {
case FsmStat_1stKeyCh:
case FsmStat_OthKeyChOrColon:
return EcoRes_BadHdrLine;
case FsmStat_1stSpcOr1stValCh:
case FsmStat_OthSpcOr1stValCh:
valBuf = "";
valLen = 0;
break;
case FsmStat_OthValCh:
valLen = lineLen - (valBuf - line);
break;
}
res = EcoHdrTab_AddByBufAndLen(tab, line, keyLen, valBuf, valLen);
if (res != EcoRes_Ok) {
return res;
}
return EcoRes_Ok;
}
EcoRes EcoHdrTab_AddFmt(EcoHdrTab *tab, const char *key, const char *fmt, ...) {
char *valBuf = NULL;
size_t keyLen;
size_t valLen;
size_t bufLen;
va_list args;
EcoRes res;
int ret;
va_start(args, fmt);
ret = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if (ret < 0) {
return EcoRes_BadArg;
}
valLen = (size_t)ret;
bufLen = valLen + 1;
valBuf = (char *)malloc(bufLen);
if (valBuf == NULL) {
return EcoRes_NoMem;
}
va_start(args, fmt);
vsnprintf(valBuf, bufLen, fmt, args);
va_end(args);
keyLen = strlen(key);
res = EcoHdrTab_AddByBufAndLen(tab, key, keyLen, valBuf, valLen);
if (res != EcoRes_Ok) {
goto Finally;
}
res = EcoRes_Ok;
Finally:
if (valBuf != NULL) {
free(valBuf);
}
return res;
}
EcoRes EcoHdrTab_Drop(EcoHdrTab *tab, const char *key) {
EcoKvp *curKvp;
if (EcoHdrTab_Find(tab, key, &curKvp) == EcoRes_NotFound) {
return EcoRes_NotFound;
} else {
free(curKvp->keyBuf);
free(curKvp->valBuf);
memmove(curKvp, curKvp + 1, sizeof(EcoKvp) * (tab->kvpNum - 1));
tab->kvpNum--;
return EcoRes_Ok;
}
}
void EcoHdrTab_Clear(EcoHdrTab *tab) {
EcoHdrTab_Deinit(tab);
}
EcoRes EcoHdrTab_Find(EcoHdrTab *tab, const char *key, EcoKvp **kvp) {
return EcoHdrTab_FindByBufAndLen(tab, key, strlen(key), kvp);
}
void EcoHttpReq_Init(EcoHttpReq *req) {
req->scheme = ECO_CONF_DEF_SCHEME;
req->meth = ECO_CONF_DEF_HTTP_METH;
req->pathBuf = NULL;
req->pathLen = 0;
req->queryBuf = NULL;
req->queryLen = 0;
memcpy(req->chanAddr.addr, ECO_CONF_DEF_IP_ADDR,
sizeof(req->chanAddr.addr));
req->ver = ECO_CONF_DEF_HTTP_VER;
req->hdrTab = NULL;
req->bodyBuf = NULL;
req->bodyLen = 0;
}
EcoHttpReq *EcoHttpReq_New(void) {
EcoHttpReq *newReq;
newReq = (EcoHttpReq *)malloc(sizeof(EcoHttpReq));
if (newReq == NULL) {
return NULL;
}
EcoHttpReq_Init(newReq);
return newReq;
}
void EcoHttpReq_Deinit(EcoHttpReq *req) {
if (req->pathBuf != NULL) {
free(req->pathBuf);
}
if (req->queryBuf != NULL) {
free(req->queryBuf);
}
if (req->hdrTab != NULL) {
EcoHdrTab_Del(req->hdrTab);
}
EcoHttpReq_Init(req);
}
void EcoHttpReq_Del(EcoHttpReq *req) {
EcoHttpReq_Deinit(req);
free(req);
}
#define ECO_URL_SCHEME_MAX_LEN (8 - 1)
#define ECO_URL_SCHEME_BUF_LEN (ECO_URL_SCHEME_MAX_LEN + 1)
#define ECO_URL_PATH_MAX_LEN (1024 - 1)
#define ECO_URL_PATH_BUF_LEN (ECO_URL_PATH_MAX_LEN + 1)
#define ECO_URL_QUERY_MAX_LEN (1024 - 1)
#define ECO_URL_QUERY_BUF_LEN (ECO_URL_PATH_MAX_LEN + 1)
/* URL parsing cache. */
typedef struct _EcoUrlParCac {
/* Scheme (optional). */
bool schemeSet;
char schemeBuf[ECO_URL_SCHEME_BUF_LEN];
size_t schemeLen;
/* IPv4 address. */
uint32_t ipv4Buf[4];
size_t ipv4Len;
/* Port (optional). */
bool portSet;
uint32_t port;
/* Path (optional). */
bool pathSet;
char pathBuf[ECO_URL_PATH_BUF_LEN];
size_t pathLen;
/* Query (optional). */
bool querySet;
char queryBuf[ECO_URL_QUERY_BUF_LEN];
size_t queryLen;
} EcoUrlParCac;
void EcoUrlParCac_Init(EcoUrlParCac *cache) {
cache->schemeSet = false;
memset(cache->schemeBuf, 0, sizeof(cache->schemeBuf));
cache->schemeLen = 0;
memset(cache->ipv4Buf, 0, sizeof(cache->ipv4Buf));
cache->ipv4Len = 0;
cache->portSet = false;
cache->port = 0;
cache->pathSet = false;
memset(cache->pathBuf, 0, sizeof(cache->pathBuf));
cache->pathLen = 0;
cache->querySet = false;
memset(cache->queryBuf, 0, sizeof(cache->queryBuf));
cache->queryLen = 0;
}
EcoUrlParCac *EcoUrlParCac_New(void) {
EcoUrlParCac *newCache;
newCache = (EcoUrlParCac *)malloc(sizeof(EcoUrlParCac));
if (newCache == NULL) {
return NULL;
}
EcoUrlParCac_Init(newCache);
return newCache;
}
void EcoUrlParCac_Deinit(EcoUrlParCac *cache) {
EcoUrlParCac_Init(cache);
}
void EcoUrlParCac_Del(EcoUrlParCac *cache) {
free(cache);
}
EcoRes EcoUrlParCac_ParseUrl(EcoUrlParCac *cache, const char *url) {
typedef enum _FsmStat {
FsmStat_Start,
FsmStat_Scheme1stCh,
FsmStat_SchemeOthCh,
FsmStat_ColonAfterProto,
FsmStat_Slash1AfterProto,
FsmStat_Slash2AfterProto,
FsmStat_Ipv41stDigit,
FsmStat_Ipv4Dot,
FsmStat_Ipv4OthDigit,
FsmStat_ColonAfterHost,
FsmStat_Host1stDigit,
FsmStat_HostOthDigit,
FsmStat_PathSlash,
FsmStat_PathSegCh,
FsmStat_QuesAfterPath,
FsmStat_Query1stCh,
FsmStat_QueryOthCh,
} FsmStat;
FsmStat fsmStat = FsmStat_Start;
size_t urlLen = strlen(url);
for (size_t i = 0; i < urlLen; i++) {
char ch = url[i];
switch (fsmStat) {
case FsmStat_Start:
case FsmStat_Scheme1stCh:
if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z')) {
cache->schemeSet = true;
cache->schemeBuf[0] = ch;
cache->schemeLen = 1;
fsmStat = FsmStat_SchemeOthCh;
break;
}
if (ch >= '0' &&
ch <= '9') {
cache->ipv4Buf[cache->ipv4Len] = ch - '0';
fsmStat = FsmStat_Ipv4OthDigit;
break;
}
return EcoRes_BadChar;
case FsmStat_SchemeOthCh:
if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '+' ||
ch == '-' ||
ch == '.') {
if (cache->schemeLen == ECO_URL_SCHEME_MAX_LEN) {
return EcoRes_BadScheme;
}
cache->schemeBuf[cache->schemeLen] = ch;
cache->schemeLen++;
fsmStat = FsmStat_SchemeOthCh;
break;
}
if (ch == ':') {
cache->schemeBuf[cache->schemeLen] = '\0';
fsmStat = FsmStat_ColonAfterProto;
break;
}
return EcoRes_BadChar;
case FsmStat_ColonAfterProto:
if (ch == '/') {
fsmStat = FsmStat_Slash1AfterProto;
break;
}
return EcoRes_BadChar;
case FsmStat_Slash1AfterProto:
if (ch == '/') {
fsmStat = FsmStat_Slash2AfterProto;
break;
}
return EcoRes_BadChar;
case FsmStat_Slash2AfterProto:
case FsmStat_Ipv41stDigit:
case FsmStat_Ipv4Dot:
if (ch >= '0' &&
ch <= '9') {
cache->ipv4Buf[cache->ipv4Len] = ch - '0';
fsmStat = FsmStat_Ipv4OthDigit;
break;
}
return EcoRes_BadChar;
case FsmStat_Ipv4OthDigit:
if (ch >= '0' &&
ch <= '9') {
cache->ipv4Buf[cache->ipv4Len] *= 10;
cache->ipv4Buf[cache->ipv4Len] += ch - '0';
if (cache->ipv4Buf[cache->ipv4Len] > 255) {
return EcoRes_BadHost;
}
break;
}
if (ch == '.') {
if (cache->ipv4Len >= 3) {
return EcoRes_BadHost;
}
cache->ipv4Len++;
fsmStat = FsmStat_Ipv4Dot;
break;
}
if (ch == ':') {
if (cache->ipv4Len != 3) {
return EcoRes_BadHost;
}
cache->ipv4Len = 4;
cache->portSet = true;
fsmStat = FsmStat_ColonAfterHost;
break;
}
if (ch == '/') {
if (cache->ipv4Len != 3) {
return EcoRes_BadHost;
}
cache->ipv4Len = 4;
cache->pathSet = true;
cache->pathBuf[0] = ch;
cache->pathLen = 1;
fsmStat = FsmStat_PathSegCh;
break;
}
return EcoRes_BadChar;
case FsmStat_ColonAfterHost:
case FsmStat_Host1stDigit:
if (ch >= '0' &&
ch <= '9') {
cache->port = ch - '0';
fsmStat = FsmStat_HostOthDigit;
break;
}
return EcoRes_BadChar;
case FsmStat_HostOthDigit:
if (ch >= '0' &&
ch <= '9') {
cache->port *= 10;
cache->port += ch - '0';
if (cache->port > 65535) {
return EcoRes_BadPort;
}
fsmStat = FsmStat_HostOthDigit;
break;
}
if (ch == '/') {
cache->pathSet = true;
cache->pathBuf[0] = ch;
cache->pathLen = 1;
fsmStat = FsmStat_PathSegCh;
break;
}
return EcoRes_BadChar;
case FsmStat_PathSlash:
if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '-' ||
ch == '_' ||
ch == '.' ||
ch == '!' ||
ch == '~' ||
ch == '*' ||
ch == '\'' ||
ch == '(' ||
ch == ')' ||
ch == ':' ||
ch == '@' ||
ch == '&' ||
ch == '=' ||
ch == '+' ||
ch == '$' ||
ch == ',') {
cache->pathBuf[cache->pathLen] = ch;
cache->pathLen++;
if (cache->pathLen == ECO_URL_PATH_MAX_LEN) {
return EcoRes_BadPath;
}
fsmStat = FsmStat_PathSegCh;
break;
}
return EcoRes_BadChar;
case FsmStat_PathSegCh:
if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ||
(ch >= '0' && ch <= '9') ||
ch == '-' ||
ch == '_' ||
ch == '.' ||
ch == '!' ||
ch == '~' ||
ch == '*' ||
ch == '\'' ||
ch == '(' ||
ch == ')' ||
ch == ':' ||
ch == '@' ||
ch == '&' ||
ch == '=' ||
ch == '+' ||
ch == '$' ||
ch == ',') {
cache->pathBuf[cache->pathLen] = ch;
cache->pathLen++;
if (cache->pathLen == ECO_URL_PATH_MAX_LEN) {
return EcoRes_BadPath;
}
break;
}
if (ch == '/') {
cache->pathBuf[cache->pathLen] = ch;
cache->pathLen++;