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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! The [Response](https://fetch.spec.whatwg.org/#responses) object
//! resulting from a [fetch operation](https://fetch.spec.whatwg.org/#concept-fetch)
use std::sync::atomic::AtomicBool;
use std::sync::Mutex;

use headers::{ContentType, HeaderMapExt};
use http::HeaderMap;
use hyper_serde::Serde;
use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize};
use servo_arc::Arc;
use servo_url::ServoUrl;

use crate::http_status::HttpStatus;
use crate::{
    FetchMetadata, FilteredMetadata, Metadata, NetworkError, ReferrerPolicy, ResourceFetchTiming,
    ResourceTimingType,
};

/// [Response type](https://fetch.spec.whatwg.org/#concept-response-type)
#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum ResponseType {
    Basic,
    Cors,
    Default,
    Error(NetworkError),
    Opaque,
    OpaqueRedirect,
}

/// [Response termination reason](https://fetch.spec.whatwg.org/#concept-response-termination-reason)
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, Serialize)]
pub enum TerminationReason {
    EndUserAbort,
    Fatal,
    Timeout,
}

/// The response body can still be pushed to after fetch
/// This provides a way to store unfinished response bodies
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
pub enum ResponseBody {
    Empty, // XXXManishearth is this necessary, or is Done(vec![]) enough?
    Receiving(Vec<u8>),
    Done(Vec<u8>),
}

impl ResponseBody {
    pub fn is_done(&self) -> bool {
        match *self {
            ResponseBody::Done(..) => true,
            ResponseBody::Empty | ResponseBody::Receiving(..) => false,
        }
    }
}

/// [Cache state](https://fetch.spec.whatwg.org/#concept-response-cache-state)
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
pub enum CacheState {
    None,
    Local,
    Validated,
    Partial,
}

/// [Https state](https://fetch.spec.whatwg.org/#concept-response-https-state)
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum HttpsState {
    None,
    Deprecated,
    Modern,
}

#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
pub struct ResponseInit {
    pub url: ServoUrl,
    #[serde(
        deserialize_with = "::hyper_serde::deserialize",
        serialize_with = "::hyper_serde::serialize"
    )]
    #[ignore_malloc_size_of = "Defined in hyper"]
    pub headers: HeaderMap,
    pub status_code: u16,
    pub referrer: Option<ServoUrl>,
    pub location_url: Option<Result<ServoUrl, String>>,
}

/// A [Response](https://fetch.spec.whatwg.org/#concept-response) as defined by the Fetch spec
#[derive(Clone, Debug, MallocSizeOf)]
pub struct Response {
    pub response_type: ResponseType,
    pub termination_reason: Option<TerminationReason>,
    url: Option<ServoUrl>,
    pub url_list: Vec<ServoUrl>,
    pub status: HttpStatus,
    #[ignore_malloc_size_of = "Defined in hyper"]
    pub headers: HeaderMap,
    #[ignore_malloc_size_of = "Mutex heap size undefined"]
    pub body: Arc<Mutex<ResponseBody>>,
    pub cache_state: CacheState,
    pub https_state: HttpsState,
    pub referrer: Option<ServoUrl>,
    pub referrer_policy: ReferrerPolicy,
    /// [CORS-exposed header-name list](https://fetch.spec.whatwg.org/#concept-response-cors-exposed-header-name-list)
    pub cors_exposed_header_name_list: Vec<String>,
    /// [Location URL](https://fetch.spec.whatwg.org/#concept-response-location-url)
    pub location_url: Option<Result<ServoUrl, String>>,
    /// [Internal response](https://fetch.spec.whatwg.org/#concept-internal-response), only used if the Response
    /// is a filtered response
    pub internal_response: Option<Box<Response>>,
    /// whether or not to try to return the internal_response when asked for actual_response
    pub return_internal: bool,
    /// <https://fetch.spec.whatwg.org/#concept-response-aborted>
    #[ignore_malloc_size_of = "AtomicBool heap size undefined"]
    pub aborted: Arc<AtomicBool>,
    /// track network metrics
    #[ignore_malloc_size_of = "Mutex heap size undefined"]
    pub resource_timing: Arc<Mutex<ResourceFetchTiming>>,
}

