{"action":"created","comment":{"url":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/comments/78422449","id":78422449,"diff_hunk":"@@ -0,0 +1,58 @@\n+//! A proxy that forwards data to another server and forwards that server's responses back\n+//! to clients.\n+\n+extern crate futures;\n+extern crate tokio_core;\n+\n+use std::env;\n+use std::net::{SocketAddr};\n+\n+use futures::Future;\n+use futures::stream::Stream;\n+use tokio_core::io::{copy, Io};\n+use tokio_core::net::{TcpListener, TcpStream};\n+use tokio_core::reactor::Core;\n+\n+fn main() {\n+    let listen_addr = env::args().nth(1).unwrap_or(\"127.0.0.1:8081\".to_string());\n+    let listen_addr = listen_addr.parse::<SocketAddr>().unwrap();\n+\n+    let server_addr = env::args().nth(2).unwrap_or(\"127.0.0.1:8080\".to_string());\n+    let server_addr = server_addr.parse::<SocketAddr>().unwrap();\n+\n+    // Create the event loop that will drive this server.\n+    let mut l = Core::new().unwrap();\n+    let handle = l.handle();\n+\n+    // Create a TCP listener which will listen for incoming connections.\n+    let socket = TcpListener::bind(&listen_addr, &l.handle()).unwrap();\n+    println!(\"Listening on: {}\", listen_addr);\n+\n+    let done = socket.incoming().for_each(move |(client, client_addr)| {\n+        let server = TcpStream::connect(&server_addr, &handle);\n+        let amounts = server.and_then(move |server| {\n+            let (client_read, client_write) = client.split();\n+            let (server_read, server_write) = server.split();\n+\n+            let from_client = copy(client_read, server_write)\n+                .and_then(|(_, mut server_write, n)| {\n+                    server_write.shutdown().map(|()| n)\n+                });\n+\n+            let from_server = copy(server_read, client_write).map(|(_, _, n)| n);","path":"examples/proxy.rs","position":null,"original_position":42,"commit_id":"b2b0ad1de95860c50a82b494ae81bd99c8a5fc28","original_commit_id":"4281f89d8101db02f49aa0019e96452113e65837","user":{"login":"dwrensha","id":495768,"avatar_url":"https://avatars.githubusercontent.com/u/495768?v=3","gravatar_id":"","url":"https://api.github.com/users/dwrensha","html_url":"https://github.com/dwrensha","followers_url":"https://api.github.com/users/dwrensha/followers","following_url":"https://api.github.com/users/dwrensha/following{/other_user}","gists_url":"https://api.github.com/users/dwrensha/gists{/gist_id}","starred_url":"https://api.github.com/users/dwrensha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dwrensha/subscriptions","organizations_url":"https://api.github.com/users/dwrensha/orgs","repos_url":"https://api.github.com/users/dwrensha/repos","events_url":"https://api.github.com/users/dwrensha/events{/privacy}","received_events_url":"https://api.github.com/users/dwrensha/received_events","type":"User","site_admin":false},"body":"Yep. Good point.","created_at":"2016-09-12T18:00:30Z","updated_at":"2016-09-12T18:00:30Z","html_url":"https://github.com/tokio-rs/tokio-core/pull/28#discussion_r78422449","pull_request_url":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28","_links":{"self":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/comments/78422449"},"html":{"href":"https://github.com/tokio-rs/tokio-core/pull/28#discussion_r78422449"},"pull_request":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28"}}},"pull_request":{"url":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28","id":84798234,"html_url":"https://github.com/tokio-rs/tokio-core/pull/28","diff_url":"https://github.com/tokio-rs/tokio-core/pull/28.diff","patch_url":"https://github.com/tokio-rs/tokio-core/pull/28.patch","issue_url":"https://api.github.com/repos/tokio-rs/tokio-core/issues/28","number":28,"state":"open","locked":false,"title":"proxy example and changes motivated by it","user":{"login":"dwrensha","id":495768,"avatar_url":"https://avatars.githubusercontent.com/u/495768?v=3","gravatar_id":"","url":"https://api.github.com/users/dwrensha","html_url":"https://github.com/dwrensha","followers_url":"https://api.github.com/users/dwrensha/followers","following_url":"https://api.github.com/users/dwrensha/following{/other_user}","gists_url":"https://api.github.com/users/dwrensha/gists{/gist_id}","starred_url":"https://api.github.com/users/dwrensha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dwrensha/subscriptions","organizations_url":"https://api.github.com/users/dwrensha/orgs","repos_url":"https://api.github.com/users/dwrensha/repos","events_url":"https://api.github.com/users/dwrensha/events{/privacy}","received_events_url":"https://api.github.com/users/dwrensha/received_events","type":"User","site_admin":false},"body":"I wanted to implement a simple proxy in the style of the [echo example](https://github.com/tokio-rs/tokio-core/blob/master/examples/echo.rs), but I ran into a problem: currently there's no way to `shutdown()` the write end of a `split()` socket.  A naive proxy that ignores this problem will fail to close connections properly.\r\n\r\nSo this pull request includes two changes to enable a simple proxy example:\r\n  1.  When complete, `io::copy()` returns the reader and writer, in addition to the number of bytes forwarded (following in the style of `io::read_exact()` and `io::write_all()`).\r\n  2. `ReadHalf` and `WriteHalf` gain `shutdown()` methods.\r\n","created_at":"2016-09-11T21:10:52Z","updated_at":"2016-09-12T18:00:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8fa1c4c7a616488cbf8d68f84723d23e1531cd6f","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28/commits","review_comments_url":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28/comments","review_comment_url":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/tokio-rs/tokio-core/issues/28/comments","statuses_url":"https://api.github.com/repos/tokio-rs/tokio-core/statuses/b2b0ad1de95860c50a82b494ae81bd99c8a5fc28","head":{"label":"dwrensha:proxy-example","ref":"proxy-example","sha":"b2b0ad1de95860c50a82b494ae81bd99c8a5fc28","user":{"login":"dwrensha","id":495768,"avatar_url":"https://avatars.githubusercontent.com/u/495768?v=3","gravatar_id":"","url":"https://api.github.com/users/dwrensha","html_url":"https://github.com/dwrensha","followers_url":"https://api.github.com/users/dwrensha/followers","following_url":"https://api.github.com/users/dwrensha/following{/other_user}","gists_url":"https://api.github.com/users/dwrensha/gists{/gist_id}","starred_url":"https://api.github.com/users/dwrensha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dwrensha/subscriptions","organizations_url":"https://api.github.com/users/dwrensha/orgs","repos_url":"https://api.github.com/users/dwrensha/repos","events_url":"https://api.github.com/users/dwrensha/events{/privacy}","received_events_url":"https://api.github.com/users/dwrensha/received_events","type":"User","site_admin":false},"repo":{"id":67354972,"name":"tokio-core","full_name":"dwrensha/tokio-core","owner":{"login":"dwrensha","id":495768,"avatar_url":"https://avatars.githubusercontent.com/u/495768?v=3","gravatar_id":"","url":"https://api.github.com/users/dwrensha","html_url":"https://github.com/dwrensha","followers_url":"https://api.github.com/users/dwrensha/followers","following_url":"https://api.github.com/users/dwrensha/following{/other_user}","gists_url":"https://api.github.com/users/dwrensha/gists{/gist_id}","starred_url":"https://api.github.com/users/dwrensha/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dwrensha/subscriptions","organizations_url":"https://api.github.com/users/dwrensha/orgs","repos_url":"https://api.github.com/users/dwrensha/repos","events_url":"https://api.github.com/users/dwrensha/events{/privacy}","received_events_url":"https://api.github.com/users/dwrensha/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dwrensha/tokio-core","description":"I/O primitives and event loop for async I/O in Rust","fork":true,"url":"https://api.github.com/repos/dwrensha/tokio-core","forks_url":"https://api.github.com/repos/dwrensha/tokio-core/forks","keys_url":"https://api.github.com/repos/dwrensha/tokio-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dwrensha/tokio-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dwrensha/tokio-core/teams","hooks_url":"https://api.github.com/repos/dwrensha/tokio-core/hooks","issue_events_url":"https://api.github.com/repos/dwrensha/tokio-core/issues/events{/number}","events_url":"https://api.github.com/repos/dwrensha/tokio-core/events","assignees_url":"https://api.github.com/repos/dwrensha/tokio-core/assignees{/user}","branches_url":"https://api.github.com/repos/dwrensha/tokio-core/branches{/branch}","tags_url":"https://api.github.com/repos/dwrensha/tokio-core/tags","blobs_url":"https://api.github.com/repos/dwrensha/tokio-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dwrensha/tokio-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dwrensha/tokio-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/dwrensha/tokio-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dwrensha/tokio-core/statuses/{sha}","languages_url":"https://api.github.com/repos/dwrensha/tokio-core/languages","stargazers_url":"https://api.github.com/repos/dwrensha/tokio-core/stargazers","contributors_url":"https://api.github.com/repos/dwrensha/tokio-core/contributors","subscribers_url":"https://api.github.com/repos/dwrensha/tokio-core/subscribers","subscription_url":"https://api.github.com/repos/dwrensha/tokio-core/subscription","commits_url":"https://api.github.com/repos/dwrensha/tokio-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/dwrensha/tokio-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/dwrensha/tokio-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/dwrensha/tokio-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/dwrensha/tokio-core/contents/{+path}","compare_url":"https://api.github.com/repos/dwrensha/tokio-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dwrensha/tokio-core/merges","archive_url":"https://api.github.com/repos/dwrensha/tokio-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dwrensha/tokio-core/downloads","issues_url":"https://api.github.com/repos/dwrensha/tokio-core/issues{/number}","pulls_url":"https://api.github.com/repos/dwrensha/tokio-core/pulls{/number}","milestones_url":"https://api.github.com/repos/dwrensha/tokio-core/milestones{/number}","notifications_url":"https://api.github.com/repos/dwrensha/tokio-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dwrensha/tokio-core/labels{/name}","releases_url":"https://api.github.com/repos/dwrensha/tokio-core/releases{/id}","deployments_url":"https://api.github.com/repos/dwrensha/tokio-core/deployments","created_at":"2016-09-04T16:07:40Z","updated_at":"2016-09-04T16:07:41Z","pushed_at":"2016-09-12T17:59:50Z","git_url":"git://github.com/dwrensha/tokio-core.git","ssh_url":"git@github.com:dwrensha/tokio-core.git","clone_url":"https://github.com/dwrensha/tokio-core.git","svn_url":"https://github.com/dwrensha/tokio-core","homepage":null,"size":928,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"tokio-rs:master","ref":"master","sha":"933b34cd25b7c32f3bd0138afd01417f4715fdc7","user":{"login":"tokio-rs","id":20248544,"avatar_url":"https://avatars.githubusercontent.com/u/20248544?v=3","gravatar_id":"","url":"https://api.github.com/users/tokio-rs","html_url":"https://github.com/tokio-rs","followers_url":"https://api.github.com/users/tokio-rs/followers","following_url":"https://api.github.com/users/tokio-rs/following{/other_user}","gists_url":"https://api.github.com/users/tokio-rs/gists{/gist_id}","starred_url":"https://api.github.com/users/tokio-rs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tokio-rs/subscriptions","organizations_url":"https://api.github.com/users/tokio-rs/orgs","repos_url":"https://api.github.com/users/tokio-rs/repos","events_url":"https://api.github.com/users/tokio-rs/events{/privacy}","received_events_url":"https://api.github.com/users/tokio-rs/received_events","type":"Organization","site_admin":false},"repo":{"id":66679486,"name":"tokio-core","full_name":"tokio-rs/tokio-core","owner":{"login":"tokio-rs","id":20248544,"avatar_url":"https://avatars.githubusercontent.com/u/20248544?v=3","gravatar_id":"","url":"https://api.github.com/users/tokio-rs","html_url":"https://github.com/tokio-rs","followers_url":"https://api.github.com/users/tokio-rs/followers","following_url":"https://api.github.com/users/tokio-rs/following{/other_user}","gists_url":"https://api.github.com/users/tokio-rs/gists{/gist_id}","starred_url":"https://api.github.com/users/tokio-rs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tokio-rs/subscriptions","organizations_url":"https://api.github.com/users/tokio-rs/orgs","repos_url":"https://api.github.com/users/tokio-rs/repos","events_url":"https://api.github.com/users/tokio-rs/events{/privacy}","received_events_url":"https://api.github.com/users/tokio-rs/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/tokio-rs/tokio-core","description":"I/O primitives and event loop for async I/O in Rust","fork":false,"url":"https://api.github.com/repos/tokio-rs/tokio-core","forks_url":"https://api.github.com/repos/tokio-rs/tokio-core/forks","keys_url":"https://api.github.com/repos/tokio-rs/tokio-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tokio-rs/tokio-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tokio-rs/tokio-core/teams","hooks_url":"https://api.github.com/repos/tokio-rs/tokio-core/hooks","issue_events_url":"https://api.github.com/repos/tokio-rs/tokio-core/issues/events{/number}","events_url":"https://api.github.com/repos/tokio-rs/tokio-core/events","assignees_url":"https://api.github.com/repos/tokio-rs/tokio-core/assignees{/user}","branches_url":"https://api.github.com/repos/tokio-rs/tokio-core/branches{/branch}","tags_url":"https://api.github.com/repos/tokio-rs/tokio-core/tags","blobs_url":"https://api.github.com/repos/tokio-rs/tokio-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tokio-rs/tokio-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tokio-rs/tokio-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/tokio-rs/tokio-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tokio-rs/tokio-core/statuses/{sha}","languages_url":"https://api.github.com/repos/tokio-rs/tokio-core/languages","stargazers_url":"https://api.github.com/repos/tokio-rs/tokio-core/stargazers","contributors_url":"https://api.github.com/repos/tokio-rs/tokio-core/contributors","subscribers_url":"https://api.github.com/repos/tokio-rs/tokio-core/subscribers","subscription_url":"https://api.github.com/repos/tokio-rs/tokio-core/subscription","commits_url":"https://api.github.com/repos/tokio-rs/tokio-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/tokio-rs/tokio-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/tokio-rs/tokio-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/tokio-rs/tokio-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/tokio-rs/tokio-core/contents/{+path}","compare_url":"https://api.github.com/repos/tokio-rs/tokio-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tokio-rs/tokio-core/merges","archive_url":"https://api.github.com/repos/tokio-rs/tokio-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tokio-rs/tokio-core/downloads","issues_url":"https://api.github.com/repos/tokio-rs/tokio-core/issues{/number}","pulls_url":"https://api.github.com/repos/tokio-rs/tokio-core/pulls{/number}","milestones_url":"https://api.github.com/repos/tokio-rs/tokio-core/milestones{/number}","notifications_url":"https://api.github.com/repos/tokio-rs/tokio-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tokio-rs/tokio-core/labels{/name}","releases_url":"https://api.github.com/repos/tokio-rs/tokio-core/releases{/id}","deployments_url":"https://api.github.com/repos/tokio-rs/tokio-core/deployments","created_at":"2016-08-26T21:09:01Z","updated_at":"2016-09-12T16:30:58Z","pushed_at":"2016-09-12T17:59:51Z","git_url":"git://github.com/tokio-rs/tokio-core.git","ssh_url":"git@github.com:tokio-rs/tokio-core.git","clone_url":"https://github.com/tokio-rs/tokio-core.git","svn_url":"https://github.com/tokio-rs/tokio-core","homepage":null,"size":920,"stargazers_count":70,"watchers_count":70,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":18,"mirror_url":null,"open_issues_count":8,"forks":18,"open_issues":8,"watchers":70,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28"},"html":{"href":"https://github.com/tokio-rs/tokio-core/pull/28"},"issue":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/issues/28"},"comments":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/issues/28/comments"},"review_comments":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28/comments"},"review_comment":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/pulls/28/commits"},"statuses":{"href":"https://api.github.com/repos/tokio-rs/tokio-core/statuses/b2b0ad1de95860c50a82b494ae81bd99c8a5fc28"}}}}
{"actor":{"display_login":"dwrensha"}}