Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: rust
script:
- cargo build --verbose
- cargo test --verbose
dist: trusty
sudo: false
language: rust
script:
- cargo build --verbose
- cargo test --verbose
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]

name = "civet"
version = "0.9.1"
version = "0.10.0"
authors = ["wycats"]
license = "MIT"
description = "civetweb-based server implementation for conduit"
Expand All @@ -14,7 +14,7 @@ libc = "0.2"

[dependencies.civet-sys]
path = "civet-sys"
version = "0.1.0"
version = "0.2.0"

[dev-dependencies]
route-recognizer = "0.1.0"
5 changes: 4 additions & 1 deletion civet-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "civet-sys"
version = "0.1.4"
version = "0.2.0"
authors = ["Alex Crichton <[email protected]>"]
links = "civet"
build = "build.rs"
Expand All @@ -12,3 +12,6 @@ description = "Native bindings to the libcivetweb library"
[lib]
name = "civet_sys"
path = "lib.rs"

[build-dependencies]
cmake = "0.1"
34 changes: 11 additions & 23 deletions civet-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
use std::env;
use std::fs;
use std::process::Command;
use std::path::Path;
extern crate cmake;

fn main() {
let dst = env::var("OUT_DIR").unwrap();

assert!(Command::new("make")
.current_dir("civetweb")
.arg("lib")
.arg(&format!("BUILD_DIR={}", dst))
.env("COPT", "-fPIC")
.status().unwrap().success());
use cmake::Config;

{
let src = Path::new("civetweb/libcivetweb.a");
let dst = Path::new(&dst).join("libcivetweb.a");
if fs::rename(&src, &dst).is_err() {
fs::copy(&src, &dst).unwrap();
fs::remove_file(&src).unwrap();
}
}

println!("cargo:rustc-flags=-L {} -l static=civetweb", dst);
fn main() {
let mut dst = Config::new("civetweb")
.define("CMAKE_BUILD_TYPE", "Release")
.define("BUILD_TESTING", "OFF")
.define("CIVETWEB_ALLOW_WARNINGS", "ON")
.build();
dst.push("lib");
println!("cargo:rustc-link-search=native={}", dst.display());
println!("cargo:rustc-link-lib=static=civetweb");
}
2 changes: 1 addition & 1 deletion civet-sys/civetweb
43 changes: 21 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ extern crate civet_sys as ffi;
use std::collections::HashMap;
use std::io::prelude::*;
use std::io::{self, BufWriter};
use std::net::{SocketAddr, Ipv4Addr, SocketAddrV4};
use std::net::{SocketAddr, IpAddr, Ipv4Addr};
use std::str::FromStr;

use conduit::{Handler, Extensions, TypeMap, Method, Scheme, Host};

Expand Down Expand Up @@ -97,12 +98,9 @@ impl<'a> conduit::Request for CivetRequest<'a> {
}

fn remote_addr(&self) -> SocketAddr {
let ip = self.request_info.remote_ip();
let ip = Ipv4Addr::new((ip >> 24) as u8,
(ip >> 16) as u8,
(ip >> 8) as u8,
(ip >> 0) as u8);
SocketAddr::V4(SocketAddrV4::new(ip, self.request_info.remote_port()))
let ip = self.request_info.remote_addr();
let ip = IpAddr::from_str(ip).unwrap_or_else(|_| Ipv4Addr::new(0, 0, 0, 0).into());
SocketAddr::new(ip, self.request_info.remote_port())
}

fn content_length(&self) -> Option<u64> {
Expand Down Expand Up @@ -318,6 +316,7 @@ mod test {
}

#[test]
#[cfg(target_os = "windows")]
fn dupe_port() {
let port = port();
let s1 = Server::start(cfg(port), noop);
Expand Down Expand Up @@ -360,9 +359,9 @@ mod test {
let ip = Ipv4Addr::new(127, 0, 0, 1);
let addr = SocketAddr::V4(SocketAddrV4::new(ip, port));
let _s = Server::start(cfg(port), handler);
request(addr, r"
GET / HTTP/1.1

request(addr, "\r
GET / HTTP/1.1\r
\r
");
rx.recv().unwrap();
}
Expand All @@ -385,10 +384,10 @@ GET / HTTP/1.1
let ip = Ipv4Addr::new(127, 0, 0, 1);
let addr = SocketAddr::V4(SocketAddrV4::new(ip, port));
let _s = Server::start(cfg(port), handler);
request(addr, r"
GET / HTTP/1.1
Foo: bar

request(addr, "\r
GET / HTTP/1.1\r
Foo: bar\r
\r
");
assert_eq!(rx.recv().unwrap(), "bar");
}
Expand All @@ -406,10 +405,10 @@ Foo: bar
let ip = Ipv4Addr::new(127, 0, 0, 1);
let addr = SocketAddr::V4(SocketAddrV4::new(ip, port));
let _s = Server::start(cfg(port), Foo);
request(addr, r"
GET / HTTP/1.1
Foo: bar

request(addr, "\r
GET / HTTP/1.1\r
Foo: bar\r
\r
");
}

Expand All @@ -426,10 +425,10 @@ Foo: bar
let ip = Ipv4Addr::new(127, 0, 0, 1);
let addr = SocketAddr::V4(SocketAddrV4::new(ip, port));
let _s = Server::start(cfg(port), Foo);
let response = request(addr, r"
GET / HTTP/1.1
Foo: bar

let response = request(addr, "\r
GET / HTTP/1.1\r
Foo: bar\r
\r
");
assert!(response.contains("500 Internal"),
"not a failing response: {}", response);
Expand Down
40 changes: 25 additions & 15 deletions src/raw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use libc::{c_void,c_char,c_int,c_long,size_t};
use libc::{c_void,c_char,c_int,c_longlong,size_t};
use std::ffi::{CStr, CString};
use std::io;
use std::marker;
Expand Down Expand Up @@ -132,19 +132,25 @@ impl<'a> Header<'a> {
#[repr(C)]
pub struct MgRequestInfo {
request_method: *const c_char,
request_uri: *const c_char,
local_uri: *const c_char,
uri: *const c_char,
http_version: *const c_char,
query_string: *const c_char,
remote_user: *const c_char,
remote_ip: c_long,
remote_addr: [c_char; 48],
content_length: c_longlong,
remote_port: c_int,
is_ssl: c_int,

user_data: *mut c_void,
conn_data: *mut c_void,

num_headers: c_int,
headers: [MgHeader; 64]
headers: [MgHeader; 64],

client_cert: *mut c_void,
accepted_websocket_subprotocol: *const c_char
}

pub struct RequestInfo<'a> {
Expand Down Expand Up @@ -177,8 +183,12 @@ impl<'a> RequestInfo<'a> {
to_slice(self.as_ref(), |info| info.query_string)
}

pub fn remote_ip(&self) -> i32 {
self.as_ref().remote_ip as i32
pub fn remote_addr(&self) -> &str {
unsafe {
CStr::from_ptr(
self.as_ref().remote_addr.as_ptr()
).to_str().unwrap_or("0.0.0.0")
}
}

pub fn remote_port(&self) -> u16 {
Expand All @@ -195,15 +205,15 @@ struct MgCallbacks {
begin_request: *const c_void,
end_request: *const c_void,
log_message: *const c_void,
log_access: *const c_void,
init_ssl: *const c_void,
websocket_connect: *const c_void,
websocket_ready: *const c_void,
websocket_data: *const c_void,
connection_close: *const c_void,
open_file: *const c_void,
init_lua: *const c_void,
upload: *const c_void,
http_error: *const c_void
http_error: *const c_void,
init_context: *const c_void,
init_thread: *const c_void,
exit_context: *const c_void
}

impl MgCallbacks {
Expand All @@ -212,15 +222,15 @@ impl MgCallbacks {
begin_request: null(),
end_request: null(),
log_message: null(),
log_access: null(),
init_ssl: null(),
websocket_connect: null(),
websocket_ready: null(),
websocket_data: null(),
connection_close: null(),
open_file: null(),
init_lua: null(),
upload: null(),
http_error: null()
http_error: null(),
init_context: null(),
init_thread: null(),
exit_context: null()
}
}
}
Expand Down