impl Response {
    pub fn new(url: ServoUrl, resource_timing: ResourceFetchTiming) -> Response {
        Response {
            response_type: ResponseType::Default,
            termination_reason: None,
            url: Some(url),
            url_list: vec![],
            status: HttpStatus::default(),
            headers: HeaderMap::new(),
            body: Arc::new(Mutex::new(ResponseBody::Empty)),
            cache_state: CacheState::None,
            https_state: HttpsState::None,
            referrer: None,
            referrer_policy: ReferrerPolicy::EmptyString,
            cors_exposed_header_name_list: vec![],
            location_url: None,
            internal_response: None,
            return_internal: true,
            aborted: Arc::new(AtomicBool::new(false)),
            resource_timing: Arc::new(Mutex::new(resource_timing)),
        }
    }

    pub fn from_init(init: ResponseInit, resource_timing_type: ResourceTimingType) -> Response {
        let mut res = Response::new(init.url, ResourceFetchTiming::new(resource_timing_type));
        res.location_url = init.location_url;
        res.headers = init.headers;
        res.referrer = init.referrer;
        res.status = HttpStatus::new_raw(init.status_code, vec![]);
        res
    }

    pub fn network_error(e: NetworkError) -> Response {
        Response {
            response_type: ResponseType::Error(e),
            termination_reason: None,
            url: None,
            url_list: vec![],
            status: HttpStatus::new_error(),
            headers: HeaderMap::new(),
            body: Arc::new(Mutex::new(ResponseBody::Empty)),
            cache_state: CacheState::None,
            https_state: HttpsState::None,
            referrer: None,
            referrer_policy: ReferrerPolicy::EmptyString,
            cors_exposed_header_name_list: vec![],
            location_url: None,
            internal_response: None,
            return_internal: true,
            aborted: Arc::new(AtomicBool::new(false)),
            resource_timing: Arc::new(Mutex::new(ResourceFetchTiming::new(
                ResourceTimingType::Error,
            ))),
        }
    }

    pub fn network_internal_error<T: Into<String>>(msg: T) -> Response {
        Self::network_error(NetworkError::Internal(msg.into()))
    }

    pub fn url(&self) -> Option<&ServoUrl> {
        self.url.as_ref()
    }

    pub fn is_network_error(&self) -> bool {
        matches!(self.response_type, ResponseType::Error(..))
    }

    pub fn get_network_error(&self) -> Option<&NetworkError> {
        match self.response_type {
            ResponseType::Error(ref e) => Some(e),
            _ => None,
        }
    }

    pub fn actual_response(&self) -> &Response {
        if self.return_internal && self.internal_response.is_some() {
            self.internal_response.as_ref().unwrap()
        } else {
            self
        }
    }

    pub fn actual_response_mut(&mut self) -> &mut Response {
        if self.return_internal && self.internal_response.is_some() {
            self.internal_response.as_mut().unwrap()
        } else {
            self
        }
    }

    pub fn to_actual(self) -> Response {
        if self.return_internal && self.internal_response.is_some() {
            *self.internal_response.unwrap()
        } else {
            self
        }
    }

    pub fn get_resource_timing(&self) -> Arc<Mutex<ResourceFetchTiming>> {
        Arc::clone(&self.resource_timing)
    }

