You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trpl-zh-cn/ch20-01-single-threaded.html

599 lines
58 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE HTML>
<html lang="zh-CN" class="light" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>建立单线程 web server - Rust 程序设计语言 简体中文版</title>
<!-- Custom HTML head -->
<meta name="description" content="Rust 程序设计语言 简体中文版">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="highlight.css">
<link rel="stylesheet" href="tomorrow-night.css">
<link rel="stylesheet" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="ferris.css">
<link rel="stylesheet" href="theme/2018-edition.css">
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('light')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item expanded affix "><a href="title-page.html">Rust 程序设计语言</a></li><li class="chapter-item expanded affix "><a href="foreword.html">前言</a></li><li class="chapter-item expanded affix "><a href="ch00-00-introduction.html">简介</a></li><li class="chapter-item expanded "><a href="ch01-00-getting-started.html"><strong aria-hidden="true">1.</strong> 入门指南</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch01-01-installation.html"><strong aria-hidden="true">1.1.</strong> 安装</a></li><li class="chapter-item expanded "><a href="ch01-02-hello-world.html"><strong aria-hidden="true">1.2.</strong> Hello, World!</a></li><li class="chapter-item expanded "><a href="ch01-03-hello-cargo.html"><strong aria-hidden="true">1.3.</strong> Hello, Cargo!</a></li></ol></li><li class="chapter-item expanded "><a href="ch02-00-guessing-game-tutorial.html"><strong aria-hidden="true">2.</strong> 写个猜数字游戏</a></li><li class="chapter-item expanded "><a href="ch03-00-common-programming-concepts.html"><strong aria-hidden="true">3.</strong> 常见编程概念</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch03-01-variables-and-mutability.html"><strong aria-hidden="true">3.1.</strong> 变量与可变性</a></li><li class="chapter-item expanded "><a href="ch03-02-data-types.html"><strong aria-hidden="true">3.2.</strong> 数据类型</a></li><li class="chapter-item expanded "><a href="ch03-03-how-functions-work.html"><strong aria-hidden="true">3.3.</strong> 函数</a></li><li class="chapter-item expanded "><a href="ch03-04-comments.html"><strong aria-hidden="true">3.4.</strong> 注释</a></li><li class="chapter-item expanded "><a href="ch03-05-control-flow.html"><strong aria-hidden="true">3.5.</strong> 控制流</a></li></ol></li><li class="chapter-item expanded "><a href="ch04-00-understanding-ownership.html"><strong aria-hidden="true">4.</strong> 认识所有权</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch04-01-what-is-ownership.html"><strong aria-hidden="true">4.1.</strong> 什么是所有权?</a></li><li class="chapter-item expanded "><a href="ch04-02-references-and-borrowing.html"><strong aria-hidden="true">4.2.</strong> 引用与借用</a></li><li class="chapter-item expanded "><a href="ch04-03-slices.html"><strong aria-hidden="true">4.3.</strong> Slice 类型</a></li></ol></li><li class="chapter-item expanded "><a href="ch05-00-structs.html"><strong aria-hidden="true">5.</strong> 使用结构体组织相关联的数据</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch05-01-defining-structs.html"><strong aria-hidden="true">5.1.</strong> 结构体的定义和实例化</a></li><li class="chapter-item expanded "><a href="ch05-02-example-structs.html"><strong aria-hidden="true">5.2.</strong> 结构体示例程序</a></li><li class="chapter-item expanded "><a href="ch05-03-method-syntax.html"><strong aria-hidden="true">5.3.</strong> 方法语法</a></li></ol></li><li class="chapter-item expanded "><a href="ch06-00-enums.html"><strong aria-hidden="true">6.</strong> 枚举和模式匹配</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch06-01-defining-an-enum.html"><strong aria-hidden="true">6.1.</strong> 枚举的定义</a></li><li class="chapter-item expanded "><a href="ch06-02-match.html"><strong aria-hidden="true">6.2.</strong> match 控制流结构</a></li><li class="chapter-item expanded "><a href="ch06-03-if-let.html"><strong aria-hidden="true">6.3.</strong> if let 简洁控制流</a></li></ol></li><li class="chapter-item expanded "><a href="ch07-00-managing-growing-projects-with-packages-crates-and-modules.html"><strong aria-hidden="true">7.</strong> 使用包、Crate 和模块管理不断增长的项目</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch07-01-packages-and-crates.html"><strong aria-hidden="true">7.1.</strong> 包和 Crate</a></li><li class="chapter-item expanded "><a href="ch07-02-defining-modules-to-control-scope-and-privacy.html"><strong aria-hidden="true">7.2.</strong> 定义模块来控制作用域与私有性</a></li><li class="chapter-item expanded "><a href="ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html"><strong aria-hidden="true">7.3.</strong> 引用模块项目的路径</a></li><li class="chapter-item expanded "><a href="ch07-04-bringing-paths-into-scope-with-the-use-keyword.html"><strong aria-hidden="true">7.4.</strong> 使用 use 关键字将路径引入作用域</a></li><li class="chapter-item expanded "><a href="ch07-05-separating-modules-into-different-files.html"><strong aria-hidden="true">7.5.</strong> 将模块拆分成多个文件</a></li></ol></li><li class="chapter-item expanded "><a href="ch08-00-common-collections.html"><strong aria-hidden="true">8.</strong> 常见集合</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch08-01-vectors.html"><strong aria-hidden="true">8.1.</strong> 使用 Vector 储存列表</a></li><li class="chapter-item expanded "><a href="ch08-02-strings.html"><strong aria-hidden="true">8.2.</strong> 使用字符串储存 UTF-8 编码的文本</a></li><li class="chapter-item expanded "><a href="ch08-03-hash-maps.html"><strong aria-hidden="true">8.3.</strong> 使用 Hash Map 储存键值对</a></li></ol></li><li class="chapter-item expanded "><a href="ch09-00-error-handling.html"><strong aria-hidden="true">9.</strong> 错误处理</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch09-01-unrecoverable-errors-with-panic.html"><strong aria-hidden="true">9.1.</strong> 用 panic! 处理不可恢复的错误</a></li><li class="chapter-item expanded "><a href="ch09-02-recoverable-errors-with-result.html"><strong aria-hidden="true">9.2.</strong> 用 Result 处理可恢复的错误</a></li><li class="chapter-item expanded "><a href="ch09-03-to-panic-or-not-to-panic.html"><strong aria-hidden="true">9.3.</strong> 要不要 panic!</a></li></ol></li><li class="chapter-item expanded "><a href="ch10-00-generics.html"><strong aria-hidden="true">10.</strong> 泛型、Trait 和生命周期</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch10-01-syntax.html"><strong aria-hidden="true">10.1.</strong> 泛型数据类型</a></li><li class="chapter-item expanded "><a href="ch10-02-traits.html"><strong aria-hidden="true">10.2.</strong> Trait定义共同行为</a></li><li class="chapter-item expanded "><a href="ch10-03-lifetime-syntax.html"><strong aria-hidden="true">10.3.</strong> 生命周期确保引用有效</a></li></ol></li><li class="chapter-item expanded "><a href="ch11-00-testing.html"><strong aria-hidden="true">11.</strong> 编写自动化测试</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch11-01-writing-tests.html"><strong aria-hidden="true">11.1.</strong> 如何编写测试</a></li><li class="chapter-item expanded "><a href="ch11-02-running-tests.html"><strong aria-hidden="true">11.2.</strong> 控制测试如何运行</a></li><li class="chapter-item expanded "><a href="ch11-03-test-organization.html"><strong aria-hidden="true">11.3.</strong> 测试的组织结构</a></li></ol></li><li class="chapter-item expanded "><a href="ch12-00-an-io-project.html"><strong aria-hidden="true">12.</strong> 一个 I/O 项目:构建命令行程序</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch12-01-accepting-command-line-arguments.html"><strong aria-hidden="true">12.1.</strong> 接受命令行参数</a></li><li class="chapter-item expanded "><a href="ch12-02-reading-a-file.html"><strong aria-hidden="true">12.2.</strong> 读取文件</a></li><li class="chapter-item expanded "><a href="ch12-03-improving-error-handling-and-modularity.html"><strong aria-hidden="true">12.3.</strong> 重构以改进模块化与错误处理</a></li><li class="chapter-item expanded "><a href="ch12-04-testing-the-librarys-functionality.html"><strong aria-hidden="true">12.4.</strong> 采用测试驱动开发完善库的功能</a></li><li class="chapter-item expanded "><a href="ch12-05-working-with-environment-variables.html"><strong aria-hidden="true">12.5.</strong> 处理环境变量</a></li><li class="chapter-item expanded "><a href="ch12-06-writing-to-stderr-instead-of-stdout.html"><strong aria-hidden="true">12.6.</strong> 将错误信息输出到标准错误而不是标准输出</a></li></ol></li><li class="chapter-item expanded "><a href="ch13-00-functional-features.html"><strong aria-hidden="true">13.</strong> Rust 中的函数式语言功能:迭代器与闭包</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch13-01-closures.html"><strong aria-hidden="true">13.1.</strong> 闭包:可以捕获其环境的匿名函数</a></li><li class="chapter-item expanded "><a href="ch13-02-iterators.html"><strong aria-hidden="true">13.2.</strong> 使用迭代器处理元素序列</a></li><li class="chapter-item expanded "><a href="ch13-03-improving-our-io-project.html"><strong aria-hidden="true">13.3.</strong> 改进之前的 I/O 项目</a></li><li class="chapter-item expanded "><a href="ch13-04-performance.html"><strong aria-hidden="true">13.4.</strong> 性能比较:循环对迭代器</a></li></ol></li><li class="chapter-item expanded "><a href="ch14-00-more-about-cargo.html"><strong aria-hidden="true">14.</strong> 更多关于 Cargo 和 Crates.io 的内容</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch14-01-release-profiles.html"><strong aria-hidden="true">14.1.</strong> 采用发布配置自定义构建</a></li><li class="chapter-item expanded "><a href="ch14-02-publishing-to-crates-io.html"><strong aria-hidden="true">14.2.</strong> 将 crate 发布到 Crates.io</a></li><li class="chapter-item expanded "><a href="ch14-03-cargo-workspaces.html"><strong aria-hidden="true">14.3.</strong> Cargo 工作空间</a></li><li class="chapter-item expanded "><a href="ch14-04-installing-binaries.html"><strong aria-hidden="true">14.4.</strong> 使用 cargo install 安装二进制文件</a></li><li class="chapter-item expanded "><a href="ch14-05-extending-cargo.html"><strong aria-hidden="true">14.5.</strong> Cargo 自定义扩展命令</a></li></ol></li><li class="chapter-item expanded "><a href="ch15-00-smart-pointers.html"><strong aria-hidden="true">15.</strong> 智能指针</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch15-01-box.html"><strong aria-hidden="true">15.1.</strong> 使用 Box&lt;T&gt; 指向堆上数据</a></li><li class="chapter-item expanded "><a href="ch15-02-deref.html"><strong aria-hidden="true">15.2.</strong> 使用 Deref Trait 将智能指针当作常规引用处理</a></li><li class="chapter-item expanded "><a href="ch15-03-drop.html"><strong aria-hidden="true">15.3.</strong> 使用 Drop Trait 运行清理代码</a></li><li class="chapter-item expanded "><a href="ch15-04-rc.html"><strong aria-hidden="true">15.4.</strong> Rc&lt;T&gt; 引用计数智能指针</a></li><li class="chapter-item expanded "><a href="ch15-05-interior-mutability.html"><strong aria-hidden="true">15.5.</strong> RefCell&lt;T&gt; 与内部可变性模式</a></li><li class="chapter-item expanded "><a href="ch15-06-reference-cycles.html"><strong aria-hidden="true">15.6.</strong> 引用循环会导致内存泄漏</a></li></ol></li><li class="chapter-item expanded "><a href="ch16-00-concurrency.html"><strong aria-hidden="true">16.</strong> 无畏并发</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch16-01-threads.html"><strong aria-hidden="true">16.1.</strong> 使用线程同时地运行代码</a></li><li class="chapter-item expanded "><a href="ch16-02-message-passing.html"><strong aria-hidden="true">16.2.</strong> 使用消息传递在线程间通信</a></li><li class="chapter-item expanded "><a href="ch16-03-shared-state.html"><strong aria-hidden="true">16.3.</strong> 共享状态并发</a></li><li class="chapter-item expanded "><a href="ch16-04-extensible-concurrency-sync-and-send.html"><strong aria-hidden="true">16.4.</strong> 使用 Sync 与 Send Traits 的可扩展并发</a></li></ol></li><li class="chapter-item expanded "><a href="ch17-00-oop.html"><strong aria-hidden="true">17.</strong> Rust 的面向对象编程特性</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch17-01-what-is-oo.html"><strong aria-hidden="true">17.1.</strong> 面向对象语言的特点</a></li><li class="chapter-item expanded "><a href="ch17-02-trait-objects.html"><strong aria-hidden="true">17.2.</strong> 顾及不同类型值的 trait 对象</a></li><li class="chapter-item expanded "><a href="ch17-03-oo-design-patterns.html"><strong aria-hidden="true">17.3.</strong> 面向对象设计模式的实现</a></li></ol></li><li class="chapter-item expanded "><a href="ch18-00-patterns.html"><strong aria-hidden="true">18.</strong> 模式与模式匹配</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch18-01-all-the-places-for-patterns.html"><strong aria-hidden="true">18.1.</strong> 所有可能会用到模式的位置</a></li><li class="chapter-item expanded "><a href="ch18-02-refutability.html"><strong aria-hidden="true">18.2.</strong> Refutability可反驳性: 模式是否会匹配失效</a></li><li class="chapter-item expanded "><a href="ch18-03-pattern-syntax.html"><strong aria-hidden="true">18.3.</strong> 模式语法</a></li></ol></li><li class="chapter-item expanded "><a href="ch19-00-advanced-features.html"><strong aria-hidden="true">19.</strong> 高级特征</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch19-01-unsafe-rust.html"><strong aria-hidden="true">19.1.</strong> 不安全的 Rust</a></li><li class="chapter-item expanded "><a href="ch19-03-advanced-traits.html"><strong aria-hidden="true">19.2.</strong> 高级 trait</a></li><li class="chapter-item expanded "><a href="ch19-04-advanced-types.html"><strong aria-hidden="true">19.3.</strong> 高级类型</a></li><li class="chapter-item expanded "><a href="ch19-05-advanced-functions-and-closures.html"><strong aria-hidden="true">19.4.</strong> 高级函数与闭包</a></li><li class="chapter-item expanded "><a href="ch19-06-macros.html"><strong aria-hidden="true">19.5.</strong></a></li></ol></li><li class="chapter-item expanded "><a href="ch20-00-final-project-a-web-server.html"><strong aria-hidden="true">20.</strong> 最后的项目:构建多线程 web server</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="ch20-01-single-threaded.html" class="active"><strong aria-hidden="true">20.1.</strong> 建立单线程 web server</a></li><li class="chapter-item expanded "><a href="ch20-02-multithreaded.html"><strong aria-hidden="true">20.2.</strong> 将单线程 server 变为多线程 server</a></li><li class="chapter-item expanded "><a href="ch20-03-graceful-shutdown-and-cleanup.html"><strong aria-hidden="true">20.3.</strong> 优雅停机与清理</a></li></ol></li><li class="chapter-item expanded "><a href="appendix-00.html"><strong aria-hidden="true">21.</strong> 附录</a></li><li><ol class="section"><li class="chapter-item expanded "><a href="appendix-01-keywords.html"><strong aria-hidden="true">21.1.</strong> A - 关键字</a></li><li class="chapter-item expanded "><a href="appendix-02-operators.html"><strong aria-hidden="true">21.2.</strong> B - 运算符与符号</a></li><li class="chapter-item expanded "><a href="appendix-03-derivable-traits.html"><strong aria-hidden="true">21.3.</strong> C - 可派生的 trait</a></li><li class="chapter-item expanded "><a href="appendix-04-useful-development-tools.html"><strong aria-hidden="true">21.4.</strong> D - 实用开发工具</a></li><li class="chapter-item expanded "><a href="appendix-05-editions.html"><strong aria-hidden="true">21.5.</strong> E - 版本</a></li><li class="chapter-item expanded "><a href="appendix-06-translation.html"><strong aria-hidden="true">21.6.</strong> F - 本书译本</a></li><li class="chapter-item expanded "><a href="appendix-07-nightly-rust.html"><strong aria-hidden="true">21.7.</strong> G - Rust 是如何开发的与 “Nightly Rust”</a></li></ol></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Rust 程序设计语言 简体中文版</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/KaiserY/trpl-zh-cn/tree/main" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/KaiserY/trpl-zh-cn/edit/main/src/ch20-01-single-threaded.md" title="Suggest an edit" aria-label="Suggest an edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h2 id="构建单线程-web-server"><a class="header" href="#构建单线程-web-server">构建单线程 web server</a></h2>
<blockquote>
<p><a href="https://github.com/rust-lang/book/blob/main/src/ch20-01-single-threaded.md">ch20-01-single-threaded.md</a>
<br>
commit 5df6909c57b3ba55f156a4122a42b805436de90c</p>
</blockquote>
<p>首先让我们创建一个可运行的单线程 web server不过在开始之前我们将快速了解一下构建 web server 所涉及到的协议。这些协议的细节超出了本书的范畴,不过一个简单的概括会提供我们所需的信息。</p>
<p>web server 中涉及到的两个主要协议是 <strong>超文本传输协议</strong><em>Hypertext Transfer Protocol</em><em>HTTP</em>)和 <strong>传输控制协议</strong><em>Transmission Control Protocol</em><em>TCP</em>)。这两者都是 <strong>请求 - 响应</strong><em>request-response</em>)协议,也就是说,有 <strong>客户端</strong><em>client</em>)来初始化请求,并有 <strong>服务端</strong><em>server</em>)监听请求并向客户端提供响应。请求与响应的内容由协议本身定义。</p>
<p>TCP 是一个底层协议,它描述了信息如何从一个 server 到另一个的细节不过其并不指定信息是什么。HTTP 构建于 TCP 之上,它定义了请求和响应的内容。为此,技术上讲可将 HTTP 用于其他协议之上不过对于绝大部分情况HTTP 通过 TCP 传输。我们将要做的就是处理 TCP 和 HTTP 请求与响应的原始字节数据。</p>
<h3 id="监听-tcp-连接"><a class="header" href="#监听-tcp-连接">监听 TCP 连接</a></h3>
<p>我们的 web server 所需做的第一件事,是监听 TCP 连接。标准库提供了 <code>std::net</code> 模块处理这些功能。让我们一如既往新建一个项目:</p>
<pre><code class="language-console">$ cargo new hello
Created binary (application) `hello` project
$ cd hello
</code></pre>
<p>现在,在 <code>src/main.rs</code> 输入示例 20-1 中的代码,作为一个开始。这段代码会在地址 <code>127.0.0.1:7878</code> 上监听传入的 TCP 流。当获取到传入的流,它会打印出 <code>Connection established!</code></p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><pre class="playground"><code class="language-rust no_run">use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind(&quot;127.0.0.1:7878&quot;).unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
println!(&quot;Connection established!&quot;);
}
}</code></pre></pre>
<p><span class="caption">示例 20-1: 监听传入的流并在接收到流时打印信息</span></p>
<p><code>TcpListener</code> 用于监听 TCP 连接。我们选择监听本地地址 <code>127.0.0.1:7878</code>。将这个地址拆开来看,冒号之前的部分是一个代表本机的 IP 地址(在每台计算机上,这个地址都指本机,并不特指作者的计算机),而 <code>7878</code> 是端口。选择这个端口出于两个原因:通常 HTTP 服务器不在这个端口上接受请求,所以它不太可能与你机器上运行的其它 web server 的端口冲突;而且 7878 在电话上打出来就是 &quot;rust&quot;(译者注:九宫格键盘上的英文)。</p>
<p>在这个场景中 <code>bind</code> 函数类似于 <code>new</code> 函数,在这里它返回一个新的 <code>TcpListener</code> 实例。这个函数叫做 <code>bind</code> 是因为在网络领域连接到要监听的端口称为“绑定到端口”“binding to a port”</p>
<p><code>bind</code> 函数返回 <code>Result&lt;T, E&gt;</code>,这表明绑定可能会失败。例如,监听 80 端口需要管理员权限(非管理员用户只能监听大于 1023 的端口),所以如果尝试监听 80 端口而没有管理员权限,则会绑定失败。再比如,如果我们运行这个程序的两个实例,并因此有两个实例监听同一个端口,那么绑定也将失败。我们是出于学习目的来编写一个基础的服务器,不用关心处理这类错误,而仅仅使用 <code>unwrap</code> 在出现这些情况时直接停止程序。</p>
<p><code>TcpListener</code><code>incoming</code> 方法返回一个迭代器,它提供了一系列的流(更准确的说是 <code>TcpStream</code> 类型的流)。<strong></strong><em>stream</em>)代表一个客户端和服务端之间打开的连接。<strong>连接</strong><em>connection</em>)代表客户端连接服务端、服务端生成响应以及服务端关闭连接的全部请求 / 响应过程。为此,我们会从 <code>TcpStream</code> 读取客户端发送了什么并接着向流发送响应以向客户端发回数据。总体来说,这个 <code>for</code> 循环会依次处理每个连接并产生一系列的流供我们处理。</p>
<p>目前,处理流的代码中也有一个 <code>unwrap</code> 调用,如果 <code>stream</code> 出现任何错误会终止程序;如果没有任何错误,则打印出信息。下一个例子中,我们将为成功的情况增加更多功能。当客户端连接到服务端时,<code>incoming</code> 方法是可能返回错误的,因为我们实际上不是在遍历连接,而是遍历 <strong>连接尝试</strong><em>connection attempts</em>)。连接的尝试可能会因为多种原因不能成功,大部分是操作系统相关的。例如,很多系统限制同时打开的连接数,超出数量限制的新连接尝试会产生错误,直到一些现有的连接关闭为止。</p>
<p>让我们试试这段代码!首先在终端执行 <code>cargo run</code>,接着在浏览器中打开 <code>127.0.0.1:7878</code>。浏览器会显示出看起来类似于“连接重置”“Connection reset”的错误信息因为 server 目前并没响应任何数据。如果我们观察终端,会发现当浏览器连接我们的服务端时,会打印出一系列的信息!</p>
<pre><code class="language-text"> Running `target/debug/hello`
Connection established!
Connection established!
Connection established!
</code></pre>
<p>有时,对于一次浏览器请求,可能会打印出多条信息;这可能是因为,浏览器在请求页面的同时,还请求了其他资源,比如出现在浏览器标签页开头的图标(<em>favicon.ico</em>)。</p>
<p>这也可能是因为浏览器尝试多次连接服务端,因为服务端没有响应任何数据。作为 <code>drop</code> 实现的一部分,当 <code>stream</code> 在循环的结尾离开作用域并被丢弃,其连接将被关闭。浏览器有时通过重连来处理关闭的连接,因为对于一般网站而言,这些问题可能是暂时的。这些都不重要;现在重要的是,我们成功的处理了 TCP 连接!</p>
<p>记得当运行完特定版本的代码后,使用 <span class="keystroke">ctrl-C</span> 来停止程序。并通过执行 <code>cargo run</code> 命令在做出最新的代码修改之后重启服务。</p>
<h3 id="读取请求"><a class="header" href="#读取请求">读取请求</a></h3>
<p>让我们实现读取来自浏览器请求的功能!为了分离“获取连接”以及“接下来对连接的操作”,我们将开始写一个新函数来处理连接。在这个新的 <code>handle_connection</code> 函数中,我们从 TCP 流中读取数据,并打印出来,以便观察浏览器发送过来的数据。将代码修改为如示例 20-2 所示:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><pre class="playground"><code class="language-rust no_run">use std::{
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
};
fn main() {
let listener = TcpListener::bind(&quot;127.0.0.1:7878&quot;).unwrap();
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&amp;mut stream);
let http_request: Vec&lt;_&gt; = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
println!(&quot;Request: {http_request:#?}&quot;);
}</code></pre></pre>
<p><span class="caption">示例 20-2: 读取 <code>TcpStream</code> 并打印数据</span></p>
<p>这里将 <code>std::io::prelude</code><code>std::io::BufReader</code> 引入作用域,来获取读写流所需的特定 trait。在 <code>main</code> 函数的 <code>for</code> 循环中,相比获取到连接时打印信息,现在调用新的 <code>handle_connection</code> 函数并向其传递 <code>stream</code></p>
<p><code>handle_connection</code> 中,我们新建了一个 <code>BufReader</code> 实例来封装一个 <code>stream</code> 的可变引用。<code>BufReader</code> 增加了缓存来替我们管理 <code>std::io::Read</code> trait 方法的调用。</p>
<p>我们创建了一个 <code>http_request</code> 变量来收集浏览器发送给服务端的请求行。这里增加了 <code>Vec&lt;_&gt;</code> 类型注解表明希望将这些行收集到一个 vector 中。</p>
<p><code>BufReader</code> 实现了 <code>std::io::BufRead</code> trait它提供了 <code>lines</code> 方法。<code>lines</code> 方法通过遇到换行符newline字节就切分数据流的方式返回一个 <code>Result&lt;String, std::io::Error&gt;</code> 的迭代器。为了获取每一个 <code>String</code>,通过 map 并 <code>unwrap</code> 每一个 <code>Result</code>。如果数据不是有效的 UTF-8 编码或者读取流遇到问题时,<code>Result</code> 可能是一个错误。一如既往生产环境的程序应该更优雅地处理这些错误,不过出于简单的目的我们选择在错误情况下停止程序。</p>
<p>浏览器通过连续发送两个换行符来代表一个 HTTP 请求的结束,所以为了从流中获取一个请求,我们获取行直到它们不为空。一旦将这些行收集进 vector就可以使用友好的 debug 格式化打印它们,以便看看 web 浏览器发送给服务端的指令。</p>
<p>让我们试一试!启动程序并再次在浏览器中发起请求。注意浏览器中仍然会出现错误页面,不过终端中程序的输出现在看起来像这样:</p>
<pre><code class="language-console">$ cargo run
Compiling hello v0.1.0 (file:///projects/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
Running `target/debug/hello`
Request: [
&quot;GET / HTTP/1.1&quot;,
&quot;Host: 127.0.0.1:7878&quot;,
&quot;User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0&quot;,
&quot;Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8&quot;,
&quot;Accept-Language: en-US,en;q=0.5&quot;,
&quot;Accept-Encoding: gzip, deflate, br&quot;,
&quot;DNT: 1&quot;,
&quot;Connection: keep-alive&quot;,
&quot;Upgrade-Insecure-Requests: 1&quot;,
&quot;Sec-Fetch-Dest: document&quot;,
&quot;Sec-Fetch-Mode: navigate&quot;,
&quot;Sec-Fetch-Site: none&quot;,
&quot;Sec-Fetch-User: ?1&quot;,
&quot;Cache-Control: max-age=0&quot;,
]
</code></pre>
<p>根据使用的浏览器不同可能会出现稍微不同的数据。现在我们打印出了请求数据,可以通过观察第一行 <code>GET</code> 之后的路径来解释为何会从浏览器得到多个连接。如果重复的连接都是请求 <em>/</em>,就知道了浏览器尝试重复获取 <em>/</em> 因为它没有从程序得到响应。</p>
<p>让我们拆开请求数据来理解浏览器向程序请求了什么。</p>
<h4 id="仔细观察-http-请求"><a class="header" href="#仔细观察-http-请求">仔细观察 HTTP 请求</a></h4>
<p>HTTP 是一个基于文本的协议,同时一个请求有如下格式:</p>
<pre><code class="language-text">Method Request-URI HTTP-Version CRLF
headers CRLF
message-body
</code></pre>
<p>第一行叫做 <strong>请求行</strong><em>request line</em>),它存放了客户端请求了什么的信息。请求行的第一部分是所使用的 <em>method</em>,比如 <code>GET</code><code>POST</code>,这描述了客户端如何进行请求。这里客户端使用了 <code>GET</code> 请求,表明它在请求信息。</p>
<p>请求行接下来的部分是 <em>/</em>,它代表客户端请求的 <strong>统一资源标识符</strong><em>Uniform Resource Identifier</em><em>URI</em> —— URI 大体上类似,但也不完全类似于 URL<strong>统一资源定位符</strong><em>Uniform Resource Locators</em>。URI 和 URL 之间的区别对于本章的目的来说并不重要,不过 HTTP 规范使用术语 URI所以这里可以简单的将 URL 理解为 URI。</p>
<p>最后一部分是客户端使用的 HTTP 版本,然后请求行以 <strong>CRLF 序列</strong> CRLF 代表回车和换行,<em>carriage return line feed</em>这是打字机时代的术语结束。CRLF 序列也可以写成<code>\r\n</code>,其中<code>\r</code>是回车符,<code>\n</code>是换行符。CRLF 序列将请求行与其余请求数据分开。请注意,打印 CRLF 时,我们会看到一个新行,而不是<code>\r\n</code></p>
<p>观察目前运行程序所接收到的数据的请求行,可以看到 <code>GET</code> 是 method<em>/</em> 是请求 URI<code>HTTP/1.1</code> 是版本。</p>
<p><code>Host:</code> 开始的其余的行是 headers<code>GET</code> 请求没有 body。</p>
<p>如果你希望的话,尝试用不同的浏览器发送请求,或请求不同的地址,比如 <code>127.0.0.1:7878/test</code>,来观察请求数据如何变化。</p>
<p>现在我们知道了浏览器请求了什么。让我们返回一些数据!</p>
<h3 id="编写响应"><a class="header" href="#编写响应">编写响应</a></h3>
<p>我们将实现在客户端请求的响应中发送数据的功能。响应有如下格式:</p>
<pre><code class="language-text">HTTP-Version Status-Code Reason-Phrase CRLF
headers CRLF
message-body
</code></pre>
<p>第一行叫做 <strong>状态行</strong><em>status line</em>),它包含响应的 HTTP 版本、一个数字状态码用以总结请求的结果和一个描述之前状态码的文本原因短语。CRLF 序列之后是任意 header另一个 CRLF 序列,和响应的 body。</p>
<p>这里是一个使用 HTTP 1.1 版本的响应例子,其状态码为 200原因短语为 OK没有 header也没有 body</p>
<pre><code class="language-text">HTTP/1.1 200 OK\r\n\r\n
</code></pre>
<p>状态码 200 是一个标准的成功响应。这些文本是一个微型的成功 HTTP 响应。让我们将这些文本写入流作为成功请求的响应!在 <code>handle_connection</code> 函数中,我们需要去掉打印请求数据的 <code>println!</code>,并替换为示例 20-3 中的代码:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><pre class="playground"><code class="language-rust no_run"><span class="boring">use std::{
</span><span class="boring"> io::{prelude::*, BufReader},
</span><span class="boring"> net::{TcpListener, TcpStream},
</span><span class="boring">};
</span><span class="boring">
</span><span class="boring">fn main() {
</span><span class="boring"> let listener = TcpListener::bind(&quot;127.0.0.1:7878&quot;).unwrap();
</span><span class="boring">
</span><span class="boring"> for stream in listener.incoming() {
</span><span class="boring"> let stream = stream.unwrap();
</span><span class="boring">
</span><span class="boring"> handle_connection(stream);
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span>fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&amp;mut stream);
let http_request: Vec&lt;_&gt; = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
let response = &quot;HTTP/1.1 200 OK\r\n\r\n&quot;;
stream.write_all(response.as_bytes()).unwrap();
}</code></pre></pre>
<p><span class="caption">示例 20-3: 将一个微型成功 HTTP 响应写入流</span></p>
<p>新代码中的第一行定义了变量 <code>response</code> 来存放将要返回的成功响应的数据。接着,在 <code>response</code> 上调用 <code>as_bytes</code>,因为 <code>stream</code><code>write_all</code> 方法获取一个 <code>&amp;[u8]</code> 并直接将这些字节发送给连接。因为 <code>write_all</code> 操作可能会失败,所以像之前那样对任何错误结果使用 <code>unwrap</code>。同理,在真实世界的应用中这里需要添加错误处理。</p>
<p>有了这些修改,运行我们的代码并进行请求!我们不再向终端打印任何数据,所以不会再看到除了 Cargo 以外的任何输出。不过当在浏览器中加载 <em>127.0.0.1:7878</em> 时,会得到一个空页面而不是错误。太棒了!我们刚刚手写收发了一个 HTTP 请求与响应。</p>
<h3 id="返回真正的-html"><a class="header" href="#返回真正的-html">返回真正的 HTML</a></h3>
<p>让我们实现不只是返回空页面的功能。在项目根目录创建一个新文件,<em>hello.html</em> —— 也就是说,不是在 <code>src</code> 目录。在此可以放入任何你期望的 HTML列表 20-4 展示了一个可能的文本:</p>
<p><span class="filename">文件名hello.html</span></p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Hello!&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Hello!&lt;/h1&gt;
&lt;p&gt;Hi from Rust&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><span class="caption">示例 20-4: 一个简单的 HTML 文件用来作为响应</span></p>
<p>这是一个极小化的 HTML5 文档,它有一个标题和一小段文本。为了在 server 接受请求时返回它,需要如示例 20-5 所示修改 <code>handle_connection</code> 来读取 HTML 文件,将其加入到响应的 body 中,并发送:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><pre class="playground"><code class="language-rust no_run">use std::{
fs,
io::{prelude::*, BufReader},
net::{TcpListener, TcpStream},
};
// --snip--
<span class="boring">fn main() {
</span><span class="boring"> let listener = TcpListener::bind(&quot;127.0.0.1:7878&quot;).unwrap();
</span><span class="boring">
</span><span class="boring"> for stream in listener.incoming() {
</span><span class="boring"> let stream = stream.unwrap();
</span><span class="boring">
</span><span class="boring"> handle_connection(stream);
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span>fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&amp;mut stream);
let http_request: Vec&lt;_&gt; = buf_reader
.lines()
.map(|result| result.unwrap())
.take_while(|line| !line.is_empty())
.collect();
let status_line = &quot;HTTP/1.1 200 OK&quot;;
let contents = fs::read_to_string(&quot;hello.html&quot;).unwrap();
let length = contents.len();
let response =
format!(&quot;{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}&quot;);
stream.write_all(response.as_bytes()).unwrap();
}</code></pre></pre>
<p><span class="caption">示例 20-5: 将 <em>hello.html</em> 的内容作为响应 body 发送</span></p>
<p>我们在开头 <code>use</code> 语句将标准库的文件系统模块 <code>fs</code> 引入作用域。打开和读取文件的代码应该看起来很熟悉,因为第十二章 I/O 项目的示例 12-4 中读取文件内容时出现过类似的代码。</p>
<p>接下来,使用 <code>format!</code> 将文件内容加入到将要写入流的成功响应的 body 中。</p>
<p>使用 <code>cargo run</code> 运行程序,在浏览器加载 <em>127.0.0.1:7878</em>,你应该会看到渲染出来的 HTML 文件!</p>
<p>目前忽略了 <code>http_request</code> 中的请求数据并无条件的发送了 HTML 文件的内容。这意味着如果尝试在浏览器中请求 <em>127.0.0.1:7878/something-else</em> 也会得到同样的 HTML 响应。目前我们的 server 的作用是非常有限的,也不是大部分 server 所做的让我们检查请求并只对格式良好well-formed的请求 <code>/</code> 发送 HTML 文件。</p>
<h3 id="验证请求并有选择的进行响应"><a class="header" href="#验证请求并有选择的进行响应">验证请求并有选择的进行响应</a></h3>
<p>目前我们的 web server 不管客户端请求什么都会返回相同的 HTML 文件。让我们增加在返回 HTML 文件前检查浏览器是否请求 <em>/</em>,并在其请求任何其他内容时返回错误的功能。为此需要如示例 20-6 那样修改 <code>handle_connection</code>。新代码接收到的请求的内容与已知的 <em>/</em> 请求的一部分做比较,并增加了 <code>if</code><code>else</code> 块来区别处理请求:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><pre class="playground"><code class="language-rust no_run"><span class="boring">use std::{
</span><span class="boring"> fs,
</span><span class="boring"> io::{prelude::*, BufReader},
</span><span class="boring"> net::{TcpListener, TcpStream},
</span><span class="boring">};
</span><span class="boring">
</span><span class="boring">fn main() {
</span><span class="boring"> let listener = TcpListener::bind(&quot;127.0.0.1:7878&quot;).unwrap();
</span><span class="boring">
</span><span class="boring"> for stream in listener.incoming() {
</span><span class="boring"> let stream = stream.unwrap();
</span><span class="boring">
</span><span class="boring"> handle_connection(stream);
</span><span class="boring"> }
</span><span class="boring">}
</span>// --snip--
fn handle_connection(mut stream: TcpStream) {
let buf_reader = BufReader::new(&amp;mut stream);
let request_line = buf_reader.lines().next().unwrap().unwrap();
if request_line == &quot;GET / HTTP/1.1&quot; {
let status_line = &quot;HTTP/1.1 200 OK&quot;;
let contents = fs::read_to_string(&quot;hello.html&quot;).unwrap();
let length = contents.len();
let response = format!(
&quot;{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}&quot;
);
stream.write_all(response.as_bytes()).unwrap();
} else {
// some other request
}
}</code></pre></pre>
<p><span class="caption">示例 20-6: 以不同于其它请求的方式处理 <em>/</em> 请求</span></p>
<p>我们只看 HTTP 请求的第一行,所以不同于将整个请求读取进 vector 中,这里调用 <code>next</code> 从迭代器中获取第一项。第一个 <code>unwrap</code> 负责处理 <code>Option</code> 并在迭代器没有项时停止程序。第二个 <code>unwrap</code> 处理 <code>Result</code> 并与示例 20-2 中增加的 <code>map</code> 中的 <code>unwrap</code> 有着相同的效果。</p>
<p>接下来检查 <code>request_line</code> 是否等于一个 <em>/</em> 路径的 GET 请求。如果是,<code>if</code> 代码块返回 HTML 文件的内容。</p>
<p>如果 <code>request_line</code> <strong></strong> 等于一个 <em>/</em> 路径的 GET 请求,就说明接收的是其他请求。我们之后会在 <code>else</code> 块中增加代码来响应所有其他请求。</p>
<p>现在如果运行代码并请求 <em>127.0.0.1:7878</em>,就会得到 <em>hello.html</em> 中的 HTML。如果进行任何其他请求比如 <em>127.0.0.1:7878/something-else</em>,则会得到像运行示例 20-1 和 20-2 中代码那样的连接错误。</p>
<p>现在向示例 20-7 的 <code>else</code> 块增加代码来返回一个带有 404 状态码的响应,这代表了所请求的内容没有找到。接着也会返回一个 HTML 向浏览器终端用户表明此意:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><pre class="playground"><code class="language-rust no_run"><span class="boring">use std::{
</span><span class="boring"> fs,
</span><span class="boring"> io::{prelude::*, BufReader},
</span><span class="boring"> net::{TcpListener, TcpStream},
</span><span class="boring">};
</span><span class="boring">
</span><span class="boring">fn main() {
</span><span class="boring"> let listener = TcpListener::bind(&quot;127.0.0.1:7878&quot;).unwrap();
</span><span class="boring">
</span><span class="boring"> for stream in listener.incoming() {
</span><span class="boring"> let stream = stream.unwrap();
</span><span class="boring">
</span><span class="boring"> handle_connection(stream);
</span><span class="boring"> }
</span><span class="boring">}
</span><span class="boring">
</span><span class="boring">fn handle_connection(mut stream: TcpStream) {
</span><span class="boring"> let buf_reader = BufReader::new(&amp;mut stream);
</span><span class="boring"> let request_line = buf_reader.lines().next().unwrap().unwrap();
</span><span class="boring">
</span><span class="boring"> if request_line == &quot;GET / HTTP/1.1&quot; {
</span><span class="boring"> let status_line = &quot;HTTP/1.1 200 OK&quot;;
</span><span class="boring"> let contents = fs::read_to_string(&quot;hello.html&quot;).unwrap();
</span><span class="boring"> let length = contents.len();
</span><span class="boring">
</span><span class="boring"> let response = format!(
</span><span class="boring"> &quot;{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}&quot;
</span><span class="boring"> );
</span><span class="boring">
</span><span class="boring"> stream.write_all(response.as_bytes()).unwrap();
</span> // --snip--
} else {
let status_line = &quot;HTTP/1.1 404 NOT FOUND&quot;;
let contents = fs::read_to_string(&quot;404.html&quot;).unwrap();
let length = contents.len();
let response = format!(
&quot;{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}&quot;
);
stream.write_all(response.as_bytes()).unwrap();
}
<span class="boring">}</span></code></pre></pre>
<p><span class="caption">示例 20-7: 对于任何不是 <em>/</em> 的请求返回 <code>404</code> 状态码的响应和错误页面</span></p>
<p>这里,响应的状态行有状态码 404 和原因短语 <code>NOT FOUND</code>。仍然没有返回任何 header而其 body 将是 <em>404.html</em> 文件中的 HTML。需要在 <em>hello.html</em> 同级目录创建 <em>404.html</em> 文件作为错误页面;这一次也可以随意使用任何 HTML 或使用示例 20-8 中的示例 HTML</p>
<p><span class="filename">文件名404.html</span></p>
<pre><code class="language-html">&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Hello!&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Oops!&lt;/h1&gt;
&lt;p&gt;Sorry, I don't know what you're asking for.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p><span class="caption">示例 20-8: 任何 404 响应所返回错误页面内容样例</span></p>
<p>有了这些修改,再次运行 server。请求 <em>127.0.0.1:7878</em> 应该会返回 <em>hello.html</em> 的内容,而对于任何其他请求,比如 <em>127.0.0.1:7878/foo</em>,应该会返回 <em>404.html</em> 中的错误 HTML</p>
<h3 id="少量代码重构"><a class="header" href="#少量代码重构">少量代码重构</a></h3>
<p>目前 <code>if</code><code>else</code> 块中的代码有很多的重复:他们都读取文件并将其内容写入流。唯一的区别是状态行和文件名。为了使代码更为简明,将这些区别分别提取到一行 <code>if</code><code>else</code> 中,对状态行和文件名变量赋值;然后在读取文件和写入响应的代码中无条件的使用这些变量。重构后取代了大段 <code>if</code><code>else</code> 块代码后的结果如示例 20-9 所示:</p>
<p><span class="filename">文件名src/main.rs</span></p>
<pre><pre class="playground"><code class="language-rust no_run"><span class="boring">use std::{
</span><span class="boring"> fs,
</span><span class="boring"> io::{prelude::*, BufReader},
</span><span class="boring"> net::{TcpListener, TcpStream},
</span><span class="boring">};
</span><span class="boring">
</span><span class="boring">fn main() {
</span><span class="boring"> let listener = TcpListener::bind(&quot;127.0.0.1:7878&quot;).unwrap();
</span><span class="boring">
</span><span class="boring"> for stream in listener.incoming() {
</span><span class="boring"> let stream = stream.unwrap();
</span><span class="boring">
</span><span class="boring"> handle_connection(stream);
</span><span class="boring"> }
</span><span class="boring">}
</span>// --snip--
fn handle_connection(mut stream: TcpStream) {
// --snip--
<span class="boring"> let buf_reader = BufReader::new(&amp;mut stream);
</span><span class="boring"> let request_line = buf_reader.lines().next().unwrap().unwrap();
</span>
let (status_line, filename) = if request_line == &quot;GET / HTTP/1.1&quot; {
(&quot;HTTP/1.1 200 OK&quot;, &quot;hello.html&quot;)
} else {
(&quot;HTTP/1.1 404 NOT FOUND&quot;, &quot;404.html&quot;)
};
let contents = fs::read_to_string(filename).unwrap();
let length = contents.len();
let response =
format!(&quot;{status_line}\r\nContent-Length: {length}\r\n\r\n{contents}&quot;);
stream.write_all(response.as_bytes()).unwrap();
}</code></pre></pre>
<p><span class="caption">示例 20-9: 重构使得 <code>if</code><code>else</code> 块中只包含两个情况所不同的代码</span></p>
<p>现在 <code>if</code><code>else</code> 块所做的唯一的事就是在一个元组中返回合适的状态行和文件名的值;接着使用第十八章讲到的使用模式的 <code>let</code> 语句通过解构元组的两部分为 <code>filename</code><code>header</code> 赋值。</p>
<p>之前读取文件和写入响应的冗余代码现在位于 <code>if</code><code>else</code> 块之外,并会使用变量 <code>status_line</code><code>filename</code>。这样更易于观察这两种情况真正有何不同,还意味着如果需要改变如何读取文件或写入响应时只需要更新一处的代码。示例 20-9 中代码的行为与示例 20-8 完全一样。</p>
<p>好极了!我们有了一个 40 行左右 Rust 代码的小而简单的 server它对一个请求返回页面内容而对所有其他请求返回 404 响应。</p>
<p>目前 server 运行于单线程中,它一次只能处理一个请求。让我们模拟一些慢请求来看看这如何会成为一个问题,并进行修复以便 server 可以一次处理多个请求。</p>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="ch20-00-final-project-a-web-server.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="ch20-02-multithreaded.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="ch20-00-final-project-a-web-server.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="ch20-02-multithreaded.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
<script src="ferris.js"></script>
</div>
</body>
</html>