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 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io::Read;
use std::net::TcpStream;
use multimap::MultiMap;
use std::sync::Arc;
use std::ops::Range;
use std::rc::Rc;
use std::ffi::OsStr;
use std::io;
use std::io::prelude::*;
pub use tera::{Context, Tera};
pub mod mime;
use hmac::Hmac;
use sha2::Sha256;
use crate::cookie::Cookie;
use serde_json::Value;
use std::collections::BTreeMap;
pub mod http_response_table {
const STATE_TABLE: [(u16, &str); 20] = [
(101, "101 Switching Protocals\r\n"),
(200, "200 OK\r\n"),
(201, "201 Created\r\n"),
(202, "202 Accepted\r\n"),
(204, "204 No Content\r\n"),
(206, "206 Partial Content\r\n"),
(300, "300 Multiple Choices\r\n"),
(301, "301 Moved Permanently\r\n"),
(302, "302 Moved Temporarily\r\n"),
(304, "304 Not Modified\r\n"),
(400, "400 Bad Request\r\n"),
(401, "401 Unauthorized\r\n"),
(403, "403 Forbidden\r\n"),
(404, "404 Not Found\r\n"),
(413, "413 Request Entity Too Large\r\n"),
(416, "416 Requested Range Not Satisfiable\r\n"),
(500, "500 Internal Server Error\r\n"),
(501, "501 Not Implemented\r\n"),
(502, "502 Bad Gateway\r\n"),
(503, "503 Service Unavailable\r\n"),
];
pub(super) fn get_httpstatus_from_code(code: u16) -> &'static str {
match STATE_TABLE.binary_search_by_key(&code, |&(k, _)| k) {
Ok(index) => STATE_TABLE[index].1,
Err(_) => panic!("not supporting such a http state code"),
}
}
const HTTP_METHODS: [(u8, &str); 9] = [
(0, "GET"),
(1, "POST"),
(2, "OPTIONS"),
(3, "DELETE"),
(4, "HEAD"),
(5, "PUT"),
(6, "PATCH"),
(7, "CONNECT"),
(8, "TRACE"),
];
/// > Correspond to http GET
pub const GET: u8 = 0;
/// > Correspond to http POST
pub const POST: u8 = 1;
/// > Correspond to http OPTIONS
pub const OPTIONS: u8 = 2;
/// > Correspond to http DELETE
pub const DELETE: u8 = 3;
/// > Correspond to http HEAD
pub const HEAD: u8 = 4;
/// > Correspond to http PUT
pub const PUT: u8 = 5;
/// > Correspond to http PATCH
pub const PATCH: u8 = 6;
/// > Correspond to http CONNECT
pub const CONNECT: u8 = 7;
/// > Correspond to http TRACE
pub const TRACE: u8 = 8;
pub fn get_httpmethod_from_code(code: u8) -> &'static str {
match HTTP_METHODS.binary_search_by_key(&code, |&(k, _)| k) {
Ok(index) => HTTP_METHODS[index].1,
Err(_) => panic!("not supporting such a http state code"),
}
}
}
pub struct Request<'a> {
pub(super) header_pair: HashMap<&'a str, &'a str>,
pub(super) url: &'a str,
pub(super) method: &'a str,
pub(super) version: &'a str,
pub(super) body: BodyContent<'a>,
pub(super) conn_: Rc<RefCell<&'a mut TcpStream>>,
pub(super) secret_key: Arc<Hmac<Sha256>>,
pub(super) ctx: RefCell<BTreeMap<String, Value>>,
}
impl<'a> Request<'a> {
/// Get the value of a header pair by specifying the key
/// For example, `Content-length: 123`
/// get_header("Content-length"), the key is not case senstive
pub fn get_header(&self, key: &str) -> Option<&str> {
let r = self.header_pair.keys().find(|&&ik| {
if ik.to_lowercase() == key.to_lowercase() {
true
} else {
false
}
});
match r {
Some(r) => {
return Some(self.header_pair.get(*r).unwrap()); // confirm that unwrap() is harmless
}
None => {
return None;
}
}
}
/// > Get the value of a parameter in the requested url
/// # For example
/// > `/path?id=1`
/// >> - `get_param("id")` returns 1, the key is case senstive
pub fn get_param(&self, k: &str) -> Option<&str> {
match self.url.split_once("?") {
Some((_, v)) => {
let r = v.split("&");
for e in r {
match e.split_once("=") {
Some((ik, iv)) => {
if ik == k {
return Some(iv);
}
}
None => {}
}
}
None
}
None => None,
}
}
/// > Get the HashMap of the parameters in the requested url
/// # For example
/// > `/path?id=1&flag=true`
/// >> - `get_params()` returns `{id:1, flag:true }`
pub fn get_params(&self) -> Option<HashMap<&str, &str>> {
match self.url.split_once("?") {
Some((_, v)) => {
let r = v.split("&");
let mut map = HashMap::new();
for e in r {
match e.split_once("=") {
Some((ik, iv)) => {
map.insert(ik, iv);
}
None => {}
}
}
if map.len() == 0 {
None
} else {
Some(map)
}
}
None => None,
}
}
/// > Get the complete http headers
/// >> - `{"Content-length":"1", "key":"value",...}`
pub fn get_headers(&self) -> HashMap<&str, &str> {
self.header_pair.clone()
}
/// > Get the version of http request
pub fn get_version(&self) -> &str {
self.version
}
/// > Query the value of www-form-urlencoded or the text part of the multipart-form
/// >> - The key is not case senstive
/// # For example
/// > Assume the form has the value `id=1`, then get_query("id") returns Some("1")
///
pub fn get_query(&self, k: &str) -> Option<&str> {
if let BodyContent::UrlForm(x) = &self.body {
let r = x.keys().find(|&&ik| {
if ik.to_lowercase() == k.to_lowercase() {
true
} else {
false
}
});
match r {
Some(r) => {
return Some(x.get(*r).unwrap()); // confirm that unwrap() is harmless
}
None => {
return None;
}
}
} else if let BodyContent::Multi(x) = &self.body {
let r = x.keys().find(|&ik| {
if ik.to_lowercase() == k.to_lowercase() {
true
} else {
false
}
});
match r {
Some(s) => {
let v = x.get(s).unwrap(); // guarantee by above
match v {
MultipleFormData::Text(v) => {
return Some(*v);
}
MultipleFormData::File(_) => return None,
}
}
None => {
return None;
}
}
} else {
None
}
}
/// > This method is used to acquire the file in the multipart-form data
/// # For example,
/// ```
/// <form>
/// <input type="file" name="file1" />
/// </form>
///
///```
/// > - `get_file("file1")` return the file's meta data
pub fn get_file(&self, k: &str) -> Option<&'_ MultipleFormFile> {
if let BodyContent::Multi(x) = &self.body {
let r = x.keys().find(|&ik| {
if k.to_lowercase() == ik.to_lowercase() {
true
} else {
false
}
});
match r {
Some(s) => {
let item = x.get(s).unwrap(); // confirm that unwrap() is harmless
if let MultipleFormData::File(file) = item {
return Some(file);
} else {
return None;
}
}
None => return None,
}
} else {
None
}
}
/// > Return a HashMap that comprises all pairs in the www-form-urlencoded or the text part of the multipart-form
/// >> - It is safety called even though the request is `GET`, which returns None
pub fn get_queries(&self) -> Option<HashMap<&str, &str>> {
if let BodyContent::UrlForm(x) = &self.body {
Some(x.clone())
} else if let BodyContent::Multi(x) = &self.body {
let mut v = HashMap::new();
for (k, item) in x {
match item {
MultipleFormData::Text(text) => {
v.insert(k.as_str(), *text);
}
MultipleFormData::File(_) => {}
}
}
if v.len() != 0 {
return Some(v);
} else {
return None;
}
} else {
None
}
}
/// > Returns an array comprises of all files in the multipart-form data
pub fn get_files(&self) -> Option<Vec<&MultipleFormFile>> {
if let BodyContent::Multi(x) = &self.body {
let mut vec = Vec::new();
for (_k, v) in x {
match v {
MultipleFormData::Text(_) => {}
MultipleFormData::File(file) => {
vec.push(file);
}
}
}
if vec.len() != 0 {
return Some(vec);
} else {
return None;
}
} else {
None
}
}
/// > Returns the body of a request
/// >> - it is used for getting the posted JSON or other plain text body
pub fn plain_body(&self) -> Option<&str> {
if let BodyContent::PureText(x) = self.body {
Some(x)
} else {
None
}
}
/// > Determin whether the request has a body
pub fn has_body(&self) -> bool {
if let BodyContent::None = self.body {
false
} else {
true
}
}
/// > Return the raw instance of TcpStream
/// >> - This method should be carefully used,
/// It is better to only get some meta information of a connection, such as a peer IP
pub fn get_conn(&self) -> Rc<RefCell<&'a mut TcpStream>> {
Rc::clone(&self.conn_)
}
/// > Return the requested http method
pub fn get_method(&self) -> &str {
self.method
}
/// > Return the complete requested url
pub fn get_url(&self) -> &str {
self.url
}
pub(crate) fn get_secret_key(&self) -> Arc<Hmac<Sha256>> {
Arc::clone(&self.secret_key)
}
/// > Return the part of url exclude the parameters(if any)
pub fn url_to_path(&self) -> &str {
match self.url.find("?") {
Some(pos) => &self.url[..pos],
None => self.url,
}
}
/// > It is used to store user data
/// >> - share data between middlwares and routers(if any)
///
pub fn get_context(&self) -> &RefCell<BTreeMap<String, Value>> {
&self.ctx
}
}
pub struct ResponseConfig<'b, 'a> {
res: &'b mut Response<'a>,
has_failure: bool,
}
impl<'b, 'a> ResponseConfig<'b, 'a> {
fn get_map_key(map: &MultiMap<String, String>, key: &str) -> Option<String> {
let r = map.keys().find(|&ik| {
if ik.to_lowercase() == key.to_lowercase() {
true
} else {
false
}
});
Some((r?).clone())
}
/// > Set the transfer type of a response with chunked
pub fn chunked(&mut self) -> &mut Self {
if self.has_failure {
return self;
}
if self.res.method == "HEAD" {
return self;
}
self.res
.add_header(String::from("Transfer-Encoding"), String::from("chunked"));
if let Some(key) = Self::get_map_key(&self.res.header_pair, "content-length") {
self.res.header_pair.remove(&key);
}
self.res.chunked.enable = true;
self
}
/// > Specify the status of a http response
pub fn status(&mut self, code: u16) -> &mut Self {
if self.has_failure {
return self;
}
self.res.http_state = code;
self
}
/// > This is only used to specify the name when the client downloads a file
/// >> - Only works if it follows the write_file()
pub fn specify_file_name(&mut self, name: &str) -> &mut Self {
if self.has_failure {
return self;
}
match &self.res.body {
BodyType::Memory(_, _) => {}
BodyType::File(_, _) => {
if !self.res.header_exist("Content-Disposition") {
self.res.add_header(
"Content-Disposition".to_string(),
format!("attachment; filename=\"{name}\""),
);
}
}
BodyType::None => {}
}
self
}
/// > Start a range function for such as `write_file`, `write_string`, or `render_view_xxxx`
pub fn enable_range(&mut self) -> &mut Self {
if self.has_failure {
return self;
}
self.res
.add_header(String::from("Accept-Ranges"), String::from("bytes"));
if self.res.method == "HEAD" {
match &self.res.body {
BodyType::Memory(_, buffs) => {
self.res
.add_header(String::from("Content-length"), buffs.len().to_string());
self.res.http_state = 200;
}
BodyType::File(path, _) => {
match std::fs::OpenOptions::new().read(true).open(path) {
Ok(file) => {
match file.metadata() {
Ok(meta) => {
self.res.add_header(
String::from("Content-length"),
meta.len().to_string(),
);
self.res.http_state = 200;
}
Err(_) => {
self.has_failure = true;
self.res.write_state(404);
}
};
}
Err(_) => {
self.has_failure = true;
self.res.write_state(404);
}
}
}
BodyType::None => {}
}
} else {
match self.res.get_request_header_value("Range") {
Some(v) => {
self.res.range = parse_range_content(v);
}
None => {
self.res.range = ResponseRangeMeta::None;
}
}
}
self
}
/// > Specify cookies for the request
/// >> - Argument could be a single Cookie
/// >> - Or muliple Cookies: [Cookie,Cookie,...]
pub fn with_cookies<T: MoreThanOneCookie<Output = Cookie>>(&mut self, v: T) -> &mut Self {
if self.has_failure {
return self;
}
for e in v.into_vec() {
match e.to_string() {
Some(s) => {
self.res.add_header(String::from("set-cookie"), s);
}
None => {
continue;
}
}
}
self
}
pub fn charset(&mut self, v: &str) -> &mut Self {
if self.has_failure {
return self;
}
self.res.charset = Some(v.to_string());
self
}
}
pub trait MoreThanOneCookie {
type Output;
fn into_vec(self) -> Vec<Self::Output>;
}
impl MoreThanOneCookie for Cookie {
type Output = Cookie;
fn into_vec(self) -> Vec<Self::Output> {
vec![self]
}
}
impl<const I: usize> MoreThanOneCookie for [Cookie; I] {
type Output = Cookie;
fn into_vec(self) -> Vec<Self::Output> {
Vec::from(self)
}
}
fn parse_range_content(v: &str) -> ResponseRangeMeta {
match v.trim().split_once("=") {
Some(v) => {
let v = v.1;
match v.trim().split_once("-") {
Some(v) => {
let start;
let end;
if v.0 != "" {
let mut exception = false;
let r: u64 = v.0.parse().unwrap_or_else(|_| {
exception = true;
0
});
if r == 0 && exception == true {
start = None;
} else {
start = Some(r);
}
} else {
start = None;
}
if v.1 != "" {
let mut exception = false;
let r: u64 = v.1.parse().unwrap_or_else(|_| {
exception = true;
0
});
if r == 0 && exception == true {
end = None;
} else {
end = Some(r);
}
} else {
end = None;
}
ResponseRangeMeta::Range(start, end)
}
None => ResponseRangeMeta::Range(None, None),
}
}
None => ResponseRangeMeta::Range(None, None),
}
}
pub struct ResponseChunkMeta {
pub(super) enable: bool,
pub(super) chunk_size: usize,
}
impl ResponseChunkMeta {
pub(super) fn new(chunk_size: u32) -> Self {
ResponseChunkMeta {
enable: false,
chunk_size: chunk_size as usize,
}
}
}
pub enum ResponseRangeMeta {
Range(Option<u64>, Option<u64>),
None,
}
pub enum MemoryType {
String(String),
Binary,
}
pub enum BodyType {
Memory(MemoryType, Vec<u8>),
File(String, Option<String>),
None,
}
pub struct Response<'a> {
pub(super) header_pair: MultiMap<String, String>,
pub(super) version: &'a str,
pub(super) method: &'a str,
//pub(super) url: &'a str,
pub(super) http_state: u16,
pub(super) body: BodyType,
pub(super) chunked: ResponseChunkMeta,
pub(super) conn_: Rc<RefCell<&'a mut TcpStream>>,
pub(super) range: ResponseRangeMeta,
pub(super) request_header: HashMap<&'a str, &'a str>,
pub(super) charset: Option<String>,
}
impl<'a> Response<'a> {
fn get_request_header_value(&mut self, k: &str) -> Option<&str> {
match self.request_header.keys().find(|&&ik| {
if k.to_lowercase() == ik.to_lowercase() {
true
} else {
false
}
}) {
Some(k) => Some(self.request_header.get(*k).unwrap()), // confirm that unwrap() is harmless
None => None,
}
}
/// > Remove a pair you have writed to a reponse header
/// >> - The key is not case senstive
/// # For example
/// ```
/// add_header(String::from("a"),String::from("b"))
/// ```
/// > Header: {a:b}
/// ```
/// remove_header(String::from("a"))
/// ```
/// > Header: {}
pub fn remove_header(&mut self, key: String) {
let r = self.header_pair.keys().find(|&ik| {
if key.to_lowercase() == ik.to_lowercase() {
true
} else {
false
}
});
match r {
Some(k) => {
let s = k.clone();
let map = &mut self.header_pair;
map.remove(&s);
}
None => {}
}
}
/// > Add a pair to the header of the response
/// ```
/// add_header(String::from("a"),String::from("b"))
/// ```
/// > Header:{a:b}
pub fn add_header(&mut self, key: String, value: String) {
self.header_pair.insert(key, value);
}
fn set_default_content_type(&mut self) {
if !self.header_exist("Content-Type") {
match &self.body {
BodyType::Memory(kind, _) => {
match kind {
MemoryType::String(extension) => match &self.charset {
Some(charset) => {
self.add_header(
"Content-type".to_string(),
format!("{}; charset={}", extension, charset),
);
}
None => {
self.add_header(
"Content-type".to_string(),
format!("{}; charset=utf-8", extension),
);
}
},
MemoryType::Binary => {}
};
}
BodyType::File(_, extension) => {
if let Some(x) = extension {
match &self.charset {
Some(charset) => {
self.add_header(
"Content-type".to_string(),
format!("{}; charset={}", x, charset),
);
}
None => {
self.add_header(
"Content-type".to_string(),
format!("{}; charset=utf-8", x),
);
}
}
}
}
BodyType::None => {}
}
}
}
pub(super) fn header_to_string(&mut self) -> Vec<u8> {
//println!("header pairs: {:#?}",self.header_pair);
let mut buffs = Vec::new();
let state_text = http_response_table::get_httpstatus_from_code(self.http_state);
buffs.extend_from_slice(format!("{} {}", self.version, state_text).as_bytes());
self.set_default_content_type();
for (k, v) in &self.header_pair {
for value in v {
buffs.extend_from_slice(format!("{}: {}\r\n", k, value).as_bytes());
}
}
buffs.extend_from_slice(b"\r\n");
buffs
}
fn take_body_size(&mut self) -> io::Result<u64> {
match &self.body {
BodyType::Memory(_, buff) => Ok(buff.len() as u64),
BodyType::File(path, _) => match std::fs::OpenOptions::new().read(true).open(path) {
Ok(file) => Ok(file.metadata()?.len()),
Err(e) => Err(e),
},
BodyType::None => Ok(0),
}
}
pub(super) fn take_body_buff(&mut self) -> io::Result<LayzyBuffers> {
let body_size = self.take_body_size()?;
match self.range {
ResponseRangeMeta::Range(start, end) => {
let mut beg_pos;
let end_pos;
let mut lack_beg = false;
if let Some(x) = start {
beg_pos = x;
} else {
beg_pos = 0;
lack_beg = true;
}
if let Some(x) = end {
if lack_beg {
end_pos = body_size - 1;
beg_pos = body_size - x;
} else {
end_pos = x;
}
} else {
if lack_beg {
self.write_state(416);
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"bad requested range values with form [ - ]",
));
}
end_pos = body_size - 1;
}
if beg_pos > end_pos || (beg_pos > (body_size - 1)) || end_pos >= body_size {
self.write_state(416);
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"bad requested range values due to out of range",
));
}
let v = format!("bytes {}-{}/{}", beg_pos, end_pos, body_size);
let len = (end_pos - beg_pos + 1).to_string();
self.add_header(String::from("Content-Range"), v);
let key = "Content-Length".to_string();
self.remove_header(key.clone());
if !self.chunked.enable {
self.add_header(key, len);
}
self.http_state = 206;
match &self.body {
BodyType::Memory(_, buffs) => {
let slice = &buffs[beg_pos as usize..=end_pos as usize];
let mut ret_buff = Vec::new();
ret_buff.extend_from_slice(slice);
return Ok(LayzyBuffers {
buffs: LayzyBuffersType::Memory(ret_buff),
len: slice.len() as u64,
});
}
BodyType::File(path, _) => {
let mut file = std::fs::OpenOptions::new().read(true).open(path)?;
let need_size = end_pos - beg_pos + 1;
file.seek(std::io::SeekFrom::Start(beg_pos))?;
return Ok(LayzyBuffers {
buffs: LayzyBuffersType::File(FileType {
file: Box::new(file),
buffs: Vec::new(),
}),
len: need_size,
});
}
BodyType::None => {
return Ok(LayzyBuffers {
buffs: LayzyBuffersType::None,
len: 0,
});
}
};
}
ResponseRangeMeta::None => match &self.body {
BodyType::Memory(_, buffs) => {
return Ok(LayzyBuffers {
buffs: LayzyBuffersType::Memory(buffs.clone()),
len: buffs.len() as u64,
});
}
BodyType::File(path, _) => {
let file = std::fs::OpenOptions::new().read(true).open(path)?;
return Ok(LayzyBuffers {
buffs: LayzyBuffersType::File(FileType {
file: Box::new(file),
buffs: Vec::new(),
}),
len: body_size as u64,
});
}
BodyType::None => {
return Ok(LayzyBuffers {
buffs: LayzyBuffersType::None,
len: 0,
});
}
},
}
}
/// > Check whether a pair exists in the header of a reponse
/// # For example
/// > assume the header is {a:b}
///
/// `header_exist("a")` returns true
/// > The key is not case senstive
pub fn header_exist(&self, s: &str) -> bool {
let r = self.header_pair.keys().find(|&k| {
if k.to_lowercase() == s.to_lowercase() {
true
} else {
false
}
});
match r {
Some(_) => true,
None => false,
}
}
/// > Get response header
/// >> - Return a Vector since a single key can correspond to multiple values in the response header.
pub fn get_header(&self, k: &str) -> Option<&Vec<String>> {
self.header_pair.get_vec(k)
}
/// > Write a utf-8 String to client
pub fn write_string(&mut self, v: &str) -> ResponseConfig<'_, 'a> {
self.add_header(String::from("Content-length"), v.len().to_string());
self.body = BodyType::Memory(MemoryType::String("text/plain".to_string()), v.into());
ResponseConfig {
res: self,
has_failure: false,
}
}
/// > Write binary data to client
pub fn write_binary(&mut self, v: Vec<u8>) -> ResponseConfig<'_, 'a> {
self.add_header(String::from("Content-length"), v.len().to_string());
self.body = BodyType::Memory(MemoryType::Binary, v);
ResponseConfig {
res: self,
has_failure: false,
}
}
/// > Only respond HTTP status to the client
pub fn write_state(&mut self, code: u16) {
self.http_state = code;
self.add_header(String::from("Content-length"), 0.to_string());
self.body = BodyType::None;
}
/// > Write file data to the client
pub fn write_file(&mut self, path: String) -> ResponseConfig<'_, 'a> {
match std::fs::OpenOptions::new().read(true).open(path.clone()) {
Ok(file) => {
match file.metadata() {
Ok(meta) => {
self.add_header(String::from("Content-length"), meta.len().to_string());
}
Err(_) => {
self.write_string(&format!("{} cannot get file length", path))
.status(404);
return ResponseConfig {
res: self,
has_failure: true,
};
}
};
let extension = std::path::Path::new(&path)
.extension()
.and_then(OsStr::to_str);
match extension {
Some(extension) => {
let content_type = mime::extension_to_content_type(extension);
self.body = BodyType::File(path, Some(content_type.to_string()));
}
None => {
self.body = BodyType::File(path, None);
}
}
return ResponseConfig {
res: self,
has_failure: false,
};
}
Err(_) => {
self.write_string(&format!("{} file not found", path))
.status(404);
return ResponseConfig {
res: self,
has_failure: true,
};
}
}
}
/// > Render a view to the client
/// >> - factory implements Fn() -> tera::Result<String>
/// >>> - The factory permits you customize the behavior of the tera engine
pub fn render_view(
&mut self,
factory: impl Fn(&Request) -> tera::Result<String>,
context: &Request,
) -> ResponseConfig<'_, 'a> {
match factory(context) {
Ok(s) => {
return self.write_string(&s);
}
Err(e) => {
self.write_string(&format!("Render view error: {}", e.to_string()))
.status(404);
return ResponseConfig {
res: self,
has_failure: true,
};
}
}
}
/// > Only use the default configured tera to render a view to the client
/// >> - path: path of view file
/// >> - context: used in the view
pub fn render_view_once(&mut self, path: &str, context: &Context) -> ResponseConfig<'_, 'a> {
match OpenOptions::new().read(true).open(path) {
Ok(mut file) => {
let mut s = String::new();
match file.read_to_string(&mut s) {
Ok(_) => match Tera::one_off(&s, &context, true) {
Ok(s) => {
self.add_header(String::from("Content-length"), s.len().to_string());
let extension = std::path::Path::new(&path)
.extension()
.and_then(OsStr::to_str);
match extension {
Some(extension) => {
let content_type = mime::extension_to_content_type(extension);
self.body = BodyType::Memory(
MemoryType::String(content_type.to_string()),
s.into(),
);
}
None => {
self.body = BodyType::Memory(
MemoryType::String("text/plain".to_string()),
s.into(),
);
}
}
return ResponseConfig {
res: self,
has_failure: false,
};
}
Err(e) => {
self.write_string(&format!("Render view error: {}", e.to_string()))
.status(404);
return ResponseConfig {
res: self,
has_failure: true,
};
}
},
Err(e) => {
self.write_string(&format!("Render view error: {}", e.to_string()))
.status(404);
return ResponseConfig {
res: self,
has_failure: true,
};
}
}
}
Err(e) => {
self.write_string(&format!("Render view error: {}", e.to_string()))
.status(404);
return ResponseConfig {
res: self,
has_failure: true,
};
}
}
}
pub fn get_conn(&self) -> Rc<RefCell<&'a mut TcpStream>> {
Rc::clone(&self.conn_)
}
}
#[derive(Debug)]
pub enum BodyContent<'a> {
UrlForm(HashMap<&'a str, &'a str>),
PureText(&'a str),
Multi(HashMap<String, MultipleFormData<'a>>),
None,
Bad,
TooLarge,
}
#[derive(Debug)]
pub struct MultipleFormFile {
pub filename: String,
pub filepath: String,
pub content_type: String,
pub form_indice: String,
}
#[derive(Debug)]
pub enum MultipleFormData<'a> {
Text(&'a str),
File(MultipleFormFile),
}
pub(super) struct FileType {
file: Box<std::fs::File>,
buffs: Vec<u8>,
}
pub(super) enum LayzyBuffersType {
Memory(Vec<u8>),
File(FileType),
None,
}
pub(super) struct LayzyBuffers {
buffs: LayzyBuffersType,
len: u64,
}
impl LayzyBuffers {
pub fn len(&self) -> usize {
self.len as usize
}
pub fn get_slice_from_range(&mut self, index: Range<usize>) -> Result<&[u8], io::Error> {
match &mut self.buffs {
LayzyBuffersType::Memory(buffs) => Ok(&mut buffs[index]),
LayzyBuffersType::File(file_v) => {
let file = &mut file_v.file;
let need_size = index.end - index.start;
let buffs = &mut file_v.buffs;
buffs.resize(need_size, b'\0');
match file.read(&mut buffs[0..]) {
Ok(read_size) => {
if read_size != need_size {
let msg = format!("cannot read enough bytes from the file, need bytes: {}, actual read bytes: {}",need_size,read_size);
return Err(io::Error::new(io::ErrorKind::InvalidData, msg));
}
return Ok(&buffs[0..read_size]);
}
Err(e) => {
return Err(e);
}
}
}
LayzyBuffersType::None => unreachable!(),
}
}
// pub fn get_total_slice(& mut self)-> Result<&[u8],io::Error> {
// match &mut self.buffs {
// LayzyBuffersType::Memory(buffs) => {
// return Ok(buffs);
// },
// LayzyBuffersType::File(file_v) => {
// let file = &mut file_v.file;
// let buffs = &mut file_v.buffs;
// match file.read_to_end(buffs){
// Ok(_) => {
// return Ok(buffs);
// },
// Err(e) => {
// return Err(e);
// },
// }
// }
// LayzyBuffersType::None => unreachable!(),
// }
// }
}
// impl Index<Range<usize>> for LayzyBuffers {
// type Output = [u8];
// fn index(&self, _index: Range<usize>) -> &Self::Output {
// unreachable!()
// }
// }
// impl IndexMut<Range<usize>> for LayzyBuffers {
// fn index_mut(&mut self, index: Range<usize>) -> &mut Self::Output {
// match &mut self.buffs {
// LayzyBuffersType::Memory(buffs) => &mut buffs[index],
// LayzyBuffersType::File(file_v) => {
// let file = &mut file_v.file;
// let need_size = index.end - index.start;
// let buffs = &mut file_v.buffs;
// buffs.resize(need_size, b'\0');
// file.read(buffs).unwrap();
// buffs
// }
// LayzyBuffersType::None => unreachable!(),
// }
// }
// }
// impl Deref for LayzyBuffers {
// type Target = Vec<u8>;
// fn deref(&self) -> &Self::Target {
// unreachable!()
// }
// }
// impl DerefMut for LayzyBuffers {
// fn deref_mut(&mut self) -> &mut Self::Target {
// match &mut self.buffs {
// LayzyBuffersType::Memory(buffs) => buffs,
// LayzyBuffersType::File(file_v) => {
// let file = &mut file_v.file;
// let buffs = &mut file_v.buffs;
// file.read_to_end(buffs).unwrap();
// buffs
// }
// LayzyBuffersType::None => unreachable!(),
// }
// }
// }