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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
use std::collections::HashMap;
use std::str;
#[derive(Debug)]
pub enum BodyContentType {
SinglePart(HashMap<String, String>),
MultiPart(HashMap<String, MultiPartValue>),
}
#[derive(Debug)]
pub struct Message {
pub body: BodyContentType,
pub headers: HashMap<String, HeaderValueParts>,
pub request_line: Line,
}
#[derive(Debug)]
pub struct Line {
pub method: Method,
pub protocol: Protocol,
pub raw: String,
pub request_uri: String,
pub request_uri_base: String,
pub query_arguments: HashMap<String, String>,
pub query_string: String,
}
#[derive(Debug, Eq, PartialEq)]
pub enum Method {
Connect,
Delete,
Get,
Head,
Invalid,
Options,
Patch,
Post,
Put,
Trace,
}
#[derive(Debug)]
pub enum HeaderContentType {
MultiPart(String),
SinglePart,
}
#[derive(Debug)]
pub enum HeaderValuePart {
Single(String),
KeyValue(String, String),
}
#[derive(Debug)]
pub struct HeaderValueParts {
pub parts: Vec<Vec<HeaderValuePart>>,
}
impl HeaderValueParts {
pub fn get_key_value(&self, key: &str) -> Option<String> {
for params_block in self.parts.iter() {
for params_subblock in params_block.iter() {
if let HeaderValuePart::KeyValue(key_value_key, key_value_value) = params_subblock {
if key_value_key == key {
return Some(key_value_value.to_string());
}
}
}
}
None
}
pub fn to_string(&self) -> String {
let mut output = String::new();
let mut params_block_count = 0;
for params_block in self.parts.iter() {
if params_block_count > 0 {
output.push_str("; ");
}
let mut params_subblock_count = 0;
for params_subblock in params_block.iter() {
if params_subblock_count > 0 {
output.push_str(", ");
}
match params_subblock {
HeaderValuePart::Single(string) => {
output.push_str(&string);
}
HeaderValuePart::KeyValue(key, value) => {
output.push_str(&format!("{}={}", key, value).to_string());
}
}
params_subblock_count = params_subblock_count + 1;
}
params_block_count = params_block_count + 1;
}
output
}
}
#[derive(Debug)]
pub struct MultiPartValue {
pub body: Vec<u8>,
pub headers: HashMap<String, HeaderValueParts>,
}
#[derive(Debug, Eq, PartialEq)]
pub enum Protocol {
Invalid,
V1_0,
V1_1,
V2_0,
V0_9,
}
enum ParserSection {
Line,
HeaderFields,
MessageBody,
}
enum MultiPartSection {
End,
EndBoundary,
Skipping,
Start,
StartSuffix,
}
enum ParserMode {
Boundaries(Vec<u8>),
Lines,
}
#[derive(Debug, Eq, PartialEq)]
enum SettingValence {
Optional,
No,
Yes,
}
impl Message {
fn method_has_request_body(method: &Method) -> SettingValence {
match method {
Method::Connect => SettingValence::Yes,
Method::Delete => SettingValence::No,
Method::Get => SettingValence::Optional,
Method::Head => SettingValence::No,
Method::Options => SettingValence::Optional,
Method::Patch => SettingValence::Yes,
Method::Post => SettingValence::Yes,
Method::Put => SettingValence::Yes,
Method::Trace => SettingValence::Yes,
Method::Invalid => SettingValence::Optional,
}
}
fn _method_has_response_body(method: &Method) -> bool {
match method {
Method::Connect => true,
Method::Delete => true,
Method::Get => true,
Method::Head => false,
Method::Options => true,
Method::Patch => true,
Method::Post => true,
Method::Put => true,
Method::Trace => true,
Method::Invalid => true,
}
}
fn _method_is_safe(method: &Method) -> bool {
match method {
Method::Connect => false,
Method::Delete => false,
Method::Get => true,
Method::Head => true,
Method::Options => true,
Method::Patch => false,
Method::Post => false,
Method::Put => false,
Method::Trace => true,
Method::Invalid => true,
}
}
fn _method_is_idempotent(method: &Method) -> bool {
match method {
Method::Connect => false,
Method::Delete => true,
Method::Get => true,
Method::Head => true,
Method::Options => true,
Method::Patch => false,
Method::Post => false,
Method::Put => true,
Method::Trace => true,
Method::Invalid => true,
}
}
fn _method_is_cacheable(method: &Method) -> bool {
match method {
Method::Connect => false,
Method::Delete => false,
Method::Get => true,
Method::Head => true,
Method::Options => false,
Method::Patch => false,
Method::Post => true,
Method::Put => false,
Method::Trace => false,
Method::Invalid => false,
}
}
fn get_query_args_from_multipart_blob(data: &[u8]) -> Option<(String, MultiPartValue)> {
let mut headers: HashMap<String, HeaderValueParts> = HashMap::new();
let mut last_was_carriage_return = false;
let mut index = 0;
let mut start = 0;
for byte in data.iter() {
if byte == &10 && last_was_carriage_return {
last_was_carriage_return = false;
if let Ok(utf8_line) = str::from_utf8(&data[start..index]) {
if utf8_line.trim().is_empty() {
start = index + 1;
break;
} else {
if let Some((header_key, header_value)) =
Message::get_header_field(utf8_line)
{
headers.insert(header_key, header_value);
}
}
start = index + 1;
}
} else if byte == &13 {
last_was_carriage_return = true;
} else {
last_was_carriage_return = false;
}
index = index + 1;
}
let mut name = String::new();
if let Some(content_disposition) = headers.get("Content-Disposition") {
if let Some(content_disposition_name) = content_disposition.get_key_value("name") {
name = content_disposition_name.trim_matches('"').to_string();
}
}
if !name.is_empty() {
let body = data[start..].to_vec();
if !body.is_empty() {
return Some((name, MultiPartValue { body, headers }));
}
}
None
}
fn get_query_args_from_string(subject: &str) -> Option<HashMap<String, String>> {
let mut args: HashMap<String, String> = HashMap::new();
if !subject.is_empty() {
let subject_arguments: Vec<&str> = subject.split("&").collect();
for item in subject_arguments {
let query_arg: Vec<&str> = item.split("=").collect();
if query_arg.len() == 2 {
args.insert(query_arg.get(0)?.to_string(), query_arg.get(1)?.to_string());
} else {
args.insert(query_arg.get(0)?.to_string(), String::from("1"));
}
}
}
if args.len() > 0 {
return Some(args);
}
None
}
pub fn get_protocol_text(protocol: &Protocol) -> String {
match protocol {
Protocol::V0_9 => String::from("HTTP/0.9"),
Protocol::V1_0 => String::from("HTTP/1.0"),
Protocol::V1_1 => String::from("HTTP/1.1"),
Protocol::V2_0 => String::from("HTTP/2.0"),
Protocol::Invalid => String::from("INVALID"),
}
}
pub fn get_message_body(body: &str) -> Option<BodyContentType> {
if let Some(body) = Message::get_query_args_from_string(body) {
return Some(BodyContentType::SinglePart(body));
}
None
}
pub fn get_header_field(line: &str) -> Option<(String, HeaderValueParts)> {
let line = line.trim();
if !line.is_empty() {
let parts: Vec<&str> = line.splitn(2, ":").collect();
if parts.len() == 2 {
let header_key = parts.get(0)?.trim().to_string();
let header_value = parts.get(1)?.trim().to_string();
let mut header_parts: Vec<Vec<HeaderValuePart>> = Vec::new();
let params_blocks: Vec<&str> = header_value.split(";").collect();
for params_block in params_blocks.iter() {
let mut header_value_part: Vec<HeaderValuePart> = Vec::new();
let params_subblocks: Vec<&str> = params_block.split(",").collect();
for params_subblock in params_subblocks.iter() {
let params_subblock_clone = params_subblock.clone();
let params_key_pair: Vec<&str> =
params_subblock_clone.splitn(2, "=").collect();
if params_key_pair.len() == 2 {
let param_key = params_key_pair.get(0)?.trim().to_string();
let param_value = params_key_pair.get(1)?.trim().to_string();
header_value_part
.push(HeaderValuePart::KeyValue(param_key, param_value));
} else {
header_value_part
.push(HeaderValuePart::Single(params_subblock.trim().to_string()));
}
}
header_parts.push(header_value_part);
}
return Some((
header_key,
HeaderValueParts {
parts: header_parts,
},
));
}
}
None
}
pub fn get_request_line(line: &str) -> Option<Line> {
let line = line.trim();
let parts: Vec<&str> = line.split(" ").collect();
if parts.len() == 3 {
let method = match parts.get(0)?.as_ref() {
"CONNECT" => Method::Connect,
"DELETE" => Method::Delete,
"GET" => Method::Get,
"HEAD" => Method::Head,
"OPTIONS" => Method::Options,
"PATCH" => Method::Patch,
"PUT" => Method::Put,
"POST" => Method::Post,
"TRACE" => Method::Trace,
__ => Method::Invalid,
};
let request_uri = parts.get(1)?.to_string();
let request_uri_copy = request_uri.clone();
let mut request_uri_base = request_uri.clone();
let mut query_string = String::new();
let mut query_arguments: HashMap<String, String> = HashMap::new();
let uri_parts: Vec<&str> = request_uri_copy.splitn(2, "?").collect();
if uri_parts.len() == 2 {
request_uri_base = uri_parts.get(0)?.to_string();
query_string = uri_parts.get(1)?.to_string();
if let Some(query_args) = Message::get_query_args_from_string(&query_string) {
query_arguments = query_args;
}
}
let protocol = match parts.get(2)?.as_ref() {
"HTTP/0.9" => Protocol::V0_9,
"HTTP/1.0" => Protocol::V1_0,
"HTTP/1.1" => Protocol::V1_1,
"HTTP/2.0" => Protocol::V2_0,
_ => Protocol::Invalid,
};
if method != Method::Invalid && protocol != Protocol::Invalid {
return Some(Line {
method,
protocol,
raw: line.to_string(),
request_uri,
request_uri_base,
query_arguments,
query_string,
});
}
} else if parts.len() == 1 {
let method = Method::Get;
let request_uri = parts.get(0)?.trim_matches(char::from(0)).to_string();
if !request_uri.is_empty() {
let protocol = Protocol::V0_9;
let request_uri_copy = request_uri.clone();
let mut request_uri_base = request_uri.clone();
let mut query_string = String::new();
let mut query_arguments: HashMap<String, String> = HashMap::new();
let uri_parts: Vec<&str> = request_uri_copy.splitn(2, "?").collect();
if uri_parts.len() == 2 {
request_uri_base = uri_parts.get(0)?.to_string();
query_string = uri_parts.get(1)?.to_string();
if let Some(query_args) = Message::get_query_args_from_string(&query_string) {
query_arguments = query_args;
}
}
return Some(Line {
method,
protocol,
raw: line.to_string(),
request_uri,
request_uri_base,
query_arguments,
query_string,
});
}
}
None
}
pub fn from_tcp_stream(request: &[u8]) -> Option<Message> {
let mut message = Message {
body: BodyContentType::SinglePart(HashMap::new()),
headers: HashMap::new(),
request_line: Line {
method: Method::Invalid,
protocol: Protocol::Invalid,
raw: String::new(),
request_uri: String::new(),
request_uri_base: String::new(),
query_arguments: HashMap::new(),
query_string: String::new(),
},
};
let mut start = 0;
let mut start_boundary = 0;
let mut start_data = 0;
let mut section = ParserSection::Line;
let mut end = 0;
let mut end_data = 0;
let last_index = match request.len() {
0 => 0,
_ => request.len() - 1,
};
let mut last_was_carriage_return = false;
let mut parser_mode = ParserMode::Lines;
let mut multipart_section = MultiPartSection::Start;
for byte in request.iter() {
match parser_mode {
ParserMode::Boundaries(ref boundary) => {
match multipart_section {
MultiPartSection::Skipping => {
if byte == &13 {
last_was_carriage_return = true;
} else if byte == &10 && last_was_carriage_return {
multipart_section = MultiPartSection::Start;
start_boundary = end + 1;
last_was_carriage_return = false;
} else if byte == &0 {
break;
} else {
last_was_carriage_return = false;
}
}
MultiPartSection::Start => {
if let Some(boundary_byte) = boundary.get(end - start_boundary) {
if boundary_byte == byte {
if end - start_boundary + 1 == boundary.len() {
multipart_section = MultiPartSection::StartSuffix;
}
} else if byte == &45 && start_boundary < end {
if let Some(boundary_byte) =
boundary.get(end - start_boundary - 1)
{
if boundary_byte == byte {
start_boundary = start_boundary + 1;
} else {
multipart_section = MultiPartSection::Skipping;
}
} else {
multipart_section = MultiPartSection::Skipping;
}
} else {
multipart_section = MultiPartSection::Skipping;
}
} else if byte == &0 {
break;
} else {
multipart_section = MultiPartSection::Skipping;
}
}
MultiPartSection::StartSuffix => {
if byte == &13 {
last_was_carriage_return = true;
} else if byte == &10 && last_was_carriage_return {
multipart_section = MultiPartSection::End;
last_was_carriage_return = false;
start_data = end;
} else if byte == &0 {
break;
} else {
last_was_carriage_return = false;
multipart_section = MultiPartSection::Skipping;
}
}
MultiPartSection::End => {
if byte == &13 {
last_was_carriage_return = true;
} else if byte == &10 && last_was_carriage_return {
multipart_section = MultiPartSection::EndBoundary;
last_was_carriage_return = false;
end_data = end - 1;
start_boundary = end + 1;
} else if byte == &0 {
break;
}
}
MultiPartSection::EndBoundary => {
if let Some(boundary_byte) = boundary.get(end - start_boundary) {
if boundary_byte == byte {
if end - start_boundary + 1 == boundary.len() {
multipart_section = MultiPartSection::Skipping;
if start_data > 0
&& start_data < end_data
&& end_data < request.len()
{
let data = &request[start_data..end_data];
if let Some((query_key, query_value)) =
Message::get_query_args_from_multipart_blob(&data)
{
if let BodyContentType::MultiPart(ref mut values) =
message.body
{
values.insert(query_key, query_value);
}
}
}
}
} else if byte == &45 && start_boundary < end {
if let Some(boundary_byte) =
boundary.get(end - start_boundary - 1)
{
if boundary_byte == byte {
start_boundary = start_boundary + 1;
} else {
multipart_section = MultiPartSection::End;
}
} else {
multipart_section = MultiPartSection::End;
}
} else {
multipart_section = MultiPartSection::End;
}
} else if byte == &0 {
break;
} else {
multipart_section = MultiPartSection::End;
}
}
}
}
ParserMode::Lines => {
if byte == &13 {
last_was_carriage_return = true;
} else if byte == &10 && last_was_carriage_return {
let clean_end = end - 1;
if let Ok(utf8_line) = str::from_utf8(&request[start..clean_end]) {
Message::parse_line(
&utf8_line,
&mut section,
&mut message,
&mut parser_mode,
);
start = end + 1;
start_boundary = end + 1;
}
last_was_carriage_return = false;
} else if byte == &0 || end == last_index {
let clean_end = match byte {
&0 => end,
_ => end + 1,
};
if let Ok(utf8_line) = str::from_utf8(&request[start..clean_end]) {
Message::parse_line(
&utf8_line,
&mut section,
&mut message,
&mut parser_mode,
);
}
break;
} else {
last_was_carriage_return = false;
}
}
}
end = end + 1;
}
if message.request_line.method != Method::Invalid
&& message.request_line.protocol != Protocol::Invalid
{
return Some(message);
}
None
}
fn parse_line(
line: &str,
section: &mut ParserSection,
message: &mut Message,
parser_mode: &mut ParserMode,
) {
match section {
ParserSection::Line => {
if let Some(request_line_temp) = Message::get_request_line(line) {
message.request_line = request_line_temp;
*section = ParserSection::HeaderFields;
}
}
ParserSection::HeaderFields => {
if line.trim().is_empty() {
if let Some(content_type_header) = message.headers.get("Content-Type") {
if let Some(boundary) = content_type_header.get_key_value("boundary") {
*parser_mode = ParserMode::Boundaries(boundary.as_bytes().to_vec());
message.body = BodyContentType::MultiPart(HashMap::new());
}
}
if Message::method_has_request_body(&message.request_line.method)
!= SettingValence::No
{
*section = ParserSection::MessageBody;
}
} else {
if let Some((header_key, header_value)) = Message::get_header_field(line) {
message.headers.insert(header_key, header_value);
}
}
}
ParserSection::MessageBody => {
if !line.is_empty() {
if let Some(body_args) = Message::get_message_body(line) {
message.body = body_args;
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_message_body_single_part() {
let response = Message::get_message_body("random=abc&hej=def&def");
assert!(response.is_some());
let response_unwrapped = response.unwrap();
if let BodyContentType::SinglePart(response_unwrapped) = response_unwrapped {
assert_eq!(
response_unwrapped
.get(&"random".to_string())
.unwrap()
.to_string(),
"abc".to_string()
);
assert_eq!(
response_unwrapped
.get(&"hej".to_string())
.unwrap()
.to_string(),
"def".to_string()
);
assert_eq!(
response_unwrapped
.get(&"def".to_string())
.unwrap()
.to_string(),
"1".to_string()
);
assert!(response_unwrapped.get(&"defs".to_string()).is_none());
}
let response = Message::get_message_body("");
assert!(response.is_none());
}
#[test]
fn test_get_query_args_from_multipart_blob() {
let response = Message::get_query_args_from_multipart_blob(
b"Content-Disposition: form-data; name=\"losen\"\r\n\r\nabc\n123",
);
assert!(response.is_some());
if let Some((query_key, query_value)) = response {
assert_eq!(query_key, "losen".to_string());
assert_eq!(
query_value
.headers
.get("Content-Disposition")
.unwrap()
.to_string(),
"form-data; name=\"losen\""
);
assert_eq!(query_value.body, b"abc\n123");
} else {
panic!("Expected multipart body but received: {:?}", response);
}
let response = Message::get_query_args_from_multipart_blob(
b"Content-Disposition: form-data; name=\"file\"; filename=\"KeePassXC-2.3.1.dmg.sig\"\r\nContent-Type: application/octet-stream\r\n\r\n
-----BEGIN PGP SIGNATURE-----
iQEzBAABCAAdFiEEweTLo61406/YlPngt6ZvA7WQdqgFAlqfE5MACgkQt6ZvA7WQ
dqgnEAgAjtdbsMPaULGXKX6H+fcsYeGEN8OjiUTNz+StwNDkDxhxB4MT0N0lYZ4L
xUv86kwMdWAaxp8pvVWo6gWXTEM5gWmN302bBxkpbhBl9fnq6WdcCCDGs4GM5vHX
lOrHXWTsK+8ayLNZ0dCcP054srAtMmJHscPiuUYPfvKSgLxl+JxkPC147EktCCzv
5O+2AtQPwIEPuaMewFqP9KjaGOhWgAc0nauIKa0ASt9FXXrexq1EoZnoZ3ZQ0p/w
/otAB2D27yQ4kv+X2Rn94Ky9W0lMT2MYEF+/tQH4aEKsdMBQ7REQtfLGFlEzTMB/
BNUI5YCF3PV9MKr3N53vEVYvkbXLbw==
=LO1E
-----END PGP SIGNATURE-----
");
assert!(response.is_some());
if let Some((query_key, query_value)) = response {
assert_eq!(query_key, "file".to_string());
assert_eq!(
query_value
.headers
.get("Content-Disposition")
.unwrap()
.to_string(),
"form-data; name=\"file\"; filename=\"KeePassXC-2.3.1.dmg.sig\""
);
} else {
panic!("Expected multipart body but received: {:?}", response);
}
let response = Message::get_query_args_from_multipart_blob(
b"okasdokadsokasd oa skoasdk\r\nokadsokasdokoadskods\r\n123123",
);
assert!(response.is_none());
}
#[test]
fn test_get_header_field() {
let response = Message::get_header_field(
"User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0\r\n",
);
assert!(response.is_some());
let (key, value) = response.unwrap();
assert_eq!(key, "User-Agent".to_string());
assert_eq!(
value.to_string(),
"Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0".to_string()
);
let response = Message::get_header_field("Cache-Control: no-cache \r\n");
assert!(response.is_some());
let (key, value) = response.unwrap();
assert_eq!(key, "Cache-Control".to_string());
assert_eq!(value.to_string(), "no-cache".to_string());
let response = Message::get_header_field("Just various text here\r\n");
assert!(response.is_none());
let response = Message::get_header_field("");
assert!(response.is_none());
let response = Message::get_header_field(
"Content-Type: multipart/form-data; boundary=---------------------------208201381313076108731815782760\r\n",
);
assert!(response.is_some());
let (key, value) = response.unwrap();
assert_eq!(key, "Content-Type".to_string());
assert_eq!(value.to_string(), "multipart/form-data; boundary=---------------------------208201381313076108731815782760".to_string());
assert_eq!(
value.get_key_value("boundary").unwrap(),
"---------------------------208201381313076108731815782760".to_string()
);
}
#[test]
fn test_get_request_line() {
let response = Message::get_request_line("POST /random?abc=test HTTP/0.9\r\n");
assert!(response.is_some());
let response_unpacked = response.unwrap();
assert_eq!(response_unpacked.method, Method::Post);
assert_eq!(
response_unpacked.request_uri,
String::from("/random?abc=test")
);
assert_eq!(response_unpacked.request_uri_base, String::from("/random"));
assert_eq!(response_unpacked.query_string, String::from("abc=test"));
assert_eq!(
response_unpacked
.query_arguments
.get(&"abc".to_string())
.unwrap()
.to_string(),
String::from("test")
);
assert_eq!(response_unpacked.protocol, Protocol::V0_9);
let response = Message::get_request_line("GET / HTTP/1.0\r\n");
assert!(response.is_some());
let response_unpacked = response.unwrap();
assert_eq!(response_unpacked.method, Method::Get);
assert_eq!(response_unpacked.request_uri, String::from("/"));
assert_eq!(response_unpacked.request_uri_base, String::from("/"));
assert_eq!(response_unpacked.query_string, String::from(""));
assert_eq!(response_unpacked.protocol, Protocol::V1_0);
let response = Message::get_request_line("HEAD /moradish.html?test&abc=def HTTP/1.1\r\n");
assert!(response.is_some());
let response_unpacked = response.unwrap();
assert_eq!(response_unpacked.method, Method::Head);
assert_eq!(
response_unpacked.request_uri,
String::from("/moradish.html?test&abc=def")
);
assert_eq!(
response_unpacked.request_uri_base,
String::from("/moradish.html")
);
assert_eq!(response_unpacked.query_string, String::from("test&abc=def"));
assert_eq!(
response_unpacked
.query_arguments
.get(&"test".to_string())
.unwrap()
.to_string(),
String::from("1")
);
assert_eq!(
response_unpacked
.query_arguments
.get(&"abc".to_string())
.unwrap()
.to_string(),
String::from("def")
);
assert_eq!(response_unpacked.protocol, Protocol::V1_1);
let response = Message::get_request_line("OPTIONS /random/random2.txt HTTP/2.0\r\n");
assert!(response.is_some());
let response_unpacked = response.unwrap();
assert_eq!(response_unpacked.method, Method::Options);
assert_eq!(
response_unpacked.request_uri,
String::from("/random/random2.txt")
);
assert_eq!(response_unpacked.protocol, Protocol::V2_0);
let response = Message::get_request_line("GET / HTTP/2.2\r\n");
assert!(response.is_none());
}
#[test]
fn test_from_tcp_stream() {
let response = Message::from_tcp_stream(b"GET / HTTP/2.0\r\n");
assert!(response.is_some());
let response_unwrapped = response.expect("GET HTTP2");
assert_eq!(response_unwrapped.request_line.method, Method::Get);
assert_eq!(response_unwrapped.request_line.request_uri, "/".to_string());
assert_eq!(response_unwrapped.request_line.protocol, Protocol::V2_0);
let mut request: Vec<u8> =
b"POST /random HTTP/1.0\r\nAgent: Random browser\r\n\r\ntest=abc".to_vec();
request.push(0);
request.push(0);
let response = Message::from_tcp_stream(&request);
assert!(response.is_some());
assert_eq!(
"/random".to_string(),
response.expect("/random").request_line.request_uri
);
let response =
Message::from_tcp_stream(b"POST / HTTP/1.0\r\nAgent: Random browser\r\n\r\ntest=abc");
assert!(response.is_some());
let response_unwrapped = response.expect("POST HTTP1");
assert_eq!(response_unwrapped.request_line.method, Method::Post);
assert_eq!(response_unwrapped.request_line.protocol, Protocol::V1_0);
assert_eq!(
response_unwrapped
.headers
.get(&"Agent".to_string())
.expect("Agent")
.to_string(),
"Random browser".to_string()
);
if let BodyContentType::SinglePart(body) = response_unwrapped.body {
assert_eq!(
body.get(&"test".to_string()).expect("test-abc").to_string(),
"abc".to_string()
);
}
let response = Message::from_tcp_stream(b"RANDOM /stuff HTTP/2.5\r\n");
assert!(response.is_none());
let response = Message::from_tcp_stream(b"");
assert!(response.is_none());
let response = Message::from_tcp_stream(b"POST /?test=abcdef HTTP/1.1\r\nHost: localhost:8888\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:63.0) Gecko/20100101 Firefox/63.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-US,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nReferer: http://localhost:8888/?test=abcdef\r\nContent-Type: multipart/form-data; boundary=---------------------------5072966556248019951999579782\r\nContent-Length: 733\r\nDNT: 1\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nPragma: no-cache\r\nCache-Control: no-cache\r\n\r\n-----------------------------5072966556248019951999579782\r\nContent-Disposition: form-data; name=\"file\"; filename=\"KeePassXC-2.3.1.dmg.sig\"\r\nContent-Type: application/octet-stream\r\n\r\n-----BEGIN PGP SIGNATURE-----\n\niQEzBAABCAAdFiEEweTLo61406/YlPngt6ZvA7WQdqgFAlqfE5MACgkQt6ZvA7WQ\ndqgnEAgAjtdbsMPaULGXKX6H+fcsYeGEN8OjiUTNz+StwNDkDxhxB4MT0N0lYZ4L\nxUv86kwMdWAaxp8pvVWo6gWXTEM5gWmN302bBxkpbhBl9fnq6WdcCCDGs4GM5vHX\nlOrHXWTsK+8ayLNZ0dCcP054srAtMmJHscPiuUYPfvKSgLxl+JxkPC147EktCCzv\n5O+2AtQPwIEPuaMewFqP9KjaGOhWgAc0nauIKa0ASt9FXXrexq1EoZnoZ3ZQ0p/w\n/otAB2D27yQ4kv+X2Rn94Ky9W0lMT2MYEF+/tQH4aEKsdMBQ7REQtfLGFlEzTMB/\nBNUI5YCF3PV9MKr3N53vEVYvkbXLbw==\n=LO1E\n-----END PGP SIGNATURE-----\n\r\n-----------------------------5072966556248019951999579782--\r\nCAAdFiEEweTLo61406/YlPngt6ZvA7WQdqgFAlqfE5MACgkQt6ZvA7WQ\ndqgnEAgAjtdbsMPaULGXKX6H+fcsYeGEN8OjiUTNz+StwNDkDxhxB4MT0N0lYZ4L\nxUv86kwMdWAaxp8pvVWo6gWXTEM5gWmN302bBxkpbhBl9fnq6WdcCCDGs4GM5vHX\nlOrHXWTsK+8ayLNZ0dCcP054srAtMmJHscPiuUYPfvKSgLxl+JxkPC147");
assert!(response.is_some());
let response_unwrapped = response.expect("multipart");
if let BodyContentType::MultiPart(body) = response_unwrapped.body {
assert_eq!(
String::from_utf8(body.get(&"file".to_string()).expect("expecting file data").body.clone()).expect("expecting utf-8 file data"),
"-----BEGIN PGP SIGNATURE-----\n\niQEzBAABCAAdFiEEweTLo61406/YlPngt6ZvA7WQdqgFAlqfE5MACgkQt6ZvA7WQ\ndqgnEAgAjtdbsMPaULGXKX6H+fcsYeGEN8OjiUTNz+StwNDkDxhxB4MT0N0lYZ4L\nxUv86kwMdWAaxp8pvVWo6gWXTEM5gWmN302bBxkpbhBl9fnq6WdcCCDGs4GM5vHX\nlOrHXWTsK+8ayLNZ0dCcP054srAtMmJHscPiuUYPfvKSgLxl+JxkPC147EktCCzv\n5O+2AtQPwIEPuaMewFqP9KjaGOhWgAc0nauIKa0ASt9FXXrexq1EoZnoZ3ZQ0p/w\n/otAB2D27yQ4kv+X2Rn94Ky9W0lMT2MYEF+/tQH4aEKsdMBQ7REQtfLGFlEzTMB/\nBNUI5YCF3PV9MKr3N53vEVYvkbXLbw==\n=LO1E\n-----END PGP SIGNATURE-----\n".to_string()
);
} else {
eprintln!(
"Boundary header: {:?}",
response_unwrapped
.headers
.get("Content-Type")
.expect("A content-type header")
.get_key_value("boundary")
.expect("A boundary")
);
panic!(
"Expected multipart content but got: {:?}",
response_unwrapped
);
}
let response = Message::from_tcp_stream(b"GET / HTTP/2.0\r\n\r\nabc=123");
assert!(response.is_some());
let response_unwrapped = response.unwrap();
if let BodyContentType::SinglePart(body) = response_unwrapped.body {
assert_eq!(
body.get(&"abc".to_string()).unwrap().to_string(),
"123".to_string()
);
}
let response = Message::from_tcp_stream(b"HEAD / HTTP/2.0\r\n\r\nabc=123");
assert!(response.is_some());
let response_unwrapped = response.unwrap();
if let BodyContentType::SinglePart(body) = response_unwrapped.body {
assert!(body.get(&"abc".to_string()).is_none());
}
let response = Message::from_tcp_stream(b"html/index.html\r\n");
assert!(response.is_some());
let response_unwrapped = response.unwrap();
assert_eq!(response_unwrapped.request_line.method, Method::Get);
assert_eq!(
response_unwrapped.request_line.request_uri,
"html/index.html".to_string()
);
assert_eq!(response_unwrapped.request_line.protocol, Protocol::V0_9);
let response = Message::from_tcp_stream(&[0; 100]);
assert!(response.is_none());
}
}