    /// Convert to a filtered response, of type `filter_type`.
    /// Do not use with type Error or Default
    #[rustfmt::skip]
    pub fn to_filtered(self, filter_type: ResponseType) -> Response {
        match filter_type {
            ResponseType::Default |
            ResponseType::Error(..) => panic!(),
            _ => (),
        }

        let old_response = self.to_actual();

        if let ResponseType::Error(e) = old_response.response_type {
            return Response::network_error(e);
        }

        let old_headers = old_response.headers.clone();
        let exposed_headers = old_response.cors_exposed_header_name_list.clone();
        let mut response = old_response.clone();
        response.internal_response = Some(Box::new(old_response));
        response.response_type = filter_type;

        match response.response_type {
            ResponseType::Default |
            ResponseType::Error(..) => unreachable!(),

            ResponseType::Basic => {
                let headers = old_headers.iter().filter(|(name, _)| {
                    !matches!(&*name.as_str().to_ascii_lowercase(), "set-cookie" | "set-cookie2")
                }).map(|(n, v)| (n.clone(), v.clone())).collect();
                response.headers = headers;
            },

            ResponseType::Cors => {
                let headers = old_headers.iter().filter(|(name, _)| {
                    match &*name.as_str().to_ascii_lowercase() {
                        "cache-control" | "content-language" | "content-length" | "content-type" |
                        "expires" | "last-modified" | "pragma" => true,
                        "set-cookie" | "set-cookie2" => false,
                        header => {
                            exposed_headers.iter().any(|h| *header == h.as_str().to_ascii_lowercase())
                        }
                    }
                }).map(|(n, v)| (n.clone(), v.clone())).collect();
                response.headers = headers;
            },

            ResponseType::Opaque => {
                response.url_list = vec![];
                response.url = None;
                response.headers = HeaderMap::new();
                response.status = HttpStatus::new_error();
                response.body = Arc::new(Mutex::new(ResponseBody::Empty));
                response.cache_state = CacheState::None;
            },

            ResponseType::OpaqueRedirect => {
                response.headers = HeaderMap::new();
                response.status = HttpStatus::new_error();
                response.body = Arc::new(Mutex::new(ResponseBody::Empty));
                response.cache_state = CacheState::None;
            },
        }

        response
    }

    pub fn metadata(&self) -> Result<FetchMetadata, NetworkError> {
        fn init_metadata(response: &Response, url: &ServoUrl) -> Metadata {
            let mut metadata = Metadata::default(url.clone());
            metadata.set_content_type(
                response
                    .headers
                    .typed_get::<ContentType>()
                    .map(|v| v.into())
                    .as_ref(),
            );
            metadata.location_url.clone_from(&response.location_url);
            metadata.headers = Some(Serde(response.headers.clone()));
            metadata.status.clone_from(&response.status);
            metadata.https_state = response.https_state;
            metadata.referrer.clone_from(&response.referrer);
            metadata.referrer_policy = response.referrer_policy;
            metadata.redirected = response.actual_response().url_list.len() > 1;
            metadata
        }

        if let Some(error) = self.get_network_error() {
            return Err(error.clone());
        }

        let metadata = self.url.as_ref().map(|url| init_metadata(self, url));

        if let Some(ref response) = self.internal_response {
            match response.url {
                Some(ref url) => {
                    let unsafe_metadata = init_metadata(response, url);

                    match self.response_type {
                        ResponseType::Basic => Ok(FetchMetadata::Filtered {
                            filtered: FilteredMetadata::Basic(metadata.unwrap()),
                            unsafe_: unsafe_metadata,
                        }),
                        ResponseType::Cors => Ok(FetchMetadata::Filtered {
                            filtered: FilteredMetadata::Cors(metadata.unwrap()),
                            unsafe_: unsafe_metadata,
                        }),
                        ResponseType::Default => unreachable!(),
                        ResponseType::Error(ref network_err) => Err(network_err.clone()),
                        ResponseType::Opaque => Ok(FetchMetadata::Filtered {
                            filtered: FilteredMetadata::Opaque,
                            unsafe_: unsafe_metadata,
                        }),
                        ResponseType::OpaqueRedirect => Ok(FetchMetadata::Filtered {
                            filtered: FilteredMetadata::OpaqueRedirect(url.clone()),
                            unsafe_: unsafe_metadata,
                        }),
                    }
                },
                None => Err(NetworkError::Internal(
                    "No url found in unsafe response".to_owned(),
                )),
            }
        } else {
            assert_eq!(self.response_type, ResponseType::Default);
            Ok(FetchMetadata::Unfiltered(metadata.unwrap()))
        }
    }
}