Skip to content

Commit bc08eb7

Browse files
authored
Merge pull request fortanix#5 from FauxFaux/bump-include
Use pub(crate), not include!(), to access private fields
2 parents 3aee135 + ceb7c3a commit bc08eb7

File tree

11 files changed

+89
-423
lines changed

11 files changed

+89
-423
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
Cargo.lock
12
target
23
rls

Cargo.lock

Lines changed: 0 additions & 358 deletions
This file was deleted.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ tls = ["rustls", "webpki", "webpki-roots"]
2020
ascii = "0.9"
2121
base64 = "0.10"
2222
chunked_transfer = "1"
23-
cookie = { version = "0.11", features = ["percent-encode"] }
23+
cookie = { version = "0.12", features = ["percent-encode"] }
2424
lazy_static = "1"
2525
qstring = "0.6"
2626
url = "1"

src/agent.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use crate::error::Error;
2-
use crate::pool::ConnectionPool;
3-
use crate::response::{self, Response};
4-
use cookie::{Cookie, CookieJar};
1+
use std::sync::Arc;
52
use std::sync::Mutex;
63

7-
use crate::header::{add_header, get_all_headers, get_header, has_header, Header};
4+
use cookie::{Cookie, CookieJar};
85

9-
// to get to share private fields
10-
include!("request.rs");
11-
include!("unit.rs");
6+
use crate::header::{self, Header};
7+
use crate::pool::ConnectionPool;
8+
use crate::request::Request;
129

1310
/// Agents keep state between requests.
1411
///
@@ -41,9 +38,9 @@ include!("unit.rs");
4138
#[derive(Debug, Default, Clone)]
4239
pub struct Agent {
4340
/// Copied into each request of this agent.
44-
headers: Vec<Header>,
41+
pub(crate) headers: Vec<Header>,
4542
/// Reused agent state for repeated requests from this agent.
46-
state: Arc<Mutex<Option<AgentState>>>,
43+
pub(crate) state: Arc<Mutex<Option<AgentState>>>,
4744
}
4845

