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
//! Structs for client-side (outbound) WebSocket requests
use std::io::{Read, Write};

pub use url::Url;

use hyper::version::HttpVersion;
use hyper::buffer::BufReader;
use hyper::header::Headers;
use hyper::header::{Connection, ConnectionOption};
use hyper::header::{Upgrade, Protocol, ProtocolName};

use unicase::UniCase;

use header::{WebSocketKey, WebSocketVersion, WebSocketProtocol, WebSocketExtensions, Origin};
use result::WebSocketResult;
use client::response::Response;
use ws::util::url::ToWebSocketUrlComponents;

/// Represents a WebSocket request.
///
/// Note that nothing is written to the internal Writer until the `send()` method is called.
pub struct Request<R: Read, W: Write> {
    /// The HTTP version of this request.
    pub version: HttpVersion,
	/// The headers of this request.
	pub headers: Headers,
	
    resource_name: String,
	reader: BufReader<R>,
	writer: W,
}

unsafe impl<R, W> Send for Request<R, W> where R: Read + Send, W: Write + Send { }

impl<R: Read, W: Write> Request<R, W> {
	/// Creates a new client-side request.
	///
	/// In general `Client::connect()` should be used for connecting to servers.
	/// However, if the request is to be written to a different Writer, this function
	/// may be used.
	pub fn new<T: ToWebSocketUrlComponents>(components: T, reader: R, writer: W) -> WebSocketResult<Request<R, W>> {
		let mut headers = Headers::new();
		let (host, resource_name, _) = try!(components.to_components());
		headers.set(host);
		headers.set(Connection(vec![
			ConnectionOption::ConnectionHeader(UniCase("Upgrade".to_string()))
		]));
		headers.set(Upgrade(vec![Protocol{
			name: ProtocolName::WebSocket,
			version: None
		}]));
		headers.set(WebSocketVersion::WebSocket13);
		headers.set(WebSocketKey::new());
		
		Ok(Request {
			version: HttpVersion::Http11,
			headers: headers,
			resource_name: resource_name,
			reader: BufReader::new(reader),
			writer: writer
		})
	}
	/// Short-cut to obtain the WebSocketKey value.
	pub fn key(&self) -> Option<&WebSocketKey> {
		self.headers.get()
	}
	/// Short-cut to obtain the WebSocketVersion value.
	pub fn version(&self) -> Option<&WebSocketVersion> {
		self.headers.get()
	}
	/// Short-cut to obtain the WebSocketProtocol value.
	pub fn protocol(&self) -> Option<&WebSocketProtocol> {
		self.headers.get()
	}
	/// Short-cut to obtain the WebSocketExtensions value.
	pub fn extensions(&self) -> Option<&WebSocketExtensions> {
		self.headers.get()
	}
	/// Short-cut to obtain the Origin value.
	pub fn origin(&self) -> Option<&Origin> {
		self.headers.get()
	}
	/// Short-cut to obtain a mutable reference to the WebSocketKey value.
	///
	/// Note that to add a header that does not already exist, ```Request.headers.set()```
	/// must be used.
	pub fn key_mut(&mut self) -> Option<&mut WebSocketKey> {
		self.headers.get_mut()
	}
	/// Short-cut to obtain a mutable reference to the WebSocketVersion value.
	///
	/// Note that to add a header that does not already exist, ```Request.headers.set()```
	/// must be used.
	pub fn version_mut(&mut self) -> Option<&mut WebSocketVersion> {
		self.headers.get_mut()
	}
	/// Short-cut to obtaina mutable reference to  the WebSocketProtocol value.
	///
	/// Note that to add a header that does not already exist, ```Request.headers.set()```
	/// must be used.
	pub fn protocol_mut(&mut self) -> Option<&mut WebSocketProtocol> {
		self.headers.get_mut()
	}
	/// Short-cut to obtain a mutable reference to the WebSocketExtensions value.
	///
	/// Note that to add a header that does not already exist, ```Request.headers.set()```
	/// must be used.
	pub fn extensions_mut(&mut self) -> Option<&mut WebSocketExtensions> {
		self.headers.get_mut()
	}
	/// Short-cut to obtain a mutable reference to the Origin value.
	///
	/// Note that to add a header that does not already exist, ```Request.headers.set()```
	/// must be used.
	pub fn origin_mut(&mut self) -> Option<&mut Origin> {
		self.headers.get_mut()
	}
	/// Returns a reference to the inner Reader.
	pub fn get_reader(&self) -> &BufReader<R> {
		&self.reader
	}
	/// Returns a reference to the inner Writer.
	pub fn get_writer(&self) -> &W {
		&self.writer
	}
	/// Returns a mutable reference to the inner Reader.
	pub fn get_mut_reader(&mut self) -> &mut BufReader<R> {
		&mut self.reader
	}
	/// Returns a mutable reference to the inner Writer.
	pub fn get_mut_writer(&mut self) -> &mut W {
		&mut self.writer
	}
	/// Return the inner Reader and Writer.
	pub fn into_inner(self) -> (BufReader<R>, W) {
		(self.reader, self.writer)
	}
	/// Sends the request to the server and returns a response.
	pub fn send(mut self) -> WebSocketResult<Response<R, W>> {
		try!(write!(&mut self.writer, "GET {} {}\r\n", self.resource_name, self.version));
		try!(write!(&mut self.writer, "{}\r\n", self.headers));
		Response::read(self)
	}
}