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
/* 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 listener that encapsulates all state for an in-progress document request.
//! Any redirects that are encountered are followed. Whenever a non-redirect
//! response is received, it is forwarded to the appropriate script thread.

use base::id::PipelineId;
use crossbeam_channel::Sender;
use http::{header, HeaderMap};
use ipc_channel::ipc;
use ipc_channel::router::ROUTER;
use log::warn;
use net::http_loader::{set_default_accept_language, DOCUMENT_ACCEPT_HEADER_VALUE};
use net_traits::request::{Referrer, RequestBuilder, RequestId};
use net_traits::response::ResponseInit;
use net_traits::{
    CoreResourceMsg, FetchChannels, FetchMetadata, FetchResponseMsg, IpcSend, NetworkError,
    ResourceThreads,
};

pub struct NetworkListener {
    res_init: Option<ResponseInit>,
    request_builder: RequestBuilder,
    pipeline_id: PipelineId,
    resource_threads: ResourceThreads,
    sender: Sender<(PipelineId, FetchResponseMsg)>,
    should_send: bool,
}

impl NetworkListener {
    pub fn new(
        request_builder: RequestBuilder,
        pipeline_id: PipelineId,
        resource_threads: ResourceThreads,
        sender: Sender<(PipelineId, FetchResponseMsg)>,
    ) -> NetworkListener {
        NetworkListener {
            res_init: None,
            request_builder,
            pipeline_id,
            resource_threads,
            sender,
            should_send: false,
        }
    }

    pub fn initiate_fetch(&self, cancel_chan: Option<ipc::IpcReceiver<()>>) {
        let (ipc_sender, ipc_receiver) = ipc::channel().expect("Failed to create IPC channel!");

        let mut listener = NetworkListener {
            res_init: self.res_init.clone(),
            request_builder: self.request_builder.clone(),
            resource_threads: self.resource_threads.clone(),
            sender: self.sender.clone(),
            pipeline_id: self.pipeline_id,
            should_send: false,
        };

        let msg = match self.res_init {
            Some(ref res_init_) => CoreResourceMsg::FetchRedirect(
                self.request_builder.clone(),
                res_init_.clone(),
                ipc_sender,
                None,
            ),
            None => {
                if !listener
                    .request_builder
                    .headers
                    .contains_key(header::ACCEPT)
                {
                    listener
                        .request_builder
                        .headers
                        .insert(header::ACCEPT, DOCUMENT_ACCEPT_HEADER_VALUE);
                }

                set_default_accept_language(&mut listener.request_builder.headers);

                CoreResourceMsg::Fetch(
                    listener.request_builder.clone(),
                    FetchChannels::ResponseMsg(ipc_sender, cancel_chan),
                )
            },
        };

        ROUTER.add_typed_route(
            ipc_receiver,
            Box::new(move |message| {
                match message {
                    Ok(FetchResponseMsg::ProcessResponse(request_id, res)) => {
                        listener.check_redirect(request_id, res)
                    },
                    Ok(msg_) => listener.send(msg_),
                    Err(e) => warn!("Error while receiving network listener message: {}", e),
                };
            }),
        );

        if let Err(e) = self.resource_threads.sender().send(msg) {
            warn!("Resource thread unavailable ({})", e);
        }
    }

    fn check_redirect(
        &mut self,
        request_id: RequestId,
        message: Result<FetchMetadata, NetworkError>,
    ) {
        match message {
            Ok(res_metadata) => {
                let metadata = match res_metadata {
                    FetchMetadata::Filtered { ref unsafe_, .. } => unsafe_,
                    FetchMetadata::Unfiltered(ref m) => m,
                };

                match metadata.location_url {
                    // https://html.spec.whatwg.org/multipage/#process-a-navigate-fetch
                    // Step 7-4.
                    Some(Ok(ref location_url))
                        if matches!(location_url.scheme(), "http" | "https") =>
                    {
                        if self.request_builder.url_list.is_empty() {
                            self.request_builder
                                .url_list
                                .push(self.request_builder.url.clone());
                        }
                        self.request_builder
                            .url_list
                            .push(metadata.final_url.clone());

                        self.request_builder.referrer = metadata
                            .referrer
                            .clone()
                            .map(Referrer::ReferrerUrl)
                            .unwrap_or(Referrer::NoReferrer);
                        self.request_builder.referrer_policy = metadata.referrer_policy;

                        let headers = if let Some(ref headers) = metadata.headers {
                            headers.clone().into_inner()
                        } else {
                            HeaderMap::new()
                        };

                        self.res_init = Some(ResponseInit {
                            url: metadata.final_url.clone(),
                            location_url: metadata.location_url.clone(),
                            headers,
                            referrer: metadata.referrer.clone(),
                            status_code: metadata
                                .status
                                .try_code()
                                .map(|code| code.as_u16())
                                .unwrap_or(200),
                        });

                        // XXXManishearth we don't have the cancel_chan anymore and
                        // can't use it here.
                        //
                        // Ideally the Fetch code would handle manual redirects on its own
                        self.initiate_fetch(None);
                    },
                    _ => {
                        // Response should be processed by script thread.
                        self.should_send = true;
                        self.send(FetchResponseMsg::ProcessResponse(
                            request_id,
                            Ok(res_metadata),
                        ));
                    },
                };
            },
            Err(e) => {
                self.should_send = true;
                self.send(FetchResponseMsg::ProcessResponse(request_id, Err(e)))
            },
        };
    }

    fn send(&mut self, msg: FetchResponseMsg) {
        if self.should_send {
            if let Err(e) = self.sender.send((self.pipeline_id, msg)) {
                warn!(
                    "Failed to forward network message to pipeline {:?}: {:?}",
                    self.pipeline_id, e
                );
            }
        }
    }
}