@@ -6,7 +6,7 @@ use std::cmp::Ordering;
6
6
use itertools:: Itertools ;
7
7
8
8
use crate :: Resource ;
9
- use crate :: context:: WebmachineRequest ;
9
+ use crate :: context:: { WebmachineContext , WebmachineRequest } ;
10
10
use crate :: headers:: HeaderValue ;
11
11
12
12
/// Enum to represent a match with media types
@@ -16,12 +16,22 @@ pub enum MediaTypeMatch {
16
16
Full ,
17
17
/// Match where the sub-type was a wild card
18
18
SubStar ,
19
- /// Full whild card match (type and sub-type)
19
+ /// Full wild card match (type and sub-type)
20
20
Star ,
21
21
/// Does not match
22
22
None
23
23
}
24
24
25
+ impl MediaTypeMatch {
26
+ /// If the match is a full or partial match
27
+ pub fn is_match ( & self ) -> bool {
28
+ match self {
29
+ MediaTypeMatch :: None => false ,
30
+ _ => true
31
+ }
32
+ }
33
+ }
34
+
25
35
/// Struct to represent a media type
26
36
#[ derive( Debug , Clone , PartialEq ) ]
27
37
pub struct MediaType {
@@ -74,15 +84,19 @@ impl MediaType {
74
84
75
85
/// If this media type matches the other media type
76
86
pub fn matches ( & self , other : & MediaType ) -> MediaTypeMatch {
77
- if other. main == "*" {
78
- MediaTypeMatch :: Star
79
- } else if self . main == other. main && other. sub == "*" {
80
- MediaTypeMatch :: SubStar
81
- } else if self . main == other. main && self . sub == other. sub {
82
- MediaTypeMatch :: Full
87
+ if other. main == "*" {
88
+ if other. sub == "*" || self . sub == other. sub {
89
+ MediaTypeMatch :: Star
83
90
} else {
84
- MediaTypeMatch :: None
91
+ MediaTypeMatch :: None
85
92
}
93
+ } else if self . main == other. main && other. sub == "*" {
94
+ MediaTypeMatch :: SubStar
95
+ } else if self . main == other. main && self . sub == other. sub {
96
+ MediaTypeMatch :: Full
97
+ } else {
98
+ MediaTypeMatch :: None
99
+ }
86
100
}
87
101
88
102
/// Converts this media type into a string
@@ -137,6 +151,20 @@ pub fn matching_content_type(resource: &(dyn Resource + Send + Sync), request: &
137
151
}
138
152
}
139
153
154
+ /// Determines if the media type accepted by the resource matches the media type
155
+ /// provided by the client. Returns the match if there is one.
156
+ pub fn acceptable_content_type (
157
+ resource : & ( dyn Resource + Send + Sync ) ,
158
+ context : & mut WebmachineContext
159
+ ) -> bool {
160
+ let ct = context. request . content_type ( ) . as_media_type ( ) ;
161
+ resource. acceptable_content_types ( context)
162
+ . iter ( )
163
+ . any ( |acceptable_ct| {
164
+ ct. matches ( & MediaType :: parse_string ( acceptable_ct) ) . is_match ( )
165
+ } )
166
+ }
167
+
140
168
/// Struct to represent a media language
141
169
#[ derive( Debug , Clone , PartialEq ) ]
142
170
pub struct MediaLanguage {
0 commit comments