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
use reqwest::Client;
use url::Url;

use crate::api;
use crate::api::request::ensure_success;

/// The Firefox Send version data endpoint.
const VERSION_ENDPOINT: &str = "__version__";

/// The endpoint we can probe to determine if the server is running Firefox Send v2.
#[cfg(feature = "send2")]
const V2_PROBE_ENDPOINT: &str = "jsconfig.js";

/// The endpoint we can probe to determine if the server is running Firefox Send v3.
#[cfg(feature = "send3")]
const V3_PROBE_ENDPOINT: &str = "app.webmanifest";

/// An action to attempt to find the API version of a Send server.
///
/// This returns a `Version` as probed, and will return an error if failed to properly determine
/// the server API version.
///
/// This API specification for this action is compatible with both Firefox Send v2 and v3.
pub struct Version {
    /// The server host.
    host: Url,
}

impl Version {
    /// Construct a new version action.
    pub fn new(host: Url) -> Self {
        Self { host }
    }

    /// Invoke the version action.
    pub fn invoke(self, client: &Client) -> Result<api::Version, Error> {
        // Attempt to fetch the version from the version endpoint
        let mut result = self.fetch_version(client);

        // Probe some server URLs to determine the API version if still unknown
        if let Err(Error::Unknown) = result {
            result = self.probe(client);
        }

        result
    }

    /// Fetch the version from the endpoint defined as `VERSION_ENDPOINT`.
    ///
    /// The internal request might fail, or the response provided by the server might hold an
    /// unsupported version. In these situations a corresponding `Error` is returned.
    ///
    /// This method does not work for development versions of Firefox Send, as the version
    /// configuration is not included with it.
    /// For those instances, use the `probe` method instead.
    fn fetch_version(&self, client: &Client) -> Result<api::Version, Error> {
        // Build the version URL, request the version
        let version_url = self.host.join(VERSION_ENDPOINT).expect("invalid host");
        let mut response = client.get(version_url).send().map_err(|_| Error::Request)?;

        // Ensure the status code is successful
        match ensure_success(&response) {
            Ok(_) => {}
            Err(_) => return Err(Error::Unknown),
        }

        // Parse the response and attempt to determine the version number
        let response = response
            .json::<VersionResponse>()
            .map_err(|_| Error::Unknown)?;
        response.determine_version()
    }

    /// Attempt to determine the server version by probing some known URLs.
    ///
    /// This method is not super reliable, but good enough for guessing the server version.
    /// `Error::Unknown` will be returned if the version could not be probed.
    ///
    /// Internally, this method will make some requests which might fail.
    /// These are cached and not immediately returned.
    ///
    /// # Panics
    ///
    /// This method panics if the host was invalid.
    fn probe(&self, client: &Client) -> Result<api::Version, Error> {
        // Probe Firefox Send v3
        #[cfg(feature = "send3")]
        {
            if self.exists(client, V3_PROBE_ENDPOINT) {
                return Ok(api::Version::V3);
            }
        }

        // Probe Firefox Send v2
        #[cfg(feature = "send2")]
        {
            if self.exists(client, V2_PROBE_ENDPOINT) {
                return Ok(api::Version::V2);
            }
        }

        // Unknown or unsupported version
        Err(Error::Unknown)
    }

    /// Check if a host endpoint exists.
    ///
    /// A GET request to the URL will be made, if the server responds with success or with a
    /// redirection (see [`StatusCode::is_success`](StatusCode::is_success) and
    /// [`StatusCode::is_redirection`](StatusCode::is_redirection)) true is returned,
    /// false otherwise.
    ///
    /// # Panics
    ///
    /// This method panics if the host was invalid.
    fn exists(&self, client: &Client, endpoint: &str) -> bool {
        let url = self.host.join(endpoint).expect("invalid host");
        client.get(url)
            .send()
            .map(|r| r.status())
            .map(|s| s.is_success() || s.is_redirection())
            .unwrap_or(false)
    }
}

/// The version response.
///
/// The server responds with this JSON object when accessing `/__version__`.
/// Unused fields are omitted.
#[derive(Debug, Deserialize)]
pub struct VersionResponse {
    /// The version string.
    version: String,
}

impl VersionResponse {
    /// Attempt to determine the API version for this response.
    ///
    /// If the API version is unsupported (or not compiled due to a missing compiler feature) an
    /// `Error::Unsupported` is returned holding the version number string.
    pub fn determine_version<'a>(&'a self) -> Result<api::Version, Error> {
        api::Version::parse(&self.version).map_err(|v| Error::Unsupported(v.into()))
    }
}

#[derive(Fail, Debug)]
pub enum Error {
    /// Sending the request to check whether the file exists failed.
    #[fail(display = "failed to send request to fetch server version")]
    Request,

    /// The server was not able to respond with any version identifiable information, the server
    /// version is unknown.
    #[fail(display = "failed to determine server version")]
    Unknown,

    /// The server responded with the given version string that is currently not supported and is
    /// unknown.
    /// This might be the result of a missing compiler feature for a given Firefox Send version.
    #[fail(display = "failed to determine server version, unsupported version")]
    Unsupported(String),
}