1use super::*;
6
7use crate::headers::{TypedAppendableHeader, TypedHeader};
8
9#[derive(Debug, PartialEq, Eq, Clone)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub enum Message<Body> {
19 Request(Request<Body>),
21 Response(Response<Body>),
23 Data(Data<Body>),
25}
26
27impl<Body> From<Request<Body>> for Message<Body> {
28 fn from(v: Request<Body>) -> Self {
29 Message::Request(v)
30 }
31}
32
33impl<Body> From<Response<Body>> for Message<Body> {
34 fn from(v: Response<Body>) -> Self {
35 Message::Response(v)
36 }
37}
38
39impl<Body> From<Data<Body>> for Message<Body> {
40 fn from(v: Data<Body>) -> Self {
41 Message::Data(v)
42 }
43}
44
45impl<Body: AsRef<[u8]>> Message<Body> {
46 pub(crate) fn borrow(&self) -> MessageRef {
47 match self {
48 Message::Request(request) => MessageRef::Request(request.borrow()),
49 Message::Response(response) => MessageRef::Response(response.borrow()),
50 Message::Data(data) => MessageRef::Data(data.borrow()),
51 }
52 }
53
54 pub fn write<'b, W: std::io::Write + 'b>(&self, w: &'b mut W) -> Result<(), WriteError> {
85 self.borrow().write(w)
86 }
87
88 pub fn write_len(&self) -> u64 {
90 self.borrow().write_len()
91 }
92}
93
94impl<'a, T: From<&'a [u8]>> Message<T> {
95 pub fn parse<B: AsRef<[u8]> + 'a + ?Sized>(buf: &'a B) -> Result<(Self, usize), ParseError> {
132 let buf = buf.as_ref();
133 let (msg, consumed) = MessageRef::parse(buf)?;
134
135 Ok((msg.to_owned()?, consumed))
136 }
137}
138
139#[derive(Debug, PartialEq, Eq, Clone)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145pub enum Method {
146 Describe,
148 GetParameter,
150 Options,
152 Pause,
154 Play,
156 PlayNotify,
158 Redirect,
160 Setup,
162 SetParameter,
164 Announce,
166 Record,
168 Teardown,
170 Extension(String),
172}
173
174impl Method {
175 pub(crate) fn borrow(&self) -> MethodRef {
176 match self {
177 Method::Describe => MethodRef::Describe,
178 Method::GetParameter => MethodRef::GetParameter,
179 Method::Options => MethodRef::Options,
180 Method::Pause => MethodRef::Pause,
181 Method::Play => MethodRef::Play,
182 Method::PlayNotify => MethodRef::PlayNotify,
183 Method::Redirect => MethodRef::Redirect,
184 Method::Setup => MethodRef::Setup,
185 Method::SetParameter => MethodRef::SetParameter,
186 Method::Announce => MethodRef::Announce,
187 Method::Record => MethodRef::Record,
188 Method::Teardown => MethodRef::Teardown,
189 Method::Extension(s) => MethodRef::Extension(s),
190 }
191 }
192}
193
194impl<'a> From<&'a str> for Method {
196 fn from(v: &'a str) -> Self {
197 MethodRef::from(v).to_owned()
198 }
199}
200
201impl<'a> From<&'a Method> for &'a str {
203 fn from(v: &'a Method) -> Self {
204 match v {
205 Method::Describe => "DESCRIBE",
206 Method::GetParameter => "GET_PARAMETER",
207 Method::Options => "OPTIONS",
208 Method::Pause => "PAUSE",
209 Method::Play => "PLAY",
210 Method::PlayNotify => "PLAY_NOTIFY",
211 Method::Redirect => "REDIRECT",
212 Method::Setup => "SETUP",
213 Method::SetParameter => "SET_PARAMETER",
214 Method::Announce => "ANNOUNCE",
215 Method::Record => "RECORD",
216 Method::Teardown => "TEARDOWN",
217 Method::Extension(ref v) => v,
218 }
219 }
220}
221
222impl PartialEq<Method> for &Method {
223 fn eq(&self, other: &Method) -> bool {
224 (*self).eq(other)
225 }
226}
227
228#[derive(Debug, Clone, Eq)]
261#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
262pub struct Request<Body> {
263 pub(crate) method: Method,
264 pub(crate) request_uri: Option<Url>,
265 pub(crate) version: Version,
266 pub(crate) headers: Headers,
267 pub(crate) body: Body,
268}
269
270impl<BodyA, BodyB: PartialEq<BodyA>> PartialEq<Request<BodyA>> for Request<BodyB> {
271 fn eq(&self, other: &Request<BodyA>) -> bool {
272 self.method == other.method
273 && self.request_uri == other.request_uri
274 && self.version == other.version
275 && self.headers == other.headers
276 && self.body == other.body
277 }
278}
279
280impl Request<Empty> {
281 pub fn builder(method: Method, version: Version) -> RequestBuilder {
283 RequestBuilder::new(method, version)
284 }
285}
286
287impl<Body> Request<Body> {
288 pub(crate) fn borrow(&self) -> RequestRef
289 where
290 Body: AsRef<[u8]>,
291 {
292 let headers = self
293 .headers
294 .iter()
295 .map(|(name, value)| HeaderRef {
296 name: name.as_str(),
297 value: value.as_str(),
298 })
299 .collect();
300
301 RequestRef {
302 method: self.method.borrow(),
303 request_uri: self.request_uri.as_ref().map(|u| u.as_str()),
304 version: self.version,
305 headers,
306 body: self.body.as_ref(),
307 }
308 }
309
310 pub fn write<'b, W: std::io::Write + 'b>(&self, w: &'b mut W) -> Result<(), WriteError>
315 where
316 Body: AsRef<[u8]>,
317 {
318 self.borrow().write(w)
319 }
320
321 pub fn write_len(&self) -> u64
323 where
324 Body: AsRef<[u8]>,
325 {
326 self.borrow().write_len()
327 }
328
329 pub fn method(&self) -> &Method {
332 &self.method
333 }
334
335 pub fn set_method(&mut self, method: Method) {
337 self.method = method;
338 }
339
340 pub fn request_uri(&self) -> Option<&Url> {
342 self.request_uri.as_ref()
343 }
344
345 pub fn set_request_uri(&mut self, request_uri: Option<Url>) {
347 self.request_uri = request_uri;
348 }
349
350 pub fn version(&self) -> Version {
352 self.version
353 }
354
355 pub fn set_version(&mut self, version: Version) {
357 self.version = version;
358 }
359
360 pub fn body(&self) -> &Body {
362 &self.body
363 }
364
365 pub fn into_body(self) -> Body {
368 self.body
369 }
370
371 pub fn map_body<NewBody: AsRef<[u8]>, F: FnOnce(Body) -> NewBody>(
375 self,
376 func: F,
377 ) -> Request<NewBody> {
378 let Request {
379 method,
380 request_uri,
381 version,
382 mut headers,
383 body,
384 } = self;
385
386 let new_body = func(body);
387
388 {
389 let new_body = new_body.as_ref();
390 if new_body.is_empty() {
391 headers.remove(&crate::headers::CONTENT_LENGTH);
392 } else {
393 headers.insert(
394 crate::headers::CONTENT_LENGTH,
395 HeaderValue::from(format!("{}", new_body.len())),
396 );
397 }
398 }
399
400 Request {
401 method,
402 request_uri,
403 version,
404 headers,
405 body: new_body,
406 }
407 }
408
409 pub fn replace_body<NewBody: AsRef<[u8]>>(self, new_body: NewBody) -> Request<NewBody> {
413 let Request {
414 method,
415 request_uri,
416 version,
417 mut headers,
418 body: _body,
419 } = self;
420
421 {
422 let new_body = new_body.as_ref();
423 if new_body.is_empty() {
424 headers.remove(&crate::headers::CONTENT_LENGTH);
425 } else {
426 headers.insert(
427 crate::headers::CONTENT_LENGTH,
428 HeaderValue::from(format!("{}", new_body.len())),
429 );
430 }
431 }
432
433 Request {
434 method,
435 request_uri,
436 version,
437 headers,
438 body: new_body,
439 }
440 }
441
442 pub fn append_header<V: Into<HeaderValue>>(&mut self, name: HeaderName, value: V) {
447 let value = value.into();
448 self.headers.append(name, value);
449 }
450
451 pub fn insert_header<V: Into<HeaderValue>>(&mut self, name: HeaderName, value: V) {
457 let value = value.into();
458 self.headers.insert(name, value);
459 }
460
461 pub fn append_typed_header<H: TypedAppendableHeader>(&mut self, header: &H) {
463 self.headers.append_typed(header);
464 }
465
466 pub fn insert_typed_header<H: TypedHeader>(&mut self, header: &H) {
470 self.headers.insert_typed(header);
471 }
472
473 pub fn remove_header(&mut self, name: &HeaderName) {
475 self.headers.remove(name);
476 }
477
478 pub fn header(&self, name: &HeaderName) -> Option<&HeaderValue> {
480 self.headers.get(name)
481 }
482
483 pub fn typed_header<H: TypedHeader>(&self) -> Result<Option<H>, headers::HeaderParseError> {
485 self.headers.get_typed()
486 }
487
488 pub fn header_mut(&mut self, name: &HeaderName) -> Option<&mut HeaderValue> {
490 self.headers.get_mut(name)
491 }
492
493 pub fn headers(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
495 self.headers.iter()
496 }
497
498 pub fn header_names(&self) -> impl Iterator<Item = &HeaderName> {
500 self.headers.names()
501 }
502
503 pub fn header_values(&self) -> impl Iterator<Item = &HeaderValue> {
505 self.headers.values()
506 }
507}
508
509impl<Body> AsRef<Headers> for Request<Body> {
510 fn as_ref(&self) -> &Headers {
511 &self.headers
512 }
513}
514
515impl<Body> AsMut<Headers> for Request<Body> {
516 fn as_mut(&mut self) -> &mut Headers {
517 &mut self.headers
518 }
519}
520
521#[derive(Debug, PartialEq, Eq, Clone)]
525pub struct RequestBuilder(Request<Empty>);
526
527impl RequestBuilder {
528 fn new(method: Method, version: Version) -> Self {
529 Self(Request {
530 method,
531 request_uri: None,
532 version,
533 headers: Headers::new(),
534 body: Empty,
535 })
536 }
537
538 pub fn request_uri<U: Into<Url>>(self, request_uri: U) -> Self {
540 Self(Request {
541 request_uri: Some(request_uri.into()),
542 ..self.0
543 })
544 }
545
546 pub fn header<V: Into<HeaderValue>>(mut self, name: HeaderName, value: V) -> Self {
548 let value = value.into();
549
550 self.0.headers.append(name, value);
551
552 self
553 }
554
555 pub fn typed_header<H: TypedHeader>(mut self, header: &H) -> Self {
557 self.0.headers.insert_typed(header);
558
559 self
560 }
561
562 pub fn empty(self) -> Request<Empty> {
564 self.0
565 }
566
567 pub fn build<Body: AsRef<[u8]>>(mut self, body: Body) -> Request<Body> {
571 {
572 let body = body.as_ref();
573 if !body.is_empty() {
574 self.0.headers.insert(
575 crate::headers::CONTENT_LENGTH,
576 HeaderValue::from(format!("{}", body.len())),
577 );
578 }
579 }
580
581 Request {
582 method: self.0.method,
583 request_uri: self.0.request_uri,
584 version: self.0.version,
585 headers: self.0.headers,
586 body,
587 }
588 }
589}
590
591#[derive(Debug, Clone, Eq)]
609#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
610pub struct Response<Body> {
611 pub(crate) version: Version,
612 pub(crate) status: StatusCode,
613 pub(crate) reason_phrase: String,
614 pub(crate) headers: Headers,
615 pub(crate) body: Body,
616}
617
618impl<BodyA, BodyB: PartialEq<BodyA>> PartialEq<Response<BodyA>> for Response<BodyB> {
619 fn eq(&self, other: &Response<BodyA>) -> bool {
620 self.version == other.version
621 && self.status == other.status
622 && self.reason_phrase == other.reason_phrase
623 && self.headers == other.headers
624 && self.body == other.body
625 }
626}
627
628impl Response<Empty> {
629 pub fn builder(version: Version, status: StatusCode) -> ResponseBuilder {
631 ResponseBuilder::new(version, status)
632 }
633}
634
635impl<Body> Response<Body> {
636 pub(crate) fn borrow(&self) -> ResponseRef
637 where
638 Body: AsRef<[u8]>,
639 {
640 let headers = self
641 .headers
642 .iter()
643 .map(|(name, value)| HeaderRef {
644 name: name.as_str(),
645 value: value.as_str(),
646 })
647 .collect();
648
649 ResponseRef {
650 version: self.version,
651 status: self.status,
652 reason_phrase: &self.reason_phrase,
653 headers,
654 body: self.body.as_ref(),
655 }
656 }
657
658 pub fn write<'b, W: std::io::Write + 'b>(&self, w: &'b mut W) -> Result<(), WriteError>
663 where
664 Body: AsRef<[u8]>,
665 {
666 self.borrow().write(w)
667 }
668
669 pub fn write_len(&self) -> u64
671 where
672 Body: AsRef<[u8]>,
673 {
674 self.borrow().write_len()
675 }
676
677 pub fn version(&self) -> Version {
680 self.version
681 }
682
683 pub fn set_version(&mut self, version: Version) {
685 self.version = version;
686 }
687
688 pub fn status(&self) -> StatusCode {
690 self.status
691 }
692
693 pub fn set_status(&mut self, status: StatusCode) {
695 self.status = status;
696 }
697
698 pub fn reason_phrase(&self) -> &str {
700 self.reason_phrase.as_str()
701 }
702
703 pub fn set_reason_phrase<S: Into<String>>(&mut self, reason_phrase: S) {
705 self.reason_phrase = reason_phrase.into();
706 }
707
708 pub fn body(&self) -> &Body {
710 &self.body
711 }
712
713 pub fn into_body(self) -> Body {
716 self.body
717 }
718
719 pub fn map_body<NewBody: AsRef<[u8]>, F: FnOnce(Body) -> NewBody>(
723 self,
724 func: F,
725 ) -> Response<NewBody> {
726 let Response {
727 version,
728 status,
729 reason_phrase,
730 mut headers,
731 body,
732 } = self;
733
734 let new_body = func(body);
735
736 {
737 let new_body = new_body.as_ref();
738 if new_body.is_empty() {
739 headers.remove(&crate::headers::CONTENT_LENGTH);
740 } else {
741 headers.insert(
742 crate::headers::CONTENT_LENGTH,
743 HeaderValue::from(format!("{}", new_body.len())),
744 );
745 }
746 }
747
748 Response {
749 version,
750 status,
751 reason_phrase,
752 headers,
753 body: new_body,
754 }
755 }
756
757 pub fn replace_body<NewBody: AsRef<[u8]>>(self, new_body: NewBody) -> Response<NewBody> {
761 let Response {
762 version,
763 status,
764 reason_phrase,
765 mut headers,
766 body: _body,
767 } = self;
768
769 {
770 let new_body = new_body.as_ref();
771 if new_body.is_empty() {
772 headers.remove(&crate::headers::CONTENT_LENGTH);
773 } else {
774 headers.insert(
775 crate::headers::CONTENT_LENGTH,
776 HeaderValue::from(format!("{}", new_body.len())),
777 );
778 }
779 }
780
781 Response {
782 version,
783 status,
784 reason_phrase,
785 headers,
786 body: new_body,
787 }
788 }
789
790 pub fn append_header<V: Into<HeaderValue>>(&mut self, name: HeaderName, value: V) {
795 let value = value.into();
796 self.headers.append(name, value);
797 }
798
799 pub fn insert_header<V: Into<HeaderValue>>(&mut self, name: HeaderName, value: V) {
805 let value = value.into();
806 self.headers.insert(name, value);
807 }
808
809 pub fn append_typed_header<H: TypedAppendableHeader>(&mut self, header: &H) {
811 self.headers.append_typed(header);
812 }
813
814 pub fn insert_typed_header<H: TypedHeader>(&mut self, header: &H) {
818 self.headers.insert_typed(header);
819 }
820
821 pub fn remove_header(&mut self, name: &HeaderName) {
823 self.headers.remove(name);
824 }
825
826 pub fn header(&self, name: &HeaderName) -> Option<&HeaderValue> {
828 self.headers.get(name)
829 }
830
831 pub fn typed_header<H: TypedHeader>(&self) -> Result<Option<H>, headers::HeaderParseError> {
833 self.headers.get_typed()
834 }
835
836 pub fn header_mut(&mut self, name: &HeaderName) -> Option<&mut HeaderValue> {
838 self.headers.get_mut(name)
839 }
840
841 pub fn headers(&self) -> impl Iterator<Item = (&HeaderName, &HeaderValue)> {
843 self.headers.iter()
844 }
845
846 pub fn header_names(&self) -> impl Iterator<Item = &HeaderName> {
848 self.headers.names()
849 }
850
851 pub fn header_values(&self) -> impl Iterator<Item = &HeaderValue> {
853 self.headers.values()
854 }
855}
856
857impl<Body> AsRef<Headers> for Response<Body> {
858 fn as_ref(&self) -> &Headers {
859 &self.headers
860 }
861}
862
863impl<Body> AsMut<Headers> for Response<Body> {
864 fn as_mut(&mut self) -> &mut Headers {
865 &mut self.headers
866 }
867}
868
869#[derive(Debug, PartialEq, Eq, Clone)]
873pub struct ResponseBuilder(Response<Empty>, Option<String>);
874
875impl ResponseBuilder {
876 fn new(version: Version, status: StatusCode) -> Self {
877 let response = Response {
878 version,
879 status,
880 reason_phrase: String::new(),
881 headers: Headers::new(),
882 body: Empty,
883 };
884
885 Self(response, None)
886 }
887
888 pub fn reason_phrase<S: Into<String>>(mut self, reason_phrase: S) -> Self {
892 let reason_phrase = reason_phrase.into();
893
894 self.1 = Some(reason_phrase);
895
896 self
897 }
898
899 pub fn header<V: Into<HeaderValue>>(mut self, name: HeaderName, value: V) -> Self {
901 let value = value.into();
902
903 self.0.headers.append(name, value);
904
905 self
906 }
907
908 pub fn typed_header<H: TypedHeader>(mut self, header: &H) -> Self {
910 self.0.headers.insert_typed(header);
911
912 self
913 }
914
915 pub fn empty(self) -> Response<Empty> {
917 let ResponseBuilder(mut response, reason_phrase) = self;
918
919 response.reason_phrase = reason_phrase.unwrap_or_else(|| response.status.to_string());
920
921 response
922 }
923
924 pub fn build<Body: AsRef<[u8]>>(self, body: Body) -> Response<Body> {
928 let ResponseBuilder(mut response, reason_phrase) = self;
929
930 {
931 let body = body.as_ref();
932 if !body.is_empty() {
933 response.headers.insert(
934 crate::headers::CONTENT_LENGTH,
935 HeaderValue::from(format!("{}", body.len())),
936 );
937 }
938 }
939
940 let reason_phrase = reason_phrase.unwrap_or_else(|| response.status.to_string());
941
942 Response {
943 version: response.version,
944 status: response.status,
945 reason_phrase,
946 headers: response.headers,
947 body,
948 }
949 }
950}
951
952#[derive(Debug, Clone, Eq)]
957#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
958pub struct Data<Body> {
959 pub(crate) channel_id: u8,
960 pub(crate) body: Body,
961}
962
963impl<BodyA, BodyB: PartialEq<BodyA>> PartialEq<Data<BodyA>> for Data<BodyB> {
964 fn eq(&self, other: &Data<BodyA>) -> bool {
965 self.channel_id == other.channel_id && self.body == other.body
966 }
967}
968
969impl<Body> Data<Body> {
970 pub(crate) fn borrow(&self) -> DataRef
971 where
972 Body: AsRef<[u8]>,
973 {
974 DataRef {
975 channel_id: self.channel_id,
976 body: self.body.as_ref(),
977 }
978 }
979
980 pub fn new(channel_id: u8, body: Body) -> Self {
982 Self { channel_id, body }
983 }
984
985 pub fn write<'b, W: std::io::Write + 'b>(&self, w: &'b mut W) -> Result<(), WriteError>
990 where
991 Body: AsRef<[u8]>,
992 {
993 self.borrow().write(w)
994 }
995
996 pub fn write_len(&self) -> u64
998 where
999 Body: AsRef<[u8]>,
1000 {
1001 self.borrow().write_len()
1002 }
1003
1004 pub fn channel_id(&self) -> u8 {
1007 self.channel_id
1008 }
1009
1010 pub fn set_channel_id(&mut self, channel_id: u8) {
1012 self.channel_id = channel_id;
1013 }
1014
1015 pub fn len(&self) -> usize
1017 where
1018 Body: AsRef<[u8]>,
1019 {
1020 self.body.as_ref().len()
1021 }
1022
1023 pub fn is_empty(&self) -> bool
1025 where
1026 Body: AsRef<[u8]>,
1027 {
1028 self.body.as_ref().is_empty()
1029 }
1030
1031 pub fn as_slice(&self) -> &[u8]
1033 where
1034 Body: AsRef<[u8]>,
1035 {
1036 self.body.as_ref()
1037 }
1038
1039 pub fn into_body(self) -> Body {
1042 self.body
1043 }
1044
1045 pub fn map_body<NewBody, F: FnOnce(Body) -> NewBody>(self, func: F) -> Data<NewBody> {
1047 Data {
1048 channel_id: self.channel_id,
1049 body: func(self.body),
1050 }
1051 }
1052
1053 pub fn replace_body<NewBody>(self, new_body: NewBody) -> Data<NewBody> {
1055 Data {
1056 channel_id: self.channel_id,
1057 body: new_body,
1058 }
1059 }
1060}
1061
1062impl Data<Vec<u8>> {
1063 pub fn from_vec(channel_id: u8, body: Vec<u8>) -> Self {
1065 Self { channel_id, body }
1066 }
1067}
1068
1069impl<Body: AsRef<[u8]>> AsRef<[u8]> for Data<Body> {
1070 fn as_ref(&self) -> &[u8] {
1071 self.body.as_ref()
1072 }
1073}