4946
/// Container of the state
@@ -52,9 +49,9 @@ pub struct Agent {
5249
#[derive(Debug)]
5350
pub(crate) struct AgentState {
5451
/// Reused connections between requests.
55-
pool: ConnectionPool,
52+
pub(crate) pool: ConnectionPool,
5653
/// Cookies saved between requests.
57-
jar: CookieJar,
54+
pub(crate) jar: CookieJar,
5855
}
5956

6057
impl AgentState {
@@ -113,7 +110,7 @@ impl Agent {
113110
/// }
114111
/// ```
115112
pub fn set(&mut self, header: &str, value: &str) -> &mut Agent {
116-
add_header(&mut self.headers, Header::new(header, value));
113+
header::add_header(&mut self.headers, Header::new(header, value));
117114
self
118115
}
119116

@@ -255,7 +252,7 @@ impl Agent {
255252
}
256253
}
257254

258-
fn basic_auth(user: &str, pass: &str) -> String {
255+
pub(crate) fn basic_auth(user: &str, pass: &str) -> String {
259256
let safe = match user.find(':') {
260257
Some(idx) => &user[..idx],
261258
None => user,

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,21 @@ mod body;
9797
mod error;
9898
mod header;
9999
mod pool;
100+
mod request;
100101
mod response;
101102
mod stream;
103+
mod unit;
102104

103105
#[cfg(feature = "json")]
104106
mod serde_macros;
105107

106108
#[cfg(test)]
107109
mod test;
108110

109-
pub use crate::agent::{Agent, Request};
111+
pub use crate::agent::Agent;
110112
pub use crate::error::Error;
111113
pub use crate::header::Header;
114+
pub use crate::request::Request;
112115
pub use crate::response::Response;
113116

114117
// re-export

src/pool.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use crate::agent::Unit;
2-
use crate::stream::Stream;
31
use std::collections::HashMap;
42
use std::io::{Read, Result as IoResult};
3+
4+
use crate::stream::Stream;
5+
use crate::unit::Unit;
6+
57
use url::Url;
68

79
pub const DEFAULT_HOST: &str = "localhost";

src/request.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
use std::io::Read;
2+
use std::sync::{Arc, Mutex};
3+
14
use lazy_static::lazy_static;
25
use qstring::QString;
3-
use std::io::Read;
4-
use std::sync::Arc;
6+
use url::Url;
7+
8+
use crate::agent::{self, Agent, AgentState};
9+
use crate::body::Payload;
10+
use crate::error::Error;
11+
use crate::header::{self, Header};
12+
use crate::pool;
13+
use crate::unit::{self, Unit};
14+
use crate::Response;
515

616
#[cfg(feature = "json")]
717
use super::SerdeValue;
@@ -22,27 +32,27 @@ lazy_static! {
2232
/// ```
2333
#[derive(Clone, Default)]
2434
pub struct Request {
25-
agent: Arc<Mutex<Option<AgentState>>>,
35+
pub(crate) agent: Arc<Mutex<Option<AgentState>>>,
2636

2737
// via agent
28-
method: String,
38+
pub(crate) method: String,
2939
path: String,
3040

3141
// from request itself
32-
headers: Vec<Header>,
33-
query: QString,
34-
timeout_connect: u64,
35-
timeout_read: u64,
36-
timeout_write: u64,
37-
redirects: u32,
42+
pub(crate) headers: Vec<Header>,
43+
pub(crate) query: QString,
44+
pub(crate) timeout_connect: u64,
45+
pub(crate) timeout_read: u64,
46+
pub(crate) timeout_write: u64,
47+
pub(crate) redirects: u32,
3848
}
3949

4050
impl ::std::fmt::Debug for Request {
4151
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
4252
let (path, query) = self
4353
.to_url()
4454
.map(|u| {
45-
let query = combine_query(&u, &self.query, true);
55+
let query = unit::combine_query(&u, &self.query, true);
4656
(u.path().to_string(), query)
4757
})
4858
.unwrap_or_else(|_| ("BAD_URL".to_string(), "BAD_URL".to_string()));
@@ -55,7 +65,7 @@ impl ::std::fmt::Debug for Request {
5565
}
5666

5767
impl Request {
58-
fn new(agent: &Agent, method: String, path: String) -> Request {
68+
pub(crate) fn new(agent: &Agent, method: String, path: String) -> Request {
5969
Request {
6070
agent: Arc::clone(&agent.state),
6171
method,
@@ -99,7 +109,7 @@ impl Request {
99109
.and_then(|url| {
100110
let reader = payload.into_read();
101111
let unit = Unit::new(&self, &url, true, &reader);
102-
connect(&self, unit, true, 0, reader, false)
112+
unit::connect(&self, unit, true, 0, reader, false)
103113
})
104114
.unwrap_or_else(|e| e.into())
105115
}
@@ -148,7 +158,8 @@ impl Request {
148158
/// ```
149159
pub fn send_string(&mut self, data: &str) -> Response {
150160
let text = data.into();
151-
let charset = response::charset_from_content_type(self.header("content-type")).to_string();
161+
let charset =
162+
crate::response::charset_from_content_type(self.header("content-type")).to_string();
152163
self.do_call(Payload::Text(text, charset))
153164
}
154165

@@ -185,7 +196,7 @@ impl Request {
185196
/// }
186197
/// ```
187198
pub fn set(&mut self, header: &str, value: &str) -> &mut Request {
188-
add_header(&mut self.headers, Header::new(header, value));
199+
header::add_header(&mut self.headers, Header::new(header, value));
189200
self
190201
}
191202

@@ -198,7 +209,7 @@ impl Request {
198209
/// assert_eq!("foobar", req.header("x-api-Key").unwrap());
199210
/// ```
200211
pub fn header<'a>(&self, name: &'a str) -> Option<&str> {
201-
get_header(&self.headers, name)
212+
header::get_header(&self.headers, name)
202213
}
203214

204215
/// A list of the set header names in this request. Lowercased to be uniform.
@@ -226,7 +237,7 @@ impl Request {
226237
/// assert_eq!(true, req.has("x-api-Key"));
227238
/// ```
228239
pub fn has<'a>(&self, name: &'a str) -> bool {
229-
has_header(&self.headers, name)
240+
header::has_header(&self.headers, name)
230241
}
231242

232243
/// All headers corresponding values for the give name, or empty vector.
@@ -242,7 +253,7 @@ impl Request {
242253
/// ]);
243254
/// ```
244255
pub fn all<'a>(&self, name: &'a str) -> Vec<&str> {
245-
get_all_headers(&self.headers, name)
256+
header::get_all_headers(&self.headers, name)
246257
}
247258

248259
/// Set a query parameter.
@@ -336,7 +347,7 @@ impl Request {
336347
/// println!("{:?}", r2);
337348
/// ```
338349
pub fn auth(&mut self, user: &str, pass: &str) -> &mut Request {
339-
let pass = basic_auth(user, pass);
350+
let pass = agent::basic_auth(user, pass);
340351
self.auth_kind("Basic", &pass)
341352
}
342353

@@ -440,7 +451,7 @@ impl Request {
440451
/// ```
441452
pub fn get_host(&self) -> Result<String, Error> {
442453
self.to_url()
443-
.map(|u| u.host_str().unwrap_or(DEFAULT_HOST).to_string())
454+
.map(|u| u.host_str().unwrap_or(pool::DEFAULT_HOST).to_string())
444455
}
445456

446457
/// Returns the scheme for this request.
@@ -465,7 +476,8 @@ impl Request {
465476
/// assert_eq!(req.get_query().unwrap(), "?foo=bar&format=json");
466477
/// ```
467478
pub fn get_query(&self) -> Result<String, Error> {
468-
self.to_url().map(|u| combine_query(&u, &self.query, true))
479+
self.to_url()
480+
.map(|u| unit::combine_query(&u, &self.query, true))
469481
}
470482

471483
/// The normalized path of this request.

src/response.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
use crate::agent::Unit;
1+
use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult};
2+
use std::str::FromStr;
3+
4+
use ascii::AsciiString;
5+
use chunked_transfer::Decoder as ChunkDecoder;
6+
7+
use crate::error::Error;
28
use crate::header::Header;
39
use crate::pool::PoolReturnRead;
410
use crate::stream::Stream;
5-
use ascii::AsciiString;
6-
use chunked_transfer::Decoder as ChunkDecoder;
7-
use std::io::{Cursor, Error as IoError, ErrorKind, Read, Result as IoResult};
8-
use std::str::FromStr;
11+
use crate::unit::Unit;
912

1013
#[cfg(feature = "json")]
1114
use serde_json;
@@ -15,8 +18,6 @@ use encoding::label::encoding_from_whatwg_label;
1518
#[cfg(feature = "charset")]
1619
use encoding::DecoderTrap;
1720

18-
use crate::error::Error;
19-
2021
pub const DEFAULT_CONTENT_TYPE: &str = "text/plain";
2122
pub const DEFAULT_CHARACTER_SET: &str = "utf-8";
2223

src/stream.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::agent::Unit;
2-
use crate::error::Error;
31
use std::io::{Cursor, Read, Result as IoResult, Write};
42
use std::net::SocketAddr;
53
use std::net::TcpStream;
64
use std::net::ToSocketAddrs;
75
use std::time::Duration;
86

7+
use crate::error::Error;
8+
use crate::unit::Unit;
9+
910
#[allow(clippy::large_enum_variant)]
1011
pub enum Stream {
1112
Http(TcpStream),
@@ -98,8 +99,8 @@ pub(crate) fn connect_http(unit: &Unit) -> Result<Stream, Error> {
9899

99100
#[cfg(feature = "tls")]
100101
pub(crate) fn connect_https(unit: &Unit) -> Result<Stream, Error> {
101-
use std::sync::Arc;
102102
use lazy_static::lazy_static;
103+
use std::sync::Arc;
103104

104105
lazy_static! {
105106
static ref TLS_CONF: Arc<rustls::ClientConfig> = {

src/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::agent::Unit;
21
use crate::error::Error;
32
use crate::stream::Stream;
3+
use crate::unit::Unit;
44
use lazy_static::lazy_static;
55
use std::collections::HashMap;
66
use std::io::{Cursor, Write};

0 commit comments

Comments
 